2025-01-16 16:01:54 +01:00
|
|
|
// Copyright (C) 2025, The Duplicati Team
|
|
|
|
|
// https://duplicati.com, hello@duplicati.com
|
2025-05-21 15:20:52 +02:00
|
|
|
//
|
|
|
|
|
// 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
|
2025-01-16 16:01:54 +01:00
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
2025-05-21 15:20:52 +02:00
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
2025-01-16 16:01:54 +01:00
|
|
|
// all copies or substantial portions of the Software.
|
2025-05-21 15:20:52 +02:00
|
|
|
//
|
|
|
|
|
// 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
|
2024-02-28 15:45:30 +01:00
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
2016-02-09 09:17:31 +01:00
|
|
|
using System;
|
2016-02-22 21:27:12 +01:00
|
|
|
using System.Linq;
|
2025-04-23 12:11:16 +02:00
|
|
|
using System.Threading;
|
2016-02-09 09:17:31 +01:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Duplicati.Library.Main.Database;
|
2025-05-14 20:39:55 +02:00
|
|
|
using Duplicati.Library.Utility;
|
2016-02-09 09:17:31 +01:00
|
|
|
|
|
|
|
|
namespace Duplicati.Library.Main.Operation.Common
|
|
|
|
|
{
|
2016-02-25 19:15:51 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Asynchronous interface that ensures all requests
|
|
|
|
|
/// to the database are performed in a sequential manner
|
|
|
|
|
/// </summary>
|
2016-02-18 16:46:53 +01:00
|
|
|
internal class DatabaseCommon : SingleRunner, IDisposable
|
2016-02-09 09:17:31 +01:00
|
|
|
{
|
2018-04-11 23:02:47 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// The tag used for log messages
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly string LOGTAG = Logging.Log.LogTagFromType<DatabaseCommon>();
|
|
|
|
|
|
2019-10-19 10:56:21 -07:00
|
|
|
protected readonly LocalDatabase m_db;
|
|
|
|
|
protected readonly Options m_options;
|
2016-02-09 09:17:31 +01:00
|
|
|
|
2016-02-27 02:15:42 +01:00
|
|
|
public DatabaseCommon(LocalDatabase db, Options options)
|
2016-02-09 09:17:31 +01:00
|
|
|
: base()
|
|
|
|
|
{
|
|
|
|
|
m_db = db;
|
2016-02-27 02:15:42 +01:00
|
|
|
m_options = options;
|
2025-01-16 16:01:54 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
public Task<long> RegisterRemoteVolumeAsync(string name, RemoteVolumeType type, RemoteVolumeState state, CancellationToken cancellationToken)
|
2016-02-09 09:17:31 +01:00
|
|
|
{
|
2025-06-12 08:34:29 +02:00
|
|
|
return RunOnMain(async () =>
|
|
|
|
|
await m_db
|
2025-06-19 11:04:09 +02:00
|
|
|
.RegisterRemoteVolume(name, type, state, cancellationToken)
|
2025-06-12 08:34:29 +02:00
|
|
|
.ConfigureAwait(false)
|
|
|
|
|
);
|
2016-02-09 09:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
public Task UpdateRemoteVolumeAsync(string name, RemoteVolumeState state, long size, string hash, bool suppressCleanup, TimeSpan deleteGraceTime, bool? setArchived, CancellationToken cancellationToken)
|
2016-02-09 09:17:31 +01:00
|
|
|
{
|
2025-06-12 08:34:29 +02:00
|
|
|
return RunOnMain(async () =>
|
|
|
|
|
await m_db
|
2025-06-19 11:04:09 +02:00
|
|
|
.UpdateRemoteVolume(name, state, size, hash, suppressCleanup, deleteGraceTime, setArchived, cancellationToken)
|
2025-06-12 08:34:29 +02:00
|
|
|
.ConfigureAwait(false)
|
|
|
|
|
);
|
2016-02-09 09:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
public async Task FlushBackendMessagesAndCommitAsync(IBackendManager backendManager, CancellationToken cancellationToken)
|
2025-04-23 12:11:16 +02:00
|
|
|
{
|
2025-06-19 11:04:09 +02:00
|
|
|
await FlushPendingBackendMessagesAsync(backendManager, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
await CommitTransactionAsync("FlushBackendMessagesAndCommitAsync", true, cancellationToken).ConfigureAwait(false);
|
2025-04-23 12:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
private Task FlushPendingBackendMessagesAsync(IBackendManager backendManager, CancellationToken cancellationToken)
|
|
|
|
|
=> RunOnMain(async () => await backendManager.FlushPendingMessagesAsync(m_db, cancellationToken).ConfigureAwait(false));
|
2025-04-23 12:11:16 +02:00
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
public Task CommitTransactionAsync(string message, bool restart, CancellationToken cancellationToken)
|
2016-02-09 09:17:31 +01:00
|
|
|
{
|
2025-05-21 15:21:18 +02:00
|
|
|
return RunOnMain(async () =>
|
2016-02-09 09:17:31 +01:00
|
|
|
{
|
2016-02-27 02:15:42 +01:00
|
|
|
if (m_options.Dryrun)
|
|
|
|
|
{
|
2025-06-12 08:34:29 +02:00
|
|
|
await m_db.Transaction
|
2025-06-19 11:04:09 +02:00
|
|
|
.RollBackAsync(cancellationToken)
|
2025-06-12 08:34:29 +02:00
|
|
|
.ConfigureAwait(false);
|
2016-02-27 02:15:42 +01:00
|
|
|
}
|
2025-05-21 15:21:18 +02:00
|
|
|
else
|
2025-01-16 16:01:54 +01:00
|
|
|
{
|
|
|
|
|
using (new Logging.Timer(LOGTAG, "CommitTransactionAsync", message))
|
2025-06-12 08:34:29 +02:00
|
|
|
await m_db.Transaction
|
2025-06-19 11:04:09 +02:00
|
|
|
.CommitAsync(message, restart, cancellationToken)
|
2025-06-12 08:34:29 +02:00
|
|
|
.ConfigureAwait(false);
|
2025-01-16 16:01:54 +01:00
|
|
|
}
|
2016-02-09 09:17:31 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
public Task RollbackTransactionAsync(CancellationToken cancellationToken)
|
2016-02-22 22:40:27 +01:00
|
|
|
{
|
2025-06-12 08:34:29 +02:00
|
|
|
return RunOnMain(async () =>
|
|
|
|
|
await m_db.Transaction
|
2025-06-19 11:04:09 +02:00
|
|
|
.RollBackAsync(cancellationToken)
|
2025-06-12 08:34:29 +02:00
|
|
|
.ConfigureAwait(false)
|
|
|
|
|
);
|
2016-02-22 22:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
public Task RenameRemoteFileAsync(string oldname, string newname, CancellationToken cancellationToken)
|
2016-02-18 16:46:53 +01:00
|
|
|
{
|
2025-06-12 08:34:29 +02:00
|
|
|
return RunOnMain(async () =>
|
|
|
|
|
await m_db
|
2025-06-19 11:04:09 +02:00
|
|
|
.RenameRemoteFile(oldname, newname, cancellationToken)
|
2025-06-12 08:34:29 +02:00
|
|
|
.ConfigureAwait(false)
|
|
|
|
|
);
|
2016-02-18 16:46:53 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
public Task LogRemoteOperationAsync(string operation, string path, string data, CancellationToken cancellationToken)
|
2016-02-18 16:46:53 +01:00
|
|
|
{
|
2025-06-12 08:34:29 +02:00
|
|
|
return RunOnMain(async () =>
|
|
|
|
|
await m_db
|
2025-06-19 11:04:09 +02:00
|
|
|
.LogRemoteOperation(operation, path, data, cancellationToken)
|
2025-06-12 08:34:29 +02:00
|
|
|
.ConfigureAwait(false)
|
|
|
|
|
);
|
2016-02-18 16:46:53 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
public Task<LocalDatabase.IBlock[]> GetBlocksAsync(long volumeid, CancellationToken cancellationToken)
|
2016-02-22 21:27:12 +01:00
|
|
|
{
|
2018-06-04 11:56:54 +02:00
|
|
|
// TODO: Figure out how to return the enumerable, while keeping the lock
|
|
|
|
|
// and not creating the entire result in memory
|
2025-06-12 08:34:29 +02:00
|
|
|
return RunOnMain(async () =>
|
|
|
|
|
await m_db
|
2025-06-19 11:04:09 +02:00
|
|
|
.GetBlocks(volumeid, cancellationToken)
|
|
|
|
|
.ToArrayAsync(cancellationToken: cancellationToken)
|
2025-06-12 08:34:29 +02:00
|
|
|
.ConfigureAwait(false)
|
|
|
|
|
);
|
2016-02-22 21:27:12 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
public Task<RemoteVolumeEntry> GetVolumeInfoAsync(string remotename, CancellationToken cancellationToken)
|
2016-02-22 21:27:12 +01:00
|
|
|
{
|
2025-06-12 08:34:29 +02:00
|
|
|
return RunOnMain(async () =>
|
|
|
|
|
await m_db
|
2025-06-19 11:04:09 +02:00
|
|
|
.GetRemoteVolume(remotename, cancellationToken)
|
2025-06-12 08:34:29 +02:00
|
|
|
.ConfigureAwait(false)
|
|
|
|
|
);
|
2016-02-22 21:27:12 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
public Task<(string Hash, byte[] Buffer, int Size)[]> GetBlocklistsAsync(long volumeid, int blocksize, int hashsize, CancellationToken cancellationToken)
|
2016-02-22 21:27:12 +01:00
|
|
|
{
|
2018-06-04 11:56:54 +02:00
|
|
|
// TODO: Figure out how to return the enumerable, while keeping the lock
|
|
|
|
|
// and not creating the entire result in memory
|
2025-06-12 08:34:29 +02:00
|
|
|
return RunOnMain(async () =>
|
|
|
|
|
await m_db
|
2025-06-19 11:04:09 +02:00
|
|
|
.GetBlocklists(volumeid, blocksize, hashsize, cancellationToken)
|
|
|
|
|
.ToArrayAsync(cancellationToken: cancellationToken)
|
2025-06-12 08:34:29 +02:00
|
|
|
.ConfigureAwait(false)
|
|
|
|
|
);
|
2016-02-22 21:27:12 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
public Task<long> GetRemoteVolumeIDAsync(string remotename, CancellationToken cancellationToken)
|
2016-02-22 21:27:12 +01:00
|
|
|
{
|
2025-06-12 08:34:29 +02:00
|
|
|
return RunOnMain(async () =>
|
|
|
|
|
await m_db
|
2025-06-19 11:04:09 +02:00
|
|
|
.GetRemoteVolumeID(remotename, cancellationToken)
|
2025-06-12 08:34:29 +02:00
|
|
|
.ConfigureAwait(false)
|
|
|
|
|
);
|
2016-02-22 21:27:12 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:04:09 +02:00
|
|
|
public Task AddIndexBlockLinkAsync(long indexVolumeID, long blockVolumeID, CancellationToken cancellationToken)
|
2016-02-22 22:40:27 +01:00
|
|
|
{
|
2025-06-12 08:34:29 +02:00
|
|
|
return RunOnMain(async () =>
|
|
|
|
|
await m_db
|
2025-06-19 11:04:09 +02:00
|
|
|
.AddIndexBlockLink(indexVolumeID, blockVolumeID, cancellationToken)
|
2025-06-12 08:34:29 +02:00
|
|
|
.ConfigureAwait(false)
|
|
|
|
|
);
|
2016-02-22 22:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-02-22 21:27:12 +01:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
2025-05-14 20:39:55 +02:00
|
|
|
if (m_workerSource.IsCancellationRequested)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-05-26 09:02:08 +02:00
|
|
|
base.Dispose(isDisposing);
|
2016-02-22 21:27:12 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 09:17:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|