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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
use std::{
    cell::{Cell, RefCell},
    convert::TryFrom,
    io::{Cursor, IoSlice, IoSliceMut, Read, Seek as _, SeekFrom, Write},
    path::{Path, PathBuf},
};

use blockchain_traits::{AccountMeta, Address, PendingTransaction};
use wasi_types::{
    ErrNo, Fd, FdFlags, FdStat, FileDelta, FileSize, FileStat, FileType, OpenFlags, Rights, Whence,
};

use crate::{
    file::{File, FileCache, FileKind, CHAIN_DIR_FILENO, HOME_DIR_FILENO},
    Result,
};

pub struct BCFS<A: Address, M: AccountMeta> {
    files: Vec<Option<File<A>>>,
    home_addr: A,
    _account_meta: std::marker::PhantomData<M>,
}

impl<A: Address, M: AccountMeta> BCFS<A, M> {
    /// Creates a new ptx FS with a backing `ptx` and hex stringified
    /// owner address.
    pub fn new<S: AsRef<str>>(
        home_addr: A,
        // ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        blockchain_name: S,
    ) -> Self {
        Self {
            files: File::defaults(blockchain_name.as_ref()),
            home_addr,
            _account_meta: std::marker::PhantomData,
        }
    }

    pub fn prestat(
        &mut self,
        _ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
    ) -> Result<&Path> {
        match &self.file(fd)?.kind {
            FileKind::Directory { path } => Ok(path),
            _ => Err(ErrNo::BadF),
        }
    }

    pub fn open(
        &mut self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        curdir: Fd,
        path: &Path,
        open_flags: OpenFlags,
        fd_flags: FdFlags,
    ) -> Result<Fd> {
        if open_flags.contains(OpenFlags::DIRECTORY) {
            // The virutal filesystem does not yet allow opening directories.
            return Err(ErrNo::NotSup);
        }

        match &self.file(curdir)?.kind {
            FileKind::Directory { .. } => (),
            _ => return Err(ErrNo::BadF),
        };

        let mut file_exists = true;
        let file_kind = match self.canonicalize_path(curdir, path)? {
            (None, path) if path == Path::new("log") => FileKind::Log,
            (Some(addr), path) if path == Path::new("balance") => FileKind::Balance { addr },
            (Some(addr), path) if path == Path::new("bytecode") => FileKind::Bytecode { addr },
            (Some(addr), path) if addr == self.home_addr => {
                let key = Self::key_for_path(&path)?;
                file_exists = ptx.state().contains(&key);
                if file_exists && open_flags.contains(OpenFlags::EXCL) {
                    return Err(ErrNo::Exist);
                } else if !file_exists && !open_flags.contains(OpenFlags::CREATE) {
                    return Err(ErrNo::NoEnt);
                } else if !file_exists {
                    ptx.state_mut().set(&key, &[]);
                    // ^ This must be done eagerly to match POSIX which immediately creates the file.
                }
                FileKind::Regular { key }
            }
            _ => return Err(ErrNo::NoEnt),
        };

        if file_kind.is_blockchain_intrinsic() {
            if open_flags.intersects(OpenFlags::CREATE | OpenFlags::EXCL) {
                return Err(ErrNo::Exist);
            }
            if open_flags.intersects(OpenFlags::TRUNC | OpenFlags::DIRECTORY)
                || (file_kind.is_log() && !fd_flags.contains(FdFlags::APPEND))
                || (!file_kind.is_log() && fd_flags.contains(FdFlags::APPEND))
            {
                return Err(ErrNo::Inval);
            }
        }

        let fd = self.alloc_fd()?;
        self.files.push(Some(File {
            kind: file_kind,
            flags: fd_flags,
            metadata: Cell::new(if file_exists {
                None
            } else {
                Some(FileStat {
                    device: 0u64.into(),
                    inode: 0u32.into(),
                    file_type: FileType::RegularFile,
                    num_links: 0,
                    file_size: 0,
                    atime: 0u64.into(),
                    mtime: 0u64.into(),
                    ctime: 0u64.into(),
                })
            }),
            buf: RefCell::new(if file_exists {
                FileCache::Absent(SeekFrom::Start(0))
            } else {
                FileCache::Present(Cursor::new(Vec::new()))
            }),
            dirty: Cell::new(false),
        }));
        Ok(fd)
    }

