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-02-28 15:45:30 +01:00
// DEALINGS IN THE SOFTWARE.
2025-03-08 15:25:55 +01:00
using Duplicati.Library.Main.Database ;
2020-02-23 13:21:17 -06:00
using Duplicati.Library.Main.Operation.Common ;
2016-02-22 22:40:27 +01:00
using Duplicati.Library.Main.Volumes ;
2020-02-23 13:21:17 -06:00
using System ;
2016-02-22 22:40:27 +01:00
using System.Linq ;
2020-02-23 13:21:17 -06:00
using System.Threading.Tasks ;
2016-02-22 22:40:27 +01:00
namespace Duplicati.Library.Main.Operation.Backup
{
2016-02-25 19:15:51 +01:00
/// <summary>
/// This class encapsulates generation of synthetic file list
/// </summary>
2016-02-22 22:40:27 +01:00
internal static class UploadSyntheticFilelist
{
2018-04-11 23:02:47 +02:00
/// <summary>
/// The tag used for log messages
/// </summary>
private static readonly string LOGTAG = Logging . Log . LogTagFromType ( typeof ( UploadSyntheticFilelist ));
2025-03-08 15:25:55 +01:00
/// <summary>
/// This method is used to generate and upload a synthetic file list, if needed
/// </summary>
/// <param name="database">The database to use</param>
/// <param name="options">The options to use</param>
/// <param name="result">The backup results</param>
/// <param name="taskreader">The task reader to use</param>
/// <param name="backendManager">The backend manager to use</param>
/// <param name="lastTempFilelist">The last temporary file list volume</param>
/// <returns></returns>
public static async Task Run ( BackupDatabase database , Options options , BasicResults result , ITaskReader taskreader , IBackendManager backendManager , RemoteVolumeEntry lastTempFilelist )
2016-02-22 22:40:27 +01:00
{
2025-01-28 08:54:50 +01:00
// Check if we should upload a synthetic filelist
2025-03-08 15:25:55 +01:00
if ( options . DisableSyntheticFilelist || string . IsNullOrWhiteSpace ( lastTempFilelist . Name ) || lastTempFilelist . ID <= 0 )
2025-01-28 08:54:50 +01:00
return ;
2016-02-24 23:48:42 +01:00
2025-01-28 08:54:50 +01:00
// Files is missing or repaired
2025-03-08 15:25:55 +01:00
if ( lastTempFilelist . State != RemoteVolumeState . Uploading && lastTempFilelist . State != RemoteVolumeState . Temporary )
2025-01-28 08:54:50 +01:00
{
2025-03-08 15:25:55 +01:00
Logging . Log . WriteInformationMessage ( LOGTAG , "SkippingSyntheticListUpload" , "Skipping synthetic upload because temporary fileset appers to be complete: ({0}, {1}, {2})" , lastTempFilelist . ID , lastTempFilelist . Name , lastTempFilelist . State );
2025-01-28 08:54:50 +01:00
return ;
}
2016-02-22 22:40:27 +01:00
2025-01-28 08:54:50 +01:00
// Ready to build and upload the synthetic list
await database . CommitTransactionAsync ( "PreSyntheticFilelist" );
var incompleteFilesets = ( await database . GetIncompleteFilesetsAsync ()). OrderBy ( x => x . Value ). ToList ();
2017-05-30 00:35:31 +02:00
2025-01-28 08:54:50 +01:00
if (! incompleteFilesets . Any ())
return ;
2017-05-30 00:35:31 +02:00
2025-01-28 08:54:50 +01:00
if (! await taskreader . ProgressRendevouz (). ConfigureAwait ( false ))
return ;
2016-02-24 23:48:42 +01:00
2025-01-28 08:54:50 +01:00
result . OperationProgressUpdater . UpdatePhase ( OperationPhase . Backup_PreviousBackupFinalize );
Logging . Log . WriteInformationMessage ( LOGTAG , "PreviousBackupFilelistUpload" , "Uploading filelist from previous interrupted backup" );
2016-02-27 02:15:42 +01:00
2025-01-28 08:54:50 +01:00
var incompleteSet = incompleteFilesets . Last ();
2025-03-06 14:53:46 +01:00
var badIds = incompleteFilesets . Select ( n => n . Key );
2020-02-23 13:21:17 -06:00
2025-03-06 14:53:46 +01:00
var prevs = ( await database . GetFilesetTimesAsync ())
. Where ( n => n . Key < incompleteSet . Key && ! badIds . Contains ( n . Key ))
. OrderBy ( n => n . Key )
. Select ( n => n . Key )
. ToArray ();
2016-02-24 23:48:42 +01:00
2025-01-28 08:54:50 +01:00
var prevId = prevs . Length == 0 ? - 1 : prevs . Last ();
2016-02-24 23:48:42 +01:00
2025-01-28 08:54:50 +01:00
FilesetVolumeWriter fsw = null ;
try
{
2025-03-10 14:37:30 +01:00
var fileTime = await FilesetVolumeWriter . ProbeUnusedFilenameName ( database , options , incompleteSet . Value ). ConfigureAwait ( false );
2025-01-28 08:54:50 +01:00
fsw = new FilesetVolumeWriter ( options , fileTime );
fsw . VolumeID = await database . RegisterRemoteVolumeAsync ( fsw . RemoteFilename , RemoteVolumeType . Files , RemoteVolumeState . Temporary );
2020-02-23 13:21:17 -06:00
2025-01-28 08:54:50 +01:00
if (! string . IsNullOrEmpty ( options . ControlFiles ))
foreach ( var p in options . ControlFiles . Split ( new char [] { System . IO . Path . PathSeparator }, StringSplitOptions . RemoveEmptyEntries ))
fsw . AddControlFile ( p , options . GetCompressionHintFromFilename ( p ));
2020-02-23 13:21:17 -06:00
2025-01-28 08:54:50 +01:00
// We declare this to be a partial backup since the synthetic filelist is only created
// when a backup is interrupted.
fsw . CreateFilesetFile ( false );
var newFilesetID = await database . CreateFilesetAsync ( fsw . VolumeID , fileTime );
await database . LinkFilesetToVolumeAsync ( newFilesetID , fsw . VolumeID );
await database . AppendFilesFromPreviousSetAsync ( null , newFilesetID , prevId , fileTime );
2020-02-23 13:21:17 -06:00
2025-01-28 08:54:50 +01:00
await database . WriteFilesetAsync ( fsw , newFilesetID );
fsw . Close ();
2020-02-23 13:21:17 -06:00
2025-01-28 08:54:50 +01:00
if (! await taskreader . ProgressRendevouz (). ConfigureAwait ( false ))
return ;
2020-02-23 13:21:17 -06:00
2025-01-28 08:54:50 +01:00
await database . UpdateRemoteVolumeAsync ( fsw . RemoteFilename , RemoteVolumeState . Uploading , - 1 , null );
2025-03-08 15:25:55 +01:00
// If the previous filelist was not uploaded, we register it for deletion, as we have created a new synthetic one
// Because it is registered as "Deleting", it will be removed from remote storage by the cleanup process if it exists
if (! string . IsNullOrWhiteSpace ( lastTempFilelist . Name ) && ( lastTempFilelist . State == RemoteVolumeState . Uploading || lastTempFilelist . State == RemoteVolumeState . Temporary ))
await database . UpdateRemoteVolumeAsync ( lastTempFilelist . Name , RemoteVolumeState . Deleting , - 1 , null );
2025-01-28 08:54:50 +01:00
await database . CommitTransactionAsync ( "CommitUpdateFilelistVolume" );
2020-02-23 13:21:17 -06:00
2025-01-28 08:54:50 +01:00
await backendManager . PutAsync ( fsw , null , null , false , taskreader . ProgressToken );
}
catch
{
await database . RollbackTransactionAsync ();
fsw ?. Dispose ();
throw ;
2020-02-23 13:21:17 -06:00
}
2016-02-22 22:40:27 +01:00
}
}
}