dvd-sector-copy.c: Fix formatting for debugging code.
[dvdrip] / dvd-sector-copy.c
index 6e2737a..1e4dc07 100644 (file)
 #define ISDIGIT(ch) CTYPE_HACK(isdigit, ch)
 #define ISSPACE(ch) CTYPE_HACK(isspace, ch)
 
+#ifdef DEBUG
+#  define D(x) x
+#else
+#  define D(x)
+#endif
+
 #define N(v) (sizeof(v)/sizeof((v)[0]))
 
 #define SECTORSZ 2048
@@ -43,8 +49,8 @@ static int status = 0;
 static void usage(FILE *fp)
 {
   fprintf(fp,
-         "usage: %s [-c] [-D DEV] [-R MAP] "
-               "[-b OUTMAP] [-o OUTFILE] [-r [START]-[END]]\n",
+         "usage: %s [-c] [-R MAP] [-b OUTMAP] [-r [START]-[END]]\n"
+         "\tDEVICE OUTFILE\n",
          prog);
 }
 
@@ -81,6 +87,23 @@ static void bail_syserr(int err, const char *fmt, ...)
   exit(2);
 }
 
+static void carefully_write(int fd, const void *buf, size_t sz)
+{
+  const unsigned char *p = buf;
+  ssize_t n;
+
+  if (fd < 0) return;
+  while (sz) {
+    n = write(fd, p, sz);
+    if (n < 0) {
+      if (errno == EINTR) continue;
+      bail_syserr(errno, "failed to write to output file");
+    }
+    if (!n) bail("unexpected short write to output file");
+    p += n; sz -= n;
+  }
+}
+
 #define DEFVEC(vtype, etype)                                           \
        typedef struct { etype *v; size_t n, sz; } vtype
 #define VEC_INIT { 0, 0, 0 }
@@ -135,6 +158,7 @@ static void store_filename(char *buf, ident id)
 
 typedef uint_least32_t secaddr;
 #define PRIuSEC PRIuLEAST32
+#define SECLIMIT 0x00400000
 
 #define MAXFILES (1 + 2*99 + 1)
 struct file {
@@ -267,7 +291,10 @@ static void put_title(dvd_reader_t *dvd, unsigned title)
 static int progresslen = 0;
 
 static void clear_progress_internal(void)
-  { while (progresslen) { fputs("\b \b", stdout); progresslen--; } }
+{
+  while (progresslen) { fputs("\b \b", stdout); progresslen--; }
+  putchar('\r');
+}
 static void clear_progress(void)
   { clear_progress_internal(); fflush(stdout); }
 static void vappend_progress(const char *fmt, va_list ap)
@@ -292,22 +319,14 @@ static void print_progress(const char *fmt, ...)
   va_end(ap);
 }
 
-struct source {
-  dvd_reader_t *dvd;
-  int dvdfd;
-  struct file *file;
-  dvd_file_t *vob;
-
-  unsigned f;
-#define SRCF_ALLPROGRESS 1u
-  secaddr last_pos, limit, nsectors, ndone;
-  struct timeval last_time;
-  double wsum, wcount;
-  const char *mapfile; FILE *mapfp;
-};
-#define SOURCE_INIT { 0, -1, 0, 0,  0, 0, 0, 0, 0, { 0, 0 }, 0.0, 0.0, 0, 0 }
+unsigned flags;
+#  define F_ALLPROGRESS 1u
+static secaddr last_pos, limit, nsectors, ndone;
+static struct timeval last_time;
+static double wsum, wcount;
+static struct file *file;
 
