hfsplus

package module
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: BSD-3-Clause Imports: 16 Imported by: 6 modules, 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.

README

go-filesystems/hfsplus

hfsplus

Go Reference CI

Pure-Go, CGO-free read/write driver for the HFS+ (Mac OS Extended) on-disk format and its HFSX (case-sensitive) variant — part of the go-filesystems family.

HFS+ is the filesystem Apple shipped on Macs from Mac OS 8.1 through the move to APFS. It stores all metadata big-endian: a volume header at byte offset 1024, a catalog B-tree keyed by (parent CNID, UTF-16 name) for the directory hierarchy, an extents-overflow B-tree for fragmented files, and per-fork allocation-block extents for file contents. This driver decodes and writes those structures and exposes the volume through the shared interface Filesystem API.

Both paths are validated against the native macOS tooling. The pure-Go formatter (Format / Mkfs) and write path produce images that pass fsck_hfs -n clean and that macOS mounts read/write, reading back the exact files and bytes the Go side wrote — verified in both directions (Go-formatted → macOS-read, and hdiutil-created → Go-written → macOS-read). A small (≈7 KB gzipped) raw fixture is committed so the cross-arch round-trip runs everywhere — including the big-endian s390x CI job, the endianness correctness test.

Support summary

Feature Status Notes
Open / OpenFile / Close ✅ Read-only; volume header at offset 1024; H+ (HFS+) and HX (HFSX) signatures
OpenWritable / OpenFileWritable / Sync ✅ Read/write in place: no copy of the image; each mutation writes only the sectors it changed (see "Cost of a write")
ListDir / Stat / ReadFile / ReadLink ✅ Catalog walk; BSD perms; data fork via 8 inline extents + extents-overflow; S_IFLNK targets
Format / Mkfs (pure Go) ✅ Lays down a valid empty HFS+/HFSX volume on every arch; fsck_hfs -n clean + macOS mounts RW
WriteFile ✅ Allocates blocks, writes the data fork, inserts catalog file + thread records
MkDir / DeleteFile / DeleteDir / Rename ✅ Catalog insert/delete with B-tree node splitting + tree-height growth; valence/freeBlocks kept in sync
SetLabel / Symlink / Truncate ✅ Optional Labeller / Symlinker / Truncater capabilities
Case-insensitive names ✅ Apple FastUnicodeCompare over the practical character set incl. ignorable-NUL (documented subset of the full fold table)
Case-sensitive (HFSX) ✅ Binary UTF-16 key comparison; HasFolderCount maintained
FormatAppleDmg (alt) ⚠️ Optional darwin-only escape hatch that shells to hdiutil; off-darwin returns ErrUnsupported. The primary Format is pure-Go.

Status

Implemented and validated against real macOS images (read + write):

  • Volume header decode/encode (H+ / HX, primary + alternate, block counts, special-file fork descriptors, clean-unmount attributes).
  • Generic HFS+ B-tree node read/write (descriptor + trailing record-offset table) shared by the catalog and extents-overflow trees.
  • Catalog & extents-overflow B-trees: index descent, leaf scan, insert/delete with leaf and index node splitting, index-record propagation, new-root growth, node-underflow rebalancing (rotate/merge), emptied-node freeing, and tree-height shrink; folder/file/thread records; valence and HasFolderCount maintenance.
  • Growable B-tree files — when a tree exhausts its node reservation it allocates more blocks, extends its backing fork's extents (inline, then spilling into the extents-overflow tree), grows the node-allocation bitmap (adding map nodes), and updates the header-node totalNodes/freeNodes and the volume-header special-file fork descriptor + freeBlocks.
  • Allocation bitmap allocate/free of contiguous runs and multi-fragment allocations with freeBlocks sync.
  • Pure-Go Format/Mkfs; WriteFile/MkDir/DeleteFile/DeleteDir/Rename; SetLabel/Symlink/Truncate.
  • File read/write: data-fork inline extents plus extents-overflow continuation for both reading and writing fragmented forks — a written file needing more than eight extents fills the inline descriptors and inserts the remainder into the extents-overflow B-tree, round-tripping exactly.

Out of scope (as with the btrfs/xfs siblings — these are deliberate non-goals, not partial implementations):

  • Journaling — since v0.18.0 a writable open of a journaled volume writes its commits as journal transactions at DurabilityStrict and DurabilityMounted (see "Writing through the journal" below; every other case, including non-journaled volumes, writes directly as described next). The rest of this bullet describes the direct path. A non-empty HFSJ journal is replayed at a writable open, and by Open(..., WithJournalReplay()) in memory, see below. A journaled volume (kHFSVolumeJournaledBit, e.g. hdiutil create -fs "Journaled HFS+") whose journal is empty (start == end, the state after a clean unmount) is opened read/write and written directly: macOS's replay at mount has nothing to copy over the written blocks, and the journal info block, journal and attribute are left as they were (since v0.12.3 the first commit that changes anything stamps lastMountedVersion = GoHF into both volume headers, journaled or not; not HFSJ, the driver does not maintain the journal). One whose journal holds transactions is replayed first when its lastMountedVersion is HFSJ (see "Writable opens replay it too" below) and refused with ErrJournaled otherwise; one whose journal cannot be verified (info block or journal outside the volume, other-device journal, bad magic or checksum, size mismatch, or since v0.12.3 the Apple journal_open range checks: jhdr_size 0, journal size outside [256 KiB, 1 GiB], start/end 0, beyond the journal size or 1 GiB, or not a multiple of 512; three older checks stay stricter than Apple's and can refuse a journal macOS opens: the magic, where we accept only JNLx and Apple also accepts JHDR; the endian field; and the journal size recorded in the journal header against the Journal Info Block's, hfsplus#48) is refused by OpenWritable / OpenFileWritable with ErrJournaled, because macOS would replay the journalled blocks over the driver's writes. Read-only Open is never refused.
  • Do not write an image that is mounted. macOS keeps its own view of a mounted volume (cached nodes, bitmap and headers) and the two clobber each other (see detach in macos_validation_test.go).
  • Hardlinks / compression / resource forks / xattrs — indirect-node hardlinks, decmpfs compression, resource forks, and the Attributes B-tree are not handled (compression is detected and rejected rather than returning wrong bytes).

Cost of a write

