ManagedFileSystem.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. SetHidden(file1, false);
  433. SetHidden(file2, false);
  434. CopyFile(file1, temp1, true);
  435. CopyFile(file2, file1, true);
  436. CopyFile(temp1, file2, true);
  437. }
  438. public bool AreEqual(string path1, string path2)
  439. {
  440. if (path1 == null && path2 == null)
  441. {
  442. return true;
  443. }
  444. if (path1 == null || path2 == null)
  445. {
  446. return false;
  447. }
  448. path1 = path1.TrimEnd(DirectorySeparatorChar);
  449. path2 = path2.TrimEnd(DirectorySeparatorChar);
  450. return string.Equals(path1, path2, StringComparison.OrdinalIgnoreCase);
  451. }
  452. public bool ContainsSubPath(string parentPath, string path)
  453. {
  454. if (string.IsNullOrEmpty(parentPath))
  455. {
  456. throw new ArgumentNullException("parentPath");
  457. }
  458. if (string.IsNullOrEmpty(path))
  459. {
  460. throw new ArgumentNullException("path");
  461. }
  462. return path.IndexOf(parentPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) != -1;
  463. }
  464. public bool IsRootPath(string path)
  465. {
  466. if (string.IsNullOrEmpty(path))
  467. {
  468. throw new ArgumentNullException("path");
  469. }
  470. var parent = Path.GetDirectoryName(path);
  471. if (!string.IsNullOrEmpty(parent))
  472. {
  473. return false;
  474. }
  475. return true;
  476. }
  477. public string NormalizePath(string path)
  478. {
  479. if (string.IsNullOrEmpty(path))
  480. {
  481. throw new ArgumentNullException("path");
  482. }
  483. if (path.EndsWith(":\\", StringComparison.OrdinalIgnoreCase))
  484. {
  485. return path;
  486. }
  487. return path.TrimEnd(Path.DirectorySeparatorChar);
  488. }
  489. public string GetFileNameWithoutExtension(FileSystemMetadata info)
  490. {
  491. if (info.IsDirectory)
  492. {
  493. return info.Name;
  494. }
  495. return Path.GetFileNameWithoutExtension(info.FullName);
  496. }
  497. public string GetFileNameWithoutExtension(string path)
  498. {
  499. return Path.GetFileNameWithoutExtension(path);
  500. }
  501. public bool IsPathFile(string path)
  502. {
  503. if (string.IsNullOrWhiteSpace(path))
  504. {
  505. throw new ArgumentNullException("path");
  506. }
  507. // Cannot use Path.IsPathRooted because it returns false under mono when using windows-based paths, e.g. C:\\
  508. if (path.IndexOf("://", StringComparison.OrdinalIgnoreCase) != -1 &&
  509. !path.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
  510. {
  511. return false;
  512. }
  513. return true;
  514. //return Path.IsPathRooted(path);
  515. }
  516. public void DeleteFile(string path)
  517. {
  518. var fileInfo = GetFileInfo(path);
  519. if (fileInfo.Exists)
  520. {
  521. if (fileInfo.IsHidden)
  522. {
  523. SetHidden(path, false);
  524. }
  525. if (fileInfo.IsReadOnly)
  526. {
  527. SetReadOnly(path, false);
  528. }
  529. }
  530. File.Delete(path);
  531. }
  532. public void DeleteDirectory(string path, bool recursive)
  533. {
  534. Directory.Delete(path, recursive);
  535. }
  536. public void CreateDirectory(string path)
  537. {
  538. Directory.CreateDirectory(path);
  539. }
  540. public List<FileSystemMetadata> GetDrives()
  541. {
  542. // Only include drives in the ready state or this method could end up being very slow, waiting for drives to timeout
  543. return DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => new FileSystemMetadata
  544. {
  545. Name = GetName(d),
  546. FullName = d.RootDirectory.FullName,
  547. IsDirectory = true
  548. }).ToList();
  549. }
  550. private string GetName(DriveInfo drive)
  551. {
  552. return drive.Name;
  553. }
  554. public IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false)
  555. {
  556. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  557. return ToMetadata(path, new DirectoryInfo(path).EnumerateDirectories("*", searchOption));
  558. }
  559. public IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false)
  560. {
  561. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  562. return ToMetadata(path, new DirectoryInfo(path).EnumerateFiles("*", searchOption));
  563. }
  564. public IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false)
  565. {
  566. var directoryInfo = new DirectoryInfo(path);
  567. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  568. if (EnableFileSystemRequestConcat)
  569. {
  570. return ToMetadata(path, directoryInfo.EnumerateDirectories("*", searchOption))
  571. .Concat(ToMetadata(path, directoryInfo.EnumerateFiles("*", searchOption)));
  572. }
  573. return ToMetadata(path, directoryInfo.EnumerateFileSystemInfos("*", searchOption));
  574. }
  575. private IEnumerable<FileSystemMetadata> ToMetadata(string parentPath, IEnumerable<FileSystemInfo> infos)
  576. {
  577. return infos.Select(i =>
  578. {
  579. try
  580. {
  581. return GetFileSystemMetadata(i);
  582. }
  583. catch (PathTooLongException)
  584. {
  585. // Can't log using the FullName because it will throw the PathTooLongExceptiona again
  586. //Logger.Warn("Path too long: {0}", i.FullName);
  587. Logger.Warn("File or directory path too long. Parent folder: {0}", parentPath);
  588. return null;
  589. }
  590. }).Where(i => i != null);
  591. }
  592. public string[] ReadAllLines(string path)
  593. {
  594. return File.ReadAllLines(path);
  595. }
  596. public void WriteAllLines(string path, IEnumerable<string> lines)
  597. {
  598. File.WriteAllLines(path, lines);
  599. }
  600. public Stream OpenRead(string path)
  601. {
  602. return File.OpenRead(path);
  603. }
  604. public void CopyFile(string source, string target, bool overwrite)
  605. {
  606. File.Copy(source, target, overwrite);
  607. }
  608. public void MoveFile(string source, string target)
  609. {
  610. File.Move(source, target);
  611. }
  612. public void MoveDirectory(string source, string target)
  613. {
  614. Directory.Move(source, target);
  615. }
  616. public bool DirectoryExists(string path)
  617. {
  618. return Directory.Exists(path);
  619. }
  620. public bool FileExists(string path)
  621. {
  622. return File.Exists(path);
  623. }
  624. public string ReadAllText(string path)
  625. {
  626. return File.ReadAllText(path);
  627. }
  628. public byte[] ReadAllBytes(string path)
  629. {
  630. return File.ReadAllBytes(path);
  631. }
  632. public void WriteAllText(string path, string text, Encoding encoding)
  633. {
  634. File.WriteAllText(path, text, encoding);
  635. }
  636. public void WriteAllText(string path, string text)
  637. {
  638. File.WriteAllText(path, text);
  639. }
  640. public void WriteAllBytes(string path, byte[] bytes)
  641. {
  642. File.WriteAllBytes(path, bytes);
  643. }
  644. public string ReadAllText(string path, Encoding encoding)
  645. {
  646. return File.ReadAllText(path, encoding);
  647. }
  648. public IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false)
  649. {
  650. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  651. return Directory.EnumerateDirectories(path, "*", searchOption);
  652. }
  653. public IEnumerable<string> GetFilePaths(string path, bool recursive = false)
  654. {
  655. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  656. return Directory.EnumerateFiles(path, "*", searchOption);
  657. }
  658. public IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false)
  659. {
  660. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  661. return Directory.EnumerateFileSystemEntries(path, "*", searchOption);
  662. }
  663. public virtual void SetExecutable(string path)
  664. {
  665. }
  666. }
  667. }