Rewrite, its smaller

This commit is contained in:
Glenn L McGrath
2002-08-23 17:19:26 +00:00
parent 192ff35d9c
commit fbef225c4b

View File

@ -4,6 +4,10 @@
* *
* Copyright (C) 2001 Matt Kraai. * 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 * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
@ -35,37 +39,25 @@
int make_directory (char *path, long mode, int flags) int make_directory (char *path, long mode, int flags)
{ {
if (!(flags & FILEUTILS_RECUR)) { int ret;
if (mkdir (path, 0777) < 0) {
perror_msg ("Cannot create directory `%s'", path); /* Calling apps probably should use 0777 instead of -1
return -1; * then we dont need this condition
*/
if (mode == -1) {
mode = 0777;
} }
if (flags == FILEUTILS_RECUR) {
if (mode != -1 && chmod (path, mode) < 0) { char *pp = strrchr(path, '/');
perror_msg ("Cannot set permissions of directory `%s'", path); if (pp) {
return -1; *pp = '\0';
} make_directory(path, mode, flags);
} else { *pp = '/';
struct stat st;
if (stat (path, &st) < 0 && errno == ENOENT) {
int status;
char *buf, *parent;
mode_t mask;
mask = umask (0);
umask (mask);
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;
} }
} }
ret = mkdir(path, mode);
return 0; if ( (ret == -1) && (errno != EEXIST) ) {
perror_msg("Cannot create directory %s", path);
}
return ret;
} }