---
title: "File system: FAT"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/subsys/fs/fat/
last_updated: 2026-12-03
sdk_version: 4.44
---
# File system: FAT

Source: https://developer.repebble.com/pebbleos/docs/samples/subsys/fs/fat/

This sample creates a FAT file system on the RAM disk, mounts it at `/disk` with the FAT format of the [file system](https://developer.repebble.com/pebbleos/docs/subsystems/fs.md), and works with directories and files on it.

FAT files grow as they are written, so appending is a seek to the end and a write. A rename moves a file between directories. The RAM disk is empty at every boot, so the sample formats it first.

Run:

`pbl samples samples/subsys/fs/fat`

Boards:

every board

## Sources

src/main.c

```
#include \<errno.h\>#include \<stdbool.h\>#include \<stdio.h\>#include \<string.h\>#include \<pbl/drivers/disk/ram.h\>#include \<pbl/fs/fat.h\>#include \<pbl/fs/fs.h\>#define HELLO "/disk/notes/hello world.txt"#define ARCHIVED "/disk/notes/archive/hello.txt"static struct pbl\_fs\_mount s\_mount = {.mnt\_point = "/disk",.ops = &pbl\_fs\_fat\_ops,.fs\_data = (void \*)&pbl\_disk\_ram,};static int prv\_write(const char \*path, const char \*text, bool append) {struct pbl\_fs\_file file;ssize\_t written;int ret;ret = pbl\_fs\_open(&file, path, PBL\_FS\_O\_WRITE | PBL\_FS\_O\_CREATE);if (ret \< 0) {return ret;}if (append) {(void)pbl\_fs\_seek(&file, 0, PBL\_FS\_SEEK\_END);}written = pbl\_fs\_write(&file, text, strlen(text));ret = pbl\_fs\_close(&file);return (written \< 0) ? (int)written : ret;}static int prv\_read(const char \*path, char \*buf, size\_t size) {struct pbl\_fs\_file file;ssize\_t len;int ret;ret = pbl\_fs\_open(&file, path, PBL\_FS\_O\_READ);if (ret \< 0) {return ret;}len = pbl\_fs\_read(&file, buf, size - 1U);buf[(len \> 0) ? len : 0] = '\0';(void)pbl\_fs\_close(&file);return (int)len;}static void prv\_list(const char \*path) {struct pbl\_fs\_dirent entry;struct pbl\_fs\_dir dir;if (pbl\_fs\_opendir(&dir, path) \< 0) {return;}while (pbl\_fs\_readdir(&dir, &entry) == 0 && entry.name[0] != '\0') {if (entry.type == PBL\_FS\_DIR\_ENTRY\_DIR) {printf("fat: listed %s, a directory\n", entry.name);} else {printf("fat: listed %s, %u bytes\n", entry.name, (unsigned int)entry.size);}}(void)pbl\_fs\_closedir(&dir);}int main(void) {struct pbl\_fs\_statvfs vfs;struct pbl\_fs\_dirent entry;char buf[32];int ret;/\* The RAM disk starts empty at every boot. \*/ret = pbl\_fs\_fat\_mkfs(&pbl\_disk\_ram, "SAMPLE");if (ret == 0) {ret = pbl\_fs\_mount(&s\_mount);}if (ret \< 0) {fprintf(stderr, "fat: no file system on the RAM disk (%d)\n", ret);return ret;}ret = pbl\_fs\_statvfs("/disk", &vfs);if (ret \< 0) {fprintf(stderr, "fat: statvfs failed (%d)\n", ret);return ret;}printf("fat: %lu KiB, %lu KiB free\n", vfs.f\_blocks \* vfs.f\_frsize / 1024U,vfs.f\_bfree \* vfs.f\_frsize / 1024U);ret = pbl\_fs\_mkdir("/disk/notes");if (ret == 0) {ret = pbl\_fs\_mkdir("/disk/notes/archive");}if (ret == 0) {ret = prv\_write(HELLO, "Hello, FAT!", false);}if (ret \< 0) {fprintf(stderr, "fat: write failed (%d)\n", ret);return ret;}ret = prv\_read(HELLO, buf, sizeof(buf));printf("fat: read %d bytes: %s\n", ret, buf);/\* Files grow as they are written. \*/ret = prv\_write(HELLO, " Bye.", true);if (ret \< 0) {fprintf(stderr, "fat: append failed (%d)\n", ret);return ret;}(void)prv\_read(HELLO, buf, sizeof(buf));printf("fat: after appending: %s\n", buf);prv\_list("/disk/notes");ret = pbl\_fs\_rename(HELLO, ARCHIVED);if (ret \< 0) {fprintf(stderr, "fat: rename failed (%d)\n", ret);return ret;}printf("fat: renamed, the old name returns %s\n",pbl\_fs\_stat(HELLO, &entry) == -ENOENT ? "-ENOENT" : "an entry");ret = pbl\_fs\_unlink(ARCHIVED);if (ret \< 0) {fprintf(stderr, "fat: unlink failed (%d)\n", ret);return ret;}printf("fat: after removing it, stat returns %s\n",pbl\_fs\_stat(ARCHIVED, &entry) == -ENOENT ? "-ENOENT" : "an entry");(void)pbl\_fs\_unmount(&s\_mount);printf("Sample done\n");return 0;}
```

## Output

```
fat: \d+ KiB, \d+ KiB free
fat: read 11 bytes: Hello, FAT!
fat: after appending: Hello, FAT! Bye\.
fat: listed archive, a directory
fat: listed hello world\.txt, 16 bytes
fat: renamed, the old name returns -ENOENT
fat: after removing it, stat returns -ENOENT
Sample done
```

Generated from PebbleOS [d278268fe](https://github.com/coredevices/pebbleos/commit/d278268fec28d152cdda8d97da06154ada5fbe3a). This page is maintained in the pebbleos repository: [docs/samples/subsys/fs/fat.md](https://github.com/coredevices/pebbleos/blob/d278268fec28d152cdda8d97da06154ada5fbe3a/docs/samples/subsys/fs/fat.md).
