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