FileData.cs 5.3 KB

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