1 /* Make a file's ancestor directories.
3 Copyright (C) 2006 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Paul Eggert. */
23 #include "mkancesdirs.h"
25 #include <sys/types.h>
35 /* Ensure that the ancestor directories of FILE exist, using an
36 algorithm that should work even if two processes execute this
37 function in parallel. Modify FILE as necessary to access the
38 ancestor directories, but restore FILE to an equivalent value
41 WD points to the working directory, using the conventions of
44 Create any ancestor directories that don't already exist, by
45 invoking MAKE_DIR (FILE, COMPONENT, MAKE_DIR_ARG). This function
46 should return 0 if successful and the resulting directory is
47 readable, 1 if successful but the resulting directory might not be
48 readable, -1 (setting errno) otherwise. If COMPONENT is relative,
49 it is relative to the temporary working directory, which may differ
52 Ordinarily MAKE_DIR is executed with the working directory changed
53 to reflect the already-made prefix, and mkancesdirs returns with
54 the working directory changed a prefix of FILE. However, if the
55 initial working directory cannot be saved in a file descriptor,
56 MAKE_DIR is invoked in a subprocess and this function returns in
57 both the parent and child process, so the caller should not assume
58 any changed state survives other than the EXITMAX component of WD,
59 and the caller should take care that the parent does not attempt to
60 do the work that the child is doing.
62 If successful and if this process can go ahead and create FILE,
63 return the length of the prefix of FILE that has already been made.
64 If successful so far but a child process is doing the actual work,
65 return -2. If unsuccessful, return -1 and set errno. */
68 mkancesdirs (char *file, struct savewd *wd,
69 int (*make_dir) (char const *, char const *, void *),
72 /* Address of the previous directory separator that follows an
73 ordinary byte in a file name in the left-to-right scan, or NULL
74 if no such separator precedes the current location P. */
77 /* Address of the leftmost file name component that has not yet
79 char *component = file;
81 char *p = file + FILE_SYSTEM_PREFIX_LEN (file);
83 bool made_dir = false;
85 /* Scan forward through FILE, creating and chdiring into directories
86 along the way. Try MAKE_DIR before chdir, so that the procedure
87 works even when two or more processes are executing it in
88 parallel. Isolate each file name component by having COMPONENT
89 point to its start and SEP point just after its end. */
97 else if (ISSLASH (c) && *p && sep)
99 /* Don't bother to make or test for "." since it does not
100 affect the algorithm. */
101 if (! (sep - component == 1 && component[0] == '.'))
103 int make_dir_errno = 0;
104 int savewd_chdir_options = 0;
107 /* Temporarily modify FILE to isolate this file name
111 /* Invoke MAKE_DIR on this component, except don't bother
112 with ".." since it must exist if its "parent" does. */
113 if (sep - component == 2
114 && component[0] == '.' && component[1] == '.')
117 switch (make_dir (file, component, make_dir_arg))
120 make_dir_errno = errno;
124 savewd_chdir_options |= SAVEWD_CHDIR_READABLE;
132 savewd_chdir_options |= SAVEWD_CHDIR_NOFOLLOW;
135 savewd_chdir (wd, component, savewd_chdir_options, NULL);
137 /* Undo the temporary modification to FILE, unless there
139 if (chdir_result != -1)
142 if (chdir_result != 0)
144 if (make_dir_errno != 0 && errno == ENOENT)
145 errno = make_dir_errno;
153 return component - file;