dvd-sector-copy.c: Export device-size machinery to the library.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 6 Mar 2022 23:54:02 +0000 (23:54 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 6 Mar 2022 23:54:02 +0000 (23:54 +0000)
dvd-sector-copy.c
lib.c
lib.h

index 2017689..cdd2849 100644 (file)
@@ -940,7 +940,7 @@ int main(int argc, char *argv[])
 {
   unsigned f = 0;
   const char *p;
-  uint64_t volsz;
+  off_t volsz;
   secaddr pos;
   off_t off;
   secaddr start, end, last;
@@ -957,7 +957,6 @@ int main(int argc, char *argv[])
   const char *rateunit, *totunit;
   char timebuf[TIMESTRMAX], id_in[MAXIDSZ], id_out[MAXIDSZ];
   dvd_reader_t *dvd_out;
-  struct stat st;
 #ifdef DEBUG
   const struct file *file;
   char fn[MAXFNSZ];
@@ -1092,19 +1091,8 @@ int main(int argc, char *argv[])
   }
 
   open_dvd(device, &dvdfd, &dvd);
-  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(dvdfd, BLKSSZGET, &blksz))
-      bail_syserr(errno, "failed to get block size for `%s'", device);
-    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);
 
+  blksz = SECTORSZ; volsz = device_size(dvdfd, device, &blksz);
   if (blksz != SECTORSZ)
     bail("device `%s' block size %d /= %d", device, blksz, SECTORSZ);
   if (volsz%SECTORSZ)
diff --git a/lib.c b/lib.c
index 80b1507..521af8d 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -85,6 +85,25 @@ void carefully_fclose(FILE *fp, const char *what)
     bail_syserr(errno, "error writing %s file", what);
 }
 
+off_t device_size(int fd, const char *file, int *blksz_out)
+{
+  struct stat st;
+  uint64_t volsz;
+
+  if (fstat(fd, &st))
+    bail_syserr(errno, "failed to obtain status for `%s'", file);
+  if (S_ISREG(st.st_mode))
+    volsz = st.st_size;
+  else if (S_ISBLK(st.st_mode)) {
+    if (ioctl(fd, BLKGETSIZE64, &volsz))
+      bail_syserr(errno, "failed to get volume size for `%s'", file);
+    if (ioctl(fd, BLKSSZGET, blksz_out))
+      bail_syserr(errno, "failed to get block size for `%s'", file);
+  } else
+    bail("can't read size for `%s': expected file or block device", file);
+  return ((off_t)volsz);
+}
+
 void store_filename(char *buf, ident id)
 {
   switch (id_kind(id)) {
diff --git a/lib.h b/lib.h
index bad7c3b..8a63e58 100644 (file)
--- a/lib.h
+++ b/lib.h
@@ -80,6 +80,7 @@ extern void open_file_on_demand(const char *file, FILE **fp_inout,
                                const char *what);
 extern void check_write(FILE *fp, const char *what);
 extern void carefully_fclose(FILE *fp, const char *what);
+extern off_t device_size(int fd, const char *file, int *blksz_out);
 
 enum { RAW, IFO, VOB, BUP };
 typedef uint_least32_t ident;