ManagedFileSystem.cs 25 KB

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