CommonFileSystem.cs 11 KB

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