PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgmkdirp.c File Reference
#include "c.h"
#include <sys/stat.h>
Include dependency graph for pgmkdirp.c:

Go to the source code of this file.

Functions

int pg_mkdir_p (char *path, int omode)
 

Function Documentation

◆ pg_mkdir_p()

int pg_mkdir_p ( char path,
int  omode 
)

Definition at line 57 of file pgmkdirp.c.

58{
60 oumask;
61 int last,
62 retval;
63 char *p;
64
65 retval = 0;
66 p = path;
67
68#ifdef WIN32
69 /* skip network and drive specifiers for win32 */
70 if (strlen(p) >= 2)
71 {
72 if (p[0] == '/' && p[1] == '/')
73 {
74 /* network drive */
75 p = strchr(p + 2, '/');
76 if (p == NULL)
77 {
78 errno = EINVAL;
79 return -1;
80 }
81 }
82 else if (p[1] == ':' &&
83 ((p[0] >= 'a' && p[0] <= 'z') ||
84 (p[0] >= 'A' && p[0] <= 'Z')))
85 {
86 /* local drive */
87 p += 2;
88 }
89 }
90#endif
91
92 /*
93 * POSIX 1003.2: For each dir operand that does not name an existing
94 * directory, effects equivalent to those caused by the following command
95 * shall occur:
96 *
97 * mkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode] dir
98 *
99 * We change the user's umask and then restore it, instead of doing
100 * chmod's. Note we assume umask() can't change errno.
101 */
102 oumask = umask(0);
103 numask = oumask & ~(S_IWUSR | S_IXUSR);
104 (void) umask(numask);
105
106 if (p[0] == '/') /* Skip leading '/'. */
107 ++p;
108 for (last = 0; !last; ++p)
109 {
110 if (p[0] == '\0')
111 last = 1;
112 else if (p[0] != '/')
113 continue;
114 *p = '\0';
115 if (!last && p[1] == '\0')
116 last = 1;
117
118 if (last)
119 (void) umask(oumask);
120
121 if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0)
122 {
123 /*
124 * If we got EEXIST because there's already a directory there,
125 * don't complain.
126 */
127#ifndef WIN32
128 int save_errno = errno;
129 struct stat sb;
130
131 if (save_errno != EEXIST ||
132 stat(path, &sb) != 0 ||
133 !S_ISDIR(sb.st_mode))
134 {
135 /* Don't let stat replace mkdir's errno */
137 retval = -1;
138 break;
139 }
140#else /* WIN32 */
141 /*
142 * On Windows, stat() opens a handle and can transiently fail on a
143 * directory another process is concurrently creating. Probe with
144 * a path-based attribute query instead: it requests only
145 * FILE_READ_ATTRIBUTES and is exempt from share-mode denial, so
146 * it reliably sees a concurrently-created directory. We assume
147 * GetFileAttributes() won't change errno.
148 */
149 DWORD attr = GetFileAttributes(path);
150
151 if (errno != EEXIST ||
152 attr == INVALID_FILE_ATTRIBUTES ||
153 !(attr & FILE_ATTRIBUTE_DIRECTORY))
154 {
155 retval = -1;
156 break;
157 }
158#endif /* WIN32 */
159 }
160
161 if (!last)
162 *p = '/';
163 }
164
165 /* ensure we restored umask */
166 (void) umask(oumask);
167
168 return retval;
169}
static int fb(int x)
#define stat
Definition win32_port.h:74
#define S_IRWXG
Definition win32_port.h:300
#define S_IRWXO
Definition win32_port.h:312
#define S_ISDIR(m)
Definition win32_port.h:315
#define mkdir(a, b)
Definition win32_port.h:80
#define S_IWUSR
Definition win32_port.h:282
#define S_IXUSR
Definition win32_port.h:285
#define S_IRWXU
Definition win32_port.h:288

References fb(), mkdir, S_IRWXG, S_IRWXO, S_IRWXU, S_ISDIR, S_IWUSR, S_IXUSR, and stat.

Referenced by create_data_directory(), create_fullpage_directory(), create_output_directory(), create_xlog_or_symlink(), main(), prepare_for_swap(), recovery_create_dbdir(), StartLogStreamer(), TablespaceCreateDbspace(), and verify_dir_is_empty_or_create().