fat

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const DirectoryEntrySize = 32

The size in bytes of a single directory entry.

View Source
const FirstCluster = 2

The first cluster that can really hold user data is always 2

View Source
const LastLongEntryMask = 0x40

Mask applied to the ord of the last long entry.

Variables

This section is empty.

Functions

func DecodeDirectoryEntry

func DecodeDirectoryEntry(d *Directory, entries []*DirectoryClusterEntry) (*DirectoryEntry, []*DirectoryClusterEntry, error)

DecodeDirectoryEntry takes a list of entries, decodes the next full DirectoryEntry, and returns the newly created entry, the remaining entries, and an error, if there was one.

func DecodeVolumeLabel added in v0.0.6

func DecodeVolumeLabel(device ffs.BlockDevice, fatType FATType) (string, error)

func FATEntryCount

func FATEntryCount(bs *BootSectorCommon) uint32

FATEntryCount returns the number of entries per fat for the given boot sector.

func Fatal added in v0.0.5

func Fatal(err error) error

func Fatalf added in v0.0.5

func Fatalf(format string, args ...interface{}) error

func FormatSuperFloppy

func FormatSuperFloppy(device ffs.BlockDevice, config *SuperFloppyConfig) error

Formats an ffs.BlockDevice with the "super floppy" format according to the given configuration. The "super floppy" standard means that the device will be formatted so that it does not contain a partition table. Instead, the entire device holds a single FAT file system.

Types

type BootSectorCommon

type BootSectorCommon struct {
	OEMName             string
	BytesPerSector      uint16
	SectorsPerCluster   uint8
	ReservedSectorCount uint16
	NumFATs             uint8
	RootEntryCount      uint16
	TotalSectors        uint32
	Media               MediaType
	SectorsPerFat       uint32
	SectorsPerTrack     uint16
	NumHeads            uint16
}

func DecodeBootSector

func DecodeBootSector(device ffs.BlockDevice) (*BootSectorCommon, error)

DecodeBootSector takes a BlockDevice and decodes the FAT boot sector from it.

func (*BootSectorCommon) Bytes

func (b *BootSectorCommon) Bytes() ([]byte, error)

func (*BootSectorCommon) BytesPerCluster

func (b *BootSectorCommon) BytesPerCluster() uint32

BytesPerCluster returns the number of bytes per cluster.

func (*BootSectorCommon) ClusterOffset

func (b *BootSectorCommon) ClusterOffset(n int) uint32

ClusterOffset returns the offset of the data section of a particular cluster.

func (*BootSectorCommon) DataOffset

func (b *BootSectorCommon) DataOffset() uint32

DataOffset returns the offset of the data section of the disk.

func (*BootSectorCommon) FATOffset

func (b *BootSectorCommon) FATOffset(n int) int

FATOffset returns the offset in bytes for the given index of the FAT

func (*BootSectorCommon) FATType

func (b *BootSectorCommon) FATType() FATType

Calculates the FAT type that this boot sector represents.

func (*BootSectorCommon) RootDirOffset

func (b *BootSectorCommon) RootDirOffset() int

RootDirOffset returns the byte offset when the root directory entries for FAT12/16 filesystems start. NOTE: This is absolutely useless for FAT32 because the root directory is just the beginning of the data region.

type BootSectorFat16

type BootSectorFat16 struct {
	BootSectorCommon

	DriveNumber         uint8
	VolumeID            uint32
	VolumeLabel         string
	FileSystemTypeLabel string
}

BootSectorFat16 is the BootSector for FAT12 and FAT16 filesystems. It contains the common fields to all FAT filesystems and also some unique.

func (*BootSectorFat16) Bytes

func (b *BootSectorFat16) Bytes() ([]byte, error)

type BootSectorFat32

type BootSectorFat32 struct {
	BootSectorCommon

	RootCluster         uint32
	FSInfoSector        uint16
	BackupBootSector    uint16
	DriveNumber         uint8
	VolumeID            uint32
	VolumeLabel         string
	FileSystemTypeLabel string
}

func (*BootSectorFat32) Bytes

func (b *BootSectorFat32) Bytes() ([]byte, error)

type ClusterChain

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

func (*ClusterChain) Read

