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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
use std::ffi::OsString;
use std::io::{Read, Result, Seek, Write};
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::time::SystemTime;

use derivative::Derivative;
use futures::{Future, TryStreamExt};
use rsfs_tokio::unix_ext::{GenFSExt, PermissionsExt};
use rsfs_tokio::{DirEntry, File, FileType, GenFS, Metadata};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncSeek, AsyncSeekExt, AsyncWrite, AsyncWriteExt};
pub type InMemoryUnixFS = rsfs_tokio::mem::unix::FS;

// TODO: DirBuilder, OpenOptions
use crate::{
    FloppyDirBuilder, FloppyDirEntry, FloppyDisk, FloppyFile, FloppyFileType, FloppyMetadata,
    FloppyPermissions, FloppyReadDir, FloppyUnixPermissions,
};

#[derive(Derivative)]
#[derivative(Debug)]
pub struct MemFloppyDisk {
    fs: InMemoryUnixFS,
}

impl MemFloppyDisk {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Self {
            fs: InMemoryUnixFS::new(),
        }
    }
}

#[async_trait::async_trait]
impl<'a> FloppyDisk<'a> for MemFloppyDisk {
    type Metadata = MemMetadata;
    type ReadDir = MemReadDir;
    type Permissions = MemPermissions;
    type DirBuilder = MemDirBuilder<'a>;

    async fn canonicalize<P: AsRef<Path> + Send>(&self, path: P) -> Result<PathBuf> {
        self.fs.canonicalize(path).await
    }

    async fn copy<P: AsRef<Path> + Send>(&self, from: P, to: P) -> Result<u64> {
        self.fs.copy(from, to).await
    }

    async fn create_dir<P: AsRef<Path> + Send>(&self, path: P) -> Result<()> {
        self.fs.create_dir(path).await
    }

    async fn create_dir_all<P: AsRef<Path> + Send>(&self, path: P) -> Result<()> {
        self.fs.create_dir_all(path).await
    }

    async fn hard_link<P: AsRef<Path> + Send>(&self, _src: P, _dst: P) -> Result<()> {
        unimplemented!("hard links are not yet supported")
    }

    async fn metadata<P: AsRef<Path> + Send>(&self, path: P) -> Result<Self::Metadata> {
        let metadata = self.fs.metadata(path).await?;
        Ok(Self::Metadata { metadata })
    }

    async fn read<P: AsRef<Path> + Send>(&self, path: P) -> Result<Vec<u8>> {
        let mut file = self.fs.open_file(path).await?;
        let file_len = file.metadata().await?.len() as usize;
        let mut buffer = vec![0u8; file_len];
        let read = file.read(&mut buffer).await?;
        debug_assert!(read <= buffer.len());
        Ok(buffer)
    }

    async fn read_dir<P: AsRef<Path> + Send>(&self, path: P) -> Result<Self::ReadDir> {
        self.fs.read_dir(path).await.map(MemReadDir::new)
    }

    async fn read_link<P: AsRef<Path> + Send>(&self, path: P) -> Result<PathBuf> {
        self.fs.read_link(path).await
    }

    async fn read_to_string<P: AsRef<Path> + Send>(&self, path: P) -> Result<String> {
        let mut file = self.fs.open_file(path).await?;
        let file_len = file.metadata().await?.len() as usize;
        let mut buffer = String::with_capacity(file_len);
        file.read_to_string(&mut buffer).await?;
        Ok(buffer)
    }

    async fn remove_dir<P: AsRef<Path> + Send>(&self, path: P) -> Result<()> {
        self.fs.remove_dir(path).await
    }

    async fn remove_dir_all<P: AsRef<Path> + Send>(&self, path: P) -> Result<()> {
        self.fs.remove_dir_all(path).await
    }

    async fn remove_file<P: AsRef<Path> + Send>(&self, path: P) -> Result<()> {
        self.fs.remove_file(path).await
    }

    async fn rename<P: AsRef<Path> + Send>(&self, from: P, to: P) -> Result<()> {
        self.fs.rename(from, to).await
    }

