journal

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package journal reads systemd journal files (the format of systemd's JOURNAL_FILE_FORMAT.md): regular and compact layouts, XZ, LZ4 and ZSTD compressed data objects, keyed or Jenkins hashes.

It reads through an io.ReaderAt and never writes. Every offset is checked against the size the file had when opened; every decompressed DATA payload is capped (MaxData) before it is allocated, and a field's name is read before its value is decompressed, so a field the caller does not keep is never decompressed whole. What it returns is raw journal content.

Index

Constants

View Source
const (

	// MaxData is the largest decompressed DATA payload (name=value) the
	// reader returns; larger fields are reported as dropped.
	MaxData = 1 << 20

	// MaxEntryData bounds the decompressed bytes kept for one entry: the
	// fields past it are reported as dropped.
	MaxEntryData = 2 * MaxData

	// MaxNameBytes is the longest field name journald accepts.
	MaxNameBytes = 64
)
View Source
const MaxXZDict uint64 = MaxData + 64<<10

MaxXZDict is the largest LZMA2 dictionary an XZ payload may declare. The decoder allocates the declared dictionary, so a crafted header could otherwise make it allocate up to 4 GiB; payloads are at most MaxData, so a larger dictionary is never needed (journald's own is 1 MiB).

Variables

View Source
var (
	ErrNotJournal  = errors.New("journal: not a journal file")
	ErrUnsupported = errors.New("journal: unsupported journal file features")
	ErrCorrupt     = errors.New("journal: corrupt journal file")
)

Errors. Their text never holds journal content.

Functions

This section is empty.

Types

type ArrayPos added in v0.2.0

type ArrayPos struct {
	Offset, Index uint64
}

ArrayPos is a position in the entry-array chain: the offset of an entry-array object and the index of its first entry. The zero value is the head of the chain.

type Entry

type Entry struct {
	EntryHead
	Offset uint64
	// Fields are the kept fields in the file's item order; a name may
	// repeat.
	Fields []Field
	// Dropped counts fields left out: kept fields over MaxData or past the
	// entry's budgets, and compressed fields left undecoded (their name
	// unread) once the decode budget is spent.
	Dropped int
	// Decoded is the decompression work the entry cost, in bytes (names
	// and values decoded, not served from the caches): the bytes a
	// source reports as processed besides the entry's own size.
	Decoded int64
}

Entry is one entry with the fields its reader kept.

type EntryHead

type EntryHead struct {
	Seqnum    uint64
	Realtime  uint64 // µs since the epoch
	Monotonic uint64
	BootID    [16]byte
	XorHash   uint64
	Size      uint64 // object size
}

EntryHead is an entry's fixed part: no field is read.

type Field

type Field struct {
	Name  string
	Value []byte
}

Field is one name=value pair. Value may be binary; callers must not modify it.

type File

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

File is an open journal file. It is not safe for concurrent use.

func Open

func Open(r io.ReaderAt, size int64) (*File, error)

Open reads and validates the header; size is the file size at open.

func (*File) Close

func (f *File) Close()

Close releases the decoders (not the reader).

func (*File) Entry

func (f *File) Entry(off uint64, keep func(name string) bool) (*Entry, error)

Entry reads the entry at off. keep is called with each field's name (before its value is decompressed); only fields it accepts are read. On an error after the entry's header, the partial entry (its Size and the work Decoded so far) is returned with the error.

func (*File) EntryOffsets

func (f *File) EntryOffsets(fn func(i uint64, off uint64) bool) error

EntryOffsets walks the entry-array chain from the header and calls fn with each entry's index and object offset, in file order (seqnum order), until fn returns false.

func (*File) EntryOffsetsFrom added in v0.2.0

func (f *File) EntryOffsetsFrom(p ArrayPos, fn func(i uint64, off uint64, at ArrayPos) bool) error

EntryOffsetsFrom walks the chain as EntryOffsets does, from the array at p (the zero ArrayPos: the header's first), and also gives fn the position of the array holding each entry: a later walk may resume there (a position read from an earlier walk of the same, unchanged file) without reading the arrays before it.

func (*File) Head

func (f *File) Head(off uint64) (EntryHead, error)

Head reads the fixed part of the entry at off.

func (*File) Header

func (f *File) Header() Header

Header returns the file header.

func (*File) Realtime

func (f *File) Realtime(off uint64) (uint64, error)

Realtime reads only the realtime (µs) of the entry at off: one small read.

type Header struct {
	Compatible, Incompatible uint32
	State                    uint8
	FileID, MachineID        [16]byte
	SeqnumID                 [16]byte
	HeaderSize               uint64
	NEntries                 uint64
	EntryArrayOffset         uint64
	HeadRealtime             uint64
	TailRealtime             uint64
	TailObjectOffset         uint64
}

Header is the part of the file header the reader uses.

Directories

Path Synopsis
Package journaltest builds small journal files in memory for tests and fuzz seeds: header, DATA, ENTRY and chained ENTRY_ARRAY objects, regular or compact, plain or ZSTD/LZ4/XZ compressed (no hash tables: the reader does not use them).
Package journaltest builds small journal files in memory for tests and fuzz seeds: header, DATA, ENTRY and chained ENTRY_ARRAY objects, regular or compact, plain or ZSTD/LZ4/XZ compressed (no hash tables: the reader does not use them).

Jump to

Keyboard shortcuts

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