summaryrefslogtreecommitdiff
path: root/ext/fs/lib.rs
blob: c1418815c69b5d837ce289f52e4d5a9c56693336 (plain)
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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

mod interface;
mod ops;
mod std_fs;

pub use crate::interface::File;
pub use crate::interface::FileSystem;
pub use crate::interface::FsDirEntry;
pub use crate::interface::FsError;
pub use crate::interface::FsFileType;
pub use crate::interface::FsPermissions;
pub use crate::interface::FsResult;
pub use crate::interface::FsStat;
pub use crate::interface::OpenOptions;
use crate::ops::*;

pub use crate::std_fs::StdFs;

use deno_core::OpState;
use std::cell::RefCell;
use std::convert::From;
use std::rc::Rc;

struct UnstableChecker {
  pub unstable: bool,
}

impl UnstableChecker {
  // NOTE(bartlomieju): keep in sync with `cli/program_state.rs`
  pub fn check_unstable(&self, api_name: &str) {
    if !self.unstable {
      eprintln!(
        "Unstable API '{api_name}'. The --unstable flag must be provided."
      );
      std::process::exit(70);
    }
  }
}

/// Helper for checking unstable features. Used for sync ops.
pub(crate) fn check_unstable(state: &OpState, api_name: &str) {
  state.borrow::<UnstableChecker>().check_unstable(api_name)
}

/// Helper for checking unstable features. Used for async ops.
pub(crate) fn check_unstable2(state: &Rc<RefCell<OpState>>, api_name: &str) {
  let state = state.borrow();
  state.borrow::<UnstableChecker>().check_unstable(api_name)
}

deno_core::extension!(deno_fs,
  deps = [ deno_web ],
  parameters = [Fs: FileSystem, P: FsPermissions],
  ops = [
    op_cwd<Fs, P>,
    op_umask<Fs>,
    op_chdir<Fs, P>,

    op_open_sync<Fs, P>,
    op_open_async<Fs, P>,
    op_mkdir_sync<Fs, P>,
    op_mkdir_async<Fs, P>,
    op_chmod_sync<Fs, P>,
    op_chmod_async<Fs, P>,
    op_chown_sync<Fs, P>,
    op_chown_async<Fs, P>,
    op_remove_sync<Fs, P>,
    op_remove_async<Fs, P>,
    op_copy_file_sync<Fs, P>,
    op_copy_file_async<Fs, P>,
    op_stat_sync<Fs, P>,
    op_stat_async<Fs, P>,
    op_lstat_sync<Fs, P>,
    op_lstat_async<Fs, P>,
    op_realpath_sync<Fs, P>,
    op_realpath_async<Fs, P>,
    op_read_dir_sync<Fs, P>,
    op_read_dir_async<Fs, P>,
    op_rename_sync<Fs, P>,
    op_rename_async<Fs, P>,
    op_link_sync<Fs, P>,
    op_link_async<Fs, P>,
    op_symlink_sync<Fs, P>,
    op_symlink_async<Fs, P>,
    op_read_link_sync<Fs, P>,
    op_read_link_async<Fs, P>,
    op_truncate_sync<Fs, P>,
    op_truncate_async<Fs, P>,
    op_utime_sync<Fs, P>,
    op_utime_async<Fs, P>,
    op_make_temp_dir_sync<Fs, P>,
    op_make_temp_dir_async<Fs, P>,
    op_make_temp_file_sync<Fs, P>,
    op_make_temp_file_async<Fs, P>,
    op_write_file_sync<Fs, P>,
    op_write_file_async<Fs, P>,
    op_read_file_sync<Fs, P>,
    op_read_file_async<Fs, P>,
    op_read_file_text_sync<Fs, P>,
    op_read_file_text_async<Fs, P>,

    op_seek_sync<Fs>,
    op_seek_async<Fs>,
    op_fdatasync_sync<Fs>,
    op_fdatasync_async<Fs>,
    op_fsync_sync<Fs>,
    op_fsync_async<Fs>,
    op_fstat_sync<Fs>,
    op_fstat_async<Fs>,
    op_flock_sync<Fs>,
    op_flock_async<Fs>,
    op_funlock_sync<Fs>,
    op_funlock_async<Fs>,
    op_ftruncate_sync<Fs>,
    op_ftruncate_async<Fs>,
    op_futime_sync<Fs>,
    op_futime_async<Fs>,

  ],
  esm = [ "30_fs.js" ],
  options = {
    unstable: bool,
    fs: Fs,
  },
  state = |state, options| {
    state.put(UnstableChecker { unstable: options.unstable });
    state.put(options.fs);
  },
);