2025-01-10 09:24:31 +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
|
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
2024-02-28 15:45:30 +01:00
|
|
|
using System;
|
2013-03-30 15:23:09 +01:00
|
|
|
using System.IO;
|
2019-09-10 13:06:55 -04:00
|
|
|
using Duplicati.Library.Interface;
|
2019-10-04 21:37:33 -07:00
|
|
|
using Duplicati.Library.Main.Operation.Common;
|
2013-03-30 15:23:09 +01:00
|
|
|
|
2013-05-08 20:17:07 +02:00
|
|
|
namespace Duplicati.Library.Main.Volumes
|
2013-03-30 15:23:09 +01:00
|
|
|
{
|
|
|
|
|
public abstract class VolumeWriterBase : VolumeBase, IDisposable
|
|
|
|
|
{
|
|
|
|
|
protected ICompression m_compression;
|
|
|
|
|
protected Library.Utility.TempFile m_localfile;
|
2017-12-25 04:12:19 +07:00
|
|
|
protected Stream m_localFileStream;
|
2013-03-30 15:23:09 +01:00
|
|
|
protected string m_volumename;
|
|
|
|
|
public string LocalFilename { get { return m_localfile; } }
|
|
|
|
|
public string RemoteFilename { get { return m_volumename; } }
|
2013-07-23 18:56:01 +02:00
|
|
|
public Library.Utility.TempFile TempFile { get { return m_localfile; } }
|
2013-03-30 15:23:09 +01:00
|
|
|
|
|
|
|
|
public abstract RemoteVolumeType FileType { get; }
|
2013-04-08 22:21:03 +02:00
|
|
|
|
|
|
|
|
public long VolumeID { get; set; }
|
2017-12-25 04:12:19 +07:00
|
|
|
|
2013-03-30 15:23:09 +01:00
|
|
|
public void SetRemoteFilename(string name)
|
|
|
|
|
{
|
2016-09-15 11:39:27 +02:00
|
|
|
m_volumename = name;
|
2013-03-30 15:23:09 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-07 17:58:46 -07:00
|
|
|
protected VolumeWriterBase(Options options)
|
2013-03-30 15:23:09 +01:00
|
|
|
: this(options, DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
}
|
2017-12-25 04:12:19 +07:00
|
|
|
|
2018-10-06 13:30:13 -07:00
|
|
|
public static string GenerateGuid()
|
2013-04-29 23:50:38 +02:00
|
|
|
{
|
2016-09-15 11:39:27 +02:00
|
|
|
var s = Guid.NewGuid().ToString("N");
|
2017-12-25 04:12:19 +07:00
|
|
|
|
2016-09-15 11:39:27 +02:00
|
|
|
//We can choose shorter GUIDs here
|
2017-12-25 04:12:19 +07:00
|
|
|
|
2016-09-15 11:39:27 +02:00
|
|
|
return s;
|
2017-12-25 04:12:19 +07:00
|
|
|
|
2013-04-29 23:50:38 +02:00
|
|
|
}
|
2013-03-30 15:23:09 +01:00
|
|
|
|
2016-03-20 12:38:36 +01:00
|
|
|
public void ResetRemoteFilename(Options options, DateTime timestamp)
|
|
|
|
|
{
|
2018-10-06 13:30:13 -07:00
|
|
|
m_volumename = GenerateFilename(this.FileType, options.Prefix, GenerateGuid(), timestamp, options.CompressionModule, options.NoEncryption ? null : options.EncryptionModule);
|
2016-03-20 12:38:36 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-07 17:58:46 -07:00
|
|
|
protected VolumeWriterBase(Options options, DateTime timestamp)
|
2013-03-30 15:23:09 +01:00
|
|
|
: base(options)
|
|
|
|
|
{
|
2016-09-28 10:36:40 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(options.AsynchronousUploadFolder))
|
2018-01-26 09:22:21 +01:00
|
|
|
m_localfile = Library.Utility.TempFile.CreateInFolder(options.AsynchronousUploadFolder, true);
|
2016-09-28 10:36:40 +02:00
|
|
|
else
|
|
|
|
|
m_localfile = new Library.Utility.TempFile();
|
2013-03-30 15:23:09 +01:00
|
|
|
|
2016-03-20 12:38:36 +01:00
|
|
|
ResetRemoteFilename(options, timestamp);
|
2017-12-25 04:12:19 +07:00
|
|
|
|
|
|
|
|
m_localFileStream = new System.IO.FileStream(m_localfile, FileMode.Create, FileAccess.Write, FileShare.Read);
|
|
|
|
|
m_compression = DynamicLoader.CompressionLoader.GetModule(options.CompressionModule, m_localFileStream, ArchiveMode.Write, options.RawOptions);
|
|
|
|
|
|
|
|
|
|
if (m_compression == null)
|
2018-03-12 14:07:11 +01:00
|
|
|
throw new UserInformationException(string.Format("Unsupported compression module: {0}", options.CompressionModule), "UnsupportedCompressionModule");
|
2017-12-25 04:12:19 +07:00
|
|
|
|
2019-09-10 13:06:55 -04:00
|
|
|
AddManifestFile();
|
2013-03-30 15:23:09 +01:00
|
|
|
}
|
|
|
|
|
|
2019-09-10 13:06:55 -04:00
|
|
|
protected void AddManifestFile()
|
2013-03-30 15:23:09 +01:00
|
|
|
{
|
|
|
|
|
using (var sr = new StreamWriter(m_compression.CreateFile(MANIFEST_FILENAME, CompressionHint.Compressible, DateTime.UtcNow), ENCODING))
|
2013-04-04 20:34:26 +02:00
|
|
|
sr.Write(ManifestData.GetManifestInstance(m_blocksize, m_blockhash, m_filehash));
|
2013-03-30 15:23:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (m_compression != null)
|
|
|
|
|
try { m_compression.Dispose(); }
|
|
|
|
|
finally { m_compression = null; }
|
|
|
|
|
|
2017-12-25 04:12:19 +07:00
|
|
|
if (m_localFileStream != null)
|
|
|
|
|
try { m_localFileStream.Dispose(); }
|
|
|
|
|
finally { m_localFileStream = null; }
|
2017-08-13 11:58:39 +01:00
|
|
|
|
2013-03-30 15:23:09 +01:00
|
|
|
if (m_localfile != null)
|
2020-02-23 13:21:17 -06:00
|
|
|
{
|
|
|
|
|
m_localfile.Protected = false;
|
2013-03-30 15:23:09 +01:00
|
|
|
try { m_localfile.Dispose(); }
|
|
|
|
|
finally { m_localfile = null; }
|
2020-02-23 13:21:17 -06:00
|
|
|
}
|
2013-03-30 15:23:09 +01:00
|
|
|
|
|
|
|
|
m_volumename = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Close()
|
|
|
|
|
{
|
|
|
|
|
if (m_compression != null)
|
|
|
|
|
try { m_compression.Dispose(); }
|
|
|
|
|
finally { m_compression = null; }
|
2017-12-25 04:12:19 +07:00
|
|
|
|
|
|
|
|
if (m_localFileStream != null)
|
|
|
|
|
try { m_localFileStream.Dispose(); }
|
|
|
|
|
finally { m_localFileStream = null; }
|
2013-03-30 15:23:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long Filesize { get { return m_compression.Size + m_compression.FlushBufferSize; } }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|