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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Types for interacting with Theseus standard I/O and file system via POSIX-style abstraction.
//!
//! This module provides APIs to:
//! * interact with a file descriptor table (open path, close fd, get underlying Theseus handle).
//! * read, write, or seek a file system node or standard I/O
//!
//! This abstraction is necessary as WASI assumes a POSIX-style file descriptor table interface.
//!

use alloc::string::String;
use core::{cmp, convert::TryFrom as _};
use fs_node::{DirRef, FileOrDir, FileRef, FsNode};
use hashbrown::HashMap;
use memfs::MemFile;
use path::{PathBuf, Path};

const FIRST_NONRESERVED_FD: wasi::Fd = 3;

/// File types that can be accessed through file descriptor table.
pub enum PosixNodeOrStdio {
    /// Standard input.
    Stdin,
    /// Standard output.
    Stdout,
    /// Standard error.
    Stderr,
    /// An underlying file system node.
    Inode(PosixNode),
}

impl PosixNodeOrStdio {
    /// Writes data from the given `buffer` to this file.
    ///
    /// The number of bytes written is dictated by the length of the given `buffer`.
    ///
    /// # Arguments
    /// * `buffer`: the buffer from which data will be written.
    ///
    /// # Return
    /// If successful, returns the number of bytes written to this file.
    /// Otherwise, returns a wasi::Errno.
    pub fn write(&mut self, buffer: &[u8]) -> Result<usize, wasi::Errno> {
        match self {
            PosixNodeOrStdio::Stdin => Err(wasi::ERRNO_NOTSUP),
            PosixNodeOrStdio::Stdout => match app_io::stdout().unwrap().write_all(buffer) {
                Ok(_) => Ok(buffer.len()),
                Err(_) => Err(wasi::ERRNO_IO),
            },
            PosixNodeOrStdio::Stderr => match app_io::stderr().unwrap().write_all(buffer) {
                Ok(_) => Ok(buffer.len()),
                Err(_) => Err(wasi::ERRNO_IO),
            },
            PosixNodeOrStdio::Inode(posix_node) => posix_node.write(buffer),
        }
    }

    /// Reads data from this file into the given `buffer`.
    ///
    /// The number of bytes read is dictated by the length of the given `buffer`.
    ///
    /// # Arguments
    /// * `buffer`: the buffer into which the data will be read.
    ///
    /// # Return
    /// If successful, returns the number of bytes read into this file.
    /// Otherwise, returns a wasi::Errno.
    pub fn read(&mut self, buffer: &mut [u8]) -> Result<usize, wasi::Errno> {
        match self {
            PosixNodeOrStdio::Stdin => match app_io::stdin().unwrap().read(buffer) {
                Ok(bytes_read) => Ok(bytes_read),
                Err(_) => Err(wasi::ERRNO_IO),
            },
            PosixNodeOrStdio::Stdout => Err(wasi::ERRNO_NOTSUP),
            PosixNodeOrStdio::Stderr => Err(wasi::ERRNO_NOTSUP),
            PosixNodeOrStdio::Inode(posix_node) => posix_node.read(buffer),
        }
    }

    /// Move the offset of this file.
    ///
    /// # Arguments
    /// * `offset`: the number of bytes to move.
    /// * `whence`: the base from which the offset is relative.
    ///
    /// # Return
    /// If successful, returns the resulting offset of this file.
    /// Otherwise, returns a wasi::Errno.
    pub fn seek(
        &mut self,
        offset: wasi::Filedelta,
        whence: wasi::Whence,
    ) -> Result<usize, wasi::Errno> {
        match self {
            PosixNodeOrStdio::Stdin => Err(wasi::ERRNO_NOTSUP),
            PosixNodeOrStdio::Stdout => Err(wasi::ERRNO_NOTSUP),
            PosixNodeOrStdio::Stderr => Err(wasi::ERRNO_NOTSUP),
            PosixNodeOrStdio::Inode(posix_node) => posix_node.seek(offset, whence),
        }
    }
}

/// A wrapper around Theseus FileOrDir to provide WASI-expected POSIX features.
pub struct PosixNode {
    /// Underlying Theseus FileOrDir.
    pub theseus_file_or_dir: FileOrDir,
    /// File system ights that apply to this file descriptor.
    fs_rights_base: wasi::Rights,
    /// Maximum set of rights applied to file descriptors opened through this file descriptor.
    fs_rights_inheriting: wasi::Rights,
    /// File descriptor flags.
    /// NOTE: contains unused flags for synchornized I/O, non-blocking mode.
    fs_flags: wasi::Fdflags,
    /// Offset of this file descriptor.
    offset: usize,
}

