---
title: "Files"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/subsys/fs/files/
last_updated: 2026-11-18
sdk_version: 4.44
---
# Files

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

This sample mounts PFS, the [file system](https://developer.repebble.com/pebbleos/docs/subsystems/fs.md) of the external flash, at the root, and works with files on it.

PFS files have a fixed size: `pbl_fs_truncate()` gives it after creating the file, and a write past it fails. A file opened with `PBL_FS_O_REPLACE` is a new version that replaces the old one only once closed, so a reset in the middle leaves either. The sample counts its runs in such a file, read back at the start of each run; a watch on another file reports it closed after writing.

The flash may hold other files, so the sample lists only its own.

Run:

`pbl samples samples/subsys/fs/files`

Boards:

every board

## Sources

src/main.c

```
#include \<errno.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<string.h\>#include \<pbl/fs/fs.h\>#include \<pbl/fs/pfs.h\>#define GREETING "/sample-greeting"#define BOOTS "/sample-boots"#define PREFIX "sample-"/\* PFS, the file system on the external flash, mounted at the root. \*/static struct pbl\_fs\_mount s\_mount = {.mnt\_point = "/", .ops = &pbl\_fs\_pfs\_ops};static unsigned int s\_closed;static void prv\_closed(void \*user\_data) {s\_closed++;}/\* PFS files have a fixed size, given with pbl\_fs\_truncate() before the first write. \*/static int prv\_write\_file(const char \*path, const void \*data, size\_t len) {struct pbl\_fs\_file file;ssize\_t written;int ret;/\* The new version replaces the old one only once closed: a reset leaves either. \*/ret = pbl\_fs\_open(&file, path, PBL\_FS\_O\_WRITE | PBL\_FS\_O\_CREATE | PBL\_FS\_O\_REPLACE);if (ret \< 0) {return ret;}ret = pbl\_fs\_truncate(&file, (off\_t)len);if (ret == 0) {written = pbl\_fs\_write(&file, data, len);if (written \< 0) {ret = (int)written;}}(void)pbl\_fs\_close(&file);return ret;}static int prv\_count\_boots(void) {struct pbl\_fs\_file file;uint32\_t boots = 0;int ret;ret = pbl\_fs\_open(&file, BOOTS, PBL\_FS\_O\_READ);if (ret == 0) {if (pbl\_fs\_read(&file, &boots, sizeof(boots)) != sizeof(boots)) {boots = 0;}(void)pbl\_fs\_close(&file);} else if (ret != -ENOENT) {return ret;}boots++;printf("fs: run %u of this sample on this flash\n", (unsigned int)boots);return prv\_write\_file(BOOTS, &boots, sizeof(boots));}int main(void) {static const char greeting[] = "Hello, flash!";struct pbl\_fs\_statvfs vfs;struct pbl\_fs\_dirent entry;struct pbl\_fs\_file file;struct pbl\_fs\_dir dir;char buf[32] = {0};ssize\_t len;int ret;ret = pbl\_fs\_mount(&s\_mount);if (ret \< 0) {fprintf(stderr, "fs: mount failed (%d)\n", ret);return ret;}ret = pbl\_fs\_statvfs("/", &vfs);if (ret \< 0) {fprintf(stderr, "fs: statvfs failed (%d)\n", ret);return ret;}printf("fs: %lu KiB, %lu KiB free\n", vfs.f\_blocks \* vfs.f\_frsize / 1024U,vfs.f\_bfree \* vfs.f\_frsize / 1024U);ret = prv\_count\_boots();if (ret \< 0) {fprintf(stderr, "fs: boot count not saved (%d)\n", ret);return ret;}struct pbl\_fs\_watch \*watch = pbl\_fs\_watch\_add(GREETING, PBL\_FS\_WATCH\_CLOSED, prv\_closed, nullptr);ret = prv\_write\_file(GREETING, greeting, strlen(greeting));if (ret \< 0) {fprintf(stderr, "fs: write failed (%d)\n", ret);return ret;}printf("fs: watch saw the file closed after writing: %s\n", s\_closed == 1 ? "yes" : "no");pbl\_fs\_watch\_remove(watch);ret = pbl\_fs\_open(&file, GREETING, PBL\_FS\_O\_READ);if (ret \< 0) {fprintf(stderr, "fs: open failed (%d)\n", ret);return ret;}len = pbl\_fs\_read(&file, buf, sizeof(buf) - 1);printf("fs: read %d of %d bytes: %s\n", (int)len, (int)pbl\_fs\_size(&file), buf);memset(buf, 0, sizeof(buf));(void)pbl\_fs\_seek(&file, 7, PBL\_FS\_SEEK\_SET);len = pbl\_fs\_read(&file, buf, sizeof(buf) - 1);printf("fs: from offset 7: %s\n", len \> 0 ? buf : "nothing");(void)pbl\_fs\_close(&file);/\* Writing past the end of a fixed-size file fails. \*/ret = pbl\_fs\_open(&file, GREETING, PBL\_FS\_O\_WRITE);if (ret \< 0) {fprintf(stderr, "fs: open failed (%d)\n", ret);return ret;}(void)pbl\_fs\_seek(&file, 0, PBL\_FS\_SEEK\_END);printf("fs: one more byte: %s\n", pbl\_fs\_write(&file, "!", 1) == -EFBIG ? "-EFBIG" : "fits");(void)pbl\_fs\_close(&file);/\* Entries come in no particular order, and the flash may hold other files. \*/ret = pbl\_fs\_opendir(&dir, "/");if (ret \< 0) {fprintf(stderr, "fs: opendir failed (%d)\n", ret);return ret;}while (pbl\_fs\_readdir(&dir, &entry) == 0 && entry.name[0] != '\0') {if (strncmp(entry.name, PREFIX, strlen(PREFIX)) == 0) {printf("fs: listed %s, %u bytes\n", entry.name, (unsigned int)entry.size);}}(void)pbl\_fs\_closedir(&dir);ret = pbl\_fs\_unlink(GREETING);if (ret \< 0) {fprintf(stderr, "fs: unlink failed (%d)\n", ret);return ret;}printf("fs: after removing it, stat returns %s\n",pbl\_fs\_stat(GREETING, &entry) == -ENOENT ? "-ENOENT" : "an entry");printf("Sample done\n");return 0;}
```

## Output

```
fs: \d+ KiB, \d+ KiB free
fs: run \d+ of this sample on this flash
fs: watch saw the file closed after writing: yes
fs: read 13 of 13 bytes: Hello, flash!
fs: from offset 7: flash!
fs: one more byte: -EFBIG
fs: listed sample-greeting, 13 bytes
fs: after removing it, stat returns -ENOENT
Sample done
```

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