diff options
| author | Stefan Boberg <[email protected]> | 2023-11-21 14:52:56 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-11-21 14:52:56 +0100 |
| commit | 8e1cb139d817880c557d407c363eb838c0893f5a (patch) | |
| tree | 69da94b1df3f2fd1d19e623084c88543c0ce85f6 /src/zencore/filesystem.cpp | |
| parent | basic ZEN_ASSERT_FORMAT implementation (#556) (diff) | |
| download | zen-8e1cb139d817880c557d407c363eb838c0893f5a.tar.xz zen-8e1cb139d817880c557d407c363eb838c0893f5a.zip | |
zen run command (#552)
initial version -- this is primarily intended to be used for running stress tests and/or benchmarks
example usage:
`zen run -n 10 -- zenserver-test`
`zen run -n 10 -- zenserver-test --ts=core.assert` run zenserver-test 10 times (testing only the `core.assert` test suite)
`zen run --time 600 --basepath=d:\test_dir\test1 -- zenserver-test` keeps spawning new instances for 10 minutes (600 seconds)
Diffstat (limited to 'src/zencore/filesystem.cpp')
| -rw-r--r-- | src/zencore/filesystem.cpp | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/src/zencore/filesystem.cpp b/src/zencore/filesystem.cpp index b941613ec..f117001b5 100644 --- a/src/zencore/filesystem.cpp +++ b/src/zencore/filesystem.cpp @@ -1558,6 +1558,76 @@ RotateFiles(const std::filesystem::path& Filename, std::size_t MaxFiles) return Result; } +std::error_code +RotateDirectories(const std::filesystem::path& DirectoryName, std::size_t MaxDirectories) +{ + const std::filesystem::path BasePath(DirectoryName.parent_path()); + const std::string Stem(DirectoryName.stem().string()); + + auto GetPathForIndex = [&](size_t Index) -> std::filesystem::path { + if (Index == 0) + { + return BasePath / Stem; + } + return BasePath / fmt::format("{}.{}", Stem, Index); + }; + + auto IsEmpty = [](const std::filesystem::path& Path, std::error_code& Ec) -> bool { return std::filesystem::is_empty(Path, Ec); }; + + std::error_code Result; + const bool BaseIsEmpty = IsEmpty(GetPathForIndex(0), Result); + if (Result) + { + return Result; + } + + if (BaseIsEmpty) + return Result; + + for (std::size_t i = MaxDirectories; i > 0; i--) + { + const std::filesystem::path SourcePath = GetPathForIndex(i - 1); + + if (std::filesystem::exists(SourcePath)) + { + std::filesystem::path TargetPath = GetPathForIndex(i); + + std::error_code DummyEc; + if (std::filesystem::exists(TargetPath, DummyEc)) + { + std::filesystem::remove_all(TargetPath, DummyEc); + } + std::filesystem::rename(SourcePath, TargetPath, DummyEc); + } + } + + return Result; +} + +std::filesystem::path +SearchPathForExecutable(std::string_view ExecutableName) +{ +#if ZEN_PLATFORM_WINDOWS + std::wstring Executable(Utf8ToWide(ExecutableName)); + + DWORD Result = SearchPathW(nullptr, Executable.c_str(), L".exe", 0, nullptr, nullptr); + + if (!Result) + return ExecutableName; + + auto PathBuffer = std::make_unique_for_overwrite<WCHAR[]>(Result); + + Result = SearchPathW(nullptr, Executable.c_str(), L".exe", Result, PathBuffer.get(), nullptr); + + if (!Result) + return ExecutableName; + + return PathBuffer.get(); +#else + return ExecutableName; +#endif +} + ////////////////////////////////////////////////////////////////////////// // // Testing related code follows... @@ -1717,6 +1787,55 @@ TEST_CASE("PathBuilder") # endif } +TEST_CASE("RotateDirectories") +{ + std::filesystem::path TestBaseDir = GetRunningExecutablePath().parent_path() / ".test"; + CleanDirectory(TestBaseDir); + std::filesystem::path RotateDir = TestBaseDir / "rotate_dir" / "dir_to_rotate"; + IoBuffer DummyFileData = IoBufferBuilder::MakeCloneFromMemory("blubb", 5); + + auto NewDir = [&] { + CreateDirectories(RotateDir); + WriteFile(RotateDir / ".placeholder", DummyFileData); + }; + + auto DirWithSuffix = [&](int Index) -> std::filesystem::path { return RotateDir.generic_string().append(fmt::format(".{}", Index)); }; + + const int RotateMax = 10; + + NewDir(); + CHECK(std::filesystem::exists(RotateDir)); + RotateDirectories(RotateDir, RotateMax); + CHECK(!std::filesystem::exists(RotateDir)); + CHECK(std::filesystem::exists(DirWithSuffix(1))); + NewDir(); + CHECK(std::filesystem::exists(RotateDir)); + RotateDirectories(RotateDir, RotateMax); + CHECK(!std::filesystem::exists(RotateDir)); + CHECK(std::filesystem::exists(DirWithSuffix(1))); + CHECK(std::filesystem::exists(DirWithSuffix(2))); + + for (int i = 0; i < RotateMax; ++i) + { + NewDir(); + std::error_code Ec = RotateDirectories(RotateDir, 10); + const bool IsError = !!Ec; + CHECK_EQ(IsError, false); + } + + CHECK(!std::filesystem::exists(RotateDir)); + + for (int i = 0; i < RotateMax; ++i) + { + CHECK(std::filesystem::exists(DirWithSuffix(i + 1))); + } + + for (int i = RotateMax; i < RotateMax + 5; ++i) + { + CHECK(!std::filesystem::exists(DirWithSuffix(RotateMax + i + 1))); + } +} + #endif } // namespace zen |