moto-rt 0.2.3

Motor OS Runtime.
Documentation
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Filesystem RT API.
//!
//! While it would be great to expose something more interesting,
//! Rust's FS PAL is a thin wrapper around POSIX FS API, and a lot
//! of popular third-party crates assume POSIXy FS API, and
//! when Motor OS later adds a libc, or another compatibility
//! layer with another language/system, it will also have to
//! expose a flavor of POSIXy FS API, so we don't try to be too
//! different here and expose a POSIXy FS API.

use crate::error::*;
use crate::ok_or_error;
use crate::to_result;
use crate::RtFd;
use crate::RtVdsoVtable;
use core::sync::atomic::Ordering;

#[cfg(not(feature = "rustc-dep-of-std"))]
extern crate alloc;

pub const TEMP_DIR: &str = "/sys/tmp";

/// The maximum length of a file/directory absolute path, in bytes.
///
/// Relatively short so that two paths can fit into a page, with some
/// extra fields (for RPC rename request). Although Linux and Windows
/// have longer max path lengths, macOS has 1024, so 1024 should also
/// be enough for Motor OS.
pub const MAX_PATH_LEN: usize = 1024;
/// The maximum length of a file/directory leaf name, in bytes.
pub const MAX_FILENAME_LEN: usize = 256;
/// The maximum length of a single file.
pub const MAX_FILE_LEN: u64 = i64::MAX as u64;

// File types.
pub const FILETYPE_FILE: u8 = 1;
pub const FILETYPE_DIRECTORY: u8 = 2;

// File permissions.
pub const PERM_READ: u64 = 1;
pub const PERM_WRITE: u64 = 2;

// Open options.
pub const O_READ: u32 = 1 << 0;
pub const O_WRITE: u32 = 1 << 1;
pub const O_APPEND: u32 = 1 << 2;
pub const O_TRUNCATE: u32 = 1 << 3;
pub const O_CREATE: u32 = 1 << 4;
pub const O_CREATE_NEW: u32 = 1 << 5;

// Seek option.
pub const SEEK_SET: u8 = 0;
pub const SEEK_CUR: u8 = 1;
pub const SEEK_END: u8 = 2;

#[repr(C, align(16))]
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub struct FileAttr {
    pub version: u64,
    pub size: u64,
    pub perm: u64,
    pub file_type: u8,
    pub _reserved: [u8; 7],
    pub created: u128,
    pub modified: u128,
    pub accessed: u128,
}

impl FileAttr {
    pub const VERSION: u64 = 1;

    fn new() -> Self {
        Self {
            version: Self::VERSION,
            ..Default::default()
        }
    }
}

/// Returned by readdir().
#[repr(C, align(16))]
#[derive(Clone, Copy)]
pub struct DirEntry {
    pub version: u64,
    pub _reserved: u64,
    pub attr: FileAttr,
    pub fname_size: u16, // Filename only, without path/ancestors.
    pub fname: [u8; MAX_FILENAME_LEN],
}

impl DirEntry {
    pub const VERSION: u64 = 1;

    fn new() -> Self {
        Self {
            version: Self::VERSION,
            _reserved: 0,
            attr: FileAttr::new(),
            fname_size: 0,
            fname: [0; MAX_FILENAME_LEN],
        }
    }
}

pub fn is_terminal(rt_fd: RtFd) -> bool {
    let vdso_is_terminal: extern "C" fn(i32) -> i32 = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_is_terminal.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    match vdso_is_terminal(rt_fd) {
        0 => false,
        1 => true,
        _ => panic!(),
    }
}

pub fn duplicate(rt_fd: RtFd) -> Result<RtFd, ErrorCode> {
    let vdso_duplicate: extern "C" fn(RtFd) -> RtFd = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_duplicate.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    to_result!(vdso_duplicate(rt_fd))
}

/// Opens a file at `path` with options specified by `opts`.
pub fn open(path: &str, opts: u32) -> Result<RtFd, ErrorCode> {
    let vdso_open: extern "C" fn(*const u8, usize, u32) -> i32 = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_open.load(Ordering::Relaxed) as usize as *const ()
        )
    };

    let bytes = path.as_bytes();
    to_result!(vdso_open(bytes.as_ptr(), bytes.len(), opts))
}

