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