Repository navigation
Expand file tree
/
Copy pathfs.wave
More file actions
229 lines (192 loc) · 5.41 KB
/
Copy pathfs.wave
File metadata and controls
229 lines (192 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// This file is part of the Wave language project.
// Copyright (c) 2024-2026 Wave Foundation
// Copyright (c) 2024-2026 LunaStev and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
// =======================================================
// Linux x86_64 filesystem syscalls
// =======================================================
//
// This layer provides minimal filesystem operations
// built directly on top of syscall.wave.
//
// All functions return raw syscall results.
// Negative values indicate -errno.
// =======================================================
import("std::sys::linux::amd64::syscall")::{
syscall0,
syscall1,
syscall2,
syscall3,
syscall4,
syscall5,
syscall6,
};
// open flags
pub const FS_O_RDONLY: i32 = 0;
pub const FS_O_WRONLY: i32 = 1;
pub const FS_O_RDWR: i32 = 2;
pub const FS_O_CREAT: i32 = 64;
pub const FS_O_EXCL: i32 = 128;
pub const FS_O_TRUNC: i32 = 512;
pub const FS_O_APPEND: i32 = 1024;
pub const FS_O_NONBLOCK: i32 = 2048;
// access modes
pub const FS_F_OK: i32 = 0;
pub const FS_X_OK: i32 = 1;
pub const FS_W_OK: i32 = 2;
pub const FS_R_OK: i32 = 4;
// seek modes
pub const FS_SEEK_SET: i32 = 0;
pub const FS_SEEK_CUR: i32 = 1;
pub const FS_SEEK_END: i32 = 2;
// fcntl modes
pub const FS_F_GETFL: i32 = 3;
pub const FS_F_SETFL: i32 = 4;
// -----------------------
// open / close
// -----------------------
pub fun open(path: str, flags: i32, mode: i32) -> i64 {
// syscall: open (2)
return syscall3(2, path as i64, flags as i64, mode as i64);
}
pub fun close(fd: i64) -> i64 {
// syscall: close (3)
return syscall1(3, fd);
}
pub fun dup(fd: i64) -> i64 {
// syscall: dup (32)
return syscall1(32, fd);
}
pub fun dup2(oldfd: i64, newfd: i64) -> i64 {
// syscall: dup2 (33)
return syscall2(33, oldfd, newfd);
}
pub fun pipe(fds: ptr<i32>) -> i64 {
// syscall: pipe (22)
return syscall1(22, fds as i64);
}
pub fun fsync(fd: i64) -> i64 {
// syscall: fsync (74)
return syscall1(74, fd);
}
pub fun fcntl(fd: i64, cmd: i32, arg: i64) -> i64 {
// syscall: fcntl (72)
return syscall3(72, fd, cmd as i64, arg);
}
// -----------------------
// read / write
// -----------------------
pub fun read(fd: i64, buf: ptr<u8>, len: i64) -> i64 {
// syscall: read (0)
return syscall3(0, fd, buf as i64, len);
}
pub fun write(fd: i64, buf: ptr<u8>, len: i64) -> i64 {
// syscall: write (1)
return syscall3(1, fd, buf as i64, len);
}
pub fun getcwd(buf: ptr<u8>, size: i64) -> i64 {
// syscall: getcwd (79)
return syscall2(79, buf as i64, size);
}
pub fun chdir(path: str) -> i64 {
// syscall: chdir (80)
return syscall1(80, path as i64);
}
pub fun access(path: str, mode: i32) -> i64 {
// syscall: access (21)
return syscall2(21, path as i64, mode as i64);
}
// -----------------------
// seek
// -----------------------
pub fun lseek(fd: i64, offset: i64, whence: i32) -> i64 {
// syscall: lseek (8)
return syscall3(8, fd, offset, whence as i64);
}
// -----------------------
// file operations
// -----------------------
pub fun unlink(path: str) -> i64 {
// syscall: unlink (87)
return syscall1(87, path as i64);
}
pub fun mkdir(path: str, mode: i32) -> i64 {
// syscall: mkdir (83)
return syscall2(83, path as i64, mode as i64);
}
pub fun rmdir(path: str) -> i64 {
// syscall: rmdir (84)
return syscall1(84, path as i64);
}
pub fun rename_path(old_path: str, new_path: str) -> i64 {
// syscall: rename (82)
return syscall2(82, old_path as i64, new_path as i64);
}
// -----------------------
// metadata
// -----------------------
// Linux x86_64 struct stat, including nanoseconds and the reserved tail.
pub struct Stat {
dev: i64;
ino: i64;
nlink: i64;
mode: i32;
uid: i32;
gid: i32;
pad0: i32;
rdev: i64;
size: i64;
blksize: i64;
blocks: i64;
atime: i64;
atime_nsec: i64;
mtime: i64;
mtime_nsec: i64;
ctime: i64;
ctime_nsec: i64;
unused0: i64;
unused1: i64;
unused2: i64;
}
pub fun stat(path: str, st: ptr<Stat>) -> i64 {
// syscall: stat (4)
return syscall2(4, path as i64, st as i64);
}
pub fun fstat(fd: i64, st: ptr<Stat>) -> i64 {
// syscall: fstat (5)
return syscall2(5, fd, st as i64);
}
// Resize the opened object without changing its current offset.
pub fun ftruncate(fd: i64, length: i64) -> i64 {
if (length < 0) {
return -22;
}
return syscall2(77, fd, length);
}
// Returns 1 for the same opened file, 0 for distinct files, or a negative error.
pub fun same_file(first_fd: i64, second_fd: i64) -> i64 {
var first: Stat;
var second: Stat;
var result: i64 = fstat(first_fd, &first);
if (result < 0) {
return result;
}
result = fstat(second_fd, &second);
if (result < 0) {
return result;
}
return (first.dev == second.dev && first.ino == second.ino) as i64;
}