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
This commit is contained in:
Alex Barney 2019-06-28 21:21:33 -05:00
parent 8f37b2b1c4
commit bbf92766c0
14 changed files with 52 additions and 210 deletions

View file

@ -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)

View file

@ -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);

View file

@ -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);

View file

@ -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));

View file

@ -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]

View file

@ -95,21 +95,7 @@ namespace LibHac.Fs
void RenameFile(string srcPath, string dstPath);
/// <summary>
/// Determines whether the specified directory exists.
/// </summary>
/// <param name="path">The full path of the directory to check.</param>
/// <returns><see langword="true"/> if the directory exists, otherwise <see langword="false"/>.</returns>
bool DirectoryExists(string path);
/// <summary>
/// Determines whether the specified file exists.
/// </summary>
/// <param name="path">The full path of the file to check.</param>
/// <returns><see langword="true"/> if the file exists, otherwise <see langword="false"/>.</returns>
bool FileExists(string path);
/// <summary>
/// Determines whether the specified path is a file or directory.
/// Determines whether the specified path is a file or directory, or does not exist.
/// </summary>
/// <param name="path">The full path to check.</param>
/// <returns>The <see cref="DirectoryEntryType"/> of the file.</returns>

View file

@ -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 (type != DirectoryEntryType.NotFound) return type;
}
if (fs.DirectoryExists(path))
{
return DirectoryEntryType.Directory;
}
}
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;

View file

@ -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;
}

View file

@ -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);

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -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;
}

View file

@ -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);