2
0

CommonFileSystem.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #if __MonoCS__
  114. private static readonly char[] InvalidFileNameChars = new char [41] { '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
  115. '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F', '\x10', '\x11', '\x12',
  116. '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D',
  117. '\x1E', '\x1F', '\x22', '\x3C', '\x3E', '\x7C', ':', '*', '?', '\\', '/' };
  118. #else
  119. private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
  120. #endif
  121. /// <summary>
  122. /// Takes a filename and removes invalid characters
  123. /// </summary>
  124. /// <param name="filename">The filename.</param>
  125. /// <returns>System.String.</returns>
  126. /// <exception cref="System.ArgumentNullException">filename</exception>
  127. public string GetValidFilename(string filename)
  128. {
  129. if (string.IsNullOrEmpty(filename))
  130. {
  131. throw new ArgumentNullException("filename");
  132. }
  133. var builder = new StringBuilder(filename);
  134. foreach (var c in InvalidFileNameChars)
  135. {
  136. builder = builder.Replace(c, SpaceChar);
  137. }
  138. return builder.ToString();
  139. }
  140. /// <summary>
  141. /// Gets the creation time UTC.
  142. /// </summary>
  143. /// <param name="info">The info.</param>
  144. /// <returns>DateTime.</returns>
  145. public DateTime GetCreationTimeUtc(FileSystemInfo info)
  146. {
  147. // This could throw an error on some file systems that have dates out of range
  148. try
  149. {
  150. return info.CreationTimeUtc;
  151. }
  152. catch (Exception ex)
  153. {
  154. Logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
  155. return DateTime.MinValue;
  156. }
  157. }
  158. /// <summary>
  159. /// Gets the creation time UTC.
  160. /// </summary>
  161. /// <param name="info">The info.</param>
  162. /// <returns>DateTime.</returns>
  163. public DateTime GetLastWriteTimeUtc(FileSystemInfo info)
  164. {
  165. // This could throw an error on some file systems that have dates out of range
  166. try
  167. {
  168. return info.LastWriteTimeUtc;
  169. }
  170. catch (Exception ex)
  171. {
  172. Logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
  173. return DateTime.MinValue;
  174. }
  175. }
  176. /// <summary>
  177. /// Gets the last write time UTC.
  178. /// </summary>
  179. /// <param name="path">The path.</param>
  180. /// <returns>DateTime.</returns>
  181. public DateTime GetLastWriteTimeUtc(string path)
  182. {
  183. return GetLastWriteTimeUtc(GetFileSystemInfo(path));
  184. }
  185. /// <summary>
  186. /// Gets the file stream.
  187. /// </summary>
  188. /// <param name="path">The path.</param>
  189. /// <param name="mode">The mode.</param>
  190. /// <param name="access">The access.</param>
  191. /// <param name="share">The share.</param>
  192. /// <param name="isAsync">if set to <c>true</c> [is asynchronous].</param>
  193. /// <returns>FileStream.</returns>
  194. public FileStream GetFileStream(string path, FileMode mode, FileAccess access, FileShare share, bool isAsync = false)
  195. {
  196. if (_supportsAsyncFileStreams && isAsync)
  197. {
  198. return new FileStream(path, mode, access, share, 4096, true);
  199. }
  200. return new FileStream(path, mode, access, share);
  201. }
  202. /// <summary>
  203. /// Swaps the files.
  204. /// </summary>
  205. /// <param name="file1">The file1.</param>
  206. /// <param name="file2">The file2.</param>
  207. public void SwapFiles(string file1, string file2)
  208. {
  209. if (string.IsNullOrEmpty(file1))
  210. {
  211. throw new ArgumentNullException("file1");
  212. }
  213. if (string.IsNullOrEmpty(file2))
  214. {
  215. throw new ArgumentNullException("file2");
  216. }
  217. var temp1 = Path.GetTempFileName();
  218. var temp2 = Path.GetTempFileName();
  219. // Copying over will fail against hidden files
  220. RemoveHiddenAttribute(file1);
  221. RemoveHiddenAttribute(file2);
  222. File.Copy(file1, temp1, true);
  223. File.Copy(file2, temp2, true);
  224. File.Copy(temp1, file2, true);
  225. File.Copy(temp2, file1, true);
  226. File.Delete(temp1);
  227. File.Delete(temp2);
  228. }
  229. /// <summary>
  230. /// Removes the hidden attribute.
  231. /// </summary>
  232. /// <param name="path">The path.</param>
  233. private void RemoveHiddenAttribute(string path)
  234. {
  235. if (string.IsNullOrEmpty(path))
  236. {
  237. throw new ArgumentNullException("path");
  238. }
  239. var currentFile = new FileInfo(path);
  240. // This will fail if the file is hidden
  241. if (currentFile.Exists)
  242. {
  243. if ((currentFile.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  244. {
  245. currentFile.Attributes &= ~FileAttributes.Hidden;
  246. }
  247. }
  248. }
  249. public bool ContainsSubPath(string parentPath, string path)
  250. {
  251. if (string.IsNullOrEmpty(parentPath))
  252. {
  253. throw new ArgumentNullException("parentPath");
  254. }
  255. if (string.IsNullOrEmpty(path))
  256. {
  257. throw new ArgumentNullException("path");
  258. }
  259. return path.IndexOf(parentPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) != -1;
  260. }
  261. public bool IsRootPath(string path)
  262. {
  263. if (string.IsNullOrEmpty(path))
  264. {
  265. throw new ArgumentNullException("path");
  266. }
  267. var parent = Path.GetDirectoryName(path);
  268. if (!string.IsNullOrEmpty(parent))
  269. {
  270. return false;
  271. }
  272. return true;
  273. }
  274. public string NormalizePath(string path)
  275. {
  276. if (string.IsNullOrEmpty(path))
  277. {
  278. throw new ArgumentNullException("path");
  279. }
  280. if (path.EndsWith(":\\", StringComparison.OrdinalIgnoreCase))
  281. {
  282. return path;
  283. }
  284. return path.TrimEnd(Path.DirectorySeparatorChar);
  285. }
  286. }
  287. }