razor_atomic_create_dir() shouldn't set mode of existing directories
authorJ. Ali Harlow <ali@juiblex.co.uk>
Tue Sep 30 18:12:55 2014 +0100 (2014-09-30)
changeset 450f969505e9265
parent 449 f3baf790a815
child 451 457307a3966b
razor_atomic_create_dir() shouldn't set mode of existing directories
librazor/atomic-actions.c
librazor/atomic-none.c
     1.1 --- a/librazor/atomic-actions.c	Tue Sep 30 16:19:55 2014 +0100
     1.2 +++ b/librazor/atomic-actions.c	Tue Sep 30 18:12:55 2014 +0100
     1.3 @@ -247,6 +247,7 @@
     1.4  			 struct atomic_action *action)
     1.5  {
     1.6  	mode_t mode;
     1.7 +	struct stat buf;
     1.8  
     1.9  	if (razor_atomic_in_error_state(atomic)) {
    1.10  		atomic_action_free(action);
    1.11 @@ -256,16 +257,22 @@
    1.12  	mode = action->args.u.create_dir.mode & (S_IRWXU | S_IRWXG | S_IRWXO);
    1.13  
    1.14  	if (!mkdir(action->args.path, mode))
    1.15 -		return 0;
    1.16 +		return action;
    1.17  
    1.18 -	if (errno != EEXIST || chmod(action->args.path, mode) < 0) {
    1.19 +	if (errno != EEXIST || stat(action->args.path, &buf)) {
    1.20  		if (!atomic->error)
    1.21  			atomic->error = razor_error_new_posix(action->args.path);
    1.22  		atomic_action_free(action);
    1.23  		return NULL;
    1.24  	}
    1.25  
    1.26 -	return action;
    1.27 +	if (!S_ISDIR(buf.st_mode) && !atomic->error)
    1.28 +		atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, EEXIST,
    1.29 +						    action->args.path,
    1.30 +						    "Not a directory");
    1.31 +
    1.32 +	atomic_action_free(action);
    1.33 +	return NULL;
    1.34  }
    1.35  
    1.36  static struct atomic_action *atomic_action_rmdir(struct razor_atomic *atomic,
     2.1 --- a/librazor/atomic-none.c	Tue Sep 30 16:19:55 2014 +0100
     2.2 +++ b/librazor/atomic-none.c	Tue Sep 30 18:12:55 2014 +0100
     2.3 @@ -98,13 +98,14 @@
     2.4  
     2.5  		if (stat(buffer, &buf) == 0) {
     2.6  			if (!S_ISDIR(buf.st_mode)) {
     2.7 -				atomic->error = razor_error_new_str(buffer,
     2.8 +				atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR,
     2.9 +								    EEXIST,
    2.10 +								    buffer,
    2.11  								    "Not a directory");
    2.12  				return -1;
    2.13  			}
    2.14  		} else if (mkdir(buffer, 0777) < 0) {
    2.15 -			atomic->error = razor_error_new_str(buffer,
    2.16 -							    strerror(errno));
    2.17 +			atomic->error = razor_error_new_posix(buffer);
    2.18  			return -1;
    2.19  		}
    2.20  	}
    2.21 @@ -140,7 +141,7 @@
    2.22  	free(buf);
    2.23  #else
    2.24  	if (remove(path))
    2.25 -		atomic->error = razor_error_new_str(path, strerror(errno));
    2.26 +		atomic->error = razor_error_new_posix(path);
    2.27  #endif
    2.28  
    2.29  	return razor_atomic_in_error_state(atomic);
    2.30 @@ -177,7 +178,7 @@
    2.31  	free(oldbuf);
    2.32  #else
    2.33  	if (rename(oldpath, newpath))
    2.34 -		atomic->error = razor_error_new_str(newpath, strerror(errno));
    2.35 +		atomic->error = razor_error_new_posix(newpath);
    2.36  #endif
    2.37  
    2.38  	return razor_atomic_in_error_state(atomic);
    2.39 @@ -187,19 +188,23 @@
    2.40  razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
    2.41  			mode_t mode)
    2.42  {
    2.43 +	struct stat buf;
    2.44 +
    2.45  	if (razor_atomic_in_error_state(atomic))
    2.46  		return -1;
    2.47  
    2.48  	if (!mkdir(dirname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)))
    2.49  		return 0;
    2.50  
    2.51 -	if (errno != EEXIST) {
    2.52 -		atomic->error = razor_error_new_str(dirname, strerror(errno));
    2.53 +	if (errno != EEXIST || stat(dirname, &buf)) {
    2.54 +		atomic->error = razor_error_new_posix(dirname);
    2.55  		return -1;
    2.56  	}
    2.57  
    2.58 -	if (chmod(dirname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
    2.59 -		atomic->error = razor_error_new_str(dirname, strerror(errno));
    2.60 +	if (!S_ISDIR(buf.st_mode)) {
    2.61 +		atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, EEXIST,
    2.62 +						    action->args.path,
    2.63 +						    "Not a directory");
    2.64  		return -1;
    2.65  	}
    2.66  
    2.67 @@ -215,13 +220,14 @@
    2.68  
    2.69  #if HAVE_SYMLINK
    2.70  	if (symlink(target, path) < 0) {
    2.71 -		atomic->error = razor_error_new_str(path, strerror(errno));
    2.72 +		atomic->error = razor_error_new_posix(path);
    2.73  		return -1;
    2.74  	}
    2.75  
    2.76  	return 0;
    2.77  #else
    2.78 -	atomic->error = razor_error_new_str(NULL,
    2.79 +	atomic->error = razor_error_new_str(RAZOR_GENERAL_ERROR,
    2.80 +					    RAZOR_GENERAL_ERROR_FAILED, NULL,
    2.81  					    "Symbolic links not supported "
    2.82  					    "on this platform");
    2.83  
    2.84 @@ -242,7 +248,7 @@
    2.85  		  mode & (S_IRWXU | S_IRWXG | S_IRWXO));
    2.86  
    2.87  	if (fd == -1)
    2.88 -		atomic->error = razor_error_new_str(filename, strerror(errno));
    2.89 +		atomic->error = razor_error_new_posix(filename);
    2.90  
    2.91  	return fd;
    2.92  }