2
0

FileData.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Logging;
  3. using MediaBrowser.Common.Win32;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Model.Logging;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Runtime.InteropServices;
  10. namespace MediaBrowser.Controller.IO
  11. {
  12. /// <summary>
  13. /// Provides low level File access that is much faster than the File/Directory api's
  14. /// </summary>
  15. public static class FileData
  16. {
  17. /// <summary>
  18. /// The logger
  19. /// </summary>
  20. private static readonly ILogger Logger = LogManager.GetLogger("FileData");
  21. /// <summary>
  22. /// Gets all file system entries within a foler
  23. /// </summary>
  24. /// <param name="path">The path.</param>
  25. /// <param name="searchPattern">The search pattern.</param>
  26. /// <param name="includeFiles">if set to <c>true</c> [include files].</param>
  27. /// <param name="includeDirectories">if set to <c>true</c> [include directories].</param>
  28. /// <param name="flattenFolderDepth">The flatten folder depth.</param>
  29. /// <param name="args">The args.</param>
  30. /// <returns>Dictionary{System.StringWIN32_FIND_DATA}.</returns>
  31. /// <exception cref="System.ArgumentNullException"></exception>
  32. /// <exception cref="System.IO.IOException">GetFileSystemEntries failed</exception>
  33. public static Dictionary<string, WIN32_FIND_DATA> GetFilteredFileSystemEntries(string path, string searchPattern = "*", bool includeFiles = true, bool includeDirectories = true, int flattenFolderDepth = 0, ItemResolveArgs args = null)
  34. {
  35. if (string.IsNullOrEmpty(path))
  36. {
  37. throw new ArgumentNullException();
  38. }
  39. var lpFileName = Path.Combine(path, searchPattern);
  40. WIN32_FIND_DATA lpFindFileData;
  41. var handle = NativeMethods.FindFirstFileEx(lpFileName, FINDEX_INFO_LEVELS.FindExInfoBasic, out lpFindFileData,
  42. FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, FindFirstFileExFlags.FIND_FIRST_EX_LARGE_FETCH);
  43. if (handle == IntPtr.Zero)
  44. {
  45. int hr = Marshal.GetLastWin32Error();
  46. if (hr != 2 && hr != 0x12)
  47. {
  48. throw new IOException("GetFileSystemEntries failed");
  49. }
  50. return new Dictionary<string, WIN32_FIND_DATA>(StringComparer.OrdinalIgnoreCase);
  51. }
  52. var dict = new Dictionary<string, WIN32_FIND_DATA>(StringComparer.OrdinalIgnoreCase);
  53. if (FileSystem.IncludeInFindFileOutput(lpFindFileData.cFileName, lpFindFileData.dwFileAttributes, includeFiles, includeDirectories))
  54. {
  55. if (!string.IsNullOrEmpty(lpFindFileData.cFileName))
  56. {
  57. lpFindFileData.Path = Path.Combine(path, lpFindFileData.cFileName);
  58. dict[lpFindFileData.Path] = lpFindFileData;
  59. }
  60. }
  61. while (NativeMethods.FindNextFile(handle, out lpFindFileData) != IntPtr.Zero)
  62. {
  63. // This is the one circumstance where we can completely disregard a file
  64. if (lpFindFileData.IsSystemFile)
  65. {
  66. continue;
  67. }
  68. // Filter out invalid entries
  69. if (lpFindFileData.cFileName.Equals(".", StringComparison.OrdinalIgnoreCase))
  70. {
  71. continue;
  72. }
  73. if (lpFindFileData.cFileName.Equals("..", StringComparison.OrdinalIgnoreCase))
  74. {
  75. continue;
  76. }
  77. lpFindFileData.Path = Path.Combine(path, lpFindFileData.cFileName);
  78. if (FileSystem.IsShortcut(lpFindFileData.Path))
  79. {
  80. var newPath = FileSystem.ResolveShortcut(lpFindFileData.Path);
  81. if (string.IsNullOrWhiteSpace(newPath))
  82. {
  83. //invalid shortcut - could be old or target could just be unavailable
  84. Logger.Warn("Encountered invalid shortuct: "+lpFindFileData.Path);
  85. continue;
  86. }
  87. var data = FileSystem.GetFileData(newPath);
  88. if (data.HasValue)
  89. {
  90. lpFindFileData = data.Value;
  91. // Find out if the shortcut is pointing to a directory or file
  92. if (lpFindFileData.IsDirectory)
  93. {
  94. // add to our physical locations
  95. if (args != null)
  96. {
  97. args.AddAdditionalLocation(newPath);
  98. }
  99. }
  100. dict[lpFindFileData.Path] = lpFindFileData;
  101. }
  102. }
  103. else if (flattenFolderDepth > 0 && lpFindFileData.IsDirectory)
  104. {
  105. foreach (var child in GetFilteredFileSystemEntries(lpFindFileData.Path, flattenFolderDepth: flattenFolderDepth - 1))
  106. {
  107. dict[child.Key] = child.Value;
  108. }
  109. }
  110. else
  111. {
  112. dict[lpFindFileData.Path] = lpFindFileData;
  113. }
  114. }
  115. NativeMethods.FindClose(handle);
  116. return dict;
  117. }
  118. }
  119. }