TARFS
0.1.5
Read-only TAR filesystem for ESP32
Toggle main menu visibility
Loading...
Searching...
No Matches
os.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 os.h
14
* @brief Platform porting layer API
15
*/
16
17
18
/* Platform abstraction layer.
19
*
20
* The only mandatory operation is mapping the TAR image into a contiguous
21
* read-only memory region. Mutex support is optional and may be implemented
22
* as no-ops on single-threaded systems. Likewise, unmap may be a no-op if
23
* the mapping has no associated resources.
24
*/
25
26
#pragma once
27
28
#include <stdint.h>
29
#include <stddef.h>
30
#include <stdbool.h>
31
#include <string.h>
32
33
#include "
config.h
"
34
35
#ifdef __cplusplus
36
extern
"C"
{
37
#endif
38
/* Platform API */
39
40
/* TARFS uses a single recursive mutex to protect its internal state.
41
*
42
* On single-threaded systems, or systems without suitable synchronization
43
* primitives, all three functions may be implemented as no-ops.
44
*/
45
void
tarfs_os_init
();
46
void
tarfs_os_acquire_mutex
();
47
void
tarfs_os_release_mutex
();
48
49
/* Map a TAR filesystem image into the process address space.
50
*
51
* TARFS expects the archive to be accessible as a contiguous, read-only
52
* memory region. How this is achieved is platform-specific:
53
*
54
* - memory-mapped flash (e.g. RP2040 XIP),
55
* - runtime memory mapping (e.g. Linux, ESP-IDF),
56
* - linker-embedded binary image,
57
* - or any other mechanism that provides a linear memory view.
58
*
59
* The returned pointer must remain valid until tarfs_os_unmap_tarfile()
60
* is called.
61
*/
62
void
const
*
tarfs_os_map_tarfile
(
const
char
*
name
,
void
**os_handle_out,
size_t
*size_out);
63
void
tarfs_os_unmap_tarfile
(
void
*os_handle,
const
void
*ptr,
size_t
size
);
64
65
/* Register or unregister TARFS with the platform VFS.
66
*
67
* TARFS driver calls this as a final part of the nount() process. Registration function
68
* is responsible for registering FS handlers in host VFS
69
*
70
* On systems providing a Virtual Filesystem layer, these functions should
71
* expose TARFS through the native file API (open(), read(), stat(), etc.).
72
* See os_esp32.c for the sample implementation
73
*
74
* On systems without a VFS, both functions may be implemented as `{ return true; }`
75
* Applications can then access the filesystem through the TARFS API directly.
76
* See os_cygwin.c for the sample implementation
77
*
78
* @param prefix Path prefix (a mount point)
79
* @param context A raw pointer to the driver filesystem descriptor. This pointer will be passed to
80
* each handler function (e.g. to tarfs_open(), tarfs_read() etc)
81
* @return
82
* tarfs_os_register_fs():
83
* true - registration succeeded.
84
* false - registration failed. Mounting is aborted and all resources
85
* allocated during the mount procedure are released.
86
*
87
* tarfs_os_unregister_fs():
88
* true - unregistration succeeded and TARFS may immediately release
89
* all associated resources.
90
*
91
* false - unregistration could not complete because the platform may
92
* still issue delayed callbacks into this filesystem. TARFS
93
* will keep critical data structures alive to avoid use-after-
94
* free crashes. This intentionally trades a memory leak for
95
* safety.
96
*/
97
bool
tarfs_os_register_fs
(
const
char
*
prefix
,
void
*context);
98
bool
tarfs_os_unregister_fs
(
const
char
*
prefix
);
99
111
size_t
tarfs_os_mp_maxlen
();
112
123
void
*
tarfs_os_malloc
(
size_t
size
);
124
void
tarfs_os_free
(
void
*buffer);
125
129
#if CONFIG_TARFS_HAVE_OPTIMIZED_MEMCPY
130
/* rely on the .S file; see os_esp32s3.S for sample implementation */
131
void
*
tarfs_os_memcpy
(
void
*dst,
const
void
*src,
size_t
len);
132
#else
133
static
inline
void
*
tarfs_os_memcpy
(
void
*dst,
const
void
*src,
size_t
len) {
134
return
memcpy(dst, src, len);
135
}
136
#endif
137
138
139
#ifdef __cplusplus
140
};
141
#endif
config.h
tarfs_os_map_tarfile
void const * tarfs_os_map_tarfile(const char *name, void **os_handle_out, size_t *size_out)
Map a ESP32 flash partition to a virtual address space.
Definition
os_esp32.c:222
tarfs_os_mp_maxlen
size_t tarfs_os_mp_maxlen()
Return the maximum mount point name length supported by the platform.
Definition
os_esp32.c:175
tarfs_os_unmap_tarfile
void tarfs_os_unmap_tarfile(void *os_handle, const void *ptr, size_t size)
Opposite of tarfs_os_map_tarfile().
Definition
os_esp32.c:279
tarfs_os_register_fs
bool tarfs_os_register_fs(const char *prefix, void *context)
Tell the VFS that TARFS is now handle all the paths starting from 'prefix'.
Definition
os_esp32.c:319
tarfs_os_malloc
void * tarfs_os_malloc(size_t size)
Memory allocation backend.
Definition
os_esp32.c:187
tarfs_os_memcpy
static void * tarfs_os_memcpy(void *dst, const void *src, size_t len)
read() uses memcpy(), which can be optimized on many architectures
Definition
os.h:133
tarfs_os_free
void tarfs_os_free(void *buffer)
Definition
os_esp32.c:207
tarfs_os_unregister_fs
bool tarfs_os_unregister_fs(const char *prefix)
Tell the VFS that path 'prefix' is not handled by tarfs anymore.
Definition
os_esp32.c:306
tarfs_os_release_mutex
void tarfs_os_release_mutex()
Unlock access to s_tarfs[] table and to the s_numfs counter only.
Definition
os_esp32.c:166
tarfs_os_init
void tarfs_os_init()
Create a recursive sync object.
Definition
os_esp32.c:147
tarfs_os_acquire_mutex
void tarfs_os_acquire_mutex()
Lock access to s_tarfs[] table and to the s_numfs counter only.
Definition
os_esp32.c:156
name
const char name[100]
Definition
tar.h:1
prefix
const char prefix[155]
Definition
tar.h:16
size
const char size[12]
Definition
tar.h:5
src
os.h
Generated by
1.17.0