ManagedFileSystem.cs 25 KB

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