FileData.cs 5.4 KB

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