Files
duplicati/Duplicati/Library/Main/Database/LocalLockDatabase.cs
T

129 lines
5.9 KiB
C#
Raw Normal View History

// Copyright (C) 2026, The Duplicati Team
2025-12-14 17:17:48 +01:00
// 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.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Duplicati.Library.Main.Database
{
/// <summary>
/// A local database specialized for object lock operations.
/// </summary>
internal class LocalLockDatabase : LocalListDatabase
{
/// <summary>
/// Creates a new instance of the <see cref="LocalLockDatabase"/> class.
/// </summary>
/// <param name="path">The path to the database file.</param>
/// <param name="dbnew">An optional existing database instance to use. Used to mimic constructor chaining.</param>
/// <param name="token">A cancellation token to cancel the operation.</param>
/// <returns>A task that when awaited contains a new instance of <see cref="LocalLockDatabase"/>.</returns>
public static async Task<LocalLockDatabase> CreateAsync(string path, LocalLockDatabase? dbnew, CancellationToken token)
{
dbnew ??= new LocalLockDatabase();
dbnew = (LocalLockDatabase)
await CreateLocalDatabaseAsync(path, "Lock", false, dbnew, token)
.ConfigureAwait(false);
dbnew.ShouldCloseConnection = true;
return dbnew;
}
2026-06-03 12:45:57 +02:00
/// <summary>
/// Creates a new instance of the <see cref="LocalLockDatabase"/> class.
/// </summary>
/// <param name="database">The database to create the new instance on.</param>
/// <param name="token">A cancellation token to cancel the operation.</param>
/// <returns>The <see cref="LocalLockDatabase"/> instance</returns>
public static async Task<LocalLockDatabase> CreateAsync(LocalDatabase database, CancellationToken token)
{
var db = (LocalLockDatabase)await LocalDatabase.CreateLocalDatabaseAsync(
database,
new LocalLockDatabase(),
token
).ConfigureAwait(false);
db.ShouldCloseConnection = false;
return db;
}
2025-12-14 17:17:48 +01:00
/// <summary>
/// Updates the lock expiration time for a remote volume.
/// </summary>
/// <param name="name">The name of the remote volume.</param>
/// <param name="lockExpirationTime">The lock expiration time, or null to clear the lock.</param>
/// <param name="token">Cancellation token.</param>
/// <returns>A task that completes when the update is done.</returns>
2026-05-13 15:04:13 +02:00
public async Task UpdateRemoteVolumeLockExpirationAsync(string name, DateTime lockExpirationTime, CancellationToken token)
2025-12-14 17:17:48 +01:00
{
await using var cmd = m_connection.CreateCommand();
cmd.SetTransaction(m_rtr);
cmd.SetCommandAndParameters(@"
UPDATE ""Remotevolume""
SET ""LockExpirationTime"" = @LockExpirationTime
WHERE ""Name"" = @Name
");
cmd.SetParameterValue("@Name", name);
cmd.SetParameterValue("@LockExpirationTime", Library.Utility.Utility.NormalizeDateTimeToEpochSeconds(lockExpirationTime));
2026-05-06 10:52:58 +02:00
var c = await cmd.ExecuteNonQueryAsync(true, token).ConfigureAwait(false);
2025-12-14 17:17:48 +01:00
if (c != 1)
throw new Exception($"Unexpected number of remote volumes updated: {c}, expected 1");
}
/// <summary>
/// Gets all remote volumes with their lock expiration times.
/// </summary>
/// <param name="onlyWithoutLockInformation">If true, only returns volumes without lock expiration</param>
2025-12-14 17:17:48 +01:00
/// <param name="token">Cancellation token.</param>
/// <returns>An asynchronous sequence of tuples containing the volume name and lock expiration time.</returns>
2026-05-13 15:04:13 +02:00
public async IAsyncEnumerable<(string Name, DateTime? LockExpirationTime)> GetRemoteVolumesWithLockExpirationAsync(bool onlyWithoutLockInformation, [EnumeratorCancellation] CancellationToken token)
2025-12-14 17:17:48 +01:00
{
await using var cmd = m_connection.CreateCommand();
cmd.SetTransaction(m_rtr);
var whereClause = onlyWithoutLockInformation
? @"WHERE ""LockExpirationTime"" = 0"
2025-12-14 17:17:48 +01:00
: "";
cmd.SetCommandAndParameters($@"
SELECT ""Name"", ""LockExpirationTime""
FROM ""Remotevolume""
{whereClause}
");
await using var rd = await cmd.ExecuteReaderAsync(token).ConfigureAwait(false);
while (await rd.ReadAsync(token).ConfigureAwait(false))
{
var name = rd.ConvertValueToString(0) ?? string.Empty;
var lockExpiration = ParseFromEpochSeconds(rd.ConvertValueToInt64(1));
2025-12-14 17:17:48 +01:00
yield return (name, lockExpiration);
}
}
}
}