Skip to content

Commit 79af7bd

Browse files
committed
Test getting a file path with procfs and devfs
1 parent 3afd7da commit 79af7bd

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

.github/workflows/posix.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name: posix
33
"on":
44
push:
5-
branches: [main]
5+
branches: [main, file-path]
66
pull_request:
77

88
env:

include/tmp/file

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ public:
7575
/// @returns The underlying implementation-defined handle
7676
native_handle_type native_handle() const noexcept;
7777

78+
/// Returns the managed path
79+
/// @returns The full path this entry manages
80+
operator const std::filesystem::path&() const noexcept;
81+
82+
/// Returns the managed path
83+
/// @returns The full path this entry manages
84+
const std::filesystem::path& path() const noexcept;
85+
7886
/// Moves the managed file to a given target, releasing
7987
/// ownership of the managed file; behaves like `std::filesystem::rename`
8088
/// even when moving between filesystems

src/file.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ file::native_handle_type file::native_handle() const noexcept {
202202
#endif
203203
}
204204

205+
file::operator const fs::path&() const noexcept {
206+
#ifdef __linux__
207+
return fs::path("/proc/self/fd") / std::to_string(native_handle());
208+
#else
209+
return fs::path("/dev/fd") / std::to_string(native_handle());
210+
#endif
211+
}
212+
213+
const fs::path& file::path() const noexcept {
214+
return *this;
215+
}
216+
205217
void file::move(const fs::path& to) {
206218
// TODO: I couldn't figure out how to create a hard link to a file without
207219
// other hard links, so I just copy it even within the same file system

tests/file.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ TEST(file, create) {
8787
EXPECT_EQ(file_stat.st_mode & S_IFMT, S_IFREG); // Is a regular file
8888
EXPECT_EQ(file_stat.st_nlink, 0); // Has no hardlinks
8989
#endif
90+
91+
EXPECT_TRUE(fs::exists(tmpfile));
92+
std::ofstream(tmpfile.path(), std::ios::binary) << "Hello, world!";
93+
94+
tmpfile.seekp(0, std::ios::beg);
95+
96+
std::string content = std::string(std::istreambuf_iterator(tmpfile), {});
97+
EXPECT_EQ(content, "Hello, world!");
9098
}
9199

92100
/// Tests multiple file creation

0 commit comments

Comments
 (0)