/coreutils-8.17/src/find-mount-point.c

# · C · 113 lines · 75 code · 11 blank · 27 comment · 15 complexity · d056d1a4d6f77a007c6444f88ffc326c MD5 · raw file

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