geektutu/7days-golang · error

invalid magic number

Error message

invalid magic number

What it means

Sentinel validation failure in meta.validate(): the meta page read from disk does not carry the gee-bolt magic constant 0xED0CDAED, so the file is not a gee-bolt database (or is corrupted/truncated). It fires for any DB open whose first-page magic field differs from the expected marker.

Source

Thrown at gee-bolt/day3-tree/meta.go:27

// Represent a marker value to indicate that a file is a gee-bolt DB
const magic uint32 = 0xED0CDAED

type meta struct {
	magic    uint32
	pageSize uint32
	pgid     uint64
	checksum uint64
}

func (m *meta) sum64() uint64 {
	var h = fnv.New64a()
	_, _ = h.Write((*[unsafe.Offsetof(meta{}.checksum)]byte)(unsafe.Pointer(m))[:])
	return h.Sum64()
}

func (m *meta) validate() error {
	if m.magic != magic {
		return errors.New("invalid magic number")
	}
	if m.checksum != m.sum64() {
		return errors.New("invalid checksum")
	}
	return nil
}

View on GitHub (pinned to cf36443821)

Solutions

  1. Initialize the file through the library (create + write meta with magic) before opening/validating
  2. Confirm the path resolves to a bolt file created by the same codebase version
  3. Remove the stale/corrupt file and let the library create a fresh one
  4. Hex-dump the first bytes to compare against the magic constant

Example fix

// before
f, _ := os.Create("data.db")
f.Close() // empty file, no magic written
m.validate() // -> invalid magic number
// after
db, _ := bolt.Open("data.db", os.O_CREATE|os.O_RDWR, 0600) // meta written with magic
Defensive patterns

Strategy: validation

Validate before calling

func ensureInitialized(path string) error {
    if fi, err := os.Stat(path); err != nil || fi.Size() == 0 {
        return bolt.Open(path, os.O_CREATE|os.O_RDWR, 0600) // writes magic
    }
    return nil
}

Type guard

func isValidBoltMeta(m *meta) bool { return m != nil && m.magic == magic }

Prevention

When it happens

Trigger: Opening a nonexistent-format file, an empty file created with O_CREATE but never initialized with magic, or reading at the wrong page offset.

Common situations: Reusing a day1 file path with a later schema/version, tests pointing at a temp file that was never initialized, mistyped file path resolving to another format.

Related errors


AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03). Data as JSON: /api/errors/9bf8f8c9be812131. Report an issue: GitHub.