• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019-2023 the Contributors to the WASI Specification
2 // This file is adapted from the WASI preview1 spec here:
3 // https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md
4 package okio.internal.preview1
5 
6 import kotlin.wasm.WasmImport
7 
8 /** `u32`. */
9 typealias size = Int
10 
11 /**
12  * `Handle`.
13  *
14  * A file descriptor handle.
15  */
16 typealias fd = Int
17 
18 /**
19  * `u64`.
20  *
21  * A reference to the offset of a directory entry.
22  *
23  * The value 0 signifies the start of the directory.
24  */
25 typealias dircookie = Long
26 
27 /**
28  * `Variant`.
29  *
30  * Error codes returned by functions. Not all of these error codes are returned by the functions
31  * provided by this API; some are used in higher-level library layers, and others are provided
32  * merely for alignment with POSIX.
33  */
34 typealias errno = Short
35 
36 /**
37  * `u64`.
38  *
39  * File serial number that is unique within its file system.
40  */
41 typealias inode = Long
42 
43 /**
44  * `u32`.
45  *
46  * The type for the [`dirent::d_namlen`](#dirent.d_namlen) field of [`dirent`](#dirent) struct.
47  */
48 typealias dirnamelen = Int
49 
50 /**
51  * `Pointer<u8>`.
52  */
53 typealias PointerU8 = Int
54 
55 /**
56  * path_create_directory(fd: fd, path: string) -> Result<(), errno>
57  *
58  * Create a directory.
59  * Note: This is similar to `mkdirat` in POSIX.
60  */
61 @WasmImport("wasi_snapshot_preview1", "path_create_directory")
path_create_directorynull62 internal external fun path_create_directory(
63   fd: fd,
64   path: PointerU8,
65   pathSize: size,
66 ): Int // should be Short??
67 
68 /**
69  * path_filestat_get(fd: fd, flags: lookupflags, path: string) -> Result<filestat, errno>
70  *
71  * Return the attributes of a file or directory.
72  * Note: This is similar to `stat` in POSIX.
73  */
74 @WasmImport("wasi_snapshot_preview1", "path_filestat_get")
75 internal external fun path_filestat_get(
76   fd: fd,
77   flags: lookupflags,
78   path: PointerU8,
79   pathSize: size,
80   returnPointer: PointerU8,
81 ): Int // should be Short??
82 
83 /**
84  * path_open(fd: fd, dirflags: lookupflags, path: string, oflags: oflags, fs_rights_base: rights, fs_rights_inheriting: rights, fdflags: fdflags) -> Result<fd, errno>
85  *
86  * Open a file or directory.
87  * The returned file descriptor is not guaranteed to be the lowest-numbered
88  * file descriptor not currently open; it is randomized to prevent
89  * applications from depending on making assumptions about indexes, since this
90  * is error-prone in multi-threaded contexts. The returned file descriptor is
91  * guaranteed to be less than 2**31.
92  * Note: This is similar to `openat` in POSIX.
93  */
94 @WasmImport("wasi_snapshot_preview1", "path_open")
95 internal external fun path_open(
96   fd: fd,
97   dirflags: lookupflags,
98   path: PointerU8,
99   pathSize: size,
100   oflags: oflags,
101   fs_rights_base: rights,
102   fs_rights_inheriting: rights,
103   fdflags: fdflags,
104   returnPointer: PointerU8,
105 ): Int // should be Short??
106 
107 /**
108  * path_readlink(fd: fd, path: string, buf: Pointer<u8>, buf_len: size) -> Result<size, errno>
109  *
110  * Read the contents of a symbolic link.
111  * Note: This is similar to `readlinkat` in POSIX.
112  */
113 @WasmImport("wasi_snapshot_preview1", "path_readlink")
114 internal external fun path_readlink(
115   fd: fd,
116   path: PointerU8,
117   pathSize: size,
118   buf: PointerU8,
119   buf_len: size,
120   returnPointer: PointerU8,
121 ): Int // should be Short??
122 
123 /**
124  * path_remove_directory(fd: fd, path: string) -> Result<(), errno>
125  *
126  * Remove a directory.
127  * Return [`errno::notempty`](#errno.notempty) if the directory is not empty.
128  * Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX.
129  */
130 @WasmImport("wasi_snapshot_preview1", "path_remove_directory")
131 internal external fun path_remove_directory(
132   fd: fd,
133   path: PointerU8,
134   pathSize: size,
135 ): Int // should be Short??
136 
137 /**
138  * path_rename(fd: fd, old_path: string, new_fd: fd, new_path: string) -> Result<(), errno>
139  *
140  * Rename a file or directory.
141  * Note: This is similar to `renameat` in POSIX.
142  */
143 @WasmImport("wasi_snapshot_preview1", "path_rename")
144 internal external fun path_rename(
145   fd: fd,
146   old_path: PointerU8,
147   old_pathSize: size,
148   new_fd: fd,
149   new_path: PointerU8,
150   new_pathSize: size,
151 ): Int // should be Short??
152 
153 /**
154  * path_symlink(old_path: string, fd: fd, new_path: string) -> Result<(), errno>
155  *
156  * Create a symbolic link.
157  * Note: This is similar to `symlinkat` in POSIX.
158  */
159 @WasmImport("wasi_snapshot_preview1", "path_symlink")
160 internal external fun path_symlink(
161   old_path: PointerU8,
162   old_pathSize: size,
163   fd: fd,
164   new_path: PointerU8,
165   new_pathSize: size,
166 ): Int // should be Short??
167 
168 /**
169  * path_unlink_file(fd: fd, path: string) -> Result<(), errno>
170  *
171  * Unlink a file.
172  * Return [`errno::isdir`](#errno.isdir) if the path refers to a directory.
173  * Note: This is similar to `unlinkat(fd, path, 0)` in POSIX.
174  */
175 @WasmImport("wasi_snapshot_preview1", "path_unlink_file")
176 internal external fun path_unlink_file(
177   fd: fd,
178   path: PointerU8,
179   pathSize: size,
180 ): Int // should be Short??
181 
182 /**
183  * fd_close(fd: fd) -> Result<(), errno>
184  *
185  * Close a file descriptor.
186  * Note: This is similar to `close` in POSIX.
187  */
188 @WasmImport("wasi_snapshot_preview1", "fd_close")
189 internal external fun fd_close(
190   fd: fd,
191 ): Int // should be Short??
192 
193 /**
194  * fd_filestat_get(fd: fd) -> Result<filestat, errno>
195  *
196  * Return the attributes of an open file.
197  */
198 @WasmImport("wasi_snapshot_preview1", "fd_filestat_get")
199 internal external fun fd_filestat_get(
200   fd: fd,
201   returnPointer: PointerU8,
202 ): Int // should be Short??
203 
204 /**
205  * fd_pread(fd: fd, iovs: iovec_array, offset: filesize) -> Result<size, errno>
206  *
207  * Read from a file descriptor.
208  * Note: This is similar to `readv` in POSIX.
209  */
210 @WasmImport("wasi_snapshot_preview1", "fd_pread")
211 internal external fun fd_pread(
212   fd: fd,
213   iovs: PointerU8,
214   iovsSize: size,
215   offset: Long,
216   returnPointer: PointerU8,
217 ): Int // should be Short??
218 
219 /**
220  * fd_prestat_dir_name(fd: fd, path: Pointer<u8>, path_len: size) -> Result<(), errno>
221  *
222  * Return a description of the given preopened file descriptor.
223  */
224 @WasmImport("wasi_snapshot_preview1", "fd_prestat_dir_name")
225 internal external fun fd_prestat_dir_name(
226   fd: fd,
227   path: PointerU8,
228   pathSize: size,
229 ): Int // should be Short??
230 
231 /**
232  * fd_prestat_get(fd: fd) -> Result<prestat, errno>
233  *
234  * Return a description of the given preopened file descriptor.
235  */
236 @WasmImport("wasi_snapshot_preview1", "fd_prestat_get")
237 internal external fun fd_prestat_get(
238   fd: fd,
239   returnPointer: PointerU8,
240 ): Int // should be Short??
241 
242 /**
243  * fd_pwrite(fd: fd, iovs: ciovec_array, offset: filesize) -> Result<size, errno>`
244  *
245  * Write to a file descriptor, without using and updating the file descriptor's offset.
246  * Note: This is similar to `pwritev` in Linux (and other Unix-es).
247  *
248  * Like Linux (and other Unix-es), any calls of `pwrite` (and other
249  * functions to read or write) for a regular file by other threads in the
250  * WASI process should not be interleaved while `pwrite` is executed.
251  */
252 @WasmImport("wasi_snapshot_preview1", "fd_pwrite")
253 internal external fun fd_pwrite(
254   fd: fd,
255   iovs: PointerU8,
256   iovsSize: size,
257   offset: Long,
258   returnPointer: PointerU8,
259 ): Int // should be Short??
260 
261 /**
262  * fd_read(fd: fd, iovs: iovec_array) -> Result<size, errno>
263  *
264  * Read from a file descriptor.
265  * Note: This is similar to `readv` in POSIX.
266  */
267 @WasmImport("wasi_snapshot_preview1", "fd_read")
268 internal external fun fd_read(
269   fd: fd,
270   iovs: PointerU8,
271   iovsSize: size,
272   returnPointer: PointerU8,
273 ): Int // should be Short??
274 
275 /**
276  * fd_readdir(fd: fd, buf: Pointer<u8>, buf_len: size, cookie: dircookie) -> Result<size, errno>
277  *
278  * Read directory entries from a directory.
279  * When successful, the contents of the output buffer consist of a sequence of
280  * directory entries. Each directory entry consists of a [`dirent`](#dirent) object,
281  * followed by [`dirent::d_namlen`](#dirent.d_namlen) bytes holding the name of the directory
282  * entry.
283  * This function fills the output buffer as much as possible, potentially
284  * truncating the last directory entry. This allows the caller to grow its
285  * read buffer size in case it's too small to fit a single large directory
286  * entry, or skip the oversized directory entry.
287  */
288 @WasmImport("wasi_snapshot_preview1", "fd_readdir")
289 internal external fun fd_readdir(
290   fd: fd,
291   buf: PointerU8,
292   buf_len: size,
293   cookie: dircookie,
294   returnPointer: PointerU8,
295 ): Int // should be Short??
296 
297 /**
298  * fd_filestat_set_size(fd: fd, size: filesize) -> Result<(), errno>
299  *
300  * Adjust the size of an open file. If this increases the file's size, the extra bytes are filled with zeros.
301  * Note: This is similar to `ftruncate` in POSIX.
302  */
303 @WasmImport("wasi_snapshot_preview1", "fd_filestat_set_size")
304 internal external fun fd_filestat_set_size(
305   fd: fd,
306   size: Long,
307 ): Int // should be Short??
308 
309 /**
310  * fd_sync(fd: fd) -> Result<(), errno>
311  *
312  * Synchronize the data and metadata of a file to disk.
313  * Note: This is similar to `fsync` in POSIX.
314  */
315 @WasmImport("wasi_snapshot_preview1", "fd_sync")
316 internal external fun fd_sync(
317   fd: fd,
318 ): Int // should be Short??
319 
320 /**
321  * fd_write(fd: fd, iovs: ciovec_array) -> Result<size, errno>
322  *
323  * Write to a file descriptor.
324  * Note: This is similar to `writev` in POSIX.
325  *
326  * Like POSIX, any calls of `write` (and other functions to read or write)
327  * for a regular file by other threads in the WASI process should not be
328  * interleaved while `write` is executed.
329  */
330 @WasmImport("wasi_snapshot_preview1", "fd_write")
331 internal external fun fd_write(
332   fd: fd,
333   iovs: PointerU8,
334   iovsSize: size,
335   returnPointer: PointerU8,
336 ): Int // should be Short??
337