From aa2b9c8c8db16f03e7a828673ded4dc0aaec714b Mon Sep 17 00:00:00 2001 From: Robert Harrison Date: Sat, 6 Feb 2021 21:46:39 +0800 Subject: [PATCH 1/3] add support for finding iso on usbdrive --- installer/kayak-menu | 1 + src/mount_media.c | 104 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/installer/kayak-menu b/installer/kayak-menu index d571a32..1d23e27 100755 --- a/installer/kayak-menu +++ b/installer/kayak-menu @@ -45,6 +45,7 @@ if [ $debug -eq 0 ]; then echo echo "Scanning for media..." mkdir /.cdrom + mkdir /.usbdrive /kayak/bin/mount_media $VERSION fi diff --git a/src/mount_media.c b/src/mount_media.c index 8ee8d38..d67677f 100644 --- a/src/mount_media.c +++ b/src/mount_media.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -48,6 +49,7 @@ #define HSFS_OPTS "ro" #define UFS_OPTS "ro,nologging,noatime" +#define PCFS_OPTS "ro" static boolean_t mounted = B_FALSE; static boolean_t verbose = B_FALSE; @@ -76,13 +78,110 @@ check_volsetid(const char *volid) return (ret); } +/* + * Mounts and tests supplied path to iso file for supplied volid + * Returns 0 if volid found, -1 otherwise. + */ +static int +test_iso_file(const char *path, const char *volid) +{ + int ret = -1; + char opts[MAX_MNTOPT_STR]; + + strcpy(opts, HSFS_OPTS); + if (verbose) + printf("%s: mount hsfs (iso)\n", path); + if (mount(path, "/.cdrom", MS_RDONLY | MS_OPTIONSTR, + "hsfs", NULL, 0, opts, sizeof (opts)) != 0) + return (ret); /* mount failed */ + + /* Mounted, see if it's the image we're looking for, unmount if not */ + ret = check_volsetid(volid); + if (ret != 0) { + if (verbose) + printf("%s: wrong ID, unmounting\n", path); + (void) umount("/.cdrom"); + } + return (ret); +} + +/* Compare two files by name */ +static int +fts_compare_files(const FTSENT **left, const FTSENT **right) +{ + return (strcmp((*left)->fts_name, (*right)->fts_name)); +} + +/* + * Attempt to mount the path as pcfs. Then walk the file tree + * and look for any iso files. + * For every one found, mount it and check for volid + * and umount if failed. return's 0 if the right iso is found + * and -1 otherwise. + */ +static int +check_for_iso(const char *path, const char *volid) +{ + int ret = -1; + char opts[MAX_MNTOPT_STR]; + + strcpy(opts, PCFS_OPTS); + if (verbose) + printf("%s: mount PCFS\n", path); + if (mount(path, "/.usbdrive", MS_RDONLY | MS_OPTIONSTR, + "pcfs", NULL, 0, opts, sizeof (opts)) != 0) { + return (ret); /* mount failed */ + } else { + /* mount succeeded, look for iso files */ + FTS *tree; + FTSENT *f; + char *pathtowalk[] = { "/.usbdrive", NULL }; + + tree = fts_open(pathtowalk, FTS_LOGICAL | FTS_NOSTAT, fts_compare_files); + if (tree == 0) { + if (verbose) + printf("%s: fts_open failed\n", path); + return (ret); /* traverse failed */ + } + + while ((f = fts_read(tree)) != 0 ) { + if (f->fts_info == FTS_F) { /* regular file */ + + if ( f->fts_namelen > 4 && + (strcmp(".iso", f->fts_name + f->fts_namelen - 4) == 0) ) { + if (verbose) + printf("iso found: %s\n", f->fts_name); + + ret = test_iso_file(f->fts_path, volid); + if (ret == 0) + break; /* correct iso found */ + } + } + } + + if (fts_close(tree) < 0) + if (verbose) + printf("error closing tree\n"); + + if (ret != 0) { + if (verbose) + printf("%s: wrong volume, unmounting\n", path); + (void) umount("/.usbdrive"); + /* if ret==0 we do not unmount the usbdrive mount */ + } + } + + return (ret); +} + static int mount_image(const char *path, const char *volid) { int ret = -1; char opts[MAX_MNTOPT_STR]; - /* First try mounting it as hsfs; if that fails, try ufs */ + /* First try mounting it as hsfs; if that fails, try ufs; if + that fails try check_for_iso() */ strcpy(opts, HSFS_OPTS); if (verbose) printf("%s: mount HSFS\n", path); @@ -93,7 +192,8 @@ mount_image(const char *path, const char *volid) printf("%s: mount UFS\n", path); if (mount(path, "/.cdrom", MS_OPTIONSTR, "ufs", NULL, 0, opts, sizeof (opts)) != 0) - return (ret); + if (check_for_iso(path, volid) != 0) + return (ret); } if (verbose) From 29b5ad6f085a3d7f7dc6e14174162280743a09b5 Mon Sep 17 00:00:00 2001 From: Robert Harrison Date: Sun, 7 Feb 2021 12:56:03 +0800 Subject: [PATCH 2/3] case insensitive filename compare --- src/mount_media.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mount_media.c b/src/mount_media.c index d67677f..4c298f7 100644 --- a/src/mount_media.c +++ b/src/mount_media.c @@ -49,7 +49,7 @@ #define HSFS_OPTS "ro" #define UFS_OPTS "ro,nologging,noatime" -#define PCFS_OPTS "ro" +#define PCFS_OPTS "ro" static boolean_t mounted = B_FALSE; static boolean_t verbose = B_FALSE; @@ -148,7 +148,7 @@ check_for_iso(const char *path, const char *volid) if (f->fts_info == FTS_F) { /* regular file */ if ( f->fts_namelen > 4 && - (strcmp(".iso", f->fts_name + f->fts_namelen - 4) == 0) ) { + (strcasecmp(".iso", f->fts_name + f->fts_namelen - 4) == 0) ) { if (verbose) printf("iso found: %s\n", f->fts_name); From bffae14e9b2aeb16e5e8fac72d8fbea7d9e5f9cf Mon Sep 17 00:00:00 2001 From: Robert Harrison Date: Sun, 7 Feb 2021 21:49:48 +0800 Subject: [PATCH 3/3] review and style updates --- src/mount_media.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/mount_media.c b/src/mount_media.c index 4c298f7..1e03050 100644 --- a/src/mount_media.c +++ b/src/mount_media.c @@ -105,15 +105,8 @@ test_iso_file(const char *path, const char *volid) return (ret); } -/* Compare two files by name */ -static int -fts_compare_files(const FTSENT **left, const FTSENT **right) -{ - return (strcmp((*left)->fts_name, (*right)->fts_name)); -} - /* - * Attempt to mount the path as pcfs. Then walk the file tree + * Attempt to mount the path as pcfs. Then walk the file tree * and look for any iso files. * For every one found, mount it and check for volid * and umount if failed. return's 0 if the right iso is found @@ -129,7 +122,7 @@ check_for_iso(const char *path, const char *volid) if (verbose) printf("%s: mount PCFS\n", path); if (mount(path, "/.usbdrive", MS_RDONLY | MS_OPTIONSTR, - "pcfs", NULL, 0, opts, sizeof (opts)) != 0) { + "pcfs", NULL, 0, opts, sizeof (opts)) != 0) { return (ret); /* mount failed */ } else { /* mount succeeded, look for iso files */ @@ -137,25 +130,26 @@ check_for_iso(const char *path, const char *volid) FTSENT *f; char *pathtowalk[] = { "/.usbdrive", NULL }; - tree = fts_open(pathtowalk, FTS_LOGICAL | FTS_NOSTAT, fts_compare_files); + tree = fts_open(pathtowalk, FTS_LOGICAL | FTS_NOSTAT, NULL); if (tree == 0) { if (verbose) printf("%s: fts_open failed\n", path); return (ret); /* traverse failed */ } - while ((f = fts_read(tree)) != 0 ) { - if (f->fts_info == FTS_F) { /* regular file */ + while ((f = fts_read(tree)) != 0) { + if (f->fts_info != FTS_F) /* regular file */ + continue; - if ( f->fts_namelen > 4 && - (strcasecmp(".iso", f->fts_name + f->fts_namelen - 4) == 0) ) { - if (verbose) - printf("iso found: %s\n", f->fts_name); + if (f->fts_namelen > 4 && + (strcasecmp(".iso", + f->fts_name + f->fts_namelen - 4) == 0)) { + if (verbose) + printf("iso found: %s\n", f->fts_name); - ret = test_iso_file(f->fts_path, volid); - if (ret == 0) - break; /* correct iso found */ - } + ret = test_iso_file(f->fts_path, volid); + if (ret == 0) + break; /* correct iso found */ } } @@ -180,8 +174,10 @@ mount_image(const char *path, const char *volid) int ret = -1; char opts[MAX_MNTOPT_STR]; - /* First try mounting it as hsfs; if that fails, try ufs; if - that fails try check_for_iso() */ + /* + * First try mounting it as hsfs; if that fails, try ufs; if + * that fails try check_for_iso() + */ strcpy(opts, HSFS_OPTS); if (verbose) printf("%s: mount HSFS\n", path);