From c80c8c1cfd594f2597e46b09d44360ade9f4fec2 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Thu, 23 Aug 2012 01:45:26 -0400 Subject: Switched all i/o to win32 methods and added protobuf serialization for ffprobe caching --- MediaBrowser.Controller/IO/FileData.cs | 84 ++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 25 deletions(-) (limited to 'MediaBrowser.Controller/IO/FileData.cs') diff --git a/MediaBrowser.Controller/IO/FileData.cs b/MediaBrowser.Controller/IO/FileData.cs index 98332c794..eca166ead 100644 --- a/MediaBrowser.Controller/IO/FileData.cs +++ b/MediaBrowser.Controller/IO/FileData.cs @@ -2,6 +2,11 @@ using System.IO; using System.Runtime.InteropServices; +using System.Runtime.ConstrainedExecution; +using Microsoft.Win32.SafeHandles; +using System.Collections.Generic; +using System.Linq; + namespace MediaBrowser.Controller.IO { public static class FileData @@ -16,16 +21,67 @@ namespace MediaBrowser.Controller.IO if (handle == IntPtr.Zero) throw new IOException("FindFirstFile failed"); FindClose(handle); + + data.Path = fileName; return data; } - [DllImport("kernel32")] + public static IEnumerable GetFileSystemEntries(string path, string searchPattern) + { + 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 (IsValid(lpFindFileData.cFileName)) + { + yield return lpFindFileData; + } + + while (FindNextFile(handle, out lpFindFileData) != IntPtr.Zero) + { + if (IsValid(lpFindFileData.cFileName)) + { + lpFindFileData.Path = Path.Combine(path, lpFindFileData.cFileName); + yield return lpFindFileData; + } + } + + FindClose(handle); + } + + private static bool IsValid(string cFileName) + { + if (cFileName.Equals(".", StringComparison.OrdinalIgnoreCase)) + { + return false; + } + if (cFileName.Equals("..", StringComparison.OrdinalIgnoreCase)) + { + 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); - - } [StructLayout(LayoutKind.Sequential)] @@ -107,30 +163,8 @@ namespace MediaBrowser.Controller.IO highBits = highBits << 32; return DateTime.FromFileTime(highBits + (long)filetime.dwLowDateTime); } - } - public struct LazyFileInfo - { public string Path { get; set; } - - private WIN32_FIND_DATA? _FileInfo { get; set; } - - public WIN32_FIND_DATA FileInfo - { - get - { - if (_FileInfo == null) - { - _FileInfo = FileData.GetFileData(Path); - } - - return _FileInfo.Value; - } - set - { - _FileInfo = value; - } - } } } -- cgit v1.2.3