diskimagekit

package
v4.0.0-...-0ec4d9e Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 14 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 diskimagekit reads and writes RAW and ASIF disk images and layered images. Its image, overlay, and cache concepts follow Apple's DiskImageKit. It does not call the Apple framework or create native DiskImage objects.

Images hold advisory file locks until Close. All methods on an image may be called concurrently. Callers must close images when finished. No background goroutines or finalizers manage their lifetime.

ASIF support covers version 1 images with 512 or 4096 byte blocks and chunks up to 64 MiB. New images use 1 MiB chunks. Writes copy changed data and tables, reusing storage that neither directory generation references. This limits growth during repeated overwrites but does not shrink the physical file. Sync reports storage errors but does not promise atomic recovery from power loss or torn sector writes.

See https://developer.apple.com/documentation/diskimagekit.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrReadOnly means the requested operation requires writable storage.
	ErrReadOnly = errors.New("diskimagekit: image is read-only")
	// ErrInvalidImage means the image has malformed or unsupported ASIF data.
	ErrInvalidImage = errors.New("diskimagekit: invalid or unsupported image")
	// ErrIncompatibleStack means layers cannot form a valid stack.
	ErrIncompatibleStack = errors.New("diskimagekit: incompatible stack")
)

Functions

This section is empty.

Types

type BlockSize

type BlockSize uint16

BlockSize is the number of bytes in a logical block.

See https://developer.apple.com/documentation/diskimagekit/diskimage/blocksize-swift.enum.

const (
	// BlockSize512 selects 512 byte blocks.
	BlockSize512 BlockSize = 512
	// BlockSize4096 selects 4096 byte blocks.
	BlockSize4096 BlockSize = 4096
)

type CreateOption

type CreateOption func(*createOptions) error

CreateOption configures CreateASIF.

func WithBlockSize

func WithBlockSize(block BlockSize) CreateOption

WithBlockSize sets the logical block size. The default is BlockSize512.

type DiskImage

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

DiskImage owns a base file and its layers, ordered from base to top. Use Open, CreateRAW, or CreateASIF to construct one. A zero DiskImage is closed. ReadAt and WriteAt use byte offsets; creation and Truncate use block counts. A DiskImage must not be copied after first use.

See https://developer.apple.com/documentation/diskimagekit/diskimage.

Example

This example copies a stack without cache layers into an independent ASIF image. The source files remain read-only. It copies every logical byte, so large sparse images can take substantial time and storage.

package main

import (
	"io"

	"github.com/Code-Hex/vz/v4/diskimagekit"
)

func main() {
	source, err := diskimagekit.Open("base.asif", diskimagekit.WithOpenMode(diskimagekit.ReadOnly))
	if err != nil {
		panic(err)
	}
	defer source.Close()

	if err := source.Append("overlay.asif", diskimagekit.WithOpenMode(diskimagekit.ReadOnly)); err != nil {
		panic(err)
	}

	// Preserve the stack's block size, including when its layers differ.
	destination, err := diskimagekit.CreateASIF(
		"standalone.asif",
		source.BlockCount(),
		diskimagekit.WithBlockSize(source.BlockSize()),
	)
	if err != nil {
		panic(err)
	}
	defer destination.Close()

	reader := io.NewSectionReader(source, 0, source.Size())
	writer := io.NewOffsetWriter(destination, 0)
	buffer := make([]byte, 1<<20)
	n, err := io.CopyBuffer(writer, reader, buffer)
	if err != nil {
		panic(err)
	}
	if n != source.Size() {
		panic(io.ErrUnexpectedEOF)
	}
	if err := destination.Sync(); err != nil {
		panic(err)
	}

	// Release the destination's lock before opening it through a native disk
	// attachment, such as vz.NewDiskImageStorageDeviceAttachment.
	if err := destination.Close(); err != nil {
		panic(err)
	}
}

func CreateASIF

func CreateASIF(path string, blockCount int64, options ...CreateOption) (*DiskImage, error)

CreateASIF creates a writable ASIF v1 image. blockCount is in logical blocks, not bytes. WithBlockSize defaults to BlockSize512. It replaces an existing file or symbolic link without modifying its old inode or link target. A dangling symbolic link is kept and its target is created instead. Failed creation leaves an existing file unchanged.

