using System.Threading;
using System.Threading.Tasks;
using Duplicati.Library.Interface;
namespace Duplicati.Library.Main.Backend;
#nullable enable
partial class BackendManager
{
///
/// Represents a pending CREATEFOLDER operation. The backend is created bound to
/// the operation's (or the
/// base backend URL when null), and is
/// invoked to create the folder at that URL. A
/// is treated as success (the folder already being present is the desired state).
/// Used by sync to ensure a destination sub-folder exists before uploading a file
/// into it (the backend manager points the backend at the sub-folder URL, so the
/// folder gets created at the right location regardless of whether the backend is
/// folder-enabled).
///
///
/// Creates a new CreateFolderOperation.
///
/// The execution context
/// The cancellation token
private class CreateFolderOperation(ExecuteContext context, CancellationToken cancelToken)
: PendingOperation(context, true, cancelToken)
{
///
/// The operation type
///
public override BackendActionType Operation => BackendActionType.CreateFolder;
///
/// Executes the operation
///
/// The backend to create the folder on
/// The cancellation token
/// An awaitable task returning true when the folder exists after the call
public override async Task ExecuteAsync(IBackend backend, CancellationToken cancelToken)
{
Context.Statwriter.SendEvent(BackendActionType.CreateFolder, BackendEventType.Started, null, 0);
try
{
await backend.CreateFolderAsync(cancelToken).ConfigureAwait(false);
}
catch (FolderAreadyExistedException)
{
// The folder already exists, which is the desired outcome of "ensure".
}
Context.Statwriter.SendEvent(BackendActionType.CreateFolder, BackendEventType.Completed, null, 0);
return true;
}
}
}