libgfs2: Clean up mp2fsname2
PATCH 1/1 libgfs2: Clean up mp2fsname2
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(-)
gfs2/libgfs2/libgfs2.h +1 -1
@@ -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);
gfs2/libgfs2/misc.c +12 -9
@@ -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; }
gfs2/tool/misc.c +5 -0
@@ -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" )) {
Signature · 1 line
1.6.2
PATCH 1/1 libgfs2: Clean up mp2fsname2
Hi,
Looks good to me,
Steve.
Quoted 111 lines · Andrew Price
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(-)gfs2/libgfs2/libgfs2.h +1
-1@@ -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);gfs2/libgfs2/misc.c +12
-9@@ -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; }gfs2/tool/misc.c +5
-0@@ -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" )) {