libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_fs.h
1/*-
2 * Copyright (c) 2026, The XTC Project
3 * @stability stable
4 * Use of this source code is governed by the ISC License.
5 *
6 * src/inc/xtc_fs.h
7 * Portable synchronous filesystem helpers. A small, uniform surface
8 * over the platform's file API (POSIX fds / Win32) so callers do not
9 * re-roll open/read/write/stat/rename/dir-walk and the per-platform
10 * quirks (F_FULLFSYNC vs fdatasync vs _commit, mkstemp vs _mktemp_s,
11 * dirent vs FindFirstFile, $TMPDIR vs GetTempPath).
12 *
13 * Files are int fds, so a handle from xtc_fs_open composes directly
14 * with the async xtc_aio_* ops and with raw read/write. read/write
15 * retry on EINTR/EAGAIN and loop to completion; a short pread is end
16 * of file. Functions return XTC_OK or a negative XTC_E_* code (see
17 * xtc_strerror); byte counts and metadata come back via out-params.
18 *
19 * These are blocking calls -- on a fiber loop, prefer xtc_aio_* for
20 * the read/write hot path and use these for open/stat/rename/dir
21 * management. See xtc_fs(3).
22 */
23
24#ifndef XTC_FS_H
25#define XTC_FS_H
26
27#include <stddef.h>
28#include <stdint.h>
29
30/* Flags for xtc_fs_open (mapped to O_* / _O_* per platform). */
31#define XTC_FS_READ 0x01u /* open for reading */
32#define XTC_FS_WRITE 0x02u /* open for writing */
33#define XTC_FS_CREATE 0x04u /* create the file if it does not exist */
34#define XTC_FS_TRUNC 0x08u /* truncate to zero length on open */
35#define XTC_FS_APPEND 0x10u /* each write appends at end of file */
36#define XTC_FS_EXCL 0x20u /* with CREATE: fail if the file exists */
37#define XTC_FS_DIRECT 0x40u /* bypass the OS page cache (direct I/O):
38 * O_DIRECT (Linux and BSD), F_NOCACHE (macOS),
39 * directio() (illumos),
40 * FILE_FLAG_NO_BUFFERING (Windows).
41 * Buffer address, file offset and transfer
42 * length must satisfy xtc_fs_dio_align(). */
43
44/* Metadata returned by xtc_fs_stat. */
45typedef struct xtc_fs_stat {
46 size_t size; /* sizeof(xtc_fs_stat_t): XTC_FS_STAT_INIT */
47 int64_t bytes; /* file size in bytes (1.x: `size`) */
48 int64_t mtime_ns; /* last-modified time, nanoseconds since the epoch */
49 int is_dir; /* 1 if a directory, 0 otherwise */
51
52#define XTC_FS_STAT_INIT { .size = sizeof(xtc_fs_stat_t) }
53
54/* Opaque directory iterator (xtc_fs_dir_open/next/close). */
55typedef struct xtc_fs_dir xtc_fs_dir_t;
56
57/* ---- file handles (int fd; composes with xtc_aio_* and read/write) ---- */
58int xtc_fs_open(const char *path, uint32_t flags, int *out_fd);
59int xtc_fs_close(int fd);
60int xtc_fs_pread(int fd, void *buf, size_t n, int64_t off, size_t *out_done);
61int xtc_fs_pwrite(int fd, const void *buf, size_t n, int64_t off, size_t *out_done);
62int xtc_fs_fsync(int fd); /* data + metadata durable */
63int xtc_fs_fdatasync(int fd); /* data durable (metadata best-effort) */
64int xtc_fs_ftruncate(int fd, int64_t len);
65int xtc_fs_fsize(int fd, int64_t *out_size);
66
67/* ---- direct I/O alignment ---- */
68/*
69 * Report the alignment a direct-I/O (XTC_FS_DIRECT) transfer on fd must
70 * satisfy: *mem = buffer address alignment, *off = file-offset
71 * alignment, *len = transfer-length granularity, each a power-of-two
72 * byte count. Any out-param may be NULL. The values describe what a
73 * direct op on fd requires whether or not fd is currently in direct
74 * mode, so callers can align proactively. Returns XTC_OK.
75 */
76int xtc_fs_dio_align(int fd, size_t *mem, size_t *off, size_t *len);
77
78/*
79 * Allocate a buffer suitable for direct I/O on fd: address-aligned to
80 * the memory requirement and size rounded up to the length granularity.
81 * Free with xtc_fs_dio_free. Returns XTC_OK or XTC_E_NOMEM.
82 */
83int xtc_fs_dio_alloc(int fd, size_t size, void **out);
84void xtc_fs_dio_free(void *p);
85
86/* ---- namespace operations ---- */
87int xtc_fs_stat(const char *path, xtc_fs_stat_t *out);
88int xtc_fs_exists(const char *path); /* returns 1 (yes) or 0 (no) */
89int xtc_fs_unlink(const char *path);
90int xtc_fs_rename(const char *from, const char *to); /* atomic replace */
91int xtc_fs_mkdir(const char *path);
92int xtc_fs_rmdir(const char *path);
93
94/* ---- temporary files ---- */
95int xtc_fs_tmpdir(char *buf, size_t cap); /* $TMPDIR/$TMP/$TEMP, else /tmp */
96int xtc_fs_mkstemp(char *tmpl, int *out_fd); /* tmpl ends in "XXXXXX" */
97
98/* ---- directory iteration ---- */
99int xtc_fs_dir_open(const char *path, xtc_fs_dir_t **out);
100int xtc_fs_dir_next(xtc_fs_dir_t *d, const char **out_name); /* *out_name == NULL at end */
101void xtc_fs_dir_close(xtc_fs_dir_t *d);
102
103#endif /* XTC_FS_H */