Skip to content

Commit

Permalink
io/Beneath: add OpenDirectoryBeneath()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Nov 27, 2023
1 parent 1a70553 commit cfbe01f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/io/Beneath.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,28 @@ OpenReadOnlyBeneath(FileAt file)

return fd;
}

static constexpr struct open_how directory_beneath{
.flags = O_DIRECTORY|O_RDONLY|O_NOCTTY|O_CLOEXEC,
.resolve = RESOLVE_BENEATH|RESOLVE_NO_MAGICLINKS,
};

UniqueFileDescriptor
TryOpenDirectoryBeneath(FileAt file) noexcept
{
assert(file.directory.IsDefined());

int fd = openat2(file.directory.Get(), file.name,
&directory_beneath, sizeof(directory_beneath));
return UniqueFileDescriptor{fd};
}

UniqueFileDescriptor
OpenDirectoryBeneath(FileAt file)
{
auto fd = TryOpenDirectoryBeneath(file);
if (!fd.IsDefined())
throw FmtErrno("Failed to open '{}'", file.name);

return fd;
}
6 changes: 6 additions & 0 deletions src/io/Beneath.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ TryOpenReadOnlyBeneath(FileAt file) noexcept;

UniqueFileDescriptor
OpenReadOnlyBeneath(FileAt file);

UniqueFileDescriptor
TryOpenDirectoryBeneath(FileAt file) noexcept;

UniqueFileDescriptor
OpenDirectoryBeneath(FileAt file);

0 comments on commit cfbe01f

Please sign in to comment.