X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=common-src%2Ffile.c;h=b9e9788b8e097889495e85f795a44b9ac4ed1551;hb=HEAD;hp=e8de92c62677904e4d4b06aa20aa3251e6f143eb;hpb=afaa71b3866b46b082b6c895772e15b36d8865fe;p=debian%2Famanda diff --git a/common-src/file.c b/common-src/file.c index e8de92c..b9e9788 100644 --- a/common-src/file.c +++ b/common-src/file.c @@ -1,6 +1,7 @@ /* * Amanda, The Advanced Maryland Automatic Network Disk Archiver * Copyright (c) 1997-1998 University of Maryland at College Park + * Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved. * All Rights Reserved. * * Permission to use, copy, modify, distribute, and sell this software and its @@ -34,47 +35,9 @@ #include "arglist.h" #include "file.h" -static int mk1dir(const char *, mode_t, uid_t, gid_t); static void areads_getbuf(const char *s, int l, int fd); static char *original_cwd = NULL; -/* Make a directory (internal function). - * If the directory already exists then we pretend we created it. - * - * The uid and gid are used only if we are running as root. - */ -static int -mk1dir( - const char *dir, /* directory to create */ - mode_t mode, /* mode for new directory */ - uid_t uid, /* uid for new directory */ - gid_t gid) /* gid for new directory */ -{ - int rc; /* return code */ - - rc = mkdir(dir, mode); - if(rc != 0) { - int serrno; - - serrno = errno; - if(access(dir, F_OK) == 0) - rc = 0; /* someone just beat us to it, so it's OK */ - errno = serrno; - } - - /* mkdir is affected by umask, so set the mode bits manually */ - if (rc == 0) { - rc = chmod(dir, mode); - } - - if (rc == 0 && geteuid() == 0) { - rc = chown(dir, uid, gid); - } - - return rc; -} - - /* * Make a directory hierarchy given an entry to be created (by the caller) * in the new target. In other words, create all the directories down to @@ -94,14 +57,32 @@ mkpdir( rc = 0; - dir = stralloc(file); /* make a copy we can play with */ + /* Remove last member of file, put the result in dir */ + dir = stralloc(file); /* make a copy we can play with */ p = strrchr(dir, '/'); - if(p != dir && p != NULL) { /* got a '/' or a simple name */ - *p = '\0'; + *p = '\0'; - if(access(dir, F_OK) != 0) { /* doesn't exist */ - if(mkpdir(dir, mode, uid, gid) != 0 || - mk1dir(dir, mode, uid, gid) != 0) rc = -1; /* create failed */ + rc = mkdir(dir, mode); + if (rc != 0) { + if (errno == ENOENT) { /* create parent directory */ + rc = mkpdir(dir, mode, uid, gid); + if (rc != 0) + return rc; + rc = mkdir(dir, mode); + } + if (rc != 0 && errno == EEXIST) { + amfree(dir); + return 0; + } + } + + /* mkdir succeded, set permission and ownership */ + if (rc == 0) { + /* mkdir is affected by umask, so set the mode bits manually */ + rc = chmod(dir, mode); + + if (rc == 0 && geteuid() == 0) { + rc = chown(dir, uid, gid); } } @@ -889,6 +870,8 @@ main( g_fprintf(stderr, _(" done.\n")); g_fprintf(stderr, _("Finished.\n")); + + dbclose(); return 0; }