aboutsummaryrefslogtreecommitdiff
path: root/zencore/filesystem.cpp
diff options
context:
space:
mode:
authorMartin Ridgers <[email protected]>2022-01-05 13:04:13 +0100
committerMartin Ridgers <[email protected]>2022-01-05 15:15:47 +0100
commit14549cf29410a88113596e3b33f6791f975b6503 (patch)
treeaa5e8edf8b713319d710394aed11b71f673bf3c8 /zencore/filesystem.cpp
parentContiguous range concepts for C++ libs that don't support them yet (diff)
downloadzen-14549cf29410a88113596e3b33f6791f975b6503.tar.xz
zen-14549cf29410a88113596e3b33f6791f975b6503.zip
PathFromHandle() for Mac
While the /dev/fd/ mount does list the process' open file descriptors it turns out they are not symlinks so the previous readlink() didn't work as first thought.
Diffstat (limited to 'zencore/filesystem.cpp')
-rw-r--r--zencore/filesystem.cpp21
1 files changed, 13 insertions, 8 deletions
diff --git a/zencore/filesystem.cpp b/zencore/filesystem.cpp
index 95b92277b..2507037ea 100644
--- a/zencore/filesystem.cpp
+++ b/zencore/filesystem.cpp
@@ -932,23 +932,28 @@ PathFromHandle(void* NativeHandle)
ZEN_UNUSED(FinalLength);
return FullPath;
-#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
-# if ZEN_PLATFORM_LINUX
- const char* SelfFdPathFormat = "/proc/self/fd/%d";
-# else
- const char* SelfFdPathFormat = "dev/fd/%d";
-# endif
-
+#elif ZEN_PLATFORM_LINUX
char Link[PATH_MAX];
char Path[64];
- sprintf(Path, SelfFdPathFormat, int(uintptr_t(NativeHandle)));
+ sprintf(Path, "/proc/self/fd/%d", int(uintptr_t(NativeHandle)));
ssize_t BytesRead = readlink(Path, Link, sizeof(Link) - 1);
if (BytesRead <= 0)
+ {
return std::filesystem::path();
+ }
Link[BytesRead] = '\0';
return Link;
+#elif ZEN_PLATFORM_MAC
+ int Fd = int(uintptr_t(NativeHandle));
+ char Path[MAXPATHLEN];
+ if (fcntl(Fd, F_GETPATH, Path) < 0)
+ {
+ return std::filesystem::path();
+ }
+
+ return Path;
#endif // ZEN_PLATFORM_WINDOWS
}