deno_fs 0.89.0

Ops for interacting with the file system
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
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

// Allow using Arc for this module.
#![allow(clippy::disallowed_types)]

use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::io::Error;
use std::io::ErrorKind;
use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;

use deno_core::parking_lot::Mutex;
use deno_io::fs::File;
use deno_io::fs::FsError;
use deno_io::fs::FsResult;
use deno_io::fs::FsStat;
use deno_path_util::normalize_path;

use crate::interface::AccessCheckCb;
use crate::interface::FsDirEntry;
use crate::interface::FsFileType;
use crate::FileSystem;
use crate::OpenOptions;

#[derive(Debug)]
enum PathEntry {
  Dir,
  File(Vec<u8>),
}

/// A very basic in-memory file system useful for swapping out in
/// the place of a RealFs for testing purposes.
///
/// Please develop this out as you need functionality.
#[derive(Debug, Default)]
pub struct InMemoryFs {
  entries: Mutex<HashMap<PathBuf, Arc<PathEntry>>>,
}

impl InMemoryFs {
  pub fn setup_text_files(&self, files: Vec<(String, String)>) {
    for (path, text) in files {
      let path = PathBuf::from(path);
      self.mkdir_sync(path.parent().unwrap(), true, None).unwrap();
      self
        .write_file_sync(
          &path,
          OpenOptions::write(true, false, false, None),
          None,
          &text.into_bytes(),
        )
        .unwrap();
    }
  }

  fn get_entry(&self, path: &Path) -> Option<Arc<PathEntry>> {
    let path = normalize_path(path);
    self.entries.lock().get(&path).cloned()
  }
}

#[async_trait::async_trait(?Send)]
impl FileSystem for InMemoryFs {
  fn cwd(&self) -> FsResult<PathBuf> {
    Err(FsError::NotSupported)
  }

  fn tmp_dir(&self) -> FsResult<PathBuf> {
    Err(FsError::NotSupported)
  }

  fn chdir(&self, _path: &Path) -> FsResult<()> {
    Err(FsError::NotSupported)
  }

  fn umask(&self, _mask: Option<u32>) -> FsResult<u32> {
    Err(FsError::NotSupported)
  }

