Grav v1.14.0

Tar

import "std/data" (included in import "std") · namespace std.data

Tar archives (POSIX ustar, with PAX headers for long names and files over 8 GB), written and read a piece at a time. Combined with GzipWriter and GzipReader they make and read .tar.gz files of any size in a little memory. GNU tar and bsdtar read what TarWriter writes, and TarReader reads theirs, including GNU's own long names.

grav
import "std"

fn main() {
    let path = Dir.temp() + "/backup.tar.gz"
    let gz = GzipWriter.toFile(path)
    let tar = new TarWriter(fn(b: Bytes) { gz.write(b) })
    tar.addString("README.txt", "hello\n")
    tar.addDir("data")
    let rows = Bytes.fromString("id,name\n1,Ada\n")
    tar.beginFile("data/users.csv", rows.size() as long)   // a big file: its size, then its pieces
    tar.writeData(rows.slice(0, 8)).writeData(rows.slice(8, rows.size()))
    tar.finish()
    gz.finish()

    let src = GzipReader.fromFile(path)
    let r = new TarReader(fn(n: int) -> Bytes { return src.read(n) })
    while (true) {
        let e = r.next()
        if (e == null) { break }
        print("${e.type} ${e.name} ${e.size}")
        if (e.isFile()) { print("  " + Str.replace(r.readAll().toString(), "\n", "|")) }
    }
    new File(path).delete()
}
Output
file README.txt 6
  hello|
dir data/ 0
file data/users.csv 14
  id,name|1,Ada|
  • Writing. addFile(name, bytes), addString, addDir, addSymlink, and for contents too big to hold, beginFile(name, size) followed by writeData pieces adding up to exactly size. finish() writes the end of the archive. Entries take a mode (0644 by default) and an mtime (now by default).
  • Reading. next() gives each TarEntry (name, size, mode, mtime, type: file, dir, symlink, link or other, and linkName), skipping whatever was not read of the one before; read(max) gives its data in pieces and readAll() all at once. A header with a wrong checksum, or an archive that ends inside an entry, is a ValueError.

API

class TarWriter

Writes a tar archive to a sink, a piece at a time.

Constructors

  • new TarWriter(sink: fn(Bytes))

Static members

  • static fn zeros(n: int) -> Bytes — n zero bytes.

Fields

  • written: long = 0 — Bytes handed to the sink so far.

Methods

  • fn addFile(name: string, data: Bytes, mode: int = 420, mtime: long = -1) -> TarWriter — A file with these contents.
  • fn addString(name: string, text: string, mode: int = 420, mtime: long = -1) -> TarWriter
  • fn addDir(name: string, mode: int = 493, mtime: long = -1) -> TarWriter — A directory entry ("logs/").
  • fn addSymlink(name: string, target: string, mtime: long = -1) -> TarWriter — A symbolic link to target.
  • fn beginFile(name: string, size: long, mode: int = 420, mtime: long = -1) -> TarWriter — Starts a file of exactly size bytes, given next with writeData (in any number of pieces): for contents too big to hold at once.
  • fn writeData(data: Bytes) -> TarWriter — The next piece of the file begun with beginFile.
  • fn finish() — Ends the archive (two empty blocks); once.

class TarEntry

One entry of a tar archive.

Constructors

  • new TarEntry()

Fields

  • name: string = ""
  • size: long = 0
  • mode: int = 0
  • mtime: long = 0 — Unix seconds.
  • type: string = "file" — "file", "dir", "symlink", "link" (hard) or "other".
  • linkName: string = ""

Methods

  • fn isFile() -> bool
  • fn isDir() -> bool

class TarReader

Reads a tar archive from a source, a piece at a time: next() moves to the next entry (skipping what was not read of the last), read() gives its data.

Constructors

  • new TarReader(source: fn(int) -> Bytes) — Compressed or not, the archive comes from source (up to the number of bytes given; an empty result means there is no more).

Methods

  • fn next() -> TarEntry — The next entry, or null at the end of the archive. ValueError when it is not a tar archive.
  • fn read(max: int = 65536) -> Bytes — Up to max bytes of the current entry's data; empty when it is all read.
  • fn readAll() -> Bytes — The rest of the current entry's data at once.

Edit this page on GitHub