    async fn set_permissions<P: AsRef<Path> + Send>(
        &mut self,
        path: P,
        perm: Self::Permissions,
    ) -> Result<()> {
        self.fs
            .set_permissions(path, rsfs_tokio::mem::Permissions::from_mode(perm.mode()))
            .await
    }

    async fn symlink<P: AsRef<Path> + Send>(&self, src: P, dst: P) -> Result<()> {
        self.fs.symlink(src, dst).await
    }

    async fn symlink_metadata<P: AsRef<Path> + Send>(&self, path: P) -> Result<Self::Metadata> {
        self.fs
            .symlink_metadata(path)
            .await
            .map(|metadata| Self::Metadata { metadata })
    }

    async fn try_exists<P: AsRef<Path> + Send>(&self, path: P) -> Result<bool> {
        Ok(self.fs.metadata(path).await.is_ok())
    }

    async fn write<P: AsRef<Path> + Send>(
        &self,
        path: P,
        contents: impl AsRef<[u8]> + Send,
    ) -> Result<()> {
        let mut file = self.fs.create_file(path).await?;
        let contents = contents.as_ref();
        file.write_all(contents).await?;
        Ok(())
    }

    fn new_dir_builder(&'a self) -> Self::DirBuilder {
        MemDirBuilder {
            fs: self,
            recursive: false,
            #[cfg(unix)]
            mode: 0o777,
        }
    }
}

#[derive(Derivative)]
#[derivative(Debug)]
pub struct MemFile {
    file: rsfs_tokio::mem::unix::File,
}

#[async_trait::async_trait]
impl FloppyFile for MemFile {
    type Metadata = MemMetadata;
    type Permissions = MemPermissions;

    async fn sync_all(&mut self) -> Result<()> {
        Ok(())
    }

    async fn sync_data(&mut self) -> Result<()> {
        Ok(())
    }

    async fn set_len(&mut self, size: u64) -> Result<()> {
        self.file.set_len(size).await
    }

    async fn metadata(&self) -> Result<Self::Metadata> {
        Ok(Self::Metadata {
            metadata: self.file.metadata().await?,
        })
    }

    async fn try_clone(&self) -> Result<Box<Self>> {
        Ok(Box::new(Self {
            file: self.file.try_clone().await?,
        }))
    }

    async fn set_permissions(&self, perm: Self::Permissions) -> Result<()> {
        self.file
            .set_permissions(rsfs_tokio::mem::Permissions::from_mode(perm.mode()))
            .await
    }

    async fn permissions(&self) -> Result<Self::Permissions> {
        Ok(Self::Permissions {
            mode: self.file.metadata().await?.permissions().mode(),
        })
    }
}

impl AsyncSeek for MemFile {
    fn start_seek(mut self: Pin<&mut Self>, position: std::io::SeekFrom) -> Result<()> {
        let mut this = self.as_mut();
        let file = Pin::new(&mut this.file);
        file.start_seek(position)
    }

    fn poll_complete(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<u64>> {
        let mut this = self.as_mut();
        let file = Pin::new(&mut this.file);
        file.poll_complete(cx)
    }
}

impl AsyncRead for MemFile {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>,
    ) -> std::task::Poll<Result<()>> {
        let mut this = self.as_mut();
        let file = Pin::new(&mut this.file);
        file.poll_read(cx, buf)
    }
}

impl AsyncWrite for MemFile {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &[u8],
    ) -> std::task::Poll<Result<usize>> {
        let mut this = self.as_mut();
        let file = Pin::new(&mut this.file);
        file.poll_write(cx, buf)
    }

    fn poll_flush(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<()>> {
        let mut this = self.as_mut();
        let file = Pin::new(&mut this.file);
        file.poll_flush(cx)
    }

    fn poll_shutdown(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<()>> {
        let mut this = self.as_mut();
        let file = Pin::new(&mut this.file);
        file.poll_shutdown(cx)
    }
}

impl Read for MemFile {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        run_here_outside_of_tokio_context(async { self.file.read(buf).await })
    }
}

impl Write for MemFile {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        run_here_outside_of_tokio_context(async { self.file.write(buf).await })
    }

    fn flush(&mut self) -> Result<()> {
        run_here_outside_of_tokio_context(async { self.file.flush().await })
    }
}

