From a33f829b5516749f9a894f3c01889926c388c7eb Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Tue, 15 Jan 2019 14:58:14 -0600 Subject: [PATCH] Add GetEntryType to IFileSystem --- src/LibHac.Nand/FatFileSystemProvider.cs | 11 +++++ src/LibHac/IO/AesXtsFileSystem.cs | 5 +++ src/LibHac/IO/ConcatenationFileSystem.cs | 9 ++++ src/LibHac/IO/IFileSystem.cs | 1 + src/LibHac/IO/LayeredFileSystem.cs | 20 +++++++++ src/LibHac/IO/LocalFileSystem.cs | 18 ++++++++ src/LibHac/IO/PartitionFileSystem.cs | 11 +++++ src/LibHac/IO/RomFsFileSystem.cs | 47 +++++++------------- src/LibHac/IO/Save/SaveDataFileSystem.cs | 5 +++ src/LibHac/IO/Save/SaveDataFileSystemCore.cs | 24 +++++++++- src/LibHac/IO/SubdirectoryFileSystem.cs | 7 +++ 11 files changed, 126 insertions(+), 32 deletions(-) diff --git a/src/LibHac.Nand/FatFileSystemProvider.cs b/src/LibHac.Nand/FatFileSystemProvider.cs index 3d771417..4b07cf3b 100644 --- a/src/LibHac.Nand/FatFileSystemProvider.cs +++ b/src/LibHac.Nand/FatFileSystemProvider.cs @@ -57,6 +57,17 @@ namespace LibHac.Nand return Fs.FileExists(path); } + public DirectoryEntryType GetEntryType(string path) + { + path = PathTools.Normalize(path); + string discUtilsPath = ToDiscUtilsPath(path); + + if (Fs.FileExists(discUtilsPath)) return DirectoryEntryType.File; + if (Fs.DirectoryExists(discUtilsPath)) return DirectoryEntryType.Directory; + + throw new FileNotFoundException(path); + } + public FileAttributes GetFileAttributes(string path) { path = ToDiscUtilsPath(PathTools.Normalize(path)); diff --git a/src/LibHac/IO/AesXtsFileSystem.cs b/src/LibHac/IO/AesXtsFileSystem.cs index 74da2039..6368ede4 100644 --- a/src/LibHac/IO/AesXtsFileSystem.cs +++ b/src/LibHac/IO/AesXtsFileSystem.cs @@ -85,6 +85,11 @@ namespace LibHac.IO return BaseFileSystem.FileExists(path); } + public DirectoryEntryType GetEntryType(string path) + { + return BaseFileSystem.GetEntryType(path); + } + public void Commit() { BaseFileSystem.Commit(); diff --git a/src/LibHac/IO/ConcatenationFileSystem.cs b/src/LibHac/IO/ConcatenationFileSystem.cs index dfb8388a..a864497d 100644 --- a/src/LibHac/IO/ConcatenationFileSystem.cs +++ b/src/LibHac/IO/ConcatenationFileSystem.cs @@ -120,6 +120,15 @@ namespace LibHac.IO return BaseFileSystem.FileExists(path) || BaseFileSystem.DirectoryExists(path) && IsSplitFile(path); } + public DirectoryEntryType GetEntryType(string path) + { + path = PathTools.Normalize(path); + + if (IsSplitFile(path)) return DirectoryEntryType.File; + + return BaseFileSystem.GetEntryType(path); + } + public void Commit() { BaseFileSystem.Commit(); diff --git a/src/LibHac/IO/IFileSystem.cs b/src/LibHac/IO/IFileSystem.cs index b8ddfd3f..73c73b42 100644 --- a/src/LibHac/IO/IFileSystem.cs +++ b/src/LibHac/IO/IFileSystem.cs @@ -14,6 +14,7 @@ namespace LibHac.IO void RenameFile(string srcPath, string dstPath); bool DirectoryExists(string path); bool FileExists(string path); + DirectoryEntryType GetEntryType(string path); void Commit(); } diff --git a/src/LibHac/IO/LayeredFileSystem.cs b/src/LibHac/IO/LayeredFileSystem.cs index 594e2d50..f9110359 100644 --- a/src/LibHac/IO/LayeredFileSystem.cs +++ b/src/LibHac/IO/LayeredFileSystem.cs @@ -77,6 +77,26 @@ namespace LibHac.IO return false; } + public DirectoryEntryType GetEntryType(string path) + { + path = PathTools.Normalize(path); + + foreach (IFileSystem fs in Sources) + { + if (fs.FileExists(path)) + { + return DirectoryEntryType.File; + } + + if (fs.DirectoryExists(path)) + { + return DirectoryEntryType.Directory; + } + } + + throw new FileNotFoundException(path); + } + public void Commit() { } public void CreateDirectory(string path) => throw new NotSupportedException(); diff --git a/src/LibHac/IO/LocalFileSystem.cs b/src/LibHac/IO/LocalFileSystem.cs index 1e46ef67..7a404cd8 100644 --- a/src/LibHac/IO/LocalFileSystem.cs +++ b/src/LibHac/IO/LocalFileSystem.cs @@ -121,6 +121,24 @@ namespace LibHac.IO return File.Exists(ResolveLocalPath(path)); } + public DirectoryEntryType GetEntryType(string path) + { + path = PathTools.Normalize(path); + string localPath = ResolveLocalPath(path); + + if (Directory.Exists(localPath)) + { + return DirectoryEntryType.Directory; + } + + if (File.Exists(localPath)) + { + return DirectoryEntryType.File; + } + + throw new FileNotFoundException("path"); + } + public void Commit() { throw new NotImplementedException(); diff --git a/src/LibHac/IO/PartitionFileSystem.cs b/src/LibHac/IO/PartitionFileSystem.cs index fb9ac289..1ffe5f43 100644 --- a/src/LibHac/IO/PartitionFileSystem.cs +++ b/src/LibHac/IO/PartitionFileSystem.cs @@ -93,6 +93,17 @@ namespace LibHac.IO return FileDict.ContainsKey(path); } + public DirectoryEntryType GetEntryType(string path) + { + path = PathTools.Normalize(path); + + if (path == "/") return DirectoryEntryType.Directory; + + if (FileDict.ContainsKey(path)) return DirectoryEntryType.File; + + throw new FileNotFoundException(path); + } + public void Commit() { throw new NotSupportedException(); diff --git a/src/LibHac/IO/RomFsFileSystem.cs b/src/LibHac/IO/RomFsFileSystem.cs index 87c87e4e..7ae44039 100644 --- a/src/LibHac/IO/RomFsFileSystem.cs +++ b/src/LibHac/IO/RomFsFileSystem.cs @@ -86,31 +86,21 @@ namespace LibHac.IO } } + public DirectoryEntryType GetEntryType(string path) + { + path = PathTools.Normalize(path); + + if (FileDict.ContainsKey(path)) return DirectoryEntryType.File; + if (DirectoryDict.ContainsKey(path)) return DirectoryEntryType.Directory; + + throw new FileNotFoundException(path); + } + public void Commit() { throw new NotSupportedException(); } - public void CreateDirectory(string path) - { - throw new NotSupportedException(); - } - - public void CreateFile(string path, long size) - { - throw new NotSupportedException(); - } - - public void DeleteDirectory(string path) - { - throw new NotSupportedException(); - } - - public void DeleteFile(string path) - { - throw new NotSupportedException(); - } - public IDirectory OpenDirectory(string path, OpenDirectoryMode mode) { return new RomFsDirectory(this, path, mode); @@ -138,16 +128,6 @@ namespace LibHac.IO return new RomFsFile(BaseStorage, Header.DataOffset + file.DataOffset, file.DataLength); } - public void RenameDirectory(string srcPath, string dstPath) - { - throw new NotSupportedException(); - } - - public void RenameFile(string srcPath, string dstPath) - { - throw new NotSupportedException(); - } - public bool DirectoryExists(string path) { path = PathTools.Normalize(path); @@ -166,6 +146,13 @@ namespace LibHac.IO { return BaseStorage; } + + public void CreateDirectory(string path) => throw new NotSupportedException(); + public void CreateFile(string path, long size) => throw new NotSupportedException(); + public void DeleteDirectory(string path) => throw new NotSupportedException(); + public void DeleteFile(string path) => throw new NotSupportedException(); + public void RenameDirectory(string srcPath, string dstPath) => throw new NotSupportedException(); + public void RenameFile(string srcPath, string dstPath) => throw new NotSupportedException(); } public class RomfsHeader diff --git a/src/LibHac/IO/Save/SaveDataFileSystem.cs b/src/LibHac/IO/Save/SaveDataFileSystem.cs index 025c13d3..902912ab 100644 --- a/src/LibHac/IO/Save/SaveDataFileSystem.cs +++ b/src/LibHac/IO/Save/SaveDataFileSystem.cs @@ -160,6 +160,11 @@ namespace LibHac.IO.Save public bool DirectoryExists(string path) => SaveDataFileSystemCore.DirectoryExists(path); public bool FileExists(string filename) => SaveDataFileSystemCore.FileExists(filename); + public DirectoryEntryType GetEntryType(string path) + { + return SaveDataFileSystemCore.GetEntryType(path); + } + public void Commit() { Commit(Keyset); diff --git a/src/LibHac/IO/Save/SaveDataFileSystemCore.cs b/src/LibHac/IO/Save/SaveDataFileSystemCore.cs index f5794113..c2490265 100644 --- a/src/LibHac/IO/Save/SaveDataFileSystemCore.cs +++ b/src/LibHac/IO/Save/SaveDataFileSystemCore.cs @@ -95,6 +95,8 @@ namespace LibHac.IO.Save public IFile OpenFile(string path, OpenMode mode) { + path = PathTools.Normalize(path); + if (!FileDictionary.TryGetValue(path, out SaveFileEntry file)) { throw new FileNotFoundException(); @@ -123,10 +125,28 @@ namespace LibHac.IO.Save public bool DirectoryExists(string path) { - throw new System.NotImplementedException(); + path = PathTools.Normalize(path); + + return DirDictionary.ContainsKey(path); + } + + public bool FileExists(string path) + { + path = PathTools.Normalize(path); + + return FileDictionary.ContainsKey(path); + } + + public DirectoryEntryType GetEntryType(string path) + { + path = PathTools.Normalize(path); + + if (DirDictionary.ContainsKey(path)) return DirectoryEntryType.Directory; + if (FileDictionary.ContainsKey(path)) return DirectoryEntryType.File; + + throw new FileNotFoundException(path); } - public bool FileExists(string filename) => FileDictionary.ContainsKey(filename); public void Commit() { throw new System.NotImplementedException(); diff --git a/src/LibHac/IO/SubdirectoryFileSystem.cs b/src/LibHac/IO/SubdirectoryFileSystem.cs index 08a7199d..ac415888 100644 --- a/src/LibHac/IO/SubdirectoryFileSystem.cs +++ b/src/LibHac/IO/SubdirectoryFileSystem.cs @@ -91,6 +91,13 @@ return ParentFileSystem.FileExists(ResolveFullPath(path)); } + public DirectoryEntryType GetEntryType(string path) + { + path = PathTools.Normalize(path); + + return ParentFileSystem.GetEntryType(ResolveFullPath(path)); + } + public void Commit() { ParentFileSystem.Commit();