// Copyright (C) 2025, 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.Collections.Generic; using System.IO; using Duplicati.Library.Interface; using System.Threading.Tasks; using System.Threading; 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); } /// /// Initializes a new instance of the class. /// /// A flag indicating if symlinks should be followed protected SnapshotBase(bool followSymlinks) { FollowSymlinks = followSymlinks; } #region ISnapshotService protected bool FollowSymlinks { get; } /// /// Gets the source entries /// public abstract IEnumerable SourceEntries { get; } /// /// Enuemrates the root source files and folders /// /// The source files and folders public abstract IEnumerable EnumerateFilesystemEntries(); /// /// Gets a filesystem entry for a given path /// /// The path to get the entry for /// A flag indicating if the path is a folder /// The filesystem entry public virtual ISourceProviderEntry GetFilesystemEntry(string path, bool isFolder) { if (isFolder && !DirectoryExists(path)) return null; else if (!isFolder && !FileExists(path)) return null; return new SnapshotSourceFileEntry(this, isFolder ? Util.AppendDirSeparator(path) : path, isFolder, false); } /// /// Enumerates files and folders in the given folder /// /// Source to enumerate /// The files and folders in the given folder public virtual IEnumerable EnumerateFilesystemEntries(ISourceProviderEntry source) { if (source.IsFolder) { foreach (var f in ListFiles(source.Path)) yield return new SnapshotSourceFileEntry(this, f, false, false); foreach (var d in ListFolders(source.Path)) yield return new SnapshotSourceFileEntry(this, Util.AppendDirSeparator(d), true, false); } } /// /// 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); } /// /// Opens a file for reading /// /// The full path to the file in non-snapshot format /// A token that can be used to cancel the operation /// An open filestream that can be read public virtual Task OpenReadAsync(string localPath, CancellationToken cancellationToken) => Task.FromResult(OpenRead(localPath)); /// /// 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 public abstract Dictionary GetMetadata(string localPath, bool isSymlink); /// /// 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)); } #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 } } }