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 void report_bad_blocks_progress(secaddr lo, secaddr hi, int err)
386 if (lo == hi) append_progress(": retrying bad sector");
388 append_progress(": %"PRIuSEC" bad %s",
389 hi - lo, hi == lo + 1 ? "sector" : "sectors");
390 if (err != EIO) append_progress(" (%s)", strerror(err));
394 static dvd_reader_t *dvd;
395 static int dvdfd = -1, outfd = -1;
396 static dvd_file_t *vob;
397 static const char *mapfile; static FILE *mapfp;
399 struct badblock { secaddr start, end; };
400 DEFVEC(badblock_v, struct badblock);
401 static badblock_v badblocks = VEC_INIT;
403 static int compare_badblock(const void *a, const void *b)
405 const struct badblock *ba = a, *bb = b;
407 if (ba->start < bb->start) return (-1);
408 else if (ba->start > bb->start) return (+1);
410 if (ba->end < bb->end) return (-1);
411 else if (ba->end > bb->end) return (+1);
416 static ssize_t read_sectors(secaddr pos, void *buf, secaddr want)
420 struct badblock *bad, *best;
422 best = 0; lo = 0; hi = badblocks.n;
425 printf(";; searching badblocks for %"PRIuSEC" .. %"PRIuSEC"\n",
429 mid = lo + (hi - lo)/2; bad = &badblocks.v[mid];
431 printf(";; try %zu (%"PRIuSEC" .. %"PRIuSEC")... ",
432 mid, bad->start, bad->end);
434 if (pos < bad->start) { D( printf("high\n"); ) best = bad; hi = mid; }
435 else if (pos >= bad->end) { D( printf("low\n"); ) lo = mid + 1; }
436 else { D( printf("match!\n"); ) errno = EIO; return (-1); }
440 printf(";; next is %"PRIuSEC" .. %"PRIuSEC"\n",
441 best->start, best->end);
443 if (best && pos + want > best->start) want = best->start - pos;
447 n = DVDReadBlocks(vob, pos - file->start, want, buf);
449 if (lseek(dvdfd, (off_t)pos*SECTORSZ, SEEK_SET) < 0)
450 bail_syserr(errno, "failed to seek to sector %"PRIuSEC"", pos);
451 n = read(dvdfd, buf, want*SECTORSZ);
452 if (n >= 0) n /= SECTORSZ;
454 memset(buf, 0, want*SECTORSZ);
458 if (n < 0 && errno == EINTR) goto again;
462 static ssize_t find_good_sector(secaddr *pos_inout, secaddr end,
463 unsigned char *buf, secaddr sz)
466 secaddr pos = *pos_inout, bad_lo, bad_hi, good, step;
469 report_bad_blocks_progress(pos, pos, errno);
470 for (i = 0; i < 4; i++) {
471 n = read_sectors(pos, buf, 1);
474 moan("sector %"PRIuSEC" read ok after retry", pos);
475 bad_lo = bad_hi = pos;
480 bad_lo = pos; step = 1; bad_hi = pos + 1;
482 report_bad_blocks_progress(bad_lo, bad_hi, errno);
485 moan("giving up on this extent");
486 n = 0; goto recovered;
489 if (step > end - bad_lo) step = end - bad_lo;
490 pos = bad_lo + step - 1;
491 n = read_sectors(pos, buf, 1);
497 while (good > bad_hi) {
498 report_bad_blocks_progress(bad_lo, bad_hi, errno);
499 pos = bad_hi + (good - bad_hi)/2;
500 n = read_sectors(pos, buf, 1);
501 if (n > 0) good = pos;
502 else bad_hi = pos + 1;
505 if (bad_hi > bad_lo) {
507 moan("skipping %"PRIuSEC" bad sectors (%"PRIuSEC" .. %"PRIuSEC")",
508 bad_hi - bad_lo, bad_lo, bad_hi);
511 mapfp = fopen(mapfile, "w");
513 bail_syserr(errno, "failed to open bad-sector map file `%s'",
515 fprintf(mapfp, "## bad sector map\n\n");
517 fprintf(mapfp, "%"PRIuSEC" %"PRIuSEC"\n", bad_lo, bad_hi);
520 bail_syserr(errno, "error writing bad-sector map file");
522 if (lseek(outfd, (off_t)(bad_hi - bad_lo)*SECTORSZ, SEEK_CUR) < 0)
523 bail_syserr(errno, "failed to seek past bad sectors");
530 static void emit(secaddr start, secaddr end)
532 #define BUFSECTORS 512
535 unsigned char buf[BUFSECTORS*SECTORSZ];
539 static int first_time = 1;
547 least = least_live();
550 printf(";; %8"PRIuSEC" .. %"PRIuSEC"\n", start, end);
552 for (i = 0; i < filetab.n; i++) {
553 if (!livep(i)) continue;
554 if (act == -1) act = i;
555 f = &filetab.v[i]; store_filename(fn, f->id);
556 printf(";;\t\t%8"PRIuSEC" .. %-8"PRIuSEC" %s\n",
557 start - f->start, end - f->start, fn);
559 if (act == -1) printf(";;\t\t#<no live source>\n");
560 assert(act == least);
564 { file = 0; vob = 0; }
566 file = &filetab.v[least];
567 switch (id_kind(file->id)) {
572 if (first_time) { clear_progress(); first_time = 0; }
573 vob = DVDOpenFile(dvd, id_title(file->id),
575 ? DVD_READ_TITLE_VOBS
576 : DVD_READ_MENU_VOBS);
578 bail("failed to open %s %u",
579 id_part(file->id) ? "title" : "menu",
589 want = end - pos; if (want > BUFSECTORS) want = BUFSECTORS;
590 n = read_sectors(pos, buf, want);
592 if (n <= 0) n = find_good_sector(&pos, end, buf, BUFSECTORS);
593 if (n > 0) { carefully_write(outfd, buf, n*SECTORSZ); pos += n; }
594 report_progress(pos); fflush(stdout);
597 if (vob) { DVDCloseFile(vob); vob = 0; }
603 static void logfn(void *p, dvd_logger_level_t lev,
604 const char *fmt, va_list ap)
607 case DVD_LOGGER_LEVEL_ERROR:
608 fprintf("%s (libdvdread error): ", prog);
610 case DVD_LOGGER_LEVEL_WARN:
611 fprintf("%s (libdvdread warning): ", prog);
616 vfprintf(stderr, fmt, ap);
619 static const dvd_logger_cb logger = { logfn };
626 #define BUF_INIT { 0, 0, 0 }
627 #define BUF_REWIND(b) do { (b)->n = 0; } while (0)
628 #define BUF_FREE(b) do { \
630 free(_b->p); _b->p = 0; _b->n = _b->sz = 0; \
632 #define BUF_PUTC(b, ch) do { \
633 struct buf *_b = (b); \
634 if (_b->n >= _b->sz) { \
635 _b->sz = _b->sz ? 2*_b->sz : 32; \
636 _b->p = realloc(_b->p, _b->sz); \
637 if (!_b->p) bail("out of memory allocating %zu bytes", _b->sz); \
639 _b->p[_b->n] = (ch); \
642 static int read_line(FILE *fp, struct buf *b)
649 else if (ch != '\n') do {
650 BUF_PUTC(b, ch); b->n++;
652 } while (ch != EOF && ch != '\n');
657 #define PRF_HYPHEN 1u
658 static int parse_range(const char *p, unsigned f,
659 secaddr *start_out, secaddr *end_out)
663 unsigned long start, end;
668 start = strtoul(p, &q, 0);
669 if (errno || start >= SECLIMIT) { rc = -1; goto end; }
670 *start_out = start; p = q;
671 } else if (!(f&PRF_HYPHEN))
672 { rc = -1; goto end; }
677 if (*p != '-') { rc = -1; goto end; }
680 if (!ISSPACE(*p)) { rc = -1; goto end; }
681 do p++; while (ISSPACE(*p));
685 end = strtoul(p, &q, 0);
686 if (errno || end > SECLIMIT || end < start) { rc = -1; goto end; }
687 *end_out = end; p = q;
688 } else if (!(f&PRF_HYPHEN))
689 { rc = -1; goto end; }
691 if (!(f&PRF_HYPHEN)) while (ISSPACE(*p)) p++;
692 if (*p) { rc = -1; goto end; }
700 int main(int argc, char *argv[])
707 secaddr start, end, last;
708 const struct event *ev;
709 const char *device, *outfile;
710 struct badblock *bad;
715 struct buf buf = BUF_INIT;
718 const struct file *file;
723 #define f_continue 2u
727 p = strrchr(argv[0], '/'); prog = p ? p + 1 : argv[0];
729 opt = getopt(argc, argv, "hFR:X:b:cr:"); if (opt < 0) break;
731 case 'h': usage(stderr); exit(0);
732 case 'F': f |= f_fixup; break;
734 fp = fopen(optarg, "r");
736 bail_syserr(errno, "failed to open ranges file `%s'", optarg);
739 BUF_REWIND(&buf); if (read_line(fp, &buf)) break;
741 while (ISSPACE(*p)) p++;
742 if (!*p || *p == '#') continue;
743 if (parse_range(p, 0, &start, &end) ||
744 (last <= SECLIMIT && start < last))
745 bail("bad range `%s' at `%s' line %zu", buf.p, optarg, i);
748 eventq.v[eventq.n - 1].pos = end;
750 put_event(EV_WRITE, 0, start);
751 put_event(EV_STOP, 0, end);
757 bail_syserr(errno, "failed to read ranges file `%s'", optarg);
760 fp = fopen(optarg, "r");
762 bail_syserr(errno, "failed to open bad-blocks file `%s'", optarg);
765 BUF_REWIND(&buf); if (read_line(fp, &buf)) break;
767 while (ISSPACE(*p)) p++;
768 if (!*p || *p == '#') continue;
769 if (parse_range(p, 0, &start, &end) ||
770 (last <= SECLIMIT && start < last))
771 bail("bad range `%s' at `%s' line %zu", buf.p, optarg, i);
773 { VEC_PUSH(bad, &badblocks); bad->start = start; bad->end = end; }
776 bail_syserr(errno, "failed to read bad-blocks file `%s'", optarg);
779 if (mapfile) bail("can't have multiple map files");
782 case 'c': f |= f_continue; break;
785 if (parse_range(optarg, PRF_HYPHEN, &start, &end))
786 bail("bad range `%s'", optarg);
788 put_event(EV_WRITE, 0, start);
789 if (end <= SECLIMIT) put_event(EV_STOP, 0, end);
792 default: f |= f_bogus; break;
795 if (argc - optind != 2) f |= f_bogus;
796 if (f&f_bogus) { usage(stderr); exit(2); }
798 device = argv[optind]; outfile = argv[optind + 1];
801 qsort(badblocks.v, badblocks.n, sizeof(struct badblock),
804 printf(";; fake bad blocks:\n");
805 for (i = 0; i < badblocks.n; i++)
806 printf(";;\t%8"PRIuSEC" .. %"PRIuSEC"\n",
807 badblocks.v[i].start, badblocks.v[i].end);
811 dvdfd = open(device, O_RDONLY);
813 bail_syserr(errno, "failed to open device `%s'", device);
814 if (fstat(dvdfd, &st))
815 bail_syserr(errno, "failed to stat device `%s'", device);
816 if (S_ISREG(st.st_mode)) {
819 } else if (S_ISBLK(st.st_mode)) {
820 if (ioctl(dvdfd, BLKSSZGET, &blksz))
821 bail_syserr(errno, "failed to get block size for `%s'", device);
822 if (ioctl(dvdfd, BLKGETSIZE64, &volsz))
823 bail_syserr(errno, "failed to get volume size for `%s'", device);
825 bail("can't use `%s' as source: expected file or block device", device);
827 if (blksz != SECTORSZ)
828 bail("device `%s' block size %d /= %d", device, blksz, SECTORSZ);
830 bail("device `%s' volume size %"PRIu64" not a multiple of %d",
831 device, volsz, SECTORSZ);
834 outfd = open(outfile, O_WRONLY | O_CREAT, 0666);
836 bail_syserr(errno, "failed to create output file `%s'", outfile);
840 off = lseek(outfd, 0, SEEK_END);
842 bail_syserr(errno, "failed to seek to end of output file `%s'",
844 put_event(EV_WRITE, 0, off/SECTORSZ);
845 } else if (!eventq.n && !(f&f_fixup))
846 put_event(EV_WRITE, 0, 0);
849 dvd = DVDOpen2(0, &logger, device);
851 dvd = DVDOpen(device);
853 if (!dvd) bail("failed to open DVD on `%s'", device);
855 /* It's fast enough just to check everything. */
857 for (i = 1; i < 100; i++) {
861 put_file(mkident(RAW, 0, 0), 0, volsz/SECTORSZ);
862 assert(filetab.n <= MAXFILES);
864 for (i = 0, limit = 0; i < filetab.n; i++)
865 if (filetab.v[i].end > limit) limit = filetab.v[i].end;
867 if (end > limit) end = limit;
870 printf("\n;; files:\n");
871 for (i = 0; i < filetab.n; i++) {
872 file = &filetab.v[i];
873 store_filename(fn, file->id);
874 printf(";;\t%8"PRIuSEC" %s\n", file->start, fn);
878 qsort(eventq.v, eventq.n, sizeof(struct event), compare_event);
880 f &= ~f_write; start = 0; n = 0;
881 for (i = 0; i < eventq.n; i++) {
886 bail("overlapping ranges: range from %"PRIuSEC" still open at %"PRIuSEC"",
888 n++; f |= f_write; start = ev->pos;
896 f &= ~f_write; start = 0;
897 for (i = 0; i < eventq.n; i++) {
900 case EV_WRITE: start = ev->pos; f |= f_write; break;
901 case EV_STOP: nsectors += ev->pos - start; f &= ~f_write; break;
903 if (ev->pos >= limit) break;
904 if (f&f_fixup) start = ev->pos;
908 put_event(EV_WRITE, 0, start);
912 nsectors += limit - start;
913 put_event(EV_STOP, 0, limit);
915 if (n == 1 && (f&f_write)) flags |= F_ALLPROGRESS;
919 printf("\n;; event sweep:\n");
921 for (pos = 0, i = 0; i < eventq.n; i++) {
924 if (f&f_write) emit(pos, ev->pos);
935 store_filename(fn, filetab.v[ev->file].id);
937 printf(";; %8"PRIuSEC": begin `%s'\n", pos, fn);
941 gettimeofday(&last_time, 0); last_pos = pos;
942 if (lseek(outfd, (off_t)ev->pos*SECTORSZ, SEEK_SET) < 0)
944 "failed to seek to resume position "
945 "(sector %"PRIuSEC") in output file `%s'",
949 printf(";; %8"PRIuSEC": begin write\n", pos);
957 printf(";; %8"PRIuSEC": end write\n", pos);
961 clear_live(ev->file);
963 store_filename(fn, filetab.v[ev->file].id);
965 printf(";; %8"PRIuSEC": end `%s'\n", pos, fn);
972 if (progresslen) putchar('\n');
974 if (ftruncate(outfd, (off_t)limit*SECTORSZ) < 0)
975 bail_syserr(errno, "failed to set output file `%s' length", outfile);
977 if (dvd) DVDClose(dvd);
978 if (dvdfd >= 0) close(dvdfd);
979 if (outfd >= 0) close(outfd);
981 if (ferror(mapfp) || fclose(mapfp))
982 bail_syserr(errno, "error writing bad-sector map file");