Add DeleteDirectoryRecursively and CleanDirectoryRecursively

This commit is contained in:
Alex Barney 2019-05-08 15:35:09 -05:00
parent 6590cad251
commit 3c45e7e71b
12 changed files with 144 additions and 5 deletions

View file

@ -20,6 +20,28 @@ namespace LibHac.Nand
Fs.DeleteDirectory(path); Fs.DeleteDirectory(path);
} }
public void DeleteDirectoryRecursively(string path)
{
path = ToDiscUtilsPath(PathTools.Normalize(path));
Fs.DeleteDirectory(path, true);
}
public void CleanDirectoryRecursively(string path)
{
path = ToDiscUtilsPath(PathTools.Normalize(path));
foreach (string file in Fs.GetFiles(path))
{
Fs.DeleteFile(file);
}
foreach (string file in Fs.GetDirectories(path))
{
Fs.DeleteDirectory(file, true);
}
}
public void DeleteFile(string path) public void DeleteFile(string path)
{ {
path = ToDiscUtilsPath(PathTools.Normalize(path)); path = ToDiscUtilsPath(PathTools.Normalize(path));

View file

@ -64,6 +64,16 @@ namespace LibHac.IO
BaseFileSystem.DeleteDirectory(path); BaseFileSystem.DeleteDirectory(path);
} }
public void DeleteDirectoryRecursively(string path)
{
BaseFileSystem.DeleteDirectoryRecursively(path);
}
public void CleanDirectoryRecursively(string path)
{
BaseFileSystem.CleanDirectoryRecursively(path);
}
public void DeleteFile(string path) public void DeleteFile(string path)
{ {
BaseFileSystem.DeleteFile(path); BaseFileSystem.DeleteFile(path);

View file

@ -91,6 +91,24 @@ namespace LibHac.IO
BaseFileSystem.DeleteDirectory(path); BaseFileSystem.DeleteDirectory(path);
} }
public void DeleteDirectoryRecursively(string path)
{
path = PathTools.Normalize(path);
if (IsConcatenationFile(path)) throw new DirectoryNotFoundException();
BaseFileSystem.DeleteDirectoryRecursively(path);
}
public void CleanDirectoryRecursively(string path)
{
path = PathTools.Normalize(path);
if (IsConcatenationFile(path)) throw new DirectoryNotFoundException();
BaseFileSystem.CleanDirectoryRecursively(path);
}
public void DeleteFile(string path) public void DeleteFile(string path)
{ {
path = PathTools.Normalize(path); path = PathTools.Normalize(path);
@ -220,7 +238,7 @@ namespace LibHac.IO
public void QueryEntry(Span<byte> outBuffer, ReadOnlySpan<byte> inBuffer, string path, QueryId queryId) public void QueryEntry(Span<byte> outBuffer, ReadOnlySpan<byte> inBuffer, string path, QueryId queryId)
{ {
if(queryId != QueryId.MakeConcatFile) throw new NotSupportedException(); if (queryId != QueryId.MakeConcatFile) throw new NotSupportedException();
SetConcatenationFileAttribute(path); SetConcatenationFileAttribute(path);
} }

View file

@ -182,6 +182,27 @@ namespace LibHac.IO
{ {
fs.QueryEntry(Span<byte>.Empty, Span<byte>.Empty, path, QueryId.MakeConcatFile); fs.QueryEntry(Span<byte>.Empty, Span<byte>.Empty, path, QueryId.MakeConcatFile);
} }
public static void CleanDirectoryRecursivelyGeneric(IDirectory directory)
{
IFileSystem fs = directory.ParentFileSystem;
foreach (DirectoryEntry entry in directory.Read())
{
if (entry.Type == DirectoryEntryType.Directory)
{
string subPath = directory.FullPath + '/' + entry.Name;
IDirectory subDir = fs.OpenDirectory(subPath, OpenDirectoryMode.All);
CleanDirectoryRecursivelyGeneric(subDir);
fs.DeleteDirectory(subPath);
}
else if (entry.Type == DirectoryEntryType.File)
{
fs.DeleteFile(directory.FullPath + '/' + entry.Name);
}
}
}
} }
[Flags] [Flags]

View file

@ -33,9 +33,9 @@ namespace LibHac.IO
/// <exception cref="IOException">An I/O error occurred while deleting the directory.</exception> /// <exception cref="IOException">An I/O error occurred while deleting the directory.</exception>
void DeleteDirectory(string path); void DeleteDirectory(string path);
//void DeleteDirectoryRecursively(string path); void DeleteDirectoryRecursively(string path);
//void CleanDirectoryRecursively(string path); void CleanDirectoryRecursively(string path);
/// <summary> /// <summary>
/// Deletes the specified file. /// Deletes the specified file.

View file

