fileio: teach read_one_line_file_at() XAT_FDROOT support

This commit is contained in:
Lennart Poettering
2026-05-11 12:04:02 +02:00
parent 20594fdd6e
commit 1b8a2b6abe
3 changed files with 102 additions and 9 deletions
+12 -6
View File
@@ -420,11 +420,11 @@ int read_one_line_file_at(int dir_fd, const char *filename, char **ret) {
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
assert(filename);
assert(ret);
r = fopen_unlocked_at(dir_fd, filename, "re", 0, &f);
r = fopen_unlocked_at(dir_fd, filename, "re", /* open_flags= */ 0, &f);
if (r < 0)
return r;
@@ -1010,13 +1010,19 @@ static int xfopenat_regular(int dir_fd, const char *path, const char *mode, int
/* A combination of fopen() with openat() */
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
assert(mode);
assert(ret);
if (dir_fd == AT_FDCWD && path && open_flags == 0)
f = fopen(path, mode);
else {
else if (dir_fd == XAT_FDROOT && path && open_flags == 0) {
_cleanup_free_ char *j = strjoin("/", path);
if (!j)
return -ENOMEM;
f = fopen(j, mode);
} else {
_cleanup_close_ int fd = -EBADF;
int mode_flags;
@@ -1051,7 +1057,7 @@ static int xfopenat_unix_socket(int dir_fd, const char *path, const char *bind_n
FILE *f;
int r;
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
assert(ret);
sk = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
@@ -1099,7 +1105,7 @@ int xfopenat_full(
FILE *f = NULL; /* avoid false maybe-uninitialized warning */
int r;
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
assert(mode);
assert(ret);