2 #define _FILE_OFFSET_BITS 64
18 #include <sys/ioctl.h>
26 #include <dvdread/dvd_reader.h>
27 #include <dvdread/dvd_udf.h>
28 #include <dvdread/ifo_read.h>
29 #include <dvdread/ifo_types.h>
31 #define CTYPE_HACK(fn, ch) fn((unsigned char)(ch))
32 #define ISDIGIT(ch) CTYPE_HACK(isdigit, ch)
33 #define ISSPACE(ch) CTYPE_HACK(isspace, ch)
41 #define N(v) (sizeof(v)/sizeof((v)[0]))
44 #define SECTORS(n) (((n) + (SECTORSZ - 1))/SECTORSZ)
46 static const char *prog = "<unset>";
47 static int status = 0;
49 static void usage(FILE *fp)
52 "usage: %s [-c] [-R MAP] [-b OUTMAP] [-r [START]-[END]]\n"
57 static void vmoan(const char *fmt, va_list ap)
58 { fprintf(stderr, "%s: ", prog); vfprintf(stderr, fmt, ap); }
60 __attribute__((format(printf, 1, 2)))
61 static void moan(const char *fmt, ...)
65 va_start(ap, fmt); vmoan(fmt, ap); va_end(ap);
69 __attribute__((noreturn, format(printf, 1, 2)))
70 static void bail(const char *fmt, ...)
74 va_start(ap, fmt); vmoan(fmt, ap); va_end(ap);
79 __attribute__((noreturn, format(printf, 2, 3)))
80 static void bail_syserr(int err, const char *fmt, ...)
84 va_start(ap, fmt); vmoan(fmt, ap); va_end(ap);
85 if (err) fprintf(stderr, ": %s", strerror(errno));
90 static void carefully_write(int fd, const void *buf, size_t sz)
92 const unsigned char *p = buf;
99 if (errno == EINTR) continue;
100 bail_syserr(errno, "failed to write to output file");
102 if (!n) bail("unexpected short write to output file");
107 #define DEFVEC(vtype, etype) \
108 typedef struct { etype *v; size_t n, sz; } vtype
109 #define VEC_INIT { 0, 0, 0 }
110 #define VEC_FREE(vv) do { \
111 free((vv)->v); (vv)->v 0; (vv)->n = (vv)->sz = 0; \
113 #define VEC_PUSH(p, vv) do { \
115 if ((vv)->n >= (vv)->sz) { \
116 (vv)->sz = (vv)->sz ? 2*(vv)->sz : 32; \
117 _want = (vv)->sz*sizeof(*(vv)->v); \
118 (vv)->v = realloc((vv)->v, _want); \
119 if (!(vv)->v) bail("out of memory allocating %zu bytes", _want); \
121 (p) = &(vv)->v[(vv)->n++]; \
124 enum { RAW, IFO, VOB, BUP };
125 typedef uint_least32_t ident;
127 static inline ident mkident(unsigned kind, unsigned title, unsigned part)
128 { return (((ident)kind << 0) | ((ident)title << 8) | ((ident)part << 16)); }
129 static inline unsigned id_kind(ident id) { return ((id >> 0)&0x0ff); }
130 static inline unsigned id_title(ident id) { return ((id >> 8)&0x0ff); }
131 static inline unsigned id_part(ident id) { return ((id >> 16)&0x0ff); }
133 #define MAXFNSZ (1 + 8 + 1 + 12 + 1)
135 static void store_filename(char *buf, ident id)
137 switch (id_kind(id)) {
139 sprintf(buf, "#<raw device>");
142 if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.IFO");
143 else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.IFO", id_title(id));
146 if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.BUP");
147 else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.BUP", id_title(id));
150 if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.VOB");
152 sprintf(buf, "/VIDEO_TS/VTS_%02u_%u.VOB", id_title(id), id_part(id));
159 typedef uint_least32_t secaddr;
160 #define PRIuSEC PRIuLEAST32
161 #define SECLIMIT 0x00400000
163 #define MAXFILES (1 + 2*99 + 1)
168 DEFVEC(file_v, struct file);
169 static file_v filetab = VEC_INIT;
171 enum { EV_STOP, EV_BEGIN, EV_END, EV_WRITE };
173 unsigned char ev, file;
176 DEFVEC(event_v, struct event);
177 static event_v eventq = VEC_INIT;
179 static int compare_event(const void *a, const void *b)
181 const struct event *eva = a, *evb = b;
183 if (eva->pos < evb->pos) return (-1);
184 else if (eva->pos > evb->pos) return (+1);
186 if (eva->ev < evb->ev) return (-1);
187 else if (eva->ev > evb->ev) return (+1);
189 if (eva->file < evb->file) return (-1);
190 else if (eva->file > evb->file) return (+1);
195 typedef uint_least32_t bits;
196 static bits live[(MAXFILES + 31)/32];
198 static inline int livep(unsigned i)
199 { return (live[i/32]&((bits)1 << (i%32))); }
200 static inline void set_live(unsigned i)
201 { live[i/32] |= (bits)1 << (i%32); }
202 static inline void clear_live(unsigned i)
203 { live[i/32] &= ~((bits)1 << (i%32)); }
204 static inline int least_live(void)
206 unsigned i, n = (filetab.n + 32)/32;
209 for (i = 0; i < n; i++) { b = live[i]; if (b) goto found; }
213 if (!(b&0x0000ffff)) { b >>= 16; i += 16; }
214 if (!(b&0x000000ff)) { b >>= 8; i += 8; }
215 if (!(b&0x0000000f)) { b >>= 4; i += 4; }
216 if (!(b&0x00000003)) { b >>= 2; i += 2; }
217 if (!(b&0x00000001)) { b >>= 1; i += 1; }
222 static void put_event(unsigned evtype, unsigned file, secaddr pos)
226 VEC_PUSH(ev, &eventq);
227 ev->ev = evtype; ev->file = file; ev->pos = pos;
230 static void put_file(ident id, secaddr start, secaddr end)
235 VEC_PUSH(f, &filetab); i = f - filetab.v;
236 f->id = id; f->start = start; f->end = end;
237 put_event(EV_BEGIN, i, start);
238 put_event(EV_END, i, end);
241 static void put_menu(dvd_reader_t *dvd, unsigned title)
243 ident id = mkident(VOB, title, 0);
247 store_filename(fn, id);
248 start = UDFFindFile(dvd, fn, &len); if (!start) return;
250 printf(";; %8"PRIuSEC" .. %-8"PRIuSEC": %s\n",
251 start, start + SECTORS(len), fn);
253 put_file(id, start, start + SECTORS(len));
256 static void put_title(dvd_reader_t *dvd, unsigned title)
259 secaddr start[9], len[9];
262 for (i = 0; i < 9; i++) {
263 store_filename(fn, mkident(VOB, title, i + 1));
264 start[i] = UDFFindFile(dvd, fn, &len[i]); if (!start[i]) break;
266 npart = i; if (!npart) return;
269 for (i = 0; i < npart; i++) {
270 store_filename(fn, mkident(VOB, title, i + 1));
271 printf(";; %8"PRIuSEC" .. %-8"PRIuSEC": %s\n",
272 start[i], start[i] + SECTORS(len[i]), fn);
277 for (i = 0; i < npart - 1; i++) {
279 bail("title %u part %u length = %"PRIuSEC" not a multiple of %d",
280 title, i, len[i], SECTORSZ);
281 if (start[i] + len[i]/SECTORSZ != start[i + 1])
283 ("title %u part %u end = %"PRIuSEC" /= part %u start = %"PRIuSEC"",
284 title, i, start[i] + len[i]/SECTORSZ, i + 1, start[i + 1]);
287 put_file(mkident(VOB, title, 1),
288 start[0], start[npart - 1] + SECTORS(len[npart - 1]));
291 static int progresslen = 0;
293 static void clear_progress_internal(void)
295 while (progresslen) { fputs("\b \b", stdout); progresslen--; }
298 static void clear_progress(void)
299 { clear_progress_internal(); fflush(stdout); }
300 static void vappend_progress(const char *fmt, va_list ap)
301 { progresslen += vprintf(fmt, ap); }
302 __attribute__((format(printf, 1, 2)))
303 static void append_progress(const char *fmt, ...)
308 vappend_progress(fmt, ap);
311 __attribute__((format(printf, 1, 2)))
312 static void print_progress(const char *fmt, ...)
317 clear_progress_internal();
318 vappend_progress(fmt, ap);
323 # define F_ALLPROGRESS 1u
324 static secaddr last_pos, limit, nsectors, ndone;
325 static struct timeval last_time;
326 static double wsum, wcount;
327 static struct file *file;
329 static void report_progress(secaddr pos)
334 double percent, t, f, g, rate;
338 #define BETA (1 - ALPHA)
340 gettimeofday(&now, 0);
341 t = (now.tv_sec - last_time.tv_sec) +
342 (now.tv_usec - last_time.tv_usec)/1000000.0;
345 g = wcount ? pow(BETA, t) : 0.0; f = (1 - g)/(1 - BETA);
346 wsum = f*(pos - last_pos)/t + g*wsum;
347 wcount = f + g*wcount;
348 ndone += pos - last_pos;
349 last_time = now; last_pos = pos;
352 if (!wsum || !wcount)
353 { rate = 0; strcpy(etastr, "???"); }
356 eta = (int)((nsectors - ndone)/rate);
357 sprintf(etastr, "%d:%02d:%02d", eta/3600, (eta/60)%60, eta%60);
360 rate *= SECTORSZ; unit = "";
361 if (rate > 128) { rate /= 1024; unit = "k"; }
362 if (rate > 128) { rate /= 1024; unit = "M"; }
363 if (rate > 128) { rate /= 1024; unit = "G"; }
365 if (flags&F_ALLPROGRESS) percent = pos*100.0/limit;
366 else percent = ndone*100.0/nsectors;
368 ("copied %.1f%% (%"PRIuSEC" of %"PRIuSEC"; %.1f %sB/s, ETA %s)",
369 percent, pos, limit, rate, unit, etastr);
370 if (file && id_kind(file->id) == VOB) {
371 append_progress(" -- %s %d %3.1f%%",
372 id_part(file->id) ? "title" : "menu",
374 (pos - file->start)*100.0/
375 (file->end - file->start));
382 static dvd_reader_t *dvd;
383 static int dvdfd = -1, outfd = -1;
384 static dvd_file_t *vob;
385 static const char *mapfile; static FILE *mapfp;
387 struct badblock { secaddr start, end; };
388 DEFVEC(badblock_v, struct badblock);
389 static badblock_v badblocks = VEC_INIT;
391 static int compare_badblock(const void *a, const void *b)
393 const struct badblock *ba = a, *bb = b;
395 if (ba->start < bb->start) return (-1);
396 else if (ba->start > bb->start) return (+1);
398 if (ba->end < bb->end) return (-1);
399 else if (ba->end > bb->end) return (+1);
404 static ssize_t read_sectors(secaddr pos, void *buf, secaddr want)
409 struct badblock *bad, *best;
410 unsigned char *p = buf;
412 best = 0; lo = 0; hi = badblocks.n;
415 printf(";; searching badblocks for %"PRIuSEC" .. %"PRIuSEC"\n",
419 mid = lo + (hi - lo)/2; bad = &badblocks.v[mid];
421 printf(";; try %zu (%"PRIuSEC" .. %"PRIuSEC")... ",
422 mid, bad->start, bad->end);
424 if (pos < bad->start) { D( printf("high\n"); ) best = bad; hi = mid; }
425 else if (pos >= bad->end) { D( printf("low\n"); ) lo = mid + 1; }
426 else { D( printf("match!\n"); ) errno = EIO; return (-1); }
430 printf(";; next is %"PRIuSEC" .. %"PRIuSEC"\n",
431 best->start, best->end);
433 if (best && pos + want > best->start)
434 { want = best->start - pos; fakeerr = EIO; }
439 { errno = 0; n = DVDReadBlocks(vob, pos - file->start, want, p); }
441 if (lseek(dvdfd, (off_t)pos*SECTORSZ, SEEK_SET) < 0)
442 bail_syserr(errno, "failed to seek to sector %"PRIuSEC"", pos);
443 errno = 0; n = read(dvdfd, p, want*SECTORSZ);
444 if (n >= 0) n /= SECTORSZ;
446 memset(p, 0, want*SECTORSZ);
450 if (n > 0) { done += n; pos += n; p += n*SECTORSZ; want -= n; }
451 else if (!n || errno != EINTR) break;
453 if (fakeerr && !errno) errno = fakeerr;
454 return (!done && errno ? -1 : done);
457 static void report_bad_blocks_progress(secaddr lo, secaddr hi, int err)
461 if (lo == hi) append_progress(": retrying bad sector");
463 append_progress(": %"PRIuSEC" bad %s",
464 hi - lo, hi == lo + 1 ? "sector" : "sectors");
465 if (err && err != EIO) append_progress(" (%s)", strerror(err));
469 static ssize_t find_good_sector(secaddr *pos_inout, secaddr end,
470 unsigned char *buf, secaddr sz)
473 secaddr pos = *pos_inout, bad_lo, bad_hi, good, step;
476 report_bad_blocks_progress(pos, pos, errno);
477 for (i = 0; i < 4; i++) {
478 n = read_sectors(pos, buf, 1);
481 moan("sector %"PRIuSEC" read ok after retry", pos);
482 bad_lo = bad_hi = pos;
487 bad_lo = pos; step = 1; bad_hi = pos + 1;
489 report_bad_blocks_progress(bad_lo, bad_hi, errno);
492 moan("giving up on this extent");
493 n = 0; goto recovered;
496 if (step > end - bad_lo) step = end - bad_lo;
497 pos = bad_lo + step - 1;
498 n = read_sectors(pos, buf, 1);
504 while (good > bad_hi) {
505 report_bad_blocks_progress(bad_lo, bad_hi, errno);
506 pos = bad_hi + (good - bad_hi)/2;
507 n = read_sectors(pos, buf, 1);
508 if (n > 0) good = pos;
509 else bad_hi = pos + 1;
512 if (bad_hi > bad_lo) {
514 moan("skipping %"PRIuSEC" bad sectors (%"PRIuSEC" .. %"PRIuSEC")",
515 bad_hi - bad_lo, bad_lo, bad_hi);
518 mapfp = fopen(mapfile, "w");
520 bail_syserr(errno, "failed to open bad-sector map file `%s'",
522 fprintf(mapfp, "## bad sector map\n\n");
524 fprintf(mapfp, "%"PRIuSEC" %"PRIuSEC"\n", bad_lo, bad_hi);
527 bail_syserr(errno, "error writing bad-sector map file");
529 if (lseek(outfd, (off_t)(bad_hi - bad_lo)*SECTORSZ, SEEK_CUR) < 0)
530 bail_syserr(errno, "failed to seek past bad sectors");
537 static void emit(secaddr start, secaddr end)
539 #define BUFSECTORS 512
542 unsigned char buf[BUFSECTORS*SECTORSZ];
546 static int first_time = 1;
554 least = least_live();
557 printf(";; %8"PRIuSEC" .. %"PRIuSEC"\n", start, end);
559 for (i = 0; i < filetab.n; i++) {
560 if (!livep(i)) continue;
561 if (act == -1) act = i;
562 f = &filetab.v[i]; store_filename(fn, f->id);
563 printf(";;\t\t%8"PRIuSEC" .. %-8"PRIuSEC" %s\n",
564 start - f->start, end - f->start, fn);
566 if (act == -1) printf(";;\t\t#<no live source>\n");
567 assert(act == least);
571 { file = 0; vob = 0; }
573 file = &filetab.v[least];
574 switch (id_kind(file->id)) {
579 if (first_time) { clear_progress(); first_time = 0; }
580 vob = DVDOpenFile(dvd, id_title(file->id),
582 ? DVD_READ_TITLE_VOBS
583 : DVD_READ_MENU_VOBS);
585 bail("failed to open %s %u",
586 id_part(file->id) ? "title" : "menu",
596 want = end - pos; if (want > BUFSECTORS) want = BUFSECTORS;
597 n = read_sectors(pos, buf, want);
599 if (n <= 0) n = find_good_sector(&pos, end, buf, BUFSECTORS);
600 if (n > 0) { carefully_write(outfd, buf, n*SECTORSZ); pos += n; }
601 report_progress(pos); fflush(stdout);
604 if (vob) { DVDCloseFile(vob); vob = 0; }
610 static void logfn(void *p, dvd_logger_level_t lev,
611 const char *fmt, va_list ap)
614 case DVD_LOGGER_LEVEL_ERROR:
615 fprintf("%s (libdvdread error): ", prog);
617 case DVD_LOGGER_LEVEL_WARN:
618 fprintf("%s (libdvdread warning): ", prog);
623 vfprintf(stderr, fmt, ap);
626 static const dvd_logger_cb logger = { logfn };
633 #define BUF_INIT { 0, 0, 0 }
634 #define BUF_REWIND(b) do { (b)->n = 0; } while (0)
635 #define BUF_FREE(b) do { \
637 free(_b->p); _b->p = 0; _b->n = _b->sz = 0; \
639 #define BUF_PUTC(b, ch) do { \
640 struct buf *_b = (b); \
641 if (_b->n >= _b->sz) { \
642 _b->sz = _b->sz ? 2*_b->sz : 32; \
643 _b->p = realloc(_b->p, _b->sz); \
644 if (!_b->p) bail("out of memory allocating %zu bytes", _b->sz); \
646 _b->p[_b->n] = (ch); \
649 static int read_line(FILE *fp, struct buf *b)
656 else if (ch != '\n') do {
657 BUF_PUTC(b, ch); b->n++;
659 } while (ch != EOF && ch != '\n');
664 #define PRF_HYPHEN 1u
665 static int parse_range(const char *p, unsigned f,
666 secaddr *start_out, secaddr *end_out)
670 unsigned long start, end;
675 start = strtoul(p, &q, 0);
676 if (errno || start >= SECLIMIT) { rc = -1; goto end; }
677 *start_out = start; p = q;
678 } else if (!(f&PRF_HYPHEN))
679 { rc = -1; goto end; }
684 if (*p != '-') { rc = -1; goto end; }
687 if (!ISSPACE(*p)) { rc = -1; goto end; }
688 do p++; while (ISSPACE(*p));
692 end = strtoul(p, &q, 0);
693 if (errno || end > SECLIMIT || end < start) { rc = -1; goto end; }
694 *end_out = end; p = q;
695 } else if (!(f&PRF_HYPHEN))
696 { rc = -1; goto end; }
698 if (!(f&PRF_HYPHEN)) while (ISSPACE(*p)) p++;
699 if (*p) { rc = -1; goto end; }
707 int main(int argc, char *argv[])
714 secaddr start, end, last;
715 const struct event *ev;
716 const char *device, *outfile;
717 struct badblock *bad;
722 struct buf buf = BUF_INIT;
725 const struct file *file;
730 #define f_continue 2u
734 p = strrchr(argv[0], '/'); prog = p ? p + 1 : argv[0];
736 opt = getopt(argc, argv, "hFR:X:b:cr:"); if (opt < 0) break;
738 case 'h': usage(stderr); exit(0);
739 case 'F': f |= f_fixup; break;
741 fp = fopen(optarg, "r");
743 bail_syserr(errno, "failed to open ranges file `%s'", optarg);
746 BUF_REWIND(&buf); if (read_line(fp, &buf)) break;
748 while (ISSPACE(*p)) p++;
749 if (!*p || *p == '#') continue;
750 if (parse_range(p, 0, &start, &end) ||
751 (last <= SECLIMIT && start < last))
752 bail("bad range `%s' at `%s' line %zu", buf.p, optarg, i);
755 eventq.v[eventq.n - 1].pos = end;
757 put_event(EV_WRITE, 0, start);
758 put_event(EV_STOP, 0, end);
764 bail_syserr(errno, "failed to read ranges file `%s'", optarg);
767 fp = fopen(optarg, "r");
769 bail_syserr(errno, "failed to open bad-blocks file `%s'", optarg);
772 BUF_REWIND(&buf); if (read_line(fp, &buf)) break;
774 while (ISSPACE(*p)) p++;
775 if (!*p || *p == '#') continue;
776 if (parse_range(p, 0, &start, &end) ||
777 (last <= SECLIMIT && start < last))
778 bail("bad range `%s' at `%s' line %zu", buf.p, optarg, i);
780 { VEC_PUSH(bad, &badblocks); bad->start = start; bad->end = end; }
783 bail_syserr(errno, "failed to read bad-blocks file `%s'", optarg);
786 if (mapfile) bail("can't have multiple map files");
789 case 'c': f |= f_continue; break;
792 if (parse_range(optarg, PRF_HYPHEN, &start, &end))
793 bail("bad range `%s'", optarg);
795 put_event(EV_WRITE, 0, start);
796 if (end <= SECLIMIT) put_event(EV_STOP, 0, end);
799 default: f |= f_bogus; break;
802 if (argc - optind != 2) f |= f_bogus;
803 if (f&f_bogus) { usage(stderr); exit(2); }
805 device = argv[optind]; outfile = argv[optind + 1];
808 qsort(badblocks.v, badblocks.n, sizeof(struct badblock),
811 printf(";; fake bad blocks:\n");
812 for (i = 0; i < badblocks.n; i++)
813 printf(";;\t%8"PRIuSEC" .. %"PRIuSEC"\n",
814 badblocks.v[i].start, badblocks.v[i].end);
818 dvdfd = open(device, O_RDONLY);
820 bail_syserr(errno, "failed to open device `%s'", device);
821 if (fstat(dvdfd, &st))
822 bail_syserr(errno, "failed to stat device `%s'", device);
823 if (S_ISREG(st.st_mode)) {
826 } else if (S_ISBLK(st.st_mode)) {
827 if (ioctl(dvdfd, BLKSSZGET, &blksz))
828 bail_syserr(errno, "failed to get block size for `%s'", device);
829 if (ioctl(dvdfd, BLKGETSIZE64, &volsz))
830 bail_syserr(errno, "failed to get volume size for `%s'", device);
832 bail("can't use `%s' as source: expected file or block device", device);
834 if (blksz != SECTORSZ)
835 bail("device `%s' block size %d /= %d", device, blksz, SECTORSZ);
837 bail("device `%s' volume size %"PRIu64" not a multiple of %d",
838 device, volsz, SECTORSZ);
841 outfd = open(outfile, O_WRONLY | O_CREAT, 0666);
843 bail_syserr(errno, "failed to create output file `%s'", outfile);
847 off = lseek(outfd, 0, SEEK_END);
849 bail_syserr(errno, "failed to seek to end of output file `%s'",
851 put_event(EV_WRITE, 0, off/SECTORSZ);
852 } else if (!eventq.n && !(f&f_fixup))
853 put_event(EV_WRITE, 0, 0);
856 dvd = DVDOpen2(0, &logger, device);
858 dvd = DVDOpen(device);
860 if (!dvd) bail("failed to open DVD on `%s'", device);
862 /* It's fast enough just to check everything. */
864 for (i = 1; i < 100; i++) {
868 put_file(mkident(RAW, 0, 0), 0, volsz/SECTORSZ);
869 assert(filetab.n <= MAXFILES);
871 for (i = 0, limit = 0; i < filetab.n; i++)
872 if (filetab.v[i].end > limit) limit = filetab.v[i].end;
874 if (end > limit) end = limit;
877 printf("\n;; files:\n");
878 for (i = 0; i < filetab.n; i++) {
879 file = &filetab.v[i];
880 store_filename(fn, file->id);
881 printf(";;\t%8"PRIuSEC" %s\n", file->start, fn);
885 qsort(eventq.v, eventq.n, sizeof(struct event), compare_event);
887 f &= ~f_write; start = 0; n = 0;
888 for (i = 0; i < eventq.n; i++) {
893 bail("overlapping ranges: range from %"PRIuSEC" still open at %"PRIuSEC"",
895 n++; f |= f_write; start = ev->pos;
903 f &= ~f_write; start = 0;
904 for (i = 0; i < eventq.n; i++) {
907 case EV_WRITE: start = ev->pos; f |= f_write; break;
908 case EV_STOP: nsectors += ev->pos - start; f &= ~f_write; break;
910 if (ev->pos >= limit) break;
911 if (f&f_fixup) start = ev->pos;
915 put_event(EV_WRITE, 0, start);
919 nsectors += limit - start;
920 put_event(EV_STOP, 0, limit);
922 if (n == 1 && (f&f_write)) flags |= F_ALLPROGRESS;
926 printf("\n;; event sweep:\n");
928 for (pos = 0, i = 0; i < eventq.n; i++) {
931 if (f&f_write) emit(pos, ev->pos);
942 store_filename(fn, filetab.v[ev->file].id);
944 printf(";; %8"PRIuSEC": begin `%s'\n", pos, fn);
948 gettimeofday(&last_time, 0); last_pos = pos;
949 if (lseek(outfd, (off_t)ev->pos*SECTORSZ, SEEK_SET) < 0)
951 "failed to seek to resume position "
952 "(sector %"PRIuSEC") in output file `%s'",
956 printf(";; %8"PRIuSEC": begin write\n", pos);
964 printf(";; %8"PRIuSEC": end write\n", pos);
968 clear_live(ev->file);
970 store_filename(fn, filetab.v[ev->file].id);
972 printf(";; %8"PRIuSEC": end `%s'\n", pos, fn);
979 if (progresslen) putchar('\n');
981 if (ftruncate(outfd, (off_t)limit*SECTORSZ) < 0)
982 bail_syserr(errno, "failed to set output file `%s' length", outfile);
984 if (dvd) DVDClose(dvd);
985 if (dvdfd >= 0) close(dvdfd);
986 if (outfd >= 0) close(outfd);
988 if (ferror(mapfp) || fclose(mapfp))
989 bail_syserr(errno, "error writing bad-sector map file");