Jelajahi Sumber

vfs: Add GetEntries method
Maps name string to directory or file.

Zach Hilman 7 tahun lalu
induk
melakukan
d6cbb3a3e0

+ 9 - 0
src/core/file_sys/vfs.cpp

@@ -399,6 +399,15 @@ bool VfsDirectory::Copy(std::string_view src, std::string_view dest) {
     return f2->WriteBytes(f1->ReadAllBytes()) == f1->GetSize();
 }
 
+std::map<std::string, VfsEntryType> VfsDirectory::GetEntries() const {
+    std::map<std::string, VfsEntryType> out;
+    for (const auto& dir : GetSubdirectories())
+        out.emplace(dir->GetName(), VfsEntryType::Directory);
+    for (const auto& file : GetFiles())
+        out.emplace(file->GetName(), VfsEntryType::File);
+    return out;
+}
+
 std::string VfsDirectory::GetFullPath() const {
     if (IsRoot())
         return GetName();

+ 5 - 0
src/core/file_sys/vfs.h

@@ -4,6 +4,7 @@
 
 #pragma once
 
+#include <map>
 #include <memory>
 #include <string>
 #include <string_view>
@@ -265,6 +266,10 @@ public:
     // dest.
     virtual bool Copy(std::string_view src, std::string_view dest);
 
+    // Gets all of the entries directly in the directory (files and dirs), returning a map between
+    // item name -> type.
+    virtual std::map<std::string, VfsEntryType> GetEntries() const;
+
     // Interprets the file with name file instead as a directory of type directory.
     // The directory must have a constructor that takes a single argument of type
     // std::shared_ptr<VfsFile>. Allows to reinterpret container files (i.e NCA, zip, XCI, etc) as a

+ 17 - 0
src/core/file_sys/vfs_real.cpp

@@ -413,6 +413,23 @@ std::string RealVfsDirectory::GetFullPath() const {
     return out;
 }
 
+std::map<std::string, VfsEntryType> RealVfsDirectory::GetEntries() const {
+    if (perms == Mode::Append)
+        return {};
+
+    std::map<std::string, VfsEntryType> out;
+    FileUtil::ForeachDirectoryEntry(
+        nullptr, path,
+        [&out](u64* entries_out, const std::string& directory, const std::string& filename) {
+            const std::string full_path = directory + DIR_SEP + filename;
+            out.emplace(filename, FileUtil::IsDirectory(full_path) ? VfsEntryType::Directory
+                                                                   : VfsEntryType::File);
+            return true;
+        });
+
+    return out;
+}
+
 bool RealVfsDirectory::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) {
     return false;
 }

+ 1 - 0
src/core/file_sys/vfs_real.h

@@ -98,6 +98,7 @@ public:
     bool DeleteFile(std::string_view name) override;
     bool Rename(std::string_view name) override;
     std::string GetFullPath() const override;
+    std::map<std::string, VfsEntryType> GetEntries() const override;
 
 protected:
     bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;