-static void report_progress(struct source *src, secaddr pos)
+static void report_progress(secaddr pos)
 {
   char etastr[32];
   struct timeval now;
@@ -319,22 +338,22 @@ static void report_progress(struct source *src, secaddr pos)
 #define BETA (1 - ALPHA)
 
   gettimeofday(&now, 0);
-  t = (now.tv_sec - src->last_time.tv_sec) +
-    (now.tv_usec - src->last_time.tv_usec)/1000000.0;
+  t = (now.tv_sec - last_time.tv_sec) +
+    (now.tv_usec - last_time.tv_usec)/1000000.0;
 
   if (t) {
-    g = src->wcount ? pow(BETA, t) : 0.0; f = (1 - g)/(1 - BETA);
-    src->wsum = f*(pos - src->last_pos)/t + g*src->wsum;
-    src->wcount = f + g*src->wcount;
-    src->ndone += pos - src->last_pos;
-    src->last_time = now; src->last_pos = pos;
+    g = wcount ? pow(BETA, t) : 0.0; f = (1 - g)/(1 - BETA);
+    wsum = f*(pos - last_pos)/t + g*wsum;
+    wcount = f + g*wcount;
+    ndone += pos - last_pos;
+    last_time = now; last_pos = pos;
   }
 
-  if (!src->wsum || !src->wcount)
+  if (!wsum || !wcount)
     { rate = 0; strcpy(etastr, "???"); }
   else {
-    rate = src->wsum/src->wcount;
-    eta = (int)((src->nsectors - src->ndone)/rate);
+    rate = wsum/wcount;
+    eta = (int)((nsectors - ndone)/rate);
     sprintf(etastr, "%d:%02d:%02d", eta/3600, (eta/60)%60, eta%60);
   }
 
@@ -343,83 +362,375 @@ static void report_progress(struct source *src, secaddr pos)
   if (rate > 128) { rate /= 1024; unit = "M"; }
   if (rate > 128) { rate /= 1024; unit = "G"; }
 
-  if (src->f&SRCF_ALLPROGRESS) percent = pos*100.0/src->limit;
-  else percent = src->ndone*100.0/src->nsectors;
+  if (flags&F_ALLPROGRESS) percent = pos*100.0/limit;
+  else percent = ndone*100.0/nsectors;
   print_progress
     ("copied %.1f%% (%"PRIuSEC" of %"PRIuSEC"; %.1f %sB/s, ETA %s)",
-     percent, pos, src->limit,  rate, unit, etastr);
-  if (src->file && id_kind(src->file->id) == VOB) {
+     percent, pos, limit,  rate, unit, etastr);
+  if (file && id_kind(file->id) == VOB) {
     append_progress(" -- %s %d %3.1f%%",
-                   id_part(src->file->id) ? "title" : "menu",
-                   id_title(src->file->id),
-                   (pos - src->file->start)*100.0/
-                     (src->file->end - src->file->start));
+                   id_part(file->id) ? "title" : "menu",
+                   id_title(file->id),
+                   (pos - file->start)*100.0/
+                     (file->end - file->start));
   }
 
 #undef ALPHA
 #undef BETA
 }
 
-static void report_bad_blocks_progress(struct source *src,
-                                      secaddr lo, secaddr hi, int err)
+static dvd_reader_t *dvd;
+static int dvdfd = -1, outfd = -1;
+static dvd_file_t *vob;
+static const char *mapfile; static FILE *mapfp;
+
+struct badblock { secaddr start, end; };
+DEFVEC(badblock_v, struct badblock);
+static badblock_v badblocks = VEC_INIT;
+
+static int compare_badblock(const void *a, const void *b)
+{
+  const struct badblock *ba = a, *bb = b;
+
+  if (ba->start < bb->start) return (-1);
+  else if (ba->start > bb->start) return (+1);
+
+  if (ba->end < bb->end) return (-1);
+  else if (ba->end > bb->end) return (+1);
+
+  return (0);
+}
+
+static ssize_t read_sectors(secaddr pos, void *buf, secaddr want)
+{
+  ssize_t n, done;
+  size_t lo, mid, hi;
+  int fakeerr = 0;
+  struct badblock *bad, *best;
+  unsigned char *p = buf;
+
+  if (badblocks.n) {
+    best = 0; lo = 0; hi = badblocks.n;
+#ifdef DEBUG
+    debug_clear_progress();
+    printf(";; searching badblocks for %"PRIuSEC" .. %"PRIuSEC"\n",
+          pos, pos + want);
+#endif
+    while (lo < hi) {
+      mid = lo + (hi - lo)/2; bad = &badblocks.v[mid];
+#ifdef DEBUG
+      printf(";;   try %zu (%"PRIuSEC" .. %"PRIuSEC")... ",
+            mid, bad->start, bad->end);
+#endif
+      if (pos < bad->start) { D( printf("high\n"); ) best = bad; hi = mid; }
+      else if (pos >= bad->end) { D( printf("low\n"); ) lo = mid + 1; }
+      else { D( printf("match!\n"); ) errno = EIO; return (-1); }
+    }
+#ifdef DEBUG
+    if (best)
+      printf(";;   next is %"PRIuSEC" .. %"PRIuSEC"\n",
+            best->start, best->end);
+#endif
+    if (best && pos + want > best->start)
+      { want = best->start - pos; fakeerr = EIO; }
+  }
+  done = 0;
+  while (want) {
+    if (vob)
+      { errno = 0; n = DVDReadBlocks(vob, pos - file->start, want, p); }
+    else if (file) {
+      if (lseek(dvdfd, (off_t)pos*SECTORSZ, SEEK_SET) < 0)
+       bail_syserr(errno, "failed to seek to sector %"PRIuSEC"", pos);
+      errno = 0; n = read(dvdfd, p, want*SECTORSZ);
+      if (n >= 0) n /= SECTORSZ;
+    } else {
+      memset(p, 0, want*SECTORSZ);
+      n = want;
+    }
+
+    if (n > 0) { done += n; pos += n; p += n*SECTORSZ; want -= n; }
+    else if (!n || errno != EINTR) break;
+  }
+  if (fakeerr && !errno) errno = fakeerr;
+  return (!done && errno ? -1 : done);
+}
+
+static void report_bad_blocks_progress(secaddr lo, secaddr hi, int err)
 {
-  report_progress(src, hi);
+  report_progress(hi);
 
   if (lo == hi) append_progress(": retrying bad sector");
   else
     append_progress(": %"PRIuSEC" bad %s",
                    hi - lo, hi == lo + 1 ? "sector" : "sectors");
-  if (err != EIO) append_progress(" (%s)", strerror(err));
+  if (err && err != EIO) append_progress(" (%s)", strerror(err));
   fflush(stdout);
 }
 
