diff options
| author | Stefan Boberg <[email protected]> | 2021-09-27 12:34:52 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-09-27 12:34:52 +0200 |
| commit | f0036eada7f6bcf6e08afe3ea8517367ed73450e (patch) | |
| tree | b1ce3466bba36175cad369028fad1b410a34b5ec /zencore/filesystem.cpp | |
| parent | Fixed httpsys Windows compilation error (diff) | |
| parent | GetWindowsErrorAsString() -> GetSystemErrorAsString() (diff) | |
| download | zen-f0036eada7f6bcf6e08afe3ea8517367ed73450e.tar.xz zen-f0036eada7f6bcf6e08afe3ea8517367ed73450e.zip | |
Merged latest from main
Diffstat (limited to 'zencore/filesystem.cpp')
| -rw-r--r-- | zencore/filesystem.cpp | 83 |
1 files changed, 39 insertions, 44 deletions
diff --git a/zencore/filesystem.cpp b/zencore/filesystem.cpp index 8ddcbac52..d1b8b7aeb 100644 --- a/zencore/filesystem.cpp +++ b/zencore/filesystem.cpp @@ -7,6 +7,8 @@ #include <zencore/iobuffer.h> #include <zencore/logging.h> #include <zencore/string.h> +#include <zencore/testing.h> + #if ZEN_PLATFORM_WINDOWS # include <zencore/windows.h> #endif @@ -24,8 +26,6 @@ #endif #include <filesystem> - -#include <doctest/doctest.h> #include <gsl/gsl-lite.hpp> namespace zen { @@ -158,13 +158,11 @@ CleanDirectory(const wchar_t* DirPath) { return WipeDirectory(DirPath); } - else - { - return CreateDirectories(DirPath); - } + + return CreateDirectories(DirPath); } -#endif // ZEN_PLATFORM_WINDOWS +#endif // ZEN_PLATFORM_WINDOWS bool CreateDirectories(const std::filesystem::path& Dir) @@ -240,7 +238,7 @@ SupportsBlockRefCounting(std::filesystem::path Path) return true; #else return false; -#endif // ZEN_PLATFORM_WINDOWS +#endif // ZEN_PLATFORM_WINDOWS } bool @@ -404,7 +402,7 @@ CloneFile(std::filesystem::path FromPath, std::filesystem::path ToPath) #else ZEN_ERROR("CloneFile() is not implemented on this platform"); return false; -#endif // ZEN_PLATFORM_WINDOWS +#endif // ZEN_PLATFORM_WINDOWS } bool @@ -445,7 +443,7 @@ CopyFile(std::filesystem::path FromPath, std::filesystem::path ToPath, const Cop #else ZEN_ERROR("CopyFile() is not implemented on this platform"); return false; -#endif // ZEN_PLATFORM_WINDOWS +#endif // ZEN_PLATFORM_WINDOWS } void @@ -504,7 +502,7 @@ WriteFile(std::filesystem::path Path, const IoBuffer* const* Data, size_t Buffer { ThrowLastError("File write failed for '{}'"_format(Path)); } -#endif // ZEN_PLATFORM_WINDOWS +#endif // ZEN_PLATFORM_WINDOWS WriteSize -= ChunkSize; DataPtr = reinterpret_cast<const uint8_t*>(DataPtr) + ChunkSize; @@ -528,7 +526,7 @@ FileContents ReadFile(std::filesystem::path Path) { uint64_t FileSizeBytes; - void* Handle; + void* Handle; #if ZEN_PLATFORM_WINDOWS ATL::CHandle FromFile(CreateFileW(Path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr)); @@ -545,7 +543,7 @@ ReadFile(std::filesystem::path Path) } FileSizeBytes = FileSize.EndOfFile.QuadPart; - Handle = FromFile.Detach(); + Handle = FromFile.Detach(); #else int Fd = open(Path.c_str(), O_RDONLY); if (Fd < 0) @@ -558,7 +556,7 @@ ReadFile(std::filesystem::path Path) fstat(Fd, &Stat); FileSizeBytes = Stat.st_size; - Handle = (void*)uintptr_t(Fd); + Handle = (void*)uintptr_t(Fd); #endif FileContents Contents; @@ -599,7 +597,7 @@ ScanFile(std::filesystem::path Path, const uint64_t ChunkSize, std::function<voi #else ZEN_ERROR("ScanFile() is not implemented on this platform"); return false; -#endif // ZEN_PLATFORM_WINDOWS +#endif // ZEN_PLATFORM_WINDOWS } std::string @@ -622,11 +620,8 @@ FileSystemTraversal::TraverseFileSystem(const std::filesystem::path& RootDir, Tr bool Continue = true; CAtlFile RootDirHandle; - HRESULT hRes = RootDirHandle.Create(RootDir.c_str(), - GENERIC_READ, - FILE_SHARE_READ | FILE_SHARE_WRITE, - OPEN_EXISTING, - FILE_FLAG_BACKUP_SEMANTICS); + HRESULT hRes = + RootDirHandle.Create(RootDir.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS); if (FAILED(hRes)) { @@ -682,7 +677,9 @@ FileSystemTraversal::TraverseFileSystem(const std::filesystem::path& RootDir, Tr } else if (DirInfo->FileAttributes & FILE_ATTRIBUTE_DEVICE) { - ZEN_WARN("encountered device node during file system traversal: {} found in {}", WideToUtf8(FileName), WideToUtf8(RootDir.c_str())); + ZEN_WARN("encountered device node during file system traversal: {} found in {}", + WideToUtf8(FileName), + WideToUtf8(RootDir.c_str())); } else { @@ -714,7 +711,7 @@ FileSystemTraversal::TraverseFileSystem(const std::filesystem::path& RootDir, Tr { const char* FileName = Entry->d_name; - struct stat Stat; + struct stat Stat; std::filesystem::path FullPath = RootDir / FileName; stat(FullPath.c_str(), &Stat); @@ -736,16 +733,16 @@ FileSystemTraversal::TraverseFileSystem(const std::filesystem::path& RootDir, Tr else { ZEN_WARN("encountered non-regular file during file system traversal ({}): {} found in {}", - Stat.st_mode, FileName, RootDir.c_str()); + Stat.st_mode, + FileName, + RootDir.c_str()); } } closedir(Dir); -#endif // ZEN_PLATFORM_WINDOWS +#endif // ZEN_PLATFORM_WINDOWS } - - std::filesystem::path PathFromHandle(void* NativeHandle) { @@ -776,7 +773,7 @@ PathFromHandle(void* NativeHandle) return Buffer; #else # error Unimplemented platform -#endif // ZEN_PLATFORM_WINDOWS +#endif // ZEN_PLATFORM_WINDOWS } std::filesystem::path @@ -798,16 +795,16 @@ GetRunningExecutablePath() return Buffer; #else # error Unimplemented platform -#endif // ZEN_PLATFORM_WINDOWS +#endif // ZEN_PLATFORM_WINDOWS } - - ////////////////////////////////////////////////////////////////////////// // // Testing related code follows... // +#if ZEN_WITH_TESTS + void filesystem_forcelink() { @@ -824,24 +821,23 @@ TEST_CASE("filesystem") // PathFromHandle void* Handle; -#if ZEN_PLATFORM_WINDOWS - Handle = CreateFileW(BinPath.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, - OPEN_EXISTING, 0, nullptr); +# if ZEN_PLATFORM_WINDOWS + Handle = CreateFileW(BinPath.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); CHECK(Handle != INVALID_HANDLE_VALUE); -#else +# else int Fd = open(BinPath.c_str(), O_RDONLY); CHECK(Fd >= 0); Handle = (void*)uintptr_t(Fd); -#endif +# endif auto FromHandle = PathFromHandle((void*)uintptr_t(Handle)); CHECK(equivalent(FromHandle, BinPath)); -#if ZEN_PLATFORM_WINDOWS +# if ZEN_PLATFORM_WINDOWS CloseHandle(Handle); -#else +# else close(int(uintptr_t(Handle))); -#endif +# endif // Traversal struct : public FileSystemTraversal::TreeVisitor @@ -851,13 +847,10 @@ TEST_CASE("filesystem") bFoundExpected |= std::filesystem::equivalent(Parent / File, Expected); } - virtual bool VisitDirectory(const std::filesystem::path&, const path_view&) override - { - return true; - } + virtual bool VisitDirectory(const std::filesystem::path&, const path_view&) override { return true; } - bool bFoundExpected = false; - std::filesystem::path Expected; + bool bFoundExpected = false; + std::filesystem::path Expected; } Visitor; Visitor.Expected = BinPath; @@ -865,4 +858,6 @@ TEST_CASE("filesystem") CHECK(Visitor.bFoundExpected); } +#endif + } // namespace zen |