impl Seek for MemFile {
    fn seek(&mut self, pos: std::io::SeekFrom) -> Result<u64> {
        run_here_outside_of_tokio_context(async { self.file.seek(pos).await })
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MemPermissions {
    mode: u32,
}

impl FloppyPermissions for MemPermissions {
    fn readonly(&self) -> bool {
        self.mode & 0o222 == 0
    }

    fn set_readonly(&mut self, readonly: bool) {
        if readonly {
            self.mode &= !0o222;
        } else {
            self.mode |= 0o222;
        }
    }
}

impl FloppyUnixPermissions for MemPermissions {
    fn mode(&self) -> u32 {
        self.mode
    }

    fn set_mode(&mut self, mode: u32) {
        self.mode = mode;
    }

    fn from_mode(mode: u32) -> Self {
        Self { mode }
    }
}

#[derive(Debug)]
pub struct MemMetadata {
    metadata: rsfs_tokio::mem::unix::Metadata,
}

#[async_trait::async_trait]
impl FloppyMetadata for MemMetadata {
    type FileType = MemFileType;

    type Permissions = MemPermissions;

    async fn file_type(&self) -> Self::FileType {
        MemFileType(self.metadata.file_type())
    }

    async fn is_dir(&self) -> bool {
        self.metadata.is_dir()
    }

    async fn is_file(&self) -> bool {
        self.metadata.is_file()
    }

    async fn is_symlink(&self) -> bool {
        self.metadata.file_type().is_symlink()
    }

    async fn len(&self) -> u64 {
        self.metadata.len()
    }

    async fn permissions(&self) -> Self::Permissions {
        MemPermissions {
            mode: self.metadata.permissions().mode(),
        }
    }

    async fn modified(&self) -> Result<SystemTime> {
        self.metadata.modified()
    }

    async fn accessed(&self) -> Result<SystemTime> {
        self.metadata.accessed()
    }

    async fn created(&self) -> Result<SystemTime> {
        self.metadata.created()
    }
}

#[derive(Debug)]
pub struct MemFileType(#[doc(hidden)] rsfs_tokio::mem::unix::FileType);

impl FloppyFileType for MemFileType {
    fn is_dir(&self) -> bool {
        self.0.is_dir()
    }

    fn is_file(&self) -> bool {
        self.0.is_file()
    }

    fn is_symlink(&self) -> bool {
        self.0.is_symlink()
    }
}

#[derive(Debug)]
pub struct MemReadDir {
    read_dir: rsfs_tokio::mem::unix::ReadDir,
}

impl MemReadDir {
    fn new(read_dir: rsfs_tokio::mem::unix::ReadDir) -> Self {
        Self { read_dir }
    }
}

#[async_trait::async_trait]
impl FloppyReadDir for MemReadDir {
    type DirEntry = MemDirEntry;

    async fn next_entry(&mut self) -> Result<Option<Self::DirEntry>> {
        match self.read_dir.try_next().await {
            Ok(Some(Some(entry))) => Ok(Some(MemDirEntry { entry })),
            Ok(Some(None)) => Ok(None),
            Ok(None) => Ok(None),
            Err(e) => Err(e),
        }
    }
}

#[derive(Debug)]
pub struct MemDirEntry {
    entry: rsfs_tokio::mem::unix::DirEntry,
}

#[async_trait::async_trait]
impl FloppyDirEntry for MemDirEntry {
    type Metadata = MemMetadata;
    type FileType = MemFileType;

    fn path(&self) -> PathBuf {
        self.entry.path()
    }
    fn file_name(&self) -> OsString {
        self.entry.file_name()
    }
    async fn metadata(&self) -> Result<Self::Metadata> {
        Ok(Self::Metadata {
            metadata: self.entry.metadata().await?,
        })
    }
    async fn file_type(&self) -> Result<Self::FileType> {
        Ok(MemFileType(self.entry.file_type().await?))
    }

    #[cfg(unix)]
    fn ino(&self) -> u64 {
        unimplemented!("not currently supported")
    }
}

#[derive(Debug)]
pub struct MemDirBuilder<'a> {
    fs: &'a MemFloppyDisk,
    recursive: bool,
    #[cfg(unix)]
    mode: u32,
}

#[async_trait::async_trait]
impl FloppyDirBuilder for MemDirBuilder<'_> {
    fn recursive(&mut self, recursive: bool) -> &mut Self {
        self.recursive = recursive;
        self
    }

    async fn create<P: AsRef<Path> + Send>(&self, path: P) -> Result<()> {
        if self.recursive {
            self.fs.create_dir_all(path).await
        } else {
            self.fs.create_dir(path).await
        }
    }

    #[cfg(unix)]
    fn mode(&mut self, mode: u32) -> &mut Self {
        self.mode = mode;
        self
    }
}

#[allow(unused)]
fn run_here<F: Future>(fut: F) -> F::Output {
    // TODO: This is evil
    // Adapted from https://stackoverflow.com/questions/66035290
    let handle = tokio::runtime::Handle::try_current().unwrap();
    let _guard = handle.enter();
    futures::executor::block_on(fut)
}

#[allow(unused)]
fn run_here_outside_of_tokio_context<F: Future>(fut: F) -> F::Output {
    // TODO: This is slightly less-evil than the previous one but still pretty bad
    let rt = tokio::runtime::Builder::new_current_thread()
        .build()
        .unwrap();

    rt.block_on(fut)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::*;
    use std::io::Result;

    #[tokio::test]
    async fn test_mem_floppy_disk() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        assert_eq!("asdf", fs.read_to_string("/test.txt").await?);

        Ok(())
    }

