114 lines
4.4 KiB
C#
114 lines
4.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using AMWD.Common.Packing.Tar.Interfaces;
|
|
using AMWD.Common.Packing.Tar.Utils;
|
|
|
|
namespace AMWD.Common.Packing.Tar
|
|
{
|
|
/// <summary>
|
|
/// Writes a tar (see GNU tar) archive to a stream.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Copied from: <see href="https://github.com/ygoe/DotnetMakeDeb/blob/v1.1.0/DotnetMakeDeb/Tar/TarWriter.cs">DotnetMakeDeb</see>
|
|
/// </remarks>
|
|
public class TarWriter : LegacyTarWriter
|
|
{
|
|
/// <summary>
|
|
/// Initilizes a new instance of the <see cref="TarWriter"/> class.
|
|
/// </summary>
|
|
/// <param name="outStream">The stream to write the archive to.</param>
|
|
public TarWriter(Stream outStream)
|
|
: base(outStream)
|
|
{ }
|
|
|
|
/// <summary>
|
|
/// Writes an entry header (file, dir, ...) to the archive.
|
|
/// </summary>
|
|
/// <param name="name">The name.</param>
|
|
/// <param name="lastModificationTime">The last modification time.</param>
|
|
/// <param name="count">The number of bytes.</param>
|
|
/// <param name="userId">The user id.</param>
|
|
/// <param name="groupId">The group id.</param>
|
|
/// <param name="mode">The access mode.</param>
|
|
/// <param name="entryType">The entry type.</param>
|
|
protected override void WriteHeader(string name, DateTime lastModificationTime, long count, int userId, int groupId, int mode, EntryType entryType)
|
|
{
|
|
var tarHeader = new UsTarHeader()
|
|
{
|
|
FileName = name,
|
|
Mode = mode,
|
|
UserId = userId,
|
|
GroupId = groupId,
|
|
SizeInBytes = count,
|
|
LastModification = lastModificationTime,
|
|
EntryType = entryType,
|
|
UserName = Convert.ToString(userId, 8),
|
|
GroupName = Convert.ToString(groupId, 8)
|
|
};
|
|
OutStream.Write(tarHeader.GetHeaderValue(), 0, tarHeader.HeaderSize);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes an entry header (file, dir, ...) to the archive.
|
|
/// Hashes the username and groupname down to a HashCode.
|
|
/// </summary>
|
|
/// <param name="name">The name.</param>
|
|
/// <param name="lastModificationTime">The last modification time.</param>
|
|
/// <param name="count">The number of bytes.</param>
|
|
/// <param name="userName">The username.</param>
|
|
/// <param name="groupName">The group name.</param>
|
|
/// <param name="mode">The access mode.</param>
|
|
/// <param name="entryType">The entry type.</param>
|
|
protected virtual void WriteHeader(string name, DateTime lastModificationTime, long count, string userName, string groupName, int mode, EntryType entryType)
|
|
{
|
|
WriteHeader(
|
|
name: name,
|
|
lastModificationTime: lastModificationTime,
|
|
count: count,
|
|
userId: userName.GetHashCode(),
|
|
groupId: groupName.GetHashCode(),
|
|
mode: mode,
|
|
entryType: entryType);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a file to the archive.
|
|
/// </summary>
|
|
/// <param name="name">The file name.</param>
|
|
/// <param name="dataSizeInBytes">The filesize in bytes.</param>
|
|
/// <param name="userName">The username.</param>
|
|
/// <param name="groupName">The group name.</param>
|
|
/// <param name="mode">The access mode.</param>
|
|
/// <param name="lastModificationTime">The last modification time.</param>
|
|
/// <param name="writeDelegate">The write handle.</param>
|
|
public virtual void Write(string name, long dataSizeInBytes, string userName, string groupName, int mode, DateTime lastModificationTime, WriteDataDelegate writeDelegate)
|
|
{
|
|
var writer = new DataWriter(OutStream, dataSizeInBytes);
|
|
WriteHeader(name, lastModificationTime, dataSizeInBytes, userName, groupName, mode, EntryType.File);
|
|
while (writer.CanWrite)
|
|
{
|
|
writeDelegate(writer);
|
|
}
|
|
AlignTo512(dataSizeInBytes, false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a file to the archive.
|
|
/// </summary>
|
|
/// <param name="data">The file stream to add to the archive.</param>
|
|
/// <param name="dataSizeInBytes">The filesize in bytes.</param>
|
|
/// <param name="fileName">The file name.</param>
|
|
/// <param name="userId">The user id.</param>
|
|
/// <param name="groupId">The group id.</param>
|
|
/// <param name="mode">The access mode.</param>
|
|
/// <param name="lastModificationTime">The last modification time.</param>
|
|
public void Write(Stream data, long dataSizeInBytes, string fileName, string userId, string groupId, int mode,
|
|
DateTime lastModificationTime)
|
|
{
|
|
WriteHeader(fileName, lastModificationTime, dataSizeInBytes, userId, groupId, mode, EntryType.File);
|
|
WriteContent(dataSizeInBytes, data);
|
|
AlignTo512(dataSizeInBytes, false);
|
|
}
|
|
}
|
|
}
|