From: Mark Wooding Date: Fri, 8 Apr 2022 14:40:49 +0000 (+0100) Subject: lib.[ch], *.c: Have `open_dvd' return errors rather than bailing. X-Git-Url: https://git.distorted.org.uk/~mdw/dvdrip/commitdiff_plain/d23998cb71b4c5b65f833893b2c9f6f70f782720 lib.[ch], *.c: Have `open_dvd' return errors rather than bailing. In particular, this means that `dvd-id' can report on all of the other discs. --- diff --git a/chkdvdimg.c b/chkdvdimg.c index cd6b0c5..3a94463 100644 --- a/chkdvdimg.c +++ b/chkdvdimg.c @@ -104,7 +104,8 @@ static void check_img(const char *file) secaddr end; unsigned i, j; - open_dvd(file, (flags&F_FIX) ? O_RDWR : O_RDONLY, &fd, 0); + if (open_dvd(file, (flags&F_FIX) ? O_RDWR : O_RDONLY, &fd, 0)) + { set_status(2); goto end; } blksz = SECTORSZ; volsz = device_size(fd, file, &blksz); if (SECTORSZ != 2048) { gripe(2, "device sector size %d /= 2048", blksz); goto end; } diff --git a/dvd-cache-keys.c b/dvd-cache-keys.c index db085b6..8af2176 100644 --- a/dvd-cache-keys.c +++ b/dvd-cache-keys.c @@ -41,7 +41,7 @@ int main(int argc, char *argv[]) progress_init(&progress); for (i = optind; i < argc; i++) { - open_dvd(argv[i], O_RDONLY, 0, &dvd); + if (open_dvd(argv[i], O_RDONLY, 0, &dvd)) exit(2); for (j = 0; j < 100; j++) { kick_vob(j, 0); diff --git a/dvd-check-keys.c b/dvd-check-keys.c index 1bdc082..edbce10 100644 --- a/dvd-check-keys.c +++ b/dvd-check-keys.c @@ -62,7 +62,7 @@ int main(int argc, char *argv[]) setlocale(LC_ALL, ""); progress_init(&progress); - open_dvd(argv[optind], O_RDONLY, 0, &dvd); + if (open_dvd(argv[optind], O_RDONLY, 0, &dvd)) exit(2); css = dvdcss_open(argv[optind]); if (!css) bail_syserr(errno, "failed to attach CSS machinery to `%s'", diff --git a/dvd-id.c b/dvd-id.c index 72059b9..496b6bb 100644 --- a/dvd-id.c +++ b/dvd-id.c @@ -8,7 +8,7 @@ int main(int argc, char *argv[]) char id[MAXIDSZ]; int i, opt, st = 0; unsigned f = 0, dif = 0; - static dvd_reader_t *dvd; + static dvd_reader_t *dvd = 0; #define f_bogus 1u set_prog(argv[0]); @@ -27,14 +27,14 @@ int main(int argc, char *argv[]) progress_init(&progress); for (i = optind; i < argc; i++) { - open_dvd(argv[i], O_RDONLY, 0, &dvd); - if (dvd_id(id, dvd, dif, argv[i])) + if (open_dvd(argv[i], O_RDONLY, 0, &dvd) || + dvd_id(id, dvd, dif, argv[i])) st = 2; else { if (argc - optind > 1) printf("%s: ", argv[i]); printf("%s\n", id); } - DVDClose(dvd); + DVDClose(dvd); dvd = 0; } progress_free(&progress); diff --git a/dvd-info.c b/dvd-info.c index 93d9567..8d51ef7 100644 --- a/dvd-info.c +++ b/dvd-info.c @@ -167,7 +167,7 @@ int main(int argc, char *argv[]) if (f&f_bogus) { usage(stderr); exit(2); } setlocale(LC_ALL, ""); progress_init(&progress); - dvdfn = argv[optind]; open_dvd(dvdfn, O_RDONLY, 0, &dvd); + dvdfn = argv[optind]; if (open_dvd(dvdfn, O_RDONLY, 0, &dvd)) exit(2); vmgi = ifoOpenVMGI(dvd); if (!vmgi) bail("failed to open vmgi for `%s'", dvdfn); diff --git a/dvd-sector-copy.c b/dvd-sector-copy.c index a608410..81de82f 100644 --- a/dvd-sector-copy.c +++ b/dvd-sector-copy.c @@ -1043,7 +1043,7 @@ int main(int argc, char *argv[]) #endif } - open_dvd(device, O_RDONLY, &dvdfd, &dvd); + if (open_dvd(device, O_RDONLY, &dvdfd, &dvd)) exit(2); blksz = SECTORSZ; volsz = device_size(dvdfd, device, &blksz); if (blksz != SECTORSZ) @@ -1053,7 +1053,7 @@ int main(int argc, char *argv[]) device, volsz, SECTORSZ); if (f&f_checkid) { - open_dvd(outfile, O_RDONLY, 0, &dvd_out); + if (open_dvd(outfile, O_RDONLY, 0, &dvd_out)) exit(2); if (dvd_id(id_in, dvd, DIF_MUSTIFOHASH, device) || dvd_id(id_out, dvd_out, DIF_MUSTIFOHASH, device)) exit(2); diff --git a/lib.c b/lib.c index 2147228..5bf0394 100644 --- a/lib.c +++ b/lib.c @@ -260,12 +260,12 @@ static void logfn(void *p, dvd_logger_level_t lev, static const dvd_logger_cb logger = { logfn }; #endif -void open_dvd(const char *device, int mode, - int *fd_out, dvd_reader_t **dvd_out) +int open_dvd(const char *device, int mode, + int *fd_out, dvd_reader_t **dvd_out) { - int fd; - dvd_reader_t *dvd; - int bannerp = 0; + int fd = -1; + dvd_reader_t *dvd = 0; + int bannerp = 0, rc; for (;;) { fd = open(device, mode); @@ -277,18 +277,32 @@ void open_dvd(const char *device, int mode, sit(0.2); } if (bannerp) hide_banner(); - if (fd < 0) bail_syserr(errno, "failed to open device `%s'", device); + + if (fd < 0) { + moan_syserr(errno, "failed to open device `%s'", device); + rc = -1; goto end; + } + if (dvd_out) { #ifdef notdef dvd = DVDOpen2(0, &logger, device); #else dvd = DVDOpen(device); #endif - if (!dvd) bail("failed to open DVD on `%s'", device); - *dvd_out = dvd; + if (!dvd) { + moan("failed to open DVD on `%s'", device); + rc = -1; goto end; + } } - if (fd_out) *fd_out = fd; - else close(fd); + + if (fd_out) { *fd_out = fd; fd = -1; } + if (dvd_out) { *dvd_out = dvd; dvd = 0; } + rc = 0; + +end: + if (fd >= 0) close(fd); + DVDClose(dvd); + return (rc); } void store_filename(char *buf, ident id) diff --git a/lib.h b/lib.h index b982286..7c4a0cb 100644 --- a/lib.h +++ b/lib.h @@ -301,19 +301,19 @@ extern void hide_banner(void); /*----- DVD utilities -----------------------------------------------------*/ -extern void open_dvd(const char *device, int mode, - int *fd_out, dvd_reader_t **dvd_out); +extern int open_dvd(const char *device, int mode, + int *fd_out, dvd_reader_t **dvd_out); /* Open the DEVICE. If FD_OUT is not null, then open a file * descriptor onto the device, with the given open(2)-style MODE, * storing the descriptor in *FD_OUT; if DVD_OUT is not null, then * open a `libdvdread' handle onto the devie and store it in * *DVD_OUT. If both are null, then why are you calling this - * function? + * function? Returns 0 on success or -1 on failure. * * If DEVICE refers to an actual block device, and no medium is * currently inserted, then put up a banner prompting the user and - * wait for a medium to be inserted. Other problems are reported as - * fatal errors. + * wait for a medium to be inserted. Other problems are reported to + * stderr. */ enum { RAW, IFO, VOB, BUP };