impl PosixNode {
    /// Instantiates a new PosixNode.
    ///
    /// # Arguments
    /// * `file_or_dir`: underlying Theseus FileOrDir.
    /// * `fs_rights`: rights applying to this file descriptor.
    /// * `fs_rights_inheriting`: rights applying to inherting file descriptors.
    /// * `fs_flags`: file descriptor flags.
    ///
    /// # Return
    /// Returns a PosixNode of a FileOrDir with specified permissions.
    pub fn new(
        file_or_dir: FileOrDir,
        fs_rights_base: wasi::Rights,
        fs_rights_inheriting: wasi::Rights,
        fs_flags: wasi::Fdflags,
    ) -> PosixNode {
        PosixNode {
            theseus_file_or_dir: file_or_dir,
            fs_rights_base,
            fs_rights_inheriting,
            fs_flags,
            offset: 0,
        }
    }

    /// Get path relative to working directory of this file descriptor.
    ///
    /// # Return
    /// Returns relative path of file descriptor as a string.
    pub fn get_relative_path(&self) -> String {
        let absolute_path = PathBuf::from(self.theseus_file_or_dir.get_absolute_path());
        let wd_path = task::with_current_task(|t|
            PathBuf::from(t.get_env().lock().cwd())
        ).expect("couldn't get current task");

        let relative_path = absolute_path.relative(wd_path).unwrap();
        String::from(relative_path)
    }

    /// Get file system rights of this file descriptor.
    ///
    /// # Return
    /// Returns file system rights of this file descriptor.
    pub fn fs_rights_base(&self) -> wasi::Rights {
        self.fs_rights_base
    }

    /// Get inheriting file system rights of this file descriptor.
    ///
    /// # Return
    /// Returns inheriting file system rights of this file descriptor.
    pub fn fs_rights_inheriting(&self) -> wasi::Rights {
        self.fs_rights_inheriting
    }

    /// Get file descriptor flags of this file descriptor.
    ///
    /// # Return
    /// Returns file descriptor flags of this file descriptor.
    pub fn fs_flags(&self) -> wasi::Fdflags {
        self.fs_flags
    }

    /// Set file descriptor flags of this file descriptor if allowed.
    ///
    /// # Return
    /// If successful, returns ().
    /// Otherwise, returns a wasi::Errno.
    pub fn set_fs_flags(&mut self, new_flags: wasi::Fdflags) -> Result<(), wasi::Errno> {
        // Verify that file descriptor has right to set flags.
        if self.fs_rights_base() & wasi::RIGHTS_FD_FDSTAT_SET_FLAGS == 0 {
            return Err(wasi::ERRNO_ACCES);
        }

        self.fs_flags = new_flags;
        Ok(())
    }

    /// Writes data from the given `buffer` to this file system node if allowed.
    ///
    /// The number of bytes written is dictated by the length of the given `buffer`.
    ///
    /// # Arguments
    /// * `buffer`: the buffer from which data will be written.
    ///
    /// # Return
    /// If successful, returns the number of bytes written to this file system node.
    /// Otherwise, returns a wasi::Errno.
    /// NOTE: Returns wasi::ERRNO_NOBUFS on Theseus file write error.
    pub fn write(&mut self, buffer: &[u8]) -> Result<usize, wasi::Errno> {
        // Verify that file descriptor has right to write.
        if self.fs_rights_base() & wasi::RIGHTS_FD_WRITE == 0 {
            return Err(wasi::ERRNO_ACCES);
        }

        match self.theseus_file_or_dir.clone() {
            FileOrDir::File(file_ref) => {
                // Check flags for append mode.
                let is_append_mode: bool = (self.fs_flags() & wasi::FDFLAGS_APPEND) != 0;

                if is_append_mode {
                    // Write to end of file.
                    let end_of_file_offset: usize = file_ref.lock().len();
                    match file_ref.lock().write_at(buffer, end_of_file_offset) {
                        Ok(bytes_written) => Ok(bytes_written),
                        Err(_) => Err(wasi::ERRNO_NOBUFS),
                    }
                } else {
                    // Write at offset of file and update offset.
                    let offset = self.offset;
                    match file_ref.lock().write_at(buffer, offset) {
                        Ok(bytes_written) => {
                            self.offset = self.offset.checked_add(bytes_written).unwrap();
                            Ok(bytes_written)
                        }
                        Err(_) => Err(wasi::ERRNO_NOBUFS),
                    }
                }
            }
            FileOrDir::Dir { .. } => Err(wasi::ERRNO_ISDIR),
        }
    }