    // FIXME
    // #[tokio::test]
    // async fn test_canonicalize() -> Result<()> {
    //     let fs = MemFloppyDisk::new();

    //     assert_eq!(PathBuf::from("/"), fs.canonicalize("/").await?);
    //     assert_eq!(PathBuf::from("/"), fs.canonicalize("/.").await?);
    //     assert_eq!(PathBuf::from("/"), fs.canonicalize("/..").await?);
    //     assert_eq!(PathBuf::from("/"), fs.canonicalize("/../..").await?);
    //     assert_eq!(PathBuf::from("a"), fs.canonicalize("a").await?);
    //     assert_eq!(PathBuf::from("a"), fs.canonicalize("a/.").await?);
    //     assert_eq!(PathBuf::from("/a"), fs.canonicalize("/a/.").await?);
    //     assert_eq!(PathBuf::from("/a"), fs.canonicalize("/a/../a").await?);
    //     assert_eq!(
    //         PathBuf::from("/"),
    //         fs.canonicalize("/usr/bin/../../../../../../..").await?
    //     );

    //     Ok(())
    // }

    #[tokio::test]
    async fn test_copy() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        fs.copy("/test.txt", "/test2.txt").await?;
        assert_eq!("asdf", fs.read_to_string("/test2.txt").await?);

