ManagedFileSystem.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using MediaBrowser.Common.Configuration;
  10. using MediaBrowser.Model.IO;
  11. using MediaBrowser.Model.System;
  12. using Microsoft.Extensions.Logging;
  13. using OperatingSystem = MediaBrowser.Common.System.OperatingSystem;
  14. namespace Emby.Server.Implementations.IO
  15. {
  16. /// <summary>
  17. /// Class ManagedFileSystem.
  18. /// </summary>
  19. public class ManagedFileSystem : IFileSystem
  20. {
  21. protected ILogger<ManagedFileSystem> Logger;
  22. private readonly List<IShortcutHandler> _shortcutHandlers = new List<IShortcutHandler>();
  23. private readonly string _tempPath;
  24. private readonly bool _isEnvironmentCaseInsensitive;
  25. public ManagedFileSystem(
  26. ILogger<ManagedFileSystem> logger,
  27. IApplicationPaths applicationPaths)
  28. {
  29. Logger = logger;
  30. _tempPath = applicationPaths.TempDirectory;
  31. _isEnvironmentCaseInsensitive = OperatingSystem.Id == OperatingSystemId.Windows;
  32. }
  33. public virtual void AddShortcutHandler(IShortcutHandler handler)
  34. {
  35. _shortcutHandlers.Add(handler);
  36. }
  37. /// <summary>
  38. /// Determines whether the specified filename is shortcut.
  39. /// </summary>
  40. /// <param name="filename">The filename.</param>
  41. /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
  42. /// <exception cref="ArgumentNullException">filename</exception>
  43. public virtual bool IsShortcut(string filename)
  44. {
  45. if (string.IsNullOrEmpty(filename))
  46. {
  47. throw new ArgumentNullException(nameof(filename));
  48. }
  49. var extension = Path.GetExtension(filename);
  50. return _shortcutHandlers.Any(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
  51. }
  52. /// <summary>
  53. /// Resolves the shortcut.
  54. /// </summary>
  55. /// <param name="filename">The filename.</param>
  56. /// <returns>System.String.</returns>
  57. /// <exception cref="ArgumentNullException">filename</exception>
  58. public virtual string ResolveShortcut(string filename)
  59. {
  60. if (string.IsNullOrEmpty(filename))
  61. {
  62. throw new ArgumentNullException(nameof(filename));
  63. }
  64. var extension = Path.GetExtension(filename);
  65. var handler = _shortcutHandlers.FirstOrDefault(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
  66. return handler?.Resolve(filename);
  67. }
  68. public virtual string MakeAbsolutePath(string folderPath, string filePath)
  69. {
  70. // path is actually a stream
  71. if (string.IsNullOrWhiteSpace(filePath) || filePath.Contains("://", StringComparison.Ordinal))
  72. {
  73. return filePath;
  74. }
  75. if (filePath.Length > 3 && filePath[1] == ':' && filePath[2] == '/')
  76. {
  77. // absolute local path
  78. return filePath;
  79. }
  80. // unc path
  81. if (filePath.StartsWith("\\\\", StringComparison.Ordinal))
  82. {
  83. return filePath;
  84. }
  85. var firstChar = filePath[0];
  86. if (firstChar == '/')
  87. {
  88. // for this we don't really know
  89. return filePath;
  90. }
  91. // relative path
  92. if (firstChar == '\\')
  93. {
  94. filePath = filePath.Substring(1);
  95. }
  96. try
  97. {
  98. return Path.GetFullPath(Path.Combine(folderPath, filePath));
  99. }
  100. catch (ArgumentException)
  101. {
  102. return filePath;
  103. }
  104. catch (PathTooLongException)
  105. {
  106. return filePath;
  107. }
  108. catch (NotSupportedException)
  109. {
  110. return filePath;
  111. }
  112. }
  113. /// <summary>
  114. /// Creates the shortcut.
  115. /// </summary>
  116. /// <param name="shortcutPath">The shortcut path.</param>
  117. /// <param name="target">The target.</param>
  118. /// <exception cref="ArgumentNullException">The shortcutPath or target is null.</exception>
  119. public virtual void CreateShortcut(string shortcutPath, string target)
  120. {
  121. if (string.IsNullOrEmpty(shortcutPath))
  122. {
  123. throw new ArgumentNullException(nameof(shortcutPath));
  124. }
  125. if (string.IsNullOrEmpty(target))
  126. {
  127. throw new ArgumentNullException(nameof(target));
  128. }
  129. var extension = Path.GetExtension(shortcutPath);
  130. var handler = _shortcutHandlers.Find(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
  131. if (handler != null)
  132. {
  133. handler.Create(shortcutPath, target);
  134. }
  135. else
  136. {
  137. throw new NotImplementedException();
  138. }
  139. }
  140. /// <summary>
  141. /// Returns a <see cref="FileSystemMetadata"/> object for the specified file or directory path.
  142. /// </summary>
  143. /// <param name="path">A path to a file or directory.</param>
  144. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  145. /// <remarks>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> object's
  146. /// <see cref="FileSystemMetadata.IsDirectory"/> property will be set to true and all other properties will reflect the properties of the directory.</remarks>
  147. public virtual FileSystemMetadata GetFileSystemInfo(string path)
  148. {
  149. // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists
  150. if (Path.HasExtension(path))
  151. {
  152. var fileInfo = new FileInfo(path);
  153. if (fileInfo.Exists)
  154. {
  155. return GetFileSystemMetadata(fileInfo);
  156. }
  157. return GetFileSystemMetadata(new DirectoryInfo(path));
  158. }
  159. else
  160. {
  161. var fileInfo = new DirectoryInfo(path);
  162. if (fileInfo.Exists)
  163. {
  164. return GetFileSystemMetadata(fileInfo);
  165. }
  166. return GetFileSystemMetadata(new FileInfo(path));
  167. }
  168. }
  169. /// <summary>
  170. /// Returns a <see cref="FileSystemMetadata"/> object for the specified file path.
  171. /// </summary>
  172. /// <param name="path">A path to a file.</param>
  173. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  174. /// <remarks><para>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> object's
  175. /// <see cref="FileSystemMetadata.IsDirectory"/> property and the <see cref="FileSystemMetadata.Exists"/> property will both be set to false.</para>
  176. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></remarks>
  177. public virtual FileSystemMetadata GetFileInfo(string 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 virtual FileSystemMetadata GetDirectoryInfo(string path)
  191. {
  192. var fileInfo = new DirectoryInfo(path);
  193. return GetFileSystemMetadata(fileInfo);
  194. }
  195. private FileSystemMetadata GetFileSystemMetadata(FileSystemInfo info)
  196. {
  197. var result = new FileSystemMetadata
  198. {
  199. Exists = info.Exists,
  200. FullName = info.FullName,
  201. Extension = info.Extension,
  202. Name = info.Name
  203. };
  204. if (result.Exists)
  205. {
  206. result.IsDirectory = info is DirectoryInfo || (info.Attributes & FileAttributes.Directory) == FileAttributes.Directory;
  207. // if (!result.IsDirectory)
  208. //{
  209. // result.IsHidden = (info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
  210. //}
  211. if (info is FileInfo fileInfo)
  212. {
  213. result.Length = fileInfo.Length;
  214. // Issue #2354 get the size of files behind symbolic links
  215. if (fileInfo.Attributes.HasFlag(FileAttributes.ReparsePoint))
  216. {
  217. using (Stream thisFileStream = File.OpenRead(fileInfo.FullName))
  218. {
  219. result.Length = thisFileStream.Length;
  220. }
  221. }
  222. result.DirectoryName = fileInfo.DirectoryName;
  223. }
  224. result.CreationTimeUtc = GetCreationTimeUtc(info);
  225. result.LastWriteTimeUtc = GetLastWriteTimeUtc(info);
  226. }
  227. else
  228. {
  229. result.IsDirectory = info is DirectoryInfo;
  230. }
  231. return result;
  232. }
  233. private static ExtendedFileSystemInfo GetExtendedFileSystemInfo(string path)
  234. {
  235. var result = new ExtendedFileSystemInfo();
  236. var info = new FileInfo(path);
  237. if (info.Exists)
  238. {
  239. result.Exists = true;
  240. var attributes = info.Attributes;
  241. result.IsHidden = (attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
  242. result.IsReadOnly = (attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
  243. }
  244. return result;
  245. }
  246. /// <summary>
  247. /// Takes a filename and removes invalid characters.
  248. /// </summary>
  249. /// <param name="filename">The filename.</param>
  250. /// <returns>System.String.</returns>
  251. /// <exception cref="ArgumentNullException">The filename is null.</exception>
  252. public virtual string GetValidFilename(string filename)
  253. {
  254. var builder = new StringBuilder(filename);
  255. foreach (var c in Path.GetInvalidFileNameChars())
  256. {
  257. builder = builder.Replace(c, ' ');
  258. }
  259. return builder.ToString();
  260. }
  261. /// <summary>
  262. /// Gets the creation time UTC.
  263. /// </summary>
  264. /// <param name="info">The info.</param>
  265. /// <returns>DateTime.</returns>
  266. public DateTime GetCreationTimeUtc(FileSystemInfo info)
  267. {
  268. // This could throw an error on some file systems that have dates out of range
  269. try
  270. {
  271. return info.CreationTimeUtc;
  272. }
  273. catch (Exception ex)
  274. {
  275. Logger.LogError(ex, "Error determining CreationTimeUtc for {FullName}", info.FullName);
  276. return DateTime.MinValue;
  277. }
  278. }
  279. /// <summary>
  280. /// Gets the creation time UTC.
  281. /// </summary>
  282. /// <param name="path">The path.</param>
  283. /// <returns>DateTime.</returns>
  284. public virtual DateTime GetCreationTimeUtc(string path)
  285. {
  286. return GetCreationTimeUtc(GetFileSystemInfo(path));
  287. }
  288. public virtual DateTime GetCreationTimeUtc(FileSystemMetadata info)
  289. {
  290. return info.CreationTimeUtc;
  291. }
  292. public virtual DateTime GetLastWriteTimeUtc(FileSystemMetadata info)
  293. {
  294. return info.LastWriteTimeUtc;
  295. }
  296. /// <summary>
  297. /// Gets the creation time UTC.
  298. /// </summary>
  299. /// <param name="info">The info.</param>
  300. /// <returns>DateTime.</returns>
  301. public DateTime GetLastWriteTimeUtc(FileSystemInfo info)
  302. {
  303. // This could throw an error on some file systems that have dates out of range
  304. try
  305. {
  306. return info.LastWriteTimeUtc;
  307. }
  308. catch (Exception ex)
  309. {
  310. Logger.LogError(ex, "Error determining LastAccessTimeUtc for {FullName}", info.FullName);
  311. return DateTime.MinValue;
  312. }
  313. }
  314. /// <summary>
  315. /// Gets the last write time UTC.
  316. /// </summary>
  317. /// <param name="path">The path.</param>
  318. /// <returns>DateTime.</returns>
  319. public virtual DateTime GetLastWriteTimeUtc(string path)
  320. {
  321. return GetLastWriteTimeUtc(GetFileSystemInfo(path));
  322. }
  323. public virtual void SetHidden(string path, bool isHidden)
  324. {
  325. if (OperatingSystem.Id != OperatingSystemId.Windows)
  326. {
  327. return;
  328. }
  329. var info = GetExtendedFileSystemInfo(path);
  330. if (info.Exists && info.IsHidden != isHidden)
  331. {
  332. if (isHidden)
  333. {
  334. File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
  335. }
  336. else
  337. {
  338. var attributes = File.GetAttributes(path);
  339. attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
  340. File.SetAttributes(path, attributes);
  341. }
  342. }
  343. }
  344. public virtual void SetReadOnly(string path, bool isReadOnly)
  345. {
  346. if (OperatingSystem.Id != OperatingSystemId.Windows)
  347. {
  348. return;
  349. }
  350. var info = GetExtendedFileSystemInfo(path);
  351. if (info.Exists && info.IsReadOnly != isReadOnly)
  352. {
  353. if (isReadOnly)
  354. {
  355. File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);
  356. }
  357. else
  358. {
  359. var attributes = File.GetAttributes(path);
  360. attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
  361. File.SetAttributes(path, attributes);
  362. }
  363. }
  364. }
  365. public virtual void SetAttributes(string path, bool isHidden, bool isReadOnly)
  366. {
  367. if (OperatingSystem.Id != OperatingSystemId.Windows)
  368. {
  369. return;
  370. }
  371. var info = GetExtendedFileSystemInfo(path);
  372. if (!info.Exists)
  373. {
  374. return;
  375. }
  376. if (info.IsReadOnly == isReadOnly && info.IsHidden == isHidden)
  377. {
  378. return;
  379. }
  380. var attributes = File.GetAttributes(path);
  381. if (isReadOnly)
  382. {
  383. attributes = attributes | FileAttributes.ReadOnly;
  384. }
  385. else
  386. {
  387. attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
  388. }
  389. if (isHidden)
  390. {
  391. attributes = attributes | FileAttributes.Hidden;
  392. }
  393. else
  394. {
  395. attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
  396. }
  397. File.SetAttributes(path, attributes);
  398. }
  399. private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
  400. {
  401. return attributes & ~attributesToRemove;
  402. }
  403. /// <summary>
  404. /// Swaps the files.
  405. /// </summary>
  406. /// <param name="file1">The file1.</param>
  407. /// <param name="file2">The file2.</param>
  408. public virtual void SwapFiles(string file1, string file2)
  409. {
  410. if (string.IsNullOrEmpty(file1))
  411. {
  412. throw new ArgumentNullException(nameof(file1));
  413. }
  414. if (string.IsNullOrEmpty(file2))
  415. {
  416. throw new ArgumentNullException(nameof(file2));
  417. }
  418. var temp1 = Path.Combine(_tempPath, Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
  419. // Copying over will fail against hidden files
  420. SetHidden(file1, false);
  421. SetHidden(file2, false);
  422. Directory.CreateDirectory(_tempPath);
  423. File.Copy(file1, temp1, true);
  424. File.Copy(file2, file1, true);
  425. File.Copy(temp1, file2, true);
  426. }
  427. public virtual bool ContainsSubPath(string parentPath, string path)
  428. {
  429. if (string.IsNullOrEmpty(parentPath))
  430. {
  431. throw new ArgumentNullException(nameof(parentPath));
  432. }
  433. if (string.IsNullOrEmpty(path))
  434. {
  435. throw new ArgumentNullException(nameof(path));
  436. }
  437. var separatorChar = Path.DirectorySeparatorChar;
  438. return path.IndexOf(parentPath.TrimEnd(separatorChar) + separatorChar, StringComparison.OrdinalIgnoreCase) != -1;
  439. }
  440. public virtual bool IsRootPath(string path)
  441. {
  442. if (string.IsNullOrEmpty(path))
  443. {
  444. throw new ArgumentNullException(nameof(path));
  445. }
  446. var parent = Path.GetDirectoryName(path);
  447. if (!string.IsNullOrEmpty(parent))
  448. {
  449. return false;
  450. }
  451. return true;
  452. }
  453. public virtual string NormalizePath(string path)
  454. {
  455. if (string.IsNullOrEmpty(path))
  456. {
  457. throw new ArgumentNullException(nameof(path));
  458. }
  459. if (path.EndsWith(":\\", StringComparison.OrdinalIgnoreCase))
  460. {
  461. return path;
  462. }
  463. return path.TrimEnd(Path.DirectorySeparatorChar);
  464. }
  465. public virtual bool AreEqual(string path1, string path2)
  466. {
  467. if (path1 == null && path2 == null)
  468. {
  469. return true;
  470. }
  471. if (path1 == null || path2 == null)
  472. {
  473. return false;
  474. }
  475. return string.Equals(NormalizePath(path1), NormalizePath(path2), StringComparison.OrdinalIgnoreCase);
  476. }
  477. public virtual string GetFileNameWithoutExtension(FileSystemMetadata info)
  478. {
  479. if (info.IsDirectory)
  480. {
  481. return info.Name;
  482. }
  483. return Path.GetFileNameWithoutExtension(info.FullName);
  484. }
  485. public virtual bool IsPathFile(string path)
  486. {
  487. // Cannot use Path.IsPathRooted because it returns false under mono when using windows-based paths, e.g. C:\\
  488. if (path.IndexOf("://", StringComparison.OrdinalIgnoreCase) != -1 &&
  489. !path.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
  490. {
  491. return false;
  492. }
  493. return true;
  494. }
  495. public virtual void DeleteFile(string path)
  496. {
  497. SetAttributes(path, false, false);
  498. File.Delete(path);
  499. }
  500. public virtual List<FileSystemMetadata> GetDrives()
  501. {
  502. // check for ready state to avoid waiting for drives to timeout
  503. // some drives on linux have no actual size or are used for other purposes
  504. return DriveInfo.GetDrives().Where(d => d.IsReady && d.TotalSize != 0 && d.DriveType != DriveType.Ram)
  505. .Select(d => new FileSystemMetadata
  506. {
  507. Name = d.Name,
  508. FullName = d.RootDirectory.FullName,
  509. IsDirectory = true
  510. }).ToList();
  511. }
  512. public virtual IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false)
  513. {
  514. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  515. return ToMetadata(new DirectoryInfo(path).EnumerateDirectories("*", searchOption));
  516. }
  517. public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false)
  518. {
  519. return GetFiles(path, null, false, recursive);
  520. }
  521. public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, IReadOnlyList<string> extensions, bool enableCaseSensitiveExtensions, bool recursive = false)
  522. {
  523. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  524. // On linux and osx the search pattern is case sensitive
  525. // If we're OK with case-sensitivity, and we're only filtering for one extension, then use the native method
  526. if ((enableCaseSensitiveExtensions || _isEnvironmentCaseInsensitive) && extensions != null && extensions.Count == 1)
  527. {
  528. return ToMetadata(new DirectoryInfo(path).EnumerateFiles("*" + extensions[0], searchOption));
  529. }
  530. var files = new DirectoryInfo(path).EnumerateFiles("*", searchOption);
  531. if (extensions != null && extensions.Count > 0)
  532. {
  533. files = files.Where(i =>
  534. {
  535. var ext = i.Extension;
  536. if (ext == null)
  537. {
  538. return false;
  539. }
  540. return extensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
  541. });
  542. }
  543. return ToMetadata(files);
  544. }
  545. public virtual IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false)
  546. {
  547. var directoryInfo = new DirectoryInfo(path);
  548. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  549. return ToMetadata(directoryInfo.EnumerateDirectories("*", searchOption))
  550. .Concat(ToMetadata(directoryInfo.EnumerateFiles("*", searchOption)));
  551. }
  552. private IEnumerable<FileSystemMetadata> ToMetadata(IEnumerable<FileSystemInfo> infos)
  553. {
  554. return infos.Select(GetFileSystemMetadata);
  555. }
  556. public virtual IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false)
  557. {
  558. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  559. return Directory.EnumerateDirectories(path, "*", searchOption);
  560. }
  561. public virtual IEnumerable<string> GetFilePaths(string path, bool recursive = false)
  562. {
  563. return GetFilePaths(path, null, false, recursive);
  564. }
  565. public virtual IEnumerable<string> GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false)
  566. {
  567. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  568. // On linux and osx the search pattern is case sensitive
  569. // If we're OK with case-sensitivity, and we're only filtering for one extension, then use the native method
  570. if ((enableCaseSensitiveExtensions || _isEnvironmentCaseInsensitive) && extensions != null && extensions.Length == 1)
  571. {
  572. return Directory.EnumerateFiles(path, "*" + extensions[0], searchOption);
  573. }
  574. var files = Directory.EnumerateFiles(path, "*", searchOption);
  575. if (extensions != null && extensions.Length > 0)
  576. {
  577. files = files.Where(i =>
  578. {
  579. var ext = Path.GetExtension(i);
  580. if (ext == null)
  581. {
  582. return false;
  583. }
  584. return extensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
  585. });
  586. }
  587. return files;
  588. }
  589. public virtual IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false)
  590. {
  591. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  592. return Directory.EnumerateFileSystemEntries(path, "*", searchOption);
  593. }
  594. public virtual void SetExecutable(string path)
  595. {
  596. if (OperatingSystem.Id == OperatingSystemId.Darwin)
  597. {
  598. RunProcess("chmod", "+x \"" + path + "\"", Path.GetDirectoryName(path));
  599. }
  600. }
  601. private static void RunProcess(string path, string args, string workingDirectory)
  602. {
  603. using (var process = Process.Start(new ProcessStartInfo
  604. {
  605. Arguments = args,
  606. FileName = path,
  607. CreateNoWindow = true,
  608. WorkingDirectory = workingDirectory,
  609. WindowStyle = ProcessWindowStyle.Normal
  610. }))
  611. {
  612. process.WaitForExit();
  613. }
  614. }
  615. }
  616. }