    /// Reads data from this file system node into the given `buffer` if allowed.
    ///
    /// The number of bytes read is dictated by the length of the given `buffer`.
    ///
    /// # Arguments
    /// * `buffer`: the buffer into which the data will be read.
    ///
    /// # Return
    /// If successful, returns the number of bytes read into this file system node.
    /// Otherwise, returns a wasi::Errno.
    pub fn read(&mut self, buffer: &mut [u8]) -> Result<usize, wasi::Errno> {
        // Verify that file descriptor has right to read.
        if self.fs_rights_base() & wasi::RIGHTS_FD_READ == 0 {
            return Err(wasi::ERRNO_ACCES);
        }

        match self.theseus_file_or_dir.clone() {
            FileOrDir::File(file_ref) => {
                // Read at offset of file and update offset.
                let offset = self.offset;
                match file_ref.lock().read_at(buffer, offset) {
                    Ok(bytes_read) => {
                        self.offset = self.offset.checked_add(bytes_read).unwrap();
                        Ok(bytes_read)
                    }
                    Err(_) => Err(wasi::ERRNO_NOBUFS),
                }
            }
            FileOrDir::Dir { .. } => Err(wasi::ERRNO_ISDIR),
        }
    }

    /// Move the offset of this file system node.
    ///
    /// # Arguments
    /// * `offset`: the number of bytes to move.
    /// * `whence`: the base from which the offset is relative.
    ///
    /// # Return
    /// If successful, returns the resulting offset of this file system node.
    /// Otherwise, returns a wasi::Errno.
    pub fn seek(
        &mut self,
        delta: wasi::Filedelta,
        whence: wasi::Whence,
    ) -> Result<usize, wasi::Errno> {
        // Verify that file descriptor has right to seek.
        if self.fs_rights_base() & wasi::RIGHTS_FD_SEEK == 0 {
            return Err(wasi::ERRNO_ACCES);
        }

        match self.theseus_file_or_dir.clone() {
            FileOrDir::File(file_ref) => {
                let max_offset: usize = file_ref.lock().len();

                let signed_to_file_offset = |x: i64| -> usize {
                    cmp::min(usize::try_from(cmp::max(0, x)).unwrap(), max_offset)
                };

                let new_offset: usize = match whence {
                    wasi::WHENCE_CUR => {
                        signed_to_file_offset(i64::try_from(self.offset).unwrap() + delta)
                    }
                    wasi::WHENCE_END => {
                        signed_to_file_offset(i64::try_from(max_offset).unwrap() + delta)
                    }
                    wasi::WHENCE_SET => signed_to_file_offset(delta),
                    _ => {
                        return Err(wasi::ERRNO_SPIPE);
                    }
                };

                self.offset = new_offset;
                Ok(new_offset)
            }
            FileOrDir::Dir { .. } => Err(wasi::ERRNO_ISDIR),
        }
    }
}

/// File descriptor table.
pub struct FileDescriptorTable {
    /// HashMap from file descriptor number to POSIX-style file.
    fd_table: HashMap<wasi::Fd, PosixNodeOrStdio>,
}

impl FileDescriptorTable {
    /// Instantiates a new FileDescriptorTable with stdio entries filled.
    ///
    /// # Returns
    /// Returns a new FileDescriptorTable consisting of stdio entries.
    pub fn new() -> FileDescriptorTable {
        let mut fd_table = HashMap::new();
        fd_table.insert(wasi::FD_STDIN, PosixNodeOrStdio::Stdin);
        fd_table.insert(wasi::FD_STDOUT, PosixNodeOrStdio::Stdout);
        fd_table.insert(wasi::FD_STDERR, PosixNodeOrStdio::Stderr);
        FileDescriptorTable { fd_table }
    }

