From bbf92766c063f10300f3e7aa45327b12651d788c Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Fri, 28 Jun 2019 21:21:33 -0500 Subject: [PATCH] Change behavior of IFileSystem.GetEntryType GetEntryType now returns NotFound if the entry doesn't exist. The FileExists and DirectoryExists functions were removed from IFileSystem and readded as extension methods --- src/LibHac.Nand/FatFileSystemProvider.cs | 18 +----- src/LibHac/Fs/AesXtsFileSystem.cs | 10 ---- src/LibHac/Fs/ConcatenationFileSystem.cs | 14 ----- src/LibHac/Fs/DirectorySaveDataFileSystem.cs | 20 ------- src/LibHac/Fs/FileSystemExtensions.cs | 10 ++++ src/LibHac/Fs/IFileSystem.cs | 16 +---- src/LibHac/Fs/LayeredFileSystem.cs | 63 +++++++------------- src/LibHac/Fs/LocalFileSystem.cs | 15 ----- src/LibHac/Fs/PartitionFileSystem.cs | 17 +----- src/LibHac/Fs/ReadOnlyFileSystem.cs | 10 ---- src/LibHac/Fs/RomFs/RomFsFileSystem.cs | 26 +++----- src/LibHac/Fs/Save/SaveDataFileSystem.cs | 3 - src/LibHac/Fs/Save/SaveDataFileSystemCore.cs | 26 +++----- src/LibHac/Fs/SubdirectoryFileSystem.cs | 14 ----- 14 files changed, 52 insertions(+), 210 deletions(-) diff --git a/src/LibHac.Nand/FatFileSystemProvider.cs b/src/LibHac.Nand/FatFileSystemProvider.cs index b52a988a..81383955 100644 --- a/src/LibHac.Nand/FatFileSystemProvider.cs +++ b/src/LibHac.Nand/FatFileSystemProvider.cs @@ -63,22 +63,6 @@ namespace LibHac.Nand return stream.AsIFile(mode); } - public bool DirectoryExists(string path) - { - path = ToDiscUtilsPath(PathTools.Normalize(path)); - - if (path == @"\") return true; - - return Fs.DirectoryExists(path); - } - - public bool FileExists(string path) - { - path = ToDiscUtilsPath(PathTools.Normalize(path)); - - return Fs.FileExists(path); - } - public DirectoryEntryType GetEntryType(string path) { path = PathTools.Normalize(path); @@ -87,7 +71,7 @@ namespace LibHac.Nand if (Fs.FileExists(discUtilsPath)) return DirectoryEntryType.File; if (Fs.DirectoryExists(discUtilsPath)) return DirectoryEntryType.Directory; - throw new FileNotFoundException(path); + return DirectoryEntryType.NotFound; } public NxFileAttributes GetFileAttributes(string path) diff --git a/src/LibHac/Fs/AesXtsFileSystem.cs b/src/LibHac/Fs/AesXtsFileSystem.cs index 76da250a..facfbe5e 100644 --- a/src/LibHac/Fs/AesXtsFileSystem.cs +++ b/src/LibHac/Fs/AesXtsFileSystem.cs @@ -173,16 +173,6 @@ namespace LibHac.Fs } } - public bool DirectoryExists(string path) - { - return BaseFileSystem.DirectoryExists(path); - } - - public bool FileExists(string path) - { - return BaseFileSystem.FileExists(path); - } - public DirectoryEntryType GetEntryType(string path) { return BaseFileSystem.GetEntryType(path); diff --git a/src/LibHac/Fs/ConcatenationFileSystem.cs b/src/LibHac/Fs/ConcatenationFileSystem.cs index 7b514aeb..409485d9 100644 --- a/src/LibHac/Fs/ConcatenationFileSystem.cs +++ b/src/LibHac/Fs/ConcatenationFileSystem.cs @@ -199,20 +199,6 @@ namespace LibHac.Fs } } - public bool DirectoryExists(string path) - { - path = PathTools.Normalize(path); - - return BaseFileSystem.DirectoryExists(path) && !IsConcatenationFile(path); - } - - public bool FileExists(string path) - { - path = PathTools.Normalize(path); - - return BaseFileSystem.FileExists(path) || BaseFileSystem.DirectoryExists(path) && IsConcatenationFile(path); - } - public DirectoryEntryType GetEntryType(string path) { path = PathTools.Normalize(path); diff --git a/src/LibHac/Fs/DirectorySaveDataFileSystem.cs b/src/LibHac/Fs/DirectorySaveDataFileSystem.cs index 46d2a1d7..d7f109f3 100644 --- a/src/LibHac/Fs/DirectorySaveDataFileSystem.cs +++ b/src/LibHac/Fs/DirectorySaveDataFileSystem.cs @@ -143,26 +143,6 @@ namespace LibHac.Fs } } - public bool DirectoryExists(string path) - { - string fullPath = GetFullPath(PathTools.Normalize(path)); - - lock (Locker) - { - return BaseFs.DirectoryExists(fullPath); - } - } - - public bool FileExists(string path) - { - string fullPath = GetFullPath(PathTools.Normalize(path)); - - lock (Locker) - { - return BaseFs.FileExists(fullPath); - } - } - public DirectoryEntryType GetEntryType(string path) { string fullPath = GetFullPath(PathTools.Normalize(path)); diff --git a/src/LibHac/Fs/FileSystemExtensions.cs b/src/LibHac/Fs/FileSystemExtensions.cs index f78d440d..5a26555b 100644 --- a/src/LibHac/Fs/FileSystemExtensions.cs +++ b/src/LibHac/Fs/FileSystemExtensions.cs @@ -199,6 +199,16 @@ namespace LibHac.Fs { file.Write(source, offset, WriteOption.None); } + + public static bool DirectoryExists(this IFileSystem fs, string path) + { + return fs.GetEntryType(path) == DirectoryEntryType.Directory; + } + + public static bool FileExists(this IFileSystem fs, string path) + { + return fs.GetEntryType(path) == DirectoryEntryType.File; + } } [Flags] diff --git a/src/LibHac/Fs/IFileSystem.cs b/src/LibHac/Fs/IFileSystem.cs index 317f9727..e4b3ba8f 100644 --- a/src/LibHac/Fs/IFileSystem.cs +++ b/src/LibHac/Fs/IFileSystem.cs @@ -95,21 +95,7 @@ namespace LibHac.Fs void RenameFile(string srcPath, string dstPath); /// - /// Determines whether the specified directory exists. - /// - /// The full path of the directory to check. - /// if the directory exists, otherwise . - bool DirectoryExists(string path); - - /// - /// Determines whether the specified file exists. - /// - /// The full path of the file to check. - /// if the file exists, otherwise . - bool FileExists(string path); - - /// - /// Determines whether the specified path is a file or directory. + /// Determines whether the specified path is a file or directory, or does not exist. /// /// The full path to check. /// The of the file. diff --git a/src/LibHac/Fs/LayeredFileSystem.cs b/src/LibHac/Fs/LayeredFileSystem.cs index 64121f78..d0e23930 100644 --- a/src/LibHac/Fs/LayeredFileSystem.cs +++ b/src/LibHac/Fs/LayeredFileSystem.cs @@ -20,7 +20,14 @@ namespace LibHac.Fs foreach (IFileSystem fs in Sources) { - if (fs.DirectoryExists(path)) + DirectoryEntryType type = fs.GetEntryType(path); + + if (type == DirectoryEntryType.File && dirs.Count == 0) + { + ThrowHelper.ThrowResult(ResultFs.PathNotFound); + } + + if (fs.GetEntryType(path) == DirectoryEntryType.Directory) { dirs.Add(fs.OpenDirectory(path, mode)); } @@ -37,64 +44,34 @@ namespace LibHac.Fs foreach (IFileSystem fs in Sources) { - if (fs.FileExists(path)) + DirectoryEntryType type = fs.GetEntryType(path); + + if (type == DirectoryEntryType.File) { return fs.OpenFile(path, mode); } + + if (type == DirectoryEntryType.Directory) + { + ThrowHelper.ThrowResult(ResultFs.PathNotFound); + } } ThrowHelper.ThrowResult(ResultFs.PathNotFound); return default; } - public bool DirectoryExists(string path) - { - path = PathTools.Normalize(path); - - foreach (IFileSystem fs in Sources) - { - if (fs.DirectoryExists(path)) - { - return true; - } - } - - return false; - } - - public bool FileExists(string path) - { - path = PathTools.Normalize(path); - - foreach (IFileSystem fs in Sources) - { - if (fs.FileExists(path)) - { - return true; - } - } - - return false; - } - public DirectoryEntryType GetEntryType(string path) { path = PathTools.Normalize(path); foreach (IFileSystem fs in Sources) { - if (fs.FileExists(path)) - { - return DirectoryEntryType.File; - } + DirectoryEntryType type = fs.GetEntryType(path); - if (fs.DirectoryExists(path)) - { - return DirectoryEntryType.Directory; - } + if (type != DirectoryEntryType.NotFound) return type; } - ThrowHelper.ThrowResult(ResultFs.PathNotFound); return DirectoryEntryType.NotFound; } @@ -104,7 +81,7 @@ namespace LibHac.Fs foreach (IFileSystem fs in Sources) { - if (fs.FileExists(path) || fs.DirectoryExists(path)) + if (fs.GetEntryType(path) != DirectoryEntryType.NotFound) { return fs.GetFileTimeStampRaw(path); } @@ -120,7 +97,7 @@ namespace LibHac.Fs foreach (IFileSystem fs in Sources) { - if (fs.FileExists(path) || fs.DirectoryExists(path)) + if (fs.GetEntryType(path) != DirectoryEntryType.NotFound) { fs.QueryEntry(outBuffer, inBuffer, path, queryId); return; diff --git a/src/LibHac/Fs/LocalFileSystem.cs b/src/LibHac/Fs/LocalFileSystem.cs index d8a8d67e..98aef267 100644 --- a/src/LibHac/Fs/LocalFileSystem.cs +++ b/src/LibHac/Fs/LocalFileSystem.cs @@ -150,20 +150,6 @@ namespace LibHac.Fs File.Move(srcLocalPath, dstLocalPath); } - public bool DirectoryExists(string path) - { - path = PathTools.Normalize(path); - - return Directory.Exists(ResolveLocalPath(path)); - } - - public bool FileExists(string path) - { - path = PathTools.Normalize(path); - - return File.Exists(ResolveLocalPath(path)); - } - public DirectoryEntryType GetEntryType(string path) { path = PathTools.Normalize(path); @@ -179,7 +165,6 @@ namespace LibHac.Fs return DirectoryEntryType.File; } - ThrowHelper.ThrowResult(ResultFs.PathNotFound); return DirectoryEntryType.NotFound; } diff --git a/src/LibHac/Fs/PartitionFileSystem.cs b/src/LibHac/Fs/PartitionFileSystem.cs index 178b19c5..e8d75888 100644 --- a/src/LibHac/Fs/PartitionFileSystem.cs +++ b/src/LibHac/Fs/PartitionFileSystem.cs @@ -51,28 +51,15 @@ namespace LibHac.Fs return new PartitionFile(BaseStorage, HeaderSize + entry.Offset, entry.Size, mode); } - public bool DirectoryExists(string path) - { - path = PathTools.Normalize(path); - return path == "/"; - } - - public bool FileExists(string path) - { - path = PathTools.Normalize(path).TrimStart('/'); - - 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; + if (FileDict.ContainsKey(path.TrimStart('/'))) return DirectoryEntryType.File; - throw new FileNotFoundException(path); + return DirectoryEntryType.NotFound; } public void CreateDirectory(string path) => ThrowHelper.ThrowResult(ResultFs.UnsupportedOperationModifyPartitionFileSystem); diff --git a/src/LibHac/Fs/ReadOnlyFileSystem.cs b/src/LibHac/Fs/ReadOnlyFileSystem.cs index 8d8fac8b..43a9d8c3 100644 --- a/src/LibHac/Fs/ReadOnlyFileSystem.cs +++ b/src/LibHac/Fs/ReadOnlyFileSystem.cs @@ -23,16 +23,6 @@ namespace LibHac.Fs return new ReadOnlyFile(baseFile); } - public bool DirectoryExists(string path) - { - return BaseFs.DirectoryExists(path); - } - - public bool FileExists(string path) - { - return BaseFs.FileExists(path); - } - public DirectoryEntryType GetEntryType(string path) { return BaseFs.GetEntryType(path); diff --git a/src/LibHac/Fs/RomFs/RomFsFileSystem.cs b/src/LibHac/Fs/RomFs/RomFsFileSystem.cs index 8fca3270..414ea295 100644 --- a/src/LibHac/Fs/RomFs/RomFsFileSystem.cs +++ b/src/LibHac/Fs/RomFs/RomFsFileSystem.cs @@ -26,10 +26,16 @@ namespace LibHac.Fs.RomFs { path = PathTools.Normalize(path); - if (FileExists(path)) return DirectoryEntryType.File; - if (DirectoryExists(path)) return DirectoryEntryType.Directory; + if (FileTable.TryOpenFile(path, out RomFileInfo _)) + { + return DirectoryEntryType.File; + } + + if (FileTable.TryOpenDirectory(path, out FindPosition _)) + { + return DirectoryEntryType.Directory; + } - ThrowHelper.ThrowResult(ResultFs.PathNotFound); return DirectoryEntryType.NotFound; } @@ -64,20 +70,6 @@ namespace LibHac.Fs.RomFs return new RomFsFile(BaseStorage, Header.DataOffset + info.Offset, info.Length); } - public bool DirectoryExists(string path) - { - path = PathTools.Normalize(path); - - return FileTable.TryOpenDirectory(path, out FindPosition _); - } - - public bool FileExists(string path) - { - path = PathTools.Normalize(path); - - return FileTable.TryOpenFile(path, out RomFileInfo _); - } - public IStorage GetBaseStorage() { return BaseStorage; diff --git a/src/LibHac/Fs/Save/SaveDataFileSystem.cs b/src/LibHac/Fs/Save/SaveDataFileSystem.cs index a86f3d0f..110ad6a3 100644 --- a/src/LibHac/Fs/Save/SaveDataFileSystem.cs +++ b/src/LibHac/Fs/Save/SaveDataFileSystem.cs @@ -192,9 +192,6 @@ namespace LibHac.Fs.Save SaveDataFileSystemCore.RenameFile(srcPath, dstPath); } - 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); diff --git a/src/LibHac/Fs/Save/SaveDataFileSystemCore.cs b/src/LibHac/Fs/Save/SaveDataFileSystemCore.cs index e5f7b99a..a1fe5c3c 100644 --- a/src/LibHac/Fs/Save/SaveDataFileSystemCore.cs +++ b/src/LibHac/Fs/Save/SaveDataFileSystemCore.cs @@ -142,28 +142,20 @@ namespace LibHac.Fs.Save FileTable.RenameFile(srcPath, dstPath); } - public bool DirectoryExists(string path) - { - path = PathTools.Normalize(path); - - return FileTable.TryOpenDirectory(path, out SaveFindPosition _); - } - - public bool FileExists(string path) - { - path = PathTools.Normalize(path); - - return FileTable.TryOpenFile(path, out SaveFileInfo _); - } - public DirectoryEntryType GetEntryType(string path) { path = PathTools.Normalize(path); - if (FileExists(path)) return DirectoryEntryType.File; - if (DirectoryExists(path)) return DirectoryEntryType.Directory; + if (FileTable.TryOpenFile(path, out SaveFileInfo _)) + { + return DirectoryEntryType.File; + } + + if (FileTable.TryOpenDirectory(path, out SaveFindPosition _)) + { + return DirectoryEntryType.Directory; + } - ThrowHelper.ThrowResult(ResultFs.PathNotFound); return DirectoryEntryType.NotFound; } diff --git a/src/LibHac/Fs/SubdirectoryFileSystem.cs b/src/LibHac/Fs/SubdirectoryFileSystem.cs index 312f43d5..db5f0ab5 100644 --- a/src/LibHac/Fs/SubdirectoryFileSystem.cs +++ b/src/LibHac/Fs/SubdirectoryFileSystem.cs @@ -92,20 +92,6 @@ namespace LibHac.Fs ParentFileSystem.RenameFile(ResolveFullPath(srcPath), ResolveFullPath(dstPath)); } - public bool DirectoryExists(string path) - { - path = PathTools.Normalize(path); - - return ParentFileSystem.DirectoryExists(ResolveFullPath(path)); - } - - public bool FileExists(string path) - { - path = PathTools.Normalize(path); - - return ParentFileSystem.FileExists(ResolveFullPath(path)); - } - public DirectoryEntryType GetEntryType(string path) { path = PathTools.Normalize(path);