From 67bf8b19ce41464bf570795e05a047e2476ab2d6 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Mon, 6 May 2019 19:04:05 -0500 Subject: [PATCH] Add QueryEntry to IFileSystem --- src/LibHac.Nand/FatFileSystemProvider.cs | 3 ++- src/LibHac/IO/AesXtsFileSystem.cs | 5 +++++ src/LibHac/IO/ConcatenationFileSystem.cs | 17 +++++++++++++++-- src/LibHac/IO/FileSystemExtensions.cs | 5 +++++ src/LibHac/IO/IFileSystem.cs | 9 ++++++++- src/LibHac/IO/LayeredFileSystem.cs | 16 ++++++++++++++++ src/LibHac/IO/LocalFileSystem.cs | 5 ++++- src/LibHac/IO/PartitionFileSystem.cs | 1 + src/LibHac/IO/RomFs/RomFsFileSystem.cs | 1 + src/LibHac/IO/Save/SaveDataFileSystem.cs | 5 ++++- src/LibHac/IO/Save/SaveDataFileSystemCore.cs | 13 ++++++++----- src/LibHac/IO/SubdirectoryFileSystem.cs | 9 ++++++++- 12 files changed, 77 insertions(+), 12 deletions(-) diff --git a/src/LibHac.Nand/FatFileSystemProvider.cs b/src/LibHac.Nand/FatFileSystemProvider.cs index 06f3ff78..1c814a69 100644 --- a/src/LibHac.Nand/FatFileSystemProvider.cs +++ b/src/LibHac.Nand/FatFileSystemProvider.cs @@ -98,7 +98,8 @@ namespace LibHac.Nand public void CreateFile(string path, long size, CreateFileOptions options) => throw new NotSupportedException(); public void RenameDirectory(string srcPath, string dstPath) => throw new NotSupportedException(); public void RenameFile(string srcPath, string dstPath) => throw new NotSupportedException(); - + public void QueryEntry(Span outBuffer, Span inBuffer, string path, QueryId queryId) => throw new NotSupportedException(); + private static FileAccess GetFileAccess(OpenMode mode) { // FileAccess and OpenMode have the same flags diff --git a/src/LibHac/IO/AesXtsFileSystem.cs b/src/LibHac/IO/AesXtsFileSystem.cs index 5900ef26..5dbd1b30 100644 --- a/src/LibHac/IO/AesXtsFileSystem.cs +++ b/src/LibHac/IO/AesXtsFileSystem.cs @@ -184,6 +184,11 @@ namespace LibHac.IO BaseFileSystem.Commit(); } + public void QueryEntry(Span outBuffer, Span inBuffer, string path, QueryId queryId) + { + BaseFileSystem.QueryEntry(outBuffer, inBuffer, path, queryId); + } + private AesXtsFileHeader ReadXtsHeader(string filePath, string keyPath) { if (!TryReadXtsHeader(filePath, keyPath, out AesXtsFileHeader header)) diff --git a/src/LibHac/IO/ConcatenationFileSystem.cs b/src/LibHac/IO/ConcatenationFileSystem.cs index b73ad533..350ec27c 100644 --- a/src/LibHac/IO/ConcatenationFileSystem.cs +++ b/src/LibHac/IO/ConcatenationFileSystem.cs @@ -28,6 +28,13 @@ namespace LibHac.IO return (attributes & NxFileAttributes.Directory) != 0 && (attributes & NxFileAttributes.Archive) != 0; } + private void SetConcatenationFileAttribute(string path) + { + NxFileAttributes attributes = BaseFileSystem.GetFileAttributes(path); + attributes |= NxFileAttributes.Archive; + BaseFileSystem.SetFileAttributes(path, attributes); + } + public void CreateDirectory(string path) { path = PathTools.Normalize(path); @@ -57,8 +64,7 @@ namespace LibHac.IO if (IsConcatenationFile(parentDir)) throw new IOException("Cannot create files inside of a concatenation file"); BaseFileSystem.CreateDirectory(path); - NxFileAttributes attributes = BaseFileSystem.GetFileAttributes(path) | NxFileAttributes.Archive; - BaseFileSystem.SetFileAttributes(path, attributes); + SetConcatenationFileAttribute(path); long remaining = size; @@ -197,6 +203,13 @@ namespace LibHac.IO BaseFileSystem.Commit(); } + public void QueryEntry(Span outBuffer, Span inBuffer, string path, QueryId queryId) + { + if(queryId != QueryId.MakeConcatFile) throw new NotSupportedException(); + + SetConcatenationFileAttribute(path); + } + private int GetSubFileCount(string dirPath) { int count = 0; diff --git a/src/LibHac/IO/FileSystemExtensions.cs b/src/LibHac/IO/FileSystemExtensions.cs index 2f0fa2f6..bccb6a58 100644 --- a/src/LibHac/IO/FileSystemExtensions.cs +++ b/src/LibHac/IO/FileSystemExtensions.cs @@ -177,6 +177,11 @@ namespace LibHac.IO var nxAttributeBits = (FileAttributes)(((int)nxAttributes & 3) << 4); return attributes | nxAttributeBits; } + + public static void SetConcatenationFileAttribute(this IFileSystem fs, string path) + { + fs.QueryEntry(Span.Empty, Span.Empty, path, QueryId.MakeConcatFile); + } } [Flags] diff --git a/src/LibHac/IO/IFileSystem.cs b/src/LibHac/IO/IFileSystem.cs index 09879fd6..902d9cbd 100644 --- a/src/LibHac/IO/IFileSystem.cs +++ b/src/LibHac/IO/IFileSystem.cs @@ -106,6 +106,8 @@ namespace LibHac.IO /// Does nothing if called on a non-transactional file system. /// void Commit(); + + void QueryEntry(Span outBuffer, Span inBuffer, string path, QueryId queryId); } /// @@ -124,11 +126,16 @@ namespace LibHac.IO /// [Flags] public enum CreateFileOptions - { + { None = 0, /// /// On a , creates a concatenation file. /// CreateConcatenationFile = 1 << 0 } + + public enum QueryId + { + MakeConcatFile = 0 + } } \ No newline at end of file diff --git a/src/LibHac/IO/LayeredFileSystem.cs b/src/LibHac/IO/LayeredFileSystem.cs index 11475caf..9a37781e 100644 --- a/src/LibHac/IO/LayeredFileSystem.cs +++ b/src/LibHac/IO/LayeredFileSystem.cs @@ -97,6 +97,22 @@ namespace LibHac.IO throw new FileNotFoundException(path); } + public void QueryEntry(Span outBuffer, Span inBuffer, string path, QueryId queryId) + { + path = PathTools.Normalize(path); + + foreach (IFileSystem fs in Sources) + { + if (fs.FileExists(path) || fs.DirectoryExists(path)) + { + fs.QueryEntry(outBuffer, inBuffer, path, queryId); + return; + } + } + + 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 84f0ca93..0b9d0b6b 100644 --- a/src/LibHac/IO/LocalFileSystem.cs +++ b/src/LibHac/IO/LocalFileSystem.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; namespace LibHac.IO { @@ -160,5 +161,7 @@ namespace LibHac.IO } public void Commit() { } + + public void QueryEntry(Span outBuffer, Span inBuffer, string path, QueryId queryId) => throw new NotSupportedException(); } } diff --git a/src/LibHac/IO/PartitionFileSystem.cs b/src/LibHac/IO/PartitionFileSystem.cs index 51c5bba2..9cedb78c 100644 --- a/src/LibHac/IO/PartitionFileSystem.cs +++ b/src/LibHac/IO/PartitionFileSystem.cs @@ -82,6 +82,7 @@ namespace LibHac.IO public void RenameDirectory(string srcPath, string dstPath) => throw new NotSupportedException(); public void RenameFile(string srcPath, string dstPath) => throw new NotSupportedException(); public void Commit() { } + public void QueryEntry(Span outBuffer, Span inBuffer, string path, QueryId queryId) => throw new NotSupportedException(); } public enum PartitionFileSystemType diff --git a/src/LibHac/IO/RomFs/RomFsFileSystem.cs b/src/LibHac/IO/RomFs/RomFsFileSystem.cs index 4584db58..8dc2fc82 100644 --- a/src/LibHac/IO/RomFs/RomFsFileSystem.cs +++ b/src/LibHac/IO/RomFs/RomFsFileSystem.cs @@ -89,6 +89,7 @@ namespace LibHac.IO.RomFs 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 void QueryEntry(Span outBuffer, Span inBuffer, string path, QueryId queryId) => throw new NotSupportedException(); } public class RomfsHeader diff --git a/src/LibHac/IO/Save/SaveDataFileSystem.cs b/src/LibHac/IO/Save/SaveDataFileSystem.cs index 81543067..6c645918 100644 --- a/src/LibHac/IO/Save/SaveDataFileSystem.cs +++ b/src/LibHac/IO/Save/SaveDataFileSystem.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; namespace LibHac.IO.Save @@ -194,6 +195,8 @@ namespace LibHac.IO.Save Commit(Keyset); } + public void QueryEntry(Span outBuffer, Span inBuffer, string path, QueryId queryId) => throw new NotSupportedException(); + public bool Commit(Keyset keyset) { CoreDataIvfcStorage.Flush(); diff --git a/src/LibHac/IO/Save/SaveDataFileSystemCore.cs b/src/LibHac/IO/Save/SaveDataFileSystemCore.cs index 6666315d..1e777e32 100644 --- a/src/LibHac/IO/Save/SaveDataFileSystemCore.cs +++ b/src/LibHac/IO/Save/SaveDataFileSystemCore.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; namespace LibHac.IO.Save { @@ -28,7 +29,7 @@ namespace LibHac.IO.Save public void CreateDirectory(string path) { - throw new System.NotImplementedException(); + throw new NotImplementedException(); } public void CreateFile(string path, long size, CreateFileOptions options) @@ -45,7 +46,7 @@ namespace LibHac.IO.Save public void DeleteDirectory(string path) { - throw new System.NotImplementedException(); + throw new NotImplementedException(); } public void DeleteFile(string path) @@ -93,12 +94,12 @@ namespace LibHac.IO.Save public void RenameDirectory(string srcPath, string dstPath) { - throw new System.NotImplementedException(); + throw new NotImplementedException(); } public void RenameFile(string srcPath, string dstPath) { - throw new System.NotImplementedException(); + throw new NotImplementedException(); } public bool DirectoryExists(string path) @@ -130,6 +131,8 @@ namespace LibHac.IO.Save } + public void QueryEntry(Span outBuffer, Span inBuffer, string path, QueryId queryId) => throw new NotSupportedException(); + public IStorage GetBaseStorage() => BaseStorage.AsReadOnly(); public IStorage GetHeaderStorage() => HeaderStorage.AsReadOnly(); diff --git a/src/LibHac/IO/SubdirectoryFileSystem.cs b/src/LibHac/IO/SubdirectoryFileSystem.cs index 64d89f85..2ec9c141 100644 --- a/src/LibHac/IO/SubdirectoryFileSystem.cs +++ b/src/LibHac/IO/SubdirectoryFileSystem.cs @@ -1,4 +1,6 @@ -namespace LibHac.IO +using System; + +namespace LibHac.IO { public class SubdirectoryFileSystem : IFileSystem { @@ -102,5 +104,10 @@ { ParentFileSystem.Commit(); } + + public void QueryEntry(Span outBuffer, Span inBuffer, string path, QueryId queryId) + { + ParentFileSystem.QueryEntry(outBuffer, inBuffer, ResolveFullPath(path), queryId); + } } }