    /// Open file or directory at path in accordance to given open flags and insert in fd table.
    ///
    /// # Arguments
    /// * `path`: &str representing path of file or directory to open.
    /// * `starting_dir`: Theseus directory from which to search path from.
    /// * `lookup_flags`: flags determining behavior of path resolution.
    /// * `open_flags`: flags determining behavior of opening a file or directory.
    /// * `fs_rights`: rights applying to this file descriptor.
    /// * `fs_rights_inheriting`: rights applying to inherting file descriptors.
    /// * `fs_flags`: file descriptor flags.
    ///
    /// # Return
    /// If successful, returns resulting file descriptor number.
    /// Otherwise, returns a wasi::Errno.
    #[allow(clippy::too_many_arguments)]
    pub fn open_path(
        &mut self,
        path: &str,
        starting_dir: DirRef,
        lookup_flags: wasi::Lookupflags,
        open_flags: wasi::Oflags,
        fs_rights_base: wasi::Rights,
        fs_rights_inheriting: wasi::Rights,
        fs_flags: wasi::Fdflags,
    ) -> Result<wasi::Fd, wasi::Errno> {
        // NOTE: https://docs.rs/wasi/0.9.0+wasi-snapshot-preview1/wasi/constant.LOOKUPFLAGS_SYMLINK_FOLLOW.html
        // Unused as symlinks are currently not implemented.
        let _symlink_follow: bool = (lookup_flags & wasi::LOOKUPFLAGS_SYMLINK_FOLLOW) != 0;

        // Parse open flags.
        let create_file_if_no_exist: bool = (open_flags & wasi::OFLAGS_CREAT) != 0;
        let fail_if_not_dir: bool = (open_flags & wasi::OFLAGS_DIRECTORY) != 0;
        let fail_if_file_exists: bool = (open_flags & wasi::OFLAGS_EXCL) != 0;
        let truncate_file_to_size_zero: bool = (open_flags & wasi::OFLAGS_TRUNC) != 0;

        // Find first unused file descriptor number.
        // TODO: Potentially can implement a more efficient search data structure.
        let mut fd: wasi::Fd = FIRST_NONRESERVED_FD;
        while self.fd_table.contains_key(&fd) {
            fd += 1;
        }

        // Split path into parent directory path and base path.
        let file_path: &Path = path.as_ref();
        let parent_dir_path = file_path.parent().ok_or(wasi::ERRNO_NOENT)?;
        let base_name = file_path.file_name().ok_or(wasi::ERRNO_NOENT)?;
        let base_path: &Path = base_name.as_ref();

        // Get parent directory.
        let parent_dir: DirRef = match parent_dir_path.get(&starting_dir) {
            Some(file_or_dir) => match file_or_dir {
                FileOrDir::File { .. } => {
                    return Err(wasi::ERRNO_NOENT);
                }
                FileOrDir::Dir(dir_ref) => dir_ref,
            },
            None => {
                return Err(wasi::ERRNO_NOENT);
            }
        };

        // Open file or directory at path in accordance to open flags.
        let opened_file_or_dir: FileOrDir = match base_path.get(&parent_dir) {
            Some(file_or_dir) => match file_or_dir {
                FileOrDir::File { .. } => {
                    if fail_if_file_exists {
                        return Err(wasi::ERRNO_EXIST);
                    } else if fail_if_not_dir {
                        return Err(wasi::ERRNO_NOTDIR);
                    } else if truncate_file_to_size_zero {
                        // HACK: Truncate file by overwriting file.
                        let new_file: FileRef =
                            MemFile::create(String::from(base_name), &parent_dir).unwrap();
                        FileOrDir::File(new_file)
                    } else {
                        file_or_dir
                    }
                }
                FileOrDir::Dir { .. } => file_or_dir,
            },
            None => {
                if create_file_if_no_exist {
                    let new_file: FileRef =
                        MemFile::create(String::from(base_name), &parent_dir).unwrap();
                    FileOrDir::File(new_file)
                } else {
                    return Err(wasi::ERRNO_NOENT);
                }
            }
        };

        // Insert POSIX-style file in file descriptor table with given rights and flags.
        self.fd_table.insert(
            fd,
            PosixNodeOrStdio::Inode(PosixNode::new(
                opened_file_or_dir,
                fs_rights_base,
                fs_rights_inheriting,
                fs_flags,
            )),
        );

        Ok(fd)
    }

    /// Close a given file descriptor if not standard I/O.
    ///
    /// # Arguments
    /// * `fd`: file descriptor number to be closed.
    ///
    /// # Return
    /// If successful, returns ().
    /// Otherwise, returns a wasi::Errno.
    pub fn close_fd(&mut self, fd: wasi::Fd) -> Result<(), wasi::Errno> {
        if self.fd_table.contains_key(&fd)
            && fd != wasi::FD_STDIN
            && fd != wasi::FD_STDOUT
            && fd != wasi::FD_STDERR
        {
            self.fd_table.remove(&fd);
            return Ok(());
        }
        Err(wasi::ERRNO_BADF)
    }

    /// Get POSIX-style file from file descriptor number.
    ///
    /// # Arguments
    /// * `fd`: file descriptor number to access.
    ///
    /// # Return
    /// Returns corresponding POSIX-style file if exists.
    pub fn get_posix_node_or_stdio(&mut self, fd: wasi::Fd) -> Option<&mut PosixNodeOrStdio> {
        self.fd_table.get_mut(&fd)
    }

    /// Get file system node from file descriptor number.
    ///
    /// This method makes it easier to access an underlying file system node from a fd number.
    ///
    /// # Arguments
    /// * `fd`: file descriptor number to access.
    ///
    /// # Return
    /// Returns corresponding file system node if exists.
    pub fn get_posix_node(&mut self, fd: wasi::Fd) -> Option<&mut PosixNode> {
        if let Some(PosixNodeOrStdio::Inode(posix_node)) = self.get_posix_node_or_stdio(fd) {
            Some(posix_node)
        } else {
            None
        }        
    }
}