This source file includes following definitions.
- pmix_os_dirpath_create
- pmix_os_dirpath_destroy
- pmix_os_dirpath_is_empty
- pmix_os_dirpath_access
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include "pmix_config.h"
24
25 #include <errno.h>
26 #include <string.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #include <stdlib.h>
31 #if HAVE_SYS_STAT_H
32 #include <sys/stat.h>
33 #endif
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37 #ifdef HAVE_DIRENT_H
38 #include <dirent.h>
39 #endif
40
41 #include "src/util/error.h"
42 #include "src/util/output.h"
43 #include "src/util/os_dirpath.h"
44 #include "src/util/show_help.h"
45 #include "src/util/argv.h"
46 #include "src/util/os_path.h"
47 #include "pmix_common.h"
48
49 static const char path_sep[] = PMIX_PATH_SEP;
50
51 int pmix_os_dirpath_create(const char *path, const mode_t mode)
52 {
53 struct stat buf;
54 char **parts, *tmp;
55 int i, len;
56 int ret;
57
58 if (NULL == path) {
59 return(PMIX_ERR_BAD_PARAM);
60 }
61
62 if (0 == (ret = stat(path, &buf))) {
63 if (mode == (mode & buf.st_mode)) {
64 return(PMIX_SUCCESS);
65 }
66 if (0 == (ret = chmod(path, (buf.st_mode | mode)))) {
67 return(PMIX_SUCCESS);
68 }
69 pmix_show_help("help-pmix-util.txt", "dir-mode", true,
70 path, mode, strerror(errno));
71 return(PMIX_ERR_PERM);
72 }
73
74
75 if (0 == mkdir(path, mode)) {
76 return(PMIX_SUCCESS);
77 }
78
79
80
81
82 parts = pmix_argv_split(path, path_sep[0]);
83
84
85
86
87 tmp = (char*)malloc(strlen(path) + 1);
88 tmp[0] = '\0';
89
90
91
92
93
94 len = pmix_argv_count(parts);
95 for (i = 0; i < len; ++i) {
96 if (i == 0) {
97
98
99
100 if ('/' == path[0]) {
101 strcat(tmp, path_sep);
102 }
103 strcat(tmp, parts[i]);
104 }
105
106
107
108
109 else {
110 if (path_sep[0] != tmp[strlen(tmp) - 1]) {
111 strcat(tmp, path_sep);
112 }
113 strcat(tmp, parts[i]);
114 }
115
116
117 mkdir(tmp, mode);
118 ret = errno;
119 if (0 != stat(tmp, &buf)) {
120 pmix_show_help("help-pmix-util.txt", "mkdir-failed", true,
121 tmp, strerror(ret));
122 pmix_argv_free(parts);
123 free(tmp);
124 return PMIX_ERROR;
125 } else if (i == (len-1) && (mode != (mode & buf.st_mode)) && (0 > chmod(tmp, (buf.st_mode | mode)))) {
126 pmix_show_help("help-pmix-util.txt", "dir-mode", true,
127 tmp, mode, strerror(errno));
128 pmix_argv_free(parts);
129 free(tmp);
130 return(PMIX_ERR_PERM);
131 }
132 }
133
134
135
136 pmix_argv_free(parts);
137 free(tmp);
138 return PMIX_SUCCESS;
139 }
140
141
142
143
144
145
146
147
148
149 int pmix_os_dirpath_destroy(const char *path,
150 bool recursive,
151 pmix_os_dirpath_destroy_callback_fn_t cbfunc)
152 {
153 int rc, exit_status = PMIX_SUCCESS;
154 bool is_dir = false;
155 DIR *dp;
156 struct dirent *ep;
157 char *filenm;
158 struct stat buf;
159
160 if (NULL == path) {
161 return PMIX_ERROR;
162 }
163
164
165
166
167 if (PMIX_SUCCESS != (rc = pmix_os_dirpath_access(path, 0))) {
168 exit_status = rc;
169 goto cleanup;
170 }
171
172
173 dp = opendir(path);
174 if (NULL == dp) {
175 return PMIX_ERROR;
176 }
177
178 while (NULL != (ep = readdir(dp))) {
179
180
181
182 if ((0 == strcmp(ep->d_name, ".")) ||
183 (0 == strcmp(ep->d_name, ".."))) {
184 continue;
185 }
186
187
188 is_dir = false;
189
190
191
192
193
194 filenm = pmix_os_path(false, path, ep->d_name, NULL);
195
196 rc = stat(filenm, &buf);
197 if (0 > rc) {
198
199
200
201
202
203 free(filenm);
204 continue;
205 }
206 if (S_ISDIR(buf.st_mode)) {
207 is_dir = true;
208 }
209
210
211
212
213
214 if (is_dir && !recursive) {
215
216
217
218 exit_status = PMIX_ERROR;
219 free(filenm);
220 continue;
221 }
222
223
224 if (NULL != cbfunc) {
225
226
227
228
229 if (!(cbfunc(path, ep->d_name))) {
230 free(filenm);
231 continue;
232 }
233 }
234
235 if (is_dir) {
236 rc = pmix_os_dirpath_destroy(filenm, recursive, cbfunc);
237 free(filenm);
238 if (PMIX_SUCCESS != rc) {
239 exit_status = rc;
240 closedir(dp);
241 goto cleanup;
242 }
243 } else {
244
245 if (0 != (rc = unlink(filenm))) {
246 exit_status = PMIX_ERROR;
247 }
248 free(filenm);
249 }
250 }
251
252
253 closedir(dp);
254
255 cleanup:
256
257
258
259
260 if(pmix_os_dirpath_is_empty(path)) {
261 rmdir(path);
262 }
263
264 return exit_status;
265 }
266
267 bool pmix_os_dirpath_is_empty(const char *path ) {
268 DIR *dp;
269 struct dirent *ep;
270
271 if (NULL != path) {
272 dp = opendir(path);
273 if (NULL != dp) {
274 while ((ep = readdir(dp))) {
275 if ((0 != strcmp(ep->d_name, ".")) &&
276 (0 != strcmp(ep->d_name, ".."))) {
277 closedir(dp);
278 return false;
279 }
280 }
281 closedir(dp);
282 return true;
283 }
284 return false;
285 }
286
287 return true;
288 }
289
290 int pmix_os_dirpath_access(const char *path, const mode_t in_mode ) {
291 struct stat buf;
292 mode_t loc_mode = S_IRWXU;
293
294
295
296
297 if (0 != in_mode) {
298 loc_mode = in_mode;
299 }
300
301 if (0 == stat(path, &buf)) {
302 if ((buf.st_mode & loc_mode) == loc_mode) {
303 return(PMIX_SUCCESS);
304 } else {
305
306 return(PMIX_ERROR);
307 }
308 } else {
309
310 return( PMIX_ERR_NOT_FOUND );
311 }
312 }