From a7d9fe993a3e0e4d939752d2e447cc2b5fdb80f0 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 30 Nov 2018 14:44:13 -0500 Subject: service/fsp_srv: Implement CleanDirectoryRecursively This is the same behavior-wise as DeleteDirectoryRecursively, with the only difference being that it doesn't delete the top level directory in the hierarchy, so given: root_dir/ - some_dir/ - File.txt - OtherFile.txt The end result is just: root_dir/ --- src/core/file_sys/vfs.cpp | 26 ++++++++++++++++++++++++++ src/core/file_sys/vfs.h | 15 +++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) (limited to 'src/core/file_sys') diff --git a/src/core/file_sys/vfs.cpp b/src/core/file_sys/vfs.cpp index 7b584de7f..a4a3236f4 100644 --- a/src/core/file_sys/vfs.cpp +++ b/src/core/file_sys/vfs.cpp @@ -384,6 +384,28 @@ bool VfsDirectory::DeleteSubdirectoryRecursive(std::string_view name) { return success; } +bool VfsDirectory::CleanSubdirectoryRecursive(std::string_view name) { + auto dir = GetSubdirectory(name); + if (dir == nullptr) { + return false; + } + + bool success = true; + for (const auto& file : dir->GetFiles()) { + if (!dir->DeleteFile(file->GetName())) { + success = false; + } + } + + for (const auto& sdir : dir->GetSubdirectories()) { + if (!dir->DeleteSubdirectoryRecursive(sdir->GetName())) { + success = false; + } + } + + return success; +} + bool VfsDirectory::Copy(std::string_view src, std::string_view dest) { const auto f1 = GetFile(src); auto f2 = CreateFile(dest); @@ -435,6 +457,10 @@ bool ReadOnlyVfsDirectory::DeleteSubdirectory(std::string_view name) { return false; } +bool ReadOnlyVfsDirectory::CleanSubdirectoryRecursive(std::string_view name) { + return false; +} + bool ReadOnlyVfsDirectory::DeleteFile(std::string_view name) { return false; } diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h index 002f99d4e..6e1db36f1 100644 --- a/src/core/file_sys/vfs.h +++ b/src/core/file_sys/vfs.h @@ -245,12 +245,18 @@ public: // any failure. virtual std::shared_ptr CreateDirectoryAbsolute(std::string_view path); - // Deletes the subdirectory with name and returns true on success. + // Deletes the subdirectory with the given name and returns true on success. virtual bool DeleteSubdirectory(std::string_view name) = 0; - // Deletes all subdirectories and files of subdirectory with name recirsively and then deletes - // the subdirectory. Returns true on success. + + // Deletes all subdirectories and files within the provided directory and then deletes + // the directory itself. Returns true on success. virtual bool DeleteSubdirectoryRecursive(std::string_view name); - // Returnes whether or not the file with name name was deleted successfully. + + // Deletes all subdirectories and files within the provided directory. + // Unlike DeleteSubdirectoryRecursive, this does not delete the provided directory. + virtual bool CleanSubdirectoryRecursive(std::string_view name); + + // Returns whether or not the file with name name was deleted successfully. virtual bool DeleteFile(std::string_view name) = 0; // Returns whether or not this directory was renamed to name. @@ -277,6 +283,7 @@ public: std::shared_ptr CreateSubdirectory(std::string_view name) override; std::shared_ptr CreateFile(std::string_view name) override; bool DeleteSubdirectory(std::string_view name) override; + bool CleanSubdirectoryRecursive(std::string_view name) override; bool DeleteFile(std::string_view name) override; bool Rename(std::string_view name) override; }; -- cgit v1.2.3