aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/IO/FileData.cs
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2013-04-28 01:29:27 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2013-04-28 01:29:27 -0400
commitb443d591a29bc18daea36a3871908a4c1c277990 (patch)
tree9f671e0484c1b42bd5cecfe58a4d7cc3e7a6ddc7 /MediaBrowser.Controller/IO/FileData.cs
parent90bb3d46c416676105d5cf89d12279b3f7ccb944 (diff)
fixes #200 - MB3 Locking Folders for a long time
Diffstat (limited to 'MediaBrowser.Controller/IO/FileData.cs')
-rw-r--r--MediaBrowser.Controller/IO/FileData.cs90
1 files changed, 23 insertions, 67 deletions
diff --git a/MediaBrowser.Controller/IO/FileData.cs b/MediaBrowser.Controller/IO/FileData.cs
index 4571c1ad0..3f15d8ddb 100644
--- a/MediaBrowser.Controller/IO/FileData.cs
+++ b/MediaBrowser.Controller/IO/FileData.cs
@@ -3,7 +3,7 @@ using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.IO;
-using System.Runtime.InteropServices;
+using System.Linq;
namespace MediaBrowser.Controller.IO
{
@@ -13,91 +13,47 @@ namespace MediaBrowser.Controller.IO
public static class FileData
{
/// <summary>
- /// Gets all file system entries within a foler
+ /// Gets the filtered file system entries.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="logger">The logger.</param>
/// <param name="searchPattern">The search pattern.</param>
- /// <param name="includeFiles">if set to <c>true</c> [include files].</param>
- /// <param name="includeDirectories">if set to <c>true</c> [include directories].</param>
/// <param name="flattenFolderDepth">The flatten folder depth.</param>
/// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param>
/// <param name="args">The args.</param>
- /// <returns>Dictionary{System.StringWIN32_FIND_DATA}.</returns>
- /// <exception cref="System.ArgumentNullException"></exception>
- /// <exception cref="System.IO.IOException">GetFileSystemEntries failed</exception>
- public static Dictionary<string, WIN32_FIND_DATA> GetFilteredFileSystemEntries(string path, ILogger logger, string searchPattern = "*", bool includeFiles = true, bool includeDirectories = true, int flattenFolderDepth = 0, bool resolveShortcuts = true, ItemResolveArgs args = null)
+ /// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
+ /// <exception cref="System.ArgumentNullException">path</exception>
+ public static Dictionary<string, FileSystemInfo> GetFilteredFileSystemEntries(string path, ILogger logger, string searchPattern = "*", int flattenFolderDepth = 0, bool resolveShortcuts = true, ItemResolveArgs args = null)
{
if (string.IsNullOrEmpty(path))
{
- throw new ArgumentNullException();
+ throw new ArgumentNullException("path");
}
- var lpFileName = Path.Combine(path, searchPattern);
+ var dict = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
+
+ var entries = new DirectoryInfo(path).EnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly)
+ .Where(i => !i.Attributes.HasFlag(FileAttributes.System) && !i.Name.Equals(".") && !i.Name.Equals(".."));
- WIN32_FIND_DATA lpFindFileData;
- var handle = NativeMethods.FindFirstFileEx(lpFileName, FINDEX_INFO_LEVELS.FindExInfoBasic, out lpFindFileData,
- FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, FindFirstFileExFlags.FIND_FIRST_EX_LARGE_FETCH);
-
- if (handle == IntPtr.Zero)
- {
- int hr = Marshal.GetLastWin32Error();
- if (hr != 2 && hr != 0x12)
- {
- throw new IOException("GetFileSystemEntries failed");
- }
- return new Dictionary<string, WIN32_FIND_DATA>(StringComparer.OrdinalIgnoreCase);
- }
-
- var dict = new Dictionary<string, WIN32_FIND_DATA>(StringComparer.OrdinalIgnoreCase);
-
- if (FileSystem.IncludeInFindFileOutput(lpFindFileData.cFileName, lpFindFileData.dwFileAttributes, includeFiles, includeDirectories))
+ foreach (var entry in entries)
{
- if (!string.IsNullOrEmpty(lpFindFileData.cFileName))
- {
- lpFindFileData.Path = Path.Combine(path, lpFindFileData.cFileName);
-
- dict[lpFindFileData.Path] = lpFindFileData;
- }
- }
+ var isDirectory = entry.Attributes.HasFlag(FileAttributes.Directory);
- while (NativeMethods.FindNextFile(handle, out lpFindFileData) != IntPtr.Zero)
- {
- // This is the one circumstance where we can completely disregard a file
- if (lpFindFileData.IsSystemFile)
+ if (resolveShortcuts && FileSystem.IsShortcut(entry.FullName))
{
- continue;
- }
-
- // Filter out invalid entries
- if (lpFindFileData.cFileName.Equals(".", StringComparison.OrdinalIgnoreCase))
- {
- continue;
- }
- if (lpFindFileData.cFileName.Equals("..", StringComparison.OrdinalIgnoreCase))
- {
- continue;
- }
-
- lpFindFileData.Path = Path.Combine(path, lpFindFileData.cFileName);
-
- if (resolveShortcuts && FileSystem.IsShortcut(lpFindFileData.Path))
- {
- var newPath = FileSystem.ResolveShortcut(lpFindFileData.Path);
+ var newPath = FileSystem.ResolveShortcut(entry.FullName);
if (string.IsNullOrWhiteSpace(newPath))
{
//invalid shortcut - could be old or target could just be unavailable
- logger.Warn("Encountered invalid shortuct: " + lpFindFileData.Path);
+ logger.Warn("Encountered invalid shortuct: " + entry.FullName);
continue;
}
- var data = FileSystem.GetFileData(newPath);
+ var data = FileSystem.GetFileSystemInfo(newPath);
- if (data.HasValue)
+ if (data.Exists)
{
- lpFindFileData = data.Value;
-
// Find out if the shortcut is pointing to a directory or file
- if (lpFindFileData.IsDirectory)
+ if (data.Attributes.HasFlag(FileAttributes.Directory))
{
// add to our physical locations
if (args != null)
@@ -106,25 +62,25 @@ namespace MediaBrowser.Controller.IO
}
}
- dict[lpFindFileData.Path] = lpFindFileData;
+ dict[data.FullName] = data;
}
}
- else if (flattenFolderDepth > 0 && lpFindFileData.IsDirectory)
+ else if (flattenFolderDepth > 0 && isDirectory)
{
- foreach (var child in GetFilteredFileSystemEntries(lpFindFileData.Path, logger, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
+ foreach (var child in GetFilteredFileSystemEntries(entry.FullName, logger, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
{
dict[child.Key] = child.Value;
}
}
else
{
- dict[lpFindFileData.Path] = lpFindFileData;
+ dict[entry.FullName] = entry;
}
}
- NativeMethods.FindClose(handle);
return dict;
}
+
}
}