Add mkdir_errno_wrapper() and use instead of mkdir() in various places
We'd pass pointers to mkdir and mkdir_label to call in various places. mkdir returns the error in errno while mkdir_label returns the error directly.
This commit is contained in:
parent
a1b2c92d82
commit
dae8b82eb9
|
@ -779,13 +779,11 @@ int cg_create(const char *controller, const char *path) {
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
if (mkdir(fs, 0755) < 0) {
|
r = mkdir_errno_wrapper(fs, 0755);
|
||||||
|
if (r == -EEXIST)
|
||||||
if (errno == EEXIST)
|
return 0;
|
||||||
return 0;
|
if (r < 0)
|
||||||
|
return r;
|
||||||
return -errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = cg_hybrid_unified();
|
r = cg_hybrid_unified();
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
|
|
|
@ -50,11 +50,8 @@ int mkdir_label(const char *path, mode_t mode) {
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
if (mkdir(path, mode) < 0)
|
r = mkdir_errno_wrapper(path, mode);
|
||||||
r = -errno;
|
|
||||||
|
|
||||||
mac_selinux_create_file_clear();
|
mac_selinux_create_file_clear();
|
||||||
|
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,8 @@ int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, boo
|
||||||
struct stat st;
|
struct stat st;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
|
assert(_mkdir != mkdir);
|
||||||
|
|
||||||
if (_mkdir(path, mode) >= 0) {
|
if (_mkdir(path, mode) >= 0) {
|
||||||
r = chmod_and_chown(path, mode, uid, gid);
|
r = chmod_and_chown(path, mode, uid, gid);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
|
@ -68,8 +70,14 @@ int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, boo
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int mkdir_errno_wrapper(const char *pathname, mode_t mode) {
|
||||||
|
if (mkdir(pathname, mode) < 0)
|
||||||
|
return -errno;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, bool follow_symlink) {
|
int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, bool follow_symlink) {
|
||||||
return mkdir_safe_internal(path, mode, uid, gid, follow_symlink, mkdir);
|
return mkdir_safe_internal(path, mode, uid, gid, follow_symlink, mkdir_errno_wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
|
int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
|
||||||
|
@ -77,6 +85,7 @@ int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mk
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(path);
|
assert(path);
|
||||||
|
assert(_mkdir != mkdir);
|
||||||
|
|
||||||
if (prefix && !path_startswith(path, prefix))
|
if (prefix && !path_startswith(path, prefix))
|
||||||
return -ENOTDIR;
|
return -ENOTDIR;
|
||||||
|
@ -104,8 +113,7 @@ int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mk
|
||||||
e = p + strcspn(p, "/");
|
e = p + strcspn(p, "/");
|
||||||
p = e + strspn(e, "/");
|
p = e + strspn(e, "/");
|
||||||
|
|
||||||
/* Is this the last component? If so, then we're
|
/* Is this the last component? If so, then we're done */
|
||||||
* done */
|
|
||||||
if (*p == 0)
|
if (*p == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -116,13 +124,13 @@ int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mk
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
r = _mkdir(t, mode);
|
r = _mkdir(t, mode);
|
||||||
if (r < 0 && errno != EEXIST)
|
if (r < 0 && r != -EEXIST)
|
||||||
return -errno;
|
return r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int mkdir_parents(const char *path, mode_t mode) {
|
int mkdir_parents(const char *path, mode_t mode) {
|
||||||
return mkdir_parents_internal(NULL, path, mode, mkdir);
|
return mkdir_parents_internal(NULL, path, mode, mkdir_errno_wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
|
int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
|
||||||
|
@ -130,17 +138,19 @@ int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_fu
|
||||||
|
|
||||||
/* Like mkdir -p */
|
/* Like mkdir -p */
|
||||||
|
|
||||||
|
assert(_mkdir != mkdir);
|
||||||
|
|
||||||
r = mkdir_parents_internal(prefix, path, mode, _mkdir);
|
r = mkdir_parents_internal(prefix, path, mode, _mkdir);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
r = _mkdir(path, mode);
|
r = _mkdir(path, mode);
|
||||||
if (r < 0 && (errno != EEXIST || is_dir(path, true) <= 0))
|
if (r < 0 && (r != -EEXIST || is_dir(path, true) <= 0))
|
||||||
return -errno;
|
return r;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mkdir_p(const char *path, mode_t mode) {
|
int mkdir_p(const char *path, mode_t mode) {
|
||||||
return mkdir_p_internal(NULL, path, mode, mkdir);
|
return mkdir_p_internal(NULL, path, mode, mkdir_errno_wrapper);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
int mkdir_errno_wrapper(const char *pathname, mode_t mode);
|
||||||
int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, bool follow_symlink);
|
int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, bool follow_symlink);
|
||||||
int mkdir_parents(const char *path, mode_t mode);
|
int mkdir_parents(const char *path, mode_t mode);
|
||||||
int mkdir_p(const char *path, mode_t mode);
|
int mkdir_p(const char *path, mode_t mode);
|
||||||
|
|
|
@ -931,7 +931,7 @@ int bus_machine_method_bind_mount(sd_bus_message *message, void *userdata, sd_bu
|
||||||
/* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
|
/* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
|
||||||
mount_tmp = strjoina(mount_slave, "/mount");
|
mount_tmp = strjoina(mount_slave, "/mount");
|
||||||
if (S_ISDIR(st.st_mode))
|
if (S_ISDIR(st.st_mode))
|
||||||
r = mkdir(mount_tmp, 0700) < 0 ? -errno : 0;
|
r = mkdir_errno_wrapper(mount_tmp, 0700);
|
||||||
else
|
else
|
||||||
r = touch(mount_tmp);
|
r = touch(mount_tmp);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
|
|
|
@ -474,9 +474,9 @@ static int mkdir_userns(const char *path, mode_t mode, MountSettingsMask mask, u
|
||||||
|
|
||||||
assert(path);
|
assert(path);
|
||||||
|
|
||||||
r = mkdir(path, mode);
|
r = mkdir_errno_wrapper(path, mode);
|
||||||
if (r < 0 && errno != EEXIST)
|
if (r < 0 && r != -EEXIST)
|
||||||
return -errno;
|
return r;
|
||||||
|
|
||||||
if ((mask & MOUNT_USE_USERNS) == 0)
|
if ((mask & MOUNT_USE_USERNS) == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -484,8 +484,7 @@ static int mkdir_userns(const char *path, mode_t mode, MountSettingsMask mask, u
|
||||||
if (mask & MOUNT_IN_USERNS)
|
if (mask & MOUNT_IN_USERNS)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
r = lchown(path, uid_shift, uid_shift);
|
if (lchown(path, uid_shift, uid_shift) < 0)
|
||||||
if (r < 0)
|
|
||||||
return -errno;
|
return -errno;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -1313,13 +1313,14 @@ static int userns_lchown(const char *p, uid_t uid, gid_t gid) {
|
||||||
|
|
||||||
static int userns_mkdir(const char *root, const char *path, mode_t mode, uid_t uid, gid_t gid) {
|
static int userns_mkdir(const char *root, const char *path, mode_t mode, uid_t uid, gid_t gid) {
|
||||||
const char *q;
|
const char *q;
|
||||||
|
int r;
|
||||||
|
|
||||||
q = prefix_roota(root, path);
|
q = prefix_roota(root, path);
|
||||||
if (mkdir(q, mode) < 0) {
|
r = mkdir_errno_wrapper(q, mode);
|
||||||
if (errno == EEXIST)
|
if (r == -EEXIST)
|
||||||
return 0;
|
return 0;
|
||||||
return -errno;
|
if (r < 0)
|
||||||
}
|
return r;
|
||||||
|
|
||||||
return userns_lchown(q, uid, gid);
|
return userns_lchown(q, uid, gid);
|
||||||
}
|
}
|
||||||
|
@ -1599,8 +1600,10 @@ static int setup_pts(const char *dest) {
|
||||||
|
|
||||||
/* Mount /dev/pts itself */
|
/* Mount /dev/pts itself */
|
||||||
p = prefix_roota(dest, "/dev/pts");
|
p = prefix_roota(dest, "/dev/pts");
|
||||||
if (mkdir(p, 0755) < 0)
|
r = mkdir_errno_wrapper(p, 0755);
|
||||||
return log_error_errno(errno, "Failed to create /dev/pts: %m");
|
if (r < 0)
|
||||||
|
return log_error_errno(r, "Failed to create /dev/pts: %m");
|
||||||
|
|
||||||
r = mount_verbose(LOG_ERR, "devpts", p, "devpts", MS_NOSUID|MS_NOEXEC, options);
|
r = mount_verbose(LOG_ERR, "devpts", p, "devpts", MS_NOSUID|MS_NOEXEC, options);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
@ -1846,12 +1849,13 @@ static int setup_journal(const char *directory) {
|
||||||
/* don't create parents here — if the host doesn't have
|
/* don't create parents here — if the host doesn't have
|
||||||
* permanent journal set up, don't force it here */
|
* permanent journal set up, don't force it here */
|
||||||
|
|
||||||
if (mkdir(p, 0755) < 0 && errno != EEXIST) {
|
r = mkdir_errno_wrapper(p, 0755);
|
||||||
|
if (r < 0 && r != -EEXIST) {
|
||||||
if (try) {
|
if (try) {
|
||||||
log_debug_errno(errno, "Failed to create %s, skipping journal setup: %m", p);
|
log_debug_errno(r, "Failed to create %s, skipping journal setup: %m", p);
|
||||||
return 0;
|
return 0;
|
||||||
} else
|
} else
|
||||||
return log_error_errno(errno, "Failed to create %s: %m", p);
|
return log_error_errno(r, "Failed to create %s: %m", p);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (access(p, F_OK) < 0)
|
} else if (access(p, F_OK) < 0)
|
||||||
|
|
|
@ -1700,9 +1700,9 @@ int main(int argc, char *argv[]) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = mkdir("/run/udev", 0755);
|
r = mkdir_errno_wrapper("/run/udev", 0755);
|
||||||
if (r < 0 && errno != EEXIST) {
|
if (r < 0 && r != -EEXIST) {
|
||||||
r = log_error_errno(errno, "could not create /run/udev: %m");
|
log_error_errno(r, "could not create /run/udev: %m");
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue