#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 // using System.Linq; #endregion using System; using System.Collections.Generic; using System.IO; using Duplicati.Library.Common.IO; namespace Duplicati.Library.Snapshots { public abstract class SnapshotBase : ISnapshotService { /// /// Releases unmanaged resources and performs other cleanup operations before the /// is reclaimed by garbage collection. /// /// We must implement a finalizer to guarantee that our native handle is cleaned up ~SnapshotBase() { // Our finalizer should call our Dispose(bool) method with false Dispose(false); } #region ISnapshotService /// /// Enumerates all files and folders in the shadow copy /// /// Source paths to enumerate /// The callback to invoke with each found path /// The callback used to report errors public IEnumerable EnumerateFilesAndFolders(IEnumerable sources, Utility.Utility.EnumerationFilterDelegate callback, Utility.Utility.ReportAccessError errorCallback) { return sources.SelectMany(s => Utility.Utility.EnumerateFileSystemEntries(s, callback, ListFolders, ListFiles, GetAttributes, errorCallback)); } /// /// Gets the last write time of a given file in UTC /// /// The full path to the file in non-snapshot format /// The last write time of the file public virtual DateTime GetLastWriteTimeUtc(string localPath) { return File.GetLastWriteTimeUtc(localPath); } /// /// Gets the last write time of a given file in UTC /// /// The full path to the file in non-snapshot format /// The last write time of the file public virtual DateTime GetCreationTimeUtc(string localPath) { return File.GetCreationTimeUtc(localPath); } /// /// Opens a file for reading /// /// The full path to the file in non-snapshot format /// An open filestream that can be read public virtual Stream OpenRead(string localPath) { return File.Open(localPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); } /// /// Returns the size of a file /// /// The full path to the file in non-snapshot format /// The lenth of the file public virtual long GetFileSize(string localPath) { return new FileInfo(localPath).Length; } /// /// Returns the symlink target if the entry is a symlink, and null otherwise /// /// The file or folder to examine /// The symlink target public abstract string GetSymlinkTarget(string localPath); /// /// Gets the attributes for the given file or folder /// /// The file attributes /// The file or folder to examine public virtual FileAttributes GetAttributes(string localPath) { return File.GetAttributes(ConvertToSnapshotPath(localPath)); } /// /// 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 abstract Dictionary GetMetadata(string localPath, bool isSymlink, bool followSymlink); /// /// Gets a value indicating if the path points to a block device /// /// true if this instance is a block device; otherwise, false. /// The file or folder to examine public virtual bool IsBlockDevice(string localPath) { return false; } /// /// Gets a unique hardlink target ID /// /// The hardlink ID /// The file or folder to examine public virtual string HardlinkTargetID(string localPath) { return null; } /// public abstract string ConvertToLocalPath(string snapshotPath); /// public abstract string ConvertToSnapshotPath(string localPath); /// public virtual bool FileExists(string localFilePath) { return File.Exists(ConvertToSnapshotPath(localFilePath)); } /// public virtual bool DirectoryExists(string localFolderPath) { return Directory.Exists(ConvertToSnapshotPath(localFolderPath)); } /// public virtual bool PathExists(string localFileOrFolderPath) { return FileExists(localFileOrFolderPath) || DirectoryExists(localFileOrFolderPath); } /// public virtual bool IsSnapshot => false; #endregion /// /// Lists all folders in the given folder /// /// All folders found in the folder /// The folder to examinate protected virtual string[] ListFolders(string localFolderPath) { return Directory.GetDirectories(localFolderPath); } /// /// Lists all files in the given folder /// /// All folders found in the folder /// The folder to examinate protected virtual string[] ListFiles(string localFolderPath) { return Directory.GetFiles(localFolderPath); } #region IDisposable interface /// public void Dispose() { // We start by calling Dispose(bool) with true Dispose(true); // Now suppress finalization for this object, since we've already handled our resource cleanup tasks GC.SuppressFinalize(this); } #endregion /// /// Releases unmanaged and - optionally - managed resources /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected virtual void Dispose(bool disposing) { // nothing to dispose of at this level } } }