        Ok(())
    }

    #[tokio::test]
    async fn test_create_dir() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.create_dir("/test").await?;
        let metadata = fs.metadata("/test").await?;
        assert!(metadata.is_dir().await);

        Ok(())
    }

    #[tokio::test]
    async fn test_create_dir_all() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.create_dir_all("/test/a/b/c").await?;
        let metadata = fs.metadata("/test/a/b/c").await?;
        assert!(metadata.is_dir().await);

        Ok(())
    }

    // #[tokio::test]
    // async fn test_hard_link() -> Result<()> {
    //     let mut fs = MemFloppyDisk::new();
    //     fs.write("/test.txt", "asdf").await?;
    //     fs.hard_link("/test.txt", "/test2.txt").await?;
    //     assert_eq!("asdf", fs.read_to_string("/test2.txt").await?);

    //     Ok(())
    // }

    #[tokio::test]
    async fn test_metadata() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        let metadata = fs.metadata("/test.txt").await?;
        assert!(metadata.is_file().await);
        assert_eq!(4, metadata.len().await);

        Ok(())
    }

    #[tokio::test]
    async fn test_read() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        let buf = fs.read("/test.txt").await?;
        assert_eq!(b"asdf", buf.as_slice());

        Ok(())
    }

    #[tokio::test]
    async fn test_read_dir() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        fs.create_dir("/test").await?;
        let mut entries = fs.read_dir("/").await?;
        let entry = entries.next_entry().await?;
        assert!(entry.is_some());
        let entry = entry.unwrap();
        assert_eq!("test", entry.file_name().to_str().unwrap());
        assert!(entry.file_type().await?.is_dir());

        let entry = entries.next_entry().await?;
        assert!(entry.is_some());
        let entry = entry.unwrap();
        assert_eq!("test.txt", entry.file_name().to_str().unwrap());
        assert!(entry.file_type().await?.is_file());

        assert!(entries.next_entry().await?.is_none());

        Ok(())
    }

    #[tokio::test]
    async fn test_read_link() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        fs.symlink("/test.txt", "/test2.txt").await?;
        let s = fs.read_link("/test2.txt").await?;
        assert_eq!(PathBuf::from("/test.txt"), s);

        Ok(())
    }

    #[tokio::test]
    async fn test_read_to_string() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        let s = fs.read_to_string("/test.txt").await?;
        assert_eq!("asdf", s);

        Ok(())
    }

    #[tokio::test]
    async fn test_remove_dir() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.create_dir("/test").await?;
        fs.remove_dir("/test").await?;
        assert!(fs.metadata("/test").await.is_err());

        Ok(())
    }

    #[tokio::test]
    async fn test_remove_dir_all() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.create_dir_all("/test/a/b/c").await?;
        fs.remove_dir_all("/test").await?;
        assert!(fs.metadata("/test").await.is_err());
        assert!(fs.metadata("/test/a").await.is_err());
        assert!(fs.metadata("/test/a/b").await.is_err());
        assert!(fs.metadata("/test/a/b/c").await.is_err());

        Ok(())
    }

    #[tokio::test]
    async fn test_remove_file() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        fs.remove_file("/test.txt").await?;
        assert!(fs.metadata("/test.txt").await.is_err());

        Ok(())
    }

    #[tokio::test]
    async fn test_rename() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        fs.rename("/test.txt", "/test2.txt").await?;
        assert!(fs.metadata("/test.txt").await.is_err());
        assert_eq!("asdf", fs.read_to_string("/test2.txt").await?);

        Ok(())
    }

    #[tokio::test]
    async fn test_set_permissions() -> Result<()> {
        let mut fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        fs.set_permissions("/test.txt", MemPermissions::from_mode(0o777))
            .await?;
        let metadata = fs.metadata("/test.txt").await?;
        assert_eq!(0o777, metadata.permissions().await.mode());

        Ok(())
    }

    #[tokio::test]
    async fn test_symlink_metadata() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        fs.symlink("/test.txt", "/test2.txt").await?;
        let metadata = fs.symlink_metadata("/test2.txt").await?;
        assert!(metadata.is_symlink().await);

        Ok(())
    }

    #[tokio::test]
    async fn test_symlink() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        fs.symlink("/test.txt", "/test2.txt").await?;
        let s = fs.read_link("/test2.txt").await?;
        assert_eq!(PathBuf::from("/test.txt"), s);

        Ok(())
    }

    #[tokio::test]
    async fn test_write() -> Result<()> {
        let fs = MemFloppyDisk::new();
        fs.write("/test.txt", "asdf").await?;
        assert_eq!("asdf", fs.read_to_string("/test.txt").await?);

        Ok(())
    }

    #[tokio::test]
    async fn test_dir_builder() -> Result<()> {
        let fs = MemFloppyDisk::new();
        {
            let mut builder = fs.new_dir_builder();
            builder.recursive(true);
            builder.create("/test/a/b/c").await?;

            assert!(fs.metadata("/test").await?.is_dir().await);
            assert!(fs.metadata("/test/a").await?.is_dir().await);
            assert!(fs.metadata("/test/a/b").await?.is_dir().await);
            assert!(fs.metadata("/test/a/b/c").await?.is_dir().await);
        }

        {
            let mut builder = fs.new_dir_builder();
            builder.recursive(false);
            let res = builder.create("/test2/a/b/c").await;
            assert!(res.is_err());
        }

        Ok(())
    }
}