ManagedFileSystem.cs 26 KB

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