razor_atomic_create_dir() shouldn't set mode of existing directories
authorJ. Ali Harlow <ali@juiblex.co.uk>
Tue, 30 Sep 2014 17:12:55 +0000 (18:12 +0100)
committerJ. Ali Harlow <ali@juiblex.co.uk>
Tue, 30 Sep 2014 17:12:55 +0000 (18:12 +0100)
librazor/atomic-actions.c
librazor/atomic-none.c

index 5c586a5..8215cd4 100644 (file)
@@ -247,6 +247,7 @@ atomic_action_create_dir(struct razor_atomic *atomic,
                         struct atomic_action *action)
 {
        mode_t mode;
+       struct stat buf;
 
        if (razor_atomic_in_error_state(atomic)) {
                atomic_action_free(action);
@@ -256,16 +257,22 @@ atomic_action_create_dir(struct razor_atomic *atomic,
        mode = action->args.u.create_dir.mode & (S_IRWXU | S_IRWXG | S_IRWXO);
 
        if (!mkdir(action->args.path, mode))
-               return 0;
+               return action;
 
-       if (errno != EEXIST || chmod(action->args.path, mode) < 0) {
+       if (errno != EEXIST || stat(action->args.path, &buf)) {
                if (!atomic->error)
                        atomic->error = razor_error_new_posix(action->args.path);
                atomic_action_free(action);
                return NULL;
        }
 
-       return action;
+       if (!S_ISDIR(buf.st_mode) && !atomic->error)
+               atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, EEXIST,
+                                                   action->args.path,
+                                                   "Not a directory");
+
+       atomic_action_free(action);
+       return NULL;
 }
 
 static struct atomic_action *atomic_action_rmdir(struct razor_atomic *atomic,
index c7bf99d..c347faa 100644 (file)
@@ -98,13 +98,14 @@ razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
 
                if (stat(buffer, &buf) == 0) {
                        if (!S_ISDIR(buf.st_mode)) {
-                               atomic->error = razor_error_new_str(buffer,
+                               atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR,
+                                                                   EEXIST,
+                                                                   buffer,
                                                                    "Not a directory");
                                return -1;
                        }
                } else if (mkdir(buffer, 0777) < 0) {
-                       atomic->error = razor_error_new_str(buffer,
-                                                           strerror(errno));
+                       atomic->error = razor_error_new_posix(buffer);
                        return -1;
                }
        }
@@ -140,7 +141,7 @@ razor_atomic_remove(struct razor_atomic *atomic, const char *path)
        free(buf);
 #else
        if (remove(path))
-               atomic->error = razor_error_new_str(path, strerror(errno));
+               atomic->error = razor_error_new_posix(path);
 #endif
 
        return razor_atomic_in_error_state(atomic);
@@ -177,7 +178,7 @@ razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
        free(oldbuf);
 #else
        if (rename(oldpath, newpath))
-               atomic->error = razor_error_new_str(newpath, strerror(errno));
+               atomic->error = razor_error_new_posix(newpath);
 #endif
 
        return razor_atomic_in_error_state(atomic);
@@ -187,19 +188,23 @@ RAZOR_EXPORT int
 razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
                        mode_t mode)
 {
+       struct stat buf;
+
        if (razor_atomic_in_error_state(atomic))
                return -1;
 
        if (!mkdir(dirname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)))
                return 0;
 
-       if (errno != EEXIST) {
-               atomic->error = razor_error_new_str(dirname, strerror(errno));
+       if (errno != EEXIST || stat(dirname, &buf)) {
+               atomic->error = razor_error_new_posix(dirname);
                return -1;
        }
 
-       if (chmod(dirname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
-               atomic->error = razor_error_new_str(dirname, strerror(errno));
+       if (!S_ISDIR(buf.st_mode)) {
+               atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, EEXIST,
+                                                   action->args.path,
+                                                   "Not a directory");
                return -1;
        }
 
@@ -215,13 +220,14 @@ razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
 
 #if HAVE_SYMLINK
        if (symlink(target, path) < 0) {
-               atomic->error = razor_error_new_str(path, strerror(errno));
+               atomic->error = razor_error_new_posix(path);
                return -1;
        }
 
        return 0;
 #else
-       atomic->error = razor_error_new_str(NULL,
+       atomic->error = razor_error_new_str(RAZOR_GENERAL_ERROR,
+                                           RAZOR_GENERAL_ERROR_FAILED, NULL,
                                            "Symbolic links not supported "
                                            "on this platform");
 
@@ -242,7 +248,7 @@ razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
                  mode & (S_IRWXU | S_IRWXG | S_IRWXO));
 
        if (fd == -1)
-               atomic->error = razor_error_new_str(filename, strerror(errno));
+               atomic->error = razor_error_new_posix(filename);
 
        return fd;
 }