// 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.
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Duplicati.Library.Interface;
namespace Duplicati.Library.Snapshots;
///
/// A snapshot-based source file entry
///
/// The snapshot service
/// The path of the file
/// True if the entry is a folder, false otherwise
/// True if the entry is the root entry, false otherwise
public class SnapshotSourceFileEntry(ISnapshotService service, string path, bool isFolder, bool isRoot) : ISourceProviderEntry
{
///
/// The symlink target
///
private string? symlinkTarget;
///
/// The file attributes
///
private FileAttributes? attributes;
///
/// Gets the symlink target or null if the entry is not a symlink
///
public string? SymlinkTarget
{
get
{
if (symlinkTarget != null)
return symlinkTarget;
if (!service.IsSymlink(path, Attributes))
return null;
return symlinkTarget = service.GetSymlinkTarget(path);
}
}
///
/// True if the entry is a symlink, false otherwise
///
public bool IsSymlink => SymlinkTarget != null;
///
/// Gets the entry attributes
///
public FileAttributes Attributes
{
get
{
if (attributes != null)
return attributes.Value;
attributes = service.GetAttributes(path);
return attributes.Value;
}
}
///
/// Gets the minor metadata for the file
///
public Dictionary MinorMetadata => service.GetMetadata(path, IsSymlink);
///
/// Gets a value indicating if the entry is a block device
///
public bool IsBlockDevice => service.IsBlockDevice(path);
///
/// Gets a value indicating if the entry is a character device
///
public bool IsCharacterDevice => false;
///
/// Gets a value indicating if the entry is an alternate stream
///
public bool IsAlternateStream => false;
///
/// Gets the hardlink target ID, or null if the entry is not a hardlink
///
public string? HardlinkTargetId => service.HardlinkTargetID(path);
///
public bool IsFolder => isFolder;
///
public bool IsMetaEntry => false;
///
public bool IsRootEntry => isRoot;
///
public DateTime CreatedUtc => service.GetCreationTimeUtc(path);
///
public DateTime LastModificationUtc => service.GetLastWriteTimeUtc(path);
///
public string Path => path;
///
public long Size => IsFolder ? -1 : service.GetFileSize(path);
///
public Task OpenRead(CancellationToken cancellationToken) => service.OpenReadAsync(path, cancellationToken);
///
public Task OpenMetadataRead(CancellationToken cancellationToken) => Task.FromResult(new MemoryStream(System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(MinorMetadata)));
///
public IAsyncEnumerable Enumerate(CancellationToken cancellationToken) => service.EnumerateFilesystemEntries(this).ToAsyncEnumerable();
///
public Task FileExists(string filename, CancellationToken cancellationToken) => Task.FromResult(service.FileExists(System.IO.Path.Combine(path, filename)));
}