    pub fn flush(
        &mut self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
    ) -> Result<()> {
        self.do_flush(ptx, self.file(fd)?);
        Ok(())
    }

    pub fn close(
        &mut self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
    ) -> Result<()> {
        self.flush(ptx, fd)?;
        match self.files.get_mut(fd_usize(fd)) {
            Some(f) if f.is_some() => {
                *f = None;
                Ok(())
            }
            _ => Err(ErrNo::BadF),
        }
    }

    /// Removes the file at `path` and returns the number of bytes previously in the file.
    pub fn unlink(
        &mut self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        curdir: Fd,
        path: &Path,
    ) -> Result<u64> {
        let curdir_fileno = u32::from(curdir);
        if curdir_fileno != HOME_DIR_FILENO {
            return Err(ErrNo::Access);
        }

        let (addr, path) = self.canonicalize_path(curdir, path)?;
        match addr {
            Some(addr) if addr == self.home_addr => (),
            _ => return Err(ErrNo::Access),
        }

        if path == Path::new("balance") || path == Path::new("bytecode") {
            return Err(ErrNo::Access);
        }

        let key = Self::key_for_path(&path)?;
        let state = ptx.state_mut();
        let prev_len = state.get(&key).unwrap_or_default().len() as u64;
        state.remove(&key);
        Ok(prev_len)
    }

    pub fn seek(
        &mut self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
        offset: FileDelta,
        whence: Whence,
    ) -> Result<FileSize> {
        let file = self.file_mut(fd)?;

        let mut buf = file.buf.borrow_mut();

        if Whence::End == whence
            || match &*buf {
                FileCache::Absent(SeekFrom::End(_)) => true,
                _ => false,
            }
        {
            Self::populate_file(ptx, &file, &mut *buf)?;
        }

        match &mut *buf {
            FileCache::Present(ref mut cursor) => {
                Ok(cursor.seek(seekfrom_from_offset_whence(offset, whence)?)?)
            }
            FileCache::Absent(ref mut seek) => match whence {
                Whence::End => unreachable!("file was just populated"),
                Whence::Start => {
                    *seek = seekfrom_from_offset_whence(offset, whence)?;
                    Ok(offset as u64)
                }
                Whence::Current => match seek {
                    SeekFrom::Start(cur_offset) => {
                        let new_offset = Self::checked_offset(*cur_offset, offset)?;
                        *seek = SeekFrom::Start(new_offset);
                        Ok(new_offset as u64)
                    }
                    _ => unreachable!("handled above"),
                },
            },
        }
    }

    pub fn fdstat(
        &self,
        _ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
    ) -> Result<FdStat> {
        let file = self.file(fd)?;
        Ok(FdStat {
            file_type: FileType::RegularFile,
            flags: file.flags,
            rights_base: Rights::all(),
            rights_inheriting: Rights::all(),
        })
    }

    pub fn filestat(
        &self,
        ptx: &dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
    ) -> Result<FileStat> {
        let file = self.file(fd)?;
        Self::populate_file(ptx, file, &mut *file.buf.borrow_mut())
    }

    pub fn tell(
        &self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
    ) -> Result<FileSize> {
        let file = self.file(fd)?;
        let mut buf = file.buf.borrow_mut();
        if let FileCache::Absent(SeekFrom::End(_)) = &*buf {
            Self::populate_file(ptx, &file, &mut *buf)?;
        }
        Ok(match &mut *buf {
            FileCache::Present(cursor) => cursor.position(),
            FileCache::Absent(ref mut seekfrom) => match seekfrom {
                SeekFrom::Start(offset) => *offset,
                SeekFrom::End(_) => unreachable!("checked above"),
                SeekFrom::Current(_) => unreachable!(),
            },
        })
    }