func CreateRAW

func CreateRAW(path string, blockCount int64) (*DiskImage, error)

CreateRAW creates a writable RAW image with 512 byte blocks. It replaces an existing file or symbolic link without modifying its old inode or link target. A dangling symbolic link is kept and its target is created instead. Failed creation leaves an existing file unchanged.

func Open

func Open(path string, options ...OpenOption) (*DiskImage, error)

Open opens a RAW or ASIF image. The default mode is Automatic. It holds a nonblocking advisory lock until Close. Format detection follows DiskImageKit: unrecognized ASIF format flags, segment counts, or metadata flags cause RAW detection. Invalid headers return ErrInvalidImage. Directory and data layout checks are deferred until an operation needs their mappings.

See https://developer.apple.com/documentation/diskimagekit/diskimage.

func (*DiskImage) Append

func (d *DiskImage) Append(path string, options ...OpenOption) error

Append opens an ASIF layer and adds it above the current top. Failure leaves the existing stack usable. Parent UUIDs must match the nearest non-cache layer when that layer has a UUID. A standalone cache or an image with a parent UUID cannot serve as the base of a stack.

See https://developer.apple.com/documentation/diskimagekit/diskimage.

func (*DiskImage) BlockCount

func (d *DiskImage) BlockCount() int64

BlockCount returns the logical block count, including after Close.

See https://developer.apple.com/documentation/diskimagekit/diskimage/blockcount.

func (*DiskImage) BlockSize

func (d *DiskImage) BlockSize() BlockSize

BlockSize returns the top layer's logical block size.

See https://developer.apple.com/documentation/diskimagekit/diskimage/blocksize-swift.property.

func (*DiskImage) Close

func (d *DiskImage) Close() error

Close closes every owned file and releases its advisory lock. It waits for concurrent operations on this image to finish. Repeated calls return nil. Close does not replace an explicit Sync and does not report successful flushes.

func (*DiskImage) CreateLayer

func (d *DiskImage) CreateLayer(path string, kind LayerType, options ...LayerOption) error

CreateLayer creates an ASIF overlay or cache above the current image. It replaces an existing file or symbolic link. On failure the previous stack and an existing file at path remain unchanged. The layer inherits its parent's block size. The destination must not refer to a file already in the stack, including through a hard link or symbolic link. A stack may contain one cache.

See https://developer.apple.com/documentation/diskimagekit/diskimage.

func (*DiskImage) Format

func (d *DiskImage) Format() Format

Format returns Stack for layered images, or the standalone file format.

See https://developer.apple.com/documentation/diskimagekit/diskimage/format-swift.property.

func (*DiskImage) LayerUUID

func (d *DiskImage) LayerUUID() string

LayerUUID returns the top layer's UUID, or an empty string when none is set. An ASIF layer gets a new UUID before its first write through a writable open. Cache UUIDs do not change during filling.

See https://developer.apple.com/documentation/diskimagekit/diskimage/layeruuid.

func (*DiskImage) Layers

func (d *DiskImage) Layers() []LayerInfo

Layers returns independent snapshots ordered from base to top.

func (*DiskImage) OpenMode

func (d *DiskImage) OpenMode() OpenMode

OpenMode returns the image's access mode. A stack with a cache on top is read-only even when the cache file is writable for filling. A standalone image reports its file access mode, as does each entry in Layers. ASIF header restrictions can also prevent writes after a ReadWrite open.

See https://developer.apple.com/documentation/diskimagekit/diskimage/openmode.

func (*DiskImage) ParentUUID

func (d *DiskImage) ParentUUID() string

ParentUUID returns the top layer's required parent UUID, if any.

See https://developer.apple.com/documentation/diskimagekit/diskimage/parentuuid.

func (*DiskImage) ReadAt

func (d *DiskImage) ReadAt(p []byte, off int64) (int, error)

ReadAt reads logical bytes from the stack. Unallocated data inherits from lower layers; explicit zero mappings hide lower data. A cache miss fills a whole chunk in writable cache layers. A miss in a read-only cache returns ErrReadOnly. Cache write errors are returned. Inherited data in a detached cache or an ASIF image with a parent UUID requires its parent and returns ErrIncompatibleStack. An overlay on a RAW parent cannot be distinguished from a standalone base by its header alone.