-static ssize_t read_sectors(struct source *src, secaddr pos,
-                           void *buf, secaddr want)
+static void recovered(secaddr bad_lo, secaddr bad_hi)
+{
+  clear_progress();
+  moan("skipping %"PRIuSEC" bad sectors (%"PRIuSEC" .. %"PRIuSEC")",
+       bad_hi - bad_lo, bad_lo, bad_hi);
+  if (mapfile) {
+    if (!mapfp) {
+      mapfp = fopen(mapfile, "w");
+      if (!mapfp)
+       bail_syserr(errno, "failed to open bad-sector map file `%s'",
+                   optarg);
+      fprintf(mapfp, "## bad sector map\n\n");
+    }
+    fprintf(mapfp, "%"PRIuSEC" %"PRIuSEC"\n", bad_lo, bad_hi);
+    fflush(mapfp);
+    if (ferror(mapfp))
+      bail_syserr(errno, "error writing bad-sector map file");
+  }
+  if (lseek(outfd, (off_t)(bad_hi - bad_lo)*SECTORSZ, SEEK_CUR) < 0)
+    bail_syserr(errno, "failed to seek past bad sectors");
+  status = 1;
+}
+
+struct recoverybuf {
+  unsigned char *buf;
+  secaddr sz, pos, start, end;
+};
+
+static void rearrange_sectors(struct recoverybuf *r,
+                             secaddr dest, secaddr src, secaddr len)
+{
+  assert(dest + len <= r->sz);
+  assert(src + len <= r->sz);
+  memmove(r->buf + dest*SECTORSZ, r->buf + src*SECTORSZ, len*SECTORSZ);
+}
+
+#ifdef DEBUG
+__attribute__((format(printf, 2, 3)))
+static void show_recovery_buffer_map(const struct recoverybuf *r,
+                                    const char *what, ...)
+{
+  va_list ap;
+
+  va_start(ap, what);
+  clear_progress();
+  printf(";; recovery buffer (");
+  vprintf(what, ap);
+  printf("): "
+        "(%"PRIuSEC") ..%"PRIuSEC".. "
+        "[%"PRIuSEC" ..%"PRIuSEC".. %"PRIuSEC"] "
+        "..%"PRIuSEC".. (%"PRIuSEC")\n",
+        r->pos, r->start,
+        r->pos + r->start, r->end - r->start, r->pos + r->end,
+        r->sz - r->end, r->pos + r->sz);
+  va_end(ap);
+  assert(r->start <= r->end);
+  assert(r->end <= r->sz);
+}
+#endif
+
+static ssize_t recovery_read_sectors(struct recoverybuf *r,
+                                    secaddr pos, secaddr off, secaddr want)
 {
   ssize_t n;
 
-again:
-  if (src->vob)
-    n = DVDReadBlocks(src->vob, pos - src->file->start, want, buf);
-  else if (src->file) {
-    if (lseek(src->dvdfd, (off_t)pos*SECTORSZ, SEEK_SET) < 0)
-      bail_syserr(errno, "failed to seek to sector %"PRIuSEC"", pos);
-    n = read(src->dvdfd, buf, want*SECTORSZ);
-    if (n >= 0) n /= SECTORSZ;
-  } else {
-    memset(buf, 0, want*SECTORSZ);
-    n = want;
+  assert(off <= r->sz); assert(want <= r->sz - off);
+  n = read_sectors(pos, r->buf + off*SECTORSZ, want);
+  return (n);
+}
+
+static ssize_t recovery_read(struct recoverybuf *r,
+                            secaddr pos, secaddr want)
+{
+  secaddr diff, pp, nn;
+  ssize_t n;
+
+#ifdef DEBUG
+  clear_progress();
+  show_recovery_buffer_map(r, "begin(%"PRIuSEC", %"PRIuSEC")", pos, want);
+#endif
+
+  if (pos < r->pos) {
+    diff = r->pos - pos;
+    if (r->start + diff >= r->sz) {
+      r->pos = pos; r->start = r->end = 0;
+#ifdef DEBUG
+      show_recovery_buffer_map(r, "cleared; shift up by %"PRIuSEC"", diff);
+#endif
+    } else {
+      if (r->end + diff > r->sz) r->end = r->sz - diff;
+      rearrange_sectors(r, r->start + diff, r->start, r->end - r->start);
+      r->pos -= diff; r->start += diff; r->end += diff;
+#ifdef DEBUG
+      show_recovery_buffer_map(r, "shifted up by %"PRIuSEC"", diff);
+#endif
+    }
+  } else if (pos > r->pos + r->end) {
+      r->pos = pos; r->start = r->end = 0;
+#ifdef DEBUG
+      show_recovery_buffer_map(r, "cleared; beyond previous region");
+#endif
+  } else if (pos + want > r->pos + r->sz) {
+    diff = (pos + want) - (r->pos + r->sz);
+    if (r->end <= diff) {
+      r->pos = pos; r->start = r->end = 0;
+#ifdef DEBUG
+      show_recovery_buffer_map(r, "cleared; shift down by %"PRIuSEC"", diff);
+#endif
+    } else {
+      if (r->start < diff) r->start = diff;
+      rearrange_sectors(r, r->start - diff, r->start, r->end - r->start);
+      r->pos += diff; r->start -= diff; r->end -= diff;
+#ifdef DEBUG
+      show_recovery_buffer_map(r, "shifted down by %"PRIuSEC"", diff);
+#endif
+    }
   }
 
-  if (n < 0 && errno == EINTR) goto again;
+  if (pos < r->pos + r->start) {
+    pp = pos - r->pos; nn = r->start - pp;
+#ifdef DEBUG
+    printf(";; read low (%"PRIuSEC"@%"PRIuSEC", %"PRIuSEC")", pos, pp, nn);
+    fflush(stdout);
+#endif
+    n = recovery_read_sectors(r, pos, pp, nn);
+#ifdef DEBUG
+    printf(" -> %zd\n", n);
+#endif
+    if (n != nn) {
+      if (n >= 0 && n > want) n = want;
+      goto end;
+    }
+    r->start = pp;
+#ifdef DEBUG
+    show_recovery_buffer_map(r, "joined new region");
+#endif
+  }
+
+  if (pos + want > r->pos + r->end) {
+    pp = r->end; nn = (pos + want) - (r->pos + r->end);
+#ifdef DEBUG
+    printf(";; read high (%"PRIuSEC"@%"PRIuSEC", %"PRIuSEC")",
+          r->pos + pp, pp, nn);
+    fflush(stdout);
+#endif
+    n = recovery_read_sectors(r, pos + pp, pp, nn);
+#ifdef DEBUG
+    printf(" -> %zd\n", n);
+#endif
+    if (n > 0) {
+      r->end += n;
+#ifdef DEBUG
+      show_recovery_buffer_map(r, "joined new region");
+#endif
+    }
+  }
+
+  n = r->pos + r->end - pos;
+  if (!n && want) n = -1;
+
+end:
+#ifdef DEBUG
+  show_recovery_buffer_map(r, "done; return %zd", n);
+#endif
   return (n);
 }
 
-static void carefully_write(int fd, const void *buf, size_t sz)
+static secaddr run_length_wanted(secaddr pos, secaddr badlen,
+                                secaddr sz, secaddr end)
 {
-  const unsigned char *p = buf;
+  secaddr want;
+
+  want = 3*badlen/2;
+  if (!want) want = 1;
+  if (want > end - pos) want = end - pos;
+  if (want > sz) want = sz;
+  return (want);
+}
+
+static ssize_t find_good_sector(secaddr *pos_inout, secaddr end,
+                               unsigned char *buf, secaddr sz)
+{
+  int i;
+  secaddr pos = *pos_inout, bad_lo, bad_hi, good, step, want;
+  struct recoverybuf r;
   ssize_t n;
 
-  if (fd < 0) return;
-  while (sz) {
-    n = write(fd, p, sz);
-    if (n < 0) {
-      if (errno == EINTR) continue;
-      bail_syserr(errno, "failed to write to output file");
+  r.buf = buf; r.sz = sz; r.pos = r.start = r.end = 0;
+  report_bad_blocks_progress(pos, pos, errno);
+
+  want = sz; if (want > end - pos) want = end - pos;
+  for (i = 0; i < 4; i++) {
+    n = recovery_read(&r, pos, want);
+#ifdef DEBUG
+    clear_progress();
+    printf(";; [retry] try reading %"PRIuSEC" .. %"PRIuSEC" -> %zd\n",
+          pos, pos + want, n);
+#endif
+    if (n > 0) {
+      clear_progress();
+      moan("sector %"PRIuSEC" read ok after retry", pos);
+      return (n);
     }
-    if (!n) bail("unexpected short write to output file");
-    p += n; sz -= n;
   }
+
+  bad_lo = pos; bad_hi = pos + 1;
+  for (;;) {
+    report_bad_blocks_progress(bad_lo, bad_hi, errno);
+    if (bad_hi >= end) {
+      clear_progress();
+      moan("giving up on this extent");
+      recovered(bad_lo, end); *pos_inout = end; return (0);
+    }
+    step = 2*(bad_hi - bad_lo); if (step > end - bad_lo) step = end - bad_lo;
+    pos = bad_lo + step - 1;
+    want = run_length_wanted(pos, step, sz, end);
+    n = recovery_read(&r, pos, want);
+#ifdef DEBUG
+    clear_progress();
+    printf(";; [bound] try reading %"PRIuSEC" .. %"PRIuSEC" -> %zd\n",
+          pos, pos + want, n);
+#endif
+    if (n == want) break;
+    if (n < 0) n = 0;
+    bad_hi = pos + n + 1;
+  }
+
+  good = pos;
+  while (good > bad_hi) {
+    report_bad_blocks_progress(bad_lo, bad_hi, errno);
+    pos = bad_hi + (good - bad_hi)/2;
+    step = pos - bad_lo;
+    want = run_length_wanted(pos, step, sz, end);
+    n = recovery_read(&r, pos, want);
+#ifdef DEBUG
+    clear_progress();
+    printf(";; [limit] try reading %"PRIuSEC" .. %"PRIuSEC" -> %zd\n",
+          pos, pos + want, n);
+#endif
+    if (n < 0) n = 0;
+    if (n == want) good = pos;
+    else bad_hi = pos + n + 1;
+  }
+  recovered(bad_lo, bad_hi); *pos_inout = good;
+  if (good < r.pos + r.start || r.pos + r.end <= good)
+    n = 0;
+  else {
+    n = r.pos + r.end - good;
+    rearrange_sectors(&r, 0, good - r.pos, n);
+  }
+#ifdef DEBUG
+  show_recovery_buffer_map(&r, "returning %zd good sectors at %"PRIuSEC"",
+                          n, good);
+#endif
+  return (n);
 }
 
-static void emit(struct source *src, int outfd, secaddr start, secaddr end)
+static void emit(secaddr start, secaddr end)
 {
 #define BUFSECTORS 512
 
-  int least, i;
+  int least;
   unsigned char buf[BUFSECTORS*SECTORSZ];
   secaddr pos;
-  secaddr bad_lo, bad_hi, good, step;
   size_t want;
   ssize_t n;
   static int first_time = 1;
@@ -427,6 +738,7 @@ static void emit(struct source *src, int outfd, secaddr start, secaddr end)
   struct file *f;
   char fn[MAXFNSZ];
   int act = -1;
+  int i;
 #endif
 
   least = least_live();
@@ -446,23 +758,23 @@ static void emit(struct source *src, int outfd, secaddr start, secaddr end)
 #endif
 
   if (least == -1)
-    { src->file = 0; src->vob = 0; }
+    { file = 0; vob = 0; }
   else {
-    src->file = &filetab.v[least];
-    switch (id_kind(src->file->id)) {
+    file = &filetab.v[least];
+    switch (id_kind(file->id)) {
       case RAW:
-       src->vob = 0;
+       vob = 0;
        break;
       case VOB:
        if (first_time) { clear_progress(); first_time = 0; }
-       src->vob = DVDOpenFile(src->dvd, id_title(src->file->id),
-                              id_part(src->file->id)
-                                ? DVD_READ_TITLE_VOBS
-                                : DVD_READ_MENU_VOBS);
-       if (!src->vob)
+       vob = DVDOpenFile(dvd, id_title(file->id),
+                         id_part(file->id)
+                           ? DVD_READ_TITLE_VOBS
+                           : DVD_READ_MENU_VOBS);
+       if (!vob)
          bail("failed to open %s %u",
-              id_part(src->file->id) ? "title" : "menu",
-              id_title(src->file->id));
+              id_part(file->id) ? "title" : "menu",
+              id_title(file->id));
        break;
       default:
        abort();
@@ -472,75 +784,14 @@ static void emit(struct source *src, int outfd, secaddr start, secaddr end)
   pos = start;
   while (pos < end) {
     want = end - pos; if (want > BUFSECTORS) want = BUFSECTORS;
-    n = read_sectors(src, pos, buf, want);
-
-    if (n <= 0) {
-      report_bad_blocks_progress(src, pos, pos, errno);
-      for (i = 0; i < 4; i++) {
-       n = read_sectors(src, pos, buf, 1);
-       if (n > 0) {
-         clear_progress();
-         moan("sector %"PRIuSEC" read ok after retry", pos);
-         bad_lo = bad_hi = pos;
-         goto recovered;
-       }
-      }
-
-      bad_lo = pos; step = 1; bad_hi = pos + 1;
-      for (;;) {
-       report_bad_blocks_progress(src, bad_lo, bad_hi, errno);
-       if (bad_hi >= end) {
-         clear_progress();
-         moan("giving up on this extent");
-         n = 0; goto recovered;
-       }
-       step *= 2;
-       if (step > end - bad_lo) step = end - bad_lo;
-       pos = bad_lo + step - 1;
-       n = read_sectors(src, pos, buf, 1);
-       if (n > 0) break;
-       bad_hi = pos + 1;
-      }
-
-      good = pos;
-      while (good > bad_hi) {
-       report_bad_blocks_progress(src, bad_lo, bad_hi, errno);
-       pos = bad_hi + (good - bad_hi)/2;
-       n = read_sectors(src, pos, buf, 1);
-       if (n > 0) good = pos;
-       else bad_hi = pos + 1;
-      }
-    recovered:
-      if (bad_hi > bad_lo) {
-       clear_progress();
-       moan("skipping %"PRIuSEC" bad sectors (%"PRIuSEC" .. %"PRIuSEC")",
-            bad_hi - bad_lo, bad_lo, bad_hi);
-       if (src->mapfile) {
-         if (!src->mapfp) {
-           src->mapfp = fopen(src->mapfile, "w");
-           if (!src->mapfp)
-             bail_syserr(errno, "failed to open bad-sector map file `%s'",
-                         optarg);
-           fprintf(src->mapfp, "## bad sector map\n\n");
-         }
-         fprintf(src->mapfp, "%"PRIuSEC" %"PRIuSEC"\n", bad_lo, bad_hi);
-         fflush(src->mapfp);
-         if (ferror(src->mapfp))
-           bail_syserr(errno, "error writing bad-sector map file");
-       }
-       if (outfd >= 0 &&
-           lseek(outfd, (off_t)(bad_hi - bad_lo)*SECTORSZ, SEEK_CUR) < 0)
-         bail_syserr(errno, "failed to seek past bad sectors");
-       status = 1;
-      }
-      pos = bad_hi;
-    }
+    n = read_sectors(pos, buf, want);
 
+    if (n <= 0) n = find_good_sector(&pos, end, buf, BUFSECTORS);
     if (n > 0) { carefully_write(outfd, buf, n*SECTORSZ); pos += n; }
-    report_progress(src, pos); fflush(stdout);
+    report_progress(pos); fflush(stdout);
   }
 
-  if (src->vob) { DVDCloseFile(src->vob); src->vob = 0; }
+  if (vob) { DVDCloseFile(vob); vob = 0; }
 
 #undef BUFSECTORS
 }
@@ -600,6 +851,49 @@ static int read_line(FILE *fp, struct buf *b)
   return (0);
 }
 
+#define PRF_HYPHEN 1u
+static int parse_range(const char *p, unsigned f,
+                      secaddr *start_out, secaddr *end_out)
+{
+  char *q;
+  int err, rc;
+  unsigned long start, end;
+
+  err = errno;
+
+  if (ISDIGIT(*p)) {
+    start = strtoul(p, &q, 0);
+    if (errno || start >= SECLIMIT) { rc = -1; goto end; }
+    *start_out = start; p = q;
+  } else if (!(f&PRF_HYPHEN))
+    { rc = -1; goto end; }
+  else
+    start = 0;
+
+  if (f&PRF_HYPHEN) {
+    if (*p != '-')  { rc = -1; goto end; }
+    p++;
+  } else {
+    if (!ISSPACE(*p)) { rc = -1; goto end; }
+    do p++; while (ISSPACE(*p));
+  }
+
+  if (ISDIGIT(*p)) {
+    end = strtoul(p, &q, 0);
+    if (errno || end > SECLIMIT || end < start) { rc = -1; goto end; }
+    *end_out = end; p = q;
+  } else if (!(f&PRF_HYPHEN))
+    { rc = -1; goto end; }
+
+  if (!(f&PRF_HYPHEN)) while (ISSPACE(*p)) p++;
+  if (*p) { rc = -1; goto end; }
+
+  rc = 0;
+end:
+  errno = err;
+  return (rc);
+}
+
 int main(int argc, char *argv[])
 {
   unsigned f = 0;
@@ -607,11 +901,11 @@ int main(int argc, char *argv[])
   uint64_t volsz;
   secaddr pos;
   off_t off;
-  struct source src = SOURCE_INIT;
-  unsigned long start, end;
+  secaddr start, end, last;
   const struct event *ev;
-  const char *device = "/dev/dvd", *outfile = 0;
-  int opt, err, outfd = -1, blksz;
+  const char *device, *outfile;
+  struct badblock *bad;
+  int opt, blksz;
   unsigned n;
   size_t i;
   FILE *fp;
@@ -629,93 +923,100 @@ int main(int argc, char *argv[])
 
   p = strrchr(argv[0], '/'); prog = p ? p + 1 : argv[0];
   for (;;) {
-    opt = getopt(argc, argv, "hD:FR:b:co:r:"); if (opt < 0) break;
+    opt = getopt(argc, argv, "hFR:X:b:cr:"); if (opt < 0) break;
     switch (opt) {
       case 'h': usage(stderr); exit(0);
-      case 'D': device = optarg; break;
       case 'F': f |= f_fixup; break;
       case 'R':
        fp = fopen(optarg, "r");
        if (!fp)
          bail_syserr(errno, "failed to open ranges file `%s'", optarg);
-       i = 0;
+       i = 0; last = -1;
        for (;;) {
          BUF_REWIND(&buf); if (read_line(fp, &buf)) break;
          p = buf.p; i++;
          while (ISSPACE(*p)) p++;
          if (!*p || *p == '#') continue;
-         if (!ISDIGIT(*p)) goto bad_range_file;
-         start = strtoul(p, &p, 0);
-         if (errno || !ISSPACE(*p)) goto bad_range_file;
-         do p++; while (ISSPACE(*p));
-         if (!ISDIGIT(*p)) goto bad_range_file;
-         end = strtoul(p, &p, 0);
-         if (errno || (*p && !ISSPACE(*p))) goto bad_range_file;
-         while (ISSPACE(*p)) p++;
-         if (*p) goto bad_range_file;
-         if (start > end) goto bad_range_file;
+         if (parse_range(p, 0, &start, &end) ||
+             (last <= SECLIMIT && start < last))
+           bail("bad range `%s' at `%s' line %zu", buf.p, optarg, i);
          if (start < end) {
-           put_event(EV_WRITE, 0, start);
-           put_event(EV_STOP, 0, end);
+           if (start == last)
+             eventq.v[eventq.n - 1].pos = end;
+           else {
+             put_event(EV_WRITE, 0, start);
+             put_event(EV_STOP, 0, end);
+           }
+           last = end;
          }
        }
        if (ferror(fp))
          bail_syserr(errno, "failed to read ranges file `%s'", optarg);
        break;
-      bad_range_file:
-       bail("bad range `%s' at `%s' line %zu", buf.p, optarg, i);
+      case 'X':
+       fp = fopen(optarg, "r");
+       if (!fp)
+         bail_syserr(errno, "failed to open bad-blocks file `%s'", optarg);
+       i = 0; last = -1;
+       for (;;) {
+         BUF_REWIND(&buf); if (read_line(fp, &buf)) break;
+         p = buf.p; i++;
+         while (ISSPACE(*p)) p++;
+         if (!*p || *p == '#') continue;
+         if (parse_range(p, 0, &start, &end) ||
+             (last <= SECLIMIT && start < last))
+           bail("bad range `%s' at `%s' line %zu", buf.p, optarg, i);
+         if (start < end)
+           { VEC_PUSH(bad, &badblocks); bad->start = start; bad->end = end; }
+       }
+       if (ferror(fp))
+         bail_syserr(errno, "failed to read bad-blocks file `%s'", optarg);
+       break;
       case 'b':
-       if (src.mapfile) bail("can't have multiple map files");
-       src.mapfile = optarg;
+       if (mapfile) bail("can't have multiple map files");
+       mapfile = optarg;
        break;
       case 'c': f |= f_continue; break;
-      case 'o': outfile = optarg; break;
       case 'r':
-       err = errno; errno = 0;
-       p = optarg;
-       if (*p == '-')
-         start = 0;
-       else {
-         if (!ISDIGIT(*p)) goto bad_range;
-         start = strtoul(p, &p, 0);
-         if (errno || *p != '-') goto bad_range;
-       }
-       p++;
-       if (!*p)
+       start = 0; end = -1;
+       if (parse_range(optarg, PRF_HYPHEN, &start, &end))
+         bail("bad range `%s'", optarg);
+       if (start < end) {
          put_event(EV_WRITE, 0, start);
-       else {
-         if (!ISDIGIT(*p)) goto bad_range;
-         end = strtoul(p, &p, 0);
-         if (errno || *p) goto bad_range;
-         if (start > end) goto bad_range;
-         if (start < end) {
-           put_event(EV_WRITE, 0, start);
-           put_event(EV_STOP, 0, end);
-         }
+         if (end <= SECLIMIT) put_event(EV_STOP, 0, end);
        }
-       errno = err;
-       break;
-      bad_range:
-       bail("bad range `%s'", optarg);
        break;
       default: f |= f_bogus; break;
     }
   }
-  if (optind < argc) f |= f_bogus;
+  if (argc - optind != 2) f |= f_bogus;
   if (f&f_bogus) { usage(stderr); exit(2); }
 
-  src.dvdfd = open(device, O_RDONLY);
-  if (src.dvdfd < 0)
+  device = argv[optind]; outfile = argv[optind + 1];
+
+  if (badblocks.n) {
+    qsort(badblocks.v, badblocks.n, sizeof(struct badblock),
+         compare_badblock);
+#ifdef DEBUG
+    printf(";; fake bad blocks:\n");
+    for (i = 0; i < badblocks.n; i++)
+      printf(";;\t%8"PRIuSEC" .. %"PRIuSEC"\n",
+            badblocks.v[i].start, badblocks.v[i].end);
+#endif
+  }
+
+  dvdfd = open(device, O_RDONLY);
+  if (dvdfd < 0)
     bail_syserr(errno, "failed to open device `%s'", device);
-  if (fstat(src.dvdfd, &st))
+  if (fstat(dvdfd, &st))
     bail_syserr(errno, "failed to stat device `%s'", device);
   if (S_ISREG(st.st_mode)) {
     blksz = SECTORSZ;
     volsz = st.st_size;
   } else if (S_ISBLK(st.st_mode)) {
-    if (ioctl(src.dvdfd, BLKSSZGET, &blksz))
+    if (ioctl(dvdfd, BLKSSZGET, &blksz))
       bail_syserr(errno, "failed to get block size for `%s'", device);
-    if (ioctl(src.dvdfd, BLKGETSIZE64, &volsz))
+    if (ioctl(dvdfd, BLKGETSIZE64, &volsz))
       bail_syserr(errno, "failed to get volume size for `%s'", device);
   } else
     bail("can't use `%s' as source: expected file or block device", device);
@@ -733,7 +1034,6 @@ int main(int argc, char *argv[])
   }
 
   if (f&f_continue) {
-    if (!outfile) bail("can't continue without output file");
     off = lseek(outfd, 0, SEEK_END);
     if (off < 0)
       bail_syserr(errno, "failed to seek to end of output file `%s'",
@@ -743,25 +1043,25 @@ int main(int argc, char *argv[])
     put_event(EV_WRITE, 0, 0);
 
 #ifdef notdef
-  src.dvd = DVDOpen2(0, &logger, device);
+  dvd = DVDOpen2(0, &logger, device);
 #else
-  src.dvd = DVDOpen(device);
+  dvd = DVDOpen(device);
 #endif
-  if (!src.dvd) bail("failed to open DVD on `%s'", device);
+  if (!dvd) bail("failed to open DVD on `%s'", device);
 
   /* It's fast enough just to check everything. */
-  put_menu(src.dvd, 0);
+  put_menu(dvd, 0);
   for (i = 1; i < 100; i++) {
-    put_menu(src.dvd, i);
-    put_title(src.dvd, i);
+    put_menu(dvd, i);
+    put_title(dvd, i);
   }
   put_file(mkident(RAW, 0, 0), 0, volsz/SECTORSZ);
   assert(filetab.n <= MAXFILES);
 
-  for (i = 0, src.limit = 0; i < filetab.n; i++)
-    if (filetab.v[i].end > src.limit) src.limit = filetab.v[i].end;
+  for (i = 0, limit = 0; i < filetab.n; i++)
+    if (filetab.v[i].end > limit) limit = filetab.v[i].end;
 
-  if (end > src.limit) end = src.limit;
+  if (end > limit) end = limit;
 
 #ifdef DEBUG
   printf("\n;; files:\n");
@@ -780,7 +1080,7 @@ int main(int argc, char *argv[])
     switch (ev->ev) {
       case EV_WRITE:
        if (f&f_write)
-         bail("overlapping ranges: range from %lu still open at %"PRIuSEC"",
+         bail("overlapping ranges: range from %"PRIuSEC" still open at %"PRIuSEC"",
               start, ev->pos);
        n++; f |= f_write; start = ev->pos;
        break;
@@ -795,9 +1095,9 @@ int main(int argc, char *argv[])
     ev = &eventq.v[i];
     switch (ev->ev) {
       case EV_WRITE: start = ev->pos; f |= f_write; break;
-      case EV_STOP: src.nsectors += ev->pos - start; f &= ~f_write; break;
+      case EV_STOP: nsectors += ev->pos - start; f &= ~f_write; break;
     }
-    if (ev->pos >= src.limit) break;
+    if (ev->pos >= limit) break;
     if (f&f_fixup) start = ev->pos;
   }
   eventq.n = i;
@@ -806,11 +1106,10 @@ int main(int argc, char *argv[])
     n++; f |= f_write;
   }
   if (f&f_write) {
-    src.nsectors += src.limit - start;
-    put_event(EV_STOP, 0, src.limit);
+    nsectors += limit - start;
+    put_event(EV_STOP, 0, limit);
   }
-  if (n == 1 && (f&f_write))
-    src.f |= SRCF_ALLPROGRESS;
+  if (n == 1 && (f&f_write)) flags |= F_ALLPROGRESS;
   f &= ~f_write;
 
 #ifdef DEBUG
@@ -819,7 +1118,7 @@ int main(int argc, char *argv[])
   for (pos = 0, i = 0; i < eventq.n; i++) {
     ev = &eventq.v[i];
     if (ev->pos > pos) {
-      if (f&f_write) emit(&src, outfd, pos, ev->pos);
+      if (f&f_write) emit(pos, ev->pos);
       pos = ev->pos;
 #ifdef DEBUG
       clear_progress();
@@ -836,9 +1135,8 @@ int main(int argc, char *argv[])
 #endif
        break;
       case EV_WRITE:
-       gettimeofday(&src.last_time, 0); src.last_pos = pos;
-       if (outfd >= 0 &&
-           lseek(outfd, (off_t)ev->pos*SECTORSZ, SEEK_SET) < 0)
+       gettimeofday(&last_time, 0); last_pos = pos;
+       if (lseek(outfd, (off_t)ev->pos*SECTORSZ, SEEK_SET) < 0)
          bail_syserr(errno,
                      "failed to seek to resume position "
                      "(sector %"PRIuSEC") in output file `%s'",
@@ -870,14 +1168,14 @@ int main(int argc, char *argv[])
 
   if (progresslen) putchar('\n');
 
-  if (outfd >= 0 && ftruncate(outfd, (off_t)src.limit*SECTORSZ) < 0)
+  if (ftruncate(outfd, (off_t)limit*SECTORSZ) < 0)
     bail_syserr(errno, "failed to set output file `%s' length", outfile);
 
-  if (src.dvd) DVDClose(src.dvd);
-  if (src.dvdfd >= 0) close(src.dvdfd);
+  if (dvd) DVDClose(dvd);
+  if (dvdfd >= 0) close(dvdfd);
   if (outfd >= 0) close(outfd);
-  if (src.mapfp) {
-    if (ferror(src.mapfp) || fclose(src.mapfp))
+  if (mapfp) {
+    if (ferror(mapfp) || fclose(mapfp))
       bail_syserr(errno, "error writing bad-sector map file");
   }