From b443d591a29bc18daea36a3871908a4c1c277990 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 28 Apr 2013 01:29:27 -0400 Subject: fixes #200 - MB3 Locking Folders for a long time --- MediaBrowser.Controller/IO/FileData.cs | 90 +++++------------ MediaBrowser.Controller/IO/FileSystem.cs | 159 ++++++++----------------------- 2 files changed, 64 insertions(+), 185 deletions(-) (limited to 'MediaBrowser.Controller/IO') 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 { /// - /// Gets all file system entries within a foler + /// Gets the filtered file system entries. /// /// The path. /// The logger. /// The search pattern. - /// if set to true [include files]. - /// if set to true [include directories]. /// The flatten folder depth. /// if set to true [resolve shortcuts]. /// The args. - /// Dictionary{System.StringWIN32_FIND_DATA}. - /// - /// GetFileSystemEntries failed - public static Dictionary GetFilteredFileSystemEntries(string path, ILogger logger, string searchPattern = "*", bool includeFiles = true, bool includeDirectories = true, int flattenFolderDepth = 0, bool resolveShortcuts = true, ItemResolveArgs args = null) + /// Dictionary{System.StringFileSystemInfo}. + /// path + public static Dictionary 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(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(StringComparer.OrdinalIgnoreCase); - } - - var dict = new Dictionary(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; } + } } diff --git a/MediaBrowser.Controller/IO/FileSystem.cs b/MediaBrowser.Controller/IO/FileSystem.cs index 7b031744e..cbe561fdf 100644 --- a/MediaBrowser.Controller/IO/FileSystem.cs +++ b/MediaBrowser.Controller/IO/FileSystem.cs @@ -1,8 +1,7 @@ -using System; -using System.Collections.Generic; +using MediaBrowser.Model.Logging; +using System; using System.Collections.Specialized; using System.IO; -using System.Runtime.InteropServices; using System.Text; namespace MediaBrowser.Controller.IO @@ -13,153 +12,77 @@ namespace MediaBrowser.Controller.IO public static class FileSystem { /// - /// Gets information about a path + /// Gets the file system info. /// /// The path. - /// System.Nullable{WIN32_FIND_DATA}. - /// path - /// GetFileData failed for + path - public static WIN32_FIND_DATA? GetFileData(string path) + /// FileSystemInfo. + public static FileSystemInfo GetFileSystemInfo(string path) { - if (string.IsNullOrEmpty(path)) + // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists + if (Path.HasExtension(path)) { - throw new ArgumentNullException("path"); - } - - WIN32_FIND_DATA data; - var handle = NativeMethods.FindFirstFileEx(path, FINDEX_INFO_LEVELS.FindExInfoBasic, out data, - FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, FindFirstFileExFlags.NONE); + var fileInfo = new FileInfo(path); - var getFilename = false; - - if (handle == NativeMethods.INVALID_HANDLE_VALUE && !Path.HasExtension(path)) - { - if (!path.EndsWith("*", StringComparison.OrdinalIgnoreCase)) + if (fileInfo.Exists) { - NativeMethods.FindClose(handle); - - handle = NativeMethods.FindFirstFileEx(Path.Combine(path, "*"), FINDEX_INFO_LEVELS.FindExInfoBasic, out data, - FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, FindFirstFileExFlags.NONE); - - getFilename = true; + return fileInfo; } - } - - if (handle == IntPtr.Zero) - { - throw new IOException("GetFileData failed for " + path); - } - NativeMethods.FindClose(handle); - - // According to MSDN documentation, this will default to 1601 for paths that don't exist. - if (data.CreationTimeUtc.Year == 1601) - { - return null; + return new DirectoryInfo(path); } - - if (getFilename) + else { - data.cFileName = Path.GetFileName(path); - } + var fileInfo = new DirectoryInfo(path); - data.Path = path; - return data; - } + if (fileInfo.Exists) + { + return fileInfo; + } - /// - /// Gets all files within a folder - /// - /// The path. - /// The search pattern. - /// IEnumerable{WIN32_FIND_DATA}. - public static IEnumerable GetFiles(string path, string searchPattern = "*") - { - return GetFileSystemEntries(path, searchPattern, includeDirectories: false); + return new FileInfo(path); + } } /// - /// Gets all file system entries within a foler + /// Gets the creation time UTC. /// - /// The path. - /// The search pattern. - /// if set to true [include files]. - /// if set to true [include directories]. - /// IEnumerable{WIN32_FIND_DATA}. - /// path - /// GetFileSystemEntries failed - public static IEnumerable GetFileSystemEntries(string path, string searchPattern = "*", bool includeFiles = true, bool includeDirectories = true) + /// The info. + /// The logger. + /// DateTime. + public static DateTime GetLastWriteTimeUtc(FileSystemInfo info, ILogger logger) { - if (string.IsNullOrEmpty(path)) - { - throw new ArgumentNullException("path"); - } - - 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) - { - var hr = Marshal.GetLastWin32Error(); - if (hr != 2 && hr != 0x12) - { - throw new IOException("GetFileSystemEntries failed"); - } - yield break; - } + // This could throw an error on some file systems that have dates out of range - if (IncludeInFindFileOutput(lpFindFileData.cFileName, lpFindFileData.dwFileAttributes, includeFiles, includeDirectories)) + try { - lpFindFileData.Path = Path.Combine(path, lpFindFileData.cFileName); - - yield return lpFindFileData; + return info.LastAccessTimeUtc; } - - while (NativeMethods.FindNextFile(handle, out lpFindFileData) != IntPtr.Zero) + catch (Exception ex) { - if (IncludeInFindFileOutput(lpFindFileData.cFileName, lpFindFileData.dwFileAttributes, includeFiles, includeDirectories)) - { - lpFindFileData.Path = Path.Combine(path, lpFindFileData.cFileName); - yield return lpFindFileData; - } + logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName); + return DateTime.MinValue; } - - NativeMethods.FindClose(handle); } /// - /// Includes the in find file output. + /// Gets the creation time UTC. /// - /// Name of the c file. - /// The attributes. - /// if set to true [include files]. - /// if set to true [include directories]. - /// true if XXXX, false otherwise - public static bool IncludeInFindFileOutput(string cFileName, FileAttributes attributes, bool includeFiles, bool includeDirectories) + /// The info. + /// The logger. + /// DateTime. + public static DateTime GetCreationTimeUtc(FileSystemInfo info, ILogger logger) { - if (cFileName.Equals(".", StringComparison.OrdinalIgnoreCase)) - { - return false; - } - if (cFileName.Equals("..", StringComparison.OrdinalIgnoreCase)) - { - return false; - } + // This could throw an error on some file systems that have dates out of range - if (!includeFiles && !attributes.HasFlag(FileAttributes.Directory)) + try { - return false; + return info.CreationTimeUtc; } - - if (!includeDirectories && attributes.HasFlag(FileAttributes.Directory)) + catch (Exception ex) { - return false; + logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName); + return DateTime.MinValue; } - - return true; } /// -- cgit v1.2.3