123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #include "libbb.h"
- int FAST_FUNC bb_make_directory(char *path, long mode, int flags)
- {
- mode_t cur_mask;
- mode_t org_mask;
- const char *fail_msg;
- char *s;
- char c;
- struct stat st;
-
- if (LONE_CHAR(path, '/'))
- return 0;
- if (path[0] == '.') {
- if (path[1] == '\0')
- return 0;
- }
- org_mask = cur_mask = (mode_t)-1L;
- s = path;
- while (1) {
- c = '\0';
- if (flags & FILEUTILS_RECUR) {
-
- while (*s) {
- if (*s == '/') {
- do {
- ++s;
- } while (*s == '/');
- c = *s;
- *s = '\0';
- break;
- }
- ++s;
- }
- }
- if (c != '\0') {
-
- if (cur_mask == (mode_t)-1L) {
- mode_t new_mask;
- org_mask = umask(0);
- cur_mask = 0;
-
- new_mask = (org_mask & ~(mode_t)0300);
-
- if (new_mask != cur_mask) {
- cur_mask = new_mask;
- umask(new_mask);
- }
- }
- } else {
-
-
- if (org_mask != cur_mask) {
- cur_mask = org_mask;
- umask(org_mask);
- }
- }
- if (mkdir(path, 0777) < 0) {
-
- if ((errno != EEXIST && errno != EISDIR)
- || !(flags & FILEUTILS_RECUR)
- || ((stat(path, &st) < 0) || !S_ISDIR(st.st_mode))
- ) {
- fail_msg = "create";
- break;
- }
-
- if (!c) {
- goto ret0;
- }
- } else {
- if (flags & FILEUTILS_VERBOSE) {
- printf("created directory: '%s'\n", path);
- }
- }
- if (!c) {
-
- if ((mode != -1) && (chmod(path, mode) < 0)) {
- fail_msg = "set permissions of";
- if (flags & FILEUTILS_IGNORE_CHMOD_ERR) {
- flags = 0;
- goto print_err;
- }
- break;
- }
- goto ret0;
- }
-
- *s = c;
- }
- flags = -1;
- print_err:
- bb_perror_msg("can't %s directory '%s'", fail_msg, path);
- goto ret;
- ret0:
- flags = 0;
- ret:
-
- if (org_mask != cur_mask)
- umask(org_mask);
- return flags;
- }
|