Skip to content

Commit

Permalink
feat: add method to delete a file from a storage.
Browse files Browse the repository at this point in the history
Include unit tests.
Update README and CHANGELOG.
  • Loading branch information
poirierlouis committed Jun 2, 2024
1 parent a3a9cd5 commit f4c5db0
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- method to delete a file from storage with `DeleteFile`.

### Fixed
- support [MO2](https://github.com/ModOrganizer2/modorganizer/).

Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ for file in files {
> It will only include regular files. Using `IsFile()` is redundant as it will
> return `FileSystemStatus.true` for each item.
#### DeleteFile
> DeleteFile(path: String) -> FileSystemStatus
You can delete a file from storage:
```swift
// ...
let status = storage.DeleteFile("to_remove.txt");

if Equals(status, FileSystemStatus.Failure) {
LogChannel(n"Error", "Operation failed.");
} else if Equals(status, FileSystemStatus.Denied) {
LogChannel(n"Error", "Operation denied.");
} else if Equals(status, FileSystemStatus.True) {
LogChannel(n"Info", "File removed.");
}
```

### Read text
> ReadAsText() -> String
> ReadAsLines() -> array<String>
Expand Down
2 changes: 2 additions & 0 deletions scripts/RedFileSystem/FileSystemStorage.reds
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public native class FileSystemStorage {
public native func GetFile(path: String) -> ref<File>;
public native func GetFiles() -> array<ref<File>>;
public native func DeleteFile(path: String) -> FileSystemStatus;
public native func GetAsyncFile(path: String) -> ref<AsyncFile>;
public native func GetAsyncFiles() -> array<ref<AsyncFile>>;
Expand Down
27 changes: 25 additions & 2 deletions scripts/RedFileSystem/Test/FileSystemStorageTest.reds
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class FileSystemStorageTest extends BaseTest {
private cb func Test_GetFiles() {
let files = this.m_storage.GetFiles();
this.ExpectInt32("GetFiles() return 6 files", ArraySize(files), 6);
this.ExpectInt32("GetFiles() return 7 files", ArraySize(files), 7);
let i = 0;
while i < 4 {
Expand All @@ -66,6 +66,9 @@ public class FileSystemStorageTest extends BaseTest {
this.ExpectString("files[5] == 'test.txt'", files[5].GetFilename(), "test.txt");
this.ExpectString("files[5] == '<path>\\test.txt'", files[5].GetAbsolutePath(), s"\(this.STORAGE_PATH)test.txt");
this.ExpectString("files[6] == 'to_remove.txt'", files[6].GetFilename(), "to_remove.txt");
this.ExpectString("files[6] == '<path>\\to_remove.txt'", files[6].GetAbsolutePath(), s"\(this.STORAGE_PATH)to_remove.txt");
}
private cb func Test_GetAsyncFile() {
Expand All @@ -80,7 +83,7 @@ public class FileSystemStorageTest extends BaseTest {
private cb func Test_GetAsyncFiles() {
let files = this.m_storage.GetAsyncFiles();
this.ExpectInt32("GetAsyncFiles() return 6 files", ArraySize(files), 6);
this.ExpectInt32("GetAsyncFiles() return 7 files", ArraySize(files), 7);
let i = 0;
while i < 4 {
Expand All @@ -89,6 +92,26 @@ public class FileSystemStorageTest extends BaseTest {
}
this.ExpectString("files[4] == 'test.json'", files[4].GetFilename(), "test.json");
this.ExpectString("files[5] == 'test.txt'", files[5].GetFilename(), "test.txt");
this.ExpectString("files[6] == 'to_remove.txt'", files[6].GetFilename(), "to_remove.txt");
}
// It must be after Get[Async]Files
private cb func Test_DeleteFile() {
let status = this.m_storage.DeleteFile("..\\..\\..\\..\\..\\..\\..\\steam.exe");
this.ExpectString("DeleteFile denied", s"\(status)", "Denied");
let status = this.m_storage.DeleteFile("test");
this.ExpectString("DeleteFile failure", s"\(status)", "Failure");
let status = this.m_storage.DeleteFile("to_remove_not_found.txt");
this.ExpectString("DeleteFile failure", s"\(status)", "Failure");
// NOTE: it should be false when file didn't exist. MS STL implementation
// is non-conforming?
//this.ExpectString("DeleteFile false", s"\(status)", "False");
let status = this.m_storage.DeleteFile("to_remove.txt");
this.ExpectString("DeleteFile true", s"\(status)", "True");
}
}
6 changes: 6 additions & 0 deletions scripts/StorageTest/to_remove.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
صِف خَلقَ خَودِ كَمِثلِ الشَمسِ إِذ بَزَغَت — يَحظى الضَجيعُ بِها نَجلاءَ مِعطارِ (A poem by Al Farāhīdi)
ऋषियों को सताने वाले दुष्ट राक्षसों के राजा रावण का सर्वनाश करने वाले विष्णुवतार भगवान श्रीराम, अयोध्या के महाराज दशरथ के बड़े सपुत्र थे।
いろはにほへと ちりぬるを わかよたれそ つねならむ うゐのおくやま けふこえて あさきゆめみし ゑひもせす(ん)
Sic fugiens, dux, zelotypos, quam Karus haberis.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
22 changes: 22 additions & 0 deletions src/FileSystemStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ Red::DynArray<Red::Handle<File>> FileSystemStorage::get_files() {
return files;
}

FileSystemStatus FileSystemStorage::delete_file(
const Red::CString& p_path) const {
if (!rw_permission) {
return FileSystemStatus::Denied;
}
std::error_code error;
auto path = restrict_path(p_path.c_str(), error);

if (error) {
return FileSystemStatus::Denied;
}
if (!std::filesystem::is_regular_file(path, error)) {
return FileSystemStatus::Failure;
}
bool is_removed = std::filesystem::remove(path, error);

if (error) {
return FileSystemStatus::Failure;
}
return (is_removed) ? FileSystemStatus::True : FileSystemStatus::False;
}

Red::Handle<AsyncFile> FileSystemStorage::get_async_file(
const Red::CString& p_path) {
if (!rw_permission) {
Expand Down
4 changes: 4 additions & 0 deletions src/FileSystemStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class FileSystemStorage : public Red::IScriptable {
[[nodiscard]] Red::Handle<File> get_file(const Red::CString& p_path);
[[nodiscard]] Red::DynArray<Red::Handle<File>> get_files();

[[nodiscard]] FileSystemStatus delete_file(const Red::CString& p_path) const;

[[nodiscard]] Red::Handle<AsyncFile> get_async_file(
const Red::CString& p_path);
[[nodiscard]] Red::DynArray<Red::Handle<AsyncFile>> get_async_files();
Expand All @@ -59,6 +61,8 @@ RTTI_DEFINE_CLASS(RedFS::FileSystemStorage, {
RTTI_METHOD(get_file, "GetFile");
RTTI_METHOD(get_files, "GetFiles");

RTTI_METHOD(delete_file, "DeleteFile");

RTTI_METHOD(get_async_file, "GetAsyncFile");
RTTI_METHOD(get_async_files, "GetAsyncFiles");
});
Expand Down

0 comments on commit f4c5db0

Please sign in to comment.