* Rewrite the backend manager. This creates a new more logic backend manager that handles all backend operations. For each top-level operation, there is now a single backend manager instance that is passed to sub-commands. The internals of the backend manager are now rewritten to use async logic instead of threading. The design uses a single task runner that dispatches operations and honors concurrent settings and limits from one place. The logic for each operation has been moved into a separate files/classes to make it easier to understand each operation. This fixes #5804 * Fixed a few issues with not awaiting tasks * Fixed not creating empty databases * Fixed an issue with hashes not being recorded on download * Reworked the download logic to avoid attempting to decrypt the file if it has been modified. * Review fixes * Renamed db collector to better reflect the purpose. * Review fixes
107 lines
4.3 KiB
C#
107 lines
4.3 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Duplicati.Library.Interface;
|
|
using Duplicati.Library.Localization.Short;
|
|
|
|
namespace Duplicati.Library.Main.Backend;
|
|
|
|
#nullable enable
|
|
|
|
partial class BackendManager
|
|
{
|
|
/// <summary>
|
|
/// Represents a pending DELETE operation
|
|
/// </summary>
|
|
private class DeleteOperation : PendingOperation<bool>
|
|
{
|
|
/// <summary>
|
|
/// The log tag for this class
|
|
/// </summary>
|
|
private static readonly string LOGTAG = Logging.Log.LogTagFromType<DeleteOperation>();
|
|
|
|
/// <summary>
|
|
/// The remote filename that is to be deleted
|
|
/// </summary>
|
|
public override string RemoteFilename { get; }
|
|
/// <summary>
|
|
/// The size of the remote file, or -1 if unknown
|
|
/// </summary>
|
|
public override long Size { get; }
|
|
|
|
/// <summary>
|
|
/// The operation type
|
|
/// </summary>
|
|
public override BackendActionType Operation => BackendActionType.Delete;
|
|
|
|
/// <summary>
|
|
/// Creates a new DeleteOperation
|
|
/// </summary>
|
|
/// <param name="remoteName">The remote filename to delete</param>
|
|
/// <param name="size">The size of the remote file, or -1 if unknown</param>
|
|
/// <param name="context">The execution context</param>
|
|
/// <param name="waitForComplete">Whether to wait for the operation to complete</param>
|
|
public DeleteOperation(string remoteName, long size, ExecuteContext context, bool waitForComplete, CancellationToken cancelToken)
|
|
: base(context, waitForComplete, cancelToken)
|
|
{
|
|
RemoteFilename = remoteName;
|
|
Size = size;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override async Task<bool> ExecuteAsync(IBackend backend, CancellationToken cancelToken)
|
|
{
|
|
Context.Statwriter.SendEvent(BackendActionType.Delete, BackendEventType.Started, RemoteFilename, Size);
|
|
|
|
string? result = null;
|
|
try
|
|
{
|
|
await backend.DeleteAsync(RemoteFilename, cancelToken).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Check if the file was not found, and if so, check if it was actually deleted
|
|
var isFileMissingException = ex is Library.Interface.FileMissingException || ex is System.IO.FileNotFoundException;
|
|
var wr = (ex as System.Net.WebException)?.Response as System.Net.HttpWebResponse;
|
|
bool recovered = false;
|
|
|
|
if (isFileMissingException || (wr != null && wr.StatusCode == System.Net.HttpStatusCode.NotFound))
|
|
{
|
|
Logging.Log.WriteInformationMessage(LOGTAG, "DeleteRemoteFileFailed", LC.L($"Delete operation failed for {RemoteFilename} with FileNotFound, listing contents"));
|
|
|
|
try
|
|
{
|
|
recovered = !backend.List().Select(x => x.Name).Contains(RemoteFilename);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
|
|
if (recovered)
|
|
Logging.Log.WriteInformationMessage(LOGTAG, "DeleteRemoteFileSuccess", LC.L($"Listing indicates file {RemoteFilename} was deleted correctly"));
|
|
else
|
|
Logging.Log.WriteWarningMessage(LOGTAG, "DeleteRemoteFileFailed", ex, LC.L($"Listing confirms file {RemoteFilename} was not deleted"));
|
|
}
|
|
|
|
if (!recovered)
|
|
{
|
|
result = ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
// Here, we do not know if the file was actually deleted or not
|
|
// We log that the operation was performed, and the result
|
|
Context.Database.LogRemoteOperation("delete", RemoteFilename, result);
|
|
|
|
// We also log the new state of the file, so it will be attempted to be re-deleted on later listings
|
|
Context.Database.LogRemoteVolumeUpdated(RemoteFilename, RemoteVolumeState.Deleted, -1, null);
|
|
}
|
|
|
|
Context.Statwriter.SendEvent(BackendActionType.Delete, BackendEventType.Completed, RemoteFilename, Size);
|
|
return true;
|
|
}
|
|
}
|
|
} |