  fn open_sync(
    &self,
    _path: &Path,
    _options: OpenOptions,
    _access_check: Option<AccessCheckCb>,
  ) -> FsResult<Rc<dyn File>> {
    Err(FsError::NotSupported)
  }
  async fn open_async<'a>(
    &'a self,
    path: PathBuf,
    options: OpenOptions,
    access_check: Option<AccessCheckCb<'a>>,
  ) -> FsResult<Rc<dyn File>> {
    self.open_sync(&path, options, access_check)
  }

  fn mkdir_sync(
    &self,
    path: &Path,
    recursive: bool,
    _mode: Option<u32>,
  ) -> FsResult<()> {
    let path = normalize_path(path);

    if let Some(parent) = path.parent() {
      let entry = self.entries.lock().get(parent).cloned();
      match entry {
        Some(entry) => match &*entry {
          PathEntry::File(_) => {
            return Err(FsError::Io(Error::new(
              ErrorKind::InvalidInput,
              "Parent is a file",
            )))
          }
          PathEntry::Dir => {}
        },
        None => {
          if recursive {
            self.mkdir_sync(parent, true, None)?;
          } else {
            return Err(FsError::Io(Error::new(
              ErrorKind::NotFound,
              "Not found",
            )));
          }
        }
      }
    }

    let entry = self.entries.lock().get(&path).cloned();
    match entry {
      Some(entry) => match &*entry {
        PathEntry::File(_) => Err(FsError::Io(Error::new(
          ErrorKind::InvalidInput,
          "Is a file",
        ))),
        PathEntry::Dir => Ok(()),
      },
      None => {
        self.entries.lock().insert(path, Arc::new(PathEntry::Dir));
        Ok(())
      }
    }
  }
  async fn mkdir_async(
    &self,
    path: PathBuf,
    recursive: bool,
    mode: Option<u32>,
  ) -> FsResult<()> {
    self.mkdir_sync(&path, recursive, mode)
  }

  fn chmod_sync(&self, _path: &Path, _mode: u32) -> FsResult<()> {
    Err(FsError::NotSupported)
  }
  async fn chmod_async(&self, path: PathBuf, mode: u32) -> FsResult<()> {
    self.chmod_sync(&path, mode)
  }

  fn chown_sync(
    &self,
    _path: &Path,
    _uid: Option<u32>,
    _gid: Option<u32>,
  ) -> FsResult<()> {
    Err(FsError::NotSupported)
  }
  async fn chown_async(
    &self,
    path: PathBuf,
    uid: Option<u32>,
    gid: Option<u32>,
  ) -> FsResult<()> {
    self.chown_sync(&path, uid, gid)
  }

  fn lchown_sync(
    &self,
    _path: &Path,
    _uid: Option<u32>,
    _gid: Option<u32>,
  ) -> FsResult<()> {
    Err(FsError::NotSupported)
  }

  async fn lchown_async(
    &self,
    path: PathBuf,
    uid: Option<u32>,
    gid: Option<u32>,
  ) -> FsResult<()> {
    self.lchown_sync(&path, uid, gid)
  }

  fn remove_sync(&self, _path: &Path, _recursive: bool) -> FsResult<()> {
    Err(FsError::NotSupported)
  }
  async fn remove_async(&self, path: PathBuf, recursive: bool) -> FsResult<()> {
    self.remove_sync(&path, recursive)
  }

  fn copy_file_sync(&self, _from: &Path, _to: &Path) -> FsResult<()> {
    Err(FsError::NotSupported)
  }
  async fn copy_file_async(&self, from: PathBuf, to: PathBuf) -> FsResult<()> {
    self.copy_file_sync(&from, &to)
  }

  fn cp_sync(&self, _from: &Path, _to: &Path) -> FsResult<()> {
    Err(FsError::NotSupported)
  }
  async fn cp_async(&self, from: PathBuf, to: PathBuf) -> FsResult<()> {
    self.cp_sync(&from, &to)
  }

  fn stat_sync(&self, path: &Path) -> FsResult<FsStat> {
    let entry = self.get_entry(path);
    match entry {
      Some(entry) => match &*entry {
        PathEntry::Dir => Ok(FsStat {
          is_file: false,
          is_directory: true,
          is_symlink: false,
          size: 0,
          mtime: None,
          atime: None,
          birthtime: None,
          ctime: None,
          dev: 0,
          ino: 0,
          mode: 0,
          nlink: 0,
          uid: 0,
          gid: 0,
          rdev: 0,
          blksize: 0,
          blocks: 0,
          is_block_device: false,
          is_char_device: false,
          is_fifo: false,
          is_socket: false,
        }),
        PathEntry::File(data) => Ok(FsStat {
          is_file: true,
          is_directory: false,
          is_symlink: false,
          size: data.len() as u64,
          mtime: None,
          atime: None,
          birthtime: None,
          ctime: None,
          dev: 0,
          ino: 0,
          mode: 0,
          nlink: 0,
          uid: 0,
          gid: 0,
          rdev: 0,
          blksize: 0,
          blocks: 0,
          is_block_device: false,
          is_char_device: false,
          is_fifo: false,
          is_socket: false,
        }),
      },
      None => Err(FsError::Io(Error::new(ErrorKind::NotFound, "Not found"))),
    }
  }
  async fn stat_async(&self, path: PathBuf) -> FsResult<FsStat> {
    self.stat_sync(&path)
  }

  fn lstat_sync(&self, _path: &Path) -> FsResult<FsStat> {
    Err(FsError::NotSupported)
  }
  async fn lstat_async(&self, path: PathBuf) -> FsResult<FsStat> {
    self.lstat_sync(&path)
  }

  fn realpath_sync(&self, _path: &Path) -> FsResult<PathBuf> {
    Err(FsError::NotSupported)
  }
  async fn realpath_async(&self, path: PathBuf) -> FsResult<PathBuf> {
    self.realpath_sync(&path)
  }

  fn read_dir_sync(&self, _path: &Path) -> FsResult<Vec<FsDirEntry>> {
    Err(FsError::NotSupported)
  }
  async fn read_dir_async(&self, path: PathBuf) -> FsResult<Vec<FsDirEntry>> {
    self.read_dir_sync(&path)
  }

  fn rename_sync(&self, _oldpath: &Path, _newpath: &Path) -> FsResult<()> {
    Err(FsError::NotSupported)
  }
  async fn rename_async(
    &self,
    oldpath: PathBuf,
    newpath: PathBuf,
  ) -> FsResult<()> {
    self.rename_sync(&oldpath, &newpath)
  }

  fn link_sync(&self, _oldpath: &Path, _newpath: &Path) -> FsResult<()> {
    Err(FsError::NotSupported)
  }
  async fn link_async(
    &self,
    oldpath: PathBuf,
    newpath: PathBuf,
  ) -> FsResult<()> {
    self.link_sync(&oldpath, &newpath)
  }

  fn symlink_sync(
    &self,
    _oldpath: &Path,
    _newpath: &Path,
    _file_type: Option<FsFileType>,
  ) -> FsResult<()> {
    Err(FsError::NotSupported)
  }
  async fn symlink_async(
    &self,
    oldpath: PathBuf,
    newpath: PathBuf,
    file_type: Option<FsFileType>,
  ) -> FsResult<()> {
    self.symlink_sync(&oldpath, &newpath, file_type)
  }

  fn read_link_sync(&self, _path: &Path) -> FsResult<PathBuf> {
    Err(FsError::NotSupported)
  }
  async fn read_link_async(&self, path: PathBuf) -> FsResult<PathBuf> {
    self.read_link_sync(&path)
  }

  fn truncate_sync(&self, _path: &Path, _len: u64) -> FsResult<()> {
    Err(FsError::NotSupported)
  }
  async fn truncate_async(&self, path: PathBuf, len: u64) -> FsResult<()> {
    self.truncate_sync(&path, len)
  }

  fn utime_sync(
    &self,
    _path: &Path,
    _atime_secs: i64,
    _atime_nanos: u32,
    _mtime_secs: i64,
    _mtime_nanos: u32,
  ) -> FsResult<()> {
    Err(FsError::NotSupported)
  }
  async fn utime_async(
    &self,
    path: PathBuf,
    atime_secs: i64,
    atime_nanos: u32,
    mtime_secs: i64,
    mtime_nanos: u32,
  ) -> FsResult<()> {
    self.utime_sync(&path, atime_secs, atime_nanos, mtime_secs, mtime_nanos)
  }

  fn lutime_sync(
    &self,
    _path: &Path,
    _atime_secs: i64,
    _atime_nanos: u32,
    _mtime_secs: i64,
    _mtime_nanos: u32,
  ) -> FsResult<()> {
    Err(FsError::NotSupported)
  }
  async fn lutime_async(
    &self,
    path: PathBuf,
    atime_secs: i64,
    atime_nanos: u32,
    mtime_secs: i64,
    mtime_nanos: u32,
  ) -> FsResult<()> {
    self.lutime_sync(&path, atime_secs, atime_nanos, mtime_secs, mtime_nanos)
  }

  fn write_file_sync(
    &self,
    path: &Path,
    options: OpenOptions,
    _access_check: Option<AccessCheckCb>,
    data: &[u8],
  ) -> FsResult<()> {
    let path = normalize_path(path);
    let has_parent_dir = path
      .parent()
      .and_then(|parent| self.get_entry(parent))
      .map(|e| matches!(*e, PathEntry::Dir))
      .unwrap_or(false);
    if !has_parent_dir {
      return Err(FsError::Io(Error::new(
        ErrorKind::NotFound,
        "Parent directory does not exist",
      )));
    }
    let mut entries = self.entries.lock();
    let entry = entries.entry(path.clone());
    match entry {
      Entry::Occupied(mut entry) => {
        if let PathEntry::File(existing_data) = &**entry.get() {
          if options.create_new {
            return Err(FsError::Io(Error::new(
              ErrorKind::AlreadyExists,
              "File already exists",
            )));
          }
          if options.append {
            let mut new_data = existing_data.clone();
            new_data.extend_from_slice(data);
            entry.insert(Arc::new(PathEntry::File(new_data)));
          } else {
            entry.insert(Arc::new(PathEntry::File(data.to_vec())));
          }
          Ok(())
        } else {
          Err(FsError::Io(Error::new(
            ErrorKind::InvalidInput,
            "Not a file",
          )))
        }
      }
      Entry::Vacant(entry) => {
        entry.insert(Arc::new(PathEntry::File(data.to_vec())));
        Ok(())
      }
    }
  }

  async fn write_file_async<'a>(
    &'a self,
    path: PathBuf,
    options: OpenOptions,
    access_check: Option<AccessCheckCb<'a>>,
    data: Vec<u8>,
  ) -> FsResult<()> {
    self.write_file_sync(&path, options, access_check, &data)
  }

  fn read_file_sync(
    &self,
    path: &Path,
    _access_check: Option<AccessCheckCb>,
  ) -> FsResult<Vec<u8>> {
    let entry = self.get_entry(path);
    match entry {
      Some(entry) => match &*entry {
        PathEntry::File(data) => Ok(data.clone()),
        PathEntry::Dir => Err(FsError::Io(Error::new(
          ErrorKind::InvalidInput,
          "Is a directory",
        ))),
      },
      None => Err(FsError::Io(Error::new(ErrorKind::NotFound, "Not found"))),
    }
  }
  async fn read_file_async<'a>(
    &'a self,
    path: PathBuf,
    access_check: Option<AccessCheckCb<'a>>,
  ) -> FsResult<Vec<u8>> {
    self.read_file_sync(&path, access_check)
  }
}