(mh_makedir): New function
(mh_check_folder): Use mh_makedir to create directories (or directory hierarchies).
Showing
1 changed file
with
53 additions
and
13 deletions
... | @@ -214,6 +214,58 @@ mh_my_email () | ... | @@ -214,6 +214,58 @@ mh_my_email () |
214 | return my_email; | 214 | return my_email; |
215 | } | 215 | } |
216 | 216 | ||
217 | static int | ||
218 | make_dir_hier (const char *p, mode_t perm) | ||
219 | { | ||
220 | int rc = 0; | ||
221 | char *dir = xstrdup (p); | ||
222 | char *q = dir; | ||
223 | |||
224 | while (!rc && (q = strchr (q + 1, '/'))) | ||
225 | { | ||
226 | *q = 0; | ||
227 | if (access (dir, X_OK)) | ||
228 | { | ||
229 | if (errno != ENOENT) | ||
230 | { | ||
231 | mh_error (_("Cannot create directory %s: error accessing name component %s: %s"), | ||
232 | dir, strerror (errno)); | ||
233 | rc = 1; | ||
234 | } | ||
235 | else if ((rc = mkdir (dir, perm))) | ||
236 | mh_error (_("Cannot create directory %s: error creating name component %s: %s"), | ||
237 | dir, rc); | ||
238 | } | ||
239 | *q = '/'; | ||
240 | } | ||
241 | free (dir); | ||
242 | return rc; | ||
243 | } | ||
244 | |||
245 | int | ||
246 | mh_makedir (char *p) | ||
247 | { | ||
248 | int rc; | ||
249 | mode_t save_umask; | ||
250 | mode_t perm = 0711; | ||
251 | char *pb = mh_global_profile_get ("Folder-Protect", NULL); | ||
252 | if (pb) | ||
253 | perm = strtoul (pb, NULL, 8); | ||
254 | |||
255 | save_umask = umask (0); | ||
256 | |||
257 | if ((rc = make_dir_hier (p, perm)) == 0) | ||
258 | { | ||
259 | rc = mkdir (p, perm); | ||
260 | if (rc) | ||
261 | mh_error (_("Cannot create directory %s: %s"), | ||
262 | p, strerror (errno)); | ||
263 | } | ||
264 | |||
265 | umask (save_umask); | ||
266 | return rc; | ||
267 | } | ||
268 | |||
217 | int | 269 | int |
218 | mh_check_folder (char *pathname, int confirm) | 270 | mh_check_folder (char *pathname, int confirm) |
219 | { | 271 | { |
... | @@ -230,19 +282,7 @@ mh_check_folder (char *pathname, int confirm) | ... | @@ -230,19 +282,7 @@ mh_check_folder (char *pathname, int confirm) |
230 | if (errno == ENOENT) | 282 | if (errno == ENOENT) |
231 | { | 283 | { |
232 | if (!confirm || mh_getyn (_("Create folder \"%s\""), p)) | 284 | if (!confirm || mh_getyn (_("Create folder \"%s\""), p)) |
233 | { | 285 | return mh_makedir (p); |
234 | int perm = 0711; | ||
235 | char *pb = mh_global_profile_get ("Folder-Protect", NULL); | ||
236 | if (pb) | ||
237 | perm = strtoul (pb, NULL, 8); | ||
238 | if (mkdir (p, perm)) | ||
239 | { | ||
240 | mh_error (_("Cannot create directory %s: %s"), | ||
241 | p, strerror (errno)); | ||
242 | return 1; | ||
243 | } | ||
244 | return 0; | ||
245 | } | ||
246 | else | 286 | else |
247 | return 1; | 287 | return 1; |
248 | } | 288 | } | ... | ... |
-
Please register or sign in to post a comment