makepath.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* makepath.c -- Ensure that a directory path exists.
  2. Copyright (C) 1990, 1997, 1998, 1999, 2000, 2002, 2003 Free
  3. 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 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and Jim Meyering. */
  16. #include "src/common.h"
  17. #include "makepath.h"
  18. #include "dirname.h"
  19. //#include "save-cwd.h"
  20. //#include "error.h"
  21. //#include "quote.h"
  22. //#include <alloca.h>
  23. #include <sys/stat.h>
  24. #if !defined S_ISDIR && defined S_IFDIR
  25. # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  26. #endif
  27. #ifndef S_IRWXUGO
  28. # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
  29. #endif
  30. #ifndef S_ISUID
  31. # define S_ISUID 04000
  32. #endif
  33. #ifndef S_ISGID
  34. # define S_ISGID 02000
  35. #endif
  36. #ifndef S_ISVTX
  37. # define S_ISVTX 01000
  38. #endif
  39. #ifndef S_IRUSR
  40. # define S_IRUSR 0200
  41. #endif
  42. #ifndef S_IWUSR
  43. # define S_IWUSR 0200
  44. #endif
  45. #ifndef S_IXUSR
  46. # define S_IXUSR 0100
  47. #endif
  48. #ifndef S_IRWXU
  49. # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
  50. #endif
  51. #define WX_USR (S_IWUSR | S_IXUSR)
  52. #define CLEANUP_CWD \
  53. do \
  54. { \
  55. /* We're done operating on basename_dir. \
  56. Restore working directory. */ \
  57. if (do_chdir) \
  58. { \
  59. if (restore_cwd (&cwd) != 0) \
  60. { \
  61. int _saved_errno = errno; \
  62. error (0, errno, \
  63. "failed to return to initial working directory"); \
  64. free_cwd (&cwd); \
  65. errno = _saved_errno; \
  66. return 1; \
  67. } \
  68. free_cwd (&cwd); \
  69. } \
  70. } \
  71. while (0)
  72. #define CLEANUP \
  73. do \
  74. { \
  75. umask (oldmask); \
  76. CLEANUP_CWD; \
  77. } \
  78. while (0)
  79. /* Attempt to create directory DIR (aka DIRPATH) with the specified MODE.
  80. If CREATED_DIR_P is non-NULL, set *CREATED_DIR_P to non-zero if this
  81. function creates DIR and to zero otherwise. Give a diagnostic and
  82. return non-zero if DIR cannot be created or cannot be determined to
  83. exist already. Use DIRPATH in any diagnostic, not DIR.
  84. Note that if DIR already exists, this function returns zero
  85. (indicating success) and sets *CREATED_DIR_P to zero. */
  86. int
  87. make_dir (const char *dir, const char *dirpath, mode_t mode, int *created_dir_p)
  88. {
  89. int fail = 0;
  90. int created_dir;
  91. created_dir = (mkdir (dir, mode) == 0);
  92. if (!created_dir)
  93. {
  94. struct stat stats;
  95. int saved_errno = errno;
  96. /* The mkdir and stat calls below may appear to be reversed.
  97. They are not. It is important to call mkdir first and then to
  98. call stat (to distinguish the three cases) only if mkdir fails.
  99. The alternative to this approach is to `stat' each directory,
  100. then to call mkdir if it doesn't exist. But if some other process
  101. were to create the directory between the stat & mkdir, the mkdir
  102. would fail with EEXIST. */
  103. if (stat (dir, &stats))
  104. fail = 1;
  105. else if (!S_ISDIR (stats.st_mode))
  106. fail = 1;
  107. }
  108. if (created_dir_p)
  109. *created_dir_p = created_dir;
  110. return fail;
  111. }
  112. /* Ensure that the directory ARGPATH exists.
  113. Create any leading directories that don't already exist, with
  114. permissions PARENT_MODE.
  115. If the last element of ARGPATH does not exist, create it as
  116. a new directory with permissions MODE.
  117. If OWNER and GROUP are non-negative, use them to set the UID and GID of
  118. any created directories.
  119. If VERBOSE_FMT_STRING is nonzero, use it as a printf format
  120. string for printing a message after successfully making a directory,
  121. with the name of the directory that was just made as an argument.
  122. If PRESERVE_EXISTING is non-zero and ARGPATH is an existing directory,
  123. then do not attempt to set its permissions and ownership.
  124. Return 0 if ARGPATH exists as a directory with the proper
  125. ownership and permissions when done, otherwise 1. */
  126. int
  127. make_path (const char *argpath,
  128. int mode,
  129. int parent_mode,
  130. uid_t owner,
  131. gid_t group,
  132. int preserve_existing,
  133. const char *verbose_fmt_string)
  134. {
  135. struct stat stats;
  136. int retval = 0;
  137. if (stat (argpath, &stats))
  138. {
  139. char *slash;
  140. int tmp_mode; /* Initial perms for leading dirs. */
  141. int re_protect; /* Should leading dirs be unwritable? */
  142. struct ptr_list
  143. {
  144. char *dirname_end;
  145. struct ptr_list *next;
  146. };
  147. struct ptr_list *p, *leading_dirs = NULL;
  148. int do_chdir; /* Whether to chdir before each mkdir. */
  149. struct saved_cwd cwd;
  150. char *basename_dir;
  151. char *dirpath;
  152. /* Temporarily relax umask in case it's overly restrictive. */
  153. mode_t oldmask = umask (0);
  154. /* Make a copy of ARGPATH that we can scribble NULs on. */
  155. dirpath = (char *) calloc(1, strlen (argpath) + 1);
  156. strcpy (dirpath, argpath);
  157. if (dirpath[strlen(dirpath) - 1] == '/')
  158. dirpath[strlen(dirpath) - 1] = 0;
  159. /* If leading directories shouldn't be writable or executable,
  160. or should have set[ug]id or sticky bits set and we are setting
  161. their owners, we need to fix their permissions after making them. */
  162. if (((parent_mode & WX_USR) != WX_USR)
  163. || ((owner != (uid_t) -1 || group != (gid_t) -1)
  164. && (parent_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0))
  165. {
  166. tmp_mode = S_IRWXU;
  167. re_protect = 1;
  168. }
  169. else
  170. {
  171. tmp_mode = parent_mode;
  172. re_protect = 0;
  173. }
  174. /* If we can record the current working directory, we may be able
  175. to do the chdir optimization. */
  176. do_chdir = !save_cwd (&cwd);
  177. /* If we've saved the cwd and DIRPATH is an absolute pathname,
  178. we must chdir to `/' in order to enable the chdir optimization.
  179. So if chdir ("/") fails, turn off the optimization. */
  180. if (do_chdir && *dirpath == '/' && chdir ("/") < 0)
  181. do_chdir = 0;
  182. slash = dirpath;
  183. /* Skip over leading slashes. */
  184. while (*slash == '/')
  185. slash++;
  186. while (1)
  187. {
  188. int newly_created_dir;
  189. int fail;
  190. /* slash points to the leftmost unprocessed component of dirpath. */
  191. basename_dir = slash;
  192. slash = strchr (slash, '/');
  193. if (slash == NULL)
  194. break;
  195. /* If we're *not* doing chdir before each mkdir, then we have to refer
  196. to the target using the full (multi-component) directory name. */
  197. if (!do_chdir)
  198. basename_dir = dirpath;
  199. *slash = '\0';
  200. fail = make_dir (basename_dir, dirpath, tmp_mode, &newly_created_dir);
  201. if (fail)
  202. {
  203. CLEANUP;
  204. free(dirname);
  205. return 1;
  206. }
  207. if (newly_created_dir)
  208. {
  209. if ((owner != (uid_t) -1 || group != (gid_t) -1)
  210. && chown (basename_dir, owner, group) && errno != EPERM)
  211. {
  212. CLEANUP;
  213. free(dirname);
  214. return 1;
  215. }
  216. if (re_protect)
  217. {
  218. struct ptr_list *newlist = (struct ptr_list *) calloc(1, sizeof (struct ptr_list));
  219. newlist->dirname_end = slash;
  220. newlist->next = leading_dirs;
  221. leading_dirs = newlist;
  222. }
  223. }
  224. /* If we were able to save the initial working directory,
  225. then we can use chdir to change into each directory before
  226. creating an entry in that directory. This avoids making
  227. stat and mkdir process O(n^2) file name components. */
  228. if (do_chdir && chdir (basename_dir) < 0)
  229. {
  230. CLEANUP;
  231. free(dirname);
  232. return 1;
  233. }
  234. *slash++ = '/';
  235. /* Avoid unnecessary calls to `stat' when given
  236. pathnames containing multiple adjacent slashes. */
  237. while (*slash == '/')
  238. slash++;
  239. }
  240. if (!do_chdir)
  241. basename_dir = dirpath;
  242. /* Done creating leading directories. Restore original umask. */
  243. umask (oldmask);
  244. /* We're done making leading directories.
  245. Create the final component of the path. */
  246. if (make_dir (basename_dir, dirpath, mode, NULL))
  247. {
  248. CLEANUP;
  249. free(dirname);
  250. return 1;
  251. }
  252. if (owner != (uid_t) -1 || group != (gid_t) -1)
  253. {
  254. if (chown (basename_dir, owner, group) && errno != EPERM)
  255. retval = 1;
  256. }
  257. /* The above chown may have turned off some permission bits in MODE.
  258. Another reason we may have to use chmod here is that mkdir(2) is
  259. required to honor only the file permission bits. In particular,
  260. it need not honor the `special' bits, so if MODE includes any
  261. special bits, set them here. */
  262. if ((mode & ~S_IRWXUGO) && chmod (basename_dir, mode))
  263. retval = 1;
  264. CLEANUP_CWD;
  265. /* If the mode for leading directories didn't include owner "wx"
  266. privileges, we have to reset their protections to the correct
  267. value. */
  268. for (p = leading_dirs; p != NULL; p = p->next)
  269. {
  270. *(p->dirname_end) = '\0';
  271. if (chmod (dirpath, parent_mode))
  272. retval = 1;
  273. }
  274. }
  275. else
  276. {
  277. /* We get here if the entire path already exists. */
  278. const char *dirpath = argpath;
  279. if (!S_ISDIR (stats.st_mode))
  280. {
  281. free(dirname);
  282. return 1;
  283. }
  284. if (!preserve_existing)
  285. {
  286. /* chown must precede chmod because on some systems,
  287. chown clears the set[ug]id bits for non-superusers,
  288. resulting in incorrect permissions.
  289. On System V, users can give away files with chown and then not
  290. be able to chmod them. So don't give files away. */
  291. if ((owner != (uid_t) -1 || group != (gid_t) -1) && chown (dirpath, owner, group) && errno != EPERM)
  292. retval = 1;
  293. if (chmod (dirpath, mode))
  294. retval = 1;
  295. }
  296. }
  297. free(dirname);
  298. return retval;
  299. }