>From 14db051918acb83b23c2600fb880fc606fa0b65a Mon Sep 17 00:00:00 2001 From: Aaron Burgemeister Date: Thu, 15 Jul 2010 19:54:49 -0600 Subject: [PATCH] stat: add %m to output the mount point * src/find-mount-point.c: A new file refactoring find_mount_point() out from df.c * src/find-mount-point.c: Likewise. * src/df.c: Use the new find-mount-point module. * src/stat.c (print_stat): Output the mount point for %m (usage): Document %m. * src/Makefile.am: Reference the refactored find-mount-point.c * po/POTFILES.in: Add find_mount_point.c to the translation list * doc/coreutils.texi (stat invocation): Document %m * NEWS: Mention the new feature * THANKS: Add the author --- NEWS | 3 + THANKS | 1 + doc/coreutils.texi | 1 + po/POTFILES.in | 1 + src/Makefile.am | 4 ++ src/df.c | 91 +-------------------------------------- src/find-mount-point.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++ src/find-mount-point.h | 22 +++++++++ src/stat.c | 10 ++++ 9 files changed, 155 insertions(+), 90 deletions(-) create mode 100644 src/find-mount-point.c create mode 100644 src/find-mount-point.h diff --git a/NEWS b/NEWS index ae7b839..bece07b 100644 --- a/NEWS +++ b/NEWS @@ -24,6 +24,9 @@ GNU coreutils NEWS -*- outline -*- sort now supports -d, -f, -i, -R, and -V in any combination. + stat now accepts the %m format directive to output + the mount point for a file. + ** Changes in behavior du now uses less than half as much memory when operating on trees diff --git a/THANKS b/THANKS index 34b95f1..c368a2c 100644 --- a/THANKS +++ b/THANKS @@ -8,6 +8,7 @@ the bug-report mailing list (as seen on last line of e.g., cp --help). ??? address@hidden A Costa address@hidden +Aaron Burgemeister address@hidden Aaron Hawley address@hidden Achim Blumensath address@hidden Adam Jimerson address@hidden diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 7eedec7..1453f30 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -10685,6 +10685,7 @@ The valid @var{format} directives for files with @option{--format} and @item %G - Group name of owner @item %h - Number of hard links @item %i - Inode number address@hidden %m - Mount point @item %n - File name @item %N - Quoted file name with dereference if symbolic link @item %o - I/O block size diff --git a/po/POTFILES.in b/po/POTFILES.in index be20f4c..673a7d9 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -61,6 +61,7 @@ src/expand.c src/expr.c src/factor.c src/false.c +src/find-mount-point.c src/fmt.c src/fold.c src/getlimits.c diff --git a/src/Makefile.am b/src/Makefile.am index 1a19fa6..00c7ff7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -145,6 +145,7 @@ noinst_HEADERS = \ copy.h \ cp-hash.h \ dircolors.h \ + find-mount-point.h \ fs.h \ group-list.h \ ls.h \ @@ -478,6 +479,9 @@ rm_SOURCES = rm.c remove.c mkdir_SOURCES = mkdir.c prog-fprintf.c rmdir_SOURCES = rmdir.c prog-fprintf.c +df_SOURCES = df.c find-mount-point.c +stat_SOURCES = stat.c find-mount-point.c + uname_SOURCES = uname.c uname-uname.c arch_SOURCES = uname.c uname-arch.c nproc_SOURCES = nproc.c diff --git a/src/df.c b/src/df.c index 76622fb..97b1637 100644 --- a/src/df.c +++ b/src/df.c @@ -29,8 +29,7 @@ #include "human.h" #include "mountlist.h" #include "quote.h" -#include "save-cwd.h" -#include "xgetcwd.h" +#include "find-mount-point.h" /* The official name of this program (e.g., no `g' prefix). */ #define PROGRAM_NAME "df" @@ -522,94 +521,6 @@ show_dev (char const *disk, char const *mount_point, putchar ('\n'); } -/* Return the root mountpoint of the file system on which FILE exists, in - malloced storage. FILE_STAT should be the result of stating FILE. - Give a diagnostic and return NULL if unable to determine the mount point. - Exit if unable to restore current working directory. */ -static char * -find_mount_point (const char *file, const struct stat *file_stat) -{ - struct saved_cwd cwd; - struct stat last_stat; - char *mp = NULL; /* The malloced mount point. */ - - if (save_cwd (&cwd) != 0) - { - error (0, errno, _("cannot get current directory")); - return NULL; - } - - if (S_ISDIR (file_stat->st_mode)) - /* FILE is a directory, so just chdir there directly. */ - { - last_stat = *file_stat; - if (chdir (file) < 0) - { - error (0, errno, _("cannot change to directory %s"), quote (file)); - return NULL; - } - } - else - /* FILE is some other kind of file; use its directory. */ - { - char *xdir = dir_name (file); - char *dir; - ASSIGN_STRDUPA (dir, xdir); - free (xdir); - - if (chdir (dir) < 0) - { - error (0, errno, _("cannot change to directory %s"), quote (dir)); - return NULL; - } - - if (stat (".", &last_stat) < 0) - { - error (0, errno, _("cannot stat current directory (now %s)"), - quote (dir)); - goto done; - } - } - - /* Now walk up FILE's parents until we find another file system or /, - chdiring as we go. LAST_STAT holds stat information for the last place - we visited. */ - while (true) - { - struct stat st; - if (stat ("..", &st) < 0) - { - error (0, errno, _("cannot stat %s"), quote ("..")); - goto done; - } - if (st.st_dev != last_stat.st_dev || st.st_ino == last_stat.st_ino) - /* cwd is the mount point. */ - break; - if (chdir ("..") < 0) - { - error (0, errno, _("cannot change to directory %s"), quote ("..")); - goto done; - } - last_stat = st; - } - - /* Finally reached a mount point, see what it's called. */ - mp = xgetcwd (); - -done: - /* Restore the original cwd. */ - { - int save_errno = errno; - if (restore_cwd (&cwd) != 0) - error (EXIT_FAILURE, errno, - _("failed to return to initial working directory")); - free_cwd (&cwd); - errno = save_errno; - } - - return mp; -} - /* If DISK corresponds to a mount point, show its usage and return true. Otherwise, return false. */ static bool diff --git a/src/find-mount-point.c b/src/find-mount-point.c new file mode 100644 index 0000000..289d23e --- /dev/null +++ b/src/find-mount-point.c @@ -0,0 +1,112 @@ +/* find-mount-point.c -- find the root mount point for a file. + Copyright (C) 2010 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +#include "system.h" +#include "error.h" +#include "quote.h" +#include "save-cwd.h" +#include "xgetcwd.h" +#include "find-mount-point.h" + +/* Return the root mountpoint of the file system on which FILE exists, in + malloced storage. FILE_STAT should be the result of stating FILE. + Give a diagnostic and return NULL if unable to determine the mount point. + Exit if unable to restore current working directory. */ +extern char * +find_mount_point (const char *file, const struct stat *file_stat) +{ + struct saved_cwd cwd; + struct stat last_stat; + char *mp = NULL; /* The malloced mount point. */ + + if (save_cwd (&cwd) != 0) + { + error (0, errno, _("cannot get current directory")); + return NULL; + } + + if (S_ISDIR (file_stat->st_mode)) + /* FILE is a directory, so just chdir there directly. */ + { + last_stat = *file_stat; + if (chdir (file) < 0) + { + error (0, errno, _("cannot change to directory %s"), quote (file)); + return NULL; + } + } + else + /* FILE is some other kind of file; use its directory. */ + { + char *xdir = dir_name (file); + char *dir; + ASSIGN_STRDUPA (dir, xdir); + free (xdir); + + if (chdir (dir) < 0) + { + error (0, errno, _("cannot change to directory %s"), quote (dir)); + return NULL; + } + + if (stat (".", &last_stat) < 0) + { + error (0, errno, _("cannot stat current directory (now %s)"), + quote (dir)); + goto done; + } + } + + /* Now walk up FILE's parents until we find another file system or /, + chdiring as we go. LAST_STAT holds stat information for the last place + we visited. */ + while (true) + { + struct stat st; + if (stat ("..", &st) < 0) + { + error (0, errno, _("cannot stat %s"), quote ("..")); + goto done; + } + if (st.st_dev != last_stat.st_dev || st.st_ino == last_stat.st_ino) + /* cwd is the mount point. */ + break; + if (chdir ("..") < 0) + { + error (0, errno, _("cannot change to directory %s"), quote ("..")); + goto done; + } + last_stat = st; + } + + /* Finally reached a mount point, see what it's called. */ + mp = xgetcwd (); + +done: + /* Restore the original cwd. */ + { + int save_errno = errno; + if (restore_cwd (&cwd) != 0) + error (EXIT_FAILURE, errno, + _("failed to return to initial working directory")); + free_cwd (&cwd); + errno = save_errno; + } + + return mp; +} diff --git a/src/find-mount-point.h b/src/find-mount-point.h new file mode 100644 index 0000000..a97c57f --- /dev/null +++ b/src/find-mount-point.h @@ -0,0 +1,22 @@ +/* find-mount-point.h -- find the root mount point for a file. + Copyright (C) 2010 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef FINDMOUNTPOINT_H +#define FINDMOUNTPOINT_H 1 + +extern char* find_mount_point (const char *, const struct stat *); + +#endif diff --git a/src/stat.c b/src/stat.c index c3730f0..6fb2999 100644 --- a/src/stat.c +++ b/src/stat.c @@ -68,6 +68,7 @@ #include "quotearg.h" #include "stat-time.h" #include "strftime.h" +#include "find-mount-point.h" #if USE_STATVFS # define STRUCT_STATVFS struct statvfs @@ -612,6 +613,7 @@ print_stat (char *pformat, size_t prefix_len, char m, struct stat *statbuf = (struct stat *) data; struct passwd *pw_ent; struct group *gw_ent; + char *mp; bool fail = false; switch (m) @@ -679,6 +681,13 @@ print_stat (char *pformat, size_t prefix_len, char m, case 't': out_uint_x (pformat, prefix_len, major (statbuf->st_rdev)); break; + case 'm': + mp = find_mount_point (filename, statbuf); + if (mp) + out_string (pformat, prefix_len, mp); + else + fail = true; + break; case 'T': out_uint_x (pformat, prefix_len, minor (statbuf->st_rdev)); break; @@ -1025,6 +1034,7 @@ The valid format sequences for files (without --file-system):\n\ fputs (_("\ %h Number of hard links\n\ %i Inode number\n\ + %m Mount point\n\ %n File name\n\ %N Quoted file name with dereference if symbolic link\n\ %o I/O block size\n\ -- 1.6.2.5