host: Exchange two directory entries atomically

7be9285b3d492ebd157fbce6dbf5e0dcedd32a37a54d75cd5a333c90d8a0c830
Package exports must replace a complete directory while readers can
still open it. Expose the host filesystem exchange operation through
the hosted ABI.

Assisted-by: Codex:gpt-6
Change-Id: I061c17a105d5537876f173cfa4feb15fd530ac53
Alexis Sellier committed ago 1 parent 07bf9059
AGENTS.md +1 -0
1 1
Run `make fmt` before committing changes.
2 2
Keep commit messages wrapped at 70 columns.
3 +
Use C99 (`-std=c99`) for all C source files.
3 4
Do not commit code in a language that isn't already part of the repository's
4 5
checked in source.
README +2 -0
115 115
                                              emulator; returns a child handle.
116 116
    0x40B  wait(child, out)                   Writes the outcome (kind u32:
117 117
                                              0 exited, 1 step limit, 2 fault;
118 118
                                              code i32).
119 119
    0x40C  waitAny(out)                       Returns the handle that ended.
120 +
    0x40D  exchange(left, llen, right, rlen)  Atomically swaps two existing
121 +
                                              entries on the same filesystem.
120 122
121 123
  Errors: -1 NotFound, -2 Exists, -3 Denied, -4 NotEmpty, -5 Invalid (also
122 124
  any other call number from 0x400), -6 TooLarge, -7 Limit, -8 Io.
123 125
124 126
  `spawn` takes a 128-byte record with the image path, the arguments, the
abi_host.c +8 -3
23 23
#define ECALL_RENAME   0x408
24 24
#define ECALL_LIST_DIR 0x409
25 25
#define ECALL_SPAWN    0x40A
26 26
#define ECALL_WAIT     0x40B
27 27
#define ECALL_WAIT_ANY 0x40C
28 +
#define ECALL_EXCHANGE 0x40D
28 29
29 30
/* Result of a call that faulted the guest: a0 is left unchanged. */
30 31
#define ECALL_FAULTED INT64_MIN
31 32
32 33
/* Size of the guest records exchanged with the host. */
349 350
        rc = guest_path(cpu, pc, "remove", a0, a1, path);
350 351
        if (!rc)
351 352
            rc = host_remove(path);
352 353
        break;
353 354
    case ECALL_RENAME:
354 -
        rc = guest_path(cpu, pc, "rename", a0, a1, path);
355 +
    case ECALL_EXCHANGE: {
356 +
        const char *name = call == ECALL_RENAME ? "rename" : "exchange";
357 +
        rc               = guest_path(cpu, pc, name, a0, a1, path);
355 358
        if (!rc)
356 -
            rc = guest_path(cpu, pc, "rename", a2, a3, other);
359 +
            rc = guest_path(cpu, pc, name, a2, a3, other);
357 360
        if (!rc)
358 -
            rc = host_rename(path, other);
361 +
            rc = call == ECALL_RENAME ? host_rename(path, other)
362 +
                                      : host_exchange(path, other);
359 363
        break;
364 +
    }
360 365
    case ECALL_LIST_DIR:
361 366
        rc = ecall_list_dir(cpu, pc, a0, a1, a2, a3);
362 367
        break;
363 368
    case ECALL_SPAWN:
364 369
        rc = ecall_spawn(cpu, pc, a0);
host.h +2 -0
105 105
/* Remove a file or an empty directory. */
106 106
i64 host_remove(const char *path);
107 107
108 108
/* Rename a file or directory, replacing an existing file. */
109 109
i64 host_rename(const char *from, const char *to);
110 +
/* Atomically exchange two existing directory entries on the same filesystem. */
111 +
i64 host_exchange(const char *left, const char *right);
110 112
111 113
/* List a directory into `buf`: each entry name followed by a zero byte,
112 114
 * excluding "." and "..", in host order. Returns the byte count, or
113 115
 * `HOST_E_TOO_LARGE` if `cap` bytes are not enough. */
114 116
i64 host_list_dir(const char *path, u8 *buf, u32 cap);
host_posix.c +19 -0
1 1
/* POSIX implementation of the host platform layer. */
2 +
#ifdef __APPLE__
3 +
#define _DARWIN_C_SOURCE
4 +
#else
5 +
#define _DEFAULT_SOURCE
6 +
#endif
2 7
#define _POSIX_C_SOURCE 200809L
3 8
4 9
#include <dirent.h>
5 10
#include <errno.h>
6 11
#include <fcntl.h>
14 19
#include <sys/wait.h>
15 20
#include <unistd.h>
16 21
17 22
#ifdef __APPLE__
18 23
#include <mach-o/dyld.h>
24 +
#else
25 +
#include <linux/fs.h>
26 +
#include <sys/syscall.h>
19 27
#endif
20 28
21 29
#include "host.h"
22 30
23 31
extern char **environ;
137 145
138 146
i64 host_rename(const char *from, const char *to) {
139 147
    return rename(from, to) < 0 ? fail() : 0;
140 148
}
141 149
150 +
i64 host_exchange(const char *left, const char *right) {
151 +
#ifdef __APPLE__
152 +
    int rc = renameatx_np(AT_FDCWD, left, AT_FDCWD, right, RENAME_SWAP);
153 +
#else
154 +
    long rc = syscall(
155 +
        SYS_renameat2, AT_FDCWD, left, AT_FDCWD, right, RENAME_EXCHANGE
156 +
    );
157 +
#endif
158 +
    return rc < 0 ? fail() : 0;
159 +
}
160 +
142 161
i64 host_list_dir(const char *path, u8 *buf, u32 cap) {
143 162
    DIR *dir = opendir(path);
144 163
    if (!dir)
145 164
        return fail();
146 165