Files
duplicati/Duplicati/Library/Main/Operation/Restore/VolumeDecryptor.cs
T

99 lines
4.2 KiB
C#
Raw Normal View History

2025-01-07 09:40:39 +01:00
// Copyright (C) 2025, The Duplicati Team
2024-11-22 11:47:07 +01:00
// https://duplicati.com, hello@duplicati.com
2025-02-18 09:17:08 +01: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
2024-11-22 11:47:07 +01:00
// Software is furnished to do so, subject to the following conditions:
2025-02-18 09:17:08 +01:00
//
// The above copyright notice and this permission notice shall be included in
2024-11-22 11:47:07 +01:00
// all copies or substantial portions of the Software.
2025-02-18 09:17:08 +01: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-11-22 11:47:07 +01:00
// DEALINGS IN THE SOFTWARE.
2024-11-08 09:55:17 +01:00
using System;
using System.Diagnostics;
using System.Linq;
2024-11-08 09:55:17 +01:00
using System.Threading.Tasks;
using CoCoL;
2024-12-02 15:09:23 +01:00
using Duplicati.Library.Main.Volumes;
2024-11-08 09:55:17 +01:00
namespace Duplicati.Library.Main.Operation.Restore
{
2024-11-22 15:26:04 +01:00
/// <summary>
/// Process that decrypts the volumes that the `VolumeDownloader` process has downloaded.
/// </summary>
2024-12-02 15:09:23 +01:00
internal class VolumeDecryptor
2024-11-08 09:55:17 +01:00
{
2024-12-02 20:29:40 +01:00
/// <summary>
/// The log tag for this class.
/// </summary>
2024-12-02 15:09:23 +01:00
private static readonly string LOGTAG = Logging.Log.LogTagFromType<VolumeDecryptor>();
2024-12-02 20:29:40 +01:00
/// <summary>
/// Runs the volume decryptor process.
/// </summary>
2025-02-27 14:48:08 +01:00
/// <param name="channels">The named channels for the restore operation.</param>
/// <param name="backend">The backend manager.</param>
2024-12-02 20:29:40 +01:00
/// <param name="options">The restore options.</param>
public static Task Run(Channels channels, IBackendManager backend, Options options)
2024-11-08 09:55:17 +01:00
{
return AutomationExtensions.RunTask(
new
{
Input = channels.DecryptRequest.AsRead(),
Output = channels.VolumeRequestResponse.AsWrite()
2024-11-08 09:55:17 +01:00
},
async self =>
{
2025-02-18 09:17:08 +01:00
Stopwatch sw_read = options.InternalProfiling ? new() : null;
Stopwatch sw_write = options.InternalProfiling ? new() : null;
Stopwatch sw_decrypt = options.InternalProfiling ? new() : null;
try
2024-11-08 09:55:17 +01:00
{
while (true)
{
2024-11-22 15:26:04 +01:00
// Get the block request and volume from the `VolumeDownloader` process.
sw_read?.Start();
var (volume_id, volume_name, volume) = await self.Input.ReadAsync().ConfigureAwait(false);
sw_read?.Stop();
2024-11-22 15:26:04 +01:00
sw_decrypt?.Start();
var tmpfile = backend.DecryptFile(volume, volume_name, options);
var bvr = new BlockVolumeReader(options.CompressionModule, tmpfile, options);
sw_decrypt?.Stop();
sw_write?.Start();
2024-11-22 15:26:04 +01:00
// Pass the decrypted volume to the `VolumeDecompressor` process.
await self.Output.WriteAsync((volume_id, bvr)).ConfigureAwait(false);
sw_write?.Stop();
}
}
catch (RetiredException)
{
2024-12-02 15:12:50 +01:00
Logging.Log.WriteVerboseMessage(LOGTAG, "RetiredProcess", null, "Volume decryptor retired");
if (options.InternalProfiling)
{
Logging.Log.WriteProfilingMessage(LOGTAG, "InternalTimings", $"Read: {sw_read.ElapsedMilliseconds}ms, Decrypt: {sw_decrypt.ElapsedMilliseconds}ms, Write: {sw_write.ElapsedMilliseconds}ms");
}
}
catch (Exception ex)
{
Logging.Log.WriteErrorMessage(LOGTAG, "DecryptionError", ex, "Error during decryption");
throw;
2024-11-08 09:55:17 +01:00
}
});
}
}
2024-11-22 15:26:04 +01:00
2024-11-08 09:55:17 +01:00
}