format-utils: Expose FORMAT_UID and FORMAT_GID
This pulls this generally useful helper out of sysusers and into the util lib, and updates the places throughout the codebase where it makes sense to use it.
This commit is contained in:
committed by
Lennart Poettering
parent
e2a40cc7b9
commit
80d07162e7
@@ -18,6 +18,14 @@ assert_cc(sizeof(uid_t) == sizeof(uint32_t));
|
||||
assert_cc(sizeof(gid_t) == sizeof(uint32_t));
|
||||
#define GID_FMT "%" PRIu32
|
||||
|
||||
/* Note: the lifetime of the compound literal is the immediately surrounding block,
|
||||
* see C11 §6.5.2.5, and
|
||||
* https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
|
||||
#define FORMAT_UID(uid) \
|
||||
snprintf_ok((char[DECIMAL_STR_MAX(uid_t)]){}, DECIMAL_STR_MAX(uid_t), UID_FMT, uid)
|
||||
#define FORMAT_GID(gid) \
|
||||
snprintf_ok((char[DECIMAL_STR_MAX(gid_t)]){}, DECIMAL_STR_MAX(gid_t), GID_FMT, gid)
|
||||
|
||||
#if SIZEOF_TIME_T == 8
|
||||
# define PRI_TIME PRIi64
|
||||
#elif SIZEOF_TIME_T == 4
|
||||
|
||||
+3
-9
@@ -70,17 +70,11 @@ int mkdirat_safe_internal(
|
||||
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)] = "-";
|
||||
|
||||
if (uid != UID_INVALID)
|
||||
xsprintf(u, UID_FMT, uid);
|
||||
if (gid != UID_INVALID)
|
||||
xsprintf(g, GID_FMT, gid);
|
||||
(gid != GID_INVALID && st.st_gid != gid))
|
||||
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);
|
||||
}
|
||||
path, st.st_uid, st.st_gid, uid != UID_INVALID ? FORMAT_UID(uid) : "-",
|
||||
gid != UID_INVALID ? FORMAT_GID(gid) : "-");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user