From 31f4f7cfcb38c8860f470ec9688c9f2703add035 Mon Sep 17 00:00:00 2001 From: Aaron Burgemeister Date: Wed, 7 Jul 2010 19:42:24 -0600 Subject: [PATCH] first build; make failing Split out find_mount_point from df.c to its own file for inclusion in stat.c to provide mount point display functionality. Update Makefile.am reflecting multi-.c file requirements for df and stat commands. Signed-off-by: Aaron Burgemeister --- NEWS | 2 + THANKS | 1 + doc/coreutils.texi | 1 + src/Makefile.am | 4 ++ src/df.c | 1 + src/find-mount-point.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ src/find-mount-point.h | 23 +++++++++ src/stat.c | 12 +++++ 8 files changed, 166 insertions(+), 0 deletions(-) create mode 100644 src/find-mount-point.c create mode 100644 src/find-mount-point.h diff --git a/NEWS b/NEWS index 82190d9..5954273 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,8 @@ GNU coreutils NEWS -*- outline -*- ** New features + stat can now print the mount point of a file via its new %m format directive. + cp now accepts the --attributes-only option to not copy file data, which is useful for efficiently modifying files. diff --git a/THANKS b/THANKS index caa40de..79f1f1d 100644 --- a/THANKS +++ b/THANKS @@ -8,6 +8,7 @@ the bug-report mailing list (as seen on last line of e.g., cp --help). ??? kytek@cybercomm.net A Costa agcosta@gis.net +Aaron Burgemeister dajoker@gmail.com Aaron Hawley ashawley@uvm.edu Achim Blumensath blume@corona.oche.de Adam Jimerson vendion@charter.net diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 21cf36d..ea3f142 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -10666,6 +10666,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 +@item %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/src/Makefile.am b/src/Makefile.am index 0630a06..90ab7ee 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 \ @@ -488,6 +489,9 @@ rm_SOURCES = rm.c remove.c mkdir_SOURCES = mkdir.c prog-fprintf.c rmdir_SOURCES = rmdir.c prog-fprintf.c +df_SOURCES = stat.c find-mount-point.c +stat_SOURCES = df.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..b286149 100644 --- a/src/df.c +++ b/src/df.c @@ -31,6 +31,7 @@ #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" diff --git a/src/find-mount-point.c b/src/find-mount-point.c new file mode 100644 index 0000000..bfac4c5 --- /dev/null +++ b/src/find-mount-point.c @@ -0,0 +1,122 @@ +#include +#if USE_STATVFS +# include +#elif HAVE_SYS_VFS_H +# include +#elif HAVE_SYS_MOUNT_H && HAVE_SYS_PARAM_H +/* NOTE: freebsd5.0 needs sys/param.h and sys/mount.h for statfs. + It does have statvfs.h, but shouldn't use it, since it doesn't + HAVE_STRUCT_STATVFS_F_BASETYPE. So find a clean way to fix it. */ +/* NetBSD 1.5.2 needs these, for the declaration of struct statfs. */ +# include +# include +# if HAVE_NFS_NFS_CLNT_H && HAVE_NFS_VFS_H +/* Ultrix 4.4 needs these for the declaration of struct statfs. */ +# include +# include +# include +# endif +#elif HAVE_OS_H /* BeOS */ +# include +#endif +#include + +#include "system.h" + +#include "error.h" +#include "filemode.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. */ +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; +} + diff --git a/src/find-mount-point.h b/src/find-mount-point.h new file mode 100644 index 0000000..b8666ce --- /dev/null +++ b/src/find-mount-point.h @@ -0,0 +1,23 @@ +/* stat/df-related mount point function + + Copyright (C) 2005, 2007, 2009-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 + +static char * find_mount_point (const char *, const struct stat *); + +#endif diff --git a/src/stat.c b/src/stat.c index c3730f0..a645c4d 100644 --- a/src/stat.c +++ b/src/stat.c @@ -68,6 +68,9 @@ #include "quotearg.h" #include "stat-time.h" #include "strftime.h" +#include "find-mount-point.h" +#include "save-cwd.h" +#include "xgetcwd.h" #if USE_STATVFS # define STRUCT_STATVFS struct statvfs @@ -612,6 +615,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 +683,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 +1036,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.7.1