ManagedFileSystem.cs 24 KB

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