CommonFileSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. }
  193. /// <summary>
  194. /// Adapted from http://stackoverflow.com/questions/309495/windows-shortcut-lnk-parser-in-java
  195. /// </summary>
  196. internal class WindowsShortcut
  197. {
  198. public bool IsDirectory { get; private set; }
  199. public bool IsLocal { get; private set; }
  200. public string ResolvedPath { get; private set; }
  201. public WindowsShortcut(string file)
  202. {
  203. ParseLink(File.ReadAllBytes(file), Encoding.UTF8);
  204. }
  205. private static bool isMagicPresent(byte[] link)
  206. {
  207. const int magic = 0x0000004C;
  208. const int magic_offset = 0x00;
  209. return link.Length >= 32 && bytesToDword(link, magic_offset) == magic;
  210. }
  211. /**
  212. * Gobbles up link data by parsing it and storing info in member fields
  213. * @param link all the bytes from the .lnk file
  214. */
  215. private void ParseLink(byte[] link, Encoding encoding)
  216. {
  217. if (!isMagicPresent(link))
  218. throw new IOException("Invalid shortcut; magic is missing", 0);
  219. // get the flags byte
  220. byte flags = link[0x14];
  221. // get the file attributes byte
  222. const int file_atts_offset = 0x18;
  223. byte file_atts = link[file_atts_offset];
  224. byte is_dir_mask = (byte)0x10;
  225. if ((file_atts & is_dir_mask) > 0)
  226. {
  227. IsDirectory = true;
  228. }
  229. else
  230. {
  231. IsDirectory = false;
  232. }
  233. // if the shell settings are present, skip them
  234. const int shell_offset = 0x4c;
  235. const byte has_shell_mask = (byte)0x01;
  236. int shell_len = 0;
  237. if ((flags & has_shell_mask) > 0)
  238. {
  239. // the plus 2 accounts for the length marker itself
  240. shell_len = bytesToWord(link, shell_offset) + 2;
  241. }
  242. // get to the file settings
  243. int file_start = 0x4c + shell_len;
  244. const int file_location_info_flag_offset_offset = 0x08;
  245. int file_location_info_flag = link[file_start + file_location_info_flag_offset_offset];
  246. IsLocal = (file_location_info_flag & 2) == 0;
  247. // get the local volume and local system values
  248. //final int localVolumeTable_offset_offset = 0x0C;
  249. const int basename_offset_offset = 0x10;
  250. const int networkVolumeTable_offset_offset = 0x14;
  251. const int finalname_offset_offset = 0x18;
  252. int finalname_offset = link[file_start + finalname_offset_offset] + file_start;
  253. String finalname = getNullDelimitedString(link, finalname_offset, encoding);
  254. if (IsLocal)
  255. {
  256. int basename_offset = link[file_start + basename_offset_offset] + file_start;
  257. String basename = getNullDelimitedString(link, basename_offset, encoding);
  258. ResolvedPath = basename + finalname;
  259. }
  260. else
  261. {
  262. int networkVolumeTable_offset = link[file_start + networkVolumeTable_offset_offset] + file_start;
  263. int shareName_offset_offset = 0x08;
  264. int shareName_offset = link[networkVolumeTable_offset + shareName_offset_offset]
  265. + networkVolumeTable_offset;
  266. String shareName = getNullDelimitedString(link, shareName_offset, encoding);
  267. ResolvedPath = shareName + "\\" + finalname;
  268. }
  269. }
  270. private static string getNullDelimitedString(byte[] bytes, int off, Encoding encoding)
  271. {
  272. int len = 0;
  273. // count bytes until the null character (0)
  274. while (true)
  275. {
  276. if (bytes[off + len] == 0)
  277. {
  278. break;
  279. }
  280. len++;
  281. }
  282. return encoding.GetString(bytes, off, len);
  283. }
  284. /*
  285. * convert two bytes into a short note, this is little endian because it's
  286. * for an Intel only OS.
  287. */
  288. private static int bytesToWord(byte[] bytes, int off)
  289. {
  290. return ((bytes[off + 1] & 0xff) << 8) | (bytes[off] & 0xff);
  291. }
  292. private static int bytesToDword(byte[] bytes, int off)
  293. {
  294. return (bytesToWord(bytes, off + 2) << 16) | bytesToWord(bytes, off);
  295. }
  296. }
  297. }