fat

package module
v0.0.0-...-6180576 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: BSD-3-Clause Imports: 16 Imported by: 0

README

fat

go.dev reference Go Report Card codecov Go sourcegraph

A File Allocation Table implementation written in Go. Intended for use in embedded systems with SD cards, USBs, MMC devices and also usable with an in-RAM representation. Inspired by FatFs.

This is a Work in Progress.

How to install package with newer versions of Go (+1.16):

go mod download github.com/soypat/fat@latest
Basic usage example

The following example does the following:

  1. Mounts a FAT filesystem to the fat.FS type.
  2. Creates a new empty file called newfile.txt, replacing any existing file.
  3. Writes Hello, World! to that file.
  4. Closes the file to synchronize pending changes to the FAT filesystem.
  5. Opens the file in read mode and reads all of it's contents and prints them to standard output.
package main

import "github.com/soypat/fat"

func main() {
	// device could be an SD card, RAM, or anything that implements the BlockDevice interface.
	device := NewFATDevice()
	var fs fat.FS
	err := fs.Mount(device, device.BlockSize(), fat.ModeRW)
	if err != nil {
		panic(err)
	}
	var file fat.File
	err = fs.OpenFile(&file, "newfile.txt", fat.ModeCreateAlways|fat.ModeWrite)
	if err != nil {
		panic(err)
	}

	_, err = file.Write([]byte("Hello, World!"))
	if err != nil {
		panic(err)
	}
	err = file.Close()
	if err != nil {
		panic(err)
	}

	// Read back the file:
	err = fs.OpenFile(&file, "newfile.txt", fat.ModeRead)
	if err != nil {
		panic(err)
	}
	data, err := io.ReadAll(&file)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(data))
	file.Close()
    // Output: Hello, World!
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockDevice

type BlockDevice interface {
	ReadBlocks(dst []byte, startBlock int64) (int, error)
	WriteBlocks(data []byte, startBlock int64) (int, error)
	EraseBlocks(startBlock, numBlocks int64) error
}

type Dir

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

Dir represents an open FAT directory.

func (*Dir) ForEachFile

func (dp *Dir) ForEachFile(callback func(*FileInfo) error) error

ForEachFile calls the callback function for each file in the directory.

type FS

type FS struct {
	// contains filtered or unexported fields
}
Example (Basic_usage)
package main

import (
	"fmt"
	"io"

	"github.com/soypat/fat"
)

func main() {
	// device could be an SD card, RAM, or anything that implements the BlockDevice interface.
	device := fat.DefaultFATByteBlocks(32000)
	var fs fat.FS
	err := fs.Mount(device, device.BlockSize(), fat.ModeRW)
	if err != nil {
		panic(err)
	}
	var file fat.File
	err = fs.OpenFile(&file, "newfile.txt", fat.ModeCreateAlways|fat.ModeWrite)
	if err != nil {
		panic(err)
	}

	_, err = file.Write([]byte("Hello, World!"))
	if err != nil {
		panic(err)
	}
	err = file.Close()
	if err != nil {
		panic(err)
	}

	// Read back the file:
	err = fs.OpenFile(&file, "newfile.txt", fat.ModeRead)
	if err != nil {
		panic(err)
	}
	data, err := io.ReadAll(&file)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(data))
	file.Close()
}
Output:

Hello, World!

func (*FS) Mount

func (fsys *FS) Mount(bd BlockDevice, blockSize int, mode Mode) error

Mount mounts the FAT file system on the given block device and sector size. It immediately invalidates previously open files and directories pointing to the same FS. Mode should be ModeRead, ModeWrite, or both.

func (*FS) OpenDir

func (fsys *FS) OpenDir(dp *Dir, path string) error

OpenDir opens the named directory for reading.

func (*FS) OpenFile

func (fsys *FS) OpenFile(fp *File, path string, mode Mode) error

OpenFile opens the named file for reading or writing, depending on the mode. The path must be absolute (starting with a slash) and must not contain any elements that are "." or "..".

type File

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

func (*File) Close

func (fp *File) Close() error

Close closes the file and syncs any unwritten data to the underlying device.

func (*File) Mode

func (fp *File) Mode() Mode

Mode returns the lowest 2 bits of the file's permission (read, write or both).

func (*File) Read

func (fp *File) Read(buf []byte) (int, error)

Read reads up to len(buf) bytes from the File. It implements the io.Reader interface.

func (*File) Sync

func (fp *File) Sync() error

Sync commits the current contents of the file to the filesystem immediately.

func (*File) Write

func (fp *File) Write(buf []byte) (int, error)

Write writes len(buf) bytes to the File. It implements the io.Writer interface.

type FileInfo

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

func (*FileInfo) AlternateName

func (finfo *FileInfo) AlternateName() string

AlternateName returns the alternate name of the file.

func (*FileInfo) IsDir

func (finfo *FileInfo) IsDir() bool

IsDir returns true if the file is a directory.

func (*FileInfo) ModTime

func (finfo *FileInfo) ModTime() time.Time

ModTime returns the modification time of the file.

func (*FileInfo) Name

func (finfo *FileInfo) Name() string

Name returns the name of the file.

func (*FileInfo) Size

func (finfo *FileInfo) Size() int64

Size returns the size of the file in bytes.

type Format

type Format uint8
const (
	FormatFAT12 Format
	FormatFAT16
	FormatFAT32
	FormatExFAT
)

type FormatConfig

type FormatConfig struct {
	Label string
	// ClusterSize is the size of a FAT cluster in blocks.
	ClusterSize int
	// Format selects the FAT format to use. If not specified will use FAT32.
	Format Format
}

type Formatter

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

func (*Formatter) Format

func (f *Formatter) Format(bd BlockDevice, blocksize, fsSizeInBlocks int, cfg FormatConfig) error

type Mode

type Mode uint8

Mode represents the file access mode used in Open.

const (
	ModeRead  Mode = Mode(faRead)
	ModeWrite Mode = Mode(faWrite)
	ModeRW    Mode = ModeRead | ModeWrite

	ModeCreateNew    Mode = Mode(faCreateNew)
	ModeCreateAlways Mode = Mode(faCreateAlways)
	ModeOpenExisting Mode = Mode(faOpenExisting)
	ModeOpenAppend   Mode = Mode(faOpenAppend)
)

File access modes for calling Open.

Directories

Path Synopsis
internal
gpt
mbr
package mbr implements a Master Boot Record parser and writer.
package mbr implements a Master Boot Record parser and writer.

Jump to

Keyboard shortcuts

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