diff

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 5 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package diff implements differencing (child) disk resolution over a parent chain.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoBitmapSource = errors.New("libvhdi: sector bitmap resolution requires the child's backing reader")

ErrNoBitmapSource is returned when a block can only be resolved by consulting a sector bitmap but the resolver was built without access to the child's backing file. Resolvers created through the deprecated NewVHDResolver and NewVHDXResolver constructors are in this state.

View Source
var ErrParentRequired = errors.New("libvhdi: differencing disk requires a parent")

ErrParentRequired is returned when a read resolves to a parent disk but no parent has been attached. Differencing disks fail closed: returning zeroes would be indistinguishable from genuine disk contents.

Functions

This section is empty.

Types

type BlockKind

type BlockKind int

BlockKind describes where a block's bytes live.

const (
	// BlockFromChild: the whole block is present in this disk.
	BlockFromChild BlockKind = iota

	// BlockFromParent: the whole block must be read from the parent chain.
	BlockFromParent

	// BlockZero: the block reads as zeroes and is not parent-backed.
	BlockZero

	// BlockPerSector: the block's sector bitmap decides sector by sector, so
	// SectorRuns is needed to resolve it.
	BlockPerSector
)

type Chain

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

Chain represents an ordered slice of disk readers forming a parent chain, from child (index 0) to oldest ancestor (last index).

func NewChain

func NewChain(child io.ReaderAt) *Chain

NewChain creates a new chain with the given child reader.

func (*Chain) AddParent

func (c *Chain) AddParent(parent io.ReaderAt) error

AddParent appends a parent reader to the end of the chain.

func (*Chain) Depth

func (c *Chain) Depth() int

Depth returns the number of disks in the chain (child + all parents).

func (*Chain) Reader

func (c *Chain) Reader(index int) (io.ReaderAt, error)

Reader returns the reader at the given position in the chain (0 = child).

type Config

type Config struct {
	// Source is the child's raw backing reader. It is required to read sector
	// bitmaps; without it, blocks needing per-sector resolution error out.
	Source io.ReaderAt

	// Child is the decoded child block reader (virtual address space).
	Child io.ReaderAt

	// Parent is the next disk in the chain, or nil if none is attached yet.
	// A nil Parent makes parent-backed reads return ErrParentRequired.
	Parent io.ReaderAt

	BlockSize   uint32
	SectorSize  uint32
	VirtualSize uint64

	// Exactly one of VHDBAT / VHDXBAT must be set.
	VHDBAT  *types.VHDBlockAllocationTable
	VHDXBAT *types.VHDXBlockAllocationTable
}

Config describes a differencing disk and its resolution inputs.

type Resolver

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

Resolver resolves reads on a child (differencing) disk against a parent chain, at sector granularity.

Both VHD and VHDX represent partially-written blocks with a sector bitmap: sectors whose bit is set live in the child, and sectors whose bit is clear must be served from the parent. Resolving at whole-block granularity returns the child's zero-filled holes instead of the parent's data.

func BuildVHDResolver

func BuildVHDResolver(links []io.ReaderAt, bats []*types.VHDBlockAllocationTable, blockSize uint32, virtualSize uint64) (*Resolver, error)

BuildVHDResolver constructs a layered Resolver for VHD differencing disk chains. links[i] is the raw backing reader for disk i and bats[i] its BAT; blockSize and virtualSize come from the child's metadata.

Each layer resolves at sector granularity: sectors absent from a child's block bitmap are served by the next disk in the chain.

func New

func New(cfg Config) (*Resolver, error)

New creates a differencing disk resolver.

func NewVHDResolver deprecated

func NewVHDResolver(child io.ReaderAt, parent io.ReaderAt, childBAT *types.VHDBlockAllocationTable, blockSize uint32, virtualSize uint64) *Resolver

NewVHDResolver creates a differencing disk resolver for VHD format.

Deprecated: this constructor cannot resolve partially-written blocks because it has no access to the child's backing reader, and so cannot read sector bitmaps. Such blocks return ErrNoBitmapSource rather than silently dropping parent data. Use New with a populated Config.Source instead.

func NewVHDXResolver deprecated

func NewVHDXResolver(child io.ReaderAt, parent io.ReaderAt, childBAT *types.VHDXBlockAllocationTable, blockSize uint32, virtualSize uint64) *Resolver

NewVHDXResolver creates a differencing disk resolver for VHDX format.

Deprecated: this constructor cannot resolve partially-present blocks because it has no access to the child's backing reader, and so cannot read sector bitmaps. Such blocks return ErrNoBitmapSource rather than silently dropping parent data. Use New with a populated Config.Source instead.

func (*Resolver) BlockCount

func (r *Resolver) BlockCount() uint32

BlockCount returns the number of blocks the child's BAT describes.

func (*Resolver) BlockKind

func (r *Resolver) BlockKind(blockIndex uint32) (BlockKind, error)

BlockKind reports where the given block's bytes live, without reading them.

func (*Resolver) HasParent

func (r *Resolver) HasParent() bool

HasParent reports whether this resolver has a parent disk attached.

func (*Resolver) ReadAt

func (r *Resolver) ReadAt(p []byte, virtualOffset int64) (int, error)

ReadAt implements io.ReaderAt over the child's virtual address space, resolving each region to the child, the parent chain, or zeroes.

func (*Resolver) SectorRuns

func (r *Resolver) SectorRuns(blockIndex uint32, firstSector, lastSector int64) ([]SectorRun, error)

SectorRuns groups the sectors in [firstSector, lastSector] of a BlockPerSector block into runs sharing a backing.

The bitmap bytes covering the range are read once, so this costs one I/O regardless of how many sectors the range spans.

func (*Resolver) SetParent

func (r *Resolver) SetParent(parent io.ReaderAt)

SetParent attaches or replaces the parent disk.

func (*Resolver) VirtualSize

func (r *Resolver) VirtualSize() int64

VirtualSize returns the total size of the differencing disk in bytes.

type SectorRun

type SectorRun struct {
	First, Last int64

	// InChild is true when the run is present in this disk, false when it must
	// come from the parent chain.
	InChild bool
}

SectorRun is a contiguous run of sectors within one block that share a backing. Sector indices are inclusive and relative to the block.

Jump to

Keyboard shortcuts

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