OpenFileWritable reads and writes the image in place over the file; it never holds a copy of it, so opening costs a few KiB whatever the image size, and the 64 GiB in-memory ceiling is gone.

  • Memory is what one mutation touches (the B-tree nodes and bitmap chunks it edits, the two volume headers; refused beyond 64 MiB) plus a read cache of at most 8 MiB. It does not grow with the image. The bitmap is read in chunks of at most 4 KiB, never loaded whole. (OpenWritable over a []byte is the caller's own copy, kept current for Bytes(); Format/Mkfs still build the new image in memory.)

  • Bytes written per mutation are the file's data blocks plus the 512-byte sectors that changed: a 1-byte WriteFile is a data block, a few catalog and bitmap sectors and the two 512-byte headers (tens of KiB at the most); Chtimes is one catalog node's changed sectors and the headers. Not the image. Measured on a 1 GiB sparse image by TestInPlaceLargeSparseImage.

  • Order of each mutation (Apple TN1150, "Volume Header", "Allocation File"): file data, then the primary header rewritten with kHFSVolumeUnmountedBit cleared, fsync; the B-tree and bitmap sectors, fsync; then the alternate header and, last, the primary header (bit set again), fsync. A crash anywhere in between leaves the bit clear, so the next mount runs fsck_hfs instead of trusting a half-written volume (go-filesystems/hfsplus#32, finding 6). A volume that was already not cleanly unmounted when opened is left that way. The cost is three fsyncs per mutation. The guarantee is exactly that the fsyncs separate the three groups (data and the cleared bit; B-tree and bitmap sectors; both headers); the alternate and the primary header are written back to back with no fsync between them, and the B-tree sectors are edited in place, so a crash is repaired by fsck_hfs, not by the driver (there is no journal).

  • Durability levels (SetDurability, Batch; hfsplus#35). Barriers are File.Sync, which on darwin is fcntl(F_FULLFSYNC) (Go's internal/poll/fd_fsync_darwin.go, Fsync, line 22); man 2 fsync says plain fsync(2) leaves the drive free to reorder, man 2 fcntl says F_FULLFSYNC "acts as a barrier", so the ordering above is only a guarantee over F_FULLFSYNC and no weaker primitive is offered. Fsyncs for a first mutation on a volume clean at open, a second without file data, a third with file data, then Sync (counted by TestDurabilityBarriers):

    Level first next (no data) with data Sync Durable when the mutator returns Volume between mutations
    DurabilityStrict (default) 3 3 3 1 yes marked clean
    DurabilityMounted 2 1 2 1 yes marked dirty until Sync/Close
    DurabilityLazy 1 0 0 2 no, only after Sync/Close marked dirty until Sync/Close
    Strict or Mounted, journaled volume, journal writes on (default since v0.18.0) 2 1 1 (small data) 0 or 1 yes mounted, journal live; Close marks it clean
    Lazy or Batch, journaled volume, journal writes on (v0.19.0) 0 0 0 (large data: written in place, ordered by one barrier before the transaction) 2 for a session's first flush, 1 after; the first Sync after a flush pays 1 more (its in-place writes), then 0 no, only after Sync/Close/a level switch mounted, journal live, never marked dirty

    The first three rows are the direct path: every non-journaled volume, and a journaled volume that fell back (see "Writing through the journal"). The fourth row replaces the Strict and Mounted rows on a journaled volume, which is where the journal is on by default: both levels run the same journal commit, so Mounted no longer has a dirty span there (the journal makes each commit atomic). A single commit that cannot be journaled while the journal is on (a forged sector, more than 30 runs) takes the Strict direct path at Mounted too: the volume is never held dirty while it journals. The last row replaces the Lazy row on a journaled volume: a group commit. A Lazy commit writes nothing and takes no barrier; its changed sectors (and its small data) are kept in memory, and Sync, Close, SetDurability away from Lazy or the group's size bound write them as ONE journal transaction. The memory is bounded: the group never holds more than the journal's data bound (the smaller of 1 MiB and one eighth of the journal, at most 2048 sectors; 32704 bytes on a 256 KiB journal), at most 30 runs and 4096 freed block spans, on top of the per-mutation limit that already existed. A commit that does not fit beside the group flushes the group first and starts the next one.

    Mounted is data-ordered (like ext4 data=ordered): a commit that wrote file data puts a barrier between the data and the metadata that points at it, so no crash image shows a file holding bytes it was never written with (TestDurabilityNoStaleData, red without that barrier). On the direct path Lazy is data-writeback, by design: after a crash a file may contain stale bytes of a previously deleted file (a disclosure), so do not use Lazy or Batch on a volume that is not written through its journal where different users share it. On a journaled volume written through its journal this exposure is gone, as far as the tests show: small data is in the transaction, large data is written in place only into blocks no durable state references (blocks the group freed are treated as still referenced and their new data goes through the transaction) and a barrier orders it before the transaction (TestGroupBatchAtomicCrashOracle, its delete-then-reuse case is red without that rule: the surviving file read the deleted file's bytes). On the direct path, under Mounted and Lazy the journaled bit stays cleared for the whole held span, not one commit: after a crash in it the volume stays non-journaled until diskutil enableJournal; the group commit never clears it. SetDurability and Batch must not run concurrently with mutators.

    Three barriers is the minimum for a commit that leaves the volume clean: the dirty mark must be durable before a sector moves, and those sectors before the clean mark. Mounted is TN1150's own protocol ("An implementation must clear this bit on the media when it mounts a volume for writing ... set this bit ... as the last step of unmounting"): the mark is cleared once, sectors are written in place and made durable with one barrier, and Sync/Close restores the clean marks. Nothing inside a commit is ordered, the dirty mark is what protects it: a crash between Syncs leaves a volume that fsck_hfs checks at mount, never one that claims to be clean and is not. Batch(fn) runs fn lazily then Syncs: a group of mutations (an SMB write plus its times) pays one set of barriers. On the direct path it is not a transaction (a crash loses the group in whole or in part, with the volume marked dirty). On a journaled volume written through its journal (v0.19.0) a Batch whose mutations fit one group is ATOMIC: after a crash the volume shows all of it or none of it, file bytes included (TestGroupBatchAtomicCrashOracle, every crash prefix, both write orders; and macOS's own mount and fsck_hfs on sampled crash images, TestDarwinJournalWriterBatchKernelReplay). A Batch larger than one group is NOT atomic: it is a sequence of groups, each made of whole mutations, so a crash leaves a prefix of the mutations, never part of one (TestGroupBatchLargerThanOneGroupLeavesAPrefix); an overwrite or a Truncate, which commit twice, are never split by the bound (TestGroupBatchNeverSplitsAMutation). A single mutation too large for an empty group is not atomic, as on Strict. If fn returns an error the mutations it made stay and are committed by the Batch's Sync, as before; the error is returned. Close settles. A failed volume is left dirty. Checked by TestDurabilityCrashModel (both crash models plus every write of every fsync group landing alone or missing alone) and TestDarwinCrashImagesFsck at every level.

    Measured against macOS's own HFS+ driver (v0.12.2, 2026-11-16, 1-min load 4.19 to 4.75, 30 interleaved rounds, opt-in measure_darwin_test.go with HFSPLUS_MEASURE=1), create + 1 byte + fsync: macOS 5.13 ms; Strict 12.66 ms (2.47x); Mounted 8.20 ms (1.60x); Lazy 0.095 ms (not durable until Sync). The same 6 barriers over plain fsync(2) cost 0.29 ms, so the gap is the F_FULLFSYNC count. Closing it is hfsplus#45 (journal writes); the design is docs/design/journal-write.md.

    Re-measured at v0.16.0 (2026-11-21, 1-min load 4.85 to 5.29 before and after, same test, 30 interleaved rounds, medians of rounds 2-30), create + 1 byte + fsync:

    Driver Time Barriers
    macOS, HFS+ 5.24 ms (kernel)
    macOS, Journaled HFS+ 9.41 ms (kernel)
    Strict, Journaled HFS+ image (direct path) 13.41 ms 3
    Mounted (direct path) 8.84 ms 2
    EnableJournalWrites, Journaled HFS+ image 4.37 ms 1

    On a journaled image, journal writes are 3.1x faster than Strict and 2.2x faster than macOS's own driver on the same kind of volume. mkdir and Chtimes are metadata-only: they cost 4.24 ms and 4.27 ms, the same as Mounted (one barrier each). One run on one machine; the first round of each, which includes the session's first commit, is reported apart in the test log.

    Since v0.18.0 (PR 6 of hfsplus#45) the journal is the default at Strict and Mounted on a journaled volume, so on that volume both levels now run the code of the EnableJournalWrites row and cost what it costs; the Strict and Mounted rows above are what they cost on a journaled volume that falls back, and on a non-journaled one. The barrier counts, which do not depend on load, are asserted by TestJournalWritesDefaultOn: 2, 1, 1 at both levels.

    Re-measured at v0.19.0 (2026-11-24, clean checkout of the tag, 1-min load 6.73 at the start, same test, 30 interleaved rounds, medians of rounds 2-30), create + 1 byte + fsync:

    Driver Time Barriers
    macOS, HFS+ 5.25 ms (kernel)
    macOS, Journaled HFS+ 8.73 ms (kernel)
    Strict (default), Journaled HFS+ image 4.27 ms 1
    EnableJournalWrites, Journaled HFS+ image 4.38 ms 1
    Strict, non-journaled image 14.76 ms 3
    Mounted, non-journaled image 8.71 ms 2

    The default path reproduces the journal-writes row: on a journaled volume Strict is now 2.0x faster than macOS's own driver on the same kind of volume, and faster than macOS on plain HFS+. mkdir and Chtimes on the journaled image cost 4.26 ms and 4.38 ms (1 barrier). The first round, which includes the session's first commit (2 barriers), is reported apart in the test log. One run on one machine.

  • Journaled volumes. fsck_hfs -q, the quick check macOS runs before mounting, exits 0 on a journaled volume whatever the unmounted bit says (it trusts the journal), so on a journaled volume the cleared bit alone would not get a half-written volume checked. The commit therefore also clears kHFSVolumeJournaledBit in its first header write, whether or not the volume was cleanly unmounted at open (a journaled volume left dirty with an empty journal is a real state; the journal was empty at open and is never written), and the final header restores it. Measured with fsck_hfs on crash images of Go-made, Apple HFS+, Apple Journaled HFS+ and Apple Journaled HFS+ left dirty (TestDarwinCrashImagesFsck): every dirty image makes -q exit non-zero (for the volume dirty at open, every image with the journaled bit cleared mid-commit), every image still marked clean passes -fn. What macOS does with a volume left mid-commit (TestDarwinMountOfCrashedJournaled, opt-in): it mounts it read/write as plain "HFS+", not journaled, sets the unmounted bit when it unmounts it, and fsck_hfs -fn is clean afterwards; the journal is not re-enabled (the volume stays non-journaled until diskutil enableJournal) and nothing is lost. Whether macOS ran a full check at mount was not observed directly. Restoring the bit is safe in every state: the journal is never written.

  • A failed write (a WriteAt or an fsync error after the first write of a mutation) leaves the disk in an unknown state, so the volume is latched: every later mutation returns ErrVolumeFailed and writes nothing until the volume is closed and opened again; reads still work. A failure before the first write (while writing the file's data) latches nothing.

  • freeBlocks is counted from the bitmap at the first write of a volume (one pass over the bitmap, in chunks), then kept in step bit by bit, so a wrong count in the header is put right.

  • Allocation scans the bitmap from block 0 for a free run, so a very full large volume reads more bitmap per write (reads only; the bytes written stay O(change)).

Module

github.com/go-filesystems/hfsplus

Install

go get github.com/go-filesystems/hfsplus

Usage

package main

import (
	"fmt"

	"github.com/go-filesystems/hfsplus"
)

func main() {
	// Format a fresh volume in pure Go and write into it.
	fs, err := hfsplus.Format("disk.hfs", 16<<20, hfsplus.FormatConfig{Label: "DATA"})
	if err != nil {
		panic(err)
	}
	_ = fs.MkDir("/sub", 0o755)
	_ = fs.WriteFile("/sub/hello.txt", []byte("hello\n"), 0o644)
	fs.Close() // flushes to disk.hfs

	// Reopen read-only and read it back.
	v, _ := hfsplus.OpenFile("disk.hfs")
	defer v.Close()
	entries, _ := v.ListDir("/sub")
	for _, e := range entries {
		fmt.Println(e.Name(), e.FileType())
	}
	data, _ := v.ReadFile("/sub/hello.txt")
	fmt.Printf("%d bytes\n", len(data))
}

Open(io.ReaderAt, size) / OpenWritable([]byte, io.WriterAt) are also available when you already hold the image bytes (e.g. a decompressed DMG in memory). The returned *Volume implements github.com/go-filesystems/interface.Filesystem plus the optional Labeller / Symlinker / Truncater capabilities.

Reading a journaled volume whose journal holds transactions

Open(rs, size, hfsplus.WithJournalReplay()) (and OpenFile(path, hfsplus.WithJournalReplay())) serves such a volume as macOS would see it after replaying the journal: the journaled blocks are replayed in memory, over the image, and read from there. The image is not written and the volume stays read-only; the size must be known (not -1). It is a port of Apple's replay_journal (hfs-715.100.10, core/hfs_journal.c), so a transaction is read past the header's end while sequence numbers continue, a torn block drops its transaction, and wraparound is handled. A bad block list header inside the journal's start..end range drops that transaction AND the one before it (the kernel restarts at the start of the last list that began a transaction); past end a bad list just ends the read. Every size taken from the journal is bounded. The bound on the replayed block data, min(journal size, 256 MiB), is this driver's own resource limit, not Apple's (the kernel streams blocks to the device): past it Open fails with ErrReplayBudget. A journal the replayer refuses fails Open with ErrJournalReplay (which wraps ErrJournaled; ErrReplayBudget wraps ErrJournalReplay). Without the option, Open reads the blocks as they are on the medium, without the transactions. Design: docs/design/journal-write.md, section 4; hfsplus#45.

Writable opens replay it too (since PR 2 of hfsplus#45)

OpenWritable, OpenWritableAt and OpenFileWritable on a journaled volume whose journal holds transactions and whose lastMountedVersion is HFSJ now replay the journal into the volume instead of returning ErrJournaled, then carry on with the usual write path (including the GoHF stamp of the first commit). What is written at open, in this order: the replayed blocks, a barrier, the journal header with start == end (the header sector, checksum recomputed, sequence number kept), a barrier. That is 2 barriers at open, at every durability level (the replay is always made durable before the header that empties it, which is stricter than the kernel, whose write_journal_header flushes only after the header). A crash leaves the volume as it was with its journal still replaying, or the replayed volume with an empty journal, never an empty journal over blocks that did not land. A journal that cannot be replayed (ErrJournalReplay, ErrReplayBudget) is refused before anything is written. A WriteAt or Sync error part-way is crash-safe, not "writes nothing": the journal stays non-empty and the next open replays to the same result. The open's checks after the replay can still refuse a volume the replay rewrote (a replayed journal info block that makes the journal unreadable), with the replayed blocks already written. Bytes given to OpenWritable(img, wa) with a wa that has no Sync are not replayed (ErrJournaled): without a barrier the order above cannot be kept, and an emptied journal over missing blocks is declared by nothing (a torn commit of the write path is declared by the cleared unmounted bit). The replayed blocks are bounded by the image size, as the kernel and WithJournalReplay do, not by the old volume header's totalBlocks, because an interrupted hfs_extendfs journals the new alternate header past the old end.

Still refused with ErrJournaled: a journal with transactions on a volume whose lastMountedVersion is not HFSJ (it was last mounted by something else). TN1150 says such a journal must not be replayed, and macOS throws it away at mount (hfs_vfsutils.c:2886-2895); this driver does not port that reset, because discarding transactions is a loss of data the caller did not ask for. Mount and unmount the volume on macOS to have the journal reset. Read-only Open WITHOUT WithJournalReplay shows the blocks in place; WithJournalReplay shows the journal's view, which macOS would discard, not what macOS will show. Also still refused: every journal checkJournalAt cannot verify. Before v0.18.0 the driver did not write transactions by default: the write path bypassed the journal.

Writing through the journal (on by default since v0.18.0, PR 3 and PR 6 of hfsplus#45)

What changes for an existing caller (v0.18.0). OpenWritable, OpenWritableAt and OpenFileWritable now call EnableJournalWrites themselves. On a journaled volume whose journal passes its checks, Strict (the default) and Mounted commits become journal transactions: fewer barriers (2 on a session's first commit, 1 after, instead of 3, or 2 and 1 for Mounted), the volume reads lastMountedVersion HFSJ (not GoHF) and not-cleanly-unmounted while the session runs, and Close writes a last transaction and empties the journal, so Close (or Sync) matters more than before: a volume that is never closed is left with a live journal that macOS replays at its next mount, which is correct but is not the clean image the direct path left. Mounted on a journaled volume no longer keeps a dirty span. (Since v0.19.0 SetDurability(Lazy) and Batch are a group commit on a journaled volume: nothing is written until Sync, Close or a level switch, then one transaction; no level switch empties the journal. For an existing caller of Lazy or Batch: the volume is no longer marked dirty or written in place between Syncs, a Batch that fits one group is atomic, and Bytes() of an in-memory journaled volume flushes the open group first, so it shows the last mutation (TestGroupBytesIsCurrent).) A non-journaled volume is written byte for byte as before (TestNonJournaledVolumeUnchangedByDefault). When the checks fail (not journaled, no Sync on the writer, journal never initialised, jhdr_size not 512, ...) the open still succeeds, silently, and the volume writes directly exactly as in v0.17.0: (*Volume).JournalWritesReason() returns nil when the journal is on and otherwise the reason, wrapping ErrJournalWritesUnavailable. There is no opt-out switch: no caller in the go-filesystems organisation used EnableJournalWrites; say so on hfsplus#45 if you need an in-place mode. EnableJournalWrites() stays, a no-op when the journal is already on, and re-tries the checks after a fallback.

v.EnableJournalWrites() is what the opens call. When on, a metadata-only commit at DurabilityStrict or DurabilityMounted on a journaled volume is written as a journal transaction instead of in place first: 2 barriers on a session's first commit, 1 on every later one (3 today). The first commit also switches lastMountedVersion from GoHF to HFSJ, in the same commit, and clears the unmounted bit as macOS does at mount; Close commits a last transaction whose headers carry the clean mark, then empties the journal as macOS's unmount does (3 barriers, once; since PR 5 of hfsplus#45). Sync in journal mode costs one barrier if in-place writes are pending since the last one, none otherwise. A crash leaves a journal macOS replays to the volume after the last commit that returned, or the one in flight, whole (checked against macOS's own replay, and fsck_hfs after the mount; a crash inside Close is covered too). An offline fsck_hfs -fn taken in the middle of a commit cannot verify the image until macOS has mounted it: on 407 of the 820 crash images measured it exits 8 ("could not be verified completely"), always inside a commit's still-open group, and macOS mounts every one of them and fsck_hfs -fn then exits 0. The volume is not inconsistent; why fsck's journal stage gives up (fsck_journal.c:517-624) is not established.

Cost. Each barrier is F_FULLFSYNC. In one indicative run (load 4-5, 5-minute average 8.6) a steady-state metadata commit (1 barrier) took 4.2 ms, the first commit of a session (2 barriers) 8.7 ms, against 12.8 ms for today's strict path (3 barriers) and 8.6 / 10.3 ms for macOS's own create / mkdir on Journaled HFS+. Since v0.16.0 a commit with small file data also takes 1 barrier: 4.37 ms for create + 1 byte + fsync on a quiet machine, against 9.41 ms for macOS on Journaled HFS+ (table above); the v0.19.0 default measured 4.27 ms against 8.73 ms. The design's 2.5-3 ms estimate is still not met, because one F_FULLFSYNC costs about 4 ms here. A hypothesis, not measured: the kernel's journal barrier is DKIOCSYNCHRONIZE with the barrier option (hfs_journal.c:469-482), roughly F_BARRIERFSYNC and cheaper than F_FULLFSYNC; this driver does not offer it and this PR does not change the barrier.

File data through the journal (PR 4 of hfsplus#45, v0.16.0). A commit that writes file data (WriteFile) is journaled too, at DurabilityStrict. The bound is min(1 MiB, (journal size - jhdr_size) / 8) of data per commit. Small data (staged bytes within the bound) goes into the transaction with the metadata: one barrier (2 on a session's first commit). Large data is written in place first, then a barrier, then the metadata commit: two barriers (3 on the first). Staged data over the bound is spilled to in-place writes. Blocks the revoke rule forces into the journal are staged without a bound of their own: today the allocator keeps them within the small-data bound, and file creation is the only caller (go-filesystems/hfsplus#61). Revoke rule: replay copies every block of every transaction in the ring, in order, so a block journaled in T(k-2), T(k-1) or T(k) that is later freed and reused as file data must not be written in place, or a replay would overwrite it with the old bytes. A newly allocated data block inside that window is therefore journaled in the new transaction (the allocator counts those against the bound and skips a block that would exceed it). A data sector that verifies as a block list header is never put in the ring: the data takes the ordered path (in place, barrier first); if a revoke-forced block carries one, the commit falls back to the direct path after emptying the journal. Checked by crash oracles with byte checks (small, large, the revoke reuse, and the reverse write order) and by macOS's own replay (HFSPLUS_JOURNAL_ORACLE=1).

What falls back to today's direct path, after emptying the journal (2 barriers) if a transaction is live: a commit that carries a sector which verifies as a block list header (v0.15.1, hfsplus#56: replay follows such a sector past the journal's end, and catalog nodes hold file names a user chooses; every 512-byte sector of every journaled block is checked, in both byte orders, before the transaction is written, and the writer never emits sequence number 0; a planted sector makes every commit that touches the poisoned catalog node take the direct path, whoever makes it; and, since v0.15.2 (hfsplus#58), the first journaled commit of a session first scrubs the ring: it reads the journal once, 64 KiB at a time, and zeroes every 512-byte sector that verifies as a block list header in either byte order (a sector a v0.15.0 session wrote before the guard, or another implementation left), with one extra barrier that makes the zeroes durable before the transaction lands, and only when a sector was zeroed. Unproven: whether the kernel's replay or fsck_hfs follows a forged list, since the proof covers this driver's replayer only), a commit that changes more than 30 runs of 512-byte sectors, a run that is not whole sectors, or does not fit even an empty journal, (DurabilityLazy and Batch are a group commit since PR 7 of hfsplus#45, see "Durability levels"; a group the journal cannot take is flushed and the commit goes the Strict way). A commit that fits an empty journal but not the live one (three transactions must fit, TN1150 leaves one sector free) is journal full, no longer a fallback (PR 5): the journal is emptied (2 barriers) and the commit goes in as a first commit (2 more), numbered previous + 1, with no ring scrub. Every such fallback ("bypass") empties the journal first and writes GoHF as before. At Mounted the fallback commit takes the Strict direct path (bracketed, three barriers), never the held dirty span: a volume is never held dirty while it journals, so the clean marks Sync, Close and SetDurability write never land over a live journal (TestMountedFallbackNeverHeldWhileJournaling); the next journaled commit is a first commit with the session-start sequence bump (the design text says previous + 1 there; the bump is the more conservative and was kept). EnableJournalWrites itself returns ErrJournalWritesUnavailable and the volume keeps writing directly when the writer given to OpenWritable cannot Sync (every barrier would be a no-op; a nil writer or an OpenFileWritable file is fine), it is not journaled, the journal was never initialised, its jhdr_size is not 512, or its size or blhdr_size fail the checks of the design. A transaction uses a one-sector block list header (blhdr_size is set to jhdr_size in the first header), so it holds at most 30 blocks. Design: docs/design/journal-write.md.

A commit with several candidates that exceeds 30 runs or does not fit the ring writes its data on the ordered path instead (in place, barrier, then the transaction).

API

Function / method Purpose
Open(rs io.ReaderAt, size int64, opts ...OpenOption) (*Volume, error) Parse an HFS+/HFSX volume read-only from a ReaderAt; WithJournalReplay() replays a non-empty journal in memory (see above)
OpenFile(path string, opts ...OpenOption) (*Volume, error) Open an image file read-only
OpenWritable(img []byte, wa io.WriterAt) (*Volume, error) Open an in-memory image read/write
OpenFileWritable(path string) (*Volume, error) Open an image file read/write in place (no copy of the image; each mutation writes and fsyncs what it changed)
OpenWritableAt(rw ReadWriteSyncerAt, size int64) (*Volume, error) The same in-place open over a store the caller already holds (an *os.File opened with its own checks, an os.Root, a descriptor passed in), so the image is not reopened by name. rw stays the caller's: Close does not close it. Same ErrJournaled check. size must be at least totalBlocks x blockSize and a volume header must sit at the alternate location, 1024 bytes before the end of the volume (TN1150, "Volume Header"), else ErrVolumeSize and nothing is written; a file longer than the volume is written up to the volume's end only. After Close, mutators return ErrClosed. HFS-wrapped volumes are not supported
Mkfs(size int64, cfg FormatConfig) ([]byte, error) Build a fresh empty HFS+/HFSX image in pure Go
Format(path string, size int64, cfg FormatConfig) (filesystem.Filesystem, error) Pure-Go format + open read/write
FormatAppleDmg(path, size, cfg) Optional darwin-only hdiutil alternative
(*Volume) WriteFile / MkDir / DeleteFile / DeleteDir / Rename Mutate the volume
(*Volume) SetLabel / Symlink / Truncate Optional Labeller / Symlinker / Truncater capabilities
(*Volume) Sync() error Final fsync; at DurabilityMounted/DurabilityLazy also makes everything durable and restores the clean marks. Over a writable io.WriterAt with no Sync it returns ErrSyncUnsupported (wraps errors.ErrUnsupported) rather than nil: nothing was made durable. Close, SetDurability and Batch still settle through it without error
(*Volume) JournalWritesReason() error nil when commits go through the journal (default on a journaled volume, v0.18.0), ErrReadOnly on a read-only volume, else why the volume writes directly (wraps ErrJournalWritesUnavailable)
(*Volume) SetDurability(Durability) error / Durability() Choose DurabilityStrict (default), DurabilityMounted or DurabilityLazy; see "Durability levels"
(*Volume) Batch(fn func() error) error Run fn lazily, then one Sync; on a journaled volume one atomic journal transaction when fn's mutations fit one group
(*Volume) ListDir / ReadFile / Stat / ReadLink Read paths
(*Volume) Chtimes Chtimes(path, atime, mtime): contentModDate and accessDate as whole GMT seconds since 1904 (rounded down), attributeModDate (ctime) set to the clock, createDate untouched; a zero time leaves its field unchanged; a time outside 1904-01-01 00:00:01 .. 2040-02-06 06:28:15 GMT (the date 0 reads as "not recorded") is a *TimeRangeError (errors.Is(err, ErrTimeOutOfRange)), nothing written; the catalog record is patched in place, so the B-tree is unchanged in shape. Checked with fsck_hfs and a macOS mount
(*Volume) Chmod, Chown with Chtimes, the whole filesystem.MetadataSetter (what SMB/NFS/SFTP servers probe for). Chmod replaces the 12 permission bits (setuid/setgid/sticky included) of HFSPlusBSDInfo.fileMode, keeping the type bits; Chown sets ownerID/groupID; both patch the catalog record in place (file or folder), set attributeModDate to the clock, refuse the root folder, and Stat now reports a folder's recorded mode. Checked with a macOS mount (stat -f %Lp)
(*Volume) Times filesystem.TimesReader: createDate, contentModDate, attributeModDate (ctime), accessDate from the catalog record, GMT seconds since 1904, resolution 1 s; a zero date and the root folder are unrecorded
(*Volume) OpenFile (filesystem.Opener) Positional reads of a data fork: each ReadAt reads only the extents of its window. A 1 MiB read at 66 MiB of a 72 MiB file allocates nothing through it and 75.5 MB through ReadFile
(*Volume) CaseSensitive() bool Report HFSX binary key comparison
ErrJournalWritesUnavailable (*Volume).EnableJournalWrites, and (*Volume).JournalWritesReason() after an open that fell back to direct writes, on a volume that cannot be written through its journal (not journaled, journal never initialised, jhdr_size not 512, checks failed); the volume keeps writing directly
ErrReplayBudget Open(..., WithJournalReplay()) where the blocks to replay exceed this driver's own bound, min(journal size, 256 MiB); wraps ErrJournalReplay
ErrJournalReplay Open(..., WithJournalReplay()) on a journal the replayer cannot replay (the kernel would abandon it, or a size in it is outside the bounds); wraps ErrJournaled
ErrJournaled OpenWritable / OpenFileWritable on a journaled volume whose journal is not provably empty (transactions pending on a volume whose lastMountedVersion is not HFSJ or that the replayer refuses, or unreadable / off-volume / other-device / bad checksum / a header field outside Apple's journal_open ranges since v0.12.3; three older checks stay stricter than Apple's, see hfsplus#48); errors.Is(err, ErrJournaled), the reason is in the message. Nothing is written, except when a replay succeeded and a check after it refused the volume (the replayed blocks are then on the medium, the journal empty), or a writer without Sync was refused before any write

Validation

The committed read-path fixture (testdata/hfsplus.dmg.gz) was produced on macOS with hdiutil create -fs "HFS+" and populated with known files; the reader lists those entries and reads each back with the exact MD5 macOS computed (also the big-endian s390x correctness test).

The write path is validated by darwin-only tests (macos_validation_test.go) that exercise the native tooling:

# Pure-Go format → fsck clean + macOS reads the bytes Go wrote
go test -run TestDarwinWriteRoundTrip
# hdiutil-created image → Go writes into it → fsck clean + macOS reads it
go test -run TestDarwinWriteIntoAppleImage
# journaled image with an empty journal → Go writes → macOS replays nothing and reads it
go test -run TestDarwinWriteIntoJournaledImage

The tests take the mount point from hdiutil attach's output, never from a spelled /Volumes/<name>: a volume of the same name left mounted by an earlier run (or another session) pushes the new one to /Volumes/<name> 1, and a test that read the spelled path read the stale volume, which is how TestDarwinWriteIntoAppleImage reported the driver's files as missing (#29; TestDarwinMountPointsAreNotShared keeps it fixed).

Each test formats/writes with the pure-Go driver, attaches the raw image with hdiutil, asserts fsck_hfs -n reports the volume clean, then mounts it read/write and compares the files and bytes macOS sees against what Go wrote. The cross-arch pure-Go round-trip (format → write → read) in write_test.go runs on every architecture, including big-endian s390x.

Untrusted images

An image is attacker-controlled input and every size in it is a claim. Each input-driven allocation is bounded before it is made (bombs_test.go measures each road with runtime.MemStats):

Claim in the image Bound
a fork's logical size (ReadFile, ReadLink) at most the image size, every extent the fork uses must lie inside the image (checked before the buffer is made); the last byte is probed as well
the same with the image size unknown (Open(rs, -1)) the buffer grows (1 MiB steps, doubling) with the bytes the reader delivers, so a forged 512 MiB claim over a 4 MiB volume costs 4 MiB
the B-tree leaf-chain walk (cycles) at most image size / node size steps (was: the extents' claim, up to 2^24)
a B-tree node nodeSize is a power of two in [512, 32768]

Fuzz targets: FuzzOpenHeader, FuzzOpenPatched, FuzzBombs (asserts a per-input allocation budget), FuzzJournalHeader and FuzzCheckJournal (the journal header and journal info block that decide whether a journaled volume is writable).

References

  • Apple TN1150, HFS Plus Volume Format (Journal, Journal Info Block, Journal Checksums, Replaying the Journal).
  • Linux fs/hfsplus/super.c: a journaled volume is mounted read-only unless forced.
  • man hdiutil, man fsck_hfs, man newfs_hfs.

License

BSD-3-Clause © the go-filesystems authors.

Documentation

Overview

Package hfsplus is a pure-Go, CGO-free read/write driver for the HFS+ (Mac OS Extended) on-disk format, including its HFSX (case-sensitive) variant.

HFS+ stores every multi-byte field big-endian. The volume header lives at byte offset 1024 and carries the block size, block counts, and the special fork descriptors for the catalog, extents-overflow, and allocation files. File and directory metadata live in the catalog B-tree, keyed by the parent CNID plus the UTF-16 node name; file contents are addressed by allocation blocks via up to eight inline extents per fork, spilling into the extents-overflow B-tree for fragmented files.

The package implements the full shared github.com/go-filesystems/interface Filesystem contract:

  • Open / OpenFile open a volume read-only.
  • OpenWritable / OpenFileWritable open it for read/write, in place: no copy of the image is held, and each mutation writes only what it changed.
  • Format (and the lower-level Mkfs) lay down a fresh, empty HFS+/HFSX volume in pure Go — no host tooling — that passes fsck_hfs -n clean and mounts read/write on macOS.
  • WriteFile, MkDir, DeleteFile, DeleteDir, Rename mutate the catalog B-tree (insert/delete with node splitting and tree-height growth), manage the allocation bitmap, and keep the volume-header counters in sync. The optional Labeller (SetLabel), Symlinker (Symlink), and Truncater (Truncate) capabilities are implemented too.

Every write path is validated against the native macOS tooling: fsck_hfs -n reports the volume clean and macOS mounts the image read/write and reads the exact files and bytes the Go side wrote, in both directions (Go-formatted → macOS-read and macOS-created → Go-written → macOS-read). The cross-arch, big-endian (s390x) round-trip runs in pure Go on every architecture.

Case-folding: case-insensitive name comparison implements Apple's FastUnicodeCompare for the practical character set (ASCII, Latin-1, Latin Extended-A, and the ignorable-NUL handling fsck requires) rather than embedding the full 8 KiB fold table; exotic case-folding corner cases outside that range fall back to identity ordering.

Out of scope (deliberate non-goals, as with the btrfs/xfs siblings):

  • Journaling, decmpfs compression, resource forks, and indirect-node hardlink following are not implemented; see the README Status section. A journaled volume with an empty journal is written directly; one with transactions in its journal is refused read/write with ErrJournaled.

Fragmented data forks (more than eight extents), catalog and extents-overflow B-tree growth, and node-underflow rebalancing/merging on delete ARE implemented: a written fork fills its eight inline extents and spills the remainder into the extents-overflow tree, the B-tree files grow their backing forks when their node reservation is exhausted, and deletion rebalances/merges underflowing nodes and frees emptied ones.

Index

Constants

View Source
const (
	// FinderFlagHasCustomIcon marks a folder or volume as carrying its own
	// icon — for a volume root, that is what makes the Finder look for
	// .VolumeIcon.icns and draw it instead of the generic disk. Writing the
	// file without the flag does nothing at all.
	FinderFlagHasCustomIcon = uint16(0x0400)

	// FinderFlagIsInvisible hides an entry, which is how a disk image keeps
	// its .background folder out of the window it decorates.
	FinderFlagIsInvisible = uint16(0x4000)
)

Finder flags worth naming. The rest are documented in TN1150 and in CarbonCore/Finder.h; these are the ones a disk-image builder needs.

View Source
const (

	// FinderFlagsOffset is where the flags word lives inside the 32 bytes
	// FinderInfo returns: DInfo.frFlags for a folder, FInfo.fdFlags for a
	// file. Big-endian, like everything else in HFS+.
	FinderFlagsOffset = 8
)

The Finder's own 32 bytes, which every catalog record carries and which nothing here could read or write until now.

HFS+ (Apple TN1150) puts them at the same place in both record kinds: a 16-byte userInfo at record offset 48 followed by a 16-byte finderInfo at 64. For a file those are FInfo/FXInfo, for a folder DInfo/DXInfo, and the two differ in what the first eight bytes mean — but the FLAGS word sits at +8 of userInfo either way, so it is addressable without knowing which kind a record is.

Variables

View Source
var (
	// ErrReadOnly is returned by every mutating method when the volume was
	// opened read-only (Open / OpenFile). Open it writable (OpenWritable /
	// OpenFileWritable / Format) to mutate it.
	ErrReadOnly = errors.New("hfsplus: filesystem is read-only")

	// ErrClosed is returned by every mutating method of a writable volume after
	// Close.
	ErrClosed = errors.New("hfsplus: volume is closed")

	// ErrVolumeFailed is returned by every mutating method of a writable volume
	// after a write or fsync to its backing store failed. What reached the disk is
	// then unknown (some B-tree sectors written, the headers not), so no further
	// mutation is attempted -- one would write over a half-written volume and mark
	// it cleanly unmounted. Close it and open it again; reads still work.
	ErrVolumeFailed = errors.New("hfsplus: an earlier write failed; reopen the volume")

	// ErrBadHeader is returned when the volume header at offset 1024 lacks a
	// recognized HFS+ ("H+") or HFSX ("HX") signature.
	ErrBadHeader = errors.New("hfsplus: no valid volume header")

	// ErrNotFound is returned when a path component cannot be located in the
	// catalog.
	ErrNotFound = fmt.Errorf("hfsplus: path not found: %w", iofs.ErrNotExist)

	// ErrNotDirectory is returned when ListDir targets a non-directory.
	ErrNotDirectory = errors.New("hfsplus: not a directory")

	// ErrNotRegular is returned when ReadFile targets a non-regular file.
	ErrNotRegular = errors.New("hfsplus: not a regular file")

	// ErrNotSymlink is returned by ReadLink when the target is not a symlink.
	ErrNotSymlink = errors.New("hfsplus: not a symbolic link")

	// ErrCorrupt is returned when an on-disk structure fails a sanity check.
	ErrCorrupt = errors.New("hfsplus: corrupt image")

	// ErrUnsupported is returned for on-disk features the driver does not yet
	// decode/encode (e.g. compressed forks).
	ErrUnsupported = errors.New("hfsplus: unsupported feature")

	// ErrNoSpace is returned by the write path when the volume has no free
	// allocation blocks (or no contiguous run) to satisfy a request.
	ErrNoSpace = errors.New("hfsplus: no space left on volume")

	// ErrExists is returned by mutators when the target path already exists.
	ErrExists = errors.New("hfsplus: path already exists")

	// ErrNotEmpty is returned by DeleteDir when the directory still has
	// children.
	ErrNotEmpty = errors.New("hfsplus: directory not empty")
)

Sentinel errors. Compare with errors.Is so wrapped errors keep matching.

View Source
var ErrJournalReplay = fmt.Errorf("%w: journal cannot be replayed", ErrJournaled)

ErrJournalReplay is returned when a journal that holds transactions cannot be replayed: the kernel would abandon it (hfs_journal.c, bad_replay), or a size in it is outside the bounds the replayer accepts. It wraps ErrJournaled.

View Source
var ErrJournalWritesUnavailable = errors.New("hfsplus: this volume cannot be written through its journal")

ErrJournalWritesUnavailable is returned by EnableJournalWrites when the volume cannot be written through its journal. The volume stays usable and keeps writing directly.

View Source
var ErrJournaled = errors.New("hfsplus: journaled volume with a journal that is not empty")

ErrJournaled is returned by OpenWritable / OpenFileWritable for a journaled volume whose journal is not provably empty: it holds transactions macOS would replay over the driver's writes, or its journal cannot be read, is on another device, or fails its checksum. Open it read-only, or let macOS replay it (mount and unmount the volume) first.

View Source
var ErrReplayBudget = fmt.Errorf("%w: replayed blocks exceed the replay memory budget", ErrJournalReplay)

ErrReplayBudget is returned when the blocks a journal would replay exceed this driver's own resource bound, min(journal size, 256 MiB). The kernel has no such bound (it streams blocks to the device); it protects the memory of a process that replays an untrusted journal into an overlay. It wraps ErrJournalReplay.

View Source
var ErrSyncUnsupported = fmt.Errorf("hfsplus: the backing store has no Sync, so durability cannot be promised: %w", errors.ErrUnsupported)

ErrSyncUnsupported is what Sync returns when the volume's backing store is writable but has no Sync of its own. It wraps errors.ErrUnsupported, the way fsync(2) answers EINVAL for an object that cannot be synchronised. A caller that only wants the data written, not durable, may treat errors.Is(err, ErrSyncUnsupported) as success.

View Source
var ErrTimeOutOfRange = errors.New("hfsplus: time outside the HFS+ range 1904-2040")

ErrTimeOutOfRange is what Chtimes returns, wrapped in a *TimeRangeError, for a time a catalog date cannot hold: an unsigned 32-bit count of seconds since 1904-01-01 00:00:00 GMT, so 1904-01-01 00:00:01 .. 2040-02-06 06:28:15 GMT. The value 0 (1904-01-01 00:00:00 itself) means "not recorded" to a reader, so it is refused too rather than written as a date that reads back as none.

View Source
var ErrVolumeSize = errors.New("hfsplus: image size does not match the volume")

ErrVolumeSize is returned by the writable opens when the size given for the image does not fit the volume it holds: smaller than the volume says it is, or with no volume header where the alternate header must be. Nothing is written in that case; a write at the wrong place would land inside the volume.

Functions

func Format

func Format(path string, sizeBytes int64, cfg FormatConfig) (filesystem.Filesystem, error)

Format creates a fresh, empty HFS+ (or HFSX) volume image at path of sizeBytes bytes using the pure-Go formatter (Mkfs), then opens it read/write. Pure Go, CGO-free, big-endian — works on every architecture.

The produced image passes `fsck_hfs -n` clean on macOS and mounts read/write; on every platform Open/OpenWritable round-trip it. The returned Volume is writable: WriteFile/MkDir/DeleteFile/DeleteDir/Rename mutate it and flush back to path.

The signature matches the apfs sibling (Format(path, sizeBytes, cfg)).

func FormatAppleDmg

func FormatAppleDmg(path string, sizeBytes int64, cfg FormatConfig) (filesystem.Filesystem, error)

FormatAppleDmg is the optional darwin-only alternative that shells out to the native hdiutil to author a real HFS+ image (the same tool that produced the read-path fixtures). It is provided as a parity escape hatch alongside the primary pure-Go Format, mirroring the apfs sibling's FormatAppleDmg. On non-darwin platforms it returns ErrUnsupported.

func Mkfs

func Mkfs(sizeBytes int64, cfg FormatConfig) ([]byte, error)

Mkfs lays down a valid empty HFS+/HFSX volume of sizeBytes bytes into a freshly-allocated byte slice and returns it. Pure Go, big-endian, no host tooling — runs on every architecture. The returned image passes fsck_hfs -n on macOS and can be opened with Open/OpenWritable.

func OpenReader added in v0.3.0

func OpenReader(r io.ReaderAt, size int64) (filesystem.Filesystem, error)

OpenReader opens the HFS+ volume at the start of r, exactly as Open does, and returns it as a filesystem.Filesystem.

It exists so every driver in go-filesystems answers to ONE name for the same thing. github.com/go-filesystems/detect's Opener is func(io.ReaderAt, int64) (filesystem.Filesystem, error): Open here is the right shape but returns a concrete *Volume, which is a different type and does not fit. The drivers that could only be opened from a PATH have grown this function; this one is that same function, so a caller can write

detect.Register(detect.T, driver.OpenReader)

for every driver instead of a closure per driver.

The concrete type is still there for a caller that wants what it has beyond the common interface: Open returns it.

Types

type Durability added in v0.12.0

type Durability int

Durability says when a mutation of a writable volume is on the medium and what the volume looks like to fsck if the machine dies meanwhile (hfsplus#35).

Every level keeps the one guarantee the format needs: a volume whose header still says "cleanly unmounted" (kHFSVolumeUnmountedBit, and not journaled) is consistent. They differ in how many barriers (fsync, which on macOS is F_FULLFSYNC: Go's File.Sync) a commit pays and in when the header says so again.

const (
	// DurabilityStrict is the default and the strongest: every commit is durable
	// when its mutator returns, and the volume is marked cleanly unmounted again
	// between commits, so a crash at ANY point outside a commit leaves a volume
	// fsck_hfs -q passes. Cost: three barriers per commit on a volume that was
	// clean when opened (data and the cleared bit; B-tree and bitmap sectors;
	// alternate and primary header). On a journaled volume whose journal passes the
	// checks (the default, see JournalWritesReason) a commit is a journal
	// transaction instead: one barrier, two for a session's first.
	DurabilityStrict Durability = iota
	// DurabilityMounted behaves like a mounted HFS+ volume: the first commit
	// marks the volume as not cleanly unmounted (one barrier), later commits write
	// everything and take ONE barrier, and the clean mark is restored by Sync,
	// Close or SetDurability(DurabilityStrict). Each commit is durable when its
	// mutator returns, but a crash between Syncs leaves the volume marked dirty
	// (the next mount runs fsck_hfs, which finds nothing to repair unless a commit
	// was torn). Nothing inside the metadata of a commit is ordered: the dirty
	// mark is what protects it. Data is ordered (like ext4 data=ordered): a
	// commit that wrote file data takes a barrier between the data and the
	// metadata that points at it, so a crash never shows a file holding bytes it
	// was not written with; a commit without data takes one barrier. The
	// journaled bit stays cleared for the whole held span, so after a crash in it
	// the journal stays off until diskutil enableJournal. None of this applies on a
	// journaled volume written through its journal (the default, see
	// JournalWritesReason): there Mounted is the journal commit, the same as Strict,
	// and the volume is never held dirty. A commit that cannot be journaled (see the
	// list in journal_writer.go) takes the Strict direct path at Mounted too, bracketed
	// and three barriers, so the volume is not held dirty while it journals.
	DurabilityMounted
	// DurabilityLazy is DurabilityMounted without the per-commit barrier: a
	// mutation is durable only after Sync, Close or a switch to another level
	// (Batch does this for a group of mutations). A crash before that loses the
	// mutations made since the last Sync.
	//
	// On a journaled volume written through its journal (the default) Lazy is a
	// GROUP COMMIT: a commit writes nothing and pays no barrier, its sectors (and
	// small file data) are kept in memory, and Sync, Close, a switch away from Lazy,
	// Bytes (an in-memory volume only) or the group's size bound write them as ONE journal transaction (one barrier,
	// two for a session's first; a Sync right after pays one more for the flush's in-place
	// writes, then none). The volume is never
	// marked dirty, a crash leaves the volume as of the last flush (a prefix of whole
	// commits), and the stale-data exposure below does not exist: data written in
	// place goes only into blocks no durable state references, ordered by a barrier
	// before the transaction. The memory held is bounded by the journal's data bound
	// (see journal_group.go): at most min(1 MiB, journal/8) of sectors, 30 runs and
	// 4096 freed spans.
	//
	// On every other volume it is data-writeback: the volume is held dirty, nothing
	// orders file data before the metadata that references it, and after a crash a
	// file may contain stale bytes of a previously deleted file. Do not use it there
	// where different users share a volume.
	DurabilityLazy
)

func (Durability) String added in v0.12.0

func (d Durability) String() string

String names the level.

type FormatConfig

type FormatConfig struct {
	// Label is the volume name. Defaults to "GOTEST" when empty.
	Label string
	// CaseSensitive requests an HFSX (case-sensitive) volume instead of plain
	// case-insensitive HFS+.
	CaseSensitive bool
}

FormatConfig configures Format/Mkfs.

type OpenOption added in v0.13.0

type OpenOption func(*openConfig)

OpenOption configures Open and OpenFile.

func WithJournalReplay added in v0.13.0

func WithJournalReplay() OpenOption

WithJournalReplay makes Open replay a non-empty journal in memory (see replay.go). The volume stays read-only and the backing store is not written; OpenWritable and its variants still return ErrJournaled for such a volume. The size passed to Open must be known (not -1). A journal the replayer refuses makes Open fail with ErrJournalReplay.

type ReadWriteSyncerAt added in v0.11.0

type ReadWriteSyncerAt interface {
	io.ReaderAt
	io.WriterAt
	Sync() error
}

ReadWriteSyncerAt is the store OpenWritableAt reads and writes in place: an *os.File satisfies it, and so does a block device or any other random-access store. Sync is part of it because the order in which a commit reaches the medium (data, then B-tree and bitmap sectors, then the headers) is only a guarantee if each stage is made durable before the next begins.

type TimeRangeError added in v0.8.0

type TimeRangeError struct {
	Field string // "atime", "mtime" or "now" (the clock, for attributeModDate)
	Time  time.Time
}

TimeRangeError names the field and the value Chtimes refused.

func (*TimeRangeError) Error added in v0.8.0

func (e *TimeRangeError) Error() string

func (*TimeRangeError) Unwrap added in v0.8.0

func (e *TimeRangeError) Unwrap() error

type Volume

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

Volume is an opened HFS+ (or HFSX) volume. When opened read-only (Open / OpenFile) the mutating methods return ErrReadOnly. When opened writable (OpenWritable / OpenFileWritable / Format) the image is read and written in place: nothing but the structures one mutation touches is held in memory, and each mutation writes only the sectors it changed (see store.go).

func Open

func Open(rs io.ReaderAt, size int64, opts ...OpenOption) (*Volume, error)

Open parses an HFS+ volume from rs. The caller retains ownership of rs unless it implements io.Closer (then Close releases it). Pass size = -1 if unknown.

WithJournalReplay serves a journaled volume whose journal holds transactions as macOS would see it after replaying them, read-only and without writing anything to rs.

func OpenFile

func OpenFile(path string, opts ...OpenOption) (*Volume, error)

OpenFile opens the image at path read-only.

func OpenFileWritable

func OpenFileWritable(path string) (*Volume, error)

OpenFileWritable opens the image at path for read/write, in place: the image is not copied into memory. Each mutation reads what it needs from the file and writes back only what it changed (see store.go); the mutators sync as they go.

func OpenWritable

func OpenWritable(img []byte, wa io.WriterAt) (*Volume, error)

OpenWritable opens an HFS+ image held entirely in img for read/write. The volume edits img in place; callers can retrieve the mutated bytes with Bytes() or, if wa is non-nil, flush them with Sync. Pass wa = nil for a purely in-memory writable volume.

A journaled volume whose journal holds transactions and whose lastMountedVersion is 'HFSJ' is replayed first (replay_write.go): the blocks, a barrier, the empty journal header, a barrier. If wa is non-nil the replay is written to it too.

func OpenWritableAt added in v0.11.0

func OpenWritableAt(rw ReadWriteSyncerAt, size int64) (*Volume, error)

OpenWritableAt opens the size-byte HFS+ image held by rw for read/write, in place, exactly as OpenFileWritable does: no copy of the image is made, a journaled volume whose journal holds transactions is replayed first if its lastMountedVersion is 'HFSJ' (replay_write.go) and refused with ErrJournaled if it is not, and every mutation reads what it needs from rw and writes back only what it changed (see store.go).

It exists so a caller that already holds the image open -- an *os.File it opened itself, with the checks it needs (an os.Root, O_NOFOLLOW, a descriptor passed in) -- is not made to reopen it by name, which another process can swap in between.

Ownership: rw belongs to the caller. Volume.Close does NOT close it, even if it implements io.Closer; the caller closes it, after the last use of the Volume and not before (the Volume reads and writes through it until then). size is the number of bytes of the image (the volume's alternate header is 1024 bytes before its end), usually the file's size; it must be positive. Use only one writable Volume at a time over a store: two would each keep their own view of it.

func (*Volume) Batch added in v0.12.0

func (v *Volume) Batch(fn func() error) error

Batch runs fn with DurabilityLazy, then makes everything fn wrote durable with one Sync and restores the previous level: a group of mutations pays one set of barriers instead of one per mutation (an SMB request that writes and sets times, say).

On a journaled volume written through its journal, a Batch whose mutations fit one group (the bounds on DurabilityLazy) is ATOMIC: after a crash the volume shows all of it or none of it, file bytes included. A larger Batch is a sequence of groups made of whole mutations: a crash leaves a prefix of the mutations, never part of one (an overwrite's delete and create are one mutation: a group is never flushed between them). The one exception is a single mutation too large for even an empty group, which is as torn as it is on DurabilityStrict. On any other volume it is not a transaction: the mutations may be partly lost and the volume is left marked dirty, with DurabilityLazy's data-writeback exposure.

If fn fails, the mutations it already made stay and are committed by the Sync (they are not rolled back); fn's error is returned in preference to Sync's. Like SetDurability, it must not run concurrently with mutators.

func (*Volume) Bytes

func (v *Volume) Bytes() []byte

Bytes returns the current (possibly mutated) image bytes of a volume opened with OpenWritable over a []byte, or nil for any other volume (read-only, or file-backed, which holds no copy of the image). The slice aliases the volume's buffer; copy it if you need a stable snapshot. On a journaled volume at DurabilityLazy the commits not yet written (the open group, journal_group.go) are flushed first, as one journal transaction, so the image is current; a failure of that flush latches like any other write failure and shows in the next call that returns an error.

func (*Volume) CaseSensitive

func (v *Volume) CaseSensitive() bool

CaseSensitive reports whether the volume is HFSX with binary key comparison.

func (*Volume) Chmod added in v0.8.0

func (v *Volume) Chmod(p string, perm os.FileMode) error

Chmod replaces the permission bits (including setuid, setgid and sticky) of the file or folder at path, keeping the file-type bits of fileMode. ctime (attributeModDate) is set to the clock. The root folder is refused, as it is for Chtimes.

func (*Volume) Chown added in v0.8.0

func (v *Volume) Chown(p string, uid, gid uint32) error

Chown sets ownerID and groupID of the file or folder at path (HFSPlusBSDInfo, stored as given). ctime (attributeModDate) is set to the clock; mode, body and the other dates are left alone.

func (*Volume) Chtimes added in v0.8.0

func (v *Volume) Chtimes(p string, atime, mtime time.Time) error

Chtimes sets the access and modification times of path (Apple TN1150, "HFS Plus Dates" and "Catalog File Data Records").

  • mtime is the record's contentModDate and atime its accessDate, each a GMT count of whole seconds since 1904, rounded down.
  • attributeModDate (what Times reports as Ctime) is set to the current time, as POSIX utimes refreshes ctime. createDate (Btime) is untouched.

A zero time.Time leaves that field unchanged; with both zero nothing is touched at all, ctime included. A time outside 1904-01-01 00:00:01 .. 2040-02-06 06:28:15 GMT is refused with a *TimeRangeError (errors.Is ErrTimeOutOfRange) before anything is written. The root folder is not served by Times and is refused here too.

The record is patched in place in its catalog leaf node (the length does not change, so no node is split or merged and the B-tree stays valid), then the image is committed like every other mutation.

func (*Volume) Close

func (v *Volume) Close() error

Close releases the backing handle if Volume opened one.

After Close a writable volume refuses every mutation with ErrClosed, and its cache and edit state are dropped. A store given to OpenWritableAt is not closed (it is the caller's).

func (*Volume) DeleteDir

func (v *Volume) DeleteDir(p string) error

DeleteDir removes the empty directory at path.

func (*Volume) DeleteFile

func (v *Volume) DeleteFile(p string) error

DeleteFile removes the regular file (or symlink) at path.

func (*Volume) Durability added in v0.12.0

func (v *Volume) Durability() Durability

Durability returns the current level.

func (*Volume) EnableJournalWrites added in v0.15.0

func (v *Volume) EnableJournalWrites() error

EnableJournalWrites turns journal writes on (see the comment at the top of journal_writer.go). The writable opens already call it, so a caller needs it only to retry after an open that fell back; it is a no-op when journal writes are already on. It returns ErrJournalWritesUnavailable, with the reason, when the volume is not journaled or its journal fails the checks of section 4; the volume then keeps writing directly, unchanged. It must be called on a writable volume, between mutations, and the journal must be empty (a non-empty one is replayed at open, see replay_write.go).

func (*Volume) FinderInfo added in v0.2.0

func (v *Volume) FinderInfo(p string) ([finderInfoLen]byte, error)

FinderInfo returns the 32 Finder bytes of the entry at p.

p may be "/" for the volume root, which is the case that matters for a volume icon.

func (*Volume) JournalWritesReason added in v0.18.0

func (v *Volume) JournalWritesReason() error

JournalWritesReason reports why the volume writes directly instead of through its journal: nil when journal writes are on, ErrReadOnly for a read-only volume, otherwise an error wrapping ErrJournalWritesUnavailable that says which check failed (not journaled, journal never initialised, a writer that cannot Sync, ...). A non-journaled volume always answers that error: it is the expected case, not a fault.

func (*Volume) Label

func (v *Volume) Label() string

Label returns the volume label (the root folder's name in the catalog).

func (*Volume) ListDir

func (v *Volume) ListDir(path string) ([]filesystem.DirEntry, error)

ListDir enumerates the directory at path.

func (*Volume) MkDir

func (v *Volume) MkDir(p string, perm os.FileMode) error

MkDir creates a directory at path.

func (*Volume) OpenFile added in v0.6.0

func (v *Volume) OpenFile(path string) (filesystem.File, error)

OpenFile opens the regular file at path for positional reads of its data fork. Nothing of the file is read here: each ReadAt reads only the extents of its own window (fork.readAt). A compressed file is ErrUnsupported, as in ReadFile.

func (*Volume) ReadFile

func (v *Volume) ReadFile(path string) ([]byte, error)

ReadFile returns the full contents of the regular file at path.

func (v *Volume) ReadLink(path string) (string, error)

ReadLink returns the target of a symbolic link. HFS+ stores the target as the data-fork contents of a file whose BSD mode marks it S_IFLNK.

func (*Volume) Rename

func (v *Volume) Rename(oldPath, newPath string) error

Rename moves/renames oldPath to newPath. Both parents must exist; newPath must not already exist. The data fork and CNID are preserved (catalog key change + thread parent/name update).

func (*Volume) SetDurability added in v0.12.0

func (v *Volume) SetDurability(d Durability) error

SetDurability selects the level for the following mutations. Leaving a level that holds the volume dirty first settles it: everything written is made durable and, for DurabilityStrict, the clean mark is restored. It returns the error of that settling, and the level is unchanged when it fails. Like every mutator, it must not run concurrently with another mutator, Sync, Batch or Close.

func (*Volume) SetFinderInfo added in v0.2.0

func (v *Volume) SetFinderInfo(p string, info [finderInfoLen]byte) error

SetFinderInfo replaces the 32 Finder bytes of the entry at p.

The record is re-keyed rather than patched in place, because the catalog is a B-tree and its writer owns node layout; delete-then-insert with the same key is how SetLabel already does it.

func (*Volume) SetLabel

func (v *Volume) SetLabel(label string) error

SetLabel renames the volume. The label lives as the root folder's catalog key (parent=1, name=label); SetLabel rewrites that key (and the root thread name) and is reflected by the reader and by macOS.

func (*Volume) Stat

func (v *Volume) Stat(path string) (filesystem.Stat, error)

Stat resolves path and returns mode, size, and the CNID as a pseudo-inode.

func (v *Volume) Symlink(target, linkPath string) error

Symlink creates a symbolic link at linkPath pointing at target. HFS+ stores the target as the data fork of an S_IFLNK file.

func (*Volume) Sync

func (v *Volume) Sync() error

Sync makes what the volume has written durable. Every mutator has already written its changes (data, then B-trees and bitmap, then the alternate and the primary volume header, with an fsync after each stage), so this is a final fsync; it is a no-op for read-only or purely in-memory volumes.

Over a writable backing store that has no Sync of its own (an OpenWritable wa that is a bare io.WriterAt), nothing can be made durable and Sync returns ErrSyncUnsupported instead of nil (it used to say nil, which a fsync-failure latch above could not tell from a real flush). A volume with no backing store (wa nil, the caller reads Bytes) or opened read-only still returns nil. Close, SetDurability and Batch settle through sync, which keeps that supported no-Sync path working.

func (*Volume) Times added in v0.7.0

func (v *Volume) Times(path string) (filesystem.FileTimes, error)

Times returns the times recorded in the catalog record of path.

  • Btime is createDate, Mtime is contentModDate, Ctime is attributeModDate (Mac OS X's BSD layer returns it as st_ctime), Atime is accessDate.
  • All are GMT in catalog records, resolution 1 s. (Only the Volume Header's createDate is local time, per TN1150; it is not used here.)
  • A date of 0 is reported as not recorded: TN1150 says traditional Mac OS leaves accessDate at zero, and attributeModDate "may be treated as reserved" by an implementation.
  • The root folder is synthesised by this reader without its catalog record, so none of its times are recorded. Symbolic links are not followed.

func (*Volume) Truncate

func (v *Volume) Truncate(p string, newSize int64) error

Truncate resizes the regular file at path to newSize bytes. Growing reallocates a larger contiguous run (zero-filled); shrinking reallocates a smaller run. Both rewrite the file's single inline extent.

func (*Volume) VolumeHeader

func (v *Volume) VolumeHeader() *volumeHeader

VolumeHeader exposes the decoded volume header (owned by Volume).

func (*Volume) WriteFile

func (v *Volume) WriteFile(p string, data []byte, perm os.FileMode) error

WriteFile creates or overwrites the regular file at path with data. The data fork is allocated across the volume's free allocation blocks: a single contiguous run when one is available, otherwise multiple fragments. The first numInlineExtents runs are stored in the inline extent descriptors and any remaining runs are inserted into the extents-overflow B-tree, so an arbitrarily fragmented file (more than numInlineExtents extents) round-trips correctly.

Jump to

Keyboard shortcuts

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