automobile

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

automobile

Bare bones CAR file encoder and decoder. Depends only on go-cid.

Install

go get github.com/fil-forge/automobile

Usage

See examples.

Encode
roots := []cid.Cid{ /* ... */ }
blocks := []automobile.Block{ /* ... */ }

reader, err := automobile.Encode(roots, blocks)

io.ReadAll(reader)
Streaming encode
var b bytes.Buffer
writer := NewWriter(&b)

// If you know the root(s), write them first.
//
// If you do not know the root(s), you can omit this call and the writer will
// write a header with an empty roots automatically, on the first call to
// WriteBlock.
err := writer.WriteHeader([]cid.Cid{ /* ... */ })

// repeat until all blocks written
err := writer.WriteBlock(automobile.Block{ /* ... */ })
Decode
f, err := os.Open("./path/to/my.car")

roots, blocks, err := automobile.Decode(f)

fmt.Printf("Roots: %v\n", roots)
for _, block := range blocks {
  fmt.Printf("Block: %s (%d bytes)\n", block.Link, len(block.Data))
}
Streaming decode
f, err := os.Open("./testdata/fixtures/comic.car")

reader, err := automobile.NewReader(f)

fmt.Printf("Roots: %v\n", reader.Header.Roots)

for {
  block, err := reader.Read()
  if err != nil {
    if err == io.EOF {
      break
    }
    panic(err)
  }
  fmt.Printf("Block: %s (%d bytes)\n", block.Link, len(block.Data))
}

Contributing

Feel free to join in. All welcome. Please open an issue!

License

Dual-licensed under MIT OR Apache 2.0

Documentation

Index

Constants

View Source
const Code = 0x0202

Code is the multicodec code for CAR files. See https://github.com/multiformats/multicodec/blob/45c88b89ab909c0fac7c86dafe43ad72d1e8e8a9/table.csv#L143

View Source
const ContentType = "application/vnd.ipld.car"

ContentType is the value the HTTP Content-Type header should have for CARs. See https://www.iana.org/assignments/media-types/application/vnd.ipld.car

Variables

View Source
var MaxAllowedSectionSize uint = 32 << 20 // 32MiB

MaxAllowedSectionSize dictates the maximum number of bytes that a CARv1 header or block is allowed to occupy without causing a decode to error.

Functions

func Encode

func Encode(roots []cid.Cid, blocks []Block) io.ReadCloser

Encode takes a list of root CIDs and blocks, and produces an io.ReadCloser that emits the bytes of a CAR file containing those roots and blocks. The caller should call Close on the returned ReadCloser when finished to free up resources.

func HeaderSize

func HeaderSize(h datamodel.HeaderModel) (uint64, error)

func Index

func Index(reader io.Reader, opts ...IndexerOption) (HeaderIndexEntry, []BlockIndexEntry, error)

Index reads through a CAR file and returns the byte offsets of each block along with its CID, without loading the block data into memory.

func LdRead

func LdRead(r *bufio.Reader) ([]byte, error)

func LdSize

func LdSize(d ...[]byte) uint64

func LdWrite

func LdWrite(w io.Writer, d ...[]byte) error

func ReadBlock

func ReadBlock(br *bufio.Reader) (cid.Cid, []byte, error)

func ReadHeader

func ReadHeader(br *bufio.Reader) (datamodel.HeaderModel, error)

func WriteBlock

func WriteBlock(w io.Writer, block Block) error

func WriteHeader

func WriteHeader(w io.Writer, roots []cid.Cid) error

Types

type Block

type Block struct {
	Link cid.Cid
	Data []byte
}

func Decode

func Decode(reader io.Reader, opts ...ReaderOption) ([]cid.Cid, []Block, error)

Decode takes an io.Reader containing the bytes of a CAR file and returns the list of root CIDs along with all the blocks in the CAR.

type BlockIndexEntry

type BlockIndexEntry struct {
	Link  cid.Cid
	Start uint64
	End   uint64
}

type BlockIndexer

type BlockIndexer struct {
	Reader              *bufio.Reader
	Offset              uint64
	SkipIntegrityChecks bool
}

BlockIndexer is a helper for iterating through blocks in a CAR file while keeping track of their byte offsets for indexing purposes.

func (*BlockIndexer) Read

func (bi *BlockIndexer) Read() (BlockIndexEntry, error)

type BlockReader

type BlockReader struct {
	Reader              *bufio.Reader
	SkipIntegrityChecks bool
}

BlockReader is a helper for iterating through blocks in a CAR file.

func (*BlockReader) Read

func (r *BlockReader) Read() (Block, error)

type HeaderIndexEntry

type HeaderIndexEntry struct {
	Roots []cid.Cid
	Size  uint64
}

type Indexer

type Indexer struct {
	Header HeaderIndexEntry
	// contains filtered or unexported fields
}

func NewIndexer

func NewIndexer(r io.Reader, opts ...IndexerOption) (*Indexer, error)

func (*Indexer) Read

func (r *Indexer) Read() (BlockIndexEntry, error)

type IndexerOption

type IndexerOption func(*indexerCfg)

func WithIndexerSkipIntegrityChecks

func WithIndexerSkipIntegrityChecks() IndexerOption

WithIndexSkipIntegrityChecks configures the indexer to skip integrity checks when reading blocks. This can be used to speed up indexing when the integrity of the blocks is not a concern.

type Reader

type Reader struct {
	Header datamodel.HeaderModel
	// contains filtered or unexported fields
}

func NewReader

func NewReader(r io.Reader, opts ...ReaderOption) (*Reader, error)

func (*Reader) Read

func (r *Reader) Read() (Block, error)

type ReaderOption

type ReaderOption func(*readerCfg)

func WithSkipIntegrityChecks

func WithSkipIntegrityChecks() ReaderOption

WithSkipIntegrityChecks configures the reader to skip integrity checks when reading blocks. This can be used to speed up decoding when the integrity of the blocks is not a concern.

type Writer

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

func NewWriter

func NewWriter(w io.Writer) *Writer

func (*Writer) WriteBlock

func (e *Writer) WriteBlock(block Block) error

func (*Writer) WriteHeader

func (e *Writer) WriteHeader(roots []cid.Cid) error

Directories

Path Synopsis
gen command

Jump to

Keyboard shortcuts

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