| | 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 | | { |
| 129 | 57 | | ArgumentException.ThrowIfNullOrEmpty(filename); |
| | 58 | |
|
| 129 | 59 | | var extension = Path.GetExtension(filename); |
| 129 | 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 | | try |
| | 156 | | { |
| 2 | 157 | | Directory.Move(source, destination); |
| 1 | 158 | | } |
| 1 | 159 | | catch (IOException) |
| | 160 | | { |
| | 161 | | // Cross device move requires a copy |
| 1 | 162 | | Directory.CreateDirectory(destination); |
| 1 | 163 | | var sourceDir = new DirectoryInfo(source); |
| 6 | 164 | | foreach (var file in sourceDir.EnumerateFiles()) |
| | 165 | | { |
| 2 | 166 | | file.CopyTo(Path.Combine(destination, file.Name), true); |
| | 167 | | } |
| | 168 | |
|
| 1 | 169 | | sourceDir.Delete(true); |
| 1 | 170 | | } |
| 2 | 171 | | } |
| | 172 | |
|
| | 173 | | /// <summary> |
| | 174 | | /// Returns a <see cref="FileSystemMetadata"/> object for the specified file or directory path. |
| | 175 | | /// </summary> |
| | 176 | | /// <param name="path">A path to a file or directory.</param> |
| | 177 | | /// <returns>A <see cref="FileSystemMetadata"/> object.</returns> |
| | 178 | | /// <remarks>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> object's |
| | 179 | | /// <see cref="FileSystemMetadata.IsDirectory"/> property will be set to true and all other properties will refl |
| | 180 | | public virtual FileSystemMetadata GetFileSystemInfo(string path) |
| | 181 | | { |
| | 182 | | // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists |
| 131 | 183 | | if (Path.HasExtension(path)) |
| | 184 | | { |
| 0 | 185 | | var fileInfo = new FileInfo(path); |
| | 186 | |
|
| 0 | 187 | | if (fileInfo.Exists) |
| | 188 | | { |
| 0 | 189 | | return GetFileSystemMetadata(fileInfo); |
| | 190 | | } |
| | 191 | |
|
| 0 | 192 | | return GetFileSystemMetadata(new DirectoryInfo(path)); |
| | 193 | | } |
| | 194 | | else |
| | 195 | | { |
| 131 | 196 | | var fileInfo = new DirectoryInfo(path); |
| | 197 | |
|
| 131 | 198 | | if (fileInfo.Exists) |
| | 199 | | { |
| 131 | 200 | | return GetFileSystemMetadata(fileInfo); |
| | 201 | | } |
| | 202 | |
|
| 0 | 203 | | return GetFileSystemMetadata(new FileInfo(path)); |
| | 204 | | } |
| | 205 | | } |
| | 206 | |
|
| | 207 | | /// <summary> |
| | 208 | | /// Returns a <see cref="FileSystemMetadata"/> object for the specified file path. |
| | 209 | | /// </summary> |
| | 210 | | /// <param name="path">A path to a file.</param> |
| | 211 | | /// <returns>A <see cref="FileSystemMetadata"/> object.</returns> |
| | 212 | | /// <remarks><para>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> ob |
| | 213 | | /// <see cref="FileSystemMetadata.IsDirectory"/> property and the <see cref="FileSystemMetadata.Exists"/> proper |
| | 214 | | /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></r |
| | 215 | | public virtual FileSystemMetadata GetFileInfo(string path) |
| | 216 | | { |
| 1 | 217 | | var fileInfo = new FileInfo(path); |
| | 218 | |
|
| 1 | 219 | | return GetFileSystemMetadata(fileInfo); |
| | 220 | | } |
| | 221 | |
|
| | 222 | | /// <summary> |
| | 223 | | /// Returns a <see cref="FileSystemMetadata"/> object for the specified directory path. |
| | 224 | | /// </summary> |
| | 225 | | /// <param name="path">A path to a directory.</param> |
| | 226 | | /// <returns>A <see cref="FileSystemMetadata"/> object.</returns> |
| | 227 | | /// <remarks><para>If the specified path points to a file, the returned <see cref="FileSystemMetadata"/> object' |
| | 228 | | /// <see cref="FileSystemMetadata.IsDirectory"/> property will be set to true and the <see cref="FileSystemMetad |
| | 229 | | /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></r |
| | 230 | | public virtual FileSystemMetadata GetDirectoryInfo(string path) |
| | 231 | | { |
| 114 | 232 | | var fileInfo = new DirectoryInfo(path); |
| | 233 | |
|
| 114 | 234 | | return GetFileSystemMetadata(fileInfo); |
| | 235 | | } |
| | 236 | |
|
| | 237 | | private FileSystemMetadata GetFileSystemMetadata(FileSystemInfo info) |
| | 238 | | { |
| 405 | 239 | | var result = new FileSystemMetadata |
| 405 | 240 | | { |
| 405 | 241 | | Exists = info.Exists, |
| 405 | 242 | | FullName = info.FullName, |
| 405 | 243 | | Extension = info.Extension, |
| 405 | 244 | | Name = info.Name |
| 405 | 245 | | }; |
| | 246 | |
|
| 405 | 247 | | if (result.Exists) |
| | 248 | | { |
| 405 | 249 | | result.IsDirectory = info is DirectoryInfo || (info.Attributes & FileAttributes.Directory) == FileAttrib |
| | 250 | |
|
| | 251 | | // if (!result.IsDirectory) |
| | 252 | | // { |
| | 253 | | // result.IsHidden = (info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden; |
| | 254 | | // } |
| | 255 | |
|
| 405 | 256 | | if (info is FileInfo fileInfo) |
| | 257 | | { |
| 44 | 258 | | result.Length = fileInfo.Length; |
| | 259 | |
|
| | 260 | | // Issue #2354 get the size of files behind symbolic links. Also Enum.HasFlag is bad as it boxes! |
| 44 | 261 | | if ((fileInfo.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) |
| | 262 | | { |
| | 263 | | try |
| | 264 | | { |
| 1 | 265 | | using (var fileHandle = File.OpenHandle(fileInfo.FullName, FileMode.Open, FileAccess.Read, F |
| | 266 | | { |
| 0 | 267 | | result.Length = RandomAccess.GetLength(fileHandle); |
| 0 | 268 | | } |
| 0 | 269 | | } |
| 1 | 270 | | catch (FileNotFoundException ex) |
| | 271 | | { |
| | 272 | | // Dangling symlinks cannot be detected before opening the file unfortunately... |
| 1 | 273 | | _logger.LogError(ex, "Reading the file size of the symlink at {Path} failed. Marking the fil |
| 1 | 274 | | result.Exists = false; |
| 1 | 275 | | } |
| 0 | 276 | | catch (UnauthorizedAccessException ex) |
| | 277 | | { |
| 0 | 278 | | _logger.LogError(ex, "Reading the file at {Path} failed due to a permissions exception.", fi |
| 0 | 279 | | } |
| 0 | 280 | | catch (IOException ex) |
| | 281 | | { |
| | 282 | | // IOException generally means the file is not accessible due to filesystem issues |
| | 283 | | // Catch this exception and mark the file as not exist to ignore it |
| 0 | 284 | | _logger.LogError(ex, "Reading the file at {Path} failed due to an IO Exception. Marking the |
| 0 | 285 | | result.Exists = false; |
| 0 | 286 | | } |
| | 287 | | } |
| | 288 | | } |
| | 289 | |
|
| 405 | 290 | | result.CreationTimeUtc = GetCreationTimeUtc(info); |
| 405 | 291 | | result.LastWriteTimeUtc = GetLastWriteTimeUtc(info); |
| | 292 | | } |
| | 293 | | else |
| | 294 | | { |
| 0 | 295 | | result.IsDirectory = info is DirectoryInfo; |
| | 296 | | } |
| | 297 | |
|
| 405 | 298 | | return result; |
| | 299 | | } |
| | 300 | |
|
| | 301 | | /// <summary> |
| | 302 | | /// Takes a filename and removes invalid characters. |
| | 303 | | /// </summary> |
| | 304 | | /// <param name="filename">The filename.</param> |
| | 305 | | /// <returns>System.String.</returns> |
| | 306 | | /// <exception cref="ArgumentNullException">The filename is null.</exception> |
| | 307 | | public string GetValidFilename(string filename) |
| | 308 | | { |
| 7 | 309 | | var first = filename.IndexOfAny(_invalidPathCharacters); |
| 7 | 310 | | if (first == -1) |
| | 311 | | { |
| | 312 | | // Fast path for clean strings |
| 4 | 313 | | return filename; |
| | 314 | | } |
| | 315 | |
|
| 3 | 316 | | return string.Create( |
| 3 | 317 | | filename.Length, |
| 3 | 318 | | (filename, _invalidPathCharacters, first), |
| 3 | 319 | | (chars, state) => |
| 3 | 320 | | { |
| 3 | 321 | | state.filename.AsSpan().CopyTo(chars); |
| 3 | 322 | |
|
| 3 | 323 | | chars[state.first++] = ' '; |
| 3 | 324 | |
|
| 3 | 325 | | var len = chars.Length; |
| 3 | 326 | | foreach (var c in state._invalidPathCharacters) |
| 3 | 327 | | { |
| 3 | 328 | | for (int i = state.first; i < len; i++) |
| 3 | 329 | | { |
| 3 | 330 | | if (chars[i] == c) |
| 3 | 331 | | { |
| 3 | 332 | | chars[i] = ' '; |
| 3 | 333 | | } |
| 3 | 334 | | } |
| 3 | 335 | | } |
| 3 | 336 | | }); |
| | 337 | | } |
| | 338 | |
|
| | 339 | | /// <summary> |
| | 340 | | /// Gets the creation time UTC. |
| | 341 | | /// </summary> |
| | 342 | | /// <param name="info">The info.</param> |
| | 343 | | /// <returns>DateTime.</returns> |
| | 344 | | public DateTime GetCreationTimeUtc(FileSystemInfo info) |
| | 345 | | { |
| | 346 | | // This could throw an error on some file systems that have dates out of range |
| | 347 | | try |
| | 348 | | { |
| 405 | 349 | | return info.CreationTimeUtc; |
| | 350 | | } |
| 0 | 351 | | catch (Exception ex) |
| | 352 | | { |
| 0 | 353 | | _logger.LogError(ex, "Error determining CreationTimeUtc for {FullName}", info.FullName); |
| 0 | 354 | | return DateTime.MinValue; |
| | 355 | | } |
| 405 | 356 | | } |
| | 357 | |
|
| | 358 | | /// <inheritdoc /> |
| | 359 | | public virtual DateTime GetCreationTimeUtc(string path) |
| | 360 | | { |
| 0 | 361 | | return GetCreationTimeUtc(GetFileSystemInfo(path)); |
| | 362 | | } |
| | 363 | |
|
| | 364 | | /// <inheritdoc /> |
| | 365 | | public virtual DateTime GetCreationTimeUtc(FileSystemMetadata info) |
| | 366 | | { |
| 0 | 367 | | return info.CreationTimeUtc; |
| | 368 | | } |
| | 369 | |
|
| | 370 | | /// <inheritdoc /> |
| | 371 | | public virtual DateTime GetLastWriteTimeUtc(FileSystemMetadata info) |
| | 372 | | { |
| 2 | 373 | | return info.LastWriteTimeUtc; |
| | 374 | | } |
| | 375 | |
|
| | 376 | | /// <summary> |
| | 377 | | /// Gets the creation time UTC. |
| | 378 | | /// </summary> |
| | 379 | | /// <param name="info">The info.</param> |
| | 380 | | /// <returns>DateTime.</returns> |
| | 381 | | public DateTime GetLastWriteTimeUtc(FileSystemInfo info) |
| | 382 | | { |
| | 383 | | // This could throw an error on some file systems that have dates out of range |
| | 384 | | try |
| | 385 | | { |
| 405 | 386 | | return info.LastWriteTimeUtc; |
| | 387 | | } |
| 0 | 388 | | catch (Exception ex) |
| | 389 | | { |
| 0 | 390 | | _logger.LogError(ex, "Error determining LastAccessTimeUtc for {FullName}", info.FullName); |
| 0 | 391 | | return DateTime.MinValue; |
| | 392 | | } |
| 405 | 393 | | } |
| | 394 | |
|
| | 395 | | /// <inheritdoc /> |
| | 396 | | public virtual DateTime GetLastWriteTimeUtc(string path) |
| | 397 | | { |
| 0 | 398 | | return GetLastWriteTimeUtc(GetFileSystemInfo(path)); |
| | 399 | | } |
| | 400 | |
|
| | 401 | | /// <inheritdoc /> |
| | 402 | | public virtual void SetHidden(string path, bool isHidden) |
| | 403 | | { |
| 0 | 404 | | if (!OperatingSystem.IsWindows()) |
| | 405 | | { |
| 0 | 406 | | return; |
| | 407 | | } |
| | 408 | |
|
| 0 | 409 | | var info = new FileInfo(path); |
| | 410 | |
|
| 0 | 411 | | if (info.Exists && |
| 0 | 412 | | (info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden != isHidden) |
| | 413 | | { |
| 0 | 414 | | if (isHidden) |
| | 415 | | { |
| 0 | 416 | | File.SetAttributes(path, info.Attributes | FileAttributes.Hidden); |
| | 417 | | } |
| | 418 | | else |
| | 419 | | { |
| 0 | 420 | | File.SetAttributes(path, info.Attributes & ~FileAttributes.Hidden); |
| | 421 | | } |
| | 422 | | } |
| 0 | 423 | | } |
| | 424 | |
|
| | 425 | | /// <inheritdoc /> |
| | 426 | | public virtual void SetAttributes(string path, bool isHidden, bool readOnly) |
| | 427 | | { |
| 2 | 428 | | if (!OperatingSystem.IsWindows()) |
| | 429 | | { |
| 2 | 430 | | return; |
| | 431 | | } |
| | 432 | |
|
| 0 | 433 | | var info = new FileInfo(path); |
| | 434 | |
|
| 0 | 435 | | if (!info.Exists) |
| | 436 | | { |
| 0 | 437 | | return; |
| | 438 | | } |
| | 439 | |
|
| 0 | 440 | | if ((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly == readOnly |
| 0 | 441 | | && (info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden == isHidden) |
| | 442 | | { |
| 0 | 443 | | return; |
| | 444 | | } |
| | 445 | |
|
| 0 | 446 | | var attributes = info.Attributes; |
| | 447 | |
|
| 0 | 448 | | if (readOnly) |
| | 449 | | { |
| 0 | 450 | | attributes |= FileAttributes.ReadOnly; |
| | 451 | | } |
| | 452 | | else |
| | 453 | | { |
| 0 | 454 | | attributes &= ~FileAttributes.ReadOnly; |
| | 455 | | } |
| | 456 | |
|
| 0 | 457 | | if (isHidden) |
| | 458 | | { |
| 0 | 459 | | attributes |= FileAttributes.Hidden; |
| | 460 | | } |
| | 461 | | else |
| | 462 | | { |
| 0 | 463 | | attributes &= ~FileAttributes.Hidden; |
| | 464 | | } |
| | 465 | |
|
| 0 | 466 | | File.SetAttributes(path, attributes); |
| 0 | 467 | | } |
| | 468 | |
|
| | 469 | | /// <inheritdoc /> |
| | 470 | | public virtual void SwapFiles(string file1, string file2) |
| | 471 | | { |
| 0 | 472 | | ArgumentException.ThrowIfNullOrEmpty(file1); |
| 0 | 473 | | ArgumentException.ThrowIfNullOrEmpty(file2); |
| | 474 | |
|
| 0 | 475 | | var temp1 = Path.Combine(_tempPath, Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture)); |
| | 476 | |
|
| | 477 | | // Copying over will fail against hidden files |
| 0 | 478 | | SetHidden(file1, false); |
| 0 | 479 | | SetHidden(file2, false); |
| | 480 | |
|
| 0 | 481 | | Directory.CreateDirectory(_tempPath); |
| 0 | 482 | | File.Copy(file1, temp1, true); |
| | 483 | |
|
| 0 | 484 | | File.Copy(file2, file1, true); |
| 0 | 485 | | File.Move(temp1, file2, true); |
| 0 | 486 | | } |
| | 487 | |
|
| | 488 | | /// <inheritdoc /> |
| | 489 | | public virtual bool ContainsSubPath(string parentPath, string path) |
| | 490 | | { |
| 1 | 491 | | ArgumentException.ThrowIfNullOrEmpty(parentPath); |
| 1 | 492 | | ArgumentException.ThrowIfNullOrEmpty(path); |
| | 493 | |
|
| 1 | 494 | | return path.Contains( |
| 1 | 495 | | Path.TrimEndingDirectorySeparator(parentPath) + Path.DirectorySeparatorChar, |
| 1 | 496 | | _isEnvironmentCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); |
| | 497 | | } |
| | 498 | |
|
| | 499 | | /// <inheritdoc /> |
| | 500 | | public virtual bool AreEqual(string path1, string path2) |
| | 501 | | { |
| 171 | 502 | | return Path.TrimEndingDirectorySeparator(path1).Equals( |
| 171 | 503 | | Path.TrimEndingDirectorySeparator(path2), |
| 171 | 504 | | _isEnvironmentCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); |
| | 505 | | } |
| | 506 | |
|
| | 507 | | /// <inheritdoc /> |
| | 508 | | public virtual string GetFileNameWithoutExtension(FileSystemMetadata info) |
| | 509 | | { |
| 0 | 510 | | if (info.IsDirectory) |
| | 511 | | { |
| 0 | 512 | | return info.Name; |
| | 513 | | } |
| | 514 | |
|
| 0 | 515 | | return Path.GetFileNameWithoutExtension(info.FullName); |
| | 516 | | } |
| | 517 | |
|
| | 518 | | /// <inheritdoc /> |
| | 519 | | public virtual bool IsPathFile(string path) |
| | 520 | | { |
| 2045 | 521 | | if (path.Contains("://", StringComparison.OrdinalIgnoreCase) |
| 2045 | 522 | | && !path.StartsWith("file://", StringComparison.OrdinalIgnoreCase)) |
| | 523 | | { |
| 0 | 524 | | return false; |
| | 525 | | } |
| | 526 | |
|
| 2045 | 527 | | return true; |
| | 528 | | } |
| | 529 | |
|
| | 530 | | /// <inheritdoc /> |
| | 531 | | public virtual void DeleteFile(string path) |
| | 532 | | { |
| 2 | 533 | | SetAttributes(path, false, false); |
| 2 | 534 | | File.Delete(path); |
| 2 | 535 | | } |
| | 536 | |
|
| | 537 | | /// <inheritdoc /> |
| | 538 | | public virtual IEnumerable<FileSystemMetadata> GetDrives() |
| | 539 | | { |
| | 540 | | // check for ready state to avoid waiting for drives to timeout |
| | 541 | | // some drives on linux have no actual size or are used for other purposes |
| 0 | 542 | | return DriveInfo.GetDrives() |
| 0 | 543 | | .Where( |
| 0 | 544 | | d => (d.DriveType == DriveType.Fixed || d.DriveType == DriveType.Network || d.DriveType == DriveType |
| 0 | 545 | | && d.IsReady |
| 0 | 546 | | && d.TotalSize != 0) |
| 0 | 547 | | .Select(d => new FileSystemMetadata |
| 0 | 548 | | { |
| 0 | 549 | | Name = d.Name, |
| 0 | 550 | | FullName = d.RootDirectory.FullName, |
| 0 | 551 | | IsDirectory = true |
| 0 | 552 | | }); |
| | 553 | | } |
| | 554 | |
|
| | 555 | | /// <inheritdoc /> |
| | 556 | | public virtual IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false) |
| | 557 | | { |
| 0 | 558 | | return ToMetadata(new DirectoryInfo(path).EnumerateDirectories("*", GetEnumerationOptions(recursive))); |
| | 559 | | } |
| | 560 | |
|
| | 561 | | /// <inheritdoc /> |
| | 562 | | public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false) |
| | 563 | | { |
| 2 | 564 | | return GetFiles(path, "*", recursive); |
| | 565 | | } |
| | 566 | |
|
| | 567 | | /// <inheritdoc /> |
| | 568 | | public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, string searchPattern, bool recursive = fals |
| | 569 | | { |
| 2 | 570 | | return GetFiles(path, searchPattern, null, false, recursive); |
| | 571 | | } |
| | 572 | |
|
| | 573 | | /// <inheritdoc /> |
| | 574 | | public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, IReadOnlyList<string>? extensions, bool ena |
| | 575 | | { |
| 13 | 576 | | return GetFiles(path, "*", extensions, enableCaseSensitiveExtensions, recursive); |
| | 577 | | } |
| | 578 | |
|
| | 579 | | /// <inheritdoc /> |
| | 580 | | public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, string searchPattern, IReadOnlyList<string> |
| | 581 | | { |
| 15 | 582 | | var enumerationOptions = GetEnumerationOptions(recursive); |
| | 583 | |
|
| | 584 | | // On linux and macOS the search pattern is case-sensitive |
| | 585 | | // If we're OK with case-sensitivity, and we're only filtering for one extension, then use the native method |
| 15 | 586 | | if ((enableCaseSensitiveExtensions || _isEnvironmentCaseInsensitive) && extensions is not null && extensions |
| | 587 | | { |
| 0 | 588 | | searchPattern = searchPattern.EndsWith(extensions[0], StringComparison.Ordinal) ? searchPattern : search |
| | 589 | |
|
| 0 | 590 | | return ToMetadata(new DirectoryInfo(path).EnumerateFiles(searchPattern, enumerationOptions)); |
| | 591 | | } |
| | 592 | |
|
| 15 | 593 | | var files = new DirectoryInfo(path).EnumerateFiles(searchPattern, enumerationOptions); |
| | 594 | |
|
| 15 | 595 | | if (extensions is not null && extensions.Count > 0) |
| | 596 | | { |
| 13 | 597 | | files = files.Where(i => |
| 13 | 598 | | { |
| 13 | 599 | | var ext = i.Extension.AsSpan(); |
| 13 | 600 | | if (ext.IsEmpty) |
| 13 | 601 | | { |
| 13 | 602 | | return false; |
| 13 | 603 | | } |
| 13 | 604 | |
|
| 13 | 605 | | return extensions.Contains(ext, StringComparison.OrdinalIgnoreCase); |
| 13 | 606 | | }); |
| | 607 | | } |
| | 608 | |
|
| 15 | 609 | | return ToMetadata(files); |
| | 610 | | } |
| | 611 | |
|
| | 612 | | /// <inheritdoc /> |
| | 613 | | public virtual IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false) |
| | 614 | | { |
| | 615 | | // Note: any of unhandled exceptions thrown by this method may cause the caller to believe the whole path is |
| | 616 | | // But what causing the exception may be a single file under that path. This could lead to unexpected behavi |
| | 617 | | // For example, the scanner will remove everything in that path due to unhandled errors. |
| 247 | 618 | | var directoryInfo = new DirectoryInfo(path); |
| 247 | 619 | | var enumerationOptions = GetEnumerationOptions(recursive); |
| | 620 | |
|
| 247 | 621 | | return ToMetadata(directoryInfo.EnumerateFileSystemInfos("*", enumerationOptions)); |
| | 622 | | } |
| | 623 | |
|
| | 624 | | private IEnumerable<FileSystemMetadata> ToMetadata(IEnumerable<FileSystemInfo> infos) |
| | 625 | | { |
| 262 | 626 | | return infos.Select(GetFileSystemMetadata); |
| | 627 | | } |
| | 628 | |
|
| | 629 | | /// <inheritdoc /> |
| | 630 | | public virtual IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false) |
| | 631 | | { |
| 26 | 632 | | return Directory.EnumerateDirectories(path, "*", GetEnumerationOptions(recursive)); |
| | 633 | | } |
| | 634 | |
|
| | 635 | | /// <inheritdoc /> |
| | 636 | | public virtual IEnumerable<string> GetFilePaths(string path, bool recursive = false) |
| | 637 | | { |
| 3 | 638 | | return GetFilePaths(path, null, false, recursive); |
| | 639 | | } |
| | 640 | |
|
| | 641 | | /// <inheritdoc /> |
| | 642 | | public virtual IEnumerable<string> GetFilePaths(string path, string[]? extensions, bool enableCaseSensitiveExten |
| | 643 | | { |
| 4 | 644 | | var enumerationOptions = GetEnumerationOptions(recursive); |
| | 645 | |
|
| | 646 | | // On linux and macOS the search pattern is case-sensitive |
| | 647 | | // If we're OK with case-sensitivity, and we're only filtering for one extension, then use the native method |
| 4 | 648 | | if ((enableCaseSensitiveExtensions || _isEnvironmentCaseInsensitive) && extensions is not null && extensions |
| | 649 | | { |
| 1 | 650 | | return Directory.EnumerateFiles(path, "*" + extensions[0], enumerationOptions); |
| | 651 | | } |
| | 652 | |
|
| 3 | 653 | | var files = Directory.EnumerateFiles(path, "*", enumerationOptions); |
| | 654 | |
|
| 3 | 655 | | if (extensions is not null && extensions.Length > 0) |
| | 656 | | { |
| 0 | 657 | | files = files.Where(i => |
| 0 | 658 | | { |
| 0 | 659 | | var ext = Path.GetExtension(i.AsSpan()); |
| 0 | 660 | | if (ext.IsEmpty) |
| 0 | 661 | | { |
| 0 | 662 | | return false; |
| 0 | 663 | | } |
| 0 | 664 | |
|
| 0 | 665 | | return extensions.Contains(ext, StringComparison.OrdinalIgnoreCase); |
| 0 | 666 | | }); |
| | 667 | | } |
| | 668 | |
|
| 3 | 669 | | return files; |
| | 670 | | } |
| | 671 | |
|
| | 672 | | /// <inheritdoc /> |
| | 673 | | public virtual IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false) |
| | 674 | | { |
| | 675 | | try |
| | 676 | | { |
| 26 | 677 | | return Directory.EnumerateFileSystemEntries(path, "*", GetEnumerationOptions(recursive)); |
| | 678 | | } |
| 0 | 679 | | catch (Exception ex) when (ex is UnauthorizedAccessException or DirectoryNotFoundException or SecurityExcept |
| | 680 | | { |
| 0 | 681 | | _logger.LogError(ex, "Failed to enumerate path {Path}", path); |
| 0 | 682 | | return Enumerable.Empty<string>(); |
| | 683 | | } |
| 26 | 684 | | } |
| | 685 | |
|
| | 686 | | /// <inheritdoc /> |
| | 687 | | public virtual bool DirectoryExists(string path) |
| | 688 | | { |
| 0 | 689 | | return Directory.Exists(path); |
| | 690 | | } |
| | 691 | |
|
| | 692 | | /// <inheritdoc /> |
| | 693 | | public virtual bool FileExists(string path) |
| | 694 | | { |
| 0 | 695 | | return File.Exists(path); |
| | 696 | | } |
| | 697 | |
|
| | 698 | | private EnumerationOptions GetEnumerationOptions(bool recursive) |
| | 699 | | { |
| 318 | 700 | | return new EnumerationOptions |
| 318 | 701 | | { |
| 318 | 702 | | RecurseSubdirectories = recursive, |
| 318 | 703 | | IgnoreInaccessible = true, |
| 318 | 704 | | // Don't skip any files. |
| 318 | 705 | | AttributesToSkip = 0 |
| 318 | 706 | | }; |
| | 707 | | } |
| | 708 | | } |
| | 709 | | } |