From f7b983ccb22074b34ee57e6bc88f826e3ef039f6 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Mon, 20 May 2019 15:01:14 -0500 Subject: [PATCH] Add PathTools.Combine --- src/LibHac.Nand/FatFileSystemDirectory.cs | 4 ++-- src/LibHac/Fs/FileSystemExtensions.cs | 11 ++++++----- src/LibHac/Fs/LocalDirectory.cs | 2 +- src/LibHac/Fs/LocalFileSystem.cs | 2 +- src/LibHac/Fs/PathTools.cs | 17 +++++++++++++++++ src/LibHac/Fs/RomFs/RomFsDirectory.cs | 4 ++-- src/LibHac/Fs/Save/SaveDataDirectory.cs | 4 ++-- src/LibHac/Fs/SubdirectoryFileSystem.cs | 3 +-- .../Fs/SubdirectoryFileSystemDirectory.cs | 2 +- 9 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/LibHac.Nand/FatFileSystemDirectory.cs b/src/LibHac.Nand/FatFileSystemDirectory.cs index f66affa6..add712c0 100644 --- a/src/LibHac.Nand/FatFileSystemDirectory.cs +++ b/src/LibHac.Nand/FatFileSystemDirectory.cs @@ -15,7 +15,7 @@ namespace LibHac.Nand public string FullPath { get; } public OpenDirectoryMode Mode { get; } private DiscDirectoryInfo DirInfo { get; } - + public FatFileSystemDirectory(FatFileSystemProvider fs, string path, OpenDirectoryMode mode) { ParentFileSystem = fs; @@ -38,7 +38,7 @@ namespace LibHac.Nand DirectoryEntryType type = isDir ? DirectoryEntryType.Directory : DirectoryEntryType.File; long length = isDir ? 0 : entry.FileSystem.GetFileLength(entry.FullName); - yield return new DirectoryEntry(entry.Name, FullPath + '/' + entry.Name, type, length) + yield return new DirectoryEntry(entry.Name, PathTools.Combine(FullPath, entry.Name), type, length) { Attributes = entry.Attributes.ToNxAttributes() }; diff --git a/src/LibHac/Fs/FileSystemExtensions.cs b/src/LibHac/Fs/FileSystemExtensions.cs index a8be37da..1a5ac0be 100644 --- a/src/LibHac/Fs/FileSystemExtensions.cs +++ b/src/LibHac/Fs/FileSystemExtensions.cs @@ -18,8 +18,8 @@ namespace LibHac.Fs foreach (DirectoryEntry entry in source.Read()) { - string subSrcPath = PathTools.Normalize(source.FullPath + '/' + entry.Name); - string subDstPath = PathTools.Normalize(dest.FullPath + '/' + entry.Name); + string subSrcPath = PathTools.Normalize(PathTools.Combine(source.FullPath, entry.Name)); + string subDstPath = PathTools.Normalize(PathTools.Combine(dest.FullPath, entry.Name)); if (entry.Type == DirectoryEntryType.Directory) { @@ -95,7 +95,7 @@ namespace LibHac.Fs if (entry.Type != DirectoryEntryType.Directory || !recurse) continue; - IDirectory subDir = fs.OpenDirectory(directory.FullPath + '/' + entry.Name, OpenDirectoryMode.All); + IDirectory subDir = fs.OpenDirectory(PathTools.Combine(directory.FullPath, entry.Name), OpenDirectoryMode.All); foreach (DirectoryEntry subEntry in subDir.EnumerateEntries(searchPattern, searchOptions)) { @@ -189,9 +189,10 @@ namespace LibHac.Fs foreach (DirectoryEntry entry in directory.Read()) { + string subPath = PathTools.Combine(directory.FullPath, entry.Name); + if (entry.Type == DirectoryEntryType.Directory) { - string subPath = directory.FullPath + '/' + entry.Name; IDirectory subDir = fs.OpenDirectory(subPath, OpenDirectoryMode.All); CleanDirectoryRecursivelyGeneric(subDir); @@ -199,7 +200,7 @@ namespace LibHac.Fs } else if (entry.Type == DirectoryEntryType.File) { - fs.DeleteFile(directory.FullPath + '/' + entry.Name); + fs.DeleteFile(subPath); } } } diff --git a/src/LibHac/Fs/LocalDirectory.cs b/src/LibHac/Fs/LocalDirectory.cs index 7d6c21f1..247a2087 100644 --- a/src/LibHac/Fs/LocalDirectory.cs +++ b/src/LibHac/Fs/LocalDirectory.cs @@ -34,7 +34,7 @@ namespace LibHac.Fs DirectoryEntryType type = isDir ? DirectoryEntryType.Directory : DirectoryEntryType.File; long length = isDir ? 0 : ((FileInfo)entry).Length; - yield return new DirectoryEntry(entry.Name, FullPath + '/' + entry.Name, type, length) + yield return new DirectoryEntry(entry.Name, PathTools.Combine(FullPath, entry.Name), type, length) { Attributes = entry.Attributes.ToNxAttributes() }; diff --git a/src/LibHac/Fs/LocalFileSystem.cs b/src/LibHac/Fs/LocalFileSystem.cs index 166faf65..e06c7728 100644 --- a/src/LibHac/Fs/LocalFileSystem.cs +++ b/src/LibHac/Fs/LocalFileSystem.cs @@ -24,7 +24,7 @@ namespace LibHac.Fs internal string ResolveLocalPath(string path) { - return Path.Combine(BasePath, path.TrimStart('/')); + return PathTools.Combine(BasePath, path); } public NxFileAttributes GetFileAttributes(string path) diff --git a/src/LibHac/Fs/PathTools.cs b/src/LibHac/Fs/PathTools.cs index 840cfb1c..d6b2dba6 100644 --- a/src/LibHac/Fs/PathTools.cs +++ b/src/LibHac/Fs/PathTools.cs @@ -217,6 +217,23 @@ namespace LibHac.Fs return path[rootLength] == DirectorySeparator && path.Length > rootLength + 1; } + public static string Combine(string path1, string path2) + { + if(path1 == null || path2 == null) throw new NullReferenceException(); + + if (string.IsNullOrEmpty(path1)) return path2; + if (string.IsNullOrEmpty(path2)) return path1; + + bool hasSeparator = IsDirectorySeparator(path1[path1.Length - 1]) || IsDirectorySeparator(path2[0]); + + if (hasSeparator) + { + return path1 + path2; + } + + return path1 + DirectorySeparator + path2; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static bool IsDirectorySeparator(char c) { diff --git a/src/LibHac/Fs/RomFs/RomFsDirectory.cs b/src/LibHac/Fs/RomFs/RomFsDirectory.cs index 90ca4d98..f6faa0ec 100644 --- a/src/LibHac/Fs/RomFs/RomFsDirectory.cs +++ b/src/LibHac/Fs/RomFs/RomFsDirectory.cs @@ -29,7 +29,7 @@ namespace LibHac.Fs.RomFs { while (tab.FindNextDirectory(ref position, out string name)) { - yield return new DirectoryEntry(name, FullPath + '/' + name, DirectoryEntryType.Directory, 0); + yield return new DirectoryEntry(name, PathTools.Combine(FullPath, name), DirectoryEntryType.Directory, 0); } } @@ -37,7 +37,7 @@ namespace LibHac.Fs.RomFs { while (tab.FindNextFile(ref position, out RomFileInfo info, out string name)) { - yield return new DirectoryEntry(name, FullPath + '/' + name, DirectoryEntryType.File, info.Length); + yield return new DirectoryEntry(name, PathTools.Combine(FullPath, name), DirectoryEntryType.File, info.Length); } } } diff --git a/src/LibHac/Fs/Save/SaveDataDirectory.cs b/src/LibHac/Fs/Save/SaveDataDirectory.cs index 6784ef08..84a26b96 100644 --- a/src/LibHac/Fs/Save/SaveDataDirectory.cs +++ b/src/LibHac/Fs/Save/SaveDataDirectory.cs @@ -29,7 +29,7 @@ namespace LibHac.Fs.Save { while (tab.FindNextDirectory(ref position, out string name)) { - yield return new DirectoryEntry(name, FullPath + '/' + name, DirectoryEntryType.Directory, 0); + yield return new DirectoryEntry(name, PathTools.Combine(FullPath, name), DirectoryEntryType.Directory, 0); } } @@ -37,7 +37,7 @@ namespace LibHac.Fs.Save { while (tab.FindNextFile(ref position, out SaveFileInfo info, out string name)) { - yield return new DirectoryEntry(name, FullPath + '/' + name, DirectoryEntryType.File, info.Length); + yield return new DirectoryEntry(name, PathTools.Combine(FullPath, name), DirectoryEntryType.File, info.Length); } } } diff --git a/src/LibHac/Fs/SubdirectoryFileSystem.cs b/src/LibHac/Fs/SubdirectoryFileSystem.cs index 26037ffb..312f43d5 100644 --- a/src/LibHac/Fs/SubdirectoryFileSystem.cs +++ b/src/LibHac/Fs/SubdirectoryFileSystem.cs @@ -9,8 +9,7 @@ namespace LibHac.Fs private string ResolveFullPath(string path) { - //todo - return RootPath + path; + return PathTools.Combine(RootPath, path); } public SubdirectoryFileSystem(IFileSystem fs, string rootPath) diff --git a/src/LibHac/Fs/SubdirectoryFileSystemDirectory.cs b/src/LibHac/Fs/SubdirectoryFileSystemDirectory.cs index bafcafc7..cc2762d6 100644 --- a/src/LibHac/Fs/SubdirectoryFileSystemDirectory.cs +++ b/src/LibHac/Fs/SubdirectoryFileSystemDirectory.cs @@ -22,7 +22,7 @@ namespace LibHac.Fs { foreach (DirectoryEntry entry in BaseDirectory.Read()) { - yield return new DirectoryEntry(entry.Name, FullPath + '/' + entry.Name, entry.Type, entry.Size); + yield return new DirectoryEntry(entry.Name, PathTools.Combine(FullPath, entry.Name), entry.Type, entry.Size); } }