ManagedFileSystem.cs 26 KB

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