CommonFileSystem.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.IO;
  5. using System.Text;
  6. namespace MediaBrowser.Common.Implementations.IO
  7. {
  8. /// <summary>
  9. /// Class CommonFileSystem
  10. /// </summary>
  11. public class CommonFileSystem : IFileSystem
  12. {
  13. protected ILogger Logger;
  14. private readonly bool _supportsAsyncFileStreams;
  15. public CommonFileSystem(ILogger logger, bool supportsAsyncFileStreams)
  16. {
  17. Logger = logger;
  18. _supportsAsyncFileStreams = supportsAsyncFileStreams;
  19. }
  20. /// <summary>
  21. /// Determines whether the specified filename is shortcut.
  22. /// </summary>
  23. /// <param name="filename">The filename.</param>
  24. /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
  25. /// <exception cref="System.ArgumentNullException">filename</exception>
  26. public virtual bool IsShortcut(string filename)
  27. {
  28. if (string.IsNullOrEmpty(filename))
  29. {
  30. throw new ArgumentNullException("filename");
  31. }
  32. var extension = Path.GetExtension(filename);
  33. return string.Equals(extension, ".mblink", StringComparison.OrdinalIgnoreCase);
  34. }
  35. /// <summary>
  36. /// Resolves the shortcut.
  37. /// </summary>
  38. /// <param name="filename">The filename.</param>
  39. /// <returns>System.String.</returns>
  40. /// <exception cref="System.ArgumentNullException">filename</exception>
  41. public virtual string ResolveShortcut(string filename)
  42. {
  43. if (string.IsNullOrEmpty(filename))
  44. {
  45. throw new ArgumentNullException("filename");
  46. }
  47. if (string.Equals(Path.GetExtension(filename), ".mblink", StringComparison.OrdinalIgnoreCase))
  48. {
  49. return File.ReadAllText(filename);
  50. }
  51. return null;
  52. }
  53. /// <summary>
  54. /// Creates the shortcut.
  55. /// </summary>
  56. /// <param name="shortcutPath">The shortcut path.</param>
  57. /// <param name="target">The target.</param>
  58. /// <exception cref="System.ArgumentNullException">
  59. /// shortcutPath
  60. /// or
  61. /// target
  62. /// </exception>
  63. public void CreateShortcut(string shortcutPath, string target)
  64. {
  65. if (string.IsNullOrEmpty(shortcutPath))
  66. {
  67. throw new ArgumentNullException("shortcutPath");
  68. }
  69. if (string.IsNullOrEmpty(target))
  70. {
  71. throw new ArgumentNullException("target");
  72. }
  73. File.WriteAllText(shortcutPath, target);
  74. }
  75. /// <summary>
  76. /// Gets the file system info.
  77. /// </summary>
  78. /// <param name="path">The path.</param>
  79. /// <returns>FileSystemInfo.</returns>
  80. public FileSystemInfo GetFileSystemInfo(string path)
  81. {
  82. if (string.IsNullOrEmpty(path))
  83. {
  84. throw new ArgumentNullException("path");
  85. }
  86. // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists
  87. if (Path.HasExtension(path))
  88. {
  89. var fileInfo = new FileInfo(path);
  90. if (fileInfo.Exists)
  91. {
  92. return fileInfo;
  93. }
  94. return new DirectoryInfo(path);
  95. }
  96. else
  97. {
  98. var fileInfo = new DirectoryInfo(path);
  99. if (fileInfo.Exists)
  100. {
  101. return fileInfo;
  102. }
  103. return new FileInfo(path);
  104. }
  105. }
  106. /// <summary>
  107. /// The space char
  108. /// </summary>
  109. private const char SpaceChar = ' ';
  110. /// <summary>
  111. /// The invalid file name chars
  112. /// </summary>
  113. private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
  114. /// <summary>
  115. /// Takes a filename and removes invalid characters
  116. /// </summary>
  117. /// <param name="filename">The filename.</param>
  118. /// <returns>System.String.</returns>
  119. /// <exception cref="System.ArgumentNullException">filename</exception>
  120. public string GetValidFilename(string filename)
  121. {
  122. if (string.IsNullOrEmpty(filename))
  123. {
  124. throw new ArgumentNullException("filename");
  125. }
  126. var builder = new StringBuilder(filename);
  127. foreach (var c in InvalidFileNameChars)
  128. {
  129. builder = builder.Replace(c, SpaceChar);
  130. }
  131. return builder.ToString();
  132. }
  133. /// <summary>
  134. /// Gets the creation time UTC.
  135. /// </summary>
  136. /// <param name="info">The info.</param>
  137. /// <returns>DateTime.</returns>
  138. public DateTime GetCreationTimeUtc(FileSystemInfo info)
  139. {
  140. // This could throw an error on some file systems that have dates out of range
  141. try
  142. {
  143. return info.CreationTimeUtc;
  144. }
  145. catch (Exception ex)
  146. {
  147. Logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
  148. return DateTime.MinValue;
  149. }
  150. }
  151. /// <summary>
  152. /// Gets the creation time UTC.
  153. /// </summary>
  154. /// <param name="info">The info.</param>
  155. /// <returns>DateTime.</returns>
  156. public DateTime GetLastWriteTimeUtc(FileSystemInfo info)
  157. {
  158. // This could throw an error on some file systems that have dates out of range
  159. try
  160. {
  161. return info.LastWriteTimeUtc;
  162. }
  163. catch (Exception ex)
  164. {
  165. Logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
  166. return DateTime.MinValue;
  167. }
  168. }
  169. /// <summary>
  170. /// Gets the last write time UTC.
  171. /// </summary>
  172. /// <param name="path">The path.</param>
  173. /// <returns>DateTime.</returns>
  174. public DateTime GetLastWriteTimeUtc(string path)
  175. {
  176. return GetLastWriteTimeUtc(GetFileSystemInfo(path));
  177. }
  178. /// <summary>
  179. /// Gets the file stream.
  180. /// </summary>
  181. /// <param name="path">The path.</param>
  182. /// <param name="mode">The mode.</param>
  183. /// <param name="access">The access.</param>
  184. /// <param name="share">The share.</param>
  185. /// <param name="isAsync">if set to <c>true</c> [is asynchronous].</param>
  186. /// <returns>FileStream.</returns>
  187. public FileStream GetFileStream(string path, FileMode mode, FileAccess access, FileShare share, bool isAsync = false)
  188. {
  189. if (_supportsAsyncFileStreams && isAsync)
  190. {
  191. return new FileStream(path, mode, access, share, 4096, true);
  192. }
  193. return new FileStream(path, mode, access, share);
  194. }
  195. /// <summary>
  196. /// Swaps the files.
  197. /// </summary>
  198. /// <param name="file1">The file1.</param>
  199. /// <param name="file2">The file2.</param>
  200. public void SwapFiles(string file1, string file2)
  201. {
  202. if (string.IsNullOrEmpty(file1))
  203. {
  204. throw new ArgumentNullException("file1");
  205. }
  206. if (string.IsNullOrEmpty(file2))
  207. {
  208. throw new ArgumentNullException("file2");
  209. }
  210. var temp1 = Path.GetTempFileName();
  211. var temp2 = Path.GetTempFileName();
  212. // Copying over will fail against hidden files
  213. RemoveHiddenAttribute(file1);
  214. RemoveHiddenAttribute(file2);
  215. File.Copy(file1, temp1, true);
  216. File.Copy(file2, temp2, true);
  217. File.Copy(temp1, file2, true);
  218. File.Copy(temp2, file1, true);
  219. File.Delete(temp1);
  220. File.Delete(temp2);
  221. }
  222. /// <summary>
  223. /// Removes the hidden attribute.
  224. /// </summary>
  225. /// <param name="path">The path.</param>
  226. private void RemoveHiddenAttribute(string path)
  227. {
  228. if (string.IsNullOrEmpty(path))
  229. {
  230. throw new ArgumentNullException("path");
  231. }
  232. var currentFile = new FileInfo(path);
  233. // This will fail if the file is hidden
  234. if (currentFile.Exists)
  235. {
  236. if ((currentFile.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  237. {
  238. currentFile.Attributes &= ~FileAttributes.Hidden;
  239. }
  240. }
  241. }
  242. public bool ContainsSubPath(string parentPath, string path)
  243. {
  244. if (string.IsNullOrEmpty(parentPath))
  245. {
  246. throw new ArgumentNullException("parentPath");
  247. }
  248. if (string.IsNullOrEmpty(path))
  249. {
  250. throw new ArgumentNullException("path");
  251. }
  252. return path.IndexOf(parentPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) != -1;
  253. }
  254. public bool IsRootPath(string path)
  255. {
  256. if (string.IsNullOrEmpty(path))
  257. {
  258. throw new ArgumentNullException("path");
  259. }
  260. var parent = Path.GetDirectoryName(path);
  261. if (!string.IsNullOrEmpty(parent))
  262. {
  263. return false;
  264. }
  265. return true;
  266. }
  267. public string NormalizePath(string path)
  268. {
  269. if (string.IsNullOrEmpty(path))
  270. {
  271. throw new ArgumentNullException("path");
  272. }
  273. if (path.EndsWith(":\\", StringComparison.OrdinalIgnoreCase))
  274. {
  275. return path;
  276. }
  277. return path.TrimEnd(Path.DirectorySeparatorChar);
  278. }
  279. }
  280. }