22 |
* val. Otherwise, pass -1 to get default permissions. |
* val. Otherwise, pass -1 to get default permissions. |
23 |
*/ |
*/ |
24 |
|
|
|
#include <errno.h> |
|
|
#include <unistd.h> |
|
|
#include <sys/stat.h> |
|
25 |
#include "libbb.h" |
#include "libbb.h" |
26 |
|
|
27 |
int bb_make_directory (char *path, long mode, int flags) |
/* This function is used from NOFORK applets. It must not allocate anything */ |
28 |
|
|
29 |
|
int FAST_FUNC bb_make_directory(char *path, long mode, int flags) |
30 |
{ |
{ |
31 |
mode_t mask; |
mode_t mask; |
32 |
const char *fail_msg; |
const char *fail_msg; |
35 |
struct stat st; |
struct stat st; |
36 |
|
|
37 |
mask = umask(0); |
mask = umask(0); |
38 |
if (mode == -1) { |
umask(mask & ~0300); /* Ensure intermediate dirs are wx */ |
|
umask(mask); |
|
|
mode = (S_IXUSR | S_IXGRP | S_IXOTH | |
|
|
S_IWUSR | S_IWGRP | S_IWOTH | |
|
|
S_IRUSR | S_IRGRP | S_IROTH) & ~mask; |
|
|
} else { |
|
|
umask(mask & ~0300); |
|
|
} |
|
39 |
|
|
40 |
do { |
while (1) { |
41 |
c = 0; |
c = '\0'; |
42 |
|
|
43 |
if (flags & FILEUTILS_RECUR) { /* Get the parent. */ |
if (flags & FILEUTILS_RECUR) { /* Get the parent. */ |
44 |
/* Bypass leading non-'/'s and then subsequent '/'s. */ |
/* Bypass leading non-'/'s and then subsequent '/'s. */ |
47 |
do { |
do { |
48 |
++s; |
++s; |
49 |
} while (*s == '/'); |
} while (*s == '/'); |
50 |
c = *s; /* Save the current char */ |
c = *s; /* Save the current char */ |
51 |
*s = 0; /* and replace it with nul. */ |
*s = '\0'; /* and replace it with nul. */ |
52 |
break; |
break; |
53 |
} |
} |
54 |
++s; |
++s; |
55 |
} |
} |
56 |
} |
} |
57 |
|
|
58 |
|
if (!c) /* Last component uses orig umask */ |
59 |
|
umask(mask); |
60 |
|
|
61 |
if (mkdir(path, 0777) < 0) { |
if (mkdir(path, 0777) < 0) { |
62 |
/* If we failed for any other reason than the directory |
/* If we failed for any other reason than the directory |
63 |
* already exists, output a diagnostic and return -1.*/ |
* already exists, output a diagnostic and return -1. */ |
64 |
if (errno != EEXIST |
if (errno != EEXIST |
65 |
|| !(flags & FILEUTILS_RECUR) |
|| !(flags & FILEUTILS_RECUR) |
66 |
|| (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) { |
|| ((stat(path, &st) < 0) || !S_ISDIR(st.st_mode)) |
67 |
|
) { |
68 |
fail_msg = "create"; |
fail_msg = "create"; |
69 |
umask(mask); |
umask(mask); |
70 |
break; |
break; |
71 |
} |
} |
72 |
/* Since the directory exists, don't attempt to change |
/* Since the directory exists, don't attempt to change |
73 |
* permissions if it was the full target. Note that |
* permissions if it was the full target. Note that |
74 |
* this is not an error conditon. */ |
* this is not an error condition. */ |
75 |
if (!c) { |
if (!c) { |
76 |
umask(mask); |
umask(mask); |
77 |
return 0; |
return 0; |
79 |
} |
} |
80 |
|
|
81 |
if (!c) { |
if (!c) { |
82 |
/* Done. If necessary, updated perms on the newly |
/* Done. If necessary, update perms on the newly |
83 |
* created directory. Failure to update here _is_ |
* created directory. Failure to update here _is_ |
84 |
* an error.*/ |
* an error. */ |
85 |
umask(mask); |
if ((mode != -1) && (chmod(path, mode) < 0)) { |
|
if ((mode != -1) && (chmod(path, mode) < 0)){ |
|
86 |
fail_msg = "set permissions of"; |
fail_msg = "set permissions of"; |
87 |
break; |
break; |
88 |
} |
} |
91 |
|
|
92 |
/* Remove any inserted nul from the path (recursive mode). */ |
/* Remove any inserted nul from the path (recursive mode). */ |
93 |
*s = c; |
*s = c; |
94 |
|
} /* while (1) */ |
95 |
|
|
96 |
} while (1); |
bb_perror_msg("cannot %s directory '%s'", fail_msg, path); |
|
|
|
|
bb_perror_msg ("cannot %s directory '%s'", fail_msg, path); |
|
97 |
return -1; |
return -1; |
98 |
} |
} |