From c67f44d145d19c839dc690da8aa096bb69b1b16a Mon Sep 17 00:00:00 2001 From: "kenneth@hexad.dk" Date: Mon, 6 Aug 2012 20:45:45 +0000 Subject: [PATCH] Merged r1413 and r1414 into trunk. git-svn-id: https://duplicati.googlecode.com/svn/branches/1.3.x@1415 59da171f-624f-0410-aa54-27559c288bec --- Duplicati/Library/Main/Options.cs | 2 + .../Library/Main/Strings/Options.Designer.cs | 73 +++++--- Duplicati/Library/Main/Strings/Options.resx | 9 +- .../Library/Snapshots/DefineDosDevice.cs | 167 ++++++++++++++++++ .../Duplicati.Library.Snapshots.csproj | 1 + .../Library/Snapshots/WindowsSnapshot.cs | 136 ++++++++++++-- 6 files changed, 344 insertions(+), 44 deletions(-) create mode 100644 Duplicati/Library/Snapshots/DefineDosDevice.cs diff --git a/Duplicati/Library/Main/Options.cs b/Duplicati/Library/Main/Options.cs index 6d7dd18ad..44d83c7f2 100644 --- a/Duplicati/Library/Main/Options.cs +++ b/Duplicati/Library/Main/Options.cs @@ -168,6 +168,7 @@ namespace Duplicati.Library.Main "signature-control-files", "snapshot-policy", "vss-exclude-writers", + "vss-use-mapping", "usn-policy", "open-file-policy", "exclude-empty-folders", @@ -367,6 +368,7 @@ namespace Duplicati.Library.Main new CommandLineArgument("snapshot-policy", CommandLineArgument.ArgumentType.Enumeration, Strings.Options.SnapshotpolicyShort, Strings.Options.SnapshotpolicyLong, "off", null, new string[] {"auto", "off", "on", "required"}), new CommandLineArgument("vss-exclude-writers", CommandLineArgument.ArgumentType.String, Strings.Options.VssexcludewritersShort, Strings.Options.VssexcludewritersLong), + new CommandLineArgument("vss-use-mapping", CommandLineArgument.ArgumentType.Boolean, Strings.Options.VssusemappingShort, Strings.Options.VssusemappingLong, "false"), new CommandLineArgument("usn-policy", CommandLineArgument.ArgumentType.Enumeration, Strings.Options.UsnpolicyShort, Strings.Options.UsnpolicyLong, "off", null, new string[] {"auto", "off", "on", "required"}), new CommandLineArgument("open-file-policy", CommandLineArgument.ArgumentType.Enumeration, Strings.Options.OpenfilepolicyShort, Strings.Options.OpenfilepolicyLong, "snapshot", null, new string[] { "ignore", "snapshot", "copy" }), diff --git a/Duplicati/Library/Main/Strings/Options.Designer.cs b/Duplicati/Library/Main/Strings/Options.Designer.cs index 3c75699db..12785cb27 100644 --- a/Duplicati/Library/Main/Strings/Options.Designer.cs +++ b/Duplicati/Library/Main/Strings/Options.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.239 +// Runtime Version:4.0.30319.544 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -474,6 +474,24 @@ namespace Duplicati.Library.Main.Strings { } } + /// + /// Looks up a localized string similar to Use this option to exclude files with certain attributes. Use a comma separated list of attribute names to specify more that one. Possible values are: {0}. + /// + internal static string ExcludefilesattributesLong { + get { + return ResourceManager.GetString("ExcludefilesattributesLong", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Exclude files by attribute. + /// + internal static string ExcludefilesattributesShort { + get { + return ResourceManager.GetString("ExcludefilesattributesShort", resourceCulture); + } + } + /// /// Looks up a localized string similar to Exclude files that match this filter. The filter is a "file-globbing" filter, much like commandline options. You can use "*.txt" to exclude all text files.. /// @@ -1077,6 +1095,24 @@ namespace Duplicati.Library.Main.Strings { } } + /// + /// Looks up a localized string similar to Using this option to handle symlinks different. The "{0}" option will simply record a symlink with its name and destination, and a restore will recreate the symlink as a link. Use the option "{1}" to ignore all symlinks and not store any information about them. Previous versions of Duplicati used the setting "{2}", which will cause symlinked files to be included and restore as normal files.. + /// + internal static string SymlinkpolicyLong { + get { + return ResourceManager.GetString("SymlinkpolicyLong", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Symlink handling. + /// + internal static string SymlinkpolicyShort { + get { + return ResourceManager.GetString("SymlinkpolicyShort", resourceCulture); + } + } + /// /// Looks up a localized string similar to Duplicati will upload files while scanning the disk and producing volumes, which usually makes the backup faster. Use this flag to turn the behavior off, so that Duplicati will wait for each volume to complete.. /// @@ -1186,7 +1222,7 @@ namespace Duplicati.Library.Main.Strings { } /// - /// Looks up a localized string similar to Upload empty backup files . + /// Looks up a localized string similar to Upload empty backup files. /// internal static string UploadUnchangedBackupsShort { get { @@ -1265,41 +1301,22 @@ namespace Duplicati.Library.Main.Strings { return ResourceManager.GetString("VssexcludewritersShort", resourceCulture); } } - + /// - /// Looks up a localized string similar to Using this option to handle symlinks different. The "{0}" option will simply record a symlink with its name and destination, and a restore will recreate the symlink as a link. Use the option "{1}" to ignore all symlinks and not store any information about them. Previous versions of Duplicati used the setting "{2}", which will cause symlinked files to be included and restore as normal files.. + /// Looks up a localized string similar to Activate this option to map VSS snapshots to a drive (similar to SUBST, using Win32 DefineDosDevice). This will create temporary drives that are then used to access the contents of a snapshot. This workaround can speed up file access on Windows XP.. /// - internal static string SymlinkpolicyLong { + internal static string VssusemappingLong { get { - return ResourceManager.GetString("SymlinkpolicyLong", resourceCulture); + return ResourceManager.GetString("VssusemappingLong", resourceCulture); } } /// - /// Looks up a localized string similar to Symlink handling. + /// Looks up a localized string similar to Map snapshots to a drive (Windows only). /// - internal static string SymlinkpolicyShort { + internal static string VssusemappingShort { get { - return ResourceManager.GetString("SymlinkpolicyShort", resourceCulture); - } - } - - - /// - /// Exclude files by attribute. - /// - internal static string ExcludefilesattributesShort { - get { - return ResourceManager.GetString("ExcludefilesattributesShort", resourceCulture); - } - } - - /// - /// Use this option to exclude files with certain attributes. Use a comma separated list of attribute names to specify more that one. Possible values are: {0}. - /// - internal static string ExcludefilesattributesLong { - get { - return ResourceManager.GetString("ExcludefilesattributesLong", resourceCulture); + return ResourceManager.GetString("VssusemappingShort", resourceCulture); } } } diff --git a/Duplicati/Library/Main/Strings/Options.resx b/Duplicati/Library/Main/Strings/Options.resx index 8e815d835..5da99cce5 100644 --- a/Duplicati/Library/Main/Strings/Options.resx +++ b/Duplicati/Library/Main/Strings/Options.resx @@ -518,7 +518,7 @@ A reported maximum storage - + Symlink handling @@ -531,5 +531,10 @@ Use this option to exclude files with certain attributes. Use a comma separated list of attribute names to specify more that one. Possible values are: {0} - + + Activate this option to map VSS snapshots to a drive (similar to SUBST, using Win32 DefineDosDevice). This will create temporary drives that are then used to access the contents of a snapshot. This workaround can speed up file access on Windows XP. + + + Map snapshots to a drive (Windows only) + diff --git a/Duplicati/Library/Snapshots/DefineDosDevice.cs b/Duplicati/Library/Snapshots/DefineDosDevice.cs new file mode 100644 index 000000000..f60c138ce --- /dev/null +++ b/Duplicati/Library/Snapshots/DefineDosDevice.cs @@ -0,0 +1,167 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; + +namespace Duplicati.Library.Snapshots +{ + /// + /// Implements a convenient wrapping for mapping a path to a drive letter + /// + public class DefineDosDevice : IDisposable + { + /// + /// Encapsulation of Win32 calls + /// + private static class Win32API + { + /// + /// Flags that can be used with DefineDosDevice + /// + [Flags] + public enum DDD_Flags : uint + { + /// + /// Uses the targetpath string as is. Otherwise, it is converted from an MS-DOS path to a path. + /// + DDD_RAW_TARGET_PATH = 0x1, + /// + /// Removes the specified definition for the specified device. To determine which definition to remove, the function walks the list of mappings for the device, looking for a match of targetpath against a prefix of each mapping associated with this device. The first mapping that matches is the one removed, and then the function returns. + /// If targetpath is NULL or a pointer to a NULL string, the function will remove the first mapping associated with the device and pop the most recent one pushed. If there is nothing left to pop, the device name will be removed. + /// If this value is not specified, the string pointed to by the targetpath parameter will become the new mapping for this device. + /// + DDD_REMOVE_DEFINITION = 0x2, + /// + /// If this value is specified along with DDD_REMOVE_DEFINITION, the function will use an exact match to determine which mapping to remove. Use this value to ensure that you do not delete something that you did not define. + /// + DDD_EXACT_MATCH_ON_REMOVE = 0x4, + /// + /// Do not broadcast the WM_SETTINGCHANGE message. By default, this message is broadcast to notify the shell and applications of the change. + /// + DDD_NO_BROADCAST_SYSTEM = 0x8 + } + + /// + /// Defines, redefines, or deletes MS-DOS device names. + /// + /// The controllable aspects of the DefineDosDevice function + /// A pointer to an MS-DOS device name string specifying the device the function is defining, redefining, or deleting. The device name string must not have a colon as the last character, unless a drive letter is being defined, redefined, or deleted. For example, drive C would be the string "C:". In no case is a trailing backslash ("\") allowed. + /// A pointer to a path string that will implement this device. The string is an MS-DOS path string unless the DDD_RAW_TARGET_PATH flag is specified, in which case this string is a path string. + /// True on success, false otherwise + [System.Runtime.InteropServices.DllImport("kernel32", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] + public static extern bool DefineDosDevice(DDD_Flags flags, string devicename, string targetpath); + } + + /// + /// The drive the path is mapped to + /// + private string m_drive; + /// + /// The path that is mapped to the drive + /// + private string m_targetPath; + /// + /// A value indicating if the shell should be notified of changes + /// + private bool m_shellBroadcast; + + /// + /// Gets the drive that this mapping represents + /// + public string Drive { get { return m_drive; } } + + /// + /// Gets the path that this mapping represents + /// + public string Targetpath { get { return m_targetPath; } } + + /// + /// Creates a new mapping, using default settings + /// + /// The path to map + public DefineDosDevice(string path) + : this(path, null, false) + { + } + + /// + /// Creates a new mapping + /// + /// The path to map + /// The drive to map to, use null to get a free drive letter + /// True to notify the shell of the change, false otherwise + public DefineDosDevice(string path, string drive, bool notifyShell) + { + if (string.IsNullOrEmpty(drive)) + { + List drives = new List("DEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()); + foreach (DriveInfo di in DriveInfo.GetDrives()) + { + if ((di.RootDirectory.FullName.Length == 2 && di.RootDirectory.FullName[1] == ':') || ((di.RootDirectory.FullName.Length == 3 && di.RootDirectory.FullName.EndsWith(":\\")))) + { + int i = drives.IndexOf(di.RootDirectory.FullName[0]); + if (i >= 0) + drives.RemoveAt(i); + } + } + + if (drives.Count == 0) + throw new IOException("No drive letters available"); + drive = drives[0].ToString() + ':'; + } + + while (drive.EndsWith("\\")) + drive = drive.Substring(0, drive.Length - 1); + + if (!drive.EndsWith(":")) + throw new ArgumentException("drive"); + + Win32API.DDD_Flags flags = 0; + if (!notifyShell) + flags |= Win32API.DDD_Flags.DDD_NO_BROADCAST_SYSTEM; + + if (!Win32API.DefineDosDevice(flags, drive, path)) + throw new System.ComponentModel.Win32Exception(); + + m_drive = drive; + m_targetPath = path; + m_shellBroadcast = notifyShell; + } + + /// + /// Disposes all resources held + /// + public void Dispose() + { + Dispose(true); + } + + /// + /// Disposes all resources + /// + /// True if called from Disposing, false otherwise + protected void Dispose(bool disposing) + { + if (m_drive != null) + { + Win32API.DDD_Flags flags = Win32API.DDD_Flags.DDD_REMOVE_DEFINITION | Win32API.DDD_Flags.DDD_EXACT_MATCH_ON_REMOVE; + if (m_shellBroadcast) + flags |= Win32API.DDD_Flags.DDD_NO_BROADCAST_SYSTEM; + bool success = Win32API.DefineDosDevice(flags, m_drive, m_targetPath); + m_drive = null; + } + + if (disposing) + GC.SuppressFinalize(this); + } + + /// + /// Destroys the object and releases all held resources + /// + ~DefineDosDevice() + { + Dispose(false); + } + + } +} diff --git a/Duplicati/Library/Snapshots/Duplicati.Library.Snapshots.csproj b/Duplicati/Library/Snapshots/Duplicati.Library.Snapshots.csproj index c02f6e1f8..59052406d 100644 --- a/Duplicati/Library/Snapshots/Duplicati.Library.Snapshots.csproj +++ b/Duplicati/Library/Snapshots/Duplicati.Library.Snapshots.csproj @@ -76,6 +76,7 @@ + diff --git a/Duplicati/Library/Snapshots/WindowsSnapshot.cs b/Duplicati/Library/Snapshots/WindowsSnapshot.cs index fa1c60b62..ee6527889 100644 --- a/Duplicati/Library/Snapshots/WindowsSnapshot.cs +++ b/Duplicati/Library/Snapshots/WindowsSnapshot.cs @@ -52,9 +52,22 @@ namespace Duplicati.Library.Snapshots /// private Dictionary m_volumes; /// + /// The mapping of snapshot sources to their snapshot entries , key is the path root, eg C:\. + /// The dictionary is case insensitive + /// + private Dictionary m_volumeMap; + /// + /// A list of mapped drives + /// + private List m_mappedDrives; + /// /// Commonly used string element /// private static string SLASH = Path.DirectorySeparatorChar.ToString(); + /// + /// A cached lookup for windows methods for dealing with long filenames + /// + private static readonly SystemIOWindows _ioWin = new SystemIOWindows(); /// /// Constructs a new backup snapshot, using all the required disks @@ -78,6 +91,9 @@ namespace Duplicati.Library.Snapshots excludedWriters.Add(new Guid(s)); } + //Check if we should map any drives + bool useSubst = Utility.Utility.ParseBoolOption(options, "vss-use-mapping"); + //Prepare the backup m_backup = vss.CreateVssBackupComponents(); m_backup.InitializeForBackup(null); @@ -111,7 +127,7 @@ namespace Duplicati.Library.Snapshots m_volumes = new Dictionary(StringComparer.InvariantCultureIgnoreCase); foreach (string s in m_sourcepaths) { - string drive = Path.GetPathRoot(s); + string drive = Alphaleonis.Win32.Filesystem.Path.GetPathRoot(s); if (!m_volumes.ContainsKey(drive)) { if (!m_backup.IsVolumeSupported(drive)) @@ -131,6 +147,27 @@ namespace Duplicati.Library.Snapshots //Create the shadow volumes using (IVssAsync async = m_backup.DoSnapshotSet()) async.Wait(); + + //Make a little lookup table for faster translation + m_volumeMap = new Dictionary(StringComparer.InvariantCultureIgnoreCase); + foreach(KeyValuePair kvp in m_volumes) + m_volumeMap.Add(kvp.Key, m_backup.GetSnapshotProperties(kvp.Value).SnapshotDeviceObject); + + //If we should map the drives, we do that now and update the volumeMap + if (useSubst) + { + m_mappedDrives = new List(); + foreach (string k in new List(m_volumeMap.Keys)) + { + try + { + DefineDosDevice d; + m_mappedDrives.Add(d = new DefineDosDevice(m_volumeMap[k])); + m_volumeMap[k] = Utility.Utility.AppendDirSeparator(d.Drive); + } + catch { } + } + } } catch { @@ -202,10 +239,28 @@ namespace Duplicati.Library.Snapshots /// A list of non-shadow paths private string[] ListFolders(string folder) { - string root = Utility.Utility.AppendDirSeparator(Path.GetPathRoot(folder)); + string root = Utility.Utility.AppendDirSeparator(Alphaleonis.Win32.Filesystem.Path.GetPathRoot(folder)); string volumePath = Utility.Utility.AppendDirSeparator(GetSnapshotPath(root)); - string[] tmp = Alphaleonis.Win32.Filesystem.Directory.GetDirectories(GetSnapshotPath(folder)); + string[] tmp = null; + string spath = GetSnapshotPath(folder); + + if (SystemIOWindows.IsPathTooLong(spath)) + try { tmp = Alphaleonis.Win32.Filesystem.Directory.GetDirectories(spath); } + catch (PathTooLongException) { } + catch (DirectoryNotFoundException) { } + else + try { tmp = System.IO.Directory.GetDirectories(spath); } + catch (PathTooLongException) { } + + if (tmp == null) + { + spath = SystemIOWindows.PrefixWithUNC(spath); + volumePath = SystemIOWindows.PrefixWithUNC(volumePath); + tmp = Alphaleonis.Win32.Filesystem.Directory.GetDirectories(spath); + } + + for (int i = 0; i < tmp.Length; i++) tmp[i] = root + tmp[i].Substring(volumePath.Length); return tmp; @@ -220,9 +275,27 @@ namespace Duplicati.Library.Snapshots /// A list of non-shadow paths private string[] ListFiles(string folder) { - string root = Utility.Utility.AppendDirSeparator(Path.GetPathRoot(folder)); + string root = Utility.Utility.AppendDirSeparator(Alphaleonis.Win32.Filesystem.Path.GetPathRoot(folder)); string volumePath = Utility.Utility.AppendDirSeparator(GetSnapshotPath(root)); - string[] tmp = Alphaleonis.Win32.Filesystem.Directory.GetFiles(GetSnapshotPath(folder)); + + string[] tmp = null; + string spath = GetSnapshotPath(folder); + + if (SystemIOWindows.IsPathTooLong(spath)) + try { tmp = Alphaleonis.Win32.Filesystem.Directory.GetFiles(spath); } + catch (PathTooLongException) { } + catch (DirectoryNotFoundException) { } + else + try { tmp = System.IO.Directory.GetFiles(spath); } + catch (PathTooLongException) { } + + if (tmp == null) + { + spath = SystemIOWindows.PrefixWithUNC(spath); + volumePath = SystemIOWindows.PrefixWithUNC(volumePath); + tmp = Alphaleonis.Win32.Filesystem.Directory.GetFiles(spath); + } + for (int i = 0; i < tmp.Length; i++) tmp[i] = root + tmp[i].Substring(volumePath.Length); return tmp; @@ -238,15 +311,14 @@ namespace Duplicati.Library.Snapshots if (!Path.IsPathRooted(localPath)) throw new InvalidOperationException(); - string root = Path.GetPathRoot(localPath); + string root = Alphaleonis.Win32.Filesystem.Path.GetPathRoot(localPath); - if (!m_volumes.ContainsKey(root)) + string volumePath; + if (!m_volumeMap.TryGetValue(root, out volumePath)) throw new InvalidOperationException(); localPath = localPath.Replace(root, String.Empty); - string volumePath = m_backup.GetSnapshotProperties(m_volumes[root]).SnapshotDeviceObject; - if (!volumePath.EndsWith(SLASH) && !localPath.StartsWith(SLASH)) localPath = localPath.Insert(0, SLASH); localPath = localPath.Insert(0, volumePath); @@ -294,7 +366,15 @@ namespace Duplicati.Library.Snapshots /// The last write time of the file public DateTime GetLastWriteTime(string file) { - return Alphaleonis.Win32.Filesystem.File.GetLastWriteTime(GetSnapshotPath(file)); + string spath = GetSnapshotPath(file); + if (!SystemIOWindows.IsPathTooLong(spath)) + try + { + return File.GetLastWriteTime(spath); + } + catch (PathTooLongException) { } + + return Alphaleonis.Win32.Filesystem.File.GetLastWriteTime(SystemIOWindows.PrefixWithUNC(spath)); } /// @@ -304,7 +384,17 @@ namespace Duplicati.Library.Snapshots /// An open filestream that can be read public System.IO.Stream OpenRead(string file) { - return Alphaleonis.Win32.Filesystem.File.OpenRead(GetSnapshotPath(file)); + return _ioWin.FileOpenRead(GetSnapshotPath(file)); + } + + /// + /// Returns the size of a file + /// + /// The full path to the file in non-snapshot format + /// The lenth of the file + public long GetFileSize(string file) + { + return _ioWin.FileLength(GetSnapshotPath(file)); } /// @@ -314,7 +404,7 @@ namespace Duplicati.Library.Snapshots /// The file or folder to examine public System.IO.FileAttributes GetAttributes(string file) { - return (System.IO.FileAttributes)Alphaleonis.Win32.Filesystem.File.GetAttributes(GetSnapshotPath(file)); + return _ioWin.GetFileAttributes(GetSnapshotPath(file)); } /// @@ -324,17 +414,35 @@ namespace Duplicati.Library.Snapshots /// The symlink target public string GetSymlinkTarget(string file) { - return Alphaleonis.Win32.Filesystem.File.GetLinkTargetInfo(file).PrintName; + string spath = GetSnapshotPath(file); + try + { + return Alphaleonis.Win32.Filesystem.File.GetLinkTargetInfo(spath).PrintName; + } + catch (PathTooLongException) { } + + return Alphaleonis.Win32.Filesystem.File.GetLinkTargetInfo(SystemIOWindows.PrefixWithUNC(spath)).PrintName; } #endregion #region IDisposable Members - /// /// Cleans up any resources and closes the backup set /// public void Dispose() { + try + { + if (m_mappedDrives != null) + { + foreach (DefineDosDevice d in m_mappedDrives) + d.Dispose(); + m_mappedDrives = null; + m_volumeMap = null; + } + } + catch { } + try { if (m_backup != null)