#region Disclaimer / License
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Duplicati.Library.Common.IO;
namespace Duplicati.Library.Snapshots
{
///
/// This class encapsulates all access to the Windows Volume Shadow copy Services,
/// implementing the disposable patterns to ensure correct release of resources.
///
/// The class presents all files and folders with their regular filenames to the caller,
/// and internally handles the conversion to the shadow path.
///
public sealed class WindowsSnapshot : SnapshotBase
{
///
/// The tag used for logging messages
///
public static readonly string LOGTAG = Logging.Log.LogTagFromType();
///
/// The main reference to the backup controller
///
private readonly VssBackupComponents _vssBackupComponents;
///
/// Constructs a new backup snapshot, using all the required disks
///
/// Sources to determine which volumes to include in snapshot
/// A set of commandline options
public WindowsSnapshot(IEnumerable sources, IDictionary options)
{
// For Windows, ensure we don't store paths with extended device path prefixes (i.e., @"\\?\" or @"\\?\UNC\")
sources = sources.Select(SystemIOWindows.RemoveExtendedDevicePathPrefix);
try
{
_vssBackupComponents = new VssBackupComponents();
// Default to exclude the System State writer
var excludedWriters = new Guid[] { new Guid("{e8132975-6f93-4464-a53e-1050253ae220}") };
if (options.ContainsKey("vss-exclude-writers"))
{
excludedWriters = options["vss-exclude-writers"]
.Split(';')
.Where(x => !string.IsNullOrWhiteSpace(x) && x.Trim().Length > 0)
.Select(x => new Guid(x))
.ToArray();
}
_vssBackupComponents.SetupWriters(null, excludedWriters);
_vssBackupComponents.InitShadowVolumes(sources);
_vssBackupComponents.MapVolumesToSnapShots();
//If we should map the drives, we do that now and update the volumeMap
if (Utility.Utility.ParseBoolOption(options, "vss-use-mapping"))
{
_vssBackupComponents.MapDrives();
}
}
catch (Exception ex1)
{
Logging.Log.WriteVerboseMessage(LOGTAG, "WindowsSnapshotCreation", ex1, "Failed to initialize windows snapshot instance");
//In case we fail in the constructor, we do not want a snapshot to be active
try
{
Dispose();
}
catch(Exception ex2)
{
Logging.Log.WriteVerboseMessage(LOGTAG, "VSSCleanupOnError", ex2, "Failed during VSS error cleanup");
}
throw;
}
}
#region Private functions
///
/// A callback function that takes a non-shadow path to a folder,
/// and returns all folders found in a non-shadow path format.
///
/// The non-shadow path of the folder to list
/// A list of non-shadow paths
protected override string[] ListFolders(string localFolderPath)
{
string[] tmp = null;
var spath = ConvertToSnapshotPath(localFolderPath);
tmp = SystemIO.IO_WIN.GetDirectories(spath);
var root = Util.AppendDirSeparator(SystemIO.IO_WIN.GetPathRoot(localFolderPath));
var volumePath = Util.AppendDirSeparator(ConvertToSnapshotPath(root));
volumePath = SystemIOWindows.AddExtendedDevicePathPrefix(volumePath);
for (var i = 0; i < tmp.Length; i++)
{
tmp[i] = root + SystemIOWindows.AddExtendedDevicePathPrefix(tmp[i]).Substring(volumePath.Length);
}
return tmp;
}
///
/// A callback function that takes a non-shadow path to a folder,
/// and returns all files found in a non-shadow path format.
///
/// The non-shadow path of the folder to list
/// A list of non-shadow paths
protected override string[] ListFiles(string localFolderPath)
{
string[] files = null;
var spath = ConvertToSnapshotPath(localFolderPath);
files = SystemIO.IO_WIN.GetFiles(spath);
// convert back to non-shadow, i.e., non-vss version
var root = Util.AppendDirSeparator(SystemIO.IO_WIN.GetPathRoot(localFolderPath));
var volumePath = Util.AppendDirSeparator(ConvertToSnapshotPath(root));
volumePath = SystemIOWindows.AddExtendedDevicePathPrefix(volumePath);
for (var i = 0; i < files.Length; i++)
{
files[i] = root + SystemIOWindows.AddExtendedDevicePathPrefix(files[i]).Substring(volumePath.Length);
}
return files;
}
#endregion
#region ISnapshotService Members
///
/// Enumerates all files and folders in the snapshot, restricted to sources
///
/// Sources to enumerate
/// The callback to invoke with each found path
/// The callback used to report errors
public override IEnumerable EnumerateFilesAndFolders(IEnumerable sources, Utility.Utility.EnumerationFilterDelegate callback, Utility.Utility.ReportAccessError errorCallback)
{
// For Windows, ensure we don't store paths with extended device path prefixes (i.e., @"\\?\" or @"\\?\UNC\")
return base.EnumerateFilesAndFolders(sources.Select(SystemIOWindows.RemoveExtendedDevicePathPrefix), callback, errorCallback);
}
///
/// Gets the last write time of a given file in UTC
///
/// The full path to the file in non-shadow format
/// The last write time of the file
public override DateTime GetLastWriteTimeUtc(string localPath)
{
var spath = ConvertToSnapshotPath(localPath);
return SystemIO.IO_WIN.GetLastWriteTimeUtc(SystemIOWindows.AddExtendedDevicePathPrefix(spath));
}
///
/// Gets the creation of a given file in UTC
///
/// The full path to the file in non-shadow format
/// The last write time of the file
public override DateTime GetCreationTimeUtc(string localPath)
{
var spath = ConvertToSnapshotPath(localPath);
return SystemIO.IO_WIN.GetCreationTimeUtc(SystemIOWindows.AddExtendedDevicePathPrefix(spath));
}
///
/// Opens a file for reading
///
/// The full path to the file in non-shadow format
/// An open filestream that can be read
public override Stream OpenRead(string localPath)
{
return SystemIO.IO_WIN.FileOpenRead(ConvertToSnapshotPath(localPath));
}
///
/// Returns the size of a file
///
/// The full path to the file in non-snapshot format
/// The length of the file
public override long GetFileSize(string localPath)
{
return SystemIO.IO_WIN.FileLength(ConvertToSnapshotPath(localPath));
}
///
/// Gets the attributes for the given file or folder
///
/// The file attributes
/// The file or folder to examine
public override FileAttributes GetAttributes(string localPath)
{
return SystemIO.IO_WIN.GetFileAttributes(ConvertToSnapshotPath(localPath));
}
///
/// Returns the symlink target if the entry is a symlink, and null otherwise
///
/// The file or folder to examine
/// The symlink target
public override string GetSymlinkTarget(string localPath)
{
var spath = ConvertToSnapshotPath(localPath);
return SystemIO.IO_WIN.GetSymlinkTarget(spath);
}
///
/// Gets the metadata for the given file or folder
///
/// The metadata for the given file or folder
/// The file or folder to examine
/// A flag indicating if the target is a symlink
/// A flag indicating if a symlink should be followed
public override Dictionary GetMetadata(string localPath, bool isSymlink, bool followSymlink)
{
return SystemIO.IO_WIN.GetMetadata(ConvertToSnapshotPath(localPath), isSymlink, followSymlink);
}
///
public override bool IsBlockDevice(string localPath)
{
return false;
}
///
public override string HardlinkTargetID(string localPath)
{
return null;
}
///
public override string ConvertToLocalPath(string snapshotPath)
{
if (!Path.IsPathRooted(snapshotPath))
throw new InvalidOperationException();
foreach (var kvp in _vssBackupComponents.SnapshotDeviceAndVolumes)
{
if (snapshotPath.StartsWith(kvp.Key, Utility.Utility.ClientFilenameStringComparison))
return SystemIO.IO_WIN.PathCombine(kvp.Value, snapshotPath.Substring(kvp.Key.Length));
}
throw new InvalidOperationException();
}
///
public override string ConvertToSnapshotPath(string localPath)
{
// For Windows, ensure we don't store paths with extended device path prefixes (i.e., @"\\?\" or @"\\?\UNC\")
localPath = SystemIOWindows.RemoveExtendedDevicePathPrefix(localPath);
if (!Path.IsPathRooted(localPath))
throw new InvalidOperationException();
var root = SystemIO.IO_WIN.GetPathRoot(localPath);
var volumePath = _vssBackupComponents.GetVolumeFromCache(root);
// Note that using a simple Path.Combine() for the following code
// can result in invalid snapshot paths; e.g., if localPath is
// @"C:\", mappedPath would not have the required trailing
// directory separator.
var subPath = localPath.Substring(root.Length);
if (!subPath.StartsWith(Util.DirectorySeparatorString, StringComparison.Ordinal))
{
volumePath = Util.AppendDirSeparator(volumePath, Util.DirectorySeparatorString);
}
var mappedPath = volumePath + subPath;
return mappedPath;
}
///
public override bool FileExists(string localFilePath)
{
return SystemIO.IO_WIN.FileExists(ConvertToSnapshotPath(localFilePath));
}
///
public override bool DirectoryExists(string localFolderPath)
{
return SystemIO.IO_WIN.DirectoryExists(ConvertToSnapshotPath(localFolderPath));
}
///
public override bool IsSnapshot => true;
#endregion
///
protected override void Dispose(bool disposing)
{
if (disposing)
{
_vssBackupComponents.Dispose();
}
base.Dispose(disposing);
}
}
}