2
0

SharpCifsFileSystem.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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 SharpCifs.Smb;
  8. namespace Emby.Server.Implementations.IO
  9. {
  10. public class SharpCifsFileSystem
  11. {
  12. private readonly MediaBrowser.Model.System.OperatingSystem _operatingSystem;
  13. public SharpCifsFileSystem(MediaBrowser.Model.System.OperatingSystem operatingSystem)
  14. {
  15. _operatingSystem = operatingSystem;
  16. }
  17. public bool IsEnabledForPath(string path)
  18. {
  19. if (_operatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows)
  20. {
  21. return false;
  22. }
  23. return path.StartsWith("smb://", StringComparison.OrdinalIgnoreCase) || IsUncPath(path);
  24. }
  25. public string NormalizePath(string path)
  26. {
  27. if (path.StartsWith("smb://", StringComparison.OrdinalIgnoreCase))
  28. {
  29. return path;
  30. }
  31. if (IsUncPath(path))
  32. {
  33. return ConvertUncToSmb(path);
  34. }
  35. return path;
  36. }
  37. public string GetDirectoryName(string path)
  38. {
  39. var separator = GetDirectorySeparatorChar(path);
  40. var result = Path.GetDirectoryName(path);
  41. if (separator == '/')
  42. {
  43. result = result.Replace('\\', '/');
  44. if (result.StartsWith("smb:/", StringComparison.OrdinalIgnoreCase) && !result.StartsWith("smb://", StringComparison.OrdinalIgnoreCase))
  45. {
  46. result = result.Replace("smb:/", "smb://");
  47. }
  48. }
  49. return result;
  50. }
  51. public char GetDirectorySeparatorChar(string path)
  52. {
  53. if (path.IndexOf('/') != -1)
  54. {
  55. return '/';
  56. }
  57. return '\\';
  58. }
  59. public FileSystemMetadata GetFileSystemInfo(string path)
  60. {
  61. var file = CreateSmbFile(path);
  62. return ToMetadata(file);
  63. }
  64. public FileSystemMetadata GetFileInfo(string path)
  65. {
  66. var file = CreateSmbFile(path);
  67. return ToMetadata(file, false);
  68. }
  69. public FileSystemMetadata GetDirectoryInfo(string path)
  70. {
  71. var file = CreateSmbFile(path);
  72. return ToMetadata(file, true);
  73. }
  74. private bool IsUncPath(string path)
  75. {
  76. return path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase);
  77. }
  78. private string GetReturnPath(SmbFile file)
  79. {
  80. return file.GetCanonicalPath().TrimEnd('/');
  81. //return file.GetPath();
  82. }
  83. private string ConvertUncToSmb(string path)
  84. {
  85. if (IsUncPath(path))
  86. {
  87. path = path.Replace('\\', '/');
  88. path = "smb:" + path;
  89. }
  90. return path;
  91. }
  92. private string AddAuthentication(string path)
  93. {
  94. return path;
  95. }
  96. private SmbFile CreateSmbFile(string path)
  97. {
  98. path = ConvertUncToSmb(path);
  99. path = AddAuthentication(path);
  100. return new SmbFile(path);
  101. }
  102. DateTime baseDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  103. private FileSystemMetadata ToMetadata(SmbFile info, bool? isDirectory = null)
  104. {
  105. var result = new FileSystemMetadata();
  106. result.Exists = info.Exists();
  107. result.FullName = GetReturnPath(info);
  108. result.Extension = Path.GetExtension(result.FullName);
  109. result.Name = info.GetName();
  110. if (result.Exists)
  111. {
  112. result.IsDirectory = info.IsDirectory();
  113. result.IsHidden = info.IsHidden();
  114. result.IsReadOnly = !info.CanWrite();
  115. if (info.IsFile())
  116. {
  117. result.Length = info.Length();
  118. result.DirectoryName = info.GetParent();
  119. }
  120. result.CreationTimeUtc = baseDate.AddMilliseconds(info.CreateTime());
  121. result.LastWriteTimeUtc = baseDate.AddMilliseconds(info.GetLastModified());
  122. }
  123. else
  124. {
  125. if (isDirectory.HasValue)
  126. {
  127. result.IsDirectory = isDirectory.Value;
  128. }
  129. }
  130. return result;
  131. }
  132. public void SetHidden(string path, bool isHidden)
  133. {
  134. var file = CreateSmbFile(path);
  135. SetHidden(file, isHidden);
  136. }
  137. public void SetReadOnly(string path, bool isReadOnly)
  138. {
  139. var file = CreateSmbFile(path);
  140. SetReadOnly(file, isReadOnly);
  141. }
  142. public void SetAttributes(string path, bool isHidden, bool isReadOnly)
  143. {
  144. var file = CreateSmbFile(path);
  145. SetHidden(file, isHidden);
  146. SetReadOnly(file, isReadOnly);
  147. }
  148. private void SetHidden(SmbFile file, bool isHidden)
  149. {
  150. var isCurrentlyHidden = file.IsHidden();
  151. if (isCurrentlyHidden && !isHidden)
  152. {
  153. file.SetAttributes(file.GetAttributes() & ~SmbFile.AttrHidden);
  154. }
  155. else if (!isCurrentlyHidden && isHidden)
  156. {
  157. file.SetAttributes(file.GetAttributes() | SmbFile.AttrHidden);
  158. }
  159. }
  160. private void SetReadOnly(SmbFile file, bool isReadOnly)
  161. {
  162. var isCurrentlyReadOnly = !file.CanWrite();
  163. if (isCurrentlyReadOnly && !isReadOnly)
  164. {
  165. file.SetReadWrite();
  166. }
  167. else if (!isCurrentlyReadOnly && isReadOnly)
  168. {
  169. file.SetReadOnly();
  170. }
  171. }
  172. public void DeleteFile(string path)
  173. {
  174. var file = CreateSmbFile(path);
  175. AssertFileExists(file, path);
  176. file.Delete();
  177. }
  178. public void DeleteDirectory(string path, bool recursive)
  179. {
  180. var file = CreateSmbFile(path);
  181. AssertDirectoryExists(file, path);
  182. file.Delete();
  183. }
  184. public void CreateDirectory(string path)
  185. {
  186. }
  187. public string[] ReadAllLines(string path)
  188. {
  189. var lines = new List<string>();
  190. using (var stream = OpenRead(path))
  191. {
  192. using (var reader = new StreamReader(stream))
  193. {
  194. while (!reader.EndOfStream)
  195. {
  196. lines.Add(reader.ReadLine());
  197. }
  198. }
  199. }
  200. return lines.ToArray();
  201. }
  202. public void WriteAllLines(string path, IEnumerable<string> lines)
  203. {
  204. using (var stream = GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None))
  205. {
  206. using (var writer = new StreamWriter(stream))
  207. {
  208. foreach (var line in lines)
  209. {
  210. writer.WriteLine(line);
  211. }
  212. }
  213. }
  214. }
  215. private void AssertFileExists(SmbFile file, string path)
  216. {
  217. if (!file.Exists())
  218. {
  219. throw new FileNotFoundException("File not found.", path);
  220. }
  221. }
  222. private void AssertDirectoryExists(SmbFile file, string path)
  223. {
  224. if (!file.Exists())
  225. {
  226. throw new FileNotFoundException("File not found.", path);
  227. }
  228. }
  229. public Stream OpenRead(string path)
  230. {
  231. var file = CreateSmbFile(path);
  232. AssertFileExists(file, path);
  233. return file.GetInputStream();
  234. }
  235. private Stream OpenWrite(string path)
  236. {
  237. var file = CreateSmbFile(path);
  238. AssertFileExists(file, path);
  239. return file.GetInputStream();
  240. }
  241. public void CopyFile(string source, string target, bool overwrite)
  242. {
  243. if (string.Equals(source, target, StringComparison.Ordinal))
  244. {
  245. throw new ArgumentException("Cannot CopyFile when source and target are the same");
  246. }
  247. using (var input = OpenRead(source))
  248. {
  249. using (var output = GetFileStream(target, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None))
  250. {
  251. input.CopyTo(output);
  252. }
  253. }
  254. }
  255. public void MoveFile(string source, string target)
  256. {
  257. if (string.Equals(source, target, StringComparison.Ordinal))
  258. {
  259. throw new ArgumentException("Cannot MoveFile when source and target are the same");
  260. }
  261. using (var input = OpenRead(source))
  262. {
  263. using (var output = GetFileStream(target, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None))
  264. {
  265. input.CopyTo(output);
  266. }
  267. }
  268. DeleteFile(source);
  269. }
  270. public void MoveDirectory(string source, string target)
  271. {
  272. throw new NotImplementedException();
  273. }
  274. public bool DirectoryExists(string path)
  275. {
  276. var dir = CreateSmbFile(path);
  277. return dir.Exists() && dir.IsDirectory();
  278. }
  279. public bool FileExists(string path)
  280. {
  281. var file = CreateSmbFile(path);
  282. return file.Exists();
  283. }
  284. public string ReadAllText(string path, Encoding encoding)
  285. {
  286. using (var stream = OpenRead(path))
  287. {
  288. using (var reader = new StreamReader(stream, encoding))
  289. {
  290. return reader.ReadToEnd();
  291. }
  292. }
  293. }
  294. public Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share)
  295. {
  296. if (mode == FileOpenMode.OpenOrCreate)
  297. {
  298. var file = CreateSmbFile(path);
  299. if (!file.Exists())
  300. {
  301. file.CreateNewFile();
  302. }
  303. mode = FileOpenMode.Open;
  304. }
  305. if (mode == FileOpenMode.CreateNew)
  306. {
  307. var file = CreateSmbFile(path);
  308. if (file.Exists())
  309. {
  310. throw new IOException("File already exists");
  311. }
  312. file.CreateNewFile();
  313. mode = FileOpenMode.Open;
  314. }
  315. if (mode == FileOpenMode.Create)
  316. {
  317. var file = CreateSmbFile(path);
  318. if (file.Exists())
  319. {
  320. if (file.IsHidden())
  321. {
  322. throw new UnauthorizedAccessException(string.Format("File {0} already exists and is hidden", path));
  323. }
  324. file.Delete();
  325. file.CreateNewFile();
  326. }
  327. else
  328. {
  329. file.CreateNewFile();
  330. }
  331. mode = FileOpenMode.Open;
  332. }
  333. if (mode == FileOpenMode.Open)
  334. {
  335. if (access == FileAccessMode.Read)
  336. {
  337. return OpenRead(path);
  338. }
  339. if (access == FileAccessMode.Write)
  340. {
  341. return OpenWrite(path);
  342. }
  343. throw new NotImplementedException();
  344. }
  345. throw new NotImplementedException();
  346. }
  347. public void WriteAllBytes(string path, byte[] bytes)
  348. {
  349. using (var stream = GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None))
  350. {
  351. stream.Write(bytes, 0, bytes.Length);
  352. }
  353. }
  354. public void WriteAllText(string path, string text)
  355. {
  356. using (var stream = GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None))
  357. {
  358. using (var writer = new StreamWriter(stream))
  359. {
  360. writer.Write(text);
  361. }
  362. }
  363. }
  364. public void WriteAllText(string path, string text, Encoding encoding)
  365. {
  366. using (var stream = GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None))
  367. {
  368. using (var writer = new StreamWriter(stream, encoding))
  369. {
  370. writer.Write(text);
  371. }
  372. }
  373. }
  374. public string ReadAllText(string path)
  375. {
  376. using (var stream = OpenRead(path))
  377. {
  378. using (var reader = new StreamReader(stream))
  379. {
  380. return reader.ReadToEnd();
  381. }
  382. }
  383. }
  384. public byte[] ReadAllBytes(string path)
  385. {
  386. using (var stream = OpenRead(path))
  387. {
  388. using (var ms = new MemoryStream())
  389. {
  390. stream.CopyTo(ms);
  391. ms.Position = 0;
  392. return ms.ToArray();
  393. }
  394. }
  395. }
  396. private SmbFile CreateSmbDirectoryForListFiles(string path)
  397. {
  398. // In order to call ListFiles, it has to end with the separator
  399. return CreateSmbFile(path.TrimEnd('/') + '/');
  400. }
  401. public IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false)
  402. {
  403. var dir = CreateSmbDirectoryForListFiles(path);
  404. AssertDirectoryExists(dir, path);
  405. var list = ListFiles(dir, recursive);
  406. var result = new List<FileSystemMetadata>();
  407. foreach (var file in list)
  408. {
  409. if (file.IsDirectory())
  410. {
  411. result.Add(ToMetadata(file));
  412. }
  413. }
  414. return result;
  415. }
  416. public IEnumerable<FileSystemMetadata> GetFiles(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false)
  417. {
  418. var dir = CreateSmbDirectoryForListFiles(path);
  419. AssertDirectoryExists(dir, path);
  420. var list = ListFiles(dir, recursive);
  421. var result = new List<FileSystemMetadata>();
  422. foreach (var file in list)
  423. {
  424. if (file.IsFile())
  425. {
  426. var filePath = GetReturnPath(file);
  427. var extension = Path.GetExtension(filePath);
  428. if (extensions == null || extensions.Length == 0 || extensions.Contains(extension ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  429. {
  430. result.Add(ToMetadata(file));
  431. }
  432. }
  433. }
  434. return result;
  435. }
  436. public IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false)
  437. {
  438. var dir = CreateSmbDirectoryForListFiles(path);
  439. AssertDirectoryExists(dir, path);
  440. var list = ListFiles(dir, recursive);
  441. var result = new List<FileSystemMetadata>();
  442. foreach (var file in list)
  443. {
  444. result.Add(ToMetadata(file));
  445. }
  446. return result;
  447. }
  448. public List<string> GetFileSystemEntryPaths(string path, bool recursive = false)
  449. {
  450. var result = new List<string>();
  451. var dir = CreateSmbDirectoryForListFiles(path);
  452. AssertDirectoryExists(dir, path);
  453. var list = ListFiles(dir, recursive);
  454. foreach (var file in list)
  455. {
  456. result.Add(GetReturnPath(file));
  457. }
  458. return result;
  459. }
  460. public List<string> GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false)
  461. {
  462. var dir = CreateSmbDirectoryForListFiles(path);
  463. AssertDirectoryExists(dir, path);
  464. var list = ListFiles(dir, recursive);
  465. var result = new List<string>();
  466. foreach (var file in list)
  467. {
  468. if (file.IsFile())
  469. {
  470. var filePath = GetReturnPath(file);
  471. var extension = Path.GetExtension(filePath);
  472. if (extensions == null || extensions.Length == 0 || extensions.Contains(extension ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  473. {
  474. result.Add(filePath);
  475. }
  476. }
  477. }
  478. return result;
  479. }
  480. public List<string> GetDirectoryPaths(string path, bool recursive = false)
  481. {
  482. var dir = CreateSmbDirectoryForListFiles(path);
  483. AssertDirectoryExists(dir, path);
  484. var list = ListFiles(dir, recursive);
  485. var result = new List<string>();
  486. foreach (var file in list)
  487. {
  488. if (file.IsDirectory())
  489. {
  490. result.Add(GetReturnPath(file));
  491. }
  492. }
  493. return result;
  494. }
  495. private IEnumerable<SmbFile> ListFiles(SmbFile dir, bool recursive)
  496. {
  497. var list = dir.ListFiles();
  498. var result = new List<SmbFile>();
  499. foreach (var file in list)
  500. {
  501. result.Add(file);
  502. if (recursive && file.IsDirectory())
  503. {
  504. foreach (var subFile in ListFiles(file, recursive))
  505. {
  506. result.Add(subFile);
  507. }
  508. }
  509. }
  510. return result;
  511. }
  512. }
  513. }