| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Security; |
| | | 7 | | using Jellyfin.Extensions; |
| | | 8 | | using MediaBrowser.Common.Configuration; |
| | | 9 | | using MediaBrowser.Model.IO; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace Emby.Server.Implementations.IO |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Class ManagedFileSystem. |
| | | 16 | | /// </summary> |
| | | 17 | | public class ManagedFileSystem : IFileSystem |
| | | 18 | | { |
| | 2 | 19 | | private static readonly bool _isEnvironmentCaseInsensitive = OperatingSystem.IsWindows(); |
| | 2 | 20 | | private static readonly char[] _invalidPathCharacters = |
| | 2 | 21 | | { |
| | 2 | 22 | | '\"', '<', '>', '|', '\0', |
| | 2 | 23 | | (char)1, (char)2, (char)3, (char)4, (char)5, (char)6, (char)7, (char)8, (char)9, (char)10, |
| | 2 | 24 | | (char)11, (char)12, (char)13, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19, (char)20, |
| | 2 | 25 | | (char)21, (char)22, (char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29, (char)30, |
| | 2 | 26 | | (char)31, ':', '*', '?', '\\', '/' |
| | 2 | 27 | | }; |
| | | 28 | | |
| | | 29 | | private readonly ILogger<ManagedFileSystem> _logger; |
| | | 30 | | private readonly List<IShortcutHandler> _shortcutHandlers; |
| | | 31 | | private readonly string _tempPath; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="ManagedFileSystem"/> class. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="logger">The <see cref="ILogger"/> instance to use.</param> |
| | | 37 | | /// <param name="applicationPaths">The <see cref="IApplicationPaths"/> instance to use.</param> |
| | | 38 | | /// <param name="shortcutHandlers">the <see cref="IShortcutHandler"/>'s to use.</param> |
| | | 39 | | public ManagedFileSystem( |
| | | 40 | | ILogger<ManagedFileSystem> logger, |
| | | 41 | | IApplicationPaths applicationPaths, |
| | | 42 | | IEnumerable<IShortcutHandler> shortcutHandlers) |
| | | 43 | | { |
| | 42 | 44 | | _logger = logger; |
| | 42 | 45 | | _tempPath = applicationPaths.TempDirectory; |
| | 42 | 46 | | _shortcutHandlers = shortcutHandlers.ToList(); |
| | 42 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Determines whether the specified filename is shortcut. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="filename">The filename.</param> |
| | | 53 | | /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns> |
| | | 54 | | /// <exception cref="ArgumentNullException"><paramref name="filename"/> is <c>null</c>.</exception> |
| | | 55 | | public virtual bool IsShortcut(string filename) |
| | | 56 | | { |
| | 148 | 57 | | ArgumentException.ThrowIfNullOrEmpty(filename); |
| | | 58 | | |
| | 148 | 59 | | var extension = Path.GetExtension(filename); |
| | 148 | 60 | | return _shortcutHandlers.Any(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase)) |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Resolves the shortcut. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="filename">The filename.</param> |
| | | 67 | | /// <returns>System.String.</returns> |
| | | 68 | | /// <exception cref="ArgumentNullException"><paramref name="filename"/> is <c>null</c>.</exception> |
| | | 69 | | public virtual string? ResolveShortcut(string filename) |
| | | 70 | | { |
| | 0 | 71 | | ArgumentException.ThrowIfNullOrEmpty(filename); |
| | | 72 | | |
| | 0 | 73 | | var extension = Path.GetExtension(filename); |
| | 0 | 74 | | var handler = _shortcutHandlers.Find(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgno |
| | | 75 | | |
| | 0 | 76 | | return handler?.Resolve(filename); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <inheritdoc /> |
| | | 80 | | public virtual string MakeAbsolutePath(string folderPath, string filePath) |
| | | 81 | | { |
| | | 82 | | // path is actually a stream |
| | 4 | 83 | | if (string.IsNullOrWhiteSpace(filePath)) |
| | | 84 | | { |
| | 0 | 85 | | return filePath; |
| | | 86 | | } |
| | | 87 | | |
| | 4 | 88 | | var isAbsolutePath = Path.IsPathRooted(filePath) && (!OperatingSystem.IsWindows() || filePath[0] != '\\'); |
| | | 89 | | |
| | 4 | 90 | | if (isAbsolutePath) |
| | | 91 | | { |
| | | 92 | | // absolute local path |
| | 1 | 93 | | return filePath; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | // unc path |
| | 3 | 97 | | if (filePath.StartsWith(@"\\", StringComparison.Ordinal)) |
| | | 98 | | { |
| | 0 | 99 | | return filePath; |
| | | 100 | | } |
| | | 101 | | |
| | 3 | 102 | | var filePathSpan = filePath.AsSpan(); |
| | | 103 | | |
| | | 104 | | // relative path on windows |
| | 3 | 105 | | if (filePath[0] == '\\') |
| | | 106 | | { |
| | 0 | 107 | | filePathSpan = filePathSpan.Slice(1); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | try |
| | | 111 | | { |
| | 3 | 112 | | return Path.GetFullPath(Path.Join(folderPath, filePathSpan)); |
| | | 113 | | } |
| | 0 | 114 | | catch (ArgumentException) |
| | | 115 | | { |
| | 0 | 116 | | return filePath; |
| | | 117 | | } |
| | 0 | 118 | | catch (PathTooLongException) |
| | | 119 | | { |
| | 0 | 120 | | return filePath; |
| | | 121 | | } |
| | 0 | 122 | | catch (NotSupportedException) |
| | | 123 | | { |
| | 0 | 124 | | return filePath; |
| | | 125 | | } |
| | 3 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Creates the shortcut. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="shortcutPath">The shortcut path.</param> |
| | | 132 | | /// <param name="target">The target.</param> |
| | | 133 | | /// <exception cref="ArgumentNullException">The shortcutPath or target is null.</exception> |
| | | 134 | | public virtual void CreateShortcut(string shortcutPath, string target) |
| | | 135 | | { |
| | 0 | 136 | | ArgumentException.ThrowIfNullOrEmpty(shortcutPath); |
| | 0 | 137 | | ArgumentException.ThrowIfNullOrEmpty(target); |
| | | 138 | | |
| | 0 | 139 | | var extension = Path.GetExtension(shortcutPath); |
| | 0 | 140 | | var handler = _shortcutHandlers.Find(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgno |
| | | 141 | | |
| | 0 | 142 | | if (handler is not null) |
| | | 143 | | { |
| | 0 | 144 | | handler.Create(shortcutPath, target); |
| | | 145 | | } |
| | | 146 | | else |
| | | 147 | | { |
| | 0 | 148 | | throw new NotImplementedException(); |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | /// <inheritdoc /> |
| | | 153 | | public void MoveDirectory(string source, string destination) |
| | | 154 | | { |
| | | 155 | | // Make sure parent directory of target exists |
| | 2 | 156 | | var parent = Directory.GetParent(destination); |
| | 2 | 157 | | parent?.Create(); |
| | | 158 | | |
| | | 159 | | try |
| | | 160 | | { |
| | 2 | 161 | | Directory.Move(source, destination); |
| | 1 | 162 | | } |
| | 1 | 163 | | catch (IOException) |
| | | 164 | | { |
| | | 165 | | // Cross device move requires a copy |
| | 1 | 166 | | Directory.CreateDirectory(destination); |
| | 1 | 167 | | var sourceDir = new DirectoryInfo(source); |
| | 6 | 168 | | foreach (var file in sourceDir.EnumerateFiles()) |
| | | 169 | | { |
| | 2 | 170 | | file.CopyTo(Path.Combine(destination, file.Name), true); |
| | | 171 | | } |
| | | 172 | | |
| | 1 | 173 | | sourceDir.Delete(true); |
| | 1 | 174 | | } |
| | 2 | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Returns a <see cref="FileSystemMetadata"/> object for the specified file or directory path. |
| | | 179 | | /// </summary> |
| | | 180 | | /// <param name="path">A path to a file or directory.</param> |
| | | 181 | | /// <returns>A <see cref="FileSystemMetadata"/> object.</returns> |
| | | 182 | | /// <remarks>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> object's |
| | | 183 | | /// <see cref="FileSystemMetadata.IsDirectory"/> property will be set to true and all other properties will refl |
| | | 184 | | public virtual FileSystemMetadata GetFileSystemInfo(string path) |
| | | 185 | | { |
| | | 186 | | // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists |
| | 89 | 187 | | if (Path.HasExtension(path)) |
| | | 188 | | { |
| | 0 | 189 | | var fileInfo = new FileInfo(path); |
| | | 190 | | |
| | 0 | 191 | | if (fileInfo.Exists) |
| | | 192 | | { |
| | 0 | 193 | | return GetFileSystemMetadata(fileInfo); |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | return GetFileSystemMetadata(new DirectoryInfo(path)); |
| | | 197 | | } |
| | | 198 | | else |
| | | 199 | | { |
| | 89 | 200 | | var fileInfo = new DirectoryInfo(path); |
| | | 201 | | |
| | 89 | 202 | | if (fileInfo.Exists) |
| | | 203 | | { |
| | 89 | 204 | | return GetFileSystemMetadata(fileInfo); |
| | | 205 | | } |
| | | 206 | | |
| | 0 | 207 | | return GetFileSystemMetadata(new FileInfo(path)); |
| | | 208 | | } |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | /// <summary> |
| | | 212 | | /// Returns a <see cref="FileSystemMetadata"/> object for the specified file path. |
| | | 213 | | /// </summary> |
| | | 214 | | /// <param name="path">A path to a file.</param> |
| | | 215 | | /// <returns>A <see cref="FileSystemMetadata"/> object.</returns> |
| | | 216 | | /// <remarks><para>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> ob |
| | | 217 | | /// <see cref="FileSystemMetadata.IsDirectory"/> property and the <see cref="FileSystemMetadata.Exists"/> proper |
| | | 218 | | /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></r |
| | | 219 | | public virtual FileSystemMetadata GetFileInfo(string path) |
| | | 220 | | { |
| | 1 | 221 | | var fileInfo = new FileInfo(path); |
| | | 222 | | |
| | 1 | 223 | | return GetFileSystemMetadata(fileInfo); |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Returns a <see cref="FileSystemMetadata"/> object for the specified directory path. |
| | | 228 | | /// </summary> |
| | | 229 | | /// <param name="path">A path to a directory.</param> |
| | | 230 | | /// <returns>A <see cref="FileSystemMetadata"/> object.</returns> |
| | | 231 | | /// <remarks><para>If the specified path points to a file, the returned <see cref="FileSystemMetadata"/> object' |
| | | 232 | | /// <see cref="FileSystemMetadata.IsDirectory"/> property will be set to true and the <see cref="FileSystemMetad |
| | | 233 | | /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></r |
| | | 234 | | public virtual FileSystemMetadata GetDirectoryInfo(string path) |
| | | 235 | | { |
| | 128 | 236 | | var fileInfo = new DirectoryInfo(path); |
| | | 237 | | |
| | 128 | 238 | | return GetFileSystemMetadata(fileInfo); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | private FileSystemMetadata GetFileSystemMetadata(FileSystemInfo info) |
| | | 242 | | { |
| | 398 | 243 | | var result = new FileSystemMetadata |
| | 398 | 244 | | { |
| | 398 | 245 | | Exists = info.Exists, |
| | 398 | 246 | | FullName = info.FullName, |
| | 398 | 247 | | Extension = info.Extension, |
| | 398 | 248 | | Name = info.Name |
| | 398 | 249 | | }; |
| | | 250 | | |
| | 398 | 251 | | if (result.Exists) |
| | | 252 | | { |
| | 396 | 253 | | result.IsDirectory = info is DirectoryInfo || (info.Attributes & FileAttributes.Directory) == FileAttrib |
| | | 254 | | |
| | 396 | 255 | | if (info is FileInfo fileInfo) |
| | | 256 | | { |
| | 45 | 257 | | result.CreationTimeUtc = GetCreationTimeUtc(info); |
| | 45 | 258 | | result.LastWriteTimeUtc = GetLastWriteTimeUtc(info); |
| | 45 | 259 | | if (fileInfo.LinkTarget is not null) |
| | | 260 | | { |
| | | 261 | | try |
| | | 262 | | { |
| | 1 | 263 | | var targetFileInfo = (FileInfo?)fileInfo.ResolveLinkTarget(returnFinalTarget: true); |
| | 1 | 264 | | if (targetFileInfo is not null) |
| | | 265 | | { |
| | 1 | 266 | | result.Exists = targetFileInfo.Exists; |
| | 1 | 267 | | if (result.Exists) |
| | | 268 | | { |
| | 0 | 269 | | result.Length = targetFileInfo.Length; |
| | 0 | 270 | | result.CreationTimeUtc = GetCreationTimeUtc(targetFileInfo); |
| | 0 | 271 | | result.LastWriteTimeUtc = GetLastWriteTimeUtc(targetFileInfo); |
| | | 272 | | } |
| | | 273 | | } |
| | | 274 | | else |
| | | 275 | | { |
| | 0 | 276 | | result.Exists = false; |
| | | 277 | | } |
| | 1 | 278 | | } |
| | 0 | 279 | | catch (UnauthorizedAccessException ex) |
| | | 280 | | { |
| | 0 | 281 | | _logger.LogError(ex, "Reading the file at {Path} failed due to a permissions exception.", fi |
| | 0 | 282 | | } |
| | | 283 | | } |
| | | 284 | | else |
| | | 285 | | { |
| | 44 | 286 | | result.Length = fileInfo.Length; |
| | | 287 | | } |
| | | 288 | | } |
| | | 289 | | } |
| | | 290 | | else |
| | | 291 | | { |
| | 2 | 292 | | result.IsDirectory = info is DirectoryInfo; |
| | | 293 | | } |
| | | 294 | | |
| | 398 | 295 | | return result; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// Takes a filename and removes invalid characters. |
| | | 300 | | /// </summary> |
| | | 301 | | /// <param name="filename">The filename.</param> |
| | | 302 | | /// <returns>System.String.</returns> |
| | | 303 | | /// <exception cref="ArgumentNullException">The filename is null.</exception> |
| | | 304 | | public string GetValidFilename(string filename) |
| | | 305 | | { |
| | 7 | 306 | | var first = filename.IndexOfAny(_invalidPathCharacters); |
| | 7 | 307 | | if (first == -1) |
| | | 308 | | { |
| | | 309 | | // Fast path for clean strings |
| | 4 | 310 | | return filename; |
| | | 311 | | } |
| | | 312 | | |
| | 3 | 313 | | return string.Create( |
| | 3 | 314 | | filename.Length, |
| | 3 | 315 | | (filename, _invalidPathCharacters, first), |
| | 3 | 316 | | (chars, state) => |
| | 3 | 317 | | { |
| | 3 | 318 | | state.filename.AsSpan().CopyTo(chars); |
| | 3 | 319 | | |
| | 3 | 320 | | chars[state.first++] = ' '; |
| | 3 | 321 | | |
| | 3 | 322 | | var len = chars.Length; |
| | 3 | 323 | | foreach (var c in state._invalidPathCharacters) |
| | 3 | 324 | | { |
| | 3 | 325 | | for (int i = state.first; i < len; i++) |
| | 3 | 326 | | { |
| | 3 | 327 | | if (chars[i] == c) |
| | 3 | 328 | | { |
| | 3 | 329 | | chars[i] = ' '; |
| | 3 | 330 | | } |
| | 3 | 331 | | } |
| | 3 | 332 | | } |
| | 3 | 333 | | }); |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Gets the creation time UTC. |
| | | 338 | | /// </summary> |
| | | 339 | | /// <param name="info">The info.</param> |
| | | 340 | | /// <returns>DateTime.</returns> |
| | | 341 | | public DateTime GetCreationTimeUtc(FileSystemInfo info) |
| | | 342 | | { |
| | | 343 | | // This could throw an error on some file systems that have dates out of range |
| | | 344 | | try |
| | | 345 | | { |
| | 45 | 346 | | return info.CreationTimeUtc; |
| | | 347 | | } |
| | 0 | 348 | | catch (Exception ex) |
| | | 349 | | { |
| | 0 | 350 | | _logger.LogError(ex, "Error determining CreationTimeUtc for {FullName}", info.FullName); |
| | 0 | 351 | | return DateTime.MinValue; |
| | | 352 | | } |
| | 45 | 353 | | } |
| | | 354 | | |
| | | 355 | | /// <inheritdoc /> |
| | | 356 | | public virtual DateTime GetCreationTimeUtc(string path) |
| | | 357 | | { |
| | 0 | 358 | | return GetCreationTimeUtc(GetFileSystemInfo(path)); |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | /// <inheritdoc /> |
| | | 362 | | public virtual DateTime GetCreationTimeUtc(FileSystemMetadata info) |
| | | 363 | | { |
| | 0 | 364 | | return info.CreationTimeUtc; |
| | | 365 | | } |
| | | 366 | | |
| | | 367 | | /// <inheritdoc /> |
| | | 368 | | public virtual DateTime GetLastWriteTimeUtc(FileSystemMetadata info) |
| | | 369 | | { |
| | 2 | 370 | | return info.LastWriteTimeUtc; |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | /// <summary> |
| | | 374 | | /// Gets the creation time UTC. |
| | | 375 | | /// </summary> |
| | | 376 | | /// <param name="info">The info.</param> |
| | | 377 | | /// <returns>DateTime.</returns> |
| | | 378 | | public DateTime GetLastWriteTimeUtc(FileSystemInfo info) |
| | | 379 | | { |
| | | 380 | | // This could throw an error on some file systems that have dates out of range |
| | | 381 | | try |
| | | 382 | | { |
| | 45 | 383 | | return info.LastWriteTimeUtc; |
| | | 384 | | } |
| | 0 | 385 | | catch (Exception ex) |
| | | 386 | | { |
| | 0 | 387 | | _logger.LogError(ex, "Error determining LastAccessTimeUtc for {FullName}", info.FullName); |
| | 0 | 388 | | return DateTime.MinValue; |
| | | 389 | | } |
| | 45 | 390 | | } |
| | | 391 | | |
| | | 392 | | /// <inheritdoc /> |
| | | 393 | | public virtual DateTime GetLastWriteTimeUtc(string path) |
| | | 394 | | { |
| | 0 | 395 | | return GetLastWriteTimeUtc(GetFileSystemInfo(path)); |
| | | 396 | | } |
| | | 397 | | |
| | | 398 | | /// <inheritdoc /> |
| | | 399 | | public virtual void SetHidden(string path, bool isHidden) |
| | | 400 | | { |
| | 0 | 401 | | if (!OperatingSystem.IsWindows()) |
| | | 402 | | { |
| | 0 | 403 | | return; |
| | | 404 | | } |
| | | 405 | | |
| | 0 | 406 | | var info = new FileInfo(path); |
| | | 407 | | |
| | 0 | 408 | | if (info.Exists && |
| | 0 | 409 | | (info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden != isHidden) |
| | | 410 | | { |
| | 0 | 411 | | if (isHidden) |
| | | 412 | | { |
| | 0 | 413 | | File.SetAttributes(path, info.Attributes | FileAttributes.Hidden); |
| | | 414 | | } |
| | | 415 | | else |
| | | 416 | | { |
| | 0 | 417 | | File.SetAttributes(path, info.Attributes & ~FileAttributes.Hidden); |
| | | 418 | | } |
| | | 419 | | } |
| | 0 | 420 | | } |
| | | 421 | | |
| | | 422 | | /// <inheritdoc /> |
| | | 423 | | public virtual void SetAttributes(string path, bool isHidden, bool readOnly) |
| | | 424 | | { |
| | 2 | 425 | | if (!OperatingSystem.IsWindows()) |
| | | 426 | | { |
| | 2 | 427 | | return; |
| | | 428 | | } |
| | | 429 | | |
| | 0 | 430 | | var info = new FileInfo(path); |
| | | 431 | | |
| | 0 | 432 | | if (!info.Exists) |
| | | 433 | | { |
| | 0 | 434 | | return; |
| | | 435 | | } |
| | | 436 | | |
| | 0 | 437 | | if ((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly == readOnly |
| | 0 | 438 | | && (info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden == isHidden) |
| | | 439 | | { |
| | 0 | 440 | | return; |
| | | 441 | | } |
| | | 442 | | |
| | 0 | 443 | | var attributes = info.Attributes; |
| | | 444 | | |
| | 0 | 445 | | if (readOnly) |
| | | 446 | | { |
| | 0 | 447 | | attributes |= FileAttributes.ReadOnly; |
| | | 448 | | } |
| | | 449 | | else |
| | | 450 | | { |
| | 0 | 451 | | attributes &= ~FileAttributes.ReadOnly; |
| | | 452 | | } |
| | | 453 | | |
| | 0 | 454 | | if (isHidden) |
| | | 455 | | { |
| | 0 | 456 | | attributes |= FileAttributes.Hidden; |
| | | 457 | | } |
| | | 458 | | else |
| | | 459 | | { |
| | 0 | 460 | | attributes &= ~FileAttributes.Hidden; |
| | | 461 | | } |
| | | 462 | | |
| | 0 | 463 | | File.SetAttributes(path, attributes); |
| | 0 | 464 | | } |
| | | 465 | | |
| | | 466 | | /// <inheritdoc /> |
| | | 467 | | public virtual void SwapFiles(string file1, string file2) |
| | | 468 | | { |
| | 0 | 469 | | ArgumentException.ThrowIfNullOrEmpty(file1); |
| | 0 | 470 | | ArgumentException.ThrowIfNullOrEmpty(file2); |
| | | 471 | | |
| | 0 | 472 | | var temp1 = Path.Combine(_tempPath, Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture)); |
| | | 473 | | |
| | | 474 | | // Copying over will fail against hidden files |
| | 0 | 475 | | SetHidden(file1, false); |
| | 0 | 476 | | SetHidden(file2, false); |
| | | 477 | | |
| | 0 | 478 | | Directory.CreateDirectory(_tempPath); |
| | 0 | 479 | | File.Copy(file1, temp1, true); |
| | | 480 | | |
| | 0 | 481 | | File.Copy(file2, file1, true); |
| | 0 | 482 | | File.Move(temp1, file2, true); |
| | 0 | 483 | | } |
| | | 484 | | |
| | | 485 | | /// <inheritdoc /> |
| | | 486 | | public virtual bool ContainsSubPath(string parentPath, string path) |
| | | 487 | | { |
| | 1 | 488 | | ArgumentException.ThrowIfNullOrEmpty(parentPath); |
| | 1 | 489 | | ArgumentException.ThrowIfNullOrEmpty(path); |
| | | 490 | | |
| | 1 | 491 | | return path.Contains( |
| | 1 | 492 | | Path.TrimEndingDirectorySeparator(parentPath) + Path.DirectorySeparatorChar, |
| | 1 | 493 | | _isEnvironmentCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | /// <inheritdoc /> |
| | | 497 | | public virtual bool AreEqual(string path1, string path2) |
| | | 498 | | { |
| | 176 | 499 | | return Path.TrimEndingDirectorySeparator(path1).Equals( |
| | 176 | 500 | | Path.TrimEndingDirectorySeparator(path2), |
| | 176 | 501 | | _isEnvironmentCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); |
| | | 502 | | } |
| | | 503 | | |
| | | 504 | | /// <inheritdoc /> |
| | | 505 | | public virtual string GetFileNameWithoutExtension(FileSystemMetadata info) |
| | | 506 | | { |
| | 0 | 507 | | if (info.IsDirectory) |
| | | 508 | | { |
| | 0 | 509 | | return info.Name; |
| | | 510 | | } |
| | | 511 | | |
| | 0 | 512 | | return Path.GetFileNameWithoutExtension(info.FullName); |
| | | 513 | | } |
| | | 514 | | |
| | | 515 | | /// <inheritdoc /> |
| | | 516 | | public virtual bool IsPathFile(string path) |
| | | 517 | | { |
| | 2417 | 518 | | if (path.Contains("://", StringComparison.OrdinalIgnoreCase) |
| | 2417 | 519 | | && !path.StartsWith("file://", StringComparison.OrdinalIgnoreCase)) |
| | | 520 | | { |
| | 0 | 521 | | return false; |
| | | 522 | | } |
| | | 523 | | |
| | 2417 | 524 | | return true; |
| | | 525 | | } |
| | | 526 | | |
| | | 527 | | /// <inheritdoc /> |
| | | 528 | | public virtual void DeleteFile(string path) |
| | | 529 | | { |
| | 2 | 530 | | SetAttributes(path, false, false); |
| | 2 | 531 | | File.Delete(path); |
| | 2 | 532 | | } |
| | | 533 | | |
| | | 534 | | /// <inheritdoc /> |
| | | 535 | | public virtual IEnumerable<FileSystemMetadata> GetDrives() |
| | | 536 | | { |
| | | 537 | | // check for ready state to avoid waiting for drives to timeout |
| | | 538 | | // some drives on linux have no actual size or are used for other purposes |
| | 0 | 539 | | return DriveInfo.GetDrives() |
| | 0 | 540 | | .Where( |
| | 0 | 541 | | d => (d.DriveType == DriveType.Fixed || d.DriveType == DriveType.Network || d.DriveType == DriveType |
| | 0 | 542 | | && d.IsReady |
| | 0 | 543 | | && d.TotalSize != 0) |
| | 0 | 544 | | .Select(d => new FileSystemMetadata |
| | 0 | 545 | | { |
| | 0 | 546 | | Name = d.Name, |
| | 0 | 547 | | FullName = d.RootDirectory.FullName, |
| | 0 | 548 | | IsDirectory = true |
| | 0 | 549 | | }); |
| | | 550 | | } |
| | | 551 | | |
| | | 552 | | /// <inheritdoc /> |
| | | 553 | | public virtual IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false) |
| | | 554 | | { |
| | 0 | 555 | | return ToMetadata(new DirectoryInfo(path).EnumerateDirectories("*", GetEnumerationOptions(recursive))); |
| | | 556 | | } |
| | | 557 | | |
| | | 558 | | /// <inheritdoc /> |
| | | 559 | | public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false) |
| | | 560 | | { |
| | 2 | 561 | | return GetFiles(path, "*", recursive); |
| | | 562 | | } |
| | | 563 | | |
| | | 564 | | /// <inheritdoc /> |
| | | 565 | | public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, string searchPattern, bool recursive = fals |
| | | 566 | | { |
| | 2 | 567 | | return GetFiles(path, searchPattern, null, false, recursive); |
| | | 568 | | } |
| | | 569 | | |
| | | 570 | | /// <inheritdoc /> |
| | | 571 | | public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, IReadOnlyList<string>? extensions, bool ena |
| | | 572 | | { |
| | 12 | 573 | | return GetFiles(path, "*", extensions, enableCaseSensitiveExtensions, recursive); |
| | | 574 | | } |
| | | 575 | | |
| | | 576 | | /// <inheritdoc /> |
| | | 577 | | public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, string searchPattern, IReadOnlyList<string> |
| | | 578 | | { |
| | 14 | 579 | | var enumerationOptions = GetEnumerationOptions(recursive); |
| | | 580 | | |
| | | 581 | | // On linux and macOS the search pattern is case-sensitive |
| | | 582 | | // If we're OK with case-sensitivity, and we're only filtering for one extension, then use the native method |
| | 14 | 583 | | if ((enableCaseSensitiveExtensions || _isEnvironmentCaseInsensitive) && extensions is not null && extensions |
| | | 584 | | { |
| | 0 | 585 | | searchPattern = searchPattern.EndsWith(extensions[0], StringComparison.Ordinal) ? searchPattern : search |
| | | 586 | | |
| | 0 | 587 | | return ToMetadata(new DirectoryInfo(path).EnumerateFiles(searchPattern, enumerationOptions)); |
| | | 588 | | } |
| | | 589 | | |
| | 14 | 590 | | var files = new DirectoryInfo(path).EnumerateFiles(searchPattern, enumerationOptions); |
| | | 591 | | |
| | 14 | 592 | | if (extensions is not null && extensions.Count > 0) |
| | | 593 | | { |
| | 12 | 594 | | files = files.Where(i => |
| | 12 | 595 | | { |
| | 12 | 596 | | var ext = i.Extension.AsSpan(); |
| | 12 | 597 | | if (ext.IsEmpty) |
| | 12 | 598 | | { |
| | 12 | 599 | | return false; |
| | 12 | 600 | | } |
| | 12 | 601 | | |
| | 12 | 602 | | return extensions.Contains(ext, StringComparison.OrdinalIgnoreCase); |
| | 12 | 603 | | }); |
| | | 604 | | } |
| | | 605 | | |
| | 14 | 606 | | return ToMetadata(files); |
| | | 607 | | } |
| | | 608 | | |
| | | 609 | | /// <inheritdoc /> |
| | | 610 | | public virtual IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false) |
| | | 611 | | { |
| | | 612 | | // Note: any of unhandled exceptions thrown by this method may cause the caller to believe the whole path is |
| | | 613 | | // But what causing the exception may be a single file under that path. This could lead to unexpected behavi |
| | | 614 | | // For example, the scanner will remove everything in that path due to unhandled errors. |
| | 290 | 615 | | var directoryInfo = new DirectoryInfo(path); |
| | 290 | 616 | | var enumerationOptions = GetEnumerationOptions(recursive); |
| | | 617 | | |
| | 290 | 618 | | return ToMetadata(directoryInfo.EnumerateFileSystemInfos("*", enumerationOptions)); |
| | | 619 | | } |
| | | 620 | | |
| | | 621 | | private IEnumerable<FileSystemMetadata> ToMetadata(IEnumerable<FileSystemInfo> infos) |
| | | 622 | | { |
| | 303 | 623 | | return infos.Select(GetFileSystemMetadata); |
| | | 624 | | } |
| | | 625 | | |
| | | 626 | | /// <inheritdoc /> |
| | | 627 | | public virtual IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false) |
| | | 628 | | { |
| | 26 | 629 | | return Directory.EnumerateDirectories(path, "*", GetEnumerationOptions(recursive)); |
| | | 630 | | } |
| | | 631 | | |
| | | 632 | | /// <inheritdoc /> |
| | | 633 | | public virtual IEnumerable<string> GetFilePaths(string path, bool recursive = false) |
| | | 634 | | { |
| | 3 | 635 | | return GetFilePaths(path, null, false, recursive); |
| | | 636 | | } |
| | | 637 | | |
| | | 638 | | /// <inheritdoc /> |
| | | 639 | | public virtual IEnumerable<string> GetFilePaths(string path, string[]? extensions, bool enableCaseSensitiveExten |
| | | 640 | | { |
| | 4 | 641 | | var enumerationOptions = GetEnumerationOptions(recursive); |
| | | 642 | | |
| | | 643 | | // On linux and macOS the search pattern is case-sensitive |
| | | 644 | | // If we're OK with case-sensitivity, and we're only filtering for one extension, then use the native method |
| | 4 | 645 | | if ((enableCaseSensitiveExtensions || _isEnvironmentCaseInsensitive) && extensions is not null && extensions |
| | | 646 | | { |
| | 1 | 647 | | return Directory.EnumerateFiles(path, "*" + extensions[0], enumerationOptions); |
| | | 648 | | } |
| | | 649 | | |
| | 3 | 650 | | var files = Directory.EnumerateFiles(path, "*", enumerationOptions); |
| | | 651 | | |
| | 3 | 652 | | if (extensions is not null && extensions.Length > 0) |
| | | 653 | | { |
| | 0 | 654 | | files = files.Where(i => |
| | 0 | 655 | | { |
| | 0 | 656 | | var ext = Path.GetExtension(i.AsSpan()); |
| | 0 | 657 | | if (ext.IsEmpty) |
| | 0 | 658 | | { |
| | 0 | 659 | | return false; |
| | 0 | 660 | | } |
| | 0 | 661 | | |
| | 0 | 662 | | return extensions.Contains(ext, StringComparison.OrdinalIgnoreCase); |
| | 0 | 663 | | }); |
| | | 664 | | } |
| | | 665 | | |
| | 3 | 666 | | return files; |
| | | 667 | | } |
| | | 668 | | |
| | | 669 | | /// <inheritdoc /> |
| | | 670 | | public virtual IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false) |
| | | 671 | | { |
| | | 672 | | try |
| | | 673 | | { |
| | 38 | 674 | | return Directory.EnumerateFileSystemEntries(path, "*", GetEnumerationOptions(recursive)); |
| | | 675 | | } |
| | 0 | 676 | | catch (Exception ex) when (ex is UnauthorizedAccessException or DirectoryNotFoundException or SecurityExcept |
| | | 677 | | { |
| | 0 | 678 | | _logger.LogError(ex, "Failed to enumerate path {Path}", path); |
| | 0 | 679 | | return Enumerable.Empty<string>(); |
| | | 680 | | } |
| | 38 | 681 | | } |
| | | 682 | | |
| | | 683 | | /// <inheritdoc /> |
| | | 684 | | public virtual bool DirectoryExists(string path) |
| | | 685 | | { |
| | 0 | 686 | | return Directory.Exists(path); |
| | | 687 | | } |
| | | 688 | | |
| | | 689 | | /// <inheritdoc /> |
| | | 690 | | public virtual bool FileExists(string path) |
| | | 691 | | { |
| | 0 | 692 | | return File.Exists(path); |
| | | 693 | | } |
| | | 694 | | |
| | | 695 | | private EnumerationOptions GetEnumerationOptions(bool recursive) |
| | | 696 | | { |
| | 372 | 697 | | return new EnumerationOptions |
| | 372 | 698 | | { |
| | 372 | 699 | | RecurseSubdirectories = recursive, |
| | 372 | 700 | | IgnoreInaccessible = true, |
| | 372 | 701 | | // Don't skip any files. |
| | 372 | 702 | | AttributesToSkip = 0 |
| | 372 | 703 | | }; |
| | | 704 | | } |
| | | 705 | | } |
| | | 706 | | } |