ManagedFileSystem.cs 25 KB

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