Skip to content
/ fat Public
generated from soypat/go-module-template

File Allocation Table implementation (FAT32+exFAT)

License

Notifications You must be signed in to change notification settings

soypat/fat

Repository files navigation

fat

go.dev reference Go Report Card codecov Go sourcegraph License: BSD-3Clause

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!
}

About

File Allocation Table implementation (FAT32+exFAT)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages