aboutsummaryrefslogtreecommitdiff
path: root/src/zencore
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2024-10-16 09:49:55 +0200
committerGitHub Enterprise <[email protected]>2024-10-16 09:49:55 +0200
commitb254f75968e1a5692fa872fcfda5eaa1a0ed561d (patch)
tree968e5fc30e37295a4c5767d5c290016116e196ab /src/zencore
parentupload linux mac exe to sentry (#196) (diff)
downloadzen-b254f75968e1a5692fa872fcfda5eaa1a0ed561d.tar.xz
zen-b254f75968e1a5692fa872fcfda5eaa1a0ed561d.zip
safer path from handle (#195)
* remove PathFromHandle that throws to give better context on failures
Diffstat (limited to 'src/zencore')
-rw-r--r--src/zencore/filesystem.cpp25
-rw-r--r--src/zencore/include/zencore/filesystem.h4
-rw-r--r--src/zencore/iobuffer.cpp14
-rw-r--r--src/zencore/thread.cpp12
4 files changed, 32 insertions, 23 deletions
diff --git a/src/zencore/filesystem.cpp b/src/zencore/filesystem.cpp
index 7ca076daa..ac2aabbf0 100644
--- a/src/zencore/filesystem.cpp
+++ b/src/zencore/filesystem.cpp
@@ -891,8 +891,13 @@ MoveToFile(std::filesystem::path Path, IoBuffer Data)
return false;
}
#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
- std::filesystem::path SourcePath = PathFromHandle(FileRef.FileHandle);
- int Ret = link(SourcePath.c_str(), Path.c_str());
+ std::error_code Ec;
+ std::filesystem::path SourcePath = PathFromHandle(FileRef.FileHandle, Ec);
+ if (Ec)
+ {
+ return false;
+ }
+ int Ret = link(SourcePath.c_str(), Path.c_str());
if (Ret < 0)
{
int32_t err = errno;
@@ -1409,18 +1414,6 @@ PathFromHandle(void* NativeHandle, std::error_code& Ec)
#endif // ZEN_PLATFORM_WINDOWS
}
-std::filesystem::path
-PathFromHandle(void* NativeHandle)
-{
- std::error_code Ec;
- std::filesystem::path Result = PathFromHandle(NativeHandle, Ec);
- if (Ec)
- {
- throw std::system_error(Ec, fmt::format("failed to get path from file handle '{}'", NativeHandle));
- }
- return Result;
-}
-
uint64_t
FileSizeFromHandle(void* NativeHandle)
{
@@ -1742,7 +1735,9 @@ TEST_CASE("filesystem")
Handle = (void*)uintptr_t(Fd);
# endif
- auto FromHandle = PathFromHandle((void*)uintptr_t(Handle));
+ std::error_code Ec;
+ auto FromHandle = PathFromHandle((void*)uintptr_t(Handle), Ec);
+ CHECK(!Ec);
CHECK(equivalent(FromHandle, BinPath));
# if ZEN_PLATFORM_WINDOWS
diff --git a/src/zencore/include/zencore/filesystem.h b/src/zencore/include/zencore/filesystem.h
index 0aab6a4ae..897a63d8c 100644
--- a/src/zencore/include/zencore/filesystem.h
+++ b/src/zencore/include/zencore/filesystem.h
@@ -35,10 +35,6 @@ ZENCORE_API bool CleanDirectoryExceptDotFiles(const std::filesystem::path& dir);
/** Map native file handle to a path
*/
-ZENCORE_API std::filesystem::path PathFromHandle(void* NativeHandle);
-
-/** Map native file handle to a path
- */
ZENCORE_API std::filesystem::path PathFromHandle(void* NativeHandle, std::error_code& Ec);
/** Get canonical path name from a generic path
diff --git a/src/zencore/iobuffer.cpp b/src/zencore/iobuffer.cpp
index fdee4e5e5..604cd90d6 100644
--- a/src/zencore/iobuffer.cpp
+++ b/src/zencore/iobuffer.cpp
@@ -239,8 +239,18 @@ IoBufferExtendedCore::~IoBufferExtendedCore()
SetFileInformationByHandle(m_FileHandle, FileDispositionInfo, &Fdi, sizeof Fdi);
#else
- std::filesystem::path FilePath = zen::PathFromHandle(m_FileHandle);
- unlink(FilePath.c_str());
+ std::error_code Ec;
+ std::filesystem::path FilePath = zen::PathFromHandle(m_FileHandle, Ec);
+ if (Ec)
+ {
+ ZEN_WARN("Error reported on file handle {}, get path for IoBufferExtendedCore destructor, reason '{}'",
+ m_FileHandle,
+ Ec.message());
+ }
+ else
+ {
+ unlink(FilePath.c_str());
+ }
#endif
}
#if ZEN_PLATFORM_WINDOWS
diff --git a/src/zencore/thread.cpp b/src/zencore/thread.cpp
index 394197b8e..ca3759fbf 100644
--- a/src/zencore/thread.cpp
+++ b/src/zencore/thread.cpp
@@ -348,8 +348,16 @@ NamedEvent::Close()
if (flock(Fd, LOCK_EX | LOCK_NB) == 0)
{
- std::filesystem::path Name = PathFromHandle((void*)(intptr_t(Fd)));
- unlink(Name.c_str());
+ std::error_code Ec;
+ std::filesystem::path Name = PathFromHandle((void*)(intptr_t(Fd)), Ec);
+ if (Ec)
+ {
+ ZEN_WARN("Error reported on get file path from handle {} for named event unlink operation, reason '{}'", Fd, Ec.message());
+ }
+ else
+ {
+ unlink(Name.c_str());
+ }
flock(Fd, LOCK_UN | LOCK_NB);
close(Fd);