This patch removes the calls to 'die' and 'exit' from mp2fsname2 and cleans up the function a bit. Namely, it removes an unused variable (sb), adds error checking and fixes a typo.
Signed-off-by: Andrew Price <[email protected]> --- gfs2/libgfs2/libgfs2.h | 2 +- gfs2/libgfs2/misc.c | 21 ++++++++++++--------- gfs2/tool/misc.c | 5 +++++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h index d8faf45..1742da9 100644 --- a/gfs2/libgfs2/libgfs2.h +++ b/gfs2/libgfs2/libgfs2.h @@ -632,7 +632,7 @@ extern int mount_gfs2_meta(struct gfs2_sbd *sdp); extern void cleanup_metafs(struct gfs2_sbd *sdp); extern char *find_debugfs_mount(void); extern char *mp2fsname(char *mp); -extern char *mp2fsname2(char *devname); +extern char *mp2fsname2(char *mp); extern char *get_sysfs(char *fsname, char *filename); extern int get_sysfs_uint(char *fsname, char *filename, unsigned int *val); extern int set_sysfs(char *fsname, char *filename, char *val); diff --git a/gfs2/libgfs2/misc.c b/gfs2/libgfs2/misc.c index 60b2d4f..7666ded 100644 --- a/gfs2/libgfs2/misc.c +++ b/gfs2/libgfs2/misc.c @@ -314,7 +314,7 @@ char *find_debugfs_mount(void) /* * Same as mp2fsname, except that this function doesn't stat() the mountpoint * to get the device no. Used by gfs2_tool freeze/unfreeze where we don't want - * to touch the potetially frozen filesytem and hang gfs2_tool itself. + * to touch the potentially frozen filesytem and hang gfs2_tool itself. */ char * mp2fsname2(char *mp) @@ -323,8 +323,7 @@ mp2fsname2(char *mp) struct stat statbuf; DIR *d; struct dirent *de; - struct gfs2_sbd sb; - FILE *fp = fopen("/proc/mounts", "r"); + FILE *fp; char buffer[PATH_MAX], device_name[PATH_MAX]; int fsdump, fspass, ret, found = 0; char fspath[PATH_MAX], fsoptions[PATH_MAX], fstype[80]; @@ -334,9 +333,9 @@ mp2fsname2(char *mp) if (mp[strlen(mp) - 1] == '/') mp[strlen(mp) - 1] = 0; + fp = fopen("/proc/mounts", "r"); if (fp == NULL) { - perror("open: /proc/mounts"); - exit(EXIT_FAILURE); + return NULL; } while ((fgets(buffer, PATH_MAX - 1, fp)) != NULL) { @@ -358,9 +357,12 @@ mp2fsname2(char *mp) found = 1; break; } + fclose(fp); - if (!found) - die("can't find gfs2 filesystem mounted at %s\n", mp); + if (!found) { + errno = ENOENT; + return NULL; + } if (stat(device_name, &statbuf)) return NULL; @@ -371,7 +373,7 @@ mp2fsname2(char *mp) d = opendir(SYS_BASE); if (!d) - die("can't open %s: %s\n", SYS_BASE, strerror(errno)); + return NULL; while ((de = readdir(d))) { if (de->d_name[0] == '.') @@ -386,9 +388,10 @@ mp2fsname2(char *mp) } } - fclose(fp); closedir(d); + if (!fsname) + errno = ENOENT; return fsname; } diff --git a/gfs2/tool/misc.c b/gfs2/tool/misc.c index 6b054c6..694acb8 100644 --- a/gfs2/tool/misc.c +++ b/gfs2/tool/misc.c @@ -40,6 +40,11 @@ do_freeze(int argc, char **argv) die("Usage: gfs2_tool %s <mountpoint>\n", command); name = mp2fsname2(argv[optind]); + if (!name) { + fprintf(stderr, "Couldn't find a GFS2 filesystem mounted at %s\n", + argv[optind]); + exit(-1); + } if (strcmp(command, "freeze") == 0) { if (set_sysfs(name, "freeze", "1")) { -- 1.6.2
