From fbef225c4b8d0fd3f1d34e3b0f80bf8e236861e6 Mon Sep 17 00:00:00 2001 From: Glenn L McGrath Date: Fri, 23 Aug 2002 17:19:26 +0000 Subject: Rewrite, its smaller --- libbb/make_directory.c | 52 +++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 30 deletions(-) (limited to 'libbb/make_directory.c') diff --git a/libbb/make_directory.c b/libbb/make_directory.c index a06a410d2..ca5ce7dc2 100644 --- a/libbb/make_directory.c +++ b/libbb/make_directory.c @@ -3,6 +3,10 @@ * Mini make_directory implementation for busybox * * Copyright (C) 2001 Matt Kraai. + * + * Rewriten in 2002 + * Copyright (C) 2002 Glenn McGrath + * Copyright (C) 2002 Vladimir N. Oleynik * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,37 +39,25 @@ int make_directory (char *path, long mode, int flags) { - if (!(flags & FILEUTILS_RECUR)) { - if (mkdir (path, 0777) < 0) { - perror_msg ("Cannot create directory `%s'", path); - return -1; - } - - if (mode != -1 && chmod (path, mode) < 0) { - perror_msg ("Cannot set permissions of directory `%s'", path); - return -1; - } - } else { - struct stat st; - - if (stat (path, &st) < 0 && errno == ENOENT) { - int status; - char *buf, *parent; - mode_t mask; - - mask = umask (0); - umask (mask); + int ret; - buf = xstrdup (path); - parent = dirname (buf); - status = make_directory (parent, (0777 & ~mask) | 0300, - FILEUTILS_RECUR); - free (buf); - - if (status < 0 || make_directory (path, mode, 0) < 0) - return -1; + /* Calling apps probably should use 0777 instead of -1 + * then we dont need this condition + */ + if (mode == -1) { + mode = 0777; + } + if (flags == FILEUTILS_RECUR) { + char *pp = strrchr(path, '/'); + if (pp) { + *pp = '\0'; + make_directory(path, mode, flags); + *pp = '/'; } } - - return 0; + ret = mkdir(path, mode); + if ( (ret == -1) && (errno != EEXIST) ) { + perror_msg("Cannot create directory %s", path); + } + return ret; } -- cgit v1.2.3