From: Lennart Poettering Date: Thu, 14 Nov 2024 11:20:07 +0000 (+0100) Subject: fs-util: teach xopenat_full() to pick automatically if given as MODE_INVALID X-Git-Tag: v258-rc1~1738 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8beb8c3e35025395567fe5d0305af4a0c5880210;p=thirdparty%2Fsystemd.git fs-util: teach xopenat_full() to pick automatically if given as MODE_INVALID --- diff --git a/src/basic/chase.c b/src/basic/chase.c index 8eac3566655..43fad0d93ff 100644 --- a/src/basic/chase.c +++ b/src/basic/chase.c @@ -744,10 +744,15 @@ int chase_extract_filename(const char *path, const char *root, char **ret) { return strdup_to(ret, "."); } -int chase_and_open(const char *path, const char *root, ChaseFlags chase_flags, int open_flags, char **ret_path) { +int chase_and_open( + const char *path, + const char *root, + ChaseFlags chase_flags, + int open_flags, + char **ret_path) { + _cleanup_close_ int path_fd = -EBADF; _cleanup_free_ char *p = NULL, *fname = NULL; - mode_t mode = open_flags & O_DIRECTORY ? 0755 : 0644; int r; assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP))); @@ -758,7 +763,7 @@ int chase_and_open(const char *path, const char *root, ChaseFlags chase_flags, i return xopenat_full(AT_FDCWD, path, open_flags | (FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? O_NOFOLLOW : 0), /* xopen_flags = */ 0, - mode); + MODE_INVALID); r = chase(path, root, CHASE_PARENT|chase_flags, &p, &path_fd); if (r < 0) @@ -772,7 +777,7 @@ int chase_and_open(const char *path, const char *root, ChaseFlags chase_flags, i return r; } - r = xopenat_full(path_fd, strempty(fname), open_flags|O_NOFOLLOW, /* xopen_flags = */ 0, mode); + r = xopenat_full(path_fd, strempty(fname), open_flags|O_NOFOLLOW, /* xopen_flags = */ 0, MODE_INVALID); if (r < 0) return r; @@ -948,10 +953,15 @@ int chase_and_open_parent(const char *path, const char *root, ChaseFlags chase_f return pfd; } -int chase_and_openat(int dir_fd, const char *path, ChaseFlags chase_flags, int open_flags, char **ret_path) { +int chase_and_openat( + int dir_fd, + const char *path, + ChaseFlags chase_flags, + int open_flags, + char **ret_path) { + _cleanup_close_ int path_fd = -EBADF; _cleanup_free_ char *p = NULL, *fname = NULL; - mode_t mode = open_flags & O_DIRECTORY ? 0755 : 0644; int r; assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP))); @@ -962,7 +972,7 @@ int chase_and_openat(int dir_fd, const char *path, ChaseFlags chase_flags, int o return xopenat_full(dir_fd, path, open_flags | (FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? O_NOFOLLOW : 0), /* xopen_flags = */ 0, - mode); + MODE_INVALID); r = chaseat(dir_fd, path, chase_flags|CHASE_PARENT, &p, &path_fd); if (r < 0) @@ -974,7 +984,7 @@ int chase_and_openat(int dir_fd, const char *path, ChaseFlags chase_flags, int o return r; } - r = xopenat_full(path_fd, strempty(fname), open_flags|O_NOFOLLOW, /* xopen_flags = */ 0, mode); + r = xopenat_full(path_fd, strempty(fname), open_flags|O_NOFOLLOW, /* xopen_flags= */ 0, MODE_INVALID); if (r < 0) return r; diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index 4ede324c34c..982c3bd5c3e 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -1133,8 +1133,13 @@ int xopenat_full(int dir_fd, const char *path, int open_flags, XOpenFlags xopen_ * • If the path is specified NULL or empty, behaves like fd_reopen(). * * • If XO_NOCOW is specified will turn on the NOCOW btrfs flag on the file, if available. + * + * • If mode is specified as MODE_INVALID, we'll use 0755 for dirs, and 0644 for regular files. */ + if (mode == MODE_INVALID) + mode = (open_flags & O_DIRECTORY) ? 0755 : 0644; + if (isempty(path)) { assert(!FLAGS_SET(open_flags, O_CREAT|O_EXCL)); return fd_reopen(dir_fd, open_flags & ~O_NOFOLLOW);