    pub fn read_vectored(
        &mut self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
        bufs: &mut [IoSliceMut],
    ) -> Result<usize> {
        self.do_pread_vectored(ptx, fd, bufs, None)
    }

    pub fn pread_vectored(
        &self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
        bufs: &mut [IoSliceMut],
        offset: FileSize,
    ) -> Result<usize> {
        self.do_pread_vectored(ptx, fd, bufs, Some(SeekFrom::Start(offset)))
    }

    pub fn write_vectored(
        &mut self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
        bufs: &[IoSlice],
    ) -> Result<usize> {
        self.do_pwrite_vectored(ptx, fd, bufs, None)
    }

    pub fn pwrite_vectored(
        &mut self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
        bufs: &[IoSlice],
        offset: FileSize,
    ) -> Result<usize> {
        self.do_pwrite_vectored(ptx, fd, bufs, Some(SeekFrom::Start(offset)))
    }

    pub fn renumber(
        &mut self,
        _ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
        new_fd: Fd,
    ) -> Result<()> {
        if self.has_fd(fd) && self.has_fd(new_fd) {
            self.files.swap(fd_usize(fd), fd_usize(new_fd));
            self.files[fd_usize(fd)] = None;
            Ok(())
        } else {
            Err(ErrNo::BadF)
        }
    }

    pub fn sync(&mut self, ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>) {
        // flush stdout and stderr
        for file in self.files[1..=3].iter() {
            if let Some(file) = file {
                self.do_flush(ptx, file);
            }
        }
    }
}

fn fd_usize(fd: Fd) -> usize {
    usize::try_from(u32::from(fd)).unwrap() // can't fail because usize is at least 32 bits
}

fn seekfrom_from_offset_whence(offset: FileDelta, whence: Whence) -> Result<SeekFrom> {
    Ok(match whence {
        Whence::Current => SeekFrom::Current(offset),
        Whence::Start => SeekFrom::Start(if offset < 0 {
            return Err(ErrNo::Inval);
        } else {
            offset as u64
        }),
        Whence::End => SeekFrom::End(offset),
    })
}

impl<A: Address, M: AccountMeta> BCFS<A, M> {
    fn canonicalize_path(&self, curdir: Fd, path: &Path) -> Result<(Option<A>, PathBuf)> {
        use std::path::Component;

        if path.has_root() {
            return Err(ErrNo::NoEnt); // WASI paths must be releative to a preopened dir.
        }

        let curdir_fileno = u32::from(curdir);

        let mut canon_path = PathBuf::new();

        let mut comps = path
            .components()
            .skip_while(|comp| *comp == Component::CurDir)
            .peekable();

        let addr = if curdir_fileno == CHAIN_DIR_FILENO {
            match comps.peek() {
                Some(Component::Normal(maybe_addr)) => match maybe_addr.to_str().map(A::from_str) {
                    Some(Ok(addr)) => {
                        comps.next();
                        Some(addr)
                    }
                    _ => None,
                },
                Some(Component::Prefix(_)) | Some(Component::RootDir) => return Err(ErrNo::NoEnt),
                _ => None,
            }
        } else {
            Some(self.home_addr)
        };

        let mut has_path = false;
        for comp in comps {
            match comp {
                Component::Prefix(_) | Component::RootDir => return Err(ErrNo::NoEnt),
                Component::CurDir => (),
                Component::ParentDir => {
                    if !canon_path.pop() {
                        return Err(ErrNo::NoEnt);
                    }
                }
                Component::Normal(c) => {
                    has_path |= !c.is_empty();
                    canon_path.push(c);
                }
            }
        }

        if has_path {
            Ok((addr, canon_path))
        } else {
            Err(ErrNo::Inval)
        }
    }

