2023-01-24 15:29:31 +01:00
|
|
|
|
/* Copyright (c) Cloud Software Group, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
|
* that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2020-12-07 03:19:35 +01:00
|
|
|
|
using System.Reflection;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using System.Text;
|
|
|
|
|
using ICSharpCode.SharpZipLib.Tar;
|
|
|
|
|
|
|
|
|
|
namespace XenCenterLib.Archive
|
|
|
|
|
{
|
2022-11-18 14:37:18 +01:00
|
|
|
|
public class TarArchiveWriter : ArchiveWriter
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-12-07 03:19:35 +01:00
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private TarOutputStream tar = null;
|
2020-12-07 03:19:35 +01:00
|
|
|
|
private const long bufferSize = 32 * 1024;
|
|
|
|
|
private bool disposed;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-01-04 21:54:11 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parameterless constructor needed by tests
|
|
|
|
|
/// </summary>
|
2022-11-18 14:37:18 +01:00
|
|
|
|
public TarArchiveWriter()
|
2020-05-26 14:44:24 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-18 14:37:18 +01:00
|
|
|
|
public TarArchiveWriter(Stream outputStream)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
tar = new TarOutputStream(outputStream);
|
2019-07-25 11:46:16 +02:00
|
|
|
|
disposed = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void SetBaseStream(Stream outputStream)
|
|
|
|
|
{
|
|
|
|
|
tar = new TarOutputStream(outputStream);
|
|
|
|
|
disposed = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void AddDirectory(string directoryName, DateTime modificationTime)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new StringBuilder(directoryName);
|
2020-12-07 03:19:35 +01:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
//Need to add a terminal front-slash to add a directory
|
|
|
|
|
if (!directoryName.EndsWith("/"))
|
|
|
|
|
sb.Append("/");
|
|
|
|
|
TarEntry entry = TarEntry.CreateTarEntry(sb.ToString());
|
|
|
|
|
entry.ModTime = modificationTime;
|
|
|
|
|
|
|
|
|
|
tar.PutNextEntry(entry);
|
|
|
|
|
tar.CloseEntry();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-22 16:41:54 +02:00
|
|
|
|
public override void Add(Stream fileToAdd, string fileName, DateTime modificationTime, Action cancellingDelegate)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
TarEntry entry = TarEntry.CreateTarEntry(fileName);
|
2023-09-22 16:41:54 +02:00
|
|
|
|
entry.Size = fileToAdd.Length;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
entry.ModTime = modificationTime;
|
|
|
|
|
|
2020-12-07 03:19:35 +01:00
|
|
|
|
tar.PutNextEntry(entry);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
byte[] buffer = new byte[bufferSize];
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
//You have to do this because if using a memory stream the pointer will be at the end it
|
|
|
|
|
//it's just been read and this will cause nothing to be written out
|
2023-09-22 16:41:54 +02:00
|
|
|
|
fileToAdd.Position = 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2023-09-22 16:41:54 +02:00
|
|
|
|
while ((n = fileToAdd.Read(buffer, 0, buffer.Length)) > 0)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-12-06 03:43:54 +01:00
|
|
|
|
cancellingDelegate?.Invoke();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
tar.Write(buffer, 0, n);
|
|
|
|
|
}
|
2020-12-07 03:19:35 +01:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
tar.Flush();
|
|
|
|
|
tar.CloseEntry();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
2020-12-07 03:19:35 +01:00
|
|
|
|
if (!disposed && disposing)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-12-07 03:19:35 +01:00
|
|
|
|
try
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-12-07 03:19:35 +01:00
|
|
|
|
tar?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
//workaround for CA-347483
|
|
|
|
|
log.Error("Failed to dispose tar output stream", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disposed = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2020-12-07 03:19:35 +01:00
|
|
|
|
|
|
|
|
|
base.Dispose(disposing);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|