diff options
author | Daan De Meyer <daan.j.demeyer@gmail.com> | 2022-09-26 10:15:03 +0200 |
---|---|---|
committer | Daan De Meyer <daan.j.demeyer@gmail.com> | 2022-11-09 11:14:10 +0100 |
commit | 5545141758dc0e4f1db7e185024a2af162a8e742 (patch) | |
tree | 72ce9fef11ffea8636a6ff60df5538d361d3494c /src/basic/fs-util.c | |
parent | 5bc244aaa90211ccd8370535274c266cdff6a1cb (diff) | |
download | systemd-5545141758dc0e4f1db7e185024a2af162a8e742.tar.gz |
fs-util: Add chown_and_chmod_at()
Diffstat (limited to 'src/basic/fs-util.c')
-rw-r--r-- | src/basic/fs-util.c | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index 6b757bd570..3238ff02f7 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -195,17 +195,17 @@ int readlink_and_make_absolute(const char *p, char **r) { return 0; } -int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) { +int chmod_and_chown_at(int dir_fd, const char *path, mode_t mode, uid_t uid, gid_t gid) { _cleanup_close_ int fd = -1; - assert(path); - - fd = open(path, O_PATH|O_CLOEXEC|O_NOFOLLOW); /* Let's acquire an O_PATH fd, as precaution to change - * mode/owner on the same file */ - if (fd < 0) - return -errno; + if (path) { + /* Let's acquire an O_PATH fd, as precaution to change mode/owner on the same file */ + fd = openat(dir_fd, path, O_PATH|O_CLOEXEC|O_NOFOLLOW); + if (fd < 0) + return -errno; + } - return fchmod_and_chown(fd, mode, uid, gid); + return fchmod_and_chown(path ? fd : dir_fd, mode, uid, gid); } int fchmod_and_chown_with_fallback(int fd, const char *path, mode_t mode, uid_t uid, gid_t gid) { |