tests: adjust memory limits in head-c.sh
[coreutils.git] / src / find-mount-point.c
blobb38f8ff2958c756b58c07af57f28e8e128532495
1 /* find-mount-point.c -- find the root mount point for a file.
2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 #include <config.h>
18 #include <sys/types.h>
20 #include "system.h"
21 #include "error.h"
22 #include "save-cwd.h"
23 #include "xgetcwd.h"
24 #include "find-mount-point.h"
26 /* Return the root mountpoint of the file system on which FILE exists, in
27 malloced storage. FILE_STAT should be the result of stating FILE.
28 Give a diagnostic and return NULL if unable to determine the mount point.
29 Exit if unable to restore current working directory. */
30 extern char *
31 find_mount_point (char const *file, struct stat const *file_stat)
33 struct saved_cwd cwd;
34 struct stat last_stat;
35 char *mp = NULL; /* The malloc'd mount point. */
37 if (save_cwd (&cwd) != 0)
39 error (0, errno, _("cannot get current directory"));
40 return NULL;
43 if (S_ISDIR (file_stat->st_mode))
44 /* FILE is a directory, so just chdir there directly. */
46 last_stat = *file_stat;
47 if (chdir (file) < 0)
49 error (0, errno, _("cannot change to directory %s"), quoteaf (file));
50 return NULL;
53 else
54 /* FILE is some other kind of file; use its directory. */
56 char *xdir = dir_name (file);
57 char *dir;
58 ASSIGN_STRDUPA (dir, xdir);
59 free (xdir);
61 if (chdir (dir) < 0)
63 error (0, errno, _("cannot change to directory %s"), quoteaf (dir));
64 return NULL;
67 if (stat (".", &last_stat) < 0)
69 error (0, errno, _("cannot stat current directory (now %s)"),
70 quoteaf (dir));
71 goto done;
75 /* Now walk up FILE's parents until we find another file system or /,
76 chdiring as we go. LAST_STAT holds stat information for the last place
77 we visited. */
78 while (true)
80 struct stat st;
81 if (stat ("..", &st) < 0)
83 error (0, errno, _("cannot stat %s"), quoteaf (".."));
84 goto done;
86 if (st.st_dev != last_stat.st_dev || st.st_ino == last_stat.st_ino)
87 /* cwd is the mount point. */
88 break;
89 if (chdir ("..") < 0)
91 error (0, errno, _("cannot change to directory %s"), quoteaf (".."));
92 goto done;
94 last_stat = st;
97 /* Finally reached a mount point, see what it's called. */
98 mp = xgetcwd ();
100 done:
101 /* Restore the original cwd. */
103 int save_errno = errno;
104 if (restore_cwd (&cwd) != 0)
105 error (EXIT_FAILURE, errno,
106 _("failed to return to initial working directory"));
107 free_cwd (&cwd);
108 errno = save_errno;
111 return mp;