TARFS 0.1.5
Read-only TAR filesystem for ESP32
Loading...
Searching...
No Matches
posix.c File Reference
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "config.h"
#include "os.h"
#include "fs.h"
#include "file.h"
#include "inode.h"
#include "tar.h"
#include "dir.h"
#include "posix.h"

Go to the source code of this file.

Functions

void * mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset)
 Subset of POSIX functions.
int munmap (void *addr, size_t length)
 POSIX munmap().
int dupfd (int fd)
 Create an independent duplicate of a file descriptor.
DIR * fdopendir (int fd)
 Associate an open directory file descriptor with a directory stream.
int statvfs (const char *path, struct statvfs *st)
 Obtain filesystem statistics.
ssize_t sendfile (int out_fd, int in_fd, off_t *offset, size_t count)
 Zero-overhead, no-buffer file-to-socket tranfer.

Function Documentation

◆ dupfd()

int dupfd ( int fd)

Create an independent duplicate of a file descriptor.

This function is similar to POSIX dup(), but the file position is not shared. The new descriptor has its own independent file offset, initialized to the current position of the original descriptor.

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

Definition at line 107 of file posix.c.

References ioctl_req::fd, FIOGETFD, ioctl_req::fs_idx, and tarf_dupfd().

◆ fdopendir()

DIR * fdopendir ( int fd)

Associate an open directory file descriptor with a directory stream.

Creates a directory stream from an existing directory file descriptor. After a successful call, the file descriptor is owned by the returned directory stream and must not be closed directly. It will be closed automatically by tard_closedir().

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

Definition at line 133 of file posix.c.

References ioctl_req::fd, FIOGETFD, ioctl_req::fs_idx, and tard_fdopendir().

◆ mmap()

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

Subset of POSIX functions.

Map a file into the process address space.

Mimic POSIX mmap().

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.

< 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 )

POSIX munmap().

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.

< 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(), and TARFS_MAX_FS.

◆ sendfile()

ssize_t sendfile ( int out_fd,
int in_fd,
off_t * offset,
size_t count )

Zero-overhead, no-buffer file-to-socket tranfer.

File descriptor must be a TARFS file descriptor

This function transfers data directly from in_fd to out_fd without requiring an intermediate user buffer, without read()/pread()

Parameters
out_fdDestination file descriptor. Must be a socket.
in_fdSource file descriptor. A tarfs descriptor
offsetOptional starting offset in the input file. If NULL, the current file position is used and advanced. Otherwise, the value pointed to by offset is used and updated, while the file position of in_fd remains unchanged.
countMaximum number of bytes to transfer.
Returns
Number of bytes transferred on success, or -1 on error with errno set.

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

Definition at line 196 of file posix.c.

References ioctl_req::fd, FIOGETFD, ioctl_req::fs_idx, and tarf_sendfile().

◆ statvfs()

int statvfs ( const char * path,
struct statvfs * st )

Obtain filesystem statistics.

Fills a POSIX statvfs structure with information about the mounted filesystem. Since TARFS is a read-only filesystem, the number of available blocks and inodes is always reported as zero.

Parameters
pathPointer to the path
stPointer to the statvfs structure to fill.
Returns
0 on success, or -1 on error with errno set appropriately.

Definition at line 163 of file posix.c.

References ioctl_req::fs_idx, tarfs_fsindex(), and tarfs_statvfs().