ManagedFileSystem.cs 26 KB

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