diff options
| author | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-20 20:33:05 -0500 |
|---|---|---|
| committer | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-20 20:33:05 -0500 |
| commit | 767cdc1f6f6a63ce997fc9476911e2c361f9d402 (patch) | |
| tree | 49add55976f895441167c66cfa95e5c7688d18ce /MediaBrowser.Controller/IO/FileData.cs | |
| parent | 845554722efaed872948a9e0f7202e3ef52f1b6e (diff) | |
Pushing missing changes
Diffstat (limited to 'MediaBrowser.Controller/IO/FileData.cs')
| -rw-r--r-- | MediaBrowser.Controller/IO/FileData.cs | 381 |
1 files changed, 130 insertions, 251 deletions
diff --git a/MediaBrowser.Controller/IO/FileData.cs b/MediaBrowser.Controller/IO/FileData.cs index 4ae2ee72f..74d0a7e6f 100644 --- a/MediaBrowser.Controller/IO/FileData.cs +++ b/MediaBrowser.Controller/IO/FileData.cs @@ -1,251 +1,130 @@ -using MediaBrowser.Common.Logging;
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Runtime.InteropServices;
-
-namespace MediaBrowser.Controller.IO
-{
- /// <summary>
- /// Provides low level File access that is much faster than the File/Directory api's
- /// </summary>
- public static class FileData
- {
- public const int MAX_PATH = 260;
- public const int MAX_ALTERNATE = 14;
- public static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
-
- /// <summary>
- /// Gets information about a path
- /// </summary>
- public static WIN32_FIND_DATA GetFileData(string path)
- {
- WIN32_FIND_DATA data;
- IntPtr handle = FindFirstFile(path, out data);
- bool getFilename = false;
-
- if (handle == INVALID_HANDLE_VALUE && !Path.HasExtension(path))
- {
- if (!path.EndsWith("*"))
- {
- Logger.LogInfo("Handle came back invalid for {0}. Since this is a directory we'll try appending \\*.", path);
-
- FindClose(handle);
-
- handle = FindFirstFile(Path.Combine(path, "*"), out data);
-
- getFilename = true;
- }
- }
-
- if (handle == IntPtr.Zero)
- {
- throw new IOException("FindFirstFile failed");
- }
-
- if (getFilename)
- {
- data.cFileName = Path.GetFileName(path);
- }
-
- FindClose(handle);
-
- data.Path = path;
- return data;
- }
-
- /// <summary>
- /// Gets all file system entries within a foler
- /// </summary>
- public static IEnumerable<WIN32_FIND_DATA> GetFileSystemEntries(string path, string searchPattern)
- {
- return GetFileSystemEntries(path, searchPattern, true, true);
- }
-
- /// <summary>
- /// Gets all files within a folder
- /// </summary>
- public static IEnumerable<WIN32_FIND_DATA> GetFiles(string path, string searchPattern)
- {
- return GetFileSystemEntries(path, searchPattern, true, false);
- }
-
- /// <summary>
- /// Gets all sub-directories within a folder
- /// </summary>
- public static IEnumerable<WIN32_FIND_DATA> GetDirectories(string path, string searchPattern)
- {
- return GetFileSystemEntries(path, searchPattern, false, true);
- }
-
- /// <summary>
- /// Gets all file system entries within a foler
- /// </summary>
- public static IEnumerable<WIN32_FIND_DATA> GetFileSystemEntries(string path, string searchPattern, bool includeFiles, bool includeDirectories)
- {
- string lpFileName = Path.Combine(path, searchPattern);
-
- WIN32_FIND_DATA lpFindFileData;
- var handle = FindFirstFile(lpFileName, out lpFindFileData);
-
- if (handle == IntPtr.Zero)
- {
- int hr = Marshal.GetLastWin32Error();
- if (hr != 2 && hr != 0x12)
- {
- throw new IOException("GetFileSystemEntries failed");
- }
- yield break;
- }
-
- if (IncludeInOutput(lpFindFileData.cFileName, lpFindFileData.dwFileAttributes, includeFiles, includeDirectories))
- {
- yield return lpFindFileData;
- }
-
- while (FindNextFile(handle, out lpFindFileData) != IntPtr.Zero)
- {
- if (IncludeInOutput(lpFindFileData.cFileName, lpFindFileData.dwFileAttributes, includeFiles, includeDirectories))
- {
- lpFindFileData.Path = Path.Combine(path, lpFindFileData.cFileName);
- yield return lpFindFileData;
- }
- }
-
- FindClose(handle);
- }
-
- private static bool IncludeInOutput(string cFileName, FileAttributes attributes, bool includeFiles, bool includeDirectories)
- {
- if (cFileName.Equals(".", StringComparison.OrdinalIgnoreCase))
- {
- return false;
- }
- if (cFileName.Equals("..", StringComparison.OrdinalIgnoreCase))
- {
- return false;
- }
-
- if (!includeFiles && !attributes.HasFlag(FileAttributes.Directory))
- {
- return false;
- }
-
- if (!includeDirectories && attributes.HasFlag(FileAttributes.Directory))
- {
- return false;
- }
-
- return true;
- }
-
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr FindFirstFile(string fileName, out WIN32_FIND_DATA data);
-
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA data);
-
- [DllImport("kernel32")]
- private static extern bool FindClose(IntPtr hFindFile);
-
- private const char SpaceChar = ' ';
- private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
-
- /// <summary>
- /// Takes a filename and removes invalid characters
- /// </summary>
- public static string GetValidFilename(string filename)
- {
- foreach (char c in InvalidFileNameChars)
- {
- filename = filename.Replace(c, SpaceChar);
- }
-
- return filename;
- }
- }
-
- [StructLayout(LayoutKind.Sequential)]
- public struct FILETIME
- {
- public uint dwLowDateTime;
- public uint dwHighDateTime;
- }
-
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- public struct WIN32_FIND_DATA
- {
- public FileAttributes dwFileAttributes;
- public FILETIME ftCreationTime;
- public FILETIME ftLastAccessTime;
- public FILETIME ftLastWriteTime;
- public int nFileSizeHigh;
- public int nFileSizeLow;
- public int dwReserved0;
- public int dwReserved1;
-
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = FileData.MAX_PATH)]
- public string cFileName;
-
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = FileData.MAX_ALTERNATE)]
- public string cAlternate;
-
- public bool IsHidden
- {
- get
- {
- return dwFileAttributes.HasFlag(FileAttributes.Hidden);
- }
- }
-
- public bool IsSystemFile
- {
- get
- {
- return dwFileAttributes.HasFlag(FileAttributes.System);
- }
- }
-
- public bool IsDirectory
- {
- get
- {
- return dwFileAttributes.HasFlag(FileAttributes.Directory);
- }
- }
-
- public DateTime CreationTimeUtc
- {
- get
- {
- return ParseFileTime(ftCreationTime);
- }
- }
-
- public DateTime LastAccessTimeUtc
- {
- get
- {
- return ParseFileTime(ftLastAccessTime);
- }
- }
-
- public DateTime LastWriteTimeUtc
- {
- get
- {
- return ParseFileTime(ftLastWriteTime);
- }
- }
-
- private DateTime ParseFileTime(FILETIME filetime)
- {
- long highBits = filetime.dwHighDateTime;
- highBits = highBits << 32;
- return DateTime.FromFileTimeUtc(highBits + (long)filetime.dwLowDateTime);
- }
-
- public string Path { get; set; }
- }
-
-}
+using MediaBrowser.Common.IO; +using MediaBrowser.Common.Logging; +using MediaBrowser.Common.Win32; +using MediaBrowser.Controller.Library; +using System; +using System.Collections.Generic; +using System.IO; +using System.Runtime.InteropServices; + +namespace MediaBrowser.Controller.IO +{ + /// <summary> + /// Provides low level File access that is much faster than the File/Directory api's + /// </summary> + public static class FileData + { + /// <summary> + /// Gets all file system entries within a foler + /// </summary> + /// <param name="path">The path.</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="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, string searchPattern = "*", bool includeFiles = true, bool includeDirectories = true, int flattenFolderDepth = 0, ItemResolveArgs args = null) + { + if (string.IsNullOrEmpty(path)) + { + throw new ArgumentNullException(); + } + + var lpFileName = Path.Combine(path, searchPattern); + + 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)) + { + if (!string.IsNullOrEmpty(lpFindFileData.cFileName)) + { + lpFindFileData.Path = Path.Combine(path, lpFindFileData.cFileName); + + dict[lpFindFileData.Path] = lpFindFileData; + } + } + + while (NativeMethods.FindNextFile(handle, out lpFindFileData) != IntPtr.Zero) + { + // This is the one circumstance where we can completely disregard a file + if (lpFindFileData.IsSystemFile) + { + 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 (FileSystem.IsShortcut(lpFindFileData.Path)) + { + var newPath = FileSystem.ResolveShortcut(lpFindFileData.Path); + if (string.IsNullOrWhiteSpace(newPath)) + { + //invalid shortcut - could be old or target could just be unavailable + Logger.LogWarning("Encountered invalid shortuct: "+lpFindFileData.Path); + continue; + } + var data = FileSystem.GetFileData(newPath); + + if (data.HasValue) + { + lpFindFileData = data.Value; + + // Find out if the shortcut is pointing to a directory or file + if (lpFindFileData.IsDirectory) + { + // add to our physical locations + if (args != null) + { + args.AddAdditionalLocation(newPath); + } + } + + dict[lpFindFileData.Path] = lpFindFileData; + } + } + else if (flattenFolderDepth > 0 && lpFindFileData.IsDirectory) + { + foreach (var child in GetFilteredFileSystemEntries(lpFindFileData.Path, flattenFolderDepth: flattenFolderDepth - 1)) + { + dict[child.Key] = child.Value; + } + } + else + { + dict[lpFindFileData.Path] = lpFindFileData; + } + } + + NativeMethods.FindClose(handle); + return dict; + } + } + +} |
