pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
  • Pebble Firmware
    • Overview
    • Playground
    • Application SDK
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Kernel
      • Subsystems
      • Drivers
      • Third-party
      • Rust
      • Benchmarks
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Drivers
    • Subsystems
    • Libraries
    • Third-party
  • Hardware
    • Boards
    • Board Add-ons
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

File system: FAT

This sample creates a FAT file system on the RAM disk, mounts it at /disk with the FAT format of the file system, 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. This page is maintained in the pebbleos repository: docs/samples/subsys/fs/fat.md.

Overview

  • Sources
  • Output