ManagedFileSystem.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. // 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. ArgumentException.ThrowIfNullOrEmpty(shortcutPath);
  124. ArgumentException.ThrowIfNullOrEmpty(target);
  125. var extension = Path.GetExtension(shortcutPath);
  126. var handler = _shortcutHandlers.Find(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
  127. if (handler is not 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. catch (UnauthorizedAccessException ex)
  227. {
  228. _logger.LogError(ex, "Reading the file at {Path} failed due to a permissions exception.", fileInfo.FullName);
  229. }
  230. }
  231. }
  232. result.CreationTimeUtc = GetCreationTimeUtc(info);
  233. result.LastWriteTimeUtc = GetLastWriteTimeUtc(info);
  234. }
  235. else
  236. {
  237. result.IsDirectory = info is DirectoryInfo;
  238. }
  239. return result;
  240. }
  241. /// <summary>
  242. /// Takes a filename and removes invalid characters.
  243. /// </summary>
  244. /// <param name="filename">The filename.</param>
  245. /// <returns>System.String.</returns>
  246. /// <exception cref="ArgumentNullException">The filename is null.</exception>
  247. public string GetValidFilename(string filename)
  248. {
  249. var first = filename.IndexOfAny(_invalidPathCharacters);
  250. if (first == -1)
  251. {
  252. // Fast path for clean strings
  253. return filename;
  254. }
  255. return string.Create(
  256. filename.Length,
  257. (filename, _invalidPathCharacters, first),
  258. (chars, state) =>
  259. {
  260. state.filename.AsSpan().CopyTo(chars);
  261. chars[state.first++] = ' ';
  262. var len = chars.Length;
  263. foreach (var c in state._invalidPathCharacters)
  264. {
  265. for (int i = state.first; i < len; i++)
  266. {
  267. if (chars[i] == c)
  268. {
  269. chars[i] = ' ';
  270. }
  271. }
  272. }
  273. });
  274. }
  275. /// <summary>
  276. /// Gets the creation time UTC.
  277. /// </summary>
  278. /// <param name="info">The info.</param>
  279. /// <returns>DateTime.</returns>
  280. public DateTime GetCreationTimeUtc(FileSystemInfo info)
  281. {
  282. // This could throw an error on some file systems that have dates out of range
  283. try
  284. {
  285. return info.CreationTimeUtc;
  286. }
  287. catch (Exception ex)
  288. {
  289. _logger.LogError(ex, "Error determining CreationTimeUtc for {FullName}", info.FullName);
  290. return DateTime.MinValue;
  291. }
  292. }
  293. /// <summary>
  294. /// Gets the creation time UTC.
  295. /// </summary>
  296. /// <param name="path">The path.</param>
  297. /// <returns>DateTime.</returns>
  298. public virtual DateTime GetCreationTimeUtc(string path)
  299. {
  300. return GetCreationTimeUtc(GetFileSystemInfo(path));
  301. }
  302. /// <inheritdoc />
  303. public virtual DateTime GetCreationTimeUtc(FileSystemMetadata info)
  304. {
  305. return info.CreationTimeUtc;
  306. }
  307. /// <inheritdoc />
  308. public virtual DateTime GetLastWriteTimeUtc(FileSystemMetadata info)
  309. {
  310. return info.LastWriteTimeUtc;
  311. }
  312. /// <summary>
  313. /// Gets the creation time UTC.
  314. /// </summary>
  315. /// <param name="info">The info.</param>
  316. /// <returns>DateTime.</returns>
  317. public DateTime GetLastWriteTimeUtc(FileSystemInfo info)
  318. {
  319. // This could throw an error on some file systems that have dates out of range
  320. try
  321. {
  322. return info.LastWriteTimeUtc;
  323. }
  324. catch (Exception ex)
  325. {
  326. _logger.LogError(ex, "Error determining LastAccessTimeUtc for {FullName}", info.FullName);
  327. return DateTime.MinValue;
  328. }
  329. }
  330. /// <summary>
  331. /// Gets the last write time UTC.
  332. /// </summary>
  333. /// <param name="path">The path.</param>
  334. /// <returns>DateTime.</returns>
  335. public virtual DateTime GetLastWriteTimeUtc(string path)
  336. {
  337. return GetLastWriteTimeUtc(GetFileSystemInfo(path));
  338. }
  339. /// <inheritdoc />
  340. public virtual void SetHidden(string path, bool isHidden)
  341. {
  342. if (!OperatingSystem.IsWindows())
  343. {
  344. return;
  345. }
  346. var info = new FileInfo(path);
  347. if (info.Exists &&
  348. ((info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) != isHidden)
  349. {
  350. if (isHidden)
  351. {
  352. File.SetAttributes(path, info.Attributes | FileAttributes.Hidden);
  353. }
  354. else
  355. {
  356. File.SetAttributes(path, info.Attributes & ~FileAttributes.Hidden);
  357. }
  358. }
  359. }
  360. /// <inheritdoc />
  361. public virtual void SetAttributes(string path, bool isHidden, bool readOnly)
  362. {
  363. if (!OperatingSystem.IsWindows())
  364. {
  365. return;
  366. }
  367. var info = new FileInfo(path);
  368. if (!info.Exists)
  369. {
  370. return;
  371. }
  372. if (((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) == readOnly
  373. && ((info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) == isHidden)
  374. {
  375. return;
  376. }
  377. var attributes = info.Attributes;
  378. if (readOnly)
  379. {
  380. attributes |= FileAttributes.ReadOnly;
  381. }
  382. else
  383. {
  384. attributes &= ~FileAttributes.ReadOnly;
  385. }
  386. if (isHidden)
  387. {
  388. attributes |= FileAttributes.Hidden;
  389. }
  390. else
  391. {
  392. attributes &= ~FileAttributes.Hidden;
  393. }
  394. File.SetAttributes(path, attributes);
  395. }
  396. /// <summary>
  397. /// Swaps the files.
  398. /// </summary>
  399. /// <param name="file1">The file1.</param>
  400. /// <param name="file2">The file2.</param>
  401. public virtual void SwapFiles(string file1, string file2)
  402. {
  403. ArgumentException.ThrowIfNullOrEmpty(file1);
  404. ArgumentException.ThrowIfNullOrEmpty(file2);
  405. var temp1 = Path.Combine(_tempPath, Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
  406. // Copying over will fail against hidden files
  407. SetHidden(file1, false);
  408. SetHidden(file2, false);
  409. Directory.CreateDirectory(_tempPath);
  410. File.Copy(file1, temp1, true);
  411. File.Copy(file2, file1, true);
  412. File.Copy(temp1, file2, true);
  413. }
  414. /// <inheritdoc />
  415. public virtual bool ContainsSubPath(string parentPath, string path)
  416. {
  417. ArgumentException.ThrowIfNullOrEmpty(parentPath);
  418. ArgumentException.ThrowIfNullOrEmpty(path);
  419. return path.Contains(
  420. Path.TrimEndingDirectorySeparator(parentPath) + Path.DirectorySeparatorChar,
  421. _isEnvironmentCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
  422. }
  423. /// <inheritdoc />
  424. public virtual bool AreEqual(string path1, string path2)
  425. {
  426. return Path.TrimEndingDirectorySeparator(path1).Equals(
  427. Path.TrimEndingDirectorySeparator(path2),
  428. _isEnvironmentCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
  429. }
  430. /// <inheritdoc />
  431. public virtual string GetFileNameWithoutExtension(FileSystemMetadata info)
  432. {
  433. if (info.IsDirectory)
  434. {
  435. return info.Name;
  436. }
  437. return Path.GetFileNameWithoutExtension(info.FullName);
  438. }
  439. /// <inheritdoc />
  440. public virtual bool IsPathFile(string path)
  441. {
  442. if (path.Contains("://", StringComparison.OrdinalIgnoreCase)
  443. && !path.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
  444. {
  445. return false;
  446. }
  447. return true;
  448. }
  449. /// <inheritdoc />
  450. public virtual void DeleteFile(string path)
  451. {
  452. SetAttributes(path, false, false);
  453. File.Delete(path);
  454. }
  455. /// <inheritdoc />
  456. public virtual IEnumerable<FileSystemMetadata> GetDrives()
  457. {
  458. // check for ready state to avoid waiting for drives to timeout
  459. // some drives on linux have no actual size or are used for other purposes
  460. return DriveInfo.GetDrives()
  461. .Where(
  462. d => (d.DriveType == DriveType.Fixed || d.DriveType == DriveType.Network || d.DriveType == DriveType.Removable)
  463. && d.IsReady
  464. && d.TotalSize != 0)
  465. .Select(d => new FileSystemMetadata
  466. {
  467. Name = d.Name,
  468. FullName = d.RootDirectory.FullName,
  469. IsDirectory = true
  470. });
  471. }
  472. /// <inheritdoc />
  473. public virtual IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false)
  474. {
  475. return ToMetadata(new DirectoryInfo(path).EnumerateDirectories("*", GetEnumerationOptions(recursive)));
  476. }
  477. /// <inheritdoc />
  478. public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false)
  479. {
  480. return GetFiles(path, null, false, recursive);
  481. }
  482. /// <inheritdoc />
  483. public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, IReadOnlyList<string>? extensions, bool enableCaseSensitiveExtensions, bool recursive = false)
  484. {
  485. var enumerationOptions = GetEnumerationOptions(recursive);
  486. // On linux and osx the search pattern is case sensitive
  487. // If we're OK with case-sensitivity, and we're only filtering for one extension, then use the native method
  488. if ((enableCaseSensitiveExtensions || _isEnvironmentCaseInsensitive) && extensions is not null && extensions.Count == 1)
  489. {
  490. return ToMetadata(new DirectoryInfo(path).EnumerateFiles("*" + extensions[0], enumerationOptions));
  491. }
  492. var files = new DirectoryInfo(path).EnumerateFiles("*", enumerationOptions);
  493. if (extensions is not null && extensions.Count > 0)
  494. {
  495. files = files.Where(i =>
  496. {
  497. var ext = i.Extension.AsSpan();
  498. if (ext.IsEmpty)
  499. {
  500. return false;
  501. }
  502. return extensions.Contains(ext, StringComparison.OrdinalIgnoreCase);
  503. });
  504. }
  505. return ToMetadata(files);
  506. }
  507. /// <inheritdoc />
  508. public virtual IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false)
  509. {
  510. var directoryInfo = new DirectoryInfo(path);
  511. var enumerationOptions = GetEnumerationOptions(recursive);
  512. return ToMetadata(directoryInfo.EnumerateFileSystemInfos("*", enumerationOptions));
  513. }
  514. private IEnumerable<FileSystemMetadata> ToMetadata(IEnumerable<FileSystemInfo> infos)
  515. {
  516. return infos.Select(GetFileSystemMetadata);
  517. }
  518. /// <inheritdoc />
  519. public virtual IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false)
  520. {
  521. return Directory.EnumerateDirectories(path, "*", GetEnumerationOptions(recursive));
  522. }
  523. /// <inheritdoc />
  524. public virtual IEnumerable<string> GetFilePaths(string path, bool recursive = false)
  525. {
  526. return GetFilePaths(path, null, false, recursive);
  527. }
  528. /// <inheritdoc />
  529. public virtual IEnumerable<string> GetFilePaths(string path, 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 is not null && extensions.Length == 1)
  535. {
  536. return Directory.EnumerateFiles(path, "*" + extensions[0], enumerationOptions);
  537. }
  538. var files = Directory.EnumerateFiles(path, "*", enumerationOptions);
  539. if (extensions is not null && extensions.Length > 0)
  540. {
  541. files = files.Where(i =>
  542. {
  543. var ext = Path.GetExtension(i.AsSpan());
  544. if (ext.IsEmpty)
  545. {
  546. return false;
  547. }
  548. return extensions.Contains(ext, StringComparison.OrdinalIgnoreCase);
  549. });
  550. }
  551. return files;
  552. }
  553. /// <inheritdoc />
  554. public virtual IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false)
  555. {
  556. return Directory.EnumerateFileSystemEntries(path, "*", GetEnumerationOptions(recursive));
  557. }
  558. /// <inheritdoc />
  559. public virtual bool DirectoryExists(string path)
  560. {
  561. return Directory.Exists(path);
  562. }
  563. /// <inheritdoc />
  564. public virtual bool FileExists(string path)
  565. {
  566. return File.Exists(path);
  567. }
  568. private EnumerationOptions GetEnumerationOptions(bool recursive)
  569. {
  570. return new EnumerationOptions
  571. {
  572. RecurseSubdirectories = recursive,
  573. IgnoreInaccessible = true,
  574. // Don't skip any files.
  575. AttributesToSkip = 0
  576. };
  577. }
  578. }
  579. }