pub fn close(rt_fd: RtFd) -> Result<(), ErrorCode> {
    let vdso_close: extern "C" fn(i32) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_close.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_close(rt_fd))
}

pub fn get_file_attr(rt_fd: RtFd) -> Result<FileAttr, ErrorCode> {
    let vdso_get_file_attr: extern "C" fn(i32, *mut FileAttr) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_get_file_attr.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let mut attr = FileAttr::new();
    match vdso_get_file_attr(rt_fd, &mut attr) {
        E_OK => Ok(attr),
        err => Err(err),
    }
}

pub fn fsync(rt_fd: RtFd) -> Result<(), ErrorCode> {
    let vdso_fsync: extern "C" fn(i32) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_fsync.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_fsync(rt_fd))
}

pub fn datasync(rt_fd: RtFd) -> Result<(), ErrorCode> {
    let vdso_datasync: extern "C" fn(i32) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_datasync.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_datasync(rt_fd))
}

pub fn truncate(rt_fd: RtFd, size: u64) -> Result<(), ErrorCode> {
    let vdso_truncate: extern "C" fn(i32, u64) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_truncate.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_truncate(rt_fd, size))
}

pub fn read(rt_fd: RtFd, buf: &mut [u8]) -> Result<usize, ErrorCode> {
    let vdso_read: extern "C" fn(i32, *mut u8, usize) -> i64 = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_read.load(Ordering::Relaxed) as usize as *const ()
        )
    };

    to_result!(vdso_read(rt_fd, buf.as_mut_ptr(), buf.len()))
}

pub fn write(rt_fd: RtFd, buf: &[u8]) -> Result<usize, ErrorCode> {
    let vdso_write: extern "C" fn(i32, *const u8, usize) -> i64 = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_write.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    to_result!(vdso_write(rt_fd, buf.as_ptr(), buf.len()))
}

pub fn flush(rt_fd: RtFd) -> Result<(), ErrorCode> {
    let vdso_flush: extern "C" fn(i32) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_flush.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_flush(rt_fd))
}

pub fn seek(rt_fd: RtFd, offset: i64, whence: u8) -> Result<u64, ErrorCode> {
    let vdso_seek: extern "C" fn(i32, i64, u8) -> i64 = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_seek.load(Ordering::Relaxed) as usize as *const ()
        )
    };

    to_result!(vdso_seek(rt_fd, offset, whence))
}

pub fn mkdir(path: &str) -> Result<(), ErrorCode> {
    let vdso_mkdir: extern "C" fn(*const u8, usize) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_mkdir.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let bytes = path.as_bytes();
    ok_or_error(vdso_mkdir(bytes.as_ptr(), bytes.len()))
}

pub fn unlink(path: &str) -> Result<(), ErrorCode> {
    let vdso_unlink: extern "C" fn(*const u8, usize) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_unlink.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let bytes = path.as_bytes();
    ok_or_error(vdso_unlink(bytes.as_ptr(), bytes.len()))
}

pub fn rename(old: &str, new: &str) -> Result<(), ErrorCode> {
    let vdso_rename: extern "C" fn(*const u8, usize, *const u8, usize) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_rename.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let old = old.as_bytes();
    let new = new.as_bytes();
    ok_or_error(vdso_rename(
        old.as_ptr(),
        old.len(),
        new.as_ptr(),
        new.len(),
    ))
}

pub fn rmdir(path: &str) -> Result<(), ErrorCode> {
    let vdso_rmdir: extern "C" fn(*const u8, usize) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_rmdir.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let bytes = path.as_bytes();
    ok_or_error(vdso_rmdir(bytes.as_ptr(), bytes.len()))
}

pub fn rmdir_all(path: &str) -> Result<(), ErrorCode> {
    let vdso_rmdir_all: extern "C" fn(*const u8, usize) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_rmdir_all.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let bytes = path.as_bytes();
    ok_or_error(vdso_rmdir_all(bytes.as_ptr(), bytes.len()))
}

