dvd-sector-copy.c: Use the correct character to match comments.
[dvdrip] / dvd-sector-copy.c
index b1f4a53..f70563c 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
@@ -98,6 +104,32 @@ static void carefully_write(int fd, const void *buf, size_t sz)
   }
 }
 
+static void open_file_on_demand(const char *file, FILE **fp_inout,
+                               const char *what)
+{
+  FILE *fp;
+
+  if (!*fp_inout) {
+    fp = fopen(file, "w");
+    if (!fp)
+      bail_syserr(errno, "failed to open %s file `%s'", what, file);
+    fprintf(fp, "## %s\n\n", what);
+    *fp_inout = fp;
+  }
+}
+
+static void check_write(FILE *fp, const char *what)
+{
+  fflush(fp);
+  if (ferror(fp)) bail_syserr(errno, "error writing %s file", what);
+}
+
+static void carefully_fclose(FILE *fp, const char *what)
+{
+  if (fp && (ferror(fp) || fclose(fp)))
+    bail_syserr(errno, "error writing %s file", what);
+}
+
 #define DEFVEC(vtype, etype)                                           \
        typedef struct { etype *v; size_t n, sz; } vtype
 #define VEC_INIT { 0, 0, 0 }
@@ -285,9 +317,16 @@ 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); }
+#ifdef DEBUG
+static void debug_clear_progress(void)
+  { if (progresslen) { putchar('\n'); progresslen = 0; } }
+#endif
 static void vappend_progress(const char *fmt, va_list ap)
   { progresslen += vprintf(fmt, ap); }
 __attribute__((format(printf, 1, 2)))
@@ -359,7 +398,7 @@ static void report_progress(secaddr pos)
     ("copied %.1f%% (%"PRIuSEC" of %"PRIuSEC"; %.1f %sB/s, ETA %s)",
      percent, pos, limit,  rate, unit, etastr);
   if (file && id_kind(file->id) == VOB) {
-    append_progress(" -- %s %d %3.1f%%",
+    append_progress(" -- %s %d %.1f%%",
                    id_part(file->id) ? "title" : "menu",
                    id_title(file->id),
                    (pos - file->start)*100.0/
@@ -370,6 +409,89 @@ static void report_progress(secaddr pos)
 #undef BETA
 }
 
+static dvd_reader_t *dvd;
+static int dvdfd = -1, outfd = -1;
+static dvd_file_t *vob;
+static const char *mapfile; static FILE *mapfp;
+static const char *errfile; static FILE *errfp;
+
+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) break;
+    else if (errno == EIO && errfile) {
+      open_file_on_demand(errfile, &errfp, "bad-sector error log");
+      fprintf(errfp, "%"PRIuSEC" %"PRIuSEC"\n", pos, pos + 1);
+      check_write(errfp, "bad-sector error log");
+      break;
+    } else if (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(hi);
@@ -378,33 +500,275 @@ static void report_bad_blocks_progress(secaddr lo, secaddr hi, int err)
   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 dvd_reader_t *dvd;
-static int dvdfd = -1, outfd = -1;
-static dvd_file_t *vob;
-static const char *mapfile; static FILE *mapfp;
+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) {
+    open_file_on_demand(mapfile, &mapfp, "bad-sector region map");
+    fprintf(mapfp, "%"PRIuSEC" %"PRIuSEC"", bad_lo, bad_hi);
+    if (file)
+      fprintf(mapfp, " # %s #%d %"PRIuSEC"..%"PRIuSEC" of %"PRIuSEC" (%.1f%%)",
+             id_part(file->id) ? "title" : "menu",
+             id_title(file->id),
+             bad_lo - file->start, bad_hi - file->start,
+             file->end - file->start,
+             (bad_lo - file->start)*100.0/(file->end - file->start));
+    fputc('\n', mapfp);
+    check_write(mapfp, "bad-sector region map");
+  }
+  if (lseek(outfd, (off_t)(bad_hi - bad_lo)*SECTORSZ, SEEK_CUR) < 0)
+    bail_syserr(errno, "failed to seek past bad sectors");
+  status = 1;
+}
 
-static ssize_t read_sectors(secaddr pos, void *buf, secaddr want)
+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);
+  debug_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 (vob)
-    n = DVDReadBlocks(vob, pos - file->start, want, buf);
-  else if (file) {
-    if (lseek(dvdfd, (off_t)pos*SECTORSZ, SEEK_SET) < 0)
-      bail_syserr(errno, "failed to seek to sector %"PRIuSEC"", pos);
-    n = read(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
+  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 (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, 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
+    }
   }
 
-  if (n < 0 && errno == EINTR) goto again;
+  n = r->pos + r->end - pos;
+  if (!n && want) n = -1;
+  else if (n > want) n = want;
+
+end:
+#ifdef DEBUG
+  show_recovery_buffer_map(r, "done; return %zd", n);
+#endif
+  return (n);
+}
+
+static secaddr run_length_wanted(secaddr pos, secaddr badlen,
+                                secaddr sz, secaddr end)
+{
+  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;
+
+  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
+    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);
+    }
+  }
+
+  bad_lo = pos; bad_hi = pos + 1;
+  for (;;) {
+    report_bad_blocks_progress(bad_lo, bad_hi, errno);
+#ifdef DEBUG
+    debug_clear_progress();
+    printf(";; bounding bad-block region: "
+          "%"PRIuSEC" ..%"PRIuSEC".. %"PRIuSEC"\n",
+          bad_lo, bad_hi - bad_lo, bad_hi);
+#endif
+    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
+    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);
+#ifdef DEBUG
+    debug_clear_progress();
+    printf(";; limiting bad-block region: "
+          "%"PRIuSEC" ..%"PRIuSEC".. %"PRIuSEC" ..%"PRIuSEC".. %"PRIuSEC"\n",
+          bad_lo, bad_hi - bad_lo, bad_hi, good - bad_hi, good);
+#endif
+    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
+    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);
 }
 
