chiark / gitweb /
dvd-sector-copy.c: Introduce type alias and print macro for sector numbers.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 16 Feb 2022 18:00:31 +0000 (18:00 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 16 Feb 2022 19:23:44 +0000 (19:23 +0000)
dvd-sector-copy.c

index 654a601a87edd4b0778413a0f13f01cd8f62abc6..01dd6a57f0989fdcedc82cfa8d35fdc972c4cfb1 100644 (file)
@@ -122,10 +122,13 @@ static void store_filename(char *buf, ident id)
   (p) = &(vv)->v[(vv)->n++];                                           \
 } while (0)
 
+typedef uint_least32_t secaddr;
+#define PRIuSEC PRIuLEAST32
+
 #define MAXFILES (1 + 2*99 + 1)
 struct file {
   ident id;
-  uint32_t start, end;
+  secaddr start, end;
 };
 DEFVEC(file_v, struct file);
 static file_v filetab = VEC_INIT;
@@ -133,7 +136,7 @@ static file_v filetab = VEC_INIT;
 enum { EV_STOP, EV_BEGIN, EV_END, EV_WRITE };
 struct event {
   unsigned char ev, file;
-  uint32_t pos;
+  secaddr pos;
 };
 DEFVEC(event_v, struct event);
 static event_v eventq = VEC_INIT;
@@ -165,7 +168,7 @@ found:
   return (i);
 }
 
-static void put_event(unsigned evtype, unsigned file, uint32_t pos)
+static void put_event(unsigned evtype, unsigned file, secaddr pos)
 {
   struct event *ev;
 
@@ -173,7 +176,7 @@ static void put_event(unsigned evtype, unsigned file, uint32_t pos)
   ev->ev = evtype; ev->file = file; ev->pos = pos;
 }
 