pub fn set_perm(path: &str, perm: u64) -> Result<(), ErrorCode> {
    let vdso_set_perm: extern "C" fn(*const u8, usize, u64) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_set_perm.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let bytes = path.as_bytes();
    ok_or_error(vdso_set_perm(bytes.as_ptr(), bytes.len(), perm))
}

pub fn set_file_perm(rt_fd: RtFd, perm: u64) -> Result<(), ErrorCode> {
    let vdso_set_file_perm: extern "C" fn(RtFd, u64) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_set_file_perm.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_set_file_perm(rt_fd, perm))
}

pub fn stat(path: &str) -> Result<FileAttr, ErrorCode> {
    let vdso_stat: extern "C" fn(*const u8, usize, *mut FileAttr) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_stat.load(Ordering::Relaxed) as usize as *const ()
        )
    };

    let bytes = path.as_bytes();
    let mut attr = FileAttr::new();

    match vdso_stat(bytes.as_ptr(), bytes.len(), &mut attr) {
        E_OK => Ok(attr),
        err => Err(err),
    }
}

pub fn canonicalize(path: &str) -> Result<alloc::string::String, ErrorCode> {
    let vdso_canonicalize: extern "C" fn(*const u8, usize, *mut u8, *mut usize) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_canonicalize.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let path = path.as_bytes();
    let mut bytes = [0_u8; MAX_PATH_LEN];
    let mut len = 0_usize;

    use alloc::borrow::ToOwned;
    match vdso_canonicalize(path.as_ptr(), path.len(), bytes.as_mut_ptr(), &mut len) {
        E_OK => Ok(core::str::from_utf8(&bytes[..len]).unwrap().to_owned()),
        err => Err(err),
    }
}

pub fn copy(from: &str, to: &str) -> Result<u64, ErrorCode> {
    let vdso_copy: extern "C" fn(*const u8, usize, *const u8, usize) -> i64 = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_copy.load(Ordering::Relaxed) as usize as *const ()
        )
    };

    let from = from.as_bytes();
    let to = to.as_bytes();
    to_result!(vdso_copy(from.as_ptr(), from.len(), to.as_ptr(), to.len()))
}

pub fn opendir(path: &str) -> Result<RtFd, ErrorCode> {
    let vdso_opendir: extern "C" fn(*const u8, usize) -> i32 = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_opendir.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let bytes = path.as_bytes();
    to_result!(vdso_opendir(bytes.as_ptr(), bytes.len()))
}

pub fn closedir(rt_fd: RtFd) -> Result<(), ErrorCode> {
    let vdso_closedir: extern "C" fn(i32) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_closedir.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    ok_or_error(vdso_closedir(rt_fd))
}

pub fn readdir(rt_fd: RtFd) -> Result<Option<DirEntry>, ErrorCode> {
    let vdso_readdir: extern "C" fn(i32, *mut DirEntry) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_readdir.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let mut dentry = DirEntry::new();
    match vdso_readdir(rt_fd, &mut dentry) {
        E_OK => Ok(Some(dentry)),
        E_NOT_FOUND => Ok(None),
        err => Err(err),
    }
}

pub fn getcwd() -> Result<alloc::string::String, ErrorCode> {
    let vdso_getcwd: extern "C" fn(*mut u8, *mut usize) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_getcwd.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let mut bytes = [0_u8; MAX_PATH_LEN];
    let mut len = 0_usize;

    use alloc::borrow::ToOwned;
    match vdso_getcwd(bytes.as_mut_ptr(), &mut len) {
        E_OK => Ok(core::str::from_utf8(&bytes[..len]).unwrap().to_owned()),
        err => Err(err),
    }
}

pub fn chdir(path: &str) -> Result<(), ErrorCode> {
    let vdso_chdir: extern "C" fn(*const u8, usize) -> ErrorCode = unsafe {
        core::mem::transmute(
            RtVdsoVtable::get().fs_chdir.load(Ordering::Relaxed) as usize as *const (),
        )
    };

    let bytes = path.as_bytes();
    ok_or_error(vdso_chdir(bytes.as_ptr(), bytes.len()))
}