| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | using MediaBrowser.Model.IO; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | |
| | | 8 | | namespace MediaBrowser.Controller.IO; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Helper methods for file system management. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class FileSystemHelper |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Deletes the file. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="fileSystem">The fileSystem.</param> |
| | | 19 | | /// <param name="path">The path.</param> |
| | | 20 | | /// <param name="logger">The logger.</param> |
| | | 21 | | public static void DeleteFile(IFileSystem fileSystem, string path, ILogger logger) |
| | | 22 | | { |
| | | 23 | | try |
| | | 24 | | { |
| | 0 | 25 | | fileSystem.DeleteFile(path); |
| | 0 | 26 | | } |
| | 0 | 27 | | catch (UnauthorizedAccessException ex) |
| | | 28 | | { |
| | 0 | 29 | | logger.LogError(ex, "Error deleting file {Path}", path); |
| | 0 | 30 | | } |
| | 0 | 31 | | catch (IOException ex) |
| | | 32 | | { |
| | 0 | 33 | | logger.LogError(ex, "Error deleting file {Path}", path); |
| | 0 | 34 | | } |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Recursively delete empty folders. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="fileSystem">The fileSystem.</param> |
| | | 41 | | /// <param name="path">The path.</param> |
| | | 42 | | /// <param name="logger">The logger.</param> |
| | | 43 | | public static void DeleteEmptyFolders(IFileSystem fileSystem, string path, ILogger logger) |
| | | 44 | | { |
| | 4 | 45 | | foreach (var directory in fileSystem.GetDirectoryPaths(path)) |
| | | 46 | | { |
| | 0 | 47 | | DeleteEmptyFolders(fileSystem, directory, logger); |
| | 0 | 48 | | if (!fileSystem.GetFileSystemEntryPaths(directory).Any()) |
| | | 49 | | { |
| | | 50 | | try |
| | | 51 | | { |
| | 0 | 52 | | Directory.Delete(directory, false); |
| | 0 | 53 | | } |
| | 0 | 54 | | catch (UnauthorizedAccessException ex) |
| | | 55 | | { |
| | 0 | 56 | | logger.LogError(ex, "Error deleting directory {Path}", directory); |
| | 0 | 57 | | } |
| | 0 | 58 | | catch (IOException ex) |
| | | 59 | | { |
| | 0 | 60 | | logger.LogError(ex, "Error deleting directory {Path}", directory); |
| | 0 | 61 | | } |
| | | 62 | | } |
| | | 63 | | } |
| | 2 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets the target of the specified file link. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <remarks> |
| | | 70 | | /// This helper exists because of this upstream runtime issue; https://github.com/dotnet/runtime/issues/92128. |
| | | 71 | | /// </remarks> |
| | | 72 | | /// <param name="linkPath">The path of the file link.</param> |
| | | 73 | | /// <param name="returnFinalTarget">true to follow links to the final target; false to return the immediate next lin |
| | | 74 | | /// <returns> |
| | | 75 | | /// A <see cref="FileInfo"/> if the <paramref name="linkPath"/> is a link, regardless of if the target exists; other |
| | | 76 | | /// </returns> |
| | | 77 | | public static FileInfo? ResolveLinkTarget(string linkPath, bool returnFinalTarget = false) |
| | | 78 | | { |
| | | 79 | | // Check if the file exists so the native resolve handler won't throw at us. |
| | 1 | 80 | | if (!File.Exists(linkPath)) |
| | | 81 | | { |
| | 0 | 82 | | return null; |
| | | 83 | | } |
| | | 84 | | |
| | 1 | 85 | | if (!returnFinalTarget) |
| | | 86 | | { |
| | 0 | 87 | | return File.ResolveLinkTarget(linkPath, returnFinalTarget: false) as FileInfo; |
| | | 88 | | } |
| | | 89 | | |
| | 1 | 90 | | if (File.ResolveLinkTarget(linkPath, returnFinalTarget: false) is not FileInfo targetInfo) |
| | | 91 | | { |
| | 0 | 92 | | return null; |
| | | 93 | | } |
| | | 94 | | |
| | 1 | 95 | | if (!targetInfo.Exists) |
| | | 96 | | { |
| | 1 | 97 | | return targetInfo; |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | var currentPath = targetInfo.FullName; |
| | 0 | 101 | | var visited = new HashSet<string>(StringComparer.Ordinal) { linkPath, currentPath }; |
| | 0 | 102 | | while (File.ResolveLinkTarget(currentPath, returnFinalTarget: false) is FileInfo linkInfo) |
| | | 103 | | { |
| | 0 | 104 | | var targetPath = linkInfo.FullName; |
| | | 105 | | |
| | | 106 | | // If an infinite loop is detected, return the file info for the |
| | | 107 | | // first link in the loop we encountered. |
| | 0 | 108 | | if (!visited.Add(targetPath)) |
| | | 109 | | { |
| | 0 | 110 | | return new FileInfo(targetPath); |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | targetInfo = linkInfo; |
| | 0 | 114 | | currentPath = targetPath; |
| | | 115 | | |
| | | 116 | | // Exit if the target doesn't exist, so the native resolve handler won't throw at us. |
| | 0 | 117 | | if (!targetInfo.Exists) |
| | | 118 | | { |
| | | 119 | | break; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | return targetInfo; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Gets the target of the specified file link. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <remarks> |
| | | 130 | | /// This helper exists because of this upstream runtime issue; https://github.com/dotnet/runtime/issues/92128. |
| | | 131 | | /// </remarks> |
| | | 132 | | /// <param name="fileInfo">The file info of the file link.</param> |
| | | 133 | | /// <param name="returnFinalTarget">true to follow links to the final target; false to return the immediate next lin |
| | | 134 | | /// <returns> |
| | | 135 | | /// A <see cref="FileInfo"/> if the <paramref name="fileInfo"/> is a link, regardless of if the target exists; other |
| | | 136 | | /// </returns> |
| | | 137 | | public static FileInfo? ResolveLinkTarget(FileInfo fileInfo, bool returnFinalTarget = false) |
| | | 138 | | { |
| | 1 | 139 | | ArgumentNullException.ThrowIfNull(fileInfo); |
| | | 140 | | |
| | 1 | 141 | | return ResolveLinkTarget(fileInfo.FullName, returnFinalTarget); |
| | | 142 | | } |
| | | 143 | | } |