TARFS 0.1.5
Read-only TAR filesystem for ESP32
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1/*
2 * TARFS - Immutable (read-only) filesystem for embedded systems.
3 *
4 * Copyright (c) 2026 Viacheslav Logunov
5 * SPDX-License-Identifier: MMIT
6 *
7 * Author:
8 * Viacheslav Logunov <vvb333007@gmail.com>
9 *
10 * Project:
11 * https://github.com/vvb333007/tarfs
12 *
13 * @file file.h
14 * @brief Public file API
15 */
16
17#pragma once
18
42
43#include <stdint.h>
44#include <stdlib.h>
45#include <stdio.h>
46#include <assert.h>
47#include <sys/stat.h>
48#include <sys/utime.h>
49#include "config.h"
50
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
63struct tarfs_fp {
64
65 uintptr_t fp_vaddr;
66 uint32_t fp_pos;
67 size_t fp_size;
68 int fp_idx;
69};
70
71
72/* VFS handlers below are normally invoked through a VFS.
73 *
74 * On systems without VFS support, applications should call the tarfs_(),
75 * tarf_() and tard_() APIs directly. Unlike their POSIX counterparts, these
76 * functions take an additional first argument: an index of the mounted
77 * filesystem
78 *
79 * Typical usage without a VFS:
80 *
81 * 1. Mount the filesystem and save its identifier.
82 *
83 * int ctx = tarfs_mount(...);
84 * if (ctx < 0)
85 * exit_error("failed to mount filesystem");
86 *
87 * 2. Use the context pointer in subsequent filesystem calls.
88 *
89 * int fd1 = tarf_open((void *)ctx, "/home/file1.txt", flags);
90 * int fd2 = tarf_open((void *)ctx, "/home/file2.txt", flags);
91 * int fd3 = tarf_open((void *)ctx, "/home/file3.txt", flags);
92 */
93
94
95
119int tarf_access(void* ctx, const char *path, int amode);
120
121
122/* close()
123 * FS has at least 1 extra ref, because of open()
124 *
125 * close() is not thread-safe. It is not as bad as it seems: no crashes, no sigsegv.
126 * Just DO NOT close() files being read() at the same time by another thread. Or, if you do,
127 * do not open another file immediately :)
128 *
129 * This is because of:
130 *
131 * Closing a file descriptor while another thread is executing read()
132 * (or any operation using the same descriptor) is undefined behaviour.
133 *
134 * Since file descriptor slots are immediately reusable, a concurrent
135 * open() may repurpose the same slot for another file before the running
136 * operation completes, causing it to access the newly opened file instead
137 * of the original one.
138 *
139 */
140int tarf_close(void* ctx, int fd);
141
167int tarf_open(void* ctx, const char * path, int flags, int mode);
168
177ssize_t tarf_read(void* ctx, int fd, void *dst, size_t size);
178
187ssize_t tarf_pread(void* ctx, int fd, void *dst, size_t size, off_t offset);
188
199off_t tarf_lseek(void* ctx, int fd, off_t offset, int whence);
200
215int tarf_fstat(void* ctx, int fd, struct stat * st);
216
229int tarf_fsync(void* ctx, int fd);
230
246int tarf_fcntl(void *ctx, int fd, int cmd, int arg);
247
272int tarf_ioctl(void *ctx, int fd, int cmd, va_list args);
273
298int tarf_stat(void* ctx, const char *path, struct stat *st);
299
300
301
323int tarf_dupfd(void *ctx, int fd);
324
360void *tarf_mmap(void *ctx, void *addr, size_t length, int prot, int flags, int fd, off_t offset);
361
362
377int tarf_munmap(void *ctx, void *addr, size_t length);
378
398ssize_t tarf_sendfile(void *ctx, int sock, int fd, off_t *offset, size_t count);
399
400#ifdef __cplusplus
401};
402#endif
403
404/* Compile-time sanity checks. */
405_Static_assert(TARFS_MAX_FDS > 0 && TARFS_MAX_FDS <= 32, "Code review is required");
int tarf_stat(void *ctx, const char *path, struct stat *st)
Retrieve file status information.
Definition file.c:790
off_t tarf_lseek(void *ctx, int fd, off_t offset, int whence)
lseek()
Definition file.c:533
ssize_t tarf_sendfile(void *ctx, int sock, int fd, off_t *offset, size_t count)
Send TARFS file to a socket.
Definition file.c:926
int tarf_open(void *ctx, const char *path, int flags, int mode)
Open a TARFS file or directory.
Definition file.c:252
int tarf_close(void *ctx, int fd)
Definition file.c:445
int tarf_access(void *ctx, const char *path, int amode)
Check the accessibility of a file or directory in the TarFS filesystem.
Definition file.c:210
int tarf_fstat(void *ctx, int fd, struct stat *st)
Get file status information.
Definition file.c:589
ssize_t tarf_pread(void *ctx, int fd, void *dst, size_t size, off_t offset)
Read data from an open TARFS file at a specified offset.
Definition file.c:464
int tarf_ioctl(void *ctx, int fd, int cmd, va_list args)
Perform TARFS-specific I/O control operations.
Definition file.c:740
ssize_t tarf_read(void *ctx, int fd, void *dst, size_t size)
Read data from an open TARFS file.
Definition file.c:512
void * tarf_mmap(void *ctx, void *addr, size_t length, int prot, int flags, int fd, off_t offset)
Map a file into the process address space.
Definition file.c:846
int tarf_dupfd(void *ctx, int fd)
Create an independent duplicate of a file descriptor.
Definition file.c:817
int tarf_fcntl(void *ctx, int fd, int cmd, int arg)
Perform file descriptor control operations.
Definition file.c:716
int tarf_munmap(void *ctx, void *addr, size_t length)
Remove a previously created memory mapping.
Definition file.c:898
int tarf_fsync(void *ctx, int fd)
Synchronize file contents.
Definition file.c:702
#define TARFS_MAX_FDS
Definition fs.h:30
TARFS File API: tarf_open(), tarf_close(), tarf_read(), tarf_pread(), tarf_lseek(),...
Definition file.h:63
uint32_t fp_pos
Definition file.h:66
uintptr_t fp_vaddr
Definition file.h:65
int fp_idx
Definition file.h:68
size_t fp_size
Definition file.h:67
const char mode[8]
Definition tar.h:2
const char size[12]
Definition tar.h:5