Files
duplicati/Duplicati/Library/Main/Operation/RestoreControlFilesHandler.cs
T

111 lines
5.2 KiB
C#
Raw Normal View History

2025-01-28 08:54:50 +01:00
// Copyright (C) 2025, The Duplicati Team
// 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
2024-12-18 08:27:59 +01:00
// DEALINGS IN THE SOFTWARE.
2024-02-28 15:45:30 +01:00
using System;
using System.Collections.Generic;
2025-01-28 08:54:50 +01:00
using System.Threading;
2024-12-18 08:27:59 +01:00
using Duplicati.Library.Utility;
namespace Duplicati.Library.Main.Operation
{
internal class RestoreControlFilesHandler
{
private readonly Options m_options;
private readonly RestoreControlFilesResults m_result;
2025-01-28 08:54:50 +01:00
public RestoreControlFilesHandler(Options options, RestoreControlFilesResults result)
{
m_options = options;
m_result = result;
}
2025-01-28 08:54:50 +01:00
public void Run(IEnumerable<string> filterstrings, IBackendManager backendManager, Library.Utility.IFilter compositefilter)
{
2025-01-28 08:54:50 +01:00
var cancellationToken = CancellationToken.None;
2013-05-30 23:00:09 +02:00
if (string.IsNullOrEmpty(m_options.Restorepath))
throw new Exception("Cannot restore control files without --restore-path");
if (!System.IO.Directory.Exists(m_options.Restorepath))
System.IO.Directory.CreateDirectory(m_options.Restorepath);
2024-12-18 08:27:59 +01:00
2013-05-08 21:29:59 +02:00
using (var tmpdb = new Library.Utility.TempFile())
using (var db = new Database.LocalDatabase(System.IO.File.Exists(m_options.Dbpath) ? m_options.Dbpath : (string)tmpdb, "RestoreControlFiles", true))
{
m_result.SetDatabase(db);
2024-12-18 08:27:59 +01:00
var filter = Library.Utility.JoinedFilterExpression.Join(new Library.Utility.FilterExpression(filterstrings), compositefilter);
2024-12-18 08:27:59 +01:00
try
{
2025-01-28 08:54:50 +01:00
var filteredList = ListFilesHandler.ParseAndFilterFilesets(backendManager.ListAsync(cancellationToken).Await(), m_options);
2013-05-30 23:00:09 +02:00
if (filteredList.Count == 0)
throw new Exception("No filesets found on remote target");
2024-12-18 08:27:59 +01:00
Exception lastEx = new Exception("No suitable files found on remote target");
2024-12-18 08:27:59 +01:00
foreach (var fileversion in filteredList)
try
{
2024-12-18 08:27:59 +01:00
if (!m_result.TaskControl.ProgressRendevouz().Await())
{
2025-01-28 08:54:50 +01:00
backendManager.WaitForEmptyAsync(db, null, cancellationToken).Await();
return;
2024-12-18 08:27:59 +01:00
}
2013-05-30 23:00:09 +02:00
var file = fileversion.Value.File;
var entry = db.GetRemoteVolume(file.Name);
2013-05-30 23:00:09 +02:00
var res = new List<string>();
2025-01-28 08:54:50 +01:00
using (var tmpfile = backendManager.GetAsync(file.Name, entry.Hash, entry.Size < 0 ? file.Size : entry.Size, cancellationToken).Await())
2024-12-18 08:27:59 +01:00
using (var tmp = new Volumes.FilesetVolumeReader(RestoreHandler.GetCompressionModule(file.Name), tmpfile, m_options))
foreach (var cf in tmp.ControlFiles)
if (Library.Utility.FilterExpression.Matches(filter, cf.Key))
2013-05-30 23:00:09 +02:00
{
var targetpath = System.IO.Path.Combine(m_options.Restorepath, cf.Key);
using (var ts = System.IO.File.Create(targetpath))
Library.Utility.Utility.CopyStream(cf.Value, ts);
2013-05-30 23:00:09 +02:00
res.Add(targetpath);
}
2024-12-18 08:27:59 +01:00
2013-05-30 23:00:09 +02:00
m_result.SetResult(res);
2024-12-18 08:27:59 +01:00
lastEx = null;
break;
}
2024-12-18 08:27:59 +01:00
catch (Exception ex)
{
lastEx = ex;
if (ex is System.Threading.ThreadAbortException)
throw;
}
2024-12-18 08:27:59 +01:00
if (lastEx != null)
throw lastEx;
}
finally
{
2025-01-28 08:54:50 +01:00
backendManager.WaitForEmptyAsync(db, null, cancellationToken).Await();
}
db.WriteResults();
}
}
}
}