packkit/archive

Archives held in memory: a format, a list of entries and an optional comment.

Archive and Entry are plain records, so you build them with their constructors and read them by field access or pattern matching. Paths and metadata are checked when the archive is written; see packkit.write.

Types

An archive held in memory.

pub type Archive {
  Archive(
    format: ArchiveFormat,
    entries: List(Entry),
    comment: option.Option(String),
  )
}

Constructors

  • Archive(
      format: ArchiveFormat,
      entries: List(Entry),
      comment: option.Option(String),
    )

    Arguments

    entries

    Entries in the order they are written.

    comment

    Archive comment. Only ZIP stores one; the other formats return CommentUnsupported when it is set.

Errors from reading and writing archive.

pub type ArchiveError {
  InvalidData(message: String)
  Unsupported(feature: String)
  EntryRejected(path: String, reason: String)
  LimitExceeded(limit: String, actual: Int)
  FieldOverflow(field: String, value: Int, max: Int)
  CommentUnsupported(format: ArchiveFormat)
  FormatMismatch(
    archive: ArchiveFormat,
    requested: ArchiveFormat,
  )
  CodecFailed(step: String, cause: codec.CodecError)
}

Constructors

  • InvalidData(message: String)

    The bytes are not a valid archive of the requested format.

  • Unsupported(feature: String)

    The input is valid but uses a feature packkit does not read or write.

  • EntryRejected(path: String, reason: String)

    An entry has a path or metadata that cannot be stored safely, such as an absolute path, .., or a negative owner id.

  • LimitExceeded(limit: String, actual: Int)

    The input went over one of the Limits.

  • FieldOverflow(field: String, value: Int, max: Int)

    A numeric field is too large for the format, such as a tar uid over 21 bits.

  • CommentUnsupported(format: ArchiveFormat)

    The archive has a comment and the format has no place for one.

  • FormatMismatch(archive: ArchiveFormat, requested: ArchiveFormat)

    The recipe’s format differs from the archive’s format.

  • CodecFailed(step: String, cause: codec.CodecError)

    A codec in a recipe failed. step is "encode" or "decode".

An archive format.

pub type ArchiveFormat {
  Tar
  Zip
  SevenZ
  Cpio
  Ar
}

Constructors

  • Tar

    POSIX ustar. The reader also accepts GNU long names and PAX headers.

  • Zip

    ZIP, including Zip64.

  • SevenZ

    7z with one folder.

  • Cpio

    cpio in the SVR4 “newc” layout.

  • Ar

    Unix ar with BSD long names. The reader also accepts the GNU name table.

One file, directory or link in an archive.

pub type Entry {
  Entry(
    path: String,
    kind: EntryKind,
    mode: Int,
    user_id: Int,
    group_id: Int,
    modified_at: Int,
  )
}

Constructors

  • Entry(
      path: String,
      kind: EntryKind,
      mode: Int,
      user_id: Int,
      group_id: Int,
      modified_at: Int,
    )

    Arguments

    path

    Relative path with / separators and no trailing slash.

    mode

    Permission bits, such as 0o644.

    modified_at

    Modification time in Unix seconds.

What an entry holds.

pub type EntryKind {
  File(body: BitArray)
  Directory
  Symlink(target: String)
  Hardlink(target: String)
}

Constructors

  • File(body: BitArray)
  • Directory
  • Symlink(target: String)
  • Hardlink(target: String)

Values

pub fn entry_by_path(
  archive: Archive,
  path path: String,
) -> Result(Entry, Nil)

Returns the first entry whose path is path.

A tar archive can hold two entries with the same path. tar -x keeps the last one; to get that entry, search list.reverse(archive.entries) instead.

Examples

let archive =
  Archive(format: Tar, entries: [new_entry("a.txt", File(<<>>))], comment: None)
entry_by_path(archive, "a.txt")
// -> Ok(Entry(path: "a.txt", ..))
entry_by_path(archive, "b.txt")
// -> Error(Nil)
pub fn error_to_string(error: ArchiveError) -> String

Describes an error in one line, for logs and command-line output.

Examples

error_to_string(CommentUnsupported(format: Tar))
// -> "archive: tar cannot store an archive comment"
pub fn format_to_string(format: ArchiveFormat) -> String

Returns the short name of a format: "tar", "zip", "7z", "cpio" or "ar".

pub fn new_entry(
  path path: String,
  kind kind: EntryKind,
) -> Entry

Creates an entry with owner 0, modification time 0 and the usual mode for its kind: 0o644 for files and hard links, 0o755 for directories and 0o777 for symbolic links.

Examples

let entry = new_entry(path: "bin/run", kind: File(<<"#!/bin/sh\n":utf8>>))
Entry(..entry, mode: 0o755)
✨ Search Document