From: Lennart Poettering Date: Tue, 9 Jun 2020 14:13:16 +0000 (+0200) Subject: mkdir: use log_full_errno() where appropriate X-Git-Tag: v246-rc1~175^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d582afe144985106bcd4945d89db243b510ce5b3;p=thirdparty%2Fsystemd.git mkdir: use log_full_errno() where appropriate --- diff --git a/src/basic/mkdir.c b/src/basic/mkdir.c index fa682d4c438..ff20cec9858 100644 --- a/src/basic/mkdir.c +++ b/src/basic/mkdir.c @@ -14,11 +14,18 @@ #include "stdio-util.h" #include "user-util.h" -int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags, mkdir_func_t _mkdir) { +int mkdir_safe_internal( + const char *path, + mode_t mode, + uid_t uid, gid_t gid, + MkdirFlags flags, + mkdir_func_t _mkdir) { + struct stat st; int r; - assert(_mkdir != mkdir); + assert(path); + assert(_mkdir && _mkdir != mkdir); if (_mkdir(path, mode) >= 0) { r = chmod_and_chown(path, mode, uid, gid); @@ -44,19 +51,16 @@ int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, Mkd return -errno; } - if (!S_ISDIR(st.st_mode)) { - log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG, - "Path \"%s\" already exists and is not a directory, refusing.", path); - return -ENOTDIR; - } + if (!S_ISDIR(st.st_mode)) + return log_full_errno(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG, SYNTHETIC_ERRNO(ENOTDIR), + "Path \"%s\" already exists and is not a directory, refusing.", path); if ((st.st_mode & 0007) > (mode & 0007) || (st.st_mode & 0070) > (mode & 0070) || - (st.st_mode & 0700) > (mode & 0700)) { - log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG, - "Directory \"%s\" already exists, but has mode %04o that is too permissive (%04o was requested), refusing.", - path, st.st_mode & 0777, mode); - return -EEXIST; - } + (st.st_mode & 0700) > (mode & 0700)) + return log_full_errno(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG, SYNTHETIC_ERRNO(EEXIST), + "Directory \"%s\" already exists, but has mode %04o that is too permissive (%04o was requested), refusing.", + path, st.st_mode & 0777, mode); + if ((uid != UID_INVALID && st.st_uid != uid) || (gid != GID_INVALID && st.st_gid != gid)) { char u[DECIMAL_STR_MAX(uid_t)] = "-", g[DECIMAL_STR_MAX(gid_t)] = "-"; @@ -65,10 +69,9 @@ int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, Mkd xsprintf(u, UID_FMT, uid); if (gid != UID_INVALID) xsprintf(g, GID_FMT, gid); - log_full(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG, - "Directory \"%s\" already exists, but is owned by "UID_FMT":"GID_FMT" (%s:%s was requested), refusing.", - path, st.st_uid, st.st_gid, u, g); - return -EEXIST; + return log_full_errno(flags & MKDIR_WARN_MODE ? LOG_WARNING : LOG_DEBUG, SYNTHETIC_ERRNO(EEXIST), + "Directory \"%s\" already exists, but is owned by "UID_FMT":"GID_FMT" (%s:%s was requested), refusing.", + path, st.st_uid, st.st_gid, u, g); } return 0;