1
0
mirror of https://github.com/systemd/systemd.git synced 2025-08-02 04:22:27 +03:00

fs-util: Allow xopenat() to reopen existing file descriptors

This commit is contained in:
Daan De Meyer
2023-03-23 14:30:43 +01:00
parent 1d5240cfaa
commit 06ca2db39d
2 changed files with 11 additions and 1 deletions

View File

@ -1104,6 +1104,11 @@ int xopenat(int dir_fd, const char *path, int flags, mode_t mode) {
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
assert(path);
if (isempty(path)) {
assert(!FLAGS_SET(flags, O_CREAT|O_EXCL));
return fd_reopen(dir_fd, flags);
}
if (FLAGS_SET(flags, O_DIRECTORY|O_CREAT)) {
r = RET_NERRNO(mkdirat(dir_fd, path, mode));
if (r == -EEXIST) {

View File

@ -1223,7 +1223,7 @@ TEST(openat_report_new) {
TEST(xopenat) {
_cleanup_(rm_rf_physical_and_freep) char *t = NULL;
_cleanup_close_ int tfd = -EBADF, fd = -EBADF;
_cleanup_close_ int tfd = -EBADF, fd = -EBADF, fd2 = -EBADF;
assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0);
@ -1244,6 +1244,11 @@ TEST(xopenat) {
assert_se((fd = xopenat(tfd, "def", O_CREAT|O_EXCL|O_CLOEXEC, 0644)) >= 0);
assert_se(fd_verify_regular(fd) >= 0);
fd = safe_close(fd);
/* Test that we can reopen an existing fd with xopenat() by specifying an empty path. */
assert_se((fd = xopenat(tfd, "def", O_PATH|O_CLOEXEC, 0)) >= 0);
assert_se((fd2 = xopenat(fd, "", O_RDWR|O_CLOEXEC, 0644)) >= 0);
}
TEST(xopenat_lock) {