From 489ad0fb8a946498609b29037f73f4efe63cb8c5 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Fri, 20 Aug 2021 15:57:19 -0700 Subject: [PATCH] Properly catch exceptions in LocalFileSystem.CleanDirectoryRecursively --- src/LibHac/FsSystem/LocalFileSystem.cs | 57 +++++++++++++------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/LibHac/FsSystem/LocalFileSystem.cs b/src/LibHac/FsSystem/LocalFileSystem.cs index 726ff612..22282b02 100644 --- a/src/LibHac/FsSystem/LocalFileSystem.cs +++ b/src/LibHac/FsSystem/LocalFileSystem.cs @@ -309,35 +309,10 @@ namespace LibHac.FsSystem Result rc = ResolveFullPath(out string fullPath, path, true); if (rc.IsFailure()) return rc; - foreach (string file in Directory.EnumerateFiles(fullPath)) - { - rc = TargetLockedAvoidance.RetryToAvoidTargetLocked( - () => - { - rc = GetFileInfo(out FileInfo fileInfo, file); - if (rc.IsFailure()) return rc; + rc = GetDirInfo(out DirectoryInfo dir, fullPath); + if (rc.IsFailure()) return rc; - return DeleteFileInternal(fileInfo); - }, _fsClient); - - if (rc.IsFailure()) return rc; - } - - foreach (string dir in Directory.EnumerateDirectories(fullPath)) - { - rc = TargetLockedAvoidance.RetryToAvoidTargetLocked( - () => - { - rc = GetDirInfo(out DirectoryInfo dirInfo, dir); - if (rc.IsFailure()) return rc; - - return DeleteDirectoryInternal(dirInfo, true); - }, _fsClient); - - if (rc.IsFailure()) return rc; - } - - return Result.Success; + return CleanDirectoryInternal(dir, _fsClient); } protected override Result DoDeleteFile(U8Span path) @@ -623,6 +598,32 @@ namespace LibHac.FsSystem return Result.Success; } + private static Result CleanDirectoryInternal(DirectoryInfo dir, FileSystemClient fsClient) + { + try + { + foreach (FileInfo fileInfo in dir.EnumerateFiles()) + { + Result rc = TargetLockedAvoidance.RetryToAvoidTargetLocked(() => DeleteFileInternal(fileInfo), + fsClient); + if (rc.IsFailure()) return rc; + } + + foreach (DirectoryInfo dirInfo in dir.EnumerateDirectories()) + { + Result rc = TargetLockedAvoidance.RetryToAvoidTargetLocked(() => DeleteDirectoryInternal(dirInfo, true), + fsClient); + if (rc.IsFailure()) return rc; + } + } + catch (Exception ex) when (ex.HResult < 0) + { + return HResult.HResultToHorizonResult(ex.HResult).Log(); + } + + return Result.Success; + } + private static Result DeleteDirectoryInternal(DirectoryInfo dir, bool recursive) { if (!dir.Exists)