uk.org.retep.util.io.tar.GnuHeader Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2009, Peter T Mount
* All rights reserved.
*
* 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.
*
* - Neither the name of the retep.org.uk nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
* 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 OWNER
* 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.
*/
package uk.org.retep.util.io.tar;
import java.nio.ByteBuffer;
import uk.org.retep.util.string.StringUtils;
/**
* A {@link TarHeader} for a standard file
*
* @author peter
*/
public abstract class GnuHeader
{
/**
* This represents a directory and a list of files created by the
* '--incremental' ('-G') option to gnu tar.
*
* The size field gives the total size of the associated list of files.
*
*
*
* Each file name is preceded by either a 'Y'
* (the file should be in this archive) or an 'N'. (The file is a directory,
* or is not stored in the archive.)
*
*
*
* Each file name is terminated by a null. There is an additional null
* after the last file name.
*
*/
public static class DumpDir
extends TarHeader
{
@Override
public TarHeaderType getTarHeaderType()
{
return TarHeaderType.GNU_DUMPDIR;
}
}
/**
* A Regular file who's name is longer than supported by the Posix standard
* @see StandardHeader.RegularFile
*/
public static class LongNameFile
extends TarHeader.NamedHeader
{
private String longName;
public LongNameFile( final String longName )
{
// Darwin tar seems to write this?
super( "././@LongLink" );
this.longName = longName;
size = longName.length() + 1; // +1 for null
}
@Override
public TarHeaderType getTarHeaderType()
{
return TarHeaderType.GNU_LONGNAME;
}
@Override
protected int getAdditionalSize()
{
// This needs to be padded out to a block size
return getBlockSize( (int) size );
}
@Override
protected void putAdditional( final ByteBuffer buffer )
{
StringUtils.putC( buffer, getAdditionalSize(), longName );
}
}
/**
* This represents a file continued from another volume of a multi-volume
* archive created with the '--multi-volume' ('-M') option to gnu tar.
*
*
* The original type of the file is not given here.
*
*
*
* The size field gives the maximum size of this piece of the file
* (assuming the volume does not end before the file is written out).
*
*
*
* The offset field gives the offset from the beginning of the file where
* this part of the file begins. Thus size plus offset should equal the
* original size of the file.
*
*/
public static class MultiVolume
extends TarHeader
{
@Override
public TarHeaderType getTarHeaderType()
{
return TarHeaderType.GNU_MULTIVOL;
}
}
/**
* This flag indicates that we are dealing with a sparse file.
* Note that archiving a sparse file requires special operations to find
* holes in the file, which mark the positions of these holes, along with
* the number of bytes of data to be found after the hole.
*/
public static class SparseFile
extends TarHeader
{
@Override
public TarHeaderType getTarHeaderType()
{
return TarHeaderType.GNU_SPARSE;
}
}
/**
* This file type is used to mark the volume header that was given with the
* '--label=archive-label' ('-V archive-label') option in gnu tar when the
* archive was created.
*
*
* The name field contains the name given after the '--label=archive-label'
* ('-V archive-label') option in gnu tar.
*
*
*
* The size field is zero. Only the first file in each volume of an archive
* should have this type.
*
*/
public static class VolumeHeader
extends TarHeader.NamedHeader
{
public VolumeHeader( final String name )
{
super( name );
}
@Override
public TarHeaderType getTarHeaderType()
{
return TarHeaderType.GNU_VOLUME_HEADER;
}
}
}