CommonFileSystem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists
  83. if (Path.HasExtension(path))
  84. {
  85. var fileInfo = new FileInfo(path);
  86. if (fileInfo.Exists)
  87. {
  88. return fileInfo;
  89. }
  90. return new DirectoryInfo(path);
  91. }
  92. else
  93. {
  94. var fileInfo = new DirectoryInfo(path);
  95. if (fileInfo.Exists)
  96. {
  97. return fileInfo;
  98. }
  99. return new FileInfo(path);
  100. }
  101. }
  102. /// <summary>
  103. /// The space char
  104. /// </summary>
  105. private const char SpaceChar = ' ';
  106. /// <summary>
  107. /// The invalid file name chars
  108. /// </summary>
  109. private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
  110. /// <summary>
  111. /// Takes a filename and removes invalid characters
  112. /// </summary>
  113. /// <param name="filename">The filename.</param>
  114. /// <returns>System.String.</returns>
  115. /// <exception cref="System.ArgumentNullException">filename</exception>
  116. public string GetValidFilename(string filename)
  117. {
  118. if (string.IsNullOrEmpty(filename))
  119. {
  120. throw new ArgumentNullException("filename");
  121. }
  122. var builder = new StringBuilder(filename);
  123. foreach (var c in InvalidFileNameChars)
  124. {
  125. builder = builder.Replace(c, SpaceChar);
  126. }
  127. return builder.ToString();
  128. }
  129. /// <summary>
  130. /// Gets the creation time UTC.
  131. /// </summary>
  132. /// <param name="info">The info.</param>
  133. /// <returns>DateTime.</returns>
  134. public DateTime GetCreationTimeUtc(FileSystemInfo info)
  135. {
  136. // This could throw an error on some file systems that have dates out of range
  137. try
  138. {
  139. return info.CreationTimeUtc;
  140. }
  141. catch (Exception ex)
  142. {
  143. Logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
  144. return DateTime.MinValue;
  145. }
  146. }
  147. /// <summary>
  148. /// Gets the creation time UTC.
  149. /// </summary>
  150. /// <param name="info">The info.</param>
  151. /// <param name="logger">The logger.</param>
  152. /// <returns>DateTime.</returns>
  153. public DateTime GetLastWriteTimeUtc(FileSystemInfo info)
  154. {
  155. // This could throw an error on some file systems that have dates out of range
  156. try
  157. {
  158. return info.LastWriteTimeUtc;
  159. }
  160. catch (Exception ex)
  161. {
  162. Logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
  163. return DateTime.MinValue;
  164. }
  165. }
  166. /// <summary>
  167. /// Gets the last write time UTC.
  168. /// </summary>
  169. /// <param name="path">The path.</param>
  170. /// <returns>DateTime.</returns>
  171. public DateTime GetLastWriteTimeUtc(string path)
  172. {
  173. return GetLastWriteTimeUtc(GetFileSystemInfo(path));
  174. }
  175. /// <summary>
  176. /// Gets the file stream.
  177. /// </summary>
  178. /// <param name="path">The path.</param>
  179. /// <param name="mode">The mode.</param>
  180. /// <param name="access">The access.</param>
  181. /// <param name="share">The share.</param>
  182. /// <param name="isAsync">if set to <c>true</c> [is asynchronous].</param>
  183. /// <returns>FileStream.</returns>
  184. public FileStream GetFileStream(string path, FileMode mode, FileAccess access, FileShare share, bool isAsync = false)
  185. {
  186. if (_supportsAsyncFileStreams && isAsync)
  187. {
  188. return new FileStream(path, mode, access, share, 4096, true);
  189. }
  190. return new FileStream(path, mode, access, share);
  191. }
  192. /// <summary>
  193. /// Swaps the files.
  194. /// </summary>
  195. /// <param name="file1">The file1.</param>
  196. /// <param name="file2">The file2.</param>
  197. public void SwapFiles(string file1, string file2)
  198. {
  199. var temp1 = Path.GetTempFileName();
  200. var temp2 = Path.GetTempFileName();
  201. // Copying over will fail against hidden files
  202. RemoveHiddenAttribute(file1);
  203. RemoveHiddenAttribute(file2);
  204. File.Copy(file1, temp1, true);
  205. File.Copy(file2, temp2, true);
  206. File.Copy(temp1, file2, true);
  207. File.Copy(temp2, file1, true);
  208. File.Delete(temp1);
  209. File.Delete(temp2);
  210. }
  211. /// <summary>
  212. /// Removes the hidden attribute.
  213. /// </summary>
  214. /// <param name="path">The path.</param>
  215. private void RemoveHiddenAttribute(string path)
  216. {
  217. var currentFile = new FileInfo(path);
  218. // This will fail if the file is hidden
  219. if (currentFile.Exists)
  220. {
  221. if ((currentFile.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  222. {
  223. currentFile.Attributes &= ~FileAttributes.Hidden;
  224. }
  225. }
  226. }
  227. }
  228. /// <summary>
  229. /// Adapted from http://stackoverflow.com/questions/309495/windows-shortcut-lnk-parser-in-java
  230. /// </summary>
  231. internal class WindowsShortcut
  232. {
  233. public bool IsDirectory { get; private set; }
  234. public bool IsLocal { get; private set; }
  235. public string ResolvedPath { get; private set; }
  236. public WindowsShortcut(string file)
  237. {
  238. ParseLink(File.ReadAllBytes(file), Encoding.UTF8);
  239. }
  240. private static bool isMagicPresent(byte[] link)
  241. {
  242. const int magic = 0x0000004C;
  243. const int magic_offset = 0x00;
  244. return link.Length >= 32 && bytesToDword(link, magic_offset) == magic;
  245. }
  246. /**
  247. * Gobbles up link data by parsing it and storing info in member fields
  248. * @param link all the bytes from the .lnk file
  249. */
  250. private void ParseLink(byte[] link, Encoding encoding)
  251. {
  252. if (!isMagicPresent(link))
  253. throw new IOException("Invalid shortcut; magic is missing", 0);
  254. // get the flags byte
  255. byte flags = link[0x14];
  256. // get the file attributes byte
  257. const int file_atts_offset = 0x18;
  258. byte file_atts = link[file_atts_offset];
  259. byte is_dir_mask = (byte)0x10;
  260. if ((file_atts & is_dir_mask) > 0)
  261. {
  262. IsDirectory = true;
  263. }
  264. else
  265. {
  266. IsDirectory = false;
  267. }
  268. // if the shell settings are present, skip them
  269. const int shell_offset = 0x4c;
  270. const byte has_shell_mask = (byte)0x01;
  271. int shell_len = 0;
  272. if ((flags & has_shell_mask) > 0)
  273. {
  274. // the plus 2 accounts for the length marker itself
  275. shell_len = bytesToWord(link, shell_offset) + 2;
  276. }
  277. // get to the file settings
  278. int file_start = 0x4c + shell_len;
  279. const int file_location_info_flag_offset_offset = 0x08;
  280. int file_location_info_flag = link[file_start + file_location_info_flag_offset_offset];
  281. IsLocal = (file_location_info_flag & 2) == 0;
  282. // get the local volume and local system values
  283. //final int localVolumeTable_offset_offset = 0x0C;
  284. const int basename_offset_offset = 0x10;
  285. const int networkVolumeTable_offset_offset = 0x14;
  286. const int finalname_offset_offset = 0x18;
  287. int finalname_offset = link[file_start + finalname_offset_offset] + file_start;
  288. String finalname = getNullDelimitedString(link, finalname_offset, encoding);
  289. if (IsLocal)
  290. {
  291. int basename_offset = link[file_start + basename_offset_offset] + file_start;
  292. String basename = getNullDelimitedString(link, basename_offset, encoding);
  293. ResolvedPath = basename + finalname;
  294. }
  295. else
  296. {
  297. int networkVolumeTable_offset = link[file_start + networkVolumeTable_offset_offset] + file_start;
  298. int shareName_offset_offset = 0x08;
  299. int shareName_offset = link[networkVolumeTable_offset + shareName_offset_offset]
  300. + networkVolumeTable_offset;
  301. String shareName = getNullDelimitedString(link, shareName_offset, encoding);
  302. ResolvedPath = shareName + "\\" + finalname;
  303. }
  304. }
  305. private static string getNullDelimitedString(byte[] bytes, int off, Encoding encoding)
  306. {
  307. int len = 0;
  308. // count bytes until the null character (0)
  309. while (true)
  310. {
  311. if (bytes[off + len] == 0)
  312. {
  313. break;
  314. }
  315. len++;
  316. }
  317. return encoding.GetString(bytes, off, len);
  318. }
  319. /*
  320. * convert two bytes into a short note, this is little endian because it's
  321. * for an Intel only OS.
  322. */
  323. private static int bytesToWord(byte[] bytes, int off)
  324. {
  325. return ((bytes[off + 1] & 0xff) << 8) | (bytes[off] & 0xff);
  326. }
  327. private static int bytesToDword(byte[] bytes, int off)
  328. {
  329. return (bytesToWord(bytes, off + 2) << 16) | bytesToWord(bytes, off);
  330. }
  331. }
  332. }