Merge pull request #24813 from DaanDeMeyer/at-all-the-things

Add more openat() style variants for existing stuff
This commit is contained in:
Luca Boccassi
2022-11-10 10:57:14 +01:00
committed by GitHub
16 changed files with 425 additions and 240 deletions
+8 -8
View File
@@ -194,17 +194,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);
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;
}
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;
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) {