// Copyright (C) 2024, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using System.Linq; 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 virtual 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 length 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 } } }