ManagedFileSystem.cs 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. using MediaBrowser.Model.System;
  9. namespace Emby.Common.Implementations.IO
  10. {
  11. /// <summary>
  12. /// Class ManagedFileSystem
  13. /// </summary>
  14. public class ManagedFileSystem : IFileSystem
  15. {
  16. protected ILogger Logger;
  17. private readonly bool _supportsAsyncFileStreams;
  18. private char[] _invalidFileNameChars;
  19. private readonly List<IShortcutHandler> _shortcutHandlers = new List<IShortcutHandler>();
  20. private bool EnableFileSystemRequestConcat;
  21. private string _tempPath;
  22. private SharpCifsFileSystem _sharpCifsFileSystem;
  23. public ManagedFileSystem(ILogger logger, IEnvironmentInfo environmentInfo, string tempPath)
  24. {
  25. Logger = logger;
  26. _supportsAsyncFileStreams = true;
  27. _tempPath = tempPath;
  28. // On Linux, this needs to be true or symbolic links are ignored
  29. EnableFileSystemRequestConcat = environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows &&
  30. environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.OSX;
  31. SetInvalidFileNameChars(environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows);
  32. _sharpCifsFileSystem = new SharpCifsFileSystem(environmentInfo.OperatingSystem);
  33. }
  34. public void AddShortcutHandler(IShortcutHandler handler)
  35. {
  36. _shortcutHandlers.Add(handler);
  37. }
  38. protected void SetInvalidFileNameChars(bool enableManagedInvalidFileNameChars)
  39. {
  40. if (enableManagedInvalidFileNameChars)
  41. {
  42. _invalidFileNameChars = Path.GetInvalidFileNameChars();
  43. }
  44. else
  45. {
  46. // GetInvalidFileNameChars is less restrictive in Linux/Mac than Windows, this mimic Windows behavior for mono under Linux/Mac.
  47. _invalidFileNameChars = new char[41] { '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
  48. '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F', '\x10', '\x11', '\x12',
  49. '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D',
  50. '\x1E', '\x1F', '\x22', '\x3C', '\x3E', '\x7C', ':', '*', '?', '\\', '/' };
  51. }
  52. }
  53. public char DirectorySeparatorChar
  54. {
  55. get
  56. {
  57. return Path.DirectorySeparatorChar;
  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. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  144. {
  145. return _sharpCifsFileSystem.GetFileSystemInfo(path);
  146. }
  147. // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists
  148. if (Path.HasExtension(path))
  149. {
  150. var fileInfo = new FileInfo(path);
  151. if (fileInfo.Exists)
  152. {
  153. return GetFileSystemMetadata(fileInfo);
  154. }
  155. return GetFileSystemMetadata(new DirectoryInfo(path));
  156. }
  157. else
  158. {
  159. var fileInfo = new DirectoryInfo(path);
  160. if (fileInfo.Exists)
  161. {
  162. return GetFileSystemMetadata(fileInfo);
  163. }
  164. return GetFileSystemMetadata(new FileInfo(path));
  165. }
  166. }
  167. /// <summary>
  168. /// Returns a <see cref="FileSystemMetadata"/> object for the specified file path.
  169. /// </summary>
  170. /// <param name="path">A path to a file.</param>
  171. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  172. /// <remarks><para>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> object's
  173. /// <see cref="FileSystemMetadata.IsDirectory"/> property and the <see cref="FileSystemMetadata.Exists"/> property will both be set to false.</para>
  174. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></remarks>
  175. public FileSystemMetadata GetFileInfo(string path)
  176. {
  177. if (string.IsNullOrEmpty(path))
  178. {
  179. throw new ArgumentNullException("path");
  180. }
  181. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  182. {
  183. return _sharpCifsFileSystem.GetFileInfo(path);
  184. }
  185. var fileInfo = new FileInfo(path);
  186. return GetFileSystemMetadata(fileInfo);
  187. }
  188. /// <summary>
  189. /// Returns a <see cref="FileSystemMetadata"/> object for the specified directory path.
  190. /// </summary>
  191. /// <param name="path">A path to a directory.</param>
  192. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  193. /// <remarks><para>If the specified path points to a file, the returned <see cref="FileSystemMetadata"/> object's
  194. /// <see cref="FileSystemMetadata.IsDirectory"/> property will be set to true and the <see cref="FileSystemMetadata.Exists"/> property will be set to false.</para>
  195. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></remarks>
  196. public FileSystemMetadata GetDirectoryInfo(string path)
  197. {
  198. if (string.IsNullOrEmpty(path))
  199. {
  200. throw new ArgumentNullException("path");
  201. }
  202. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  203. {
  204. return _sharpCifsFileSystem.GetDirectoryInfo(path);
  205. }
  206. var fileInfo = new DirectoryInfo(path);
  207. return GetFileSystemMetadata(fileInfo);
  208. }
  209. private FileSystemMetadata GetFileSystemMetadata(FileSystemInfo info)
  210. {
  211. var result = new FileSystemMetadata();
  212. result.Exists = info.Exists;
  213. result.FullName = info.FullName;
  214. result.Extension = info.Extension;
  215. result.Name = info.Name;
  216. if (result.Exists)
  217. {
  218. var attributes = info.Attributes;
  219. result.IsDirectory = info is DirectoryInfo || (attributes & FileAttributes.Directory) == FileAttributes.Directory;
  220. result.IsHidden = (attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
  221. result.IsReadOnly = (attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
  222. var fileInfo = info as FileInfo;
  223. if (fileInfo != null)
  224. {
  225. result.Length = fileInfo.Length;
  226. result.DirectoryName = fileInfo.DirectoryName;
  227. }
  228. result.CreationTimeUtc = GetCreationTimeUtc(info);
  229. result.LastWriteTimeUtc = GetLastWriteTimeUtc(info);
  230. }
  231. else
  232. {
  233. result.IsDirectory = info is DirectoryInfo;
  234. }
  235. return result;
  236. }
  237. /// <summary>
  238. /// The space char
  239. /// </summary>
  240. private const char SpaceChar = ' ';
  241. /// <summary>
  242. /// Takes a filename and removes invalid characters
  243. /// </summary>
  244. /// <param name="filename">The filename.</param>
  245. /// <returns>System.String.</returns>
  246. /// <exception cref="System.ArgumentNullException">filename</exception>
  247. public string GetValidFilename(string filename)
  248. {
  249. if (string.IsNullOrEmpty(filename))
  250. {
  251. throw new ArgumentNullException("filename");
  252. }
  253. var builder = new StringBuilder(filename);
  254. foreach (var c in _invalidFileNameChars)
  255. {
  256. builder = builder.Replace(c, SpaceChar);
  257. }
  258. return builder.ToString();
  259. }
  260. /// <summary>
  261. /// Gets the creation time UTC.
  262. /// </summary>
  263. /// <param name="info">The info.</param>
  264. /// <returns>DateTime.</returns>
  265. public DateTime GetCreationTimeUtc(FileSystemInfo info)
  266. {
  267. // This could throw an error on some file systems that have dates out of range
  268. try
  269. {
  270. return info.CreationTimeUtc;
  271. }
  272. catch (Exception ex)
  273. {
  274. Logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
  275. return DateTime.MinValue;
  276. }
  277. }
  278. /// <summary>
  279. /// Gets the creation time UTC.
  280. /// </summary>
  281. /// <param name="path">The path.</param>
  282. /// <returns>DateTime.</returns>
  283. public DateTime GetCreationTimeUtc(string path)
  284. {
  285. return GetCreationTimeUtc(GetFileSystemInfo(path));
  286. }
  287. public DateTime GetCreationTimeUtc(FileSystemMetadata info)
  288. {
  289. return info.CreationTimeUtc;
  290. }
  291. public DateTime GetLastWriteTimeUtc(FileSystemMetadata info)
  292. {
  293. return info.LastWriteTimeUtc;
  294. }
  295. /// <summary>
  296. /// Gets the creation time UTC.
  297. /// </summary>
  298. /// <param name="info">The info.</param>
  299. /// <returns>DateTime.</returns>
  300. public DateTime GetLastWriteTimeUtc(FileSystemInfo info)
  301. {
  302. // This could throw an error on some file systems that have dates out of range
  303. try
  304. {
  305. return info.LastWriteTimeUtc;
  306. }
  307. catch (Exception ex)
  308. {
  309. Logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
  310. return DateTime.MinValue;
  311. }
  312. }
  313. /// <summary>
  314. /// Gets the last write time UTC.
  315. /// </summary>
  316. /// <param name="path">The path.</param>
  317. /// <returns>DateTime.</returns>
  318. public DateTime GetLastWriteTimeUtc(string path)
  319. {
  320. return GetLastWriteTimeUtc(GetFileSystemInfo(path));
  321. }
  322. /// <summary>
  323. /// Gets the file stream.
  324. /// </summary>
  325. /// <param name="path">The path.</param>
  326. /// <param name="mode">The mode.</param>
  327. /// <param name="access">The access.</param>
  328. /// <param name="share">The share.</param>
  329. /// <param name="isAsync">if set to <c>true</c> [is asynchronous].</param>
  330. /// <returns>FileStream.</returns>
  331. public Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false)
  332. {
  333. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  334. {
  335. return _sharpCifsFileSystem.GetFileStream(path, mode, access, share);
  336. }
  337. if (_supportsAsyncFileStreams && isAsync)
  338. {
  339. return new FileStream(path, GetFileMode(mode), GetFileAccess(access), GetFileShare(share), 262144, true);
  340. }
  341. return new FileStream(path, GetFileMode(mode), GetFileAccess(access), GetFileShare(share), 262144);
  342. }
  343. private FileMode GetFileMode(FileOpenMode mode)
  344. {
  345. switch (mode)
  346. {
  347. //case FileOpenMode.Append:
  348. // return FileMode.Append;
  349. case FileOpenMode.Create:
  350. return FileMode.Create;
  351. case FileOpenMode.CreateNew:
  352. return FileMode.CreateNew;
  353. case FileOpenMode.Open:
  354. return FileMode.Open;
  355. case FileOpenMode.OpenOrCreate:
  356. return FileMode.OpenOrCreate;
  357. //case FileOpenMode.Truncate:
  358. // return FileMode.Truncate;
  359. default:
  360. throw new Exception("Unrecognized FileOpenMode");
  361. }
  362. }
  363. private FileAccess GetFileAccess(FileAccessMode mode)
  364. {
  365. switch (mode)
  366. {
  367. //case FileAccessMode.ReadWrite:
  368. // return FileAccess.ReadWrite;
  369. case FileAccessMode.Write:
  370. return FileAccess.Write;
  371. case FileAccessMode.Read:
  372. return FileAccess.Read;
  373. default:
  374. throw new Exception("Unrecognized FileAccessMode");
  375. }
  376. }
  377. private FileShare GetFileShare(FileShareMode mode)
  378. {
  379. switch (mode)
  380. {
  381. case FileShareMode.ReadWrite:
  382. return FileShare.ReadWrite;
  383. case FileShareMode.Write:
  384. return FileShare.Write;
  385. case FileShareMode.Read:
  386. return FileShare.Read;
  387. case FileShareMode.None:
  388. return FileShare.None;
  389. default:
  390. throw new Exception("Unrecognized FileShareMode");
  391. }
  392. }
  393. public void SetHidden(string path, bool isHidden)
  394. {
  395. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  396. {
  397. _sharpCifsFileSystem.SetHidden(path, isHidden);
  398. return;
  399. }
  400. var info = GetFileInfo(path);
  401. if (info.Exists && info.IsHidden != isHidden)
  402. {
  403. if (isHidden)
  404. {
  405. File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
  406. }
  407. else
  408. {
  409. FileAttributes attributes = File.GetAttributes(path);
  410. attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
  411. File.SetAttributes(path, attributes);
  412. }
  413. }
  414. }
  415. public void SetReadOnly(string path, bool isReadOnly)
  416. {
  417. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  418. {
  419. _sharpCifsFileSystem.SetReadOnly(path, isReadOnly);
  420. return;
  421. }
  422. var info = GetFileInfo(path);
  423. if (info.Exists && info.IsReadOnly != isReadOnly)
  424. {
  425. if (isReadOnly)
  426. {
  427. File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);
  428. }
  429. else
  430. {
  431. FileAttributes attributes = File.GetAttributes(path);
  432. attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
  433. File.SetAttributes(path, attributes);
  434. }
  435. }
  436. }
  437. private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
  438. {
  439. return attributes & ~attributesToRemove;
  440. }
  441. /// <summary>
  442. /// Swaps the files.
  443. /// </summary>
  444. /// <param name="file1">The file1.</param>
  445. /// <param name="file2">The file2.</param>
  446. public void SwapFiles(string file1, string file2)
  447. {
  448. if (string.IsNullOrEmpty(file1))
  449. {
  450. throw new ArgumentNullException("file1");
  451. }
  452. if (string.IsNullOrEmpty(file2))
  453. {
  454. throw new ArgumentNullException("file2");
  455. }
  456. var temp1 = Path.Combine(_tempPath, Guid.NewGuid().ToString("N"));
  457. // Copying over will fail against hidden files
  458. SetHidden(file1, false);
  459. SetHidden(file2, false);
  460. Directory.CreateDirectory(_tempPath);
  461. CopyFile(file1, temp1, true);
  462. CopyFile(file2, file1, true);
  463. CopyFile(temp1, file2, true);
  464. }
  465. private char GetDirectorySeparatorChar(string path)
  466. {
  467. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  468. {
  469. return _sharpCifsFileSystem.GetDirectorySeparatorChar(path);
  470. }
  471. return Path.DirectorySeparatorChar;
  472. }
  473. public bool ContainsSubPath(string parentPath, string path)
  474. {
  475. if (string.IsNullOrEmpty(parentPath))
  476. {
  477. throw new ArgumentNullException("parentPath");
  478. }
  479. if (string.IsNullOrEmpty(path))
  480. {
  481. throw new ArgumentNullException("path");
  482. }
  483. var separatorChar = GetDirectorySeparatorChar(parentPath);
  484. return path.IndexOf(parentPath.TrimEnd(separatorChar) + separatorChar, StringComparison.OrdinalIgnoreCase) != -1;
  485. }
  486. public bool IsRootPath(string path)
  487. {
  488. if (string.IsNullOrEmpty(path))
  489. {
  490. throw new ArgumentNullException("path");
  491. }
  492. var parent = GetDirectoryName(path);
  493. if (!string.IsNullOrEmpty(parent))
  494. {
  495. return false;
  496. }
  497. return true;
  498. }
  499. public string GetDirectoryName(string path)
  500. {
  501. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  502. {
  503. return _sharpCifsFileSystem.GetDirectoryName(path);
  504. }
  505. return Path.GetDirectoryName(path);
  506. }
  507. public string NormalizePath(string path)
  508. {
  509. if (string.IsNullOrEmpty(path))
  510. {
  511. throw new ArgumentNullException("path");
  512. }
  513. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  514. {
  515. return _sharpCifsFileSystem.NormalizePath(path);
  516. }
  517. if (path.EndsWith(":\\", StringComparison.OrdinalIgnoreCase))
  518. {
  519. return path;
  520. }
  521. return path.TrimEnd(GetDirectorySeparatorChar(path));
  522. }
  523. public bool AreEqual(string path1, string path2)
  524. {
  525. if (path1 == null && path2 == null)
  526. {
  527. return true;
  528. }
  529. if (path1 == null || path2 == null)
  530. {
  531. return false;
  532. }
  533. return string.Equals(NormalizePath(path1), NormalizePath(path2), StringComparison.OrdinalIgnoreCase);
  534. }
  535. public string GetFileNameWithoutExtension(FileSystemMetadata info)
  536. {
  537. if (info.IsDirectory)
  538. {
  539. return info.Name;
  540. }
  541. return Path.GetFileNameWithoutExtension(info.FullName);
  542. }
  543. public string GetFileNameWithoutExtension(string path)
  544. {
  545. return Path.GetFileNameWithoutExtension(path);
  546. }
  547. public bool IsPathFile(string path)
  548. {
  549. if (string.IsNullOrWhiteSpace(path))
  550. {
  551. throw new ArgumentNullException("path");
  552. }
  553. // Cannot use Path.IsPathRooted because it returns false under mono when using windows-based paths, e.g. C:\\
  554. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  555. {
  556. return true;
  557. }
  558. if (path.IndexOf("://", StringComparison.OrdinalIgnoreCase) != -1 &&
  559. !path.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
  560. {
  561. return false;
  562. }
  563. return true;
  564. //return Path.IsPathRooted(path);
  565. }
  566. public void DeleteFile(string path)
  567. {
  568. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  569. {
  570. _sharpCifsFileSystem.DeleteFile(path);
  571. return;
  572. }
  573. var fileInfo = GetFileInfo(path);
  574. if (fileInfo.Exists)
  575. {
  576. if (fileInfo.IsHidden)
  577. {
  578. SetHidden(path, false);
  579. }
  580. if (fileInfo.IsReadOnly)
  581. {
  582. SetReadOnly(path, false);
  583. }
  584. }
  585. File.Delete(path);
  586. }
  587. public void DeleteDirectory(string path, bool recursive)
  588. {
  589. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  590. {
  591. _sharpCifsFileSystem.DeleteDirectory(path, recursive);
  592. return;
  593. }
  594. Directory.Delete(path, recursive);
  595. }
  596. public void CreateDirectory(string path)
  597. {
  598. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  599. {
  600. _sharpCifsFileSystem.CreateDirectory(path);
  601. return;
  602. }
  603. Directory.CreateDirectory(path);
  604. }
  605. public List<FileSystemMetadata> GetDrives()
  606. {
  607. // Only include drives in the ready state or this method could end up being very slow, waiting for drives to timeout
  608. return DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => new FileSystemMetadata
  609. {
  610. Name = GetName(d),
  611. FullName = d.RootDirectory.FullName,
  612. IsDirectory = true
  613. }).ToList();
  614. }
  615. private string GetName(DriveInfo drive)
  616. {
  617. return drive.Name;
  618. }
  619. public IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false)
  620. {
  621. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  622. {
  623. return _sharpCifsFileSystem.GetDirectories(path, recursive);
  624. }
  625. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  626. return ToMetadata(new DirectoryInfo(path).EnumerateDirectories("*", searchOption));
  627. }
  628. public IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false)
  629. {
  630. return GetFiles(path, null, false, recursive);
  631. }
  632. public IEnumerable<FileSystemMetadata> GetFiles(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false)
  633. {
  634. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  635. {
  636. return _sharpCifsFileSystem.GetFiles(path, extensions, enableCaseSensitiveExtensions, recursive);
  637. }
  638. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  639. // On linux and osx the search pattern is case sensitive
  640. // If we're OK with case-sensitivity, and we're only filtering for one extension, then use the native method
  641. if (enableCaseSensitiveExtensions && extensions != null && extensions.Length == 1)
  642. {
  643. return ToMetadata(new DirectoryInfo(path).EnumerateFiles("*" + extensions[0], searchOption));
  644. }
  645. var files = new DirectoryInfo(path).EnumerateFiles("*", searchOption);
  646. if (extensions != null && extensions.Length > 0)
  647. {
  648. files = files.Where(i =>
  649. {
  650. var ext = i.Extension;
  651. if (ext == null)
  652. {
  653. return false;
  654. }
  655. return extensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
  656. });
  657. }
  658. return ToMetadata(files);
  659. }
  660. public IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false)
  661. {
  662. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  663. {
  664. return _sharpCifsFileSystem.GetFileSystemEntries(path, recursive);
  665. }
  666. var directoryInfo = new DirectoryInfo(path);
  667. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  668. if (EnableFileSystemRequestConcat)
  669. {
  670. return ToMetadata(directoryInfo.EnumerateDirectories("*", searchOption))
  671. .Concat(ToMetadata(directoryInfo.EnumerateFiles("*", searchOption)));
  672. }
  673. return ToMetadata(directoryInfo.EnumerateFileSystemInfos("*", searchOption));
  674. }
  675. private IEnumerable<FileSystemMetadata> ToMetadata(IEnumerable<FileSystemInfo> infos)
  676. {
  677. return infos.Select(GetFileSystemMetadata);
  678. }
  679. public string[] ReadAllLines(string path)
  680. {
  681. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  682. {
  683. return _sharpCifsFileSystem.ReadAllLines(path);
  684. }
  685. return File.ReadAllLines(path);
  686. }
  687. public void WriteAllLines(string path, IEnumerable<string> lines)
  688. {
  689. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  690. {
  691. _sharpCifsFileSystem.WriteAllLines(path, lines);
  692. return;
  693. }
  694. File.WriteAllLines(path, lines);
  695. }
  696. public Stream OpenRead(string path)
  697. {
  698. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  699. {
  700. return _sharpCifsFileSystem.OpenRead(path);
  701. }
  702. return File.OpenRead(path);
  703. }
  704. public void CopyFile(string source, string target, bool overwrite)
  705. {
  706. if (_sharpCifsFileSystem.IsEnabledForPath(source))
  707. {
  708. _sharpCifsFileSystem.CopyFile(source, target, overwrite);
  709. return;
  710. }
  711. File.Copy(source, target, overwrite);
  712. }
  713. public void MoveFile(string source, string target)
  714. {
  715. if (_sharpCifsFileSystem.IsEnabledForPath(source))
  716. {
  717. _sharpCifsFileSystem.MoveFile(source, target);
  718. return;
  719. }
  720. File.Move(source, target);
  721. }
  722. public void MoveDirectory(string source, string target)
  723. {
  724. if (_sharpCifsFileSystem.IsEnabledForPath(source))
  725. {
  726. _sharpCifsFileSystem.MoveDirectory(source, target);
  727. return;
  728. }
  729. Directory.Move(source, target);
  730. }
  731. public bool DirectoryExists(string path)
  732. {
  733. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  734. {
  735. return _sharpCifsFileSystem.DirectoryExists(path);
  736. }
  737. return Directory.Exists(path);
  738. }
  739. public bool FileExists(string path)
  740. {
  741. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  742. {
  743. return _sharpCifsFileSystem.FileExists(path);
  744. }
  745. return File.Exists(path);
  746. }
  747. public string ReadAllText(string path)
  748. {
  749. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  750. {
  751. return _sharpCifsFileSystem.ReadAllText(path);
  752. }
  753. return File.ReadAllText(path);
  754. }
  755. public byte[] ReadAllBytes(string path)
  756. {
  757. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  758. {
  759. return _sharpCifsFileSystem.ReadAllBytes(path);
  760. }
  761. return File.ReadAllBytes(path);
  762. }
  763. public void WriteAllText(string path, string text, Encoding encoding)
  764. {
  765. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  766. {
  767. _sharpCifsFileSystem.WriteAllText(path, text, encoding);
  768. return;
  769. }
  770. File.WriteAllText(path, text, encoding);
  771. }
  772. public void WriteAllText(string path, string text)
  773. {
  774. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  775. {
  776. _sharpCifsFileSystem.WriteAllText(path, text);
  777. return;
  778. }
  779. File.WriteAllText(path, text);
  780. }
  781. public void WriteAllBytes(string path, byte[] bytes)
  782. {
  783. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  784. {
  785. _sharpCifsFileSystem.WriteAllBytes(path, bytes);
  786. return;
  787. }
  788. File.WriteAllBytes(path, bytes);
  789. }
  790. public string ReadAllText(string path, Encoding encoding)
  791. {
  792. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  793. {
  794. return _sharpCifsFileSystem.ReadAllText(path, encoding);
  795. }
  796. return File.ReadAllText(path, encoding);
  797. }
  798. public IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false)
  799. {
  800. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  801. {
  802. return _sharpCifsFileSystem.GetDirectoryPaths(path, recursive);
  803. }
  804. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  805. return Directory.EnumerateDirectories(path, "*", searchOption);
  806. }
  807. public IEnumerable<string> GetFilePaths(string path, bool recursive = false)
  808. {
  809. return GetFilePaths(path, null, false, recursive);
  810. }
  811. public IEnumerable<string> GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false)
  812. {
  813. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  814. {
  815. return _sharpCifsFileSystem.GetFilePaths(path, extensions, enableCaseSensitiveExtensions, recursive);
  816. }
  817. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  818. // On linux and osx the search pattern is case sensitive
  819. // If we're OK with case-sensitivity, and we're only filtering for one extension, then use the native method
  820. if (enableCaseSensitiveExtensions && extensions != null && extensions.Length == 1)
  821. {
  822. return Directory.EnumerateFiles(path, "*" + extensions[0], searchOption);
  823. }
  824. var files = Directory.EnumerateFiles(path, "*", searchOption);
  825. if (extensions != null && extensions.Length > 0)
  826. {
  827. files = files.Where(i =>
  828. {
  829. var ext = Path.GetExtension(i);
  830. if (ext == null)
  831. {
  832. return false;
  833. }
  834. return extensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
  835. });
  836. }
  837. return files;
  838. }
  839. public IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false)
  840. {
  841. if (_sharpCifsFileSystem.IsEnabledForPath(path))
  842. {
  843. return _sharpCifsFileSystem.GetFileSystemEntryPaths(path, recursive);
  844. }
  845. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  846. return Directory.EnumerateFileSystemEntries(path, "*", searchOption);
  847. }
  848. public virtual void SetExecutable(string path)
  849. {
  850. }
  851. }
  852. }