    fn has_fd(&self, fd: Fd) -> bool {
        match self.files.get(fd_usize(fd)) {
            Some(Some(_)) => true,
            _ => false,
        }
    }

    fn file(&self, fd: Fd) -> Result<&File<A>> {
        match self.files.get(fd_usize(fd)) {
            Some(Some(file)) => Ok(file),
            _ => Err(ErrNo::BadF),
        }
    }

    fn file_mut(&mut self, fd: Fd) -> Result<&mut File<A>> {
        match self
            .files
            .get_mut(usize::try_from(u64::from(fd)).map_err(|_| ErrNo::BadF)?)
        {
            Some(Some(file)) => Ok(file),
            _ => Err(ErrNo::BadF),
        }
    }

    fn alloc_fd(&self) -> Result<Fd> {
        if self.files.len() >= u32::max_value() as usize {
            return Err(ErrNo::NFile); // TODO(#82)
        }
        Ok((self.files.len() as u32).into())
    }

    fn checked_offset(base: u64, offset: i64) -> Result<u64> {
        if offset >= 0 {
            base.checked_add(offset as u64)
        } else {
            base.checked_sub(-offset as u64)
        }
        .ok_or(ErrNo::Inval)
    }

    fn key_for_path(path: &Path) -> Result<Vec<u8>> {
        path.to_str()
            .ok_or(ErrNo::Inval)
            .map(|s| s.as_bytes().to_vec())
    }

    fn populate_file(
        ptx: &dyn PendingTransaction<Address = A, AccountMeta = M>,
        file: &File<A>,
        cache: &mut FileCache,
    ) -> Result<FileStat> {
        let file_size = match cache {
            FileCache::Present(cursor) => cursor.get_ref().len(),
            FileCache::Absent(offset) => {
                let bytes = match &file.kind {
                    FileKind::Stdin => ptx.input().to_vec(),
                    FileKind::Bytecode { addr } => match ptx.code_at(&addr) {
                        Some(code) => code.to_vec(),
                        None => return Err(ErrNo::NoEnt),
                    },
                    FileKind::Balance { addr } => match ptx.account_meta_at(&addr) {
                        Some(meta) => meta.balance().to_le_bytes().to_vec(),
                        None => return Err(ErrNo::NoEnt),
                    },
                    FileKind::Regular { key } => match ptx.state().get(&key) {
                        Some(val) => val.to_vec(),
                        None => return Err(ErrNo::NoEnt),
                    },
                    FileKind::Stdout | FileKind::Stderr | FileKind::Log => Vec::new(),
                    FileKind::Directory { .. } => return Err(ErrNo::Fault),
                };
                let file_size = bytes.len();
                let mut cursor = Cursor::new(bytes);
                cursor.seek(*offset)?;
                *cache = FileCache::Present(cursor);
                file_size
            }
        } as u64;
        match file.metadata.get() {
            Some(meta) => Ok(meta),
            None => {
                let meta = FileStat {
                    device: 0u64.into(),
                    inode: 0u64.into(), // TODO(#80)
                    file_type: FileType::RegularFile,
                    num_links: 0,
                    file_size,
                    atime: 0u64.into(), // TODO(#81)
                    mtime: 0u64.into(),
                    ctime: 0u64.into(),
                };
                file.metadata.set(Some(meta));
                Ok(meta)
            }
        }
    }

    fn do_pread_vectored(
        &self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
        bufs: &mut [IoSliceMut],
        offset: Option<SeekFrom>,
    ) -> Result<usize> {
        let file = self.file(fd)?;
        match file.kind {
            FileKind::Stdout | FileKind::Stderr { .. } | FileKind::Log { .. } => {
                return Err(ErrNo::Inval)
            }
            _ => (),
        };

        let mut buf = file.buf.borrow_mut();
        Self::populate_file(ptx, &file, &mut *buf)?;

        let cursor = match &mut *buf {
            FileCache::Present(ref mut cursor) => cursor,
            FileCache::Absent(_) => unreachable!("file was just populated"),
        };

        match offset {
            Some(offset) => {
                let orig_pos = cursor.position();
                cursor.seek(offset)?;
                let nbytes = cursor.read_vectored(bufs)?;
                cursor.set_position(orig_pos);
                Ok(nbytes)
            }
            None => Ok(cursor.read_vectored(bufs)?),
        }
    }

