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

116 lines
5.0 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-07 10:04:52 +01:00
using System;
2024-12-02 20:32:38 +01:00
using System.Diagnostics;
using System.Linq;
using System.Threading;
2024-11-07 10:04:52 +01:00
using System.Threading.Tasks;
using CoCoL;
using Duplicati.Library.Main.Database;
2024-12-02 15:09:23 +01:00
using Duplicati.Library.Utility;
2024-11-07 10:04:52 +01:00
namespace Duplicati.Library.Main.Operation.Restore
{
2024-11-22 15:26:04 +01:00
/// <summary>
2024-12-02 20:32:38 +01:00
/// Process that starts the download of the requested volumes.
2024-11-22 15:26:04 +01:00
/// </summary>
internal class VolumeDownloader
2024-11-07 10:04:52 +01:00
{
2024-11-22 15:26:04 +01:00
/// <summary>
/// The log tag for this class.
/// </summary>
private static readonly string LOGTAG = Logging.Log.LogTagFromType<VolumeDownloader>();
2024-12-02 20:32:38 +01:00
/// <summary>
/// Runs the volume downloader process.
/// </summary>
/// <param name="channels">The named channels for the restore operation.</param>
/// <param name="db">The local restore database, used to find volume information given the volume ID.</param>
/// <param name="backend">The backend to use for downloading the volumes.</param>
2024-12-02 20:32:38 +01:00
/// <param name="options">The restore options.</param>
/// <param name="results">The restore results.</param>
public static Task Run(Channels channels, LocalRestoreDatabase db, IBackendManager backend, Options options, RestoreResults results)
2024-11-07 10:04:52 +01:00
{
return AutomationExtensions.RunTask(
new
{
Input = channels.DownloadRequest.AsRead(),
Output = channels.DecryptRequest.AsWrite()
2024-11-07 10:04:52 +01:00
},
async self =>
{
2025-01-28 08:54:50 +01:00
Stopwatch sw_read = options.InternalProfiling ? new() : null;
Stopwatch sw_write = options.InternalProfiling ? new() : null;
Stopwatch sw_wait = options.InternalProfiling ? new() : null;
try
2024-11-07 10:04:52 +01:00
{
while (true)
{
2024-11-22 15:26:04 +01:00
// Get the block request from the `BlockManager` process.
2024-12-02 20:32:38 +01:00
sw_read?.Start();
var volume_id = await self.Input.ReadAsync().ConfigureAwait(false);
sw_read?.Stop();
2024-11-07 10:04:52 +01:00
2025-01-28 08:54:50 +01:00
// Trigger the download.
2024-12-02 20:32:38 +01:00
sw_wait?.Start();
TempFile f;
var (volume_name, size, hash) = db.GetVolumeInfo(volume_id).First();
2024-12-02 15:09:23 +01:00
try
{
f = await backend.GetDirectAsync(volume_name, hash, size, CancellationToken.None).ConfigureAwait(false);
}
2024-12-02 15:09:23 +01:00
catch (Exception)
{
2024-12-02 15:09:23 +01:00
lock (results)
results.BrokenRemoteFiles.Add(volume_name);
2025-01-28 08:54:50 +01:00
2024-12-02 15:09:23 +01:00
throw;
}
2024-12-02 15:09:23 +01:00
sw_wait?.Stop();
2024-12-02 15:12:50 +01:00
// Pass the download handle (which may or may not have downloaded already) to the `VolumeDecryptor` process.
2024-12-02 20:32:38 +01:00
sw_write?.Start();
await self.Output.WriteAsync((volume_id, volume_name, f)).ConfigureAwait(false);
sw_write?.Stop();
}
}
catch (RetiredException)
{
Logging.Log.WriteVerboseMessage(LOGTAG, "RetiredProcess", null, "Volume downloader retired");
if (options.InternalProfiling)
{
2024-12-02 15:09:23 +01:00
Logging.Log.WriteProfilingMessage(LOGTAG, "InternalTimings", $"Read: {sw_read.ElapsedMilliseconds}ms, Write: {sw_write.ElapsedMilliseconds}ms, Wait: {sw_wait.ElapsedMilliseconds}ms");
}
}
catch (Exception ex)
{
Logging.Log.WriteErrorMessage(LOGTAG, "DownloadError", ex, "Error during download");
throw;
2024-11-07 10:04:52 +01:00
}
});
}
}
2024-11-22 15:26:04 +01:00
2024-11-07 10:04:52 +01:00
}