-static void put_file(ident id, uint32_t start, uint32_t end)
+static void put_file(ident id, secaddr start, secaddr end)
 {
   struct file *f;
   size_t i;
@@ -188,12 +191,12 @@ static void put_menu(dvd_reader_t *dvd, unsigned title)
 {
   ident id = mkident(VOB, title, 0);
   char fn[MAXFNSZ];
-  uint32_t start, len;
+  secaddr start, len;
 
   store_filename(fn, id);
   start = UDFFindFile(dvd, fn, &len); if (!start) return;
 #ifdef DEBUG
-      printf(";; %8"PRIu32" .. %-8"PRIu32": %s\n",
+      printf(";; %8"PRIuSEC" .. %-8"PRIuSEC": %s\n",
             start, start + SECTORS(len), fn);
 #endif
   put_file(id, start, start + SECTORS(len));
@@ -202,7 +205,7 @@ static void put_menu(dvd_reader_t *dvd, unsigned title)
 static void put_title(dvd_reader_t *dvd, unsigned title)
 {
   char fn[MAXFNSZ];
-  uint32_t start[9], len[9];
+  secaddr start[9], len[9];
   unsigned i, npart;
 
   for (i = 0; i < 9; i++) {
@@ -214,7 +217,7 @@ static void put_title(dvd_reader_t *dvd, unsigned title)
 #ifdef DEBUG
   for (i = 0; i < npart; i++) {
     store_filename(fn, mkident(VOB, title, i + 1));
-    printf(";; %8"PRIu32" .. %-8"PRIu32": %s\n",
+    printf(";; %8"PRIuSEC" .. %-8"PRIuSEC": %s\n",
           start[i], start[i] + SECTORS(len[i]), fn);
   }
 #endif
@@ -222,11 +225,12 @@ static void put_title(dvd_reader_t *dvd, unsigned title)
   if (npart > 1)
     for (i = 0; i < npart - 1; i++) {
       if (len[i]%SECTORSZ)
-       bail("title %u part %u length = %"PRIu32" not a multiple of %d",
+       bail("title %u part %u length = %"PRIuSEC" not a multiple of %d",
             title, i, len[i], SECTORSZ);
       if (start[i] + len[i]/SECTORSZ != start[i + 1])
-       bail("title %u part %u end = %"PRIu32" /= part %u start = %"PRIu32"",
-            title, i, start[i] + len[i]/SECTORSZ, i + 1, start[i + 1]);
+       bail
+         ("title %u part %u end = %"PRIuSEC" /= part %u start = %"PRIuSEC"",
+          title, i, start[i] + len[i]/SECTORSZ, i + 1, start[i + 1]);
     }
 
   put_file(mkident(VOB, title, 1),
@@ -285,14 +289,14 @@ struct source {
 
   unsigned f;
 #define SRCF_ALLPROGRESS 1u
-  uint32_t last_pos, limit, nsectors, ndone;
+  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 }
 
-static void report_progress(struct source *src, uint32_t pos)
+static void report_progress(struct source *src, secaddr pos)
 {
   char etastr[32];
   struct timeval now;
@@ -330,9 +334,9 @@ static void report_progress(struct source *src, uint32_t pos)
 
   if (src->f&SRCF_ALLPROGRESS) percent = pos*100.0/src->limit;
   else percent = src->ndone*100.0/src->nsectors;
-  print_progress("copied %.1f%% (%"PRIu32" of %"PRIu32"; %.1f %sB/s, ETA %s)",
-                percent, pos, src->limit,
-                rate, unit, etastr);
+  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) {
     append_progress(" -- %s %d %3.1f%%",
                    id_part(src->file->id) ? "title" : "menu",
@@ -346,20 +350,20 @@ static void report_progress(struct source *src, uint32_t pos)
 }
 
 static void report_bad_blocks_progress(struct source *src,
-                                      uint32_t lo, uint32_t hi, int err)
+                                      secaddr lo, secaddr hi, int err)
 {
   report_progress(src, hi);
 
   if (lo == hi) append_progress(": retrying bad sector");
   else
-    append_progress(": %"PRIu32" bad %s",
+    append_progress(": %"PRIuSEC" bad %s",
                    hi - lo, hi == lo + 1 ? "sector" : "sectors");
   if (err != EIO) append_progress(" (%s)", strerror(err));
   fflush(stdout);
 }
 
-static ssize_t read_sectors(struct source *src, uint32_t pos,
-                           void *buf, uint32_t want)
+static ssize_t read_sectors(struct source *src, secaddr pos,
+                           void *buf, secaddr want)
 {
   ssize_t n;
 
@@ -368,7 +372,7 @@ again:
     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 %"PRIu32"", pos);
+      bail_syserr(errno, "failed to seek to sector %"PRIuSEC"", pos);
     n = read(src->dvdfd, buf, want*SECTORSZ);
     if (n >= 0) n /= SECTORSZ;
   } else {
@@ -397,14 +401,14 @@ static void carefully_write(int fd, const void *buf, size_t sz)
   }
 }
 
-static void emit(struct source *src, int outfd, uint32_t start, uint32_t end)
+static void emit(struct source *src, int outfd, secaddr start, secaddr end)
 {
 #define BUFSECTORS 512
 
   int least, i;
   unsigned char buf[BUFSECTORS*SECTORSZ];
-  uint32_t pos;
-  uint32_t bad_lo, bad_hi, good, step;
+  secaddr pos;
+  secaddr bad_lo, bad_hi, good, step;
   size_t want;
   ssize_t n;
   static int first_time = 1;
@@ -417,13 +421,13 @@ static void emit(struct source *src, int outfd, uint32_t start, uint32_t end)
   least = least_live();
 
 #ifdef DEBUG
-  printf(";; %8"PRIu32" .. %"PRIu32"\n", start, end);
+  printf(";; %8"PRIuSEC" .. %"PRIuSEC"\n", start, end);
 
   for (i = 0; i < filetab.n; i++) {
     if (!livep(i)) continue;
     if (act == -1) act = i;
     f = &filetab.v[i]; store_filename(fn, f->id);
-    printf(";;\t\t%8"PRIu32" .. %-8"PRIu32" %s\n",
+    printf(";;\t\t%8"PRIuSEC" .. %-8"PRIuSEC" %s\n",
           start - f->start, end - f->start, fn);
   }
   if (act == -1) printf(";;\t\t#<no live source>\n");
@@ -465,7 +469,7 @@ static void emit(struct source *src, int outfd, uint32_t start, uint32_t end)
        n = read_sectors(src, pos, buf, 1);
        if (n > 0) {
          clear_progress();
-         fprintf(stderr, "%s: sector %"PRIu32" read ok after retry\n",
+         fprintf(stderr, "%s: sector %"PRIuSEC" read ok after retry\n",
                  prog, pos);
          bad_lo = bad_hi = pos;
          goto recovered;
@@ -499,8 +503,8 @@ static void emit(struct source *src, int outfd, uint32_t start, uint32_t end)
     recovered:
       if (bad_hi > bad_lo) {
        clear_progress();
-       fprintf(stderr, "%s: skipping %"PRIu32" bad sectors "
-               "(%"PRIu32" .. %"PRIu32")\n",
+       fprintf(stderr, "%s: skipping %"PRIuSEC" bad sectors "
+               "(%"PRIuSEC" .. %"PRIuSEC")\n",
                prog, bad_hi - bad_lo, bad_lo, bad_hi);
        if (src->mapfile) {
          if (!src->mapfp) {
@@ -510,7 +514,7 @@ static void emit(struct source *src, int outfd, uint32_t start, uint32_t end)
                          optarg);
            fprintf(src->mapfp, "## bad sector map\n\n");
          }
-         fprintf(src->mapfp, "%"PRIu32" %"PRIu32"\n", bad_lo, bad_hi);
+         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");
@@ -592,7 +596,7 @@ int main(int argc, char *argv[])
   unsigned f = 0;
   char *p;
   uint64_t volsz;
-  uint32_t pos;
+  secaddr pos;
   off_t off;
   struct source src = SOURCE_INIT;
   unsigned long start, end;
@@ -755,7 +759,7 @@ int main(int argc, char *argv[])
   for (i = 0; i < filetab.n; i++) {
     file = &filetab.v[i];
     store_filename(fn, file->id);
-    printf(";;\t%8"PRIu32" %s\n", file->start, fn);
+    printf(";;\t%8"PRIuSEC" %s\n", file->start, fn);
   }
 #endif
 
@@ -767,7 +771,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 %"PRIu32"",
+         bail("overlapping ranges: range from %lu still open at %"PRIuSEC"",
               start, ev->pos);
        n++; f |= f_write; start = ev->pos;
        break;
@@ -819,7 +823,7 @@ int main(int argc, char *argv[])
 #ifdef DEBUG
        store_filename(fn, filetab.v[ev->file].id);
        clear_progress();
-       printf(";; %8"PRIu32": begin `%s'\n", pos, fn);
+       printf(";; %8"PRIuSEC": begin `%s'\n", pos, fn);
 #endif
        break;
       case EV_WRITE:
@@ -828,11 +832,11 @@ int main(int argc, char *argv[])
            lseek(outfd, (off_t)ev->pos*SECTORSZ, SEEK_SET) < 0)
          bail_syserr(errno,
                      "failed to seek to resume position "
-                     "(sector %"PRIu32") in output file `%s'",
+                     "(sector %"PRIuSEC") in output file `%s'",
                      ev->pos, outfile);
 #ifdef DEBUG
        clear_progress();
-       printf(";; %8"PRIu32": begin write\n", pos);
+       printf(";; %8"PRIuSEC": begin write\n", pos);
 #endif
        f |= f_write;
        break;
@@ -840,7 +844,7 @@ int main(int argc, char *argv[])
        f &= ~f_write;
 #ifdef DEBUG
        clear_progress();
-       printf(";; %8"PRIu32": end write\n", pos);
+       printf(";; %8"PRIuSEC": end write\n", pos);
 #endif
        break;
       case EV_END:
@@ -848,7 +852,7 @@ int main(int argc, char *argv[])
 #ifdef DEBUG
        store_filename(fn, filetab.v[ev->file].id);
        clear_progress();
-       printf(";; %8"PRIu32": end `%s'\n", pos, fn);
+       printf(";; %8"PRIuSEC": end `%s'\n", pos, fn);
 #endif
        break;
       default: abort();