diff --git a/src/ox/fs/filesystem.hpp b/src/ox/fs/filesystem.hpp index 12350e009..1fffc6187 100644 --- a/src/ox/fs/filesystem.hpp +++ b/src/ox/fs/filesystem.hpp @@ -47,6 +47,8 @@ class FileSystem { virtual int remove(uint64_t inode) = 0; + virtual int remove(const char *path) = 0; + virtual void resize(uint64_t size = 0) = 0; virtual int write(const char *path, void *buffer, uint64_t size, uint8_t fileType = NormalFile) = 0; @@ -146,6 +148,8 @@ class FileSystemTemplate: public FileSystem { int remove(uint64_t inode) override; + int remove(const char *path) override; + int write(const char *path, void *buffer, uint64_t size, uint8_t fileType = NormalFile) override; int write(uint64_t inode, void *buffer, uint64_t size, uint8_t fileType = NormalFile) override; @@ -297,6 +301,22 @@ uint8_t *FileSystemTemplate::read(uint64_t inode, size_t *si #pragma warning(default:4244) #endif +#ifdef _MSC_VER +#pragma warning(disable:4244) +#endif +template +int FileSystemTemplate::remove(const char *path) { + auto inode = findInodeOf(path); + if (inode) { + return remove(inode) | rmDirectoryEntry(path); + } else { + return 1; + } +} +#ifdef _MSC_VER +#pragma warning(default:4244) +#endif + #ifdef _MSC_VER #pragma warning(disable:4244) #endif diff --git a/src/ox/fs/test/CMakeLists.txt b/src/ox/fs/test/CMakeLists.txt index 224efc9e6..91084b46a 100644 --- a/src/ox/fs/test/CMakeLists.txt +++ b/src/ox/fs/test/CMakeLists.txt @@ -40,3 +40,4 @@ add_test("Test\\ PathIterator::fileName" FSTests PathIterator::fileName) add_test("Test\\ FileSystem32::findInodeOf\\ /" FSTests "FileSystem32::findInodeOf /") add_test("Test\\ FileSystem32::write\\(string\\)" FSTests "FileSystem32::write(string)") add_test("Test\\ FileSystem32::rmDirectoryEntry\\(string\\)" FSTests "FileSystem32::rmDirectoryEntry(string)") +add_test("Test\\ FileSystem32::remove\\(string\\)" FSTests "FileSystem32::remove(string)") diff --git a/src/ox/fs/test/tests.cpp b/src/ox/fs/test/tests.cpp index 321ad44ca..e26fa0776 100644 --- a/src/ox/fs/test/tests.cpp +++ b/src/ox/fs/test/tests.cpp @@ -187,6 +187,38 @@ map tests = { delete []buff; delete []dataOut; + return retval; + } + }, + { + "FileSystem32::remove(string)", + [](string) { + int retval = 0; + auto path = "/usr/share/test.txt"; + auto dataIn = "test string"; + auto dataOutLen = ox_strlen(dataIn) + 1; + auto dataOut = new char[dataOutLen]; + + const auto size = 1024 * 1024 * 10; + auto buff = new uint8_t[size]; + FileSystem32::format(buff, (FileStore32::FsSize_t) size, true); + auto fs = (FileSystem32*) createFileSystem(buff, size); + + retval |= fs->mkdir("/usr"); + retval |= fs->mkdir("/usr/share"); + + retval |= fs->write(path, (void*) dataIn, ox_strlen(dataIn) + 1); + retval |= fs->read(path, dataOut, dataOutLen); + retval |= ox_strcmp(dataIn, dataOut) != 0; + + retval |= fs->rmDirectoryEntry(path); + // the lookup should fail + retval |= fs->read(path, dataOut, dataOutLen) == 0; + + delete fs; + delete []buff; + delete []dataOut; + return retval; } },