encoder

package
v0.29.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package encoder creates pxar archives.

Encoder writes pxar archive entries in a single sequential pass. It supports both unified (v1) and split (v2) formats via separate io.Writer outputs.

Usage

enc := encoder.NewEncoder(output, nil, &rootMeta, nil)
enc.AddFile(&fileMeta, "hello.txt", []byte("hello world"))
enc.Close()

For split archives, provide a payload writer to NewEncoder and file content is written to the payload stream with PAYLOAD_REF entries in the metadata stream.

Directory Nesting

Directories are created with CreateDirectory and finalized with Finish. The encoder maintains a state stack, so Finish closes the most recently opened directory and resumes the parent's context. Close finalizes the root directory and writes the goodbye table.

AddFile and AddPayloadRef return a LinkOffset token. Pass this token to AddHardlink to create a hardlink that references the original file via a relative offset in the wire format.

Streaming Files

For large files, use CreateFile to obtain a *FileWriter (io.Writer). Write content via io.Copy or direct writes, then call Close on the writer.

Payload References

AddPayloadRef writes a PAYLOAD_REF entry pointing to existing payload data without writing the content. Use PayloadPosition and Advance to track virtual payload positions for external chunk injection.

Package encoder creates pxar archives.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder writes pxar archives.

func NewEncoder

func NewEncoder(output, payloadOut io.Writer, metadata *pxar.Metadata, prelude []byte) *Encoder

NewEncoder creates a new pxar encoder writing to the given writers. If payloadOut is non-nil, the archive is split (v2 format). metadata describes the root directory. prelude is optional v2 prelude data.

func (*Encoder) AddDevice

func (e *Encoder) AddDevice(metadata *pxar.Metadata, name string, device format.Device) error

AddDevice adds a device node.

func (*Encoder) AddFIFO

func (e *Encoder) AddFIFO(metadata *pxar.Metadata, name string) error

AddFIFO adds a named pipe.

func (*Encoder) AddFile

func (e *Encoder) AddFile(metadata *pxar.Metadata, name string, content []byte) (LinkOffset, error)

AddFile adds a complete file to the archive.

func (e *Encoder) AddHardlink(name string, target string, targetOffset LinkOffset) error

AddHardlink adds a hard link.

func (*Encoder) AddPayloadRef added in v0.17.0

func (e *Encoder) AddPayloadRef(metadata *pxar.Metadata, name string, fileSize uint64, payloadOffset uint64) (LinkOffset, error)

AddPayloadRef adds a file entry that references existing payload data. It writes the metadata entry (filename + stat + PXAR_PAYLOAD_REF) but does NOT write any real payload data. Instead, it writes a PXAR_PAYLOAD header + zero-fill to maintain correct offsets in the payload stream. The caller is responsible for ensuring the original payload chunks are available in the datastore (either via injection or dedup).

func (*Encoder) AddSocket

func (e *Encoder) AddSocket(metadata *pxar.Metadata, name string) error

AddSocket adds a named socket.

func (e *Encoder) AddSymlink(metadata *pxar.Metadata, name string, target string) error

AddSymlink adds a symbolic link.

func (*Encoder) Advance added in v0.17.0

func (e *Encoder) Advance(size uint64) error

Advance advances the payload write position by the given size. This is used with AddPayloadRef to track the virtual payload size without actually writing payload data.

func (*Encoder) Close

func (e *Encoder) Close() error

Close finalizes the archive (writes root goodbye table and finishes).

func (*Encoder) CreateDirectory

func (e *Encoder) CreateDirectory(name string, metadata *pxar.Metadata) error

CreateDirectory pushes a new directory onto the stack.

func (*Encoder) CreateFile

func (e *Encoder) CreateFile(metadata *pxar.Metadata, name string, size uint64) (*FileWriter, error)

CreateFile returns a FileWriter for streaming file content.

func (*Encoder) Finish

func (e *Encoder) Finish() error

Finish finalizes the current directory (pops state, writes goodbye table).

func (*Encoder) PayloadPosition added in v0.17.0

func (e *Encoder) PayloadPosition() uint64

PayloadPosition returns the current write position in the payload stream.

type FileWriter

type FileWriter struct {
	// contains filtered or unexported fields
}

FileWriter writes file content to a pxar archive.

func (*FileWriter) Close

func (fw *FileWriter) Close() error

Close finalizes the file entry.

func (*FileWriter) FileOffset

func (fw *FileWriter) FileOffset() LinkOffset

FileOffset returns the file's offset for use with AddHardlink.

func (*FileWriter) Write

func (fw *FileWriter) Write(data []byte) (int, error)

Write writes data to the file.

func (*FileWriter) WriteAll

func (fw *FileWriter) WriteAll(data []byte) error

WriteAll writes all data to the file.

type LinkOffset

type LinkOffset uint64

LinkOffset represents a file offset usable with AddHardlink.

func (LinkOffset) Raw

func (o LinkOffset) Raw() uint64

Raw returns the raw byte offset.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL