From be7918e5f68f67ed32a50c2d86ee9cae79cf2b93 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 30 Oct 2013 10:40:14 -0400 Subject: fixes #567 - Deprecate native shortcut code --- MediaBrowser.Controller/IO/FileData.cs | 9 +- MediaBrowser.Controller/IO/FileSystem.cs | 294 +--------------------- MediaBrowser.Controller/IO/IFileSystem.cs | 45 ++++ MediaBrowser.Controller/IO/NativeMethods.cs | 377 ---------------------------- 4 files changed, 51 insertions(+), 674 deletions(-) create mode 100644 MediaBrowser.Controller/IO/IFileSystem.cs delete mode 100644 MediaBrowser.Controller/IO/NativeMethods.cs (limited to 'MediaBrowser.Controller/IO') diff --git a/MediaBrowser.Controller/IO/FileData.cs b/MediaBrowser.Controller/IO/FileData.cs index b1fc28e7b..726467fa6 100644 --- a/MediaBrowser.Controller/IO/FileData.cs +++ b/MediaBrowser.Controller/IO/FileData.cs @@ -15,6 +15,7 @@ namespace MediaBrowser.Controller.IO /// Gets the filtered file system entries. /// /// The path. + /// The file system. /// The logger. /// The args. /// The search pattern. @@ -22,7 +23,7 @@ namespace MediaBrowser.Controller.IO /// if set to true [resolve shortcuts]. /// Dictionary{System.StringFileSystemInfo}. /// path - public static Dictionary GetFilteredFileSystemEntries(string path, ILogger logger, ItemResolveArgs args, string searchPattern = "*", int flattenFolderDepth = 0, bool resolveShortcuts = true) + public static Dictionary GetFilteredFileSystemEntries(string path, IFileSystem fileSystem, ILogger logger, ItemResolveArgs args, string searchPattern = "*", int flattenFolderDepth = 0, bool resolveShortcuts = true) { if (string.IsNullOrEmpty(path)) { @@ -56,9 +57,9 @@ namespace MediaBrowser.Controller.IO var fullName = entry.FullName; - if (resolveShortcuts && FileSystem.IsShortcut(fullName)) + if (resolveShortcuts && fileSystem.IsShortcut(fullName)) { - var newPath = FileSystem.ResolveShortcut(fullName); + var newPath = fileSystem.ResolveShortcut(fullName); if (string.IsNullOrWhiteSpace(newPath)) { @@ -77,7 +78,7 @@ namespace MediaBrowser.Controller.IO } else if (flattenFolderDepth > 0 && isDirectory) { - foreach (var child in GetFilteredFileSystemEntries(fullName, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts)) + foreach (var child in GetFilteredFileSystemEntries(fullName, fileSystem, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts)) { dict[child.Key] = child.Value; } diff --git a/MediaBrowser.Controller/IO/FileSystem.cs b/MediaBrowser.Controller/IO/FileSystem.cs index 1c49545be..f31fc53de 100644 --- a/MediaBrowser.Controller/IO/FileSystem.cs +++ b/MediaBrowser.Controller/IO/FileSystem.cs @@ -1,10 +1,6 @@ -using System.Collections.Generic; -using System.Linq; -using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Logging; using System; -using System.Collections.Specialized; using System.IO; -using System.Text; namespace MediaBrowser.Controller.IO { @@ -13,38 +9,6 @@ namespace MediaBrowser.Controller.IO /// public static class FileSystem { - /// - /// Gets the file system info. - /// - /// The path. - /// FileSystemInfo. - public static FileSystemInfo GetFileSystemInfo(string path) - { - // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists - if (Path.HasExtension(path)) - { - var fileInfo = new FileInfo(path); - - if (fileInfo.Exists) - { - return fileInfo; - } - - return new DirectoryInfo(path); - } - else - { - var fileInfo = new DirectoryInfo(path); - - if (fileInfo.Exists) - { - return fileInfo; - } - - return new FileInfo(path); - } - } - /// /// Gets the creation time UTC. /// @@ -87,116 +51,6 @@ namespace MediaBrowser.Controller.IO } } - /// - /// The space char - /// - private const char SpaceChar = ' '; - /// - /// The invalid file name chars - /// - private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars(); - - /// - /// Takes a filename and removes invalid characters - /// - /// The filename. - /// System.String. - /// filename - public static string GetValidFilename(string filename) - { - if (string.IsNullOrEmpty(filename)) - { - throw new ArgumentNullException("filename"); - } - - var builder = new StringBuilder(filename); - - foreach (var c in InvalidFileNameChars) - { - builder = builder.Replace(c, SpaceChar); - } - - return builder.ToString(); - } - - /// - /// Resolves the shortcut. - /// - /// The filename. - /// System.String. - /// filename - public static string ResolveShortcut(string filename) - { - if (string.IsNullOrEmpty(filename)) - { - throw new ArgumentNullException("filename"); - } - - if (string.Equals(Path.GetExtension(filename), ".mblink", StringComparison.OrdinalIgnoreCase)) - { - return File.ReadAllText(filename); - } - - //return new WindowsShortcut(filename).ResolvedPath; - - var link = new ShellLink(); - ((IPersistFile)link).Load(filename, NativeMethods.STGM_READ); - // TODO: if I can get hold of the hwnd call resolve first. This handles moved and renamed files. - // ((IShellLinkW)link).Resolve(hwnd, 0) - var sb = new StringBuilder(NativeMethods.MAX_PATH); - WIN32_FIND_DATA data; - ((IShellLinkW)link).GetPath(sb, sb.Capacity, out data, 0); - return sb.ToString(); - } - - /// - /// Creates a shortcut file pointing to a specified path - /// - /// The shortcut path. - /// The target. - /// shortcutPath - public static void CreateShortcut(string shortcutPath, string target) - { - if (string.IsNullOrEmpty(shortcutPath)) - { - throw new ArgumentNullException("shortcutPath"); - } - - if (string.IsNullOrEmpty(target)) - { - throw new ArgumentNullException("target"); - } - - File.WriteAllText(shortcutPath, target); - - //var link = new ShellLink(); - - //((IShellLinkW)link).SetPath(target); - - //((IPersistFile)link).Save(shortcutPath, true); - } - - private static readonly Dictionary ShortcutExtensionsDictionary = new[] { ".mblink", ".lnk" } - .ToDictionary(i => i, StringComparer.OrdinalIgnoreCase); - - /// - /// Determines whether the specified filename is shortcut. - /// - /// The filename. - /// true if the specified filename is shortcut; otherwise, false. - /// filename - public static bool IsShortcut(string filename) - { - if (string.IsNullOrEmpty(filename)) - { - throw new ArgumentNullException("filename"); - } - - var extension = Path.GetExtension(filename); - - return !string.IsNullOrEmpty(extension) && ShortcutExtensionsDictionary.ContainsKey(extension); - } - /// /// Copies all. /// @@ -234,151 +88,5 @@ namespace MediaBrowser.Controller.IO CopyAll(dir, Path.Combine(target, Path.GetFileName(dir))); } } - - /// - /// Parses the ini file. - /// - /// The path. - /// NameValueCollection. - public static NameValueCollection ParseIniFile(string path) - { - var values = new NameValueCollection(); - - foreach (var line in File.ReadAllLines(path)) - { - var data = line.Split('='); - - if (data.Length < 2) continue; - - var key = data[0]; - - var value = data.Length == 2 ? data[1] : string.Join(string.Empty, data, 1, data.Length - 1); - - values[key] = value; - } - - return values; - } - } - - /// - /// Adapted from http://stackoverflow.com/questions/309495/windows-shortcut-lnk-parser-in-java - /// - internal class WindowsShortcut - { - public bool IsDirectory { get; private set; } - public bool IsLocal { get; private set; } - public string ResolvedPath { get; private set; } - - public WindowsShortcut(string file) - { - ParseLink(File.ReadAllBytes(file), Encoding.UTF8); - } - - private static bool isMagicPresent(byte[] link) - { - - const int magic = 0x0000004C; - const int magic_offset = 0x00; - - return link.Length >= 32 && bytesToDword(link, magic_offset) == magic; - } - - /** - * Gobbles up link data by parsing it and storing info in member fields - * @param link all the bytes from the .lnk file - */ - private void ParseLink(byte[] link, Encoding encoding) - { - if (!isMagicPresent(link)) - throw new IOException("Invalid shortcut; magic is missing", 0); - - // get the flags byte - byte flags = link[0x14]; - - // get the file attributes byte - const int file_atts_offset = 0x18; - byte file_atts = link[file_atts_offset]; - byte is_dir_mask = (byte)0x10; - if ((file_atts & is_dir_mask) > 0) - { - IsDirectory = true; - } - else - { - IsDirectory = false; - } - - // if the shell settings are present, skip them - const int shell_offset = 0x4c; - const byte has_shell_mask = (byte)0x01; - int shell_len = 0; - if ((flags & has_shell_mask) > 0) - { - // the plus 2 accounts for the length marker itself - shell_len = bytesToWord(link, shell_offset) + 2; - } - - // get to the file settings - int file_start = 0x4c + shell_len; - - const int file_location_info_flag_offset_offset = 0x08; - int file_location_info_flag = link[file_start + file_location_info_flag_offset_offset]; - IsLocal = (file_location_info_flag & 2) == 0; - // get the local volume and local system values - //final int localVolumeTable_offset_offset = 0x0C; - const int basename_offset_offset = 0x10; - const int networkVolumeTable_offset_offset = 0x14; - const int finalname_offset_offset = 0x18; - int finalname_offset = link[file_start + finalname_offset_offset] + file_start; - String finalname = getNullDelimitedString(link, finalname_offset, encoding); - if (IsLocal) - { - int basename_offset = link[file_start + basename_offset_offset] + file_start; - String basename = getNullDelimitedString(link, basename_offset, encoding); - ResolvedPath = basename + finalname; - } - else - { - int networkVolumeTable_offset = link[file_start + networkVolumeTable_offset_offset] + file_start; - int shareName_offset_offset = 0x08; - int shareName_offset = link[networkVolumeTable_offset + shareName_offset_offset] - + networkVolumeTable_offset; - String shareName = getNullDelimitedString(link, shareName_offset, encoding); - ResolvedPath = shareName + "\\" + finalname; - } - } - - private static string getNullDelimitedString(byte[] bytes, int off, Encoding encoding) - { - int len = 0; - - // count bytes until the null character (0) - while (true) - { - if (bytes[off + len] == 0) - { - break; - } - len++; - } - - return encoding.GetString(bytes, off, len); - } - - /* - * convert two bytes into a short note, this is little endian because it's - * for an Intel only OS. - */ - private static int bytesToWord(byte[] bytes, int off) - { - return ((bytes[off + 1] & 0xff) << 8) | (bytes[off] & 0xff); - } - - private static int bytesToDword(byte[] bytes, int off) - { - return (bytesToWord(bytes, off + 2) << 16) | bytesToWord(bytes, off); - } - } } diff --git a/MediaBrowser.Controller/IO/IFileSystem.cs b/MediaBrowser.Controller/IO/IFileSystem.cs new file mode 100644 index 000000000..2654c3235 --- /dev/null +++ b/MediaBrowser.Controller/IO/IFileSystem.cs @@ -0,0 +1,45 @@ +using System.IO; + +namespace MediaBrowser.Controller.IO +{ + /// + /// Interface IFileSystem + /// + public interface IFileSystem + { + /// + /// Determines whether the specified filename is shortcut. + /// + /// The filename. + /// true if the specified filename is shortcut; otherwise, false. + bool IsShortcut(string filename); + + /// + /// Resolves the shortcut. + /// + /// The filename. + /// System.String. + string ResolveShortcut(string filename); + + /// + /// Creates the shortcut. + /// + /// The shortcut path. + /// The target. + void CreateShortcut(string shortcutPath, string target); + + /// + /// Gets the file system info. + /// + /// The path. + /// FileSystemInfo. + FileSystemInfo GetFileSystemInfo(string path); + + /// + /// Gets the valid filename. + /// + /// The filename. + /// System.String. + string GetValidFilename(string filename); + } +} diff --git a/MediaBrowser.Controller/IO/NativeMethods.cs b/MediaBrowser.Controller/IO/NativeMethods.cs deleted file mode 100644 index 97c7dfe87..000000000 --- a/MediaBrowser.Controller/IO/NativeMethods.cs +++ /dev/null @@ -1,377 +0,0 @@ -using System; -using System.IO; -using System.Runtime.InteropServices; -using System.Security; -using System.Text; - -namespace MediaBrowser.Controller.IO -{ - /// - /// Class NativeMethods - /// - [SuppressUnmanagedCodeSecurity] - public static class NativeMethods - { - /// - /// The MA x_ PATH - /// - public const int MAX_PATH = 260; - /// - /// The MA x_ ALTERNATE - /// - public const int MAX_ALTERNATE = 14; - /// - /// The INVALI d_ HANDL e_ VALUE - /// - public static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); - /// - /// The STG m_ READ - /// - public const uint STGM_READ = 0; - } - - /// - /// Struct FILETIME - /// - [StructLayout(LayoutKind.Sequential)] - public struct FILETIME - { - /// - /// The dw low date time - /// - public uint dwLowDateTime; - /// - /// The dw high date time - /// - public uint dwHighDateTime; - } - - - /// - /// Struct WIN32_FIND_DATA - /// - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - public struct WIN32_FIND_DATA - { - /// - /// The dw file attributes - /// - public FileAttributes dwFileAttributes; - /// - /// The ft creation time - /// - public FILETIME ftCreationTime; - /// - /// The ft last access time - /// - public FILETIME ftLastAccessTime; - /// - /// The ft last write time - /// - public FILETIME ftLastWriteTime; - /// - /// The n file size high - /// - public int nFileSizeHigh; - /// - /// The n file size low - /// - public int nFileSizeLow; - /// - /// The dw reserved0 - /// - public int dwReserved0; - /// - /// The dw reserved1 - /// - public int dwReserved1; - - /// - /// The c file name - /// - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_PATH)] - public string cFileName; - - /// - /// This will always be null when FINDEX_INFO_LEVELS = basic - /// - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_ALTERNATE)] - public string cAlternate; - - /// - /// Gets or sets the path. - /// - /// The path. - public string Path { get; set; } - - /// - /// Returns a that represents this instance. - /// - /// A that represents this instance. - public override string ToString() - { - return Path ?? string.Empty; - } - } - - /// - /// Enum SLGP_FLAGS - /// - [Flags] - public enum SLGP_FLAGS - { - /// - /// Retrieves the standard short (8.3 format) file name - /// - SLGP_SHORTPATH = 0x1, - /// - /// Retrieves the Universal Naming Convention (UNC) path name of the file - /// - SLGP_UNCPRIORITY = 0x2, - /// - /// Retrieves the raw path name. A raw path is something that might not exist and may include environment variables that need to be expanded - /// - SLGP_RAWPATH = 0x4 - } - /// - /// Enum SLR_FLAGS - /// - [Flags] - public enum SLR_FLAGS - { - /// - /// Do not display a dialog box if the link cannot be resolved. When SLR_NO_UI is set, - /// the high-order word of fFlags can be set to a time-out value that specifies the - /// maximum amount of time to be spent resolving the link. The function returns if the - /// link cannot be resolved within the time-out duration. If the high-order word is set - /// to zero, the time-out duration will be set to the default value of 3,000 milliseconds - /// (3 seconds). To specify a value, set the high word of fFlags to the desired time-out - /// duration, in milliseconds. - /// - SLR_NO_UI = 0x1, - /// - /// Obsolete and no longer used - /// - SLR_ANY_MATCH = 0x2, - /// - /// If the link object has changed, update its path and list of identifiers. - /// If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine - /// whether or not the link object has changed. - /// - SLR_UPDATE = 0x4, - /// - /// Do not update the link information - /// - SLR_NOUPDATE = 0x8, - /// - /// Do not execute the search heuristics - /// - SLR_NOSEARCH = 0x10, - /// - /// Do not use distributed link tracking - /// - SLR_NOTRACK = 0x20, - /// - /// Disable distributed link tracking. By default, distributed link tracking tracks - /// removable media across multiple devices based on the volume name. It also uses the - /// Universal Naming Convention (UNC) path to track remote file systems whose drive letter - /// has changed. Setting SLR_NOLINKINFO disables both types of tracking. - /// - SLR_NOLINKINFO = 0x40, - /// - /// Call the Microsoft Windows Installer - /// - SLR_INVOKE_MSI = 0x80 - } - - - /// - /// The IShellLink interface allows Shell links to be created, modified, and resolved - /// - [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")] - public interface IShellLinkW - { - /// - /// Retrieves the path and file name of a Shell link object - /// - /// The PSZ file. - /// The CCH max path. - /// The PFD. - /// The f flags. - void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATA pfd, SLGP_FLAGS fFlags); - /// - /// Retrieves the list of item identifiers for a Shell link object - /// - /// The ppidl. - void GetIDList(out IntPtr ppidl); - /// - /// Sets the pointer to an item identifier list (PIDL) for a Shell link object. - /// - /// The pidl. - void SetIDList(IntPtr pidl); - /// - /// Retrieves the description string for a Shell link object - /// - /// Name of the PSZ. - /// Name of the CCH max. - void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName); - /// - /// Sets the description for a Shell link object. The description can be any application-defined string - /// - /// Name of the PSZ. - void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName); - /// - /// Retrieves the name of the working directory for a Shell link object - /// - /// The PSZ dir. - /// The CCH max path. - void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath); - /// - /// Sets the name of the working directory for a Shell link object - /// - /// The PSZ dir. - void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir); - /// - /// Retrieves the command-line arguments associated with a Shell link object - /// - /// The PSZ args. - /// The CCH max path. - void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath); - /// - /// Sets the command-line arguments for a Shell link object - /// - /// The PSZ args. - void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs); - /// - /// Retrieves the hot key for a Shell link object - /// - /// The pw hotkey. - void GetHotkey(out short pwHotkey); - /// - /// Sets a hot key for a Shell link object - /// - /// The w hotkey. - void SetHotkey(short wHotkey); - /// - /// Retrieves the show command for a Shell link object - /// - /// The pi show CMD. - void GetShowCmd(out int piShowCmd); - /// - /// Sets the show command for a Shell link object. The show command sets the initial show state of the window. - /// - /// The i show CMD. - void SetShowCmd(int iShowCmd); - /// - /// Retrieves the location (path and index) of the icon for a Shell link object - /// - /// The PSZ icon path. - /// The CCH icon path. - /// The pi icon. - void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, - int cchIconPath, out int piIcon); - /// - /// Sets the location (path and index) of the icon for a Shell link object - /// - /// The PSZ icon path. - /// The i icon. - void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon); - /// - /// Sets the relative path to the Shell link object - /// - /// The PSZ path rel. - /// The dw reserved. - void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved); - /// - /// Attempts to find the target of a Shell link, even if it has been moved or renamed - /// - /// The HWND. - /// The f flags. - void Resolve(IntPtr hwnd, SLR_FLAGS fFlags); - /// - /// Sets the path and file name of a Shell link object - /// - /// The PSZ file. - void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile); - - } - - /// - /// Interface IPersist - /// - [ComImport, Guid("0000010c-0000-0000-c000-000000000046"), - InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - public interface IPersist - { - /// - /// Gets the class ID. - /// - /// The p class ID. - [PreserveSig] - void GetClassID(out Guid pClassID); - } - - - /// - /// Interface IPersistFile - /// - [ComImport, Guid("0000010b-0000-0000-C000-000000000046"), - InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - public interface IPersistFile : IPersist - { - /// - /// Gets the class ID. - /// - /// The p class ID. - new void GetClassID(out Guid pClassID); - /// - /// Determines whether this instance is dirty. - /// - [PreserveSig] - int IsDirty(); - - /// - /// Loads the specified PSZ file name. - /// - /// Name of the PSZ file. - /// The dw mode. - [PreserveSig] - void Load([In, MarshalAs(UnmanagedType.LPWStr)] - string pszFileName, uint dwMode); - - /// - /// Saves the specified PSZ file name. - /// - /// Name of the PSZ file. - /// if set to true [remember]. - [PreserveSig] - void Save([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName, - [In, MarshalAs(UnmanagedType.Bool)] bool remember); - - /// - /// Saves the completed. - /// - /// Name of the PSZ file. - [PreserveSig] - void SaveCompleted([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName); - - /// - /// Gets the cur file. - /// - /// Name of the PPSZ file. - [PreserveSig] - void GetCurFile([In, MarshalAs(UnmanagedType.LPWStr)] string ppszFileName); - } - - // CLSID_ShellLink from ShlGuid.h - /// - /// Class ShellLink - /// - [ - ComImport, - Guid("00021401-0000-0000-C000-000000000046") - ] - public class ShellLink - { - } -} -- cgit v1.2.3