diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-27 09:04:05 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-27 09:04:05 -0500 |
commit | b8345246dcc2121bcb6d1515a9341789de20199f (patch) | |
tree | 4a25857512a90ff38e8a40166c54694b74920216 /test/test_common.cpp | |
parent | f84b8259f6e980fed647d8e1ec0634f89ee59c06 (diff) |
First crack at file objects.
Diffstat (limited to 'test/test_common.cpp')
-rw-r--r-- | test/test_common.cpp | 60 |
1 files changed, 56 insertions, 4 deletions
diff --git a/test/test_common.cpp b/test/test_common.cpp index d3e1704..784cb0d 100644 --- a/test/test_common.cpp +++ b/test/test_common.cpp @@ -16,7 +16,12 @@ */ #include "test_common.hpp" + +#include "fud_string.hpp" + #include <cstdlib> +#include <ftw.h> +#include <gtest/gtest.h> namespace fud { @@ -34,22 +39,69 @@ MockFudAlloc globalDefaultMockAlloc{}; MockFudDealloc globalDefaultMockDealloc{}; -void* MockFudAllocator::allocate(size_t size) { +void* MockFudAllocator::allocate(size_t size) +{ return (*m_allocator)(size); } -void MockFudAllocator::deallocate(void* pointer) { +void MockFudAllocator::deallocate(void* pointer) +{ return (*m_deallocator)(pointer); } MockFudAllocator globalMockFudAlloc{}; -void* fudAlloc(size_t size) { +void* fudAlloc(size_t size) +{ return globalMockFudAlloc.allocate(size); } -void fudFree(void* ptr) { +void fudFree(void* ptr) +{ return globalMockFudAlloc.deallocate(ptr); } +int unlink_cb(const char* fpath, const struct stat* sb_unused, int typeflag, struct FTW* ftwbuf) +{ + static_cast<void>(sb_unused); + int retValue = remove(fpath); + + EXPECT_EQ(retValue, 0); + if (retValue != 0) { + perror(fpath); + } + + return retValue; +} + +FudStatus removeRecursive(const String& path) +{ + if (!path.utf8Valid()) { + return FudStatus::Utf8Invalid; + } + if (path.length() < 5) { + return FudStatus::ArgumentInvalid; + } + auto prefix{String::makeFromCString("/tmp/").takeOkay()}; + auto diffResult = compareMem(path.data(), path.length(), prefix.data(), prefix.length()); + if (diffResult.isError()) { + return FudStatus::ArgumentInvalid; + } + auto diff = diffResult.getOkay(); + if (diff != 0) { + return FudStatus::ArgumentInvalid; + } + constexpr int maxOpenFd = 64; + auto status = nftw(path.c_str(), unlink_cb, maxOpenFd, FTW_DEPTH | FTW_PHYS); + if (status == 0) { + return FudStatus::Success; + } + + if (errno == ENOENT) { + return FudStatus::Success; + } + + return FudStatus::Failure; +} + } // namespace fud |