func (c *ClusterChain) Read(p []byte) (n int, err error)

func (*ClusterChain) Write

func (c *ClusterChain) Write(p []byte) (n int, err error)

Write will write to the cluster chain, expanding it if necessary.

type Directory

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

Directory implements ffs.Directory and is used to interface with a directory on a FAT filesystem.

func (*Directory) AddDirectory

func (d *Directory) AddDirectory(name string) (ffs.DirectoryEntry, error)

func (*Directory) AddFile

func (d *Directory) AddFile(name string) (ffs.DirectoryEntry, error)

func (*Directory) Entries

func (d *Directory) Entries() []ffs.DirectoryEntry

func (*Directory) Entry

func (d *Directory) Entry(name string) ffs.DirectoryEntry

type DirectoryCluster

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

DirectoryCluster represents a cluster on the disk that contains entries/contents.

func DecodeDirectoryCluster

func DecodeDirectoryCluster(startCluster uint32, device ffs.BlockDevice, fat *FAT) (*DirectoryCluster, error)

func DecodeFAT16RootDirectoryCluster

func DecodeFAT16RootDirectoryCluster(device ffs.BlockDevice, bs *BootSectorCommon) (*DirectoryCluster, error)

DecodeFAT16RootDirectory decodes the FAT16 root directory structure from the device.

func DecodeFAT32RootDirectoryCluster

func DecodeFAT32RootDirectoryCluster(device ffs.BlockDevice, fat *FAT) (*DirectoryCluster, error)

DecodeFAT16RootDirectory decodes the FAT32 oroot directory structure from the device.

func NewDirectoryCluster

func NewDirectoryCluster(start uint32, parent uint32, t time.Time) *DirectoryCluster

func NewFat16RootDirectoryCluster

func NewFat16RootDirectoryCluster(bs *BootSectorCommon, label string) (*DirectoryCluster, error)

NewFat16RootDirectory creates a new DirectoryCluster that is meant only to be the root directory of a FAT12/FAT16 filesystem.

func (*DirectoryCluster) Bytes

func (d *DirectoryCluster) Bytes() []byte

Bytes returns the on-disk byte data for this directory structure.

func (*DirectoryCluster) WriteToDevice

func (d *DirectoryCluster) WriteToDevice(device ffs.BlockDevice, fat *FAT) error

WriteToDevice writes the cluster to the device.

type DirectoryClusterEntry

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

DirectoryClusterEntry is a single 32-byte entry that is part of the chain of entries in a directory cluster.

func DecodeDirectoryClusterEntry

func DecodeDirectoryClusterEntry(data []byte) (*DirectoryClusterEntry, error)

DecodeDirectoryClusterEntry decodes a single directory entry in the Directory structure.

func NewLongDirectoryClusterEntry

func NewLongDirectoryClusterEntry(name string, shortName string) ([]*DirectoryClusterEntry, error)

NewLongDirectoryClusterEntry returns the series of directory cluster entries that need to be written for a long directory entry. This list of entries does NOT contain the short name entry.

func (*DirectoryClusterEntry) Bytes

func (d *DirectoryClusterEntry) Bytes() []byte

Bytes returns the on-disk byte data for this directory entry.

func (*DirectoryClusterEntry) IsLong

func (d *DirectoryClusterEntry) IsLong() bool

IsLong returns true if this is a long entry.

func (*DirectoryClusterEntry) IsVolumeId

func (d *DirectoryClusterEntry) IsVolumeId() bool

type DirectoryEntry

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

DirectoryEntry implements ffs.DirectoryEntry and represents a single file/folder within a directory in a FAT filesystem. Note that there may be more than one underlying directory entry data structure on the disk to account for long filenames.

func (*DirectoryEntry) Attr added in v0.0.5

func (d *DirectoryEntry) Attr() ffs.DirectoryAttr

func (*DirectoryEntry) Dir

func (d *DirectoryEntry) Dir() (ffs.Directory, error)

func (*DirectoryEntry) File

func (d *DirectoryEntry) File() (ffs.File, error)

func (*DirectoryEntry) IsDir

func (d *DirectoryEntry) IsDir() bool

func (*DirectoryEntry) IsHidden added in v0.0.5

func (d *DirectoryEntry) IsHidden() bool

