ManagedFileSystem.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. 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. private static ExtendedFileSystemInfo GetExtendedFileSystemInfo(string path)
  226. {
  227. var result = new ExtendedFileSystemInfo();
  228. var info = new FileInfo(path);
  229. if (info.Exists)
  230. {
  231. result.Exists = true;
  232. var attributes = info.Attributes;
  233. result.IsHidden = (attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
  234. result.IsReadOnly = (attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
  235. }
  236. return result;
  237. }
  238. /// <summary>
  239. /// Takes a filename and removes invalid characters.
  240. /// </summary>
  241. /// <param name="filename">The filename.</param>
  242. /// <returns>System.String.</returns>
  243. /// <exception cref="ArgumentNullException">The filename is null.</exception>
  244. public virtual string GetValidFilename(string filename)
  245. {
  246. var builder = new StringBuilder(filename);
  247. foreach (var c in Path.GetInvalidFileNameChars())
  248. {
  249. builder = builder.Replace(c, ' ');
  250. }
  251. return builder.ToString();
  252. }
  253. /// <summary>
  254. /// Gets the creation time UTC.
  255. /// </summary>
  256. /// <param name="info">The info.</param>
  257. /// <returns>DateTime.</returns>
  258. public DateTime GetCreationTimeUtc(FileSystemInfo info)
  259. {
  260. // This could throw an error on some file systems that have dates out of range
  261. try
  262. {
  263. return info.CreationTimeUtc;
  264. }
  265. catch (Exception ex)
  266. {
  267. Logger.LogError(ex, "Error determining CreationTimeUtc for {FullName}", info.FullName);
  268. return DateTime.MinValue;
  269. }
  270. }
  271. /// <summary>
  272. /// Gets the creation time UTC.
  273. /// </summary>
  274. /// <param name="path">The path.</param>
  275. /// <returns>DateTime.</returns>
  276. public virtual DateTime GetCreationTimeUtc(string path)
  277. {
  278. return GetCreationTimeUtc(GetFileSystemInfo(path));
  279. }
  280. public virtual DateTime GetCreationTimeUtc(FileSystemMetadata info)
  281. {
  282. return info.CreationTimeUtc;
  283. }
  284. public virtual DateTime GetLastWriteTimeUtc(FileSystemMetadata info)
  285. {
  286. return info.LastWriteTimeUtc;
  287. }
  288. /// <summary>
  289. /// Gets the creation time UTC.
  290. /// </summary>
  291. /// <param name="info">The info.</param>
  292. /// <returns>DateTime.</returns>
  293. public DateTime GetLastWriteTimeUtc(FileSystemInfo info)
  294. {
  295. // This could throw an error on some file systems that have dates out of range
  296. try
  297. {
  298. return info.LastWriteTimeUtc;
  299. }
  300. catch (Exception ex)
  301. {
  302. Logger.LogError(ex, "Error determining LastAccessTimeUtc for {FullName}", info.FullName);
  303. return DateTime.MinValue;
  304. }
  305. }
  306. /// <summary>
  307. /// Gets the last write time UTC.
  308. /// </summary>
  309. /// <param name="path">The path.</param>
  310. /// <returns>DateTime.</returns>
  311. public virtual DateTime GetLastWriteTimeUtc(string path)
  312. {
  313. return GetLastWriteTimeUtc(GetFileSystemInfo(path));
  314. }
  315. public virtual void SetHidden(string path, bool isHidden)
  316. {
  317. if (OperatingSystem.Id != OperatingSystemId.Windows)
  318. {
  319. return;
  320. }
  321. var info = GetExtendedFileSystemInfo(path);
  322. if (info.Exists && info.IsHidden != isHidden)
  323. {
  324. if (isHidden)
  325. {
  326. File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
  327. }
  328. else
  329. {
  330. var attributes = File.GetAttributes(path);
  331. attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
  332. File.SetAttributes(path, attributes);
  333. }
  334. }
  335. }
  336. public virtual void SetReadOnly(string path, bool isReadOnly)
  337. {
  338. if (OperatingSystem.Id != OperatingSystemId.Windows)
  339. {
  340. return;
  341. }
  342. var info = GetExtendedFileSystemInfo(path);
  343. if (info.Exists && info.IsReadOnly != isReadOnly)
  344. {
  345. if (isReadOnly)
  346. {
  347. File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);
  348. }
  349. else
  350. {
  351. var attributes = File.GetAttributes(path);
  352. attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
  353. File.SetAttributes(path, attributes);
  354. }
  355. }
  356. }
  357. public virtual void SetAttributes(string path, bool isHidden, bool isReadOnly)
  358. {
  359. if (OperatingSystem.Id != OperatingSystemId.Windows)
  360. {
  361. return;
  362. }
  363. var info = GetExtendedFileSystemInfo(path);
  364. if (!info.Exists)
  365. {
  366. return;
  367. }
  368. if (info.IsReadOnly == isReadOnly && info.IsHidden == isHidden)
  369. {
  370. return;
  371. }
  372. var attributes = File.GetAttributes(path);
  373. if (isReadOnly)
  374. {
  375. attributes = attributes | FileAttributes.ReadOnly;
  376. }
  377. else
  378. {
  379. attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
  380. }
  381. if (isHidden)
  382. {
  383. attributes = attributes | FileAttributes.Hidden;
  384. }
  385. else
  386. {
  387. attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
  388. }
  389. File.SetAttributes(path, attributes);
  390. }
  391. private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
  392. {
  393. return attributes & ~attributesToRemove;
  394. }
  395. /// <summary>
  396. /// Swaps the files.
  397. /// </summary>
  398. /// <param name="file1">The file1.</param>
  399. /// <param name="file2">The file2.</param>
  400. public virtual void SwapFiles(string file1, string file2)
  401. {
  402. if (string.IsNullOrEmpty(file1))
  403. {
  404. throw new ArgumentNullException(nameof(file1));
  405. }
  406. if (string.IsNullOrEmpty(file2))
  407. {
  408. throw new ArgumentNullException(nameof(file2));
  409. }
  410. var temp1 = Path.Combine(_tempPath, Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
  411. // Copying over will fail against hidden files
  412. SetHidden(file1, false);
  413. SetHidden(file2, false);
  414. Directory.CreateDirectory(_tempPath);
  415. File.Copy(file1, temp1, true);
  416. File.Copy(file2, file1, true);
  417. File.Copy(temp1, file2, true);
  418. }
  419. public virtual bool ContainsSubPath(string parentPath, string path)
  420. {
  421. if (string.IsNullOrEmpty(parentPath))
  422. {
  423. throw new ArgumentNullException(nameof(parentPath));
  424. }
  425. if (string.IsNullOrEmpty(path))
  426. {
  427. throw new ArgumentNullException(nameof(path));
  428. }
  429. var separatorChar = Path.DirectorySeparatorChar;
  430. return path.IndexOf(parentPath.TrimEnd(separatorChar) + separatorChar, StringComparison.OrdinalIgnoreCase) != -1;
  431. }
  432. public virtual bool IsRootPath(string path)
  433. {
  434. if (string.IsNullOrEmpty(path))
  435. {
  436. throw new ArgumentNullException(nameof(path));
  437. }
  438. var parent = Path.GetDirectoryName(path);
  439. if (!string.IsNullOrEmpty(parent))
  440. {
  441. return false;
  442. }
  443. return true;
  444. }
  445. public virtual string NormalizePath(string path)
  446. {
  447. if (string.IsNullOrEmpty(path))
  448. {
  449. throw new ArgumentNullException(nameof(path));
  450. }
  451. if (path.EndsWith(":\\", StringComparison.OrdinalIgnoreCase))
  452. {
  453. return path;
  454. }
  455. return path.TrimEnd(Path.DirectorySeparatorChar);
  456. }
  457. public virtual bool AreEqual(string path1, string path2)
  458. {
  459. if (path1 == null && path2 == null)
  460. {
  461. return true;
  462. }
  463. if (path1 == null || path2 == null)
  464. {
  465. return false;
  466. }
  467. return string.Equals(NormalizePath(path1), NormalizePath(path2), StringComparison.OrdinalIgnoreCase);
  468. }
  469. public virtual string GetFileNameWithoutExtension(FileSystemMetadata info)
  470. {
  471. if (info.IsDirectory)
  472. {
  473. return info.Name;
  474. }
  475. return Path.GetFileNameWithoutExtension(info.FullName);
  476. }
  477. public virtual bool IsPathFile(string path)
  478. {
  479. // Cannot use Path.IsPathRooted because it returns false under mono when using windows-based paths, e.g. C:\\
  480. if (path.IndexOf("://", StringComparison.OrdinalIgnoreCase) != -1 &&
  481. !path.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
  482. {
  483. return false;
  484. }
  485. return true;
  486. }
  487. public virtual void DeleteFile(string path)
  488. {
  489. SetAttributes(path, false, false);
  490. File.Delete(path);
  491. }
  492. public virtual List<FileSystemMetadata> GetDrives()
  493. {
  494. // check for ready state to avoid waiting for drives to timeout
  495. // some drives on linux have no actual size or are used for other purposes
  496. return DriveInfo.GetDrives().Where(d => d.IsReady && d.TotalSize != 0 && d.DriveType != DriveType.Ram)
  497. .Select(d => new FileSystemMetadata
  498. {
  499. Name = d.Name,
  500. FullName = d.RootDirectory.FullName,
  501. IsDirectory = true
  502. }).ToList();
  503. }
  504. public virtual IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false)
  505. {
  506. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  507. return ToMetadata(new DirectoryInfo(path).EnumerateDirectories("*", searchOption));
  508. }
  509. public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false)
  510. {
  511. return GetFiles(path, null, false, recursive);
  512. }
  513. public virtual IEnumerable<FileSystemMetadata> GetFiles(string path, IReadOnlyList<string> extensions, bool enableCaseSensitiveExtensions, bool recursive = false)
  514. {
  515. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  516. // On linux and osx the search pattern is case sensitive
  517. // If we're OK with case-sensitivity, and we're only filtering for one extension, then use the native method
  518. if ((enableCaseSensitiveExtensions || _isEnvironmentCaseInsensitive) && extensions != null && extensions.Count == 1)
  519. {
  520. return ToMetadata(new DirectoryInfo(path).EnumerateFiles("*" + extensions[0], searchOption));
  521. }
  522. var files = new DirectoryInfo(path).EnumerateFiles("*", searchOption);
  523. if (extensions != null && extensions.Count > 0)
  524. {
  525. files = files.Where(i =>
  526. {
  527. var ext = i.Extension;
  528. if (ext == null)
  529. {
  530. return false;
  531. }
  532. return extensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
  533. });
  534. }
  535. return ToMetadata(files);
  536. }
  537. public virtual IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false)
  538. {
  539. var directoryInfo = new DirectoryInfo(path);
  540. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  541. return ToMetadata(directoryInfo.EnumerateDirectories("*", searchOption))
  542. .Concat(ToMetadata(directoryInfo.EnumerateFiles("*", searchOption)));
  543. }
  544. private IEnumerable<FileSystemMetadata> ToMetadata(IEnumerable<FileSystemInfo> infos)
  545. {
  546. return infos.Select(GetFileSystemMetadata);
  547. }
  548. public virtual IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false)
  549. {
  550. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  551. return Directory.EnumerateDirectories(path, "*", searchOption);
  552. }
  553. public virtual IEnumerable<string> GetFilePaths(string path, bool recursive = false)
  554. {
  555. return GetFilePaths(path, null, false, recursive);
  556. }
  557. public virtual IEnumerable<string> GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false)
  558. {
  559. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  560. // On linux and osx the search pattern is case sensitive
  561. // If we're OK with case-sensitivity, and we're only filtering for one extension, then use the native method
  562. if ((enableCaseSensitiveExtensions || _isEnvironmentCaseInsensitive) && extensions != null && extensions.Length == 1)
  563. {
  564. return Directory.EnumerateFiles(path, "*" + extensions[0], searchOption);
  565. }
  566. var files = Directory.EnumerateFiles(path, "*", searchOption);
  567. if (extensions != null && extensions.Length > 0)
  568. {
  569. files = files.Where(i =>
  570. {
  571. var ext = Path.GetExtension(i);
  572. if (ext == null)
  573. {
  574. return false;
  575. }
  576. return extensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
  577. });
  578. }
  579. return files;
  580. }
  581. public virtual IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false)
  582. {
  583. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  584. return Directory.EnumerateFileSystemEntries(path, "*", searchOption);
  585. }
  586. public virtual void SetExecutable(string path)
  587. {
  588. if (OperatingSystem.Id == OperatingSystemId.Darwin)
  589. {
  590. RunProcess("chmod", "+x \"" + path + "\"", Path.GetDirectoryName(path));
  591. }
  592. }
  593. private static void RunProcess(string path, string args, string workingDirectory)
  594. {
  595. using (var process = Process.Start(new ProcessStartInfo
  596. {
  597. Arguments = args,
  598. FileName = path,
  599. CreateNoWindow = true,
  600. WorkingDirectory = workingDirectory,
  601. WindowStyle = ProcessWindowStyle.Normal
  602. }))
  603. {
  604. process.WaitForExit();
  605. }
  606. }
  607. }
  608. }