TARFS 0.1.5
Read-only TAR filesystem for ESP32
Loading...
Searching...
No Matches
posix.h File Reference
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>

Go to the source code of this file.

Macros

#define PROT_READ   1
 POSIX mmap()/munmap() support for TARFS.
#define PROT_WRITE   2
#define PROT_EXEC   4
#define MAP_FILE   0
#define MAP_SHARED   1
#define MAP_PRIVATE   2
#define MAP_FIXED   4
#define MAP_ANONYMOUS   8
#define MAP_ANON   8
#define MAP_FAILED   ((void *)(-1))

Functions

void * mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset)
 Map a file into the process address space.
int munmap (void *addr, size_t length)
 Remove a previously created memory mapping.

Macro Definition Documentation

◆ MAP_ANON

#define MAP_ANON   8

Definition at line 38 of file posix.h.

◆ MAP_ANONYMOUS

#define MAP_ANONYMOUS   8

Unsupported, use malloc() instead

Definition at line 37 of file posix.h.

Referenced by tarf_mmap().

◆ MAP_FAILED

#define MAP_FAILED   ((void *)(-1))

Definition at line 41 of file posix.h.

Referenced by mmap(), and tarf_mmap().

◆ MAP_FILE

#define MAP_FILE   0

Default flag is MAP_FILE, we do not support MAP_ANON, we always expect MAP_FILE

Definition at line 32 of file posix.h.

◆ MAP_FIXED

#define MAP_FIXED   4

Ignored, no legit use for this flag :)

Definition at line 36 of file posix.h.

Referenced by tarf_mmap().

◆ MAP_PRIVATE

#define MAP_PRIVATE   2

Supported

Definition at line 34 of file posix.h.

◆ MAP_SHARED

#define MAP_SHARED   1

Supported

Definition at line 33 of file posix.h.

◆ PROT_EXEC

#define PROT_EXEC   4

Ignored

Definition at line 30 of file posix.h.

Referenced by tarf_mmap().

◆ PROT_READ

#define PROT_READ   1

POSIX mmap()/munmap() support for TARFS.

Default memory protection flag.

Definition at line 28 of file posix.h.

◆ PROT_WRITE

#define PROT_WRITE   2

Ignored, if used together with PROT_READ. Alone causes mmap() error (RO filesystem!)

Definition at line 29 of file posix.h.

Referenced by tarf_mmap().

Function Documentation

◆ mmap()

void * mmap ( void * addr,
size_t length,
int prot,
int flags,
int fd,
off_t offset )

Map a file into the process address space.

Subset of POSIX functions.

Typical usage:

int fd = open(...);
void *ptr = mmap(NULL, length, PROT_READ, flags, fd, offset);
close(fd);
// use ptr
void * mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
Subset of POSIX functions.
Definition posix.c:54
#define PROT_READ
POSIX mmap()/munmap() support for TARFS.
Definition posix.h:28

The mapping remains valid after the file descriptor has been closed.

Since TARFS is a read-only filesystem, this implementation does not support the PROT_WRITE protection flag. MAP_ANONYMOUS is also not supported: mmap() is intended exclusively for mapping files.

There is no limit on the number of active mappings. Every file in TARFS may be mapped simultaneously, provided the underlying flash memory can hold it. Mappings do not consume any runtime resources. In contrast, each open file occupies one file descriptor until it is closed.

Parameters
addrMust be NULL. Fixed-address mappings are not supported.
lengthNumber of bytes to map.
protMemory protection flags. Only PROT_READ is supported.
flagsMapping flags. Use MAP_SHARED or MAP_PRIVATE. On a read-only filesystem both behave identically.
fdOpen file descriptor to map.
offsetFile offset where the mapping begins.
Returns
Pointer to the mapped file data, or MAP_FAILED on error.

Map a file into the process address space.

Mimic POSIX mmap().

< Convert global fd number to local fd number. arg=&int

Definition at line 54 of file posix.c.

References ioctl_req::fd, FIOGETFD, ioctl_req::fs_idx, MAP_FAILED, and tarf_mmap().

◆ munmap()

int munmap ( void * addr,
size_t length )

Remove a previously created memory mapping.

POSIX munmap().

Since mappings themselves do not allocate runtime resources, failing to call munmap() does not lead to resource leaks. However, an active mapping keeps the filesystem mounted, preventing it from being unmounted until the mapping is removed.

Parameters
addrAddress previously returned by mmap().
lengthLength of the mapped region.
Returns
0 on success, or -1 on error.

Remove a previously created memory mapping.

< Max number of mounted TARFS filesystems

Definition at line 72 of file posix.c.

References tarfs_fs::fs_size, tarfs_fs::fs_vaddr, log, tarf_munmap(), tarfs_getfs(), tarfs_lock(), TARFS_MAX_FS, and tarfs_unlock().