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 ¶
Constants ¶
This section is empty.
Variables ¶
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 Registered ¶
Registered reports whether an Opener is currently registered for t.
Types ¶
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" 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.
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 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 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.