sos-vfs 0.3.2

Virtual file system same as tokio::fs.
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
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
#[cfg(all(test, feature = "mem-fs"))]
mod tests {
    use anyhow::Result;

    use std::ffi::OsString;
    use std::io::SeekFrom;
    use std::path::{PathBuf, MAIN_SEPARATOR};

    use crate::memory::{self as vfs, File, OpenOptions, Permissions};
    use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};

    #[tokio::test]
    async fn memory_vfs() -> Result<()> {
        file_write_read().await?;
        file_append().await?;
        file_seek().await?;
        read_to_string().await?;
        metadata().await?;
        file_overwrite().await?;
        set_len().await?;
        absolute_file_write().await?;
        copy_file().await?;
        set_permissions().await?;

        write_read().await?;
        remove_file().await?;
        create_dir_remove_dir().await?;
        create_dir_all_remove_dir_all().await?;
        read_dir().await?;
        rename().await?;
        rename_replace_file().await?;

        canonicalize().await?;

        Ok(())
    }

    #[tokio::test]
    async fn rename_with_metadata() -> Result<()> {
        let from = "/foo/from.txt";
        let to = "/foo/to.txt";
        let data = "data to copy";

        vfs::create_dir("foo").await?;

        vfs::write(from, data.as_bytes()).await?;
        assert!(vfs::try_exists(from).await?);

        vfs::rename(from, to).await?;
        assert!(vfs::try_exists(to).await?);

        // list directory
        let mut dir_reader = vfs::read_dir("/foo").await?;
        while let Some(entry) = dir_reader.next_entry().await? {
            let entry_metadata = entry.metadata().await?;
            assert!(entry_metadata.is_file());
            assert_eq!(entry_metadata.len(), data.len() as u64);
        }

        Ok(())
    }

    #[tokio::test]
    async fn poll_complete_after_write() -> Result<()> {
        use futures::future::poll_fn;
        use std::pin::Pin;
        use tokio::io::{AsyncSeek, AsyncWriteExt};

        let path = "pos_after_write.txt";
        let mut f = File::create(path).await?;
        f.write_all(b"hello").await?; // 5 bytes
        f.flush().await?; // wait for the write to finish

        // Ask the file itself where it thinks the cursor is.
        let mut pinned = Pin::new(&mut f);
        let pos = poll_fn(|cx| pinned.as_mut().poll_complete(cx)).await?;

        // **BUG**: returns 0 with the current implementation
        assert_eq!(pos, 5, "cursor should sit just past the 5 written bytes");
        Ok(())
    }

    #[tokio::test]
    async fn poll_complete_after_set_len() -> Result<()> {
        use futures::future::poll_fn;
        use std::pin::Pin;
        use tokio::io::{AsyncSeek, AsyncSeekExt, AsyncWriteExt};

        let path = "pos_after_set_len.txt";
        let mut f = File::create(path).await?;
        f.write_all(b"abcdef").await?; // 6 bytes
        f.flush().await?;
        f.seek(std::io::SeekFrom::Start(2)).await?; // move to byte 2

        f.set_len(100).await?; // extend – should *not* move the cursor

        let mut pinned = Pin::new(&mut f);
        let pos = poll_fn(|cx| pinned.as_mut().poll_complete(cx)).await?;

        // **BUG**: current code reports 0 instead of 2
        assert_eq!(pos, 2, "set_len must preserve the current offset");
        Ok(())
    }

    #[tokio::test]
    async fn poll_complete_after_read() -> Result<()> {
        use futures::future::poll_fn;
        use std::pin::Pin;
        use tokio::io::{AsyncReadExt, AsyncSeek, AsyncWriteExt};

        // Create a test file with some content
        let path = "pos_after_read.txt";
        let mut f = File::create(path).await?;
        f.write_all(b"hello world").await?; // 11 bytes
        f.flush().await?;

        // Close and reopen the file for reading
        drop(f);
        let mut f = File::open(path).await?;

        // Read part of the content
        let mut buf = [0u8; 5]; // Only read first 5 bytes
        f.read_exact(&mut buf).await?;
        assert_eq!(&buf, b"hello");

        // Ask the file where it thinks the cursor is
        let mut pinned = Pin::new(&mut f);
        let pos = poll_fn(|cx| pinned.as_mut().poll_complete(cx)).await?;

        // Verify the cursor has advanced by the number of bytes read
        assert_eq!(pos, 5, "cursor should sit just past the 5 read bytes");

        // Read more bytes and check position again
        f.read_exact(&mut buf).await?; // Read next 5 bytes
        assert_eq!(&buf, b" worl");

        let mut pinned = Pin::new(&mut f);
        let pos = poll_fn(|cx| pinned.as_mut().poll_complete(cx)).await?;

        // Verify cursor has advanced further
        assert_eq!(pos, 10, "cursor should advance after additional reads");

        Ok(())
    }

    #[tokio::test]
    async fn seek_after_pending_write() -> Result<()> {
        use tokio::io::{AsyncSeekExt, AsyncWriteExt};

        let path = "seek_after_pending.txt";
        let mut f = File::create(path).await?;
        f.write_all(b"xyz").await?; // leaves an in-flight write

        // Try to find out where we are without an explicit flush.
        // Tokio’s real File lets this succeed; our version currently errors.
        let pos = f.seek(std::io::SeekFrom::Current(0)).await;

        // **BUG**: expect Ok(3), get Err(other operation is pending)
        assert_eq!(
            pos.unwrap(),
            3,
            "seek should wait for the write to finish"
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_sync_all() -> Result<()> {
        let buf = [0u8; 1024];
        let path = "test_sync_all.txt";
        let tmp_path = "test_sync_all.txt.tmp";
        let mut f = OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(&tmp_path)
            .await?;
        f.write_all(&buf).await?;
        f.sync_all().await?;
        assert!(vfs::try_exists(&tmp_path).await?);
        assert_eq!(vfs::metadata(&tmp_path).await?.len(), buf.len() as u64);
        vfs::rename(tmp_path, path).await?;
        assert!(vfs::try_exists(path).await?);
        assert_eq!(vfs::metadata(path).await?.len(), buf.len() as u64);
        Ok(())
    }

    async fn file_write_read() -> Result<()> {
        let path = "test.txt";
        let contents = "Mock content";

        let mut fd = File::create(path).await?;
        fd.write_all(contents.as_bytes()).await?;
        fd.flush().await?;
        assert!(vfs::try_exists(path).await?);

        let mut file_contents = Vec::new();
        let mut fd = File::open(path).await?;
        fd.read_to_end(&mut file_contents).await?;
        assert_eq!(contents.as_bytes(), &file_contents);

        vfs::remove_file(path).await?;

        Ok(())
    }

    async fn file_append() -> Result<()> {
        let path = "test.txt";
        vfs::write(path, "one".as_bytes()).await?;

        let mut fd = OpenOptions::new()
            .write(true)
            .append(true)
            .open(path)
            .await?;
        fd.write_all("two".as_bytes()).await?;
        fd.flush().await?;

        let file_contents = vfs::read(path).await?;
        assert_eq!("onetwo".as_bytes(), &file_contents);

        vfs::remove_file(path).await?;

        Ok(())
    }

    async fn file_seek() -> Result<()> {
        let path = "test.txt";
        vfs::write(path, "one".as_bytes()).await?;

        let mut fd = OpenOptions::new().write(true).open(path).await?;
        fd.seek(SeekFrom::End(0)).await?;
        fd.write_all("two".as_bytes()).await?;
        fd.flush().await?;

        fd.seek(SeekFrom::Start(1)).await?;
        let mut buf = [0; 2];
        fd.read_exact(&mut buf).await?;

        let val = std::str::from_utf8(&buf).unwrap();
        assert_eq!("ne", val);

        let file_contents = vfs::read(path).await?;
        assert_eq!("onetwo".as_bytes(), &file_contents);

        vfs::remove_file(path).await?;

        Ok(())
    }

    async fn read_to_string() -> Result<()> {
        let path = "test.txt";
        let contents = "Mock content";
        vfs::write(path, contents.as_bytes()).await?;
        assert!(vfs::try_exists(path).await?);

        let file_contents = vfs::read_to_string(path).await?;
        assert_eq!(contents, &file_contents);

        vfs::remove_file(&path).await?;
        Ok(())
    }

    async fn metadata() -> Result<()> {
        let path = "test.txt";
        let contents = "Mock content";
        vfs::write(path, contents.as_bytes()).await?;
        assert!(vfs::try_exists(path).await?);

        let metadata = vfs::metadata(path).await?;
        assert_eq!(contents.len(), metadata.len() as usize);
        assert!(metadata.is_file());

        let dir_path = "test-dir";
        vfs::create_dir(dir_path).await?;

        let metadata = vfs::metadata(dir_path).await?;
        assert_eq!(0, metadata.len() as usize);
        assert!(metadata.is_dir());

        vfs::remove_file(path).await?;
        vfs::remove_dir(dir_path).await?;
        Ok(())
    }

    async fn file_overwrite() -> Result<()> {
        let path = "test.txt";
        let one = "one";
        let two = "two";

        vfs::write(path, one.as_bytes()).await?;
        let contents = vfs::read_to_string(path).await?;
        assert_eq!(one, &contents);

        vfs::write(path, two.as_bytes()).await?;
        let contents = vfs::read_to_string(path).await?;
        assert_eq!(two, &contents);

        vfs::remove_file(path).await?;

        Ok(())
    }

    async fn set_len() -> Result<()> {
        let path = "test.txt";

        let fd = File::create(path).await?;
        // Extend length with zeroes
        fd.set_len(1024).await?;

        let metadata = fd.metadata().await?;
        assert_eq!(1024, metadata.len());

        // Truncate length
        fd.set_len(512).await?;

        let metadata = fd.metadata().await?;
        assert_eq!(512, metadata.len());

        vfs::remove_file(path).await?;

        Ok(())
    }

    async fn absolute_file_write() -> Result<()> {
        let parent = "/foo/bar/baz";
        vfs::create_dir_all(parent).await?;
        assert!(vfs::try_exists(parent).await?);

        let file = format!("{}/qux.vault", parent);

        vfs::write(&file, "mock").await?;
        assert!(vfs::try_exists(&file).await?);

        vfs::remove_dir_all("/foo").await?;

        Ok(())
    }

    async fn copy_file() -> Result<()> {
        let from = "from.txt";
        let to = "to.txt";
        let data = "data to copy";

        vfs::write(from, data.as_bytes()).await?;
        assert!(vfs::try_exists(from).await?);

        // Copy to same path is a noop
        assert!(vfs::copy(from, from).await.is_ok());

        vfs::copy(from, to).await?;
        assert!(vfs::try_exists(to).await?);

        let file_contents = vfs::read(to).await?;
        assert_eq!(data.as_bytes(), &file_contents);

        // Trigger the code path that overwrites an existing file
        vfs::copy(from, to).await?;

        vfs::remove_file(from).await?;
        vfs::remove_file(to).await?;

        Ok(())
    }

    async fn set_permissions() -> Result<()> {
        let path = "test.txt";

        vfs::write(path, "mock").await?;
        assert!(vfs::try_exists(path).await?);

        let mut perm: Permissions = Default::default();
        perm.set_readonly(true);

        vfs::set_permissions(path, perm).await?;
        assert!(vfs::metadata(path).await?.permissions().readonly());

        vfs::remove_file(path).await?;

        Ok(())
    }

    async fn write_read() -> Result<()> {
        let path = "test.txt";
        let contents = b"Mock content".to_vec();
        vfs::write(path, &contents).await?;

        assert!(vfs::try_exists(path).await?);

        let file_contents = vfs::read(path).await?;
        assert_eq!(&contents, &file_contents);

        vfs::remove_file(path).await?;
        Ok(())
    }

    async fn remove_file() -> Result<()> {
        let path = "test.txt";
        let contents = b"Mock content".to_vec();
        vfs::write(path, &contents).await?;

        vfs::remove_file(path).await?;
        assert!(!vfs::try_exists(path).await?);
        Ok(())
    }

    async fn create_dir_remove_dir() -> Result<()> {
        vfs::create_dir("foo").await?;
        assert!(vfs::try_exists("foo").await?);

        vfs::remove_dir("foo").await?;
        assert!(!vfs::try_exists("foo").await?);
        Ok(())
    }

    async fn create_dir_all_remove_dir_all() -> Result<()> {
        vfs::create_dir_all("foo/bar").await?;
        assert!(vfs::try_exists("foo").await?);
        assert!(vfs::try_exists("foo/bar").await?);

        vfs::remove_dir_all("foo").await?;
        assert!(!vfs::try_exists("foo/bar").await?);
        assert!(!vfs::try_exists("foo").await?);
        Ok(())
    }

    async fn read_dir() -> Result<()> {
        let dir = "read-dir";
        vfs::create_dir(dir).await?;

        let one = b"one".to_vec();
        let two = b"two".to_vec();
        vfs::write("read-dir/abc.txt", &one).await?;
        vfs::write("read-dir/def.txt", &two).await?;
        vfs::create_dir("read-dir/ghi").await?;

        let mut dir_reader = vfs::read_dir(dir).await?;
        let first = dir_reader.next_entry().await?;

        assert_eq!(
            OsString::from("abc.txt"),
            first.as_ref().unwrap().file_name()
        );
        assert_eq!(
            PathBuf::from("/read-dir/abc.txt"),
            first.as_ref().unwrap().path()
        );
        assert!(first.as_ref().unwrap().file_type().await?.is_file());

        let second = dir_reader.next_entry().await?;
        assert_eq!(
            OsString::from("def.txt"),
            second.as_ref().unwrap().file_name()
        );
        assert_eq!(
            PathBuf::from("/read-dir/def.txt"),
            second.as_ref().unwrap().path()
        );
        assert!(second.as_ref().unwrap().file_type().await?.is_file());

        let third = dir_reader.next_entry().await?;
        assert_eq!(
            OsString::from("ghi"),
            third.as_ref().unwrap().file_name()
        );
        assert_eq!(
            PathBuf::from("/read-dir/ghi"),
            third.as_ref().unwrap().path()
        );
        assert!(third.as_ref().unwrap().file_type().await?.is_dir());

        vfs::remove_dir_all("read-dir").await?;

        Ok(())
    }

    async fn rename() -> Result<()> {
        vfs::create_dir("foo").await?;
        let exists = vfs::try_exists("foo").await?;
        assert!(exists);

        vfs::rename("foo", "bar").await?;
        assert!(!vfs::try_exists("foo").await?);
        assert!(vfs::try_exists("bar").await?);

        vfs::remove_dir_all("bar").await?;

        Ok(())
    }

    async fn rename_replace_file() -> Result<()> {
        vfs::write("foo.txt", b"foo").await?;
        vfs::write("bar.txt", b"bar").await?;
        assert!(vfs::try_exists("foo.txt").await?);
        assert!(vfs::try_exists("bar.txt").await?);

        vfs::rename("foo.txt", "bar.txt").await?;

        assert!(!vfs::try_exists("foo.txt").await?);
        assert!(vfs::try_exists("bar.txt").await?);

        Ok(())
    }

    async fn canonicalize() -> Result<()> {
        assert!(vfs::canonicalize("").await.is_err());
        assert_eq!(
            PathBuf::from(MAIN_SEPARATOR.to_string()),
            vfs::canonicalize(MAIN_SEPARATOR.to_string()).await?
        );

        vfs::create_dir("baz").await?;
        assert!(vfs::try_exists("baz").await?);
        vfs::create_dir_all("foo/bar/qux").await?;
        assert!(vfs::try_exists("foo").await?);
        assert!(vfs::try_exists("foo/bar").await?);
        assert!(vfs::try_exists("foo/bar/qux").await?);

        assert_eq!(PathBuf::from("/"), vfs::canonicalize("foo/..").await?,);

        assert_eq!(
            PathBuf::from("/foo"),
            vfs::canonicalize("foo/././.").await?,
        );

        assert_eq!(
            PathBuf::from("/baz"),
            vfs::canonicalize("foo/../baz").await?,
        );

        assert_eq!(
            PathBuf::from("/foo/bar/qux"),
            vfs::canonicalize("foo/../foo/bar/qux").await?,
        );

        assert_eq!(
            PathBuf::from("/"),
            vfs::canonicalize("foo/bar/../..").await?,
        );

        assert_eq!(
            PathBuf::from("/foo/bar/qux"),
            vfs::canonicalize("foo/bar/../../foo/bar/qux").await?,
        );

        Ok(())
    }
}