Skip to content

Commit

Permalink
stat: add %m to output the mount point for a file
Browse files Browse the repository at this point in the history
* src/find-mount-point.c: A new file refactoring
find_mount_point() out from df.c
* src/find-mount-point.h: Likewise.
* src/df.c: Use the new find-mount-point module.
* src/stat.c (print_stat): Handle the new %m format.
(find_bind_mount): A new function to
return the bind mount for a file if any.
(out_mount_mount): Print the bind mount for a file, or else
the standard mount point given by the find-mount-point module.
(usage): Document the %m format directive.
* 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,
and how it may differ from the mount point that df outputs.
* test/misc/stat-mount: A new test to correlate mount points
output from df and stat.
* tests/Makefile.am: Reference the new test.
* NEWS: Mention the new feature
* THANKS: Add the author

Signed-off-by: Pádraig Brady <P@draigBrady.com>
  • Loading branch information
dajoker authored and pixelb committed Aug 27, 2010
1 parent 872f6bb commit ddf6fb8
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 90 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ GNU coreutils NEWS -*- outline -*-


sort now supports -d, -f, -i, -R, and -V in any combination. 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 ** Changes in behavior


df now consistently prints the device name for a bind mounted file, df now consistently prints the device name for a bind mounted file,
Expand Down
1 change: 1 addition & 0 deletions THANKS
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ the bug-report mailing list (as seen on last line of e.g., cp --help).


??? kytek@cybercomm.net ??? kytek@cybercomm.net
A Costa agcosta@gis.net A Costa agcosta@gis.net
Aaron Burgemeister dajoker@gmail.com
Aaron Hawley ashawley@uvm.edu Aaron Hawley ashawley@uvm.edu
Achim Blumensath blume@corona.oche.de Achim Blumensath blume@corona.oche.de
Adam Jimerson vendion@charter.net Adam Jimerson vendion@charter.net
Expand Down
18 changes: 18 additions & 0 deletions doc/coreutils.texi
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8487,6 +8487,7 @@ removal is requested. Equivalent to @option{-I}.
When removing a hierarchy recursively, skip any directory that is on a When removing a hierarchy recursively, skip any directory that is on a
file system different from that of the corresponding command line argument. file system different from that of the corresponding command line argument.


@cindex bind mount
This option is useful when removing a build ``chroot'' hierarchy, This option is useful when removing a build ``chroot'' hierarchy,
which normally contains no valuable data. However, it is not uncommon which normally contains no valuable data. However, it is not uncommon
to bind-mount @file{/home} into such a hierarchy, to make it easier to to bind-mount @file{/home} into such a hierarchy, to make it easier to
Expand Down Expand Up @@ -10685,6 +10686,7 @@ The valid @var{format} directives for files with @option{--format} and
@item %G - Group name of owner @item %G - Group name of owner
@item %h - Number of hard links @item %h - Number of hard links
@item %i - Inode number @item %i - Inode number
@item %m - Mount point (See note below)
@item %n - File name @item %n - File name
@item %N - Quoted file name with dereference if symbolic link @item %N - Quoted file name with dereference if symbolic link
@item %o - I/O block size @item %o - I/O block size
Expand All @@ -10701,6 +10703,22 @@ The valid @var{format} directives for files with @option{--format} and
@item %Z - Time of last change as seconds since Epoch @item %Z - Time of last change as seconds since Epoch
@end itemize @end itemize


The mount point printed by @samp{%m} is similar to that output
by @command{df}, except that:
@itemize @bullet
@item
stat does not dereference symlinks by default
(unless @option{-L} is specified)
@item
stat does not search for specified device nodes in the
file system list, instead operating on them directly
@item
@cindex bind mount
stat outputs the alias for a bind mounted file,
rather than its backing device. One can recursively call stat
until there is no change in output, to get the base mount point
@end itemize

When listing file system information (@option{--file-system} (@option{-f})), When listing file system information (@option{--file-system} (@option{-f})),
you must use a different set of @var{format} directives: you must use a different set of @var{format} directives:


Expand Down
1 change: 1 addition & 0 deletions po/POTFILES.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ src/expand.c
src/expr.c src/expr.c
src/factor.c src/factor.c
src/false.c src/false.c
src/find-mount-point.c
src/fmt.c src/fmt.c
src/fold.c src/fold.c
src/getlimits.c src/getlimits.c
Expand Down
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ noinst_HEADERS = \
copy.h \ copy.h \
cp-hash.h \ cp-hash.h \
dircolors.h \ dircolors.h \
find-mount-point.h \
fs.h \ fs.h \
group-list.h \ group-list.h \
ls.h \ ls.h \
Expand Down Expand Up @@ -478,6 +479,9 @@ rm_SOURCES = rm.c remove.c
mkdir_SOURCES = mkdir.c prog-fprintf.c mkdir_SOURCES = mkdir.c prog-fprintf.c
rmdir_SOURCES = rmdir.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 uname_SOURCES = uname.c uname-uname.c
arch_SOURCES = uname.c uname-arch.c arch_SOURCES = uname.c uname-arch.c
nproc_SOURCES = nproc.c nproc_SOURCES = nproc.c
Expand Down
91 changes: 1 addition & 90 deletions src/df.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
#include "human.h" #include "human.h"
#include "mountlist.h" #include "mountlist.h"
#include "quote.h" #include "quote.h"
#include "save-cwd.h" #include "find-mount-point.h"
#include "xgetcwd.h"


/* The official name of this program (e.g., no `g' prefix). */ /* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "df" #define PROGRAM_NAME "df"
Expand Down Expand Up @@ -522,94 +521,6 @@ show_dev (char const *disk, char const *mount_point,
putchar ('\n'); 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 /* If DISK corresponds to a mount point, show its usage
and return true. Otherwise, return false. */ and return true. Otherwise, return false. */
static bool static bool
Expand Down
113 changes: 113 additions & 0 deletions src/find-mount-point.c
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,113 @@
/* 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 <http://www.gnu.org/licenses/>. */

#include <config.h>
#include <sys/types.h>

#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;
}
17 changes: 17 additions & 0 deletions src/find-mount-point.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,17 @@
/* 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 <http://www.gnu.org/licenses/>. */

extern char* find_mount_point (const char *, const struct stat *);
Loading

0 comments on commit ddf6fb8

Please sign in to comment.