ManagedFileSystem.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using MediaBrowser.Model.IO;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.System;
  9. namespace Emby.Common.Implementations.IO
  10. {
  11. /// <summary>
  12. /// Class ManagedFileSystem
  13. /// </summary>
  14. public class ManagedFileSystem : IFileSystem
  15. {
  16. protected ILogger Logger;
  17. private readonly bool _supportsAsyncFileStreams;
  18. private char[] _invalidFileNameChars;
  19. private readonly List<IShortcutHandler> _shortcutHandlers = new List<IShortcutHandler>();
  20. private bool EnableFileSystemRequestConcat;
  21. private string _tempPath;
  22. public ManagedFileSystem(ILogger logger, IEnvironmentInfo environmentInfo, string tempPath)
  23. {
  24. Logger = logger;
  25. _supportsAsyncFileStreams = true;
  26. _tempPath = tempPath;
  27. // On Linux, this needs to be true or symbolic links are ignored
  28. EnableFileSystemRequestConcat = environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows &&
  29. environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.OSX;
  30. SetInvalidFileNameChars(environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows);
  31. }
  32. public void AddShortcutHandler(IShortcutHandler handler)
  33. {
  34. _shortcutHandlers.Add(handler);
  35. }
  36. protected void SetInvalidFileNameChars(bool enableManagedInvalidFileNameChars)
  37. {
  38. if (enableManagedInvalidFileNameChars)
  39. {
  40. _invalidFileNameChars = Path.GetInvalidFileNameChars();
  41. }
  42. else
  43. {
  44. // GetInvalidFileNameChars is less restrictive in Linux/Mac than Windows, this mimic Windows behavior for mono under Linux/Mac.
  45. _invalidFileNameChars = new char[41] { '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
  46. '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F', '\x10', '\x11', '\x12',
  47. '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D',
  48. '\x1E', '\x1F', '\x22', '\x3C', '\x3E', '\x7C', ':', '*', '?', '\\', '/' };
  49. }
  50. }
  51. public char DirectorySeparatorChar
  52. {
  53. get
  54. {
  55. return Path.DirectorySeparatorChar;
  56. }
  57. }
  58. public char PathSeparator
  59. {
  60. get
  61. {
  62. return Path.PathSeparator;
  63. }
  64. }
  65. public string GetFullPath(string path)
  66. {
  67. return Path.GetFullPath(path);
  68. }
  69. /// <summary>
  70. /// Determines whether the specified filename is shortcut.
  71. /// </summary>
  72. /// <param name="filename">The filename.</param>
  73. /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
  74. /// <exception cref="System.ArgumentNullException">filename</exception>
  75. public virtual bool IsShortcut(string filename)
  76. {
  77. if (string.IsNullOrEmpty(filename))
  78. {
  79. throw new ArgumentNullException("filename");
  80. }
  81. var extension = Path.GetExtension(filename);
  82. return _shortcutHandlers.Any(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
  83. }
  84. /// <summary>
  85. /// Resolves the shortcut.
  86. /// </summary>
  87. /// <param name="filename">The filename.</param>
  88. /// <returns>System.String.</returns>
  89. /// <exception cref="System.ArgumentNullException">filename</exception>
  90. public virtual string ResolveShortcut(string filename)
  91. {
  92. if (string.IsNullOrEmpty(filename))
  93. {
  94. throw new ArgumentNullException("filename");
  95. }
  96. var extension = Path.GetExtension(filename);
  97. var handler = _shortcutHandlers.FirstOrDefault(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
  98. if (handler != null)
  99. {
  100. return handler.Resolve(filename);
  101. }
  102. return null;
  103. }
  104. /// <summary>
  105. /// Creates the shortcut.
  106. /// </summary>
  107. /// <param name="shortcutPath">The shortcut path.</param>
  108. /// <param name="target">The target.</param>
  109. /// <exception cref="System.ArgumentNullException">
  110. /// shortcutPath
  111. /// or
  112. /// target
  113. /// </exception>
  114. public void CreateShortcut(string shortcutPath, string target)
  115. {
  116. if (string.IsNullOrEmpty(shortcutPath))
  117. {
  118. throw new ArgumentNullException("shortcutPath");
  119. }
  120. if (string.IsNullOrEmpty(target))
  121. {
  122. throw new ArgumentNullException("target");
  123. }
  124. var extension = Path.GetExtension(shortcutPath);
  125. var handler = _shortcutHandlers.FirstOrDefault(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
  126. if (handler != null)
  127. {
  128. handler.Create(shortcutPath, target);
  129. }
  130. else
  131. {
  132. throw new NotImplementedException();
  133. }
  134. }
  135. /// <summary>
  136. /// Returns a <see cref="FileSystemMetadata"/> object for the specified file or directory path.
  137. /// </summary>
  138. /// <param name="path">A path to a file or directory.</param>
  139. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  140. /// <remarks>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> object's
  141. /// <see cref="FileSystemMetadata.IsDirectory"/> property will be set to true and all other properties will reflect the properties of the directory.</remarks>
  142. public FileSystemMetadata GetFileSystemInfo(string path)
  143. {
  144. if (string.IsNullOrEmpty(path))
  145. {
  146. throw new ArgumentNullException("path");
  147. }
  148. // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists
  149. if (Path.HasExtension(path))
  150. {
  151. var fileInfo = new FileInfo(path);
  152. if (fileInfo.Exists)
  153. {
  154. return GetFileSystemMetadata(fileInfo);
  155. }
  156. return GetFileSystemMetadata(new DirectoryInfo(path));
  157. }
  158. else
  159. {
  160. var fileInfo = new DirectoryInfo(path);
  161. if (fileInfo.Exists)
  162. {
  163. return GetFileSystemMetadata(fileInfo);
  164. }
  165. return GetFileSystemMetadata(new FileInfo(path));
  166. }
  167. }
  168. /// <summary>
  169. /// Returns a <see cref="FileSystemMetadata"/> object for the specified file path.
  170. /// </summary>
  171. /// <param name="path">A path to a file.</param>
  172. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  173. /// <remarks><para>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> object's
  174. /// <see cref="FileSystemMetadata.IsDirectory"/> property and the <see cref="FileSystemMetadata.Exists"/> property will both be set to false.</para>
  175. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></remarks>
  176. public FileSystemMetadata GetFileInfo(string path)
  177. {
  178. if (string.IsNullOrEmpty(path))
  179. {
  180. throw new ArgumentNullException("path");
  181. }
  182. var fileInfo = new FileInfo(path);
  183. return GetFileSystemMetadata(fileInfo);
  184. }
  185. /// <summary>
  186. /// Returns a <see cref="FileSystemMetadata"/> object for the specified directory path.
  187. /// </summary>
  188. /// <param name="path">A path to a directory.</param>
  189. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  190. /// <remarks><para>If the specified path points to a file, the returned <see cref="FileSystemMetadata"/> object's
  191. /// <see cref="FileSystemMetadata.IsDirectory"/> property will be set to true and the <see cref="FileSystemMetadata.Exists"/> property will be set to false.</para>
  192. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></remarks>
  193. public FileSystemMetadata GetDirectoryInfo(string path)
  194. {
  195. if (string.IsNullOrEmpty(path))
  196. {
  197. throw new ArgumentNullException("path");
  198. }
  199. var fileInfo = new DirectoryInfo(path);
  200. return GetFileSystemMetadata(fileInfo);
  201. }
  202. private FileSystemMetadata GetFileSystemMetadata(FileSystemInfo info)
  203. {
  204. var result = new FileSystemMetadata();
  205. result.Exists = info.Exists;
  206. result.FullName = info.FullName;
  207. result.Extension = info.Extension;
  208. result.Name = info.Name;
  209. if (result.Exists)
  210. {
  211. var attributes = info.Attributes;
  212. result.IsDirectory = info is DirectoryInfo || (attributes & FileAttributes.Directory) == FileAttributes.Directory;
  213. result.IsHidden = (attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
  214. result.IsReadOnly = (attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
  215. var fileInfo = info as FileInfo;
  216. if (fileInfo != null)
  217. {
  218. result.Length = fileInfo.Length;
  219. result.DirectoryName = fileInfo.DirectoryName;
  220. }
  221. result.CreationTimeUtc = GetCreationTimeUtc(info);
  222. result.LastWriteTimeUtc = GetLastWriteTimeUtc(info);
  223. }
  224. else
  225. {
  226. result.IsDirectory = info is DirectoryInfo;
  227. }
  228. return result;
  229. }
  230. /// <summary>
  231. /// The space char
  232. /// </summary>
  233. private const char SpaceChar = ' ';
  234. /// <summary>
  235. /// Takes a filename and removes invalid characters
  236. /// </summary>
  237. /// <param name="filename">The filename.</param>
  238. /// <returns>System.String.</returns>
  239. /// <exception cref="System.ArgumentNullException">filename</exception>
  240. public string GetValidFilename(string filename)
  241. {
  242. if (string.IsNullOrEmpty(filename))
  243. {
  244. throw new ArgumentNullException("filename");
  245. }
  246. var builder = new StringBuilder(filename);
  247. foreach (var c in _invalidFileNameChars)
  248. {
  249. builder = builder.Replace(c, SpaceChar);
  250. }
  251. return builder.ToString();
  252. }
  253. /// <summary>
  254. /// Gets the creation time UTC.
  255. /// </summary>
  256. /// <param name="info">The info.</param>
  257. /// <returns>DateTime.</returns>
  258. public DateTime GetCreationTimeUtc(FileSystemInfo info)
  259. {
  260. // This could throw an error on some file systems that have dates out of range
  261. try
  262. {
  263. return info.CreationTimeUtc;
  264. }
  265. catch (Exception ex)
  266. {
  267. Logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
  268. return DateTime.MinValue;
  269. }
  270. }
  271. /// <summary>
  272. /// Gets the creation time UTC.
  273. /// </summary>
  274. /// <param name="path">The path.</param>
  275. /// <returns>DateTime.</returns>
  276. public DateTime GetCreationTimeUtc(string path)
  277. {
  278. return GetCreationTimeUtc(GetFileSystemInfo(path));
  279. }
  280. public DateTime GetCreationTimeUtc(FileSystemMetadata info)
  281. {
  282. return info.CreationTimeUtc;
  283. }
  284. public DateTime GetLastWriteTimeUtc(FileSystemMetadata info)
  285. {
  286. return info.LastWriteTimeUtc;
  287. }
  288. /// <summary>
  289. /// Gets the creation time UTC.
  290. /// </summary>
  291. /// <param name="info">The info.</param>
  292. /// <returns>DateTime.</returns>
  293. public DateTime GetLastWriteTimeUtc(FileSystemInfo info)
  294. {
  295. // This could throw an error on some file systems that have dates out of range
  296. try
  297. {
  298. return info.LastWriteTimeUtc;
  299. }
  300. catch (Exception ex)
  301. {
  302. Logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
  303. return DateTime.MinValue;
  304. }
  305. }
  306. /// <summary>
  307. /// Gets the last write time UTC.
  308. /// </summary>
  309. /// <param name="path">The path.</param>
  310. /// <returns>DateTime.</returns>
  311. public DateTime GetLastWriteTimeUtc(string path)
  312. {
  313. return GetLastWriteTimeUtc(GetFileSystemInfo(path));
  314. }
  315. /// <summary>
  316. /// Gets the file stream.
  317. /// </summary>
  318. /// <param name="path">The path.</param>
  319. /// <param name="mode">The mode.</param>
  320. /// <param name="access">The access.</param>
  321. /// <param name="share">The share.</param>
  322. /// <param name="isAsync">if set to <c>true</c> [is asynchronous].</param>
  323. /// <returns>FileStream.</returns>
  324. public Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false)
  325. {
  326. if (_supportsAsyncFileStreams && isAsync)
  327. {
  328. return new FileStream(path, GetFileMode(mode), GetFileAccess(access), GetFileShare(share), 262144, true);
  329. }
  330. return new FileStream(path, GetFileMode(mode), GetFileAccess(access), GetFileShare(share), 262144);
  331. }
  332. private FileMode GetFileMode(FileOpenMode mode)
  333. {
  334. switch (mode)
  335. {
  336. case FileOpenMode.Append:
  337. return FileMode.Append;
  338. case FileOpenMode.Create:
  339. return FileMode.Create;
  340. case FileOpenMode.CreateNew:
  341. return FileMode.CreateNew;
  342. case FileOpenMode.Open:
  343. return FileMode.Open;
  344. case FileOpenMode.OpenOrCreate:
  345. return FileMode.OpenOrCreate;
  346. case FileOpenMode.Truncate:
  347. return FileMode.Truncate;
  348. default:
  349. throw new Exception("Unrecognized FileOpenMode");
  350. }
  351. }
  352. private FileAccess GetFileAccess(FileAccessMode mode)
  353. {
  354. switch (mode)
  355. {
  356. case FileAccessMode.ReadWrite:
  357. return FileAccess.ReadWrite;
  358. case FileAccessMode.Write:
  359. return FileAccess.Write;
  360. case FileAccessMode.Read:
  361. return FileAccess.Read;
  362. default:
  363. throw new Exception("Unrecognized FileAccessMode");
  364. }
  365. }
  366. private FileShare GetFileShare(FileShareMode mode)
  367. {
  368. switch (mode)
  369. {
  370. case FileShareMode.ReadWrite:
  371. return FileShare.ReadWrite;
  372. case FileShareMode.Write:
  373. return FileShare.Write;
  374. case FileShareMode.Read:
  375. return FileShare.Read;
  376. case FileShareMode.None:
  377. return FileShare.None;
  378. default:
  379. throw new Exception("Unrecognized FileShareMode");
  380. }
  381. }
  382. public void SetHidden(string path, bool isHidden)
  383. {
  384. var info = GetFileInfo(path);
  385. if (info.Exists && info.IsHidden != isHidden)
  386. {
  387. if (isHidden)
  388. {
  389. File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
  390. }
  391. else
  392. {
  393. FileAttributes attributes = File.GetAttributes(path);
  394. attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
  395. File.SetAttributes(path, attributes);
  396. }
  397. }
  398. }
  399. public void SetReadOnly(string path, bool isReadOnly)
  400. {
  401. var info = GetFileInfo(path);
  402. if (info.Exists && info.IsReadOnly != isReadOnly)
  403. {
  404. if (isReadOnly)
  405. {
  406. File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);
  407. }
  408. else
  409. {
  410. FileAttributes attributes = File.GetAttributes(path);
  411. attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
  412. File.SetAttributes(path, attributes);
  413. }
  414. }
  415. }
  416. private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
  417. {
  418. return attributes & ~attributesToRemove;
  419. }
  420. /// <summary>
  421. /// Swaps the files.
  422. /// </summary>
  423. /// <param name="file1">The file1.</param>
  424. /// <param name="file2">The file2.</param>
  425. public void SwapFiles(string file1, string file2)
  426. {
  427. if (string.IsNullOrEmpty(file1))
  428. {
  429. throw new ArgumentNullException("file1");
  430. }
  431. if (string.IsNullOrEmpty(file2))
  432. {
  433. throw new ArgumentNullException("file2");
  434. }
  435. var temp1 = Path.Combine(_tempPath, Guid.NewGuid().ToString("N"));
  436. // Copying over will fail against hidden files
  437. SetHidden(file1, false);
  438. SetHidden(file2, false);
  439. Directory.CreateDirectory(_tempPath);
  440. CopyFile(file1, temp1, true);
  441. CopyFile(file2, file1, true);
  442. CopyFile(temp1, file2, true);
  443. }
  444. public bool AreEqual(string path1, string path2)
  445. {
  446. if (path1 == null && path2 == null)
  447. {
  448. return true;
  449. }
  450. if (path1 == null || path2 == null)
  451. {
  452. return false;
  453. }
  454. path1 = path1.TrimEnd(DirectorySeparatorChar);
  455. path2 = path2.TrimEnd(DirectorySeparatorChar);
  456. return string.Equals(path1, path2, StringComparison.OrdinalIgnoreCase);
  457. }
  458. public bool ContainsSubPath(string parentPath, string path)
  459. {
  460. if (string.IsNullOrEmpty(parentPath))
  461. {
  462. throw new ArgumentNullException("parentPath");
  463. }
  464. if (string.IsNullOrEmpty(path))
  465. {
  466. throw new ArgumentNullException("path");
  467. }
  468. return path.IndexOf(parentPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) != -1;
  469. }
  470. public bool IsRootPath(string path)
  471. {
  472. if (string.IsNullOrEmpty(path))
  473. {
  474. throw new ArgumentNullException("path");
  475. }
  476. var parent = Path.GetDirectoryName(path);
  477. if (!string.IsNullOrEmpty(parent))
  478. {
  479. return false;
  480. }
  481. return true;
  482. }
  483. public string NormalizePath(string path)
  484. {
  485. if (string.IsNullOrEmpty(path))
  486. {
  487. throw new ArgumentNullException("path");
  488. }
  489. if (path.EndsWith(":\\", StringComparison.OrdinalIgnoreCase))
  490. {
  491. return path;
  492. }
  493. return path.TrimEnd(Path.DirectorySeparatorChar);
  494. }
  495. public string GetFileNameWithoutExtension(FileSystemMetadata info)
  496. {
  497. if (info.IsDirectory)
  498. {
  499. return info.Name;
  500. }
  501. return Path.GetFileNameWithoutExtension(info.FullName);
  502. }
  503. public string GetFileNameWithoutExtension(string path)
  504. {
  505. return Path.GetFileNameWithoutExtension(path);
  506. }
  507. public bool IsPathFile(string path)
  508. {
  509. if (string.IsNullOrWhiteSpace(path))
  510. {
  511. throw new ArgumentNullException("path");
  512. }
  513. // Cannot use Path.IsPathRooted because it returns false under mono when using windows-based paths, e.g. C:\\
  514. if (path.IndexOf("://", StringComparison.OrdinalIgnoreCase) != -1 &&
  515. !path.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
  516. {
  517. return false;
  518. }
  519. return true;
  520. //return Path.IsPathRooted(path);
  521. }
  522. public void DeleteFile(string path)
  523. {
  524. var fileInfo = GetFileInfo(path);
  525. if (fileInfo.Exists)
  526. {
  527. if (fileInfo.IsHidden)
  528. {
  529. SetHidden(path, false);
  530. }
  531. if (fileInfo.IsReadOnly)
  532. {
  533. SetReadOnly(path, false);
  534. }
  535. }
  536. File.Delete(path);
  537. }
  538. public void DeleteDirectory(string path, bool recursive)
  539. {
  540. Directory.Delete(path, recursive);
  541. }
  542. public void CreateDirectory(string path)
  543. {
  544. Directory.CreateDirectory(path);
  545. }
  546. public List<FileSystemMetadata> GetDrives()
  547. {
  548. // Only include drives in the ready state or this method could end up being very slow, waiting for drives to timeout
  549. return DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => new FileSystemMetadata
  550. {
  551. Name = GetName(d),
  552. FullName = d.RootDirectory.FullName,
  553. IsDirectory = true
  554. }).ToList();
  555. }
  556. private string GetName(DriveInfo drive)
  557. {
  558. return drive.Name;
  559. }
  560. public IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false)
  561. {
  562. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  563. return ToMetadata(path, new DirectoryInfo(path).EnumerateDirectories("*", searchOption));
  564. }
  565. public IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false)
  566. {
  567. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  568. return ToMetadata(path, new DirectoryInfo(path).EnumerateFiles("*", searchOption));
  569. }
  570. public IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false)
  571. {
  572. var directoryInfo = new DirectoryInfo(path);
  573. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  574. if (EnableFileSystemRequestConcat)
  575. {
  576. return ToMetadata(path, directoryInfo.EnumerateDirectories("*", searchOption))
  577. .Concat(ToMetadata(path, directoryInfo.EnumerateFiles("*", searchOption)));
  578. }
  579. return ToMetadata(path, directoryInfo.EnumerateFileSystemInfos("*", searchOption));
  580. }
  581. private IEnumerable<FileSystemMetadata> ToMetadata(string parentPath, IEnumerable<FileSystemInfo> infos)
  582. {
  583. return infos.Select(GetFileSystemMetadata);
  584. }
  585. public string[] ReadAllLines(string path)
  586. {
  587. return File.ReadAllLines(path);
  588. }
  589. public void WriteAllLines(string path, IEnumerable<string> lines)
  590. {
  591. File.WriteAllLines(path, lines);
  592. }
  593. public Stream OpenRead(string path)
  594. {
  595. return File.OpenRead(path);
  596. }
  597. public void CopyFile(string source, string target, bool overwrite)
  598. {
  599. File.Copy(source, target, overwrite);
  600. }
  601. public void MoveFile(string source, string target)
  602. {
  603. File.Move(source, target);
  604. }
  605. public void MoveDirectory(string source, string target)
  606. {
  607. Directory.Move(source, target);
  608. }
  609. public bool DirectoryExists(string path)
  610. {
  611. return Directory.Exists(path);
  612. }
  613. public bool FileExists(string path)
  614. {
  615. return File.Exists(path);
  616. }
  617. public string ReadAllText(string path)
  618. {
  619. return File.ReadAllText(path);
  620. }
  621. public byte[] ReadAllBytes(string path)
  622. {
  623. return File.ReadAllBytes(path);
  624. }
  625. public void WriteAllText(string path, string text, Encoding encoding)
  626. {
  627. File.WriteAllText(path, text, encoding);
  628. }
  629. public void WriteAllText(string path, string text)
  630. {
  631. File.WriteAllText(path, text);
  632. }
  633. public void WriteAllBytes(string path, byte[] bytes)
  634. {
  635. File.WriteAllBytes(path, bytes);
  636. }
  637. public string ReadAllText(string path, Encoding encoding)
  638. {
  639. return File.ReadAllText(path, encoding);
  640. }
  641. public IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false)
  642. {
  643. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  644. return Directory.EnumerateDirectories(path, "*", searchOption);
  645. }
  646. public IEnumerable<string> GetFilePaths(string path, bool recursive = false)
  647. {
  648. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  649. return Directory.EnumerateFiles(path, "*", searchOption);
  650. }
  651. public IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false)
  652. {
  653. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  654. return Directory.EnumerateFileSystemEntries(path, "*", searchOption);
  655. }
  656. public virtual void SetExecutable(string path)
  657. {
  658. }
  659. }
  660. }