commit 70d1f1c97f322a164ee872c0399d9fbccc862b18 Author: Aaron Burgemeister Date: Tue Jul 6 18:01:53 2010 -0600 broken-20100707000000Z diff --git a/src/findmountpoint.c b/src/findmountpoint.c new file mode 100644 index 0000000..665e2fc --- /dev/null +++ b/src/findmountpoint.c @@ -0,0 +1,93 @@ +#include "save-cwd.h" +#include "xgetcwd.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/findmountpoint.h b/src/findmountpoint.h new file mode 100644 index 0000000..3943b1e --- /dev/null +++ b/src/findmountpoint.h @@ -0,0 +1,27 @@ +/* stat-related time functions. + * + * 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 +#endif + + +/* 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 *, const struct stat *);