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;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "constants.h"
|
||||
#include "errno-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "format-util.h"
|
||||
#include "glyph-util.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "hostname-util.h"
|
||||
@@ -1672,10 +1673,7 @@ static int user_and_machine_equivalent(const char *user_and_machine) {
|
||||
return true;
|
||||
|
||||
/* Otherwise, we have to figure out our user id and name, and compare things with that. */
|
||||
char buf[DECIMAL_STR_MAX(uid_t)];
|
||||
xsprintf(buf, UID_FMT, uid);
|
||||
|
||||
f = startswith(user_and_machine, buf);
|
||||
f = startswith(user_and_machine, FORMAT_UID(uid));
|
||||
if (!f) {
|
||||
un = getusername_malloc();
|
||||
if (!un)
|
||||
|
||||
@@ -579,10 +579,7 @@ _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat)
|
||||
if (isempty(content))
|
||||
return 0;
|
||||
|
||||
char t[DECIMAL_STR_MAX(uid_t)];
|
||||
xsprintf(t, UID_FMT, uid);
|
||||
|
||||
return string_contains_word(content, NULL, t);
|
||||
return string_contains_word(content, NULL, FORMAT_UID(uid));
|
||||
}
|
||||
|
||||
static int uid_get_array(uid_t uid, const char *variable, char ***array) {
|
||||
|
||||
+4
-10
@@ -141,14 +141,6 @@ static void context_done(Context *c) {
|
||||
uid_range_free(c->uid_range);
|
||||
}
|
||||
|
||||
/* 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(is_set, uid) \
|
||||
((is_set) ? snprintf_ok((char[DECIMAL_STR_MAX(uid_t)]){}, DECIMAL_STR_MAX(uid_t), UID_FMT, uid) : "(unset)")
|
||||
#define FORMAT_GID(is_set, gid) \
|
||||
((is_set) ? snprintf_ok((char[DECIMAL_STR_MAX(gid_t)]){}, DECIMAL_STR_MAX(gid_t), GID_FMT, gid) : "(unset)")
|
||||
|
||||
static void maybe_emit_login_defs_warning(Context *c) {
|
||||
assert(c);
|
||||
|
||||
@@ -1620,7 +1612,8 @@ static int item_equivalent(Item *a, Item *b) {
|
||||
(a->uid_set && a->uid != b->uid)) {
|
||||
log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0,
|
||||
"Item not equivalent because UIDs differ (%s vs. %s)",
|
||||
FORMAT_UID(a->uid_set, a->uid), FORMAT_UID(b->uid_set, b->uid));
|
||||
a->uid_set ? FORMAT_UID(a->uid) : "(unset)",
|
||||
b->uid_set ? FORMAT_UID(b->uid) : "(unset)");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1628,7 +1621,8 @@ static int item_equivalent(Item *a, Item *b) {
|
||||
(a->gid_set && a->gid != b->gid)) {
|
||||
log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0,
|
||||
"Item not equivalent because GIDs differ (%s vs. %s)",
|
||||
FORMAT_GID(a->gid_set, a->gid), FORMAT_GID(b->gid_set, b->gid));
|
||||
a->gid_set ? FORMAT_GID(a->gid) : "(unset)",
|
||||
b->gid_set ? FORMAT_GID(b->gid) : "(unset)");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user