//
// Copyright (c) 2008-2011, Kenneth Bell
//
// 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.
//
using System;
using System.Collections.Generic;
using System.IO;
using DiscUtils.Streams;
namespace DiscUtils.Archives
{
///
/// Builder to create UNIX Tar archive files.
///
public sealed class TarFileBuilder : StreamBuilder
{
private readonly List _files;
///
/// Initializes a new instance of the class.
///
public TarFileBuilder()
{
_files = new List();
}
///
/// Add a file to the tar archive.
///
/// The name of the file.
/// The file data.
public void AddFile(string name, byte[] buffer)
{
_files.Add(new UnixBuildFileRecord(name, buffer));
}
///
/// Add a file to the tar archive.
///
/// The name of the file.
/// The file data.
/// The access mode of the file.
/// The uid of the owner.
/// The gid of the owner.
/// The modification time for the file.
public void AddFile(
string name, byte[] buffer, UnixFilePermissions fileMode, int ownerId, int groupId,
DateTime modificationTime)
{
_files.Add(new UnixBuildFileRecord(name, buffer, fileMode, ownerId, groupId, modificationTime));
}
///
/// Add a file to the tar archive.
///
/// The name of the file.
/// The file data.
public void AddFile(string name, Stream stream)
{
_files.Add(new UnixBuildFileRecord(name, stream));
}
///
/// Add a file to the tar archive.
///
/// The name of the file.
/// The file data.
/// The access mode of the file.
/// The uid of the owner.
/// The gid of the owner.
/// The modification time for the file.
public void AddFile(
string name, Stream stream, UnixFilePermissions fileMode, int ownerId, int groupId,
DateTime modificationTime)
{
_files.Add(new UnixBuildFileRecord(name, stream, fileMode, ownerId, groupId, modificationTime));
}
protected override List FixExtents(out long totalLength)
{
List result = new List(_files.Count * 2 + 2);
long pos = 0;
foreach (UnixBuildFileRecord file in _files)
{
BuilderExtent fileContentExtent = file.Fix(pos + TarHeader.Length);
result.Add(new TarHeaderExtent(
pos, file.Name, fileContentExtent.Length, file.FileMode, file.OwnerId, file.GroupId,
file.ModificationTime));
pos += TarHeader.Length;
result.Add(fileContentExtent);
pos += MathUtilities.RoundUp(fileContentExtent.Length, 512);
}
// Two empty 512-byte blocks at end of tar file.
result.Add(new BuilderBufferExtent(pos, new byte[1024]));
totalLength = pos + 1024;
return result;
}
}
}