Documentation
¶
Index ¶
- Constants
- Variables
- func CopyResized(dst ReaderWriterAt, size int64, src filesystem.Filesystem, cfg CopyConfig) (filesystem.Filesystem, error)
- func Format(path string, sizeBytes int64, cfg FormatConfig) (filesystem.Filesystem, error)
- func FormatImage(rw ReaderWriterAt, sizeBytes int64, cfg FormatConfig) (filesystem.Filesystem, error)
- func Open(imagePath string, partIndex int) (filesystem.Filesystem, error)
- func OpenReader(r io.ReaderAt, size int64) (filesystem.Filesystem, error)
- type CopyConfig
- type FATType
- type FormatConfig
- type Info
- type ReaderWriterAt
- type TimeRangeError
Constants ¶
const MaxLabelLen = 11
MaxLabelLen is the on-disk size of the FAT32 volume label (BS_VolLab, boot-sector offset 71, 11 bytes). The historic FAT convention is to space-pad shorter labels and to keep the label uppercase ASCII; we preserve case and pad with spaces.
Variables ¶
var ErrCopyNoFit = errors.New("fat32: the tree does not fit the new image")
ErrCopyNoFit is returned (wrapped) when the tree does not fit the new image.
var ErrCopyRootFull = errors.New("fat32: the root directory needs more entries than the new image's fixed root has")
ErrCopyRootFull is returned (wrapped) when the root directory of the tree needs more entries than the fixed root of a FAT12/16 image has.
var ErrGrowTooSmall = errors.New("fat32: grow too small to journal the layout move")
ErrGrowTooSmall is returned by Grow when the grow is too small to move the layout crash-safely: the record needs a free sector past the moved data.
var ErrNotSupported = fmt.Errorf("fat32: the format has no POSIX mode bits or owners: %w", errors.ErrUnsupported)
ErrNotSupported is what Chmod and Chown return: FAT records no owner and no POSIX mode bits (only the read-only, hidden, system and archive attributes), so there is nothing to store. It wraps errors.ErrUnsupported.
var ErrReadOnlyReader = errors.New("fat32: this filesystem was opened over a reader that cannot be written")
ErrReadOnlyReader is what a write gets when the filesystem was opened over something that can only be read.
var ErrResizeFAT = errors.New("fat32: this FAT12/FAT16 resize is not supported")
ErrResizeFAT is the parent of every refusal of a FAT12/FAT16 resize; test it with errors.Is. A FAT12/16 volume can be resized only while it keeps its FAT type (see ErrResizeType); its FAT grows with it when it must.
var ErrResizeFATFull = fmt.Errorf("%w: the FAT is too small for the new size", ErrResizeFAT)
ErrResizeFATFull was returned by Grow (v0.8.0, v0.9.0) when the FAT had no room for the extra clusters. Since v0.10.0 Grow makes the FAT bigger in place and this error is never returned; it stays so that callers that test for it still compile.
var ErrResizeType = fmt.Errorf("%w: the FAT type would change", ErrResizeFAT)
ErrResizeType is returned when the new size would put the data-cluster count on the other side of a FAT type threshold (4085, 65525): the volume would change type, which needs a reformat, not a resize.
var ErrSyncUnsupported = fmt.Errorf("fat32: 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 (an in-memory image, a caller's bare io.WriterAt). It wraps errors.ErrUnsupported, the way fsync(2) answers EINVAL for an object that cannot be synchronised: the caller learns that durability was NOT established instead of being told it was. A caller that only wants the data written, not durable, may treat it as success with errors.Is(err, ErrSyncUnsupported).
var ErrTimeOutOfRange = errors.New("fat32: time outside the FAT range 1980-2107")
ErrTimeOutOfRange is what Chtimes returns, wrapped in a *TimeRangeError, for a time the FAT date word cannot hold. FAT has a 7-bit year counted from 1980, so the representable local years are 1980..2107; the driver never clamps.
Functions ¶
func CopyResized ¶ added in v0.9.0
func CopyResized(dst ReaderWriterAt, size int64, src filesystem.Filesystem, cfg CopyConfig) (filesystem.Filesystem, error)
CopyResized formats the first size bytes of dst as a new FAT volume and copies the whole tree of src into it: names (long ones too), directories, contents, attributes, timestamps and the volume label. It is the resize that FAT12/16 refuses in place (ErrResizeType): another FAT type needs a new image. (A bigger FAT of the same type is grown in place since v0.10.0.)
src must be a FAT volume opened by this package; it is only read, so it is unchanged whether CopyResized succeeds or fails. cfg.Type picks the FAT type of the new image (the size decides when it is zero; an impossible combination is refused before anything is written). dst is truncated to size when it can be; otherwise it must already hold size bytes.
Refusals happen before dst is written when they can be known: a tree that needs more clusters than the image has (ErrCopyNoFit), a root directory needing more entries than a FAT12/16 fixed root has (ErrCopyRootFull, which names both counts). If a write fails half-way, the error is returned and the boot sector of dst is cleared so that the half-written image is not taken for a volume.
On success the new filesystem is open over dst, as FormatImage returns it; it does not close dst.
func Format ¶
func Format(path string, sizeBytes int64, cfg FormatConfig) (filesystem.Filesystem, error)
Format creates a new FAT32 filesystem in the file at path. The file is created (or truncated) and formatted. sizeBytes must be a multiple of the cluster size (4096) and large enough to hold the reserved area, two FATs, and at least one data cluster.
On success the newly formatted filesystem is opened and returned; the caller must Close it when done.
func FormatImage ¶ added in v0.5.1
func FormatImage(rw ReaderWriterAt, sizeBytes int64, cfg FormatConfig) (filesystem.Filesystem, error)
FormatImage creates a new FAT32 filesystem in the first sizeBytes of rw and opens it, with the same layout and size rules as Format.
It is Format without a path: for a caller that has no host file to give -- a WebAssembly page building an image in memory, a test, a partition inside a larger image. rw must already hold at least sizeBytes; FormatImage never grows it.
Unlike Format, which writes a file it has just truncated, FormatImage cannot assume rw reads back as zeros. It therefore writes zeros over every region a FAT32 reader interprets -- the reserved sectors, both FATs and the root directory cluster -- so a reused buffer holding an older volume does not leak entries into the new one. Data clusters are left as they are: they are free in the new FAT, and nothing reads a free cluster.
The returned filesystem reads and writes through rw (see OpenReader); it does not close rw.
func Open ¶
func Open(imagePath string, partIndex int) (filesystem.Filesystem, error)
Open opens imagePath, optionally selecting a partition, and parses the FAT32 BPB.
func OpenReader ¶ added in v0.4.0
func OpenReader(r io.ReaderAt, size int64) (filesystem.Filesystem, error)
OpenReader opens the FAT32 filesystem that starts at offset zero of r.
It exists so this driver fits the one shape the rest of go-filesystems agrees on: github.com/go-filesystems/detect's Opener is func(io.ReaderAt, int64) (filesystem.Filesystem, error), and until now this driver could only be opened from a PATH. A caller with an image already open -- a partition inside a disk image, a section of a larger file, an image in memory -- had to write it to a temporary file first, and detect.Register could not be given this driver at all.
There is no partition index: r is expected to begin at the filesystem, which is what detect has just established by reading the magic there. Open remains the way to look through a partition table.
WRITES need an io.WriterAt. A reader that is only a reader gives a filesystem that serves every read and refuses every write with ErrReadOnlyReader -- rather than one that accepts a write and loses it.
The reader is NOT closed by Close: the caller opened it and still owns it. That is the same division as iso9660, squashfs, hfsplus and ufs, whose reader-based constructors have always worked this way.
Types ¶
type CopyConfig ¶ added in v0.9.0
type CopyConfig struct {
// Type forces the FAT type of the new image: FAT12, FAT16 or FAT32. The zero
// value lets the size decide, as mkfs.fat does. A size that cannot be
// formatted as the forced type is refused.
Type FATType
// RootEntries is BPB_RootEntCnt of a FAT12/16 image: a multiple of 16, 512 when
// zero. It is ignored for FAT32, whose root directory is a cluster chain.
RootEntries uint16
// Label is the volume label (11 bytes at most); the source's when empty.
Label string
// VolumeID is the serial number; random when zero.
VolumeID uint32
}
CopyConfig holds the optional parameters of CopyResized.
type FATType ¶ added in v0.6.0
type FATType uint8
FATType is the width of one FAT entry: the variant of the format.
Bibliography: Microsoft, "FAT: General Overview of On-Disk Format" (fatgen103) section "FAT Type Determination" is the rule for FAT12 versus FAT16, and Linux fs/fat/inode.c fat_fill_super (opened 2026-11-01, torvalds/linux master) is the reference implementation of it:
- the FAT32 layout is recognised by structure: BPB_FATSz16 == 0 and BPB_FATSz32 != 0 ("Must be FAT32");
- otherwise the CLUSTER COUNT decides, never a label: total_clusters > MAX_FAT12 (4084) is FAT16, else FAT12;
- the count is (total_sectors - data_start) / sec_per_clus, with data_start = reserved + fats*fat_length + rootdir_sectors, then clamped to what one FAT can address;
- fatent.c: end of chain is >= 0xFF8 / 0xFFF8 / 0x0FFFFFF8, and the bad cluster mark is 0xFF7 / 0xFFF7 / 0x0FFFFFF7.
The three FAT variants. The zero value is treated as FAT32 so that an Info built by hand (as the tests do) keeps meaning what it always meant.
type FormatConfig ¶
type FormatConfig struct {
// Label is the volume label stored in the BPB (trimmed to 11 bytes).
Label string
// VolumeID is the 32-bit volume serial number. A random value is generated when zero.
VolumeID uint32
}
FormatConfig holds optional parameters for Format. All fields are optional; sensible defaults are used when left at their zero value.
type Info ¶
type Info struct {
// Type is the FAT variant, decided by the data-cluster count (see FATType).
Type FATType
// RootEntries is BPB_RootEntCnt: the capacity of the fixed FAT12/16 root
// directory, 0 on FAT32.
RootEntries uint16
// DataClusters is the number of data clusters of a FAT12/16 volume (0 on
// FAT32, where the older TotalSectors-based bound is used).
DataClusters uint32
OEMName string
BytesPerSector uint16
SectorsPerCluster uint8
ReservedSectors uint16
FATCount uint8
TotalSectors uint32
FATSize uint32
HiddenSectors uint32
RootCluster uint32
FSInfoSector uint16
BackupBootSector uint16
DriveNumber uint8
VolumeID uint32
VolumeLabel string
TypeLabel string
}
Info holds the fields decoded from the FAT BIOS parameter block. Despite the package name it describes FAT12, FAT16 and FAT32 volumes (see Type).
func (Info) ClusterSize ¶
ClusterSize returns the size of one allocation cluster in bytes.
func (Info) DataOffset ¶
DataOffset returns the absolute byte offset of the first data cluster.
func (Info) RootDirOffset ¶
RootDirOffset returns the absolute byte offset of the root directory: its cluster on FAT32, the fixed region after the FATs on FAT12/16.
func (Info) RootDirSectors ¶ added in v0.6.0
RootDirSectors is ((BPB_RootEntCnt*32) + (BPB_BytsPerSec-1)) / BPB_BytsPerSec.
type ReaderWriterAt ¶ added in v0.5.1
ReaderWriterAt is the image FormatImage formats: anything that can be read and written at an offset, such as a buffer in memory.
type TimeRangeError ¶ added in v0.13.0
TimeRangeError names the field and the value Chtimes refused.
func (*TimeRangeError) Error ¶ added in v0.13.0
func (e *TimeRangeError) Error() string
func (*TimeRangeError) Unwrap ¶ added in v0.13.0
func (e *TimeRangeError) Unwrap() error
