2016-02-22 22:40:27 +01:00
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System ;
using CoCoL ;
using Duplicati.Library.Main.Volumes ;
using System.Threading.Tasks ;
using System.Linq ;
using Duplicati.Library.Main.Operation.Common ;
using System.IO ;
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 ));
2017-05-30 00:35:31 +02:00
public static Task Run ( BackupDatabase database , Options options , BackupResults result , ITaskReader taskreader , string lasttempfilelist , long lasttempfileid )
2016-02-22 22:40:27 +01:00
{
return AutomationExtensions . RunTask ( new
2016-02-24 23:48:42 +01:00
{
UploadChannel = Channels . BackendRequest . ForWrite
},
async self =>
2017-05-30 00:35:31 +02:00
{
// Check if we should upload a synthetic filelist
if ( options . DisableSyntheticFilelist || string . IsNullOrWhiteSpace ( lasttempfilelist ) || lasttempfileid < 0 )
return ;
// Check that we still need to process this after the cleanup has performed its duties
var syntbase = await database . GetRemoteVolumeFromIDAsync ( lasttempfileid );
// If we do not have a valid entry, warn and quit
if ( syntbase . Name == null || syntbase . State != RemoteVolumeState . Uploaded )
2016-02-22 22:40:27 +01:00
{
2017-05-30 00:35:31 +02:00
// TODO: If the repair succeeds, this could give a false warning?
2018-04-11 23:02:47 +02:00
Logging . Log . WriteWarningMessage ( LOGTAG , "MissingTemporaryFilelist" , null , "Expected there to be a temporary fileset for synthetic filelist ({0}, {1}), but none was found?" , lasttempfileid , lasttempfilelist );
2017-05-30 00:35:31 +02:00
return ;
}
2016-02-22 22:40:27 +01:00
2017-05-30 00:35:31 +02:00
// Files is missing or repaired
if ( syntbase . Name == null || ( syntbase . State != RemoteVolumeState . Uploading && syntbase . State != RemoteVolumeState . Temporary ))
{
2018-04-11 23:02:47 +02:00
Logging . Log . WriteInformationMessage ( LOGTAG , "SkippingSyntheticListUpload" , "Skipping synthetic upload because temporary fileset appers to be complete: ({0}, {1}, {2})" , lasttempfileid , lasttempfilelist , syntbase . State );
2017-05-30 00:35:31 +02:00
return ;
}
// Ready to build and upload the synthetic list
await database . CommitTransactionAsync ( "PreSyntheticFilelist" );
var incompleteFilesets = ( await database . GetIncompleteFilesetsAsync ()). OrderBy ( x => x . Value ). ToList ();
result . OperationProgressUpdater . UpdatePhase ( OperationPhase . Backup_PreviousBackupFinalize );
2018-04-11 23:02:47 +02:00
Logging . Log . WriteInformationMessage ( LOGTAG , "PreviousBackupFilelistUpload" , "Uploading filelist from previous interrupted backup" );
2016-02-24 23:48:42 +01:00
2017-05-30 00:35:31 +02:00
if (! await taskreader . ProgressAsync )
return ;
2016-02-27 02:15:42 +01:00
2017-05-30 00:35:31 +02:00
var incompleteSet = incompleteFilesets . Last ();
var badIds = from n in incompleteFilesets select n . Key ;
2016-02-24 23:48:42 +01:00
2017-05-30 00:35:31 +02:00
var prevs = ( from n in await database . GetFilesetTimesAsync ()
where
n . Key < incompleteSet . Key
&&
! badIds . Contains ( n . Key )
orderby n . Key
select n . Key ). ToArray ();
2016-02-24 23:48:42 +01:00
2017-05-30 00:35:31 +02:00
var prevId = prevs . Length == 0 ? - 1 : prevs . Last ();
2016-02-22 22:40:27 +01:00
2016-02-24 23:48:42 +01:00
try
2016-02-22 22:40:27 +01:00
{
2016-02-24 23:48:42 +01:00
var s = 1 ;
var fileTime = incompleteSet . Value + TimeSpan . FromSeconds ( s );
// Probe for an unused filename
while ( s < 60 )
{
var id = await database . GetRemoteVolumeIDAsync ( VolumeBase . GenerateFilename ( RemoteVolumeType . Files , options , null , fileTime ));
if ( id < 0 )
break ;
fileTime = incompleteSet . Value + TimeSpan . FromSeconds (++ s );
}
2016-02-22 22:40:27 +01:00
2019-09-26 21:13:50 -07:00
using ( FilesetVolumeWriter fsw = new FilesetVolumeWriter ( options , fileTime ))
{
fsw . VolumeID = await database . RegisterRemoteVolumeAsync ( fsw . RemoteFilename , RemoteVolumeType . Files , RemoteVolumeState . Temporary );
2016-02-22 22:40:27 +01:00
2019-09-26 21:13:50 -07: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 ));
2016-02-22 22:40:27 +01:00
2019-09-26 21:13:50 -07:00
var newFilesetID = await database . CreateFilesetAsync ( fsw . VolumeID , fileTime );
await database . LinkFilesetToVolumeAsync ( newFilesetID , fsw . VolumeID );
await database . AppendFilesFromPreviousSetAsync ( null , newFilesetID , prevId , fileTime );
2019-09-10 13:12:21 -04:00
2019-09-26 21:13:50 -07:00
await database . WriteFilesetAsync ( fsw , newFilesetID );
2016-02-22 22:40:27 +01:00
2019-09-26 21:13:50 -07:00
if (! await taskreader . ProgressAsync )
return ;
2016-02-22 22:40:27 +01:00
2019-09-26 21:13:50 -07:00
await database . UpdateRemoteVolumeAsync ( fsw . RemoteFilename , RemoteVolumeState . Uploading , - 1 , null );
await database . CommitTransactionAsync ( "CommitUpdateFilelistVolume" );
await self . UploadChannel . WriteAsync ( new FilesetUploadRequest ( fsw ));
}
2016-02-22 22:40:27 +01:00
}
2016-02-24 23:48:42 +01:00
catch
{
await database . RollbackTransactionAsync ();
throw ;
}
}
2017-05-30 00:35:31 +02:00
);
2016-02-22 22:40:27 +01:00
}
}
}