void IRAM_ATTR _free_r(struct _reent *r, void* ptr)
{
- return vPortFree(ptr);
+ vPortFree(ptr);
}
void* IRAM_ATTR _realloc_r(struct _reent *r, void* ptr, size_t size)
{
- void* new_chunk;
- if (size == 0) {
- if (ptr) {
- vPortFree(ptr);
- }
- return NULL;
- }
+ void* new_chunk;
+ if (size == 0) {
+ if (ptr) {
+ vPortFree(ptr);
+ }
+ return NULL;
+ }
- new_chunk = pvPortMalloc(size);
- if (new_chunk && ptr) {
- memcpy(new_chunk, ptr, size);
- vPortFree(ptr);
- }
- // realloc behaviour: don't free original chunk if alloc failed
- return new_chunk;
+ new_chunk = pvPortMalloc(size);
+ if (new_chunk && ptr) {
+ memcpy(new_chunk, ptr, size);
+ vPortFree(ptr);
+ }
+ // realloc behaviour: don't free original chunk if alloc failed
+ return new_chunk;
}
void* IRAM_ATTR _calloc_r(struct _reent *r, size_t count, size_t size)
void* result = pvPortMalloc(count * size);
if (result)
{
- memset(result, 0, count * size);
+ memset(result, 0, count * size);
}
return result;
}
This component allows C library functions, such as fopen and fprintf, to work with FS drivers. At high level, each FS driver is associated with some path prefix. When one of C library functions needs to open a file, VFS component searches for the FS driver associated with the file's path, and forwards the call to that driver. VFS also forwards read, write, and other calls for the given file to the same FS driver.
-For example, one can register a FAT filesystem driver with ``/fat`` prefix, and call ``fopen("/fat/file.txt", "w")``. VFS component will the call ``open`` function of FAT driver and pass ``/file.txt`` argument to it (and appropriate mode flags). All subsequent calls to C library functions for the returned ``FILE*`` stream will also be forwarded to the FAT driver.
+For example, one can register a FAT filesystem driver with ``/fat`` prefix, and call ``fopen("/fat/file.txt", "w")``. VFS component will then call ``open`` function of FAT driver and pass ``/file.txt`` argument to it (and appropriate mode flags). All subsequent calls to C library functions for the returned ``FILE*`` stream will also be forwarded to the FAT driver.
FS registration
---------------
ESP_ERROR_CHECK(esp_vfs_register("/data", &myfs, NULL));
-Depending on the way FS driver declares it's APIs, either ``read``, ``write``, etc., or ``read_p``, ``write_p``, etc. should be used.
+Depending on the way FS driver declares its APIs, either ``read``, ``write``, etc., or ``read_p``, ``write_p``, etc. should be used.
Case 1: API functions are declared without an extra context pointer (FS driver is a singleton)::
// In definition of esp_vfs_t:
.flags = ESP_VFS_FLAG_DEFAULT,
.write = &myfs_write,
- // ... other member initialized
+ // ... other members initialized
// When registering FS, context pointer (third argument) is NULL:
ESP_ERROR_CHECK(esp_vfs_register("/data", &myfs, NULL));
// In definition of esp_vfs_t:
.flags = ESP_VFS_FLAG_CONTEXT_PTR,
.write_p = &myfs_write,
- // ... other member initialized
+ // ... other members initialized
// When registering FS, pass the FS context pointer into the third argument
// (hypothetical myfs_mount function is used for illustrative purposes)
- myfs_t* myfs_inst1 = myfs_mount(partition1->offset, parition1->size);
+ myfs_t* myfs_inst1 = myfs_mount(partition1->offset, partition1->size);
ESP_ERROR_CHECK(esp_vfs_register("/data1", &myfs, myfs_inst1));
// Can register another instance:
- myfs_t* myfs_inst2 = myfs_mount(partition2->offset, parition2->size);
+ myfs_t* myfs_inst2 = myfs_mount(partition2->offset, partition2->size);
ESP_ERROR_CHECK(esp_vfs_register("/data2", &myfs, myfs_inst2));
Paths
- FS 1 on /data
- FS 2 on /data/fs2
-When opening files, FS driver will only be given relative path to files. If the ``myfs`` driver is registered with ``/data`` as prefix, and application calls ``fopen("/data/config.json", ...)``, VFS component will call ``myfs_open("/config.json", ...)``.
+When opening files, FS driver will only be given relative path to files. For example:
+
+- ``myfs`` driver is registered with ``/data`` as path prefix
+- and application calls ``fopen("/data/config.json", ...)``
+- then VFS component will call ``myfs_open("/config.json", ...)``.
+- ``myfs`` driver will open ``/config.json`` file
VFS doesn't impose a limit on total file path length, but it does limit FS path prefix to ``ESP_VFS_PATH_MAX`` characters. Individual FS drivers may have their own filename length limitations.