Documentation
¶
Overview ¶
Package detect is a blkid-style, pure-Go (CGO=0) filesystem-type prober and driver-opener registry for the go-filesystems family.
It mirrors the standard library's image.Decode design: the core package depends only on github.com/go-filesystems/interface plus the standard library (and a bounded-read helper). Detect probes on-disk magic signatures and returns a Type; it never imports a concrete driver, so it composes cleanly as a meta-importer despite the drivers' use of a `replace github.com/go-filesystems/interface => ../interface` sibling directive that does not survive transitive importing.
Drivers are wired in at the call site. A consumer registers an Opener for each Type it wants to open — either directly via Register, or by blank-importing a thin per-driver adapter sub-package (for example `import _ "github.com/go-filesystems/detect/fat32reg"`), each of which calls Register in its init function.
t, err := detect.Detect(r, size) // magic probe only
fs, t, err := detect.Open(r, size) // probe then dispatch
fs, t, err := detect.OpenFile("disk.img") // open path, probe, dispatch
Index ¶
- Variables
- func Register(t Type, o Opener)
- func RegisterContainer(c Container, probe ContainerProbe, u Unlocker)
- func Registered(t Type) bool
- type Container
- type ContainerProbe
- type Opener
- type Type
- func Detect(r io.ReaderAt, size int64) (Type, error)
- func Open(r io.ReaderAt, size int64) (filesystem.Filesystem, Type, error)
- func OpenContainer(c Container, r io.ReaderAt, size int64, secret []byte) (filesystem.Filesystem, Type, error)
- func OpenFile(path string) (filesystem.Filesystem, Type, error)
- func OpenFileSecret(path string, secret []byte) (filesystem.Filesystem, Type, error)
- func OpenSecret(r io.ReaderAt, size int64, secret []byte) (filesystem.Filesystem, Type, error)
- type Unlocker
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEncrypted is returned by Open when the image is a registered // encrypted container: it needs a secret (OpenSecret), and probing its // ciphertext for a filesystem would at best find nothing and at worst // misread a header (a BitLocker To Go volume starts like FAT32). ErrEncrypted = errors.New("detect: encrypted container") // ErrNoContainer is returned by OpenContainer for a Container no // adapter registered. ErrNoContainer = errors.New("detect: no unlocker registered for container") )
var ( // ErrUnknown is returned by Detect (and wrapped by Open) when no known // magic signature matches the probed image. ErrUnknown = errors.New("detect: unknown filesystem") // ErrUnsupported is returned by Open when Detect recognised the // filesystem type but no Opener has been registered for it. ErrUnsupported = errors.New("detect: no opener registered for filesystem type") )
Sentinel errors. Callers should test with errors.Is.
Functions ¶
func Register ¶
Register associates an Opener with a filesystem Type. It is idempotent (re-registering the same Type replaces the previous Opener) and safe for concurrent use, so a driver adapter may call it from init without coordinating with others. A nil Opener, or the Unknown type, is rejected to keep the dispatch table well-formed.
func RegisterContainer ¶ added in v0.6.0
func RegisterContainer(c Container, probe ContainerProbe, u Unlocker)
RegisterContainer associates an Unlocker, and optionally a probe, with a Container. Re-registering a Container replaces it; a nil Unlocker or an empty name is ignored. probe may be nil for a container that cannot be recognised from its header: OpenContainer still reaches it, Open and OpenSecret never do.
func Registered ¶
Registered reports whether an Opener is currently registered for t.
Types ¶
type Container ¶ added in v0.6.0
type Container string
Container identifies an encryption layer that sits under a filesystem: a LUKS or BitLocker volume, a VeraCrypt container. Containers are wired in like filesystem drivers: the core knows none of them, and a caller who wants them imports an adapter (github.com/go-filesystems/detect/fdereg) that registers them with RegisterContainer. A caller who never opens encrypted images links no cryptography.
type ContainerProbe ¶ added in v0.6.0
ContainerProbe reports whether r holds the container's header. It must only read, and only a bounded amount.
type Opener ¶
type Opener func(r io.ReaderAt, size int64) (filesystem.Filesystem, error)
Opener constructs a filesystem.Filesystem from an image. It mirrors the concrete drivers' Open(rs io.ReaderAt, size int64) entry points. An Opener is responsible for its own validation; Open only calls it after Detect has already matched the corresponding Type.
type Type ¶
type Type string
Type is a filesystem-type identifier as reported by Detect. The set of recognised values is closed and matches the go-filesystems driver family.
const ( Unknown Type = "unknown" FAT12 Type = "fat12" FAT16 Type = "fat16" FAT32 Type = "fat32" ExFAT Type = "exfat" NTFS Type = "ntfs" Ext4 Type = "ext4" XFS Type = "xfs" Btrfs Type = "btrfs" ZFS Type = "zfs" APFS Type = "apfs" HFSPlus Type = "hfsplus" ISO9660 Type = "iso9660" SquashFS Type = "squashfs" UFS Type = "ufs" )
The recognised filesystem types. Unknown is the zero value returned alongside ErrUnknown when no signature matches.
func Detect ¶
Detect probes the on-disk magic signatures of r and returns the matching Type. size is the logical size of the image in bytes; pass a negative value if unknown (probes are then bounded only by maxProbeRead and the reader's own extent). Detection is read-only and performs a small number of bounded reads. When no signature matches, Detect returns (Unknown, ErrUnknown).
Probes run most-specific first so that nested or ambiguous layouts never produce a false positive: container/whole-disk magics (btrfs, squashfs, xfs, ext, apfs, ufs, zfs) are checked before the FAT-family boot-sector heuristics, and FAT32 is only reported when both its boot signature and its "FAT32 " filesystem-type label are present. FAT12 and FAT16 have no such label to trust; they are recognised from the BIOS parameter block by the count of data clusters (see fatbpb.go) and probed first.
func Open ¶
func Open(r io.ReaderAt, size int64) (filesystem.Filesystem, Type, error)
Open detects the filesystem type of r and dispatches to the Opener registered for that type. The detected Type is always returned, even on error, so callers can report it.
- If r is a registered encrypted container (RegisterContainer), Open returns (nil, Unknown, ErrEncrypted) wrapped with the container's name: open it with OpenSecret.
- If no signature matches, Open returns (nil, Unknown, ErrUnknown).
- If a type is detected but no Opener is registered for it, Open returns (nil, <type>, ErrUnsupported) wrapped with the type name.
- Otherwise the Opener's result is returned with the detected Type.
func OpenContainer ¶ added in v0.6.0
func OpenContainer(c Container, r io.ReaderAt, size int64, secret []byte) (filesystem.Filesystem, Type, error)
OpenContainer unlocks r as the named container with secret and opens the filesystem inside. It is how a container without a recognisable header (VeraCrypt) is opened: the caller says what it is.
func OpenFile ¶
func OpenFile(path string) (filesystem.Filesystem, Type, error)
OpenFile opens the file at path, detects its filesystem type and dispatches to the registered Opener. The underlying *os.File is closed only on error; on success it is retained by the returned Filesystem (whose Close must be called by the caller) — drivers read lazily through the io.ReaderAt, so the descriptor must outlive the returned value.
func OpenFileSecret ¶ added in v0.6.0
func OpenFileSecret(path string, secret []byte) (filesystem.Filesystem, Type, error)
OpenFileSecret is OpenFile for an image that may be an encrypted container: it opens it with OpenSecret.
func OpenSecret ¶ added in v0.6.0
func OpenSecret(r io.ReaderAt, size int64, secret []byte) (filesystem.Filesystem, Type, error)
OpenSecret opens the filesystem in r, unlocking it first when r is a registered encrypted container. The secret is a passphrase, a recovery key or a key file's contents, as the container defines it. An image that is not a container is opened as Open would, and the secret is unused.
type Unlocker ¶ added in v0.6.0
type Unlocker func(r io.ReaderAt, size int64, secret []byte) (plain io.ReaderAt, plainSize int64, err error)
Unlocker opens a container with secret and returns the plaintext device: a reader whose offset 0 is the first byte of the filesystem inside, and its size. Whatever the container's own offset conventions (LUKS counts from its payload, BitLocker maps the whole volume), the adapter hands drivers a device that starts at the filesystem.