@ -133,6 +133,8 @@ namespace LibHac.IO
public void CreateDirectory(string path) => throw new NotSupportedException(); public void CreateDirectory(string path) => throw new NotSupportedException();
public void CreateFile(string path, long size, CreateFileOptions options) => throw new NotSupportedException(); public void CreateFile(string path, long size, CreateFileOptions options) => throw new NotSupportedException();
public void DeleteDirectory(string path) => throw new NotSupportedException(); public void DeleteDirectory(string path) => throw new NotSupportedException();
public void DeleteDirectoryRecursively(string path) => throw new NotSupportedException();
public void CleanDirectoryRecursively(string path) => throw new NotSupportedException();
public void DeleteFile(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 RenameDirectory(string srcPath, string dstPath) => throw new NotSupportedException();
public void RenameFile(string srcPath, string dstPath) => throw new NotSupportedException(); public void RenameFile(string srcPath, string dstPath) => throw new NotSupportedException();

View file

@ -75,8 +75,30 @@ namespace LibHac.IO
{ {
path = PathTools.Normalize(path); path = PathTools.Normalize(path);
string resolveLocalPath = ResolveLocalPath(path); Directory.Delete(ResolveLocalPath(path));
Directory.Delete(resolveLocalPath); }
public void DeleteDirectoryRecursively(string path)
{
path = PathTools.Normalize(path);
Directory.Delete(ResolveLocalPath(path), true);
}
public void CleanDirectoryRecursively(string path)
{
path = PathTools.Normalize(path);
string localPath = ResolveLocalPath(path);
foreach (string file in Directory.EnumerateFiles(localPath))
{
File.Delete(file);
}
foreach (string dir in Directory.EnumerateDirectories(localPath))
{
Directory.Delete(dir, true);
}
} }
public void DeleteFile(string path) public void DeleteFile(string path)

View file

@ -78,6 +78,8 @@ namespace LibHac.IO
public void CreateDirectory(string path) => throw new NotSupportedException(); public void CreateDirectory(string path) => throw new NotSupportedException();
public void CreateFile(string path, long size, CreateFileOptions options) => throw new NotSupportedException(); public void CreateFile(string path, long size, CreateFileOptions options) => throw new NotSupportedException();
public void DeleteDirectory(string path) => throw new NotSupportedException(); public void DeleteDirectory(string path) => throw new NotSupportedException();
public void DeleteDirectoryRecursively(string path) => throw new NotSupportedException();
public void CleanDirectoryRecursively(string path) => throw new NotSupportedException();
public void DeleteFile(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 RenameDirectory(string srcPath, string dstPath) => throw new NotSupportedException();
public void RenameFile(string srcPath, string dstPath) => throw new NotSupportedException(); public void RenameFile(string srcPath, string dstPath) => throw new NotSupportedException();

View file

@ -86,6 +86,8 @@ namespace LibHac.IO.RomFs
public void CreateDirectory(string path) => throw new NotSupportedException(); public void CreateDirectory(string path) => throw new NotSupportedException();
public void CreateFile(string path, long size, CreateFileOptions options) => throw new NotSupportedException(); public void CreateFile(string path, long size, CreateFileOptions options) => throw new NotSupportedException();
public void DeleteDirectory(string path) => throw new NotSupportedException(); public void DeleteDirectory(string path) => throw new NotSupportedException();
public void DeleteDirectoryRecursively(string path) => throw new NotSupportedException();
public void CleanDirectoryRecursively(string path) => throw new NotSupportedException();
public void DeleteFile(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 RenameDirectory(string srcPath, string dstPath) => throw new NotSupportedException();
public void RenameFile(string srcPath, string dstPath) => throw new NotSupportedException(); public void RenameFile(string srcPath, string dstPath) => throw new NotSupportedException();

View file

@ -157,6 +157,16 @@ namespace LibHac.IO.Save
SaveDataFileSystemCore.DeleteDirectory(path); SaveDataFileSystemCore.DeleteDirectory(path);
} }
public void DeleteDirectoryRecursively(string path)
{
SaveDataFileSystemCore.DeleteDirectoryRecursively(path);
}
public void CleanDirectoryRecursively(string path)
{
SaveDataFileSystemCore.CleanDirectoryRecursively(path);
}
public void DeleteFile(string path) public void DeleteFile(string path)
{ {
SaveDataFileSystemCore.DeleteFile(path); SaveDataFileSystemCore.DeleteFile(path);

View file

@ -49,6 +49,22 @@ namespace LibHac.IO.Save
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void DeleteDirectoryRecursively(string path)
{
path = PathTools.Normalize(path);
CleanDirectoryRecursively(path);
DeleteDirectory(path);
}
public void CleanDirectoryRecursively(string path)
{
path = PathTools.Normalize(path);
IDirectory dir = OpenDirectory(path, OpenDirectoryMode.All);
FileSystemExtensions.CleanDirectoryRecursivelyGeneric(dir);
}
public void DeleteFile(string path) public void DeleteFile(string path)
{ {
path = PathTools.Normalize(path); path = PathTools.Normalize(path);

View file

@ -40,6 +40,20 @@ namespace LibHac.IO
ParentFileSystem.DeleteDirectory(ResolveFullPath(path)); ParentFileSystem.DeleteDirectory(ResolveFullPath(path));
} }
public void DeleteDirectoryRecursively(string path)
{
path = PathTools.Normalize(path);
ParentFileSystem.DeleteDirectoryRecursively(ResolveFullPath(path));
}
public void CleanDirectoryRecursively(string path)
{
path = PathTools.Normalize(path);
ParentFileSystem.CleanDirectoryRecursively(ResolveFullPath(path));
}
public void DeleteFile(string path) public void DeleteFile(string path)
{ {
path = PathTools.Normalize(path); path = PathTools.Normalize(path);