    fn do_pwrite_vectored(
        &mut self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        fd: Fd,
        bufs: &[IoSlice],
        offset: Option<SeekFrom>,
    ) -> Result<usize> {
        let file = self.file(fd)?;
        match file.kind {
            FileKind::Stdin | FileKind::Bytecode { .. } | FileKind::Balance { .. } => {
                return Err(ErrNo::Inval)
            }
            _ => (),
        };

        let mut buf = file.buf.borrow_mut();
        Self::populate_file(ptx, &file, &mut *buf)?;

        let cursor = match &mut *buf {
            FileCache::Present(ref mut cursor) => cursor,
            FileCache::Absent(_) => unreachable!("file was just populated"),
        };

        let nbytes = match offset {
            Some(offset) => {
                let orig_pos = cursor.position();
                cursor.seek(offset)?;
                let nbytes = cursor.write_vectored(bufs)?;
                cursor.set_position(orig_pos);
                nbytes
            }
            None => cursor.write_vectored(bufs)?,
        };
        if nbytes > 0 {
            file.dirty.replace(true);
        }
        Ok(nbytes)
    }

    fn do_flush(
        &self,
        ptx: &mut dyn PendingTransaction<Address = A, AccountMeta = M>,
        file: &File<A>,
    ) {
        if !file.dirty.get() {
            return;
        }
        let maybe_cursor = file.buf.borrow();
        let buf = match &*maybe_cursor {
            FileCache::Present(cursor) => cursor.get_ref(),
            FileCache::Absent(_) => return,
        };
        match &file.kind {
            FileKind::Stdin
            | FileKind::Bytecode { .. }
            | FileKind::Balance { .. }
            | FileKind::Directory { .. } => (),
            FileKind::Stdout => ptx.ret(buf),
            FileKind::Stderr => ptx.err(buf),
            FileKind::Log => {
                if let Some((topics, data)) = Self::parse_log(buf) {
                    ptx.emit(&topics, data);
                }
            }
            FileKind::Regular { key } => {
                ptx.state_mut().set(&key, &buf);
                for f in self.files[(HOME_DIR_FILENO as usize + 1)..].iter() {
                    if let Some(File {
                        kind: FileKind::Regular { key: f_key },
                        ..
                    }) = f
                    {
                        let f = f.as_ref().unwrap();
                        if key != f_key || f as *const File<A> == file as *const File<A> {
                            continue;
                        }
                        let mut f_buf = f.buf.borrow_mut();
                        let mut cursor = Cursor::new(buf.clone());
                        cursor
                            .seek(match &*f_buf {
                                FileCache::Absent(seek_from) => *seek_from,
                                FileCache::Present(cursor) => SeekFrom::Start(cursor.position()),
                            })
                            .ok(); // deal with the error when the file is actually read
                        *f_buf = FileCache::Present(cursor);
                        f.metadata.replace(None);
                    }
                }
                file.dirty.set(false);
            }
        }
    }

    /// Parses a log buffer into (topics, data)
    /// Format:
    /// num_topics [topic_len [topic_data; topic_len]; num_topics] data_len [data; data_len]
    /// num_* are little-endian 32-bit integers.
    fn parse_log(buf: &[u8]) -> Option<(Vec<&[u8]>, &[u8])> {
        use nom::{length_count, length_data, named, number::complete::le_u32};
        named!(parser<&[u8], Vec<&[u8]>>, length_count!(le_u32, length_data!(le_u32)));
        match parser(buf) {
            Ok((data, topics)) => Some((topics, data)),
            _ => None,
        }
    }
}