func (*DirectoryEntry) IsReadOnly added in v0.0.5

func (d *DirectoryEntry) IsReadOnly() bool

func (*DirectoryEntry) IsSystem added in v0.0.5

func (d *DirectoryEntry) IsSystem() bool

func (*DirectoryEntry) IsVolumeId added in v0.0.5

func (d *DirectoryEntry) IsVolumeId() bool

func (*DirectoryEntry) Name

func (d *DirectoryEntry) Name() string

func (*DirectoryEntry) SetAttr added in v0.0.5

func (d *DirectoryEntry) SetAttr(attr ffs.DirectoryAttr, state bool) error

func (*DirectoryEntry) SetHidden added in v0.0.5

func (d *DirectoryEntry) SetHidden(state bool) error

func (*DirectoryEntry) SetReadOnly added in v0.0.5

func (d *DirectoryEntry) SetReadOnly(state bool) error

func (*DirectoryEntry) SetSystem added in v0.0.5

func (d *DirectoryEntry) SetSystem(state bool) error

func (*DirectoryEntry) ShortName

func (d *DirectoryEntry) ShortName() string

type FAT

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

FAT is the actual file allocation table data structure that is stored on disk to describe the various clusters on the disk.

func DecodeFAT

func DecodeFAT(device ffs.BlockDevice, bs *BootSectorCommon, n int) (*FAT, error)

func NewFAT

func NewFAT(bs *BootSectorCommon) (*FAT, error)

NewFAT creates a new FAT data structure, properly initialized.

func (*FAT) AllocChain

func (f *FAT) AllocChain() (uint32, error)

func (*FAT) Bytes

func (f *FAT) Bytes() []byte

Bytes returns the raw bytes for the FAT that should be written to the block device.

func (*FAT) Chain

func (f *FAT) Chain(start uint32) []uint32

Chain returns the chain of clusters starting at a certain cluster.

func (*FAT) ResizeChain

func (f *FAT) ResizeChain(start uint32, length int) ([]uint32, error)

ResizeChain takes a given cluster number and resizes the chain to the given length. It returns the new chain of clusters.

func (*FAT) WriteToDevice

func (f *FAT) WriteToDevice(device ffs.BlockDevice) error

type FATType

type FATType uint8

FATType is a simple enum of the available FAT filesystem types.

const (
	FAT12 FATType = iota
	FAT16
	FAT32
)

func TypeForDevice

func TypeForDevice(device ffs.BlockDevice) FATType

TypeForDevice determines the usable FAT type based solely on size information about the block device.

type File

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

func (*File) Close added in v0.0.5

func (f *File) Close() error

func (*File) Read

func (f *File) Read(p []byte) (n int, err error)

func (*File) Write

func (f *File) Write(p []byte) (n int, err error)

type FileSystem

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

FileSystem is the implementation of ffs.FileSystem that can read a FAT filesystem.

func New

func New(device ffs.BlockDevice) (*FileSystem, error)

New returns a new FileSystem for accessing a previously created FAT filesystem.

func (*FileSystem) FATType added in v0.0.6

func (f *FileSystem) FATType() (int, error)

func (*FileSystem) Info added in v0.0.6

func (f *FileSystem) Info() (map[string]any, error)

func (*FileSystem) OEMName added in v0.0.6

func (f *FileSystem) OEMName() (string, error)

func (*FileSystem) RootDir

func (f *FileSystem) RootDir() (ffs.Directory, error)

func (*FileSystem) VolumeLabel added in v0.0.6

func (f *FileSystem) VolumeLabel() (string, error)

type MediaType

type MediaType uint8
const MediaFixed MediaType = 0xF8

The standard value for "fixed", non-removable media, directly from the FAT specification.

type SuperFloppyConfig

type SuperFloppyConfig struct {
	// The type of FAT filesystem to use.
	FATType FATType

	// The label of the drive. Defaults to "NONAME"
	Label string

	// The OEM name for the FAT filesystem. Defaults to "gofs" if not set.
	OEMName string
}

SuperFloppyConfig is the configuration for various properties of a new super floppy formatted block device. Once this configuration is used to format a device, it must not be modified.

Jump to

Keyboard shortcuts

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