mod

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: Apache-2.0, MIT Imports: 18 Imported by: 3

Documentation

Overview

Package mod provides DAG modification utilities for UnixFS files.

This package is designed for MFS (Mutable File System) operations where files need to be modified in place: writing at offsets, appending data, truncating, and seeking within files.

Metadata Handling (Mode and ModTime)

UnixFS supports optional Mode (unix permissions) and ModTime (modification time) metadata. Most use cases do not set these fields, and files without metadata work normally.

When metadata IS present, DagModifier follows Unix filesystem semantics:

  • ModTime is automatically updated to time.Now() when content is modified or truncated, matching how Unix filesystems behave.
  • Mode is preserved during modifications.

If you need a specific mtime after modification (e.g., for archival or reproducible builds), explicitly set it on the FSNode after the operation.

Identity CID Handling

This package automatically handles identity CIDs (multihash code 0x00) which inline data directly in the CID. When modifying nodes with identity CIDs, the package ensures the verifcid.DefaultMaxIdentityDigestSize limit is respected by automatically switching to a cryptographic hash function when the encoded data would exceed this limit. The replacement hash function is chosen from (in order): the configured Prefix if non-identity, or util.DefaultIpfsHash as a fallback.

RawNode Growth

When appending data to a RawNode that would require multiple blocks, the node is automatically converted to a UnixFS file structure. This is necessary because RawNodes cannot have child nodes. The original raw data remains accessible via its original CID, while the new structure provides full UnixFS capabilities.

Raw Leaf Collapsing

When RawLeaves is enabled and a file fits in a single block with no metadata (no Mode or ModTime), GetNode returns a RawNode directly rather than a ProtoNode wrapper. This ensures CID compatibility with `ipfs add --raw-leaves`. Files with metadata always return ProtoNode to preserve the metadata.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSeekFail           = errors.New("failed to seek properly")
	ErrUnrecognizedWhence = errors.New("unrecognized whence")
	ErrNotUnixfs          = errors.New("dagmodifier only supports unixfs nodes (proto or raw)")
)

Common errors

Functions

This section is empty.

Types

type DagModifier

type DagModifier struct {
	Prefix    cid.Prefix
	RawLeaves bool
	MaxLinks  int
	// contains filtered or unexported fields
}

DagModifier is the only struct licensed and able to correctly perform surgery on a DAG 'file' Dear god, please rename this to something more pleasant

func NewDagModifier

func NewDagModifier(ctx context.Context, from ipld.Node, serv ipld.DAGService, spl chunker.SplitterGen) (*DagModifier, error)

NewDagModifier returns a new DagModifier, the Cid prefix for newly created nodes will be inherited from the passed in node. If the Cid version is not 0 raw leaves will also be enabled. The Prefix and RawLeaves options can be overridden by changing them after the call.

func (*DagModifier) CtxReadFull

func (dm *DagModifier) CtxReadFull(ctx context.Context, b []byte) (int, error)

CtxReadFull reads data from this dag starting at the current offset

func (*DagModifier) GetNode

func (dm *DagModifier) GetNode() (ipld.Node, error)

GetNode gets the modified DAG Node

func (*DagModifier) HasChanges

func (dm *DagModifier) HasChanges() bool

HasChanges returned whether or not there are unflushed changes to this dag

func (*DagModifier) Read

func (dm *DagModifier) Read(b []byte) (int, error)

Read data from this dag starting at the current offset

func (*DagModifier) Seek

func (dm *DagModifier) Seek(offset int64, whence int) (int64, error)

Seek modifies the offset according to whence. See unixfs/io for valid whence values.

func (*DagModifier) Size

func (dm *DagModifier) Size() (int64, error)

Size returns the Filesize of the node

func (*DagModifier) Sync

func (dm *DagModifier) Sync() error

Sync writes changes to this dag to disk

func (*DagModifier) Truncate

func (dm *DagModifier) Truncate(size int64) error

Truncate truncates the current Node to 'size' and replaces it with the new one.

func (*DagModifier) Write

func (dm *DagModifier) Write(b []byte) (int, error)

Write continues writing to the dag at the current offset

func (*DagModifier) WriteAt

func (dm *DagModifier) WriteAt(b []byte, offset int64) (int, error)

WriteAt will modify a dag file in place

Jump to

Keyboard shortcuts

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