Class PosixTarHeader

java.lang.Object
google.registry.util.PosixTarHeader

@Immutable public final class PosixTarHeader extends Object
POSIX Tar Header.

This class represents the 512-byte header that precedes each file within a tar file archive. It's in the POSIX ustar format which is equivalent to using the following GNU tar flags: tar --format=ustar. It's called ustar because you're a star!

Warning: This class is not a complete tar implementation. It also offers no abstractions beyond the header format and has only been tested against very simple archives created in the ustar format and the default format for gnu and bsd tar. If your goal is to be able to read generic tar files, you should use a more mature tar implementation like Apache Commons Compress.

This class is only really useful in situations where the following statements are true:

  1. You want to create tar archives.
  2. You don't need to be able to read tar files from external sources.
  3. You don't want additional dependencies.
  4. You don't need fancy features like symbolic links.
  5. You want something that's a step above writing out the bytes by hand.

To create a tar archive using this class, you must do the following: For each file in the archive, output a header and the file contents (null-padded to the nearest 512-byte boundary). Then output another 1024 null bytes to indicate end of archive.

The ustar tar header contains the following fields:

name
[Offset: 0, Length: 100]
C String which we'll assume is UTF-8 (Offset: 0)
mode
[Offset: 100, Length: 8]
ASCII 7-digit zero-padded octal file mode and null byte.
uid
[Offset: 108, Length: 8]
ASCII 7-digit zero-padded octal UNIX user ID and null byte.
gid
[Offset: 116, Length: 8]
ASCII 7-digit zero-padded octal UNIX group ID and null byte.
size
[Offset: 124, Length: 12]
ASCII 11-digit zero-padded octal file size and null byte.
mtime
[Offset: 136, Length: 12]
ASCII octal UNIX timestamp modified time and null byte.
chksum
[Offset: 148, Length: 8]
ASCII octal sum of all header bytes where chksum are 0's.
typeflag
[Offset: 156]
Always '0' (zero character) for regular type of file.
linkname
[Offset: 157, Length: 100]
All null bytes because we don't support symbolic links.
magic
[Offset: 257, Length: 6]
Always the C string "ustar".
version
[Offset: 263, Length: 2]
Always "00" without a null or blank on GNU systems.
uname
[Offset: 265, Length: 32]
The C string UNIX user name corresponding to uid.
gname
[Offset: 297, Length: 32]
The C string UNIX group name corresponding to gid.
devmajor
[Offset: 329, Length: 8]
Not supported; set to zero.
devminor
[Offset: 337, Length: 8]
Not supported; set to zero.
prefix
[Offset: 345, Length: 155]
Not supported; set to null.
See Also:
  • Field Details

  • Method Details

    • from

      public static PosixTarHeader from(byte[] header)
      Create a new header from the bytes of an existing tar file header (Safe).

      This routine validates the checksum to ensure the data header is correct and supported.

      Parameters:
      header - the existing and assumed correct header. Value is defensively copied.
      Throws:
      IllegalArgumentException - if header isn't ustar or has a bad checksum.
      NullPointerException - if header is null.
    • getBytes

      public byte[] getBytes()
      Returns 512-byte tar header (safe copy).
    • getName

      public String getName()
      Returns the filename.
    • getMode

      public int getMode()
      Returns the octal UNIX mode aka permissions.
    • getUid

      public int getUid()
      Returns the UNIX owner/user id.
    • getGid

      public int getGid()
      Returns the UNIX group id.
    • getSize

      public int getSize()
      Returns the file size in bytes.
    • getMtime

      public org.joda.time.DateTime getMtime()
      Returns the modified time as a UTC DateTime object.
    • getChksum

      public int getChksum()
      Returns the checksum value stored in the .
    • getType

      public PosixTarHeader.Type getType()
      Returns the PosixTarHeader.Type of file.
    • getLinkName

      public String getLinkName()
      Returns the UNIX symbolic link name.

      This feature is unsupported but the getter is included for completeness.

    • getMagic

      public String getMagic()
      Returns the magic field. Only "ustar" is supported.
    • getVersion

      public String getVersion()
      Returns the magic field. Only "00" is supported.
    • getUname

      public String getUname()
      Returns the UNIX user name (or owner) of the file.
    • getGname

      public String getGname()
      Returns the UNIX group name associated with the file.
    • getDevMajor

      public String getDevMajor()
      Returns the devmajor field.

      This feature is unsupported but the getter is included for completeness.

    • getDevMinor

      public String getDevMinor()
      Returns the devminor field.

      This feature is unsupported but the getter is included for completeness.

    • getPrefix

      public String getPrefix()
      Returns the prefix field.

      This feature is unsupported but the getter is included for completeness.

    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
      See Also:
    • equals

      public boolean equals(@Nullable Object rhs)
      Overrides:
      equals in class Object
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object
      See Also: