Documentation
¶
Overview ¶
Package fat12 provides utilities to interact with, manipulate and create a FAT12 filesystem on a block device or disk image. It also exports the shared BPB types and the FATTable interface used by the fat16 package.
References:
https://en.wikipedia.org/wiki/Design_of_the_FAT_file_system https://www.cs.fsu.edu/~cop4610t/assignments/project3/spec/fatspec.pdf https://wiki.osdev.org/FAT
Index ¶
- Constants
- type Directory
- type DiskRange
- type Dos20BPB
- type Dos40EBPB
- type Dos331BPB
- type FATTable
- type File
- func (fl *File) Close() error
- func (fl *File) GetClusterChain() ([]uint32, error)
- func (fl *File) GetDiskRanges() ([]DiskRange, error)
- func (de File) Info() (iofs.FileInfo, error)
- func (de File) IsDir() bool
- func (fl *File) IsHidden() bool
- func (fl *File) IsReadOnly() bool
- func (fl *File) IsSystem() bool
- func (de File) Name() string
- func (fl *File) Read(b []byte) (int, error)
- func (fl *File) Seek(offset int64, whence int) (int64, error)
- func (fl *File) SetHidden(on bool) error
- func (fl *File) SetReadOnly(on bool) error
- func (fl *File) SetSystem(on bool) error
- func (fl *File) Stat() (iofs.FileInfo, error)
- func (de File) Type() iofs.FileMode
- func (fl *File) Write(p []byte) (int, error)
- type FileInfo
- type FileSystem
- func (fs *FileSystem) Backend() backend.Storage
- func (fs *FileSystem) BytesPerCluster() int
- func (fs *FileSystem) Chmod(_ string, _ os.FileMode) error
- func (fs *FileSystem) Chown(_ string, _, _ int) error
- func (fs *FileSystem) Chtimes(p string, ctime, atime, mtime time.Time) error
- func (fs *FileSystem) Close() error
- func (fs *FileSystem) DataStart() uint32
- func (fs *FileSystem) Equal(a *FileSystem) bool
- func (fs *FileSystem) GetArchiveBit(p string) (bool, error)
- func (fs *FileSystem) Label() string
- func (fs *FileSystem) Link(_, _ string) error
- func (fs *FileSystem) Mkdir(p string) error
- func (fs *FileSystem) Mknod(_ string, _ uint32, _ int) error
- func (fs *FileSystem) Open(p string) (iofs.File, error)
- func (fs *FileSystem) OpenFile(p string, flag int) (filesystem.File, error)
- func (fs *FileSystem) ReadDir(p string) ([]iofs.DirEntry, error)
- func (fs *FileSystem) ReadFile(name string) ([]byte, error)
- func (fs *FileSystem) Remove(pathname string) error
- func (fs *FileSystem) Rename(oldpath, newpath string) error
- func (fs *FileSystem) SetArchiveBit(p string, set bool) error
- func (fs *FileSystem) SetLabel(volumeLabel string) error
- func (fs *FileSystem) SetRootDirLabel(volumeLabel string) error
- func (fs *FileSystem) Start() int64
- func (fs *FileSystem) Stat(name string) (iofs.FileInfo, error)
- func (fs *FileSystem) Symlink(_, _ string) error
- func (fs *FileSystem) Type() filesystem.Type
- func (fs *FileSystem) WriteBootSector() error
- func (fs *FileSystem) WriteFat() error
- type MsdosMediaType
- type SectorSize
Constants ¶
const ( // KB represents one KB KB int64 = 1024 // MB represents one MB MB int64 = 1024 * KB // GB represents one GB GB int64 = 1024 * MB // Fat12MaxSize is the maximum size of a FAT12 filesystem in bytes. // FAT12 supports at most 4084 clusters; at 64 sectors/cluster × 512 bytes = 32 KB/cluster // that gives ~128 MB, but in practice FAT12 is used only on small volumes. Fat12MaxSize int64 = 128 * MB // Fat16MaxSize is the maximum size of a FAT16 filesystem in bytes (2 GB). Fat16MaxSize int64 = 2 * GB )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Directory ¶
type Directory struct {
// contains filtered or unexported fields
}
Directory represents a single directory in a FAT12/FAT16 filesystem
type Dos20BPB ¶
type Dos20BPB struct {
BytesPerSector SectorSize // always 512 for FAT12/16
SectorsPerCluster uint8
ReservedSectors uint16
FatCount uint8
RootDirectoryEntries uint16 // non-zero for FAT12/FAT16; zero for FAT32
TotalSectors uint16 // non-zero if total sectors fit in 16 bits; else see Dos331BPB
MediaType uint8
SectorsPerFat uint16 // for FAT12/16; zero for FAT32 (see dos71EBPB)
}
Dos20BPB is a DOS 2.0 BIOS Parameter Block structure, shared by FAT12, FAT16, and FAT32.
func Dos20BPBFromBytes ¶
Dos20BPBFromBytes reads the DOS 2.0 BPB from exactly 13 bytes.
type Dos40EBPB ¶
type Dos40EBPB struct {
Dos331BPB *Dos331BPB
DriveNumber uint8
ReservedFlags uint8
ExtBootSignature uint8 // 0x28 (short) or 0x29 (long, includes label+type)
VolumeSerialNumber uint32
VolumeLabel string // 11 bytes, padded with spaces; only present when ExtBootSignature == 0x29
FileSystemType string // 8 bytes, e.g. "FAT12 " or "FAT16 "; only when 0x29
}
Dos40EBPB is the Extended BIOS Parameter Block used by FAT12 and FAT16. It wraps the DOS 3.31 BPB and adds drive identification, volume serial number, volume label, and filesystem type string.
func Dos40EBPBFromBytes ¶
Dos40EBPBFromBytes parses a Dos40EBPB from a byte slice. The slice must be at least 26 bytes (short form) or 51 bytes (long form). Returns the parsed struct and the number of bytes consumed.
func Dos40EBPBFromBytesOnly ¶
Dos40EBPBFromBytesOnly is a convenience wrapper around Dos40EBPBFromBytes that discards the consumed-byte count. Useful when the caller does not need to know where the BPB ends (e.g. when only reading, not writing).
func (*Dos40EBPB) ToBytes ¶
ToBytes serialises the Dos40EBPB. Returns an error if the volume label or filesystem type strings are too long or contain non-ASCII characters.
func (*Dos40EBPB) TotalSectors ¶
TotalSectors returns the effective total sector count, preferring the 32-bit field.
type Dos331BPB ¶
type Dos331BPB struct {
Dos20BPB *Dos20BPB
SectorsPerTrack uint16
Heads uint16
HiddenSectors uint32
TotalSectors32 uint32 // used when TotalSectors in Dos20BPB is 0
}
Dos331BPB is the DOS 3.31 BIOS Parameter Block, shared by FAT12, FAT16, and FAT32.
func Dos331BPBFromBytes ¶
Dos331BPBFromBytes reads the DOS 3.31 BPB from exactly 25 bytes.
type FATTable ¶
type FATTable interface {
// ClusterValue returns the value stored for cluster n.
ClusterValue(n uint32) uint32
// SetCluster sets the value for cluster n.
SetCluster(n uint32, val uint32)
// IsEOC reports whether val is an end-of-chain marker.
IsEOC(val uint32) bool
// EOCMarker returns the canonical end-of-chain value to write.
EOCMarker() uint32
// UnusedMarker returns the value that marks a free cluster.
UnusedMarker() uint32
// MaxCluster returns the highest valid cluster index.
MaxCluster() uint32
// FATID returns the media-descriptor word stored at FAT[0].
FATID() uint32
// RootDirCluster returns the cluster that holds the root directory.
// For FAT12/16 this is always 2 (though the root dir is stored in
// a fixed region, not via the cluster chain).
RootDirCluster() uint32
// Size returns the on-disk size of one FAT copy in bytes.
Size() uint32
// FromBytes populates the table from raw FAT bytes read from disk.
FromBytes(b []byte)
// Bytes serialises the table to raw FAT bytes ready to write to disk.
Bytes() []byte
}
FATTable is the interface that abstracts on-disk FAT entry encoding. fat12 provides a 12-bit implementation; fat16 and fat32 each provide their own implementation in their respective packages. All implementations use uint32 as the in-memory cluster value for uniformity.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File represents a single file in a FAT32 filesystem
func (*File) GetClusterChain ¶
Get the full cluster chain of the File. Getting this file system internal info can be beneficial for some low-level operations, such as: - Performing secure erase. - Detecting file fragmentation. - Passing Disk locations to a different tool that can work with it.
func (*File) GetDiskRanges ¶
Get the disk ranges occupied by the File. Returns an array of disk ranges, where each entry is a contiguous area on disk. This information is similar to that returned by GetClusterChain, just in a different format, directly returning disk ranges instead of FAT clusters.
func (*File) IsReadOnly ¶
func (*File) Read ¶
Read reads up to len(b) bytes from the File. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF reads from the last known offset in the file from last read or write and increments the offset by the number of bytes read. Use Seek() to set at a particular point
func (*File) SetReadOnly ¶
func (*File) Write ¶
Write writes len(b) bytes to the File. It returns the number of bytes written and an error, if any. returns a non-nil error when n != len(b) writes to the last known offset in the file from last read or write and increments the offset by the number of bytes read. Use Seek() to set at a particular point
type FileInfo ¶
type FileInfo struct {
// contains filtered or unexported fields
}
FileInfo represents the information for an individual file it fulfills os.FileInfo interface
func (FileInfo) Name ¶
Name base name of the file
will return the long name of the file. If none exists, returns the shortname and extension
type FileSystem ¶
type FileSystem struct {
// WriteBootSectorFn, when non-nil, is called by WriteBootSector instead of
// the default fat12/16 boot-sector writer. FAT32 uses this to write the
// dos71EBPB-format boot sector (including backup sector).
WriteBootSectorFn func() error
// AfterWriteFAT, when non-nil, is called at the end of WriteFat. FAT32
// uses this to flush the FSInformationSector after every FAT change.
AfterWriteFAT func() error
// contains filtered or unexported fields
}
FileSystem implements filesystem.FileSystem for FAT12 (and, via embedding, FAT16 and FAT32).
func Create ¶
func Create(b backend.Storage, size, start, blocksize int64, volumeLabel string, reproducible bool) (*FileSystem, error)
Create creates a FAT12 filesystem.
func NewFileSystem ¶
func NewFileSystem( b backend.Storage, bpb *Dos40EBPB, tbl FATTable, dataStart uint32, bytesPerCluster int, size, start int64, rootDirOffset int64, rootDirMaxEntries int, fatPrimaryStart, fatSecondaryStart uint64, ) *FileSystem
NewFileSystem constructs a FileSystem with caller-supplied table and layout parameters. Called by fat16 and fat32 to reuse all high-level methods.
bpb may be nil when the caller (fat32) manages its own boot-sector format and supplies WriteBootSectorFn / AfterWriteFAT hooks after construction.
func Read ¶
func Read(b backend.Storage, size, start, blocksize int64) (*FileSystem, error)
Read reads a FAT12 filesystem from the backend. Returns an error if the image is not a valid FAT12 filesystem (e.g. it is FAT16/FAT32), so that disk.GetFilesystem() can fall through to the next candidate.
func (*FileSystem) Backend ¶
func (fs *FileSystem) Backend() backend.Storage
Backend returns the storage backend. Used by fat32 to write FSIS.
func (*FileSystem) BytesPerCluster ¶
func (fs *FileSystem) BytesPerCluster() int
BytesPerCluster returns the cluster size in bytes.
func (*FileSystem) Chtimes ¶
func (fs *FileSystem) Chtimes(p string, ctime, atime, mtime time.Time) error
func (*FileSystem) Close ¶
func (fs *FileSystem) Close() error
func (*FileSystem) DataStart ¶
func (fs *FileSystem) DataStart() uint32
DataStart returns the byte offset of the first data cluster.
func (*FileSystem) Equal ¶
func (fs *FileSystem) Equal(a *FileSystem) bool
Equal compares two FileSystems.
func (*FileSystem) GetArchiveBit ¶ added in v1.9.1
func (fs *FileSystem) GetArchiveBit(p string) (bool, error)
GetArchiveBit returns the current state of the FAT archive attribute.
func (*FileSystem) Label ¶
func (fs *FileSystem) Label() string
func (*FileSystem) Link ¶
func (fs *FileSystem) Link(_, _ string) error
func (*FileSystem) Mkdir ¶
func (fs *FileSystem) Mkdir(p string) error
func (*FileSystem) OpenFile ¶
func (fs *FileSystem) OpenFile(p string, flag int) (filesystem.File, error)
func (*FileSystem) Remove ¶
func (fs *FileSystem) Remove(pathname string) error
func (*FileSystem) Rename ¶
func (fs *FileSystem) Rename(oldpath, newpath string) error
func (*FileSystem) SetArchiveBit ¶ added in v1.9.1
func (fs *FileSystem) SetArchiveBit(p string, set bool) error
SetArchiveBit sets or clears the FAT archive attribute on the named file or directory.
func (*FileSystem) SetLabel ¶
func (fs *FileSystem) SetLabel(volumeLabel string) error
func (*FileSystem) SetRootDirLabel ¶
func (fs *FileSystem) SetRootDirLabel(volumeLabel string) error
SetRootDirLabel updates the volume-label directory entry in the root directory without touching the boot sector. Used by fat32, which manages its own boot sector format, to reuse the root-directory update logic.
func (*FileSystem) Start ¶
func (fs *FileSystem) Start() int64
Start returns the filesystem start offset within the backend. Used by fat32.
func (*FileSystem) Symlink ¶
func (fs *FileSystem) Symlink(_, _ string) error
func (*FileSystem) Type ¶
func (fs *FileSystem) Type() filesystem.Type
Type returns filesystem.TypeFat12. fat16.FileSystem overrides this.
func (*FileSystem) WriteBootSector ¶
func (fs *FileSystem) WriteBootSector() error
WriteBootSector writes the boot sector to disk. When WriteBootSectorFn is set (by fat32), that function is called instead of the default fat12/16 writer.
func (*FileSystem) WriteFat ¶
func (fs *FileSystem) WriteFat() error
WriteFat writes both FAT copies to disk. When AfterWriteFAT is set (by fat32), it is called afterwards to flush the FSInformationSector.
type MsdosMediaType ¶
type MsdosMediaType uint8
MsdosMediaType is the (mostly unused) media type.
const ( // Media8InchDrDos for single-sided 250KB DR-DOS disks Media8InchDrDos MsdosMediaType = 0xe5 // Media525InchTandy for 5.25 inch floppy disks for Tandy Media525InchTandy MsdosMediaType = 0xed // MediaCustomPartitionsDrDos for non-standard custom DR-DOS partitions MediaCustomPartitionsDrDos MsdosMediaType = 0xee // MediaCustomSuperFloppyDrDos for non-standard custom superfloppy disks for DR-DOS MediaCustomSuperFloppyDrDos MsdosMediaType = 0xef // Media35Inch for standard 1.44MB and 2.88MB 3.5 inch floppy disks Media35Inch MsdosMediaType = 0xf0 // MediaDoubleDensityAltos for double-density floppy disks for Altos only MediaDoubleDensityAltos MsdosMediaType = 0xf4 // MediaFixedDiskAltos for fixed disk 1.95MB for Altos only MediaFixedDiskAltos MsdosMediaType = 0xf5 // MediaFixedDisk for standard fixed disks MediaFixedDisk MsdosMediaType = 0xf8 )
type SectorSize ¶
type SectorSize uint16
SectorSize indicates what the sector size in bytes is
const ( // SectorSize512 is a sector size of 512 bytes SectorSize512 SectorSize = 512 )