func (*DiskImage) Size

func (d *DiskImage) Size() int64

Size returns the logical size in bytes, including after Close.

func (*DiskImage) Sync

func (d *DiskImage) Sync() error

Sync flushes every writable file. An earlier ASIF mutation failure is returned rather than retried. Sync does not make the stack a power-fail transaction.

func (*DiskImage) Truncate

func (d *DiskImage) Truncate(blockCount int64) error

Truncate changes the logical size in blocks, not bytes. An overlay expanding into an unallocated range inherits its parent. ASIF shrinking visits existing mapping tables only. Whole discarded chunks become zero; an inherited partial chunk and absent tables keep inheriting when regrown. If a partial chunk has no owned data left, the whole chunk takes its first block's mapping state. Cache layers cannot be resized. An unchanged size succeeds even when the image is read-only.

See https://developer.apple.com/documentation/diskimagekit/diskimage/truncate(blockcount:).

func (*DiskImage) WriteAt

func (d *DiskImage) WriteAt(p []byte, off int64) (int, error)

WriteAt writes logical bytes into the top image without changing lower layers. A write cannot extend the logical size. Writes crossing the end return the number written and io.ErrShortWrite. A cache cannot be the writable top. ASIF metadata failures disable further mutations on this handle; close and reopen it before deciding whether to retry. Reads remain available.

type Format

type Format uint8

Format identifies an image's storage format.

See https://developer.apple.com/documentation/diskimagekit/diskimage/format-swift.enum.

const (
	// RAW is an unstructured image with 512 byte blocks.
	RAW Format = iota + 1
	// ASIF is an Apple Sparse Image Format image.
	ASIF
	// Stack is a base image with one or more ASIF layers.
	Stack
)

type LayerInfo

type LayerInfo struct {
	// Path is the path used to open the file.
	Path string
	// Format is RAW or ASIF.
	Format Format
	// BlockCount is the logical size in blocks.
	BlockCount int64
	// BlockSize is the number of bytes in a logical block.
	//
	// See https://developer.apple.com/documentation/diskimagekit/diskimage/blocksize-swift.enum.
	BlockSize BlockSize
	// OpenMode is the access mode actually obtained.
	OpenMode OpenMode
	// LayerType is Overlay, Cache, or zero for a standalone image or stack base.
	LayerType LayerType
	// LayerUUID identifies this layer. It is empty when the image has no UUID.
	LayerUUID string
	// ParentUUID identifies the required parent. It is empty for RAW parents.
	ParentUUID string
}

LayerInfo is a snapshot of a file owned by a DiskImage.

type LayerOption

type LayerOption func(*layerOptions) error

LayerOption configures CreateLayer.

func WithBlockCount

func WithBlockCount(count int64) LayerOption

WithBlockCount sets an overlay's size in blocks of its parent's block size. Without it, the overlay has the same logical size as its parent. Cache layers always have their parent's size and reject this option.

type LayerType

type LayerType uint8

LayerType is the role of a layer in a stack. Zero means a standalone image or the base of a stack.

See https://developer.apple.com/documentation/diskimagekit/diskimage/layertype-swift.struct.

const (
	// Overlay stores changes without modifying its parent.
	Overlay LayerType = iota + 1
	// Cache stores copies of data read from its parent. A stack may have one cache.
	Cache
)

type OpenMode

type OpenMode uint8

OpenMode controls file access. The default is Automatic.

See https://developer.apple.com/documentation/diskimagekit/openconfiguration/mode-swift.enum.

const (
	// ReadOnly opens an image for reading and holds a shared advisory lock.
	ReadOnly OpenMode = iota
	// ReadWrite opens an image for writing and holds an exclusive advisory lock.
	ReadWrite
	// Automatic tries ReadWrite, falling back to ReadOnly only for permission
	// errors. Lock contention never causes a fallback.
	Automatic
)

type OpenOption

type OpenOption func(*openOptions) error

OpenOption configures Open or Append.

func WithOpenMode

func WithOpenMode(mode OpenMode) OpenOption

WithOpenMode sets file access for Open or Append.

Jump to

Keyboard shortcuts

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