if (r < 0)
return r;
- if (mkdir(fs, 0755) < 0) {
-
- if (errno == EEXIST)
- return 0;
-
- return -errno;
- }
+ r = mkdir_errno_wrapper(fs, 0755);
+ if (r == -EEXIST)
+ return 0;
+ if (r < 0)
+ return r;
r = cg_hybrid_unified();
if (r < 0)
if (r < 0)
return r;
- if (mkdir(path, mode) < 0)
- r = -errno;
-
+ r = mkdir_errno_wrapper(path, mode);
mac_selinux_create_file_clear();
-
if (r < 0)
return r;
struct stat st;
int r;
+ assert(_mkdir != mkdir);
+
if (_mkdir(path, mode) >= 0) {
r = chmod_and_chown(path, mode, uid, gid);
if (r < 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) {
- 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 r;
assert(path);
+ assert(_mkdir != mkdir);
if (prefix && !path_startswith(path, prefix))
return -ENOTDIR;
e = p + strcspn(p, "/");
p = e + strspn(e, "/");
- /* Is this the last component? If so, then we're
- * done */
+ /* Is this the last component? If so, then we're done */
if (*p == 0)
return 0;
continue;
r = _mkdir(t, mode);
- if (r < 0 && errno != EEXIST)
- return -errno;
+ if (r < 0 && r != -EEXIST)
+ return r;
}
}
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) {
/* Like mkdir -p */
+ assert(_mkdir != mkdir);
+
r = mkdir_parents_internal(prefix, path, mode, _mkdir);
if (r < 0)
return r;
r = _mkdir(path, mode);
- if (r < 0 && (errno != EEXIST || is_dir(path, true) <= 0))
- return -errno;
+ if (r < 0 && (r != -EEXIST || is_dir(path, true) <= 0))
+ return r;
return 0;
}
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);
}
#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_parents(const char *path, mode_t mode);
int mkdir_p(const char *path, mode_t mode);
/* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
mount_tmp = strjoina(mount_slave, "/mount");
if (S_ISDIR(st.st_mode))
- r = mkdir(mount_tmp, 0700) < 0 ? -errno : 0;
+ r = mkdir_errno_wrapper(mount_tmp, 0700);
else
r = touch(mount_tmp);
if (r < 0) {
assert(path);
- r = mkdir(path, mode);
- if (r < 0 && errno != EEXIST)
- return -errno;
+ r = mkdir_errno_wrapper(path, mode);
+ if (r < 0 && r != -EEXIST)
+ return r;
if ((mask & MOUNT_USE_USERNS) == 0)
return 0;
if (mask & MOUNT_IN_USERNS)
return 0;
- r = lchown(path, uid_shift, uid_shift);
- if (r < 0)
+ if (lchown(path, uid_shift, uid_shift) < 0)
return -errno;
return 0;
static int userns_mkdir(const char *root, const char *path, mode_t mode, uid_t uid, gid_t gid) {
const char *q;
+ int r;
q = prefix_roota(root, path);
- if (mkdir(q, mode) < 0) {
- if (errno == EEXIST)
- return 0;
- return -errno;
- }
+ r = mkdir_errno_wrapper(q, mode);
+ if (r == -EEXIST)
+ return 0;
+ if (r < 0)
+ return r;
return userns_lchown(q, uid, gid);
}
/* Mount /dev/pts itself */
p = prefix_roota(dest, "/dev/pts");
- if (mkdir(p, 0755) < 0)
- return log_error_errno(errno, "Failed to create /dev/pts: %m");
+ r = mkdir_errno_wrapper(p, 0755);
+ 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);
if (r < 0)
return r;
/* don't create parents here — if the host doesn't have
* 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) {
- 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;
} 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)
goto exit;
}
- r = mkdir("/run/udev", 0755);
- if (r < 0 && errno != EEXIST) {
- r = log_error_errno(errno, "could not create /run/udev: %m");
+ r = mkdir_errno_wrapper("/run/udev", 0755);
+ if (r < 0 && r != -EEXIST) {
+ log_error_errno(r, "could not create /run/udev: %m");
goto exit;
}