@@ -412,10 +776,9 @@ 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;
@@ -423,6 +786,7 @@ static void emit(secaddr start, secaddr end)
   struct file *f;
   char fn[MAXFNSZ];
   int act = -1;
+  int i;
 #endif
 
   least = least_live();
@@ -470,67 +834,7 @@ static void emit(secaddr start, secaddr end)
     want = end - pos; if (want > BUFSECTORS) want = BUFSECTORS;
     n = read_sectors(pos, buf, want);
 
-    if (n <= 0) {
-      report_bad_blocks_progress(pos, pos, errno);
-      for (i = 0; i < 4; i++) {
-       n = read_sectors(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(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(pos, buf, 1);
-       if (n > 0) break;
-       bad_hi = pos + 1;
-      }
-
-      good = pos;
-      while (good > bad_hi) {
-       report_bad_blocks_progress(bad_lo, bad_hi, errno);
-       pos = bad_hi + (good - bad_hi)/2;
-       n = read_sectors(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 (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;
-      }
-      pos = bad_hi;
-    }
-
+    if (n <= 0) n = find_good_sector(&pos, end, buf, BUFSECTORS);
     if (n > 0) { carefully_write(outfd, buf, n*SECTORSZ); pos += n; }
     report_progress(pos); fflush(stdout);
   }
@@ -630,7 +934,7 @@ static int parse_range(const char *p, unsigned f,
     { rc = -1; goto end; }
 
   if (!(f&PRF_HYPHEN)) while (ISSPACE(*p)) p++;
-  if (*p) { rc = -1; goto end; }
+  if (*p && ((f&PRF_HYPHEN) || *p != '#')) { rc = -1; goto end; }
 
   rc = 0;
 end:
@@ -645,9 +949,10 @@ int main(int argc, char *argv[])
   uint64_t volsz;
   secaddr pos;
   off_t off;
-  secaddr start, end;
+  secaddr start, end, last;
   const struct event *ev;
   const char *device, *outfile;
+  struct badblock *bad;
   int opt, blksz;
   unsigned n;
   size_t i;
@@ -666,15 +971,16 @@ int main(int argc, char *argv[])
 
   p = strrchr(argv[0], '/'); prog = p ? p + 1 : argv[0];
   for (;;) {
-    opt = getopt(argc, argv, "hFR:X:b:cr:"); if (opt < 0) break;
+    opt = getopt(argc, argv, "hE:FR:X:b:cr:"); if (opt < 0) break;
     switch (opt) {
       case 'h': usage(stderr); exit(0);
+      case 'E': errfile = 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++;
@@ -684,13 +990,37 @@ int main(int argc, char *argv[])
              (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;
+      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 (mapfile) bail("can't have multiple map files");
        mapfile = optarg;
@@ -713,6 +1043,17 @@ int main(int argc, char *argv[])
 
   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);
@@ -829,7 +1170,7 @@ int main(int argc, char *argv[])
       if (f&f_write) emit(pos, ev->pos);
       pos = ev->pos;
 #ifdef DEBUG
-      clear_progress();
+      debug_clear_progress();
       printf(";;\n");
 #endif
     }
@@ -838,7 +1179,7 @@ int main(int argc, char *argv[])
        set_live(ev->file);
 #ifdef DEBUG
        store_filename(fn, filetab.v[ev->file].id);
-       clear_progress();
+       debug_clear_progress();
        printf(";; %8"PRIuSEC": begin `%s'\n", pos, fn);
 #endif
        break;
@@ -850,7 +1191,7 @@ int main(int argc, char *argv[])
                      "(sector %"PRIuSEC") in output file `%s'",
                      ev->pos, outfile);
 #ifdef DEBUG
-       clear_progress();
+       debug_clear_progress();
        printf(";; %8"PRIuSEC": begin write\n", pos);
 #endif
        f |= f_write;
@@ -858,7 +1199,7 @@ int main(int argc, char *argv[])
       case EV_STOP:
        f &= ~f_write;
 #ifdef DEBUG
-       clear_progress();
+       debug_clear_progress();
        printf(";; %8"PRIuSEC": end write\n", pos);
 #endif
        break;
@@ -866,7 +1207,7 @@ int main(int argc, char *argv[])
        clear_live(ev->file);
 #ifdef DEBUG
        store_filename(fn, filetab.v[ev->file].id);
-       clear_progress();
+       debug_clear_progress();
        printf(";; %8"PRIuSEC": end `%s'\n", pos, fn);
 #endif
        break;
@@ -882,10 +1223,8 @@ int main(int argc, char *argv[])
   if (dvd) DVDClose(dvd);
   if (dvdfd >= 0) close(dvdfd);
   if (outfd >= 0) close(outfd);
-  if (mapfp) {
-    if (ferror(mapfp) || fclose(mapfp))
-      bail_syserr(errno, "error writing bad-sector map file");
-  }
+  carefully_fclose(mapfp, "bad-sector region map");
+  carefully_fclose(errfp, "bad-sector error log");
 
 #undef f_bogus
 #undef f_continue