flop 0.2.7

floppy-disk facades for common archive formats!
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
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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
macro_rules! archive_format {
    ( $format:ident, $fixture:expr, $open:expr, $close:expr ) => {
        paste::paste! {
            use std::ffi::OsString;
            use std::io::Result;
            use std::path::{Path, PathBuf};
            use std::time::SystemTime;

            use floppy_disk::mem::*;
            use floppy_disk::prelude::*;
            use indexmap::IndexSet;
            use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite};
            use tokio::pin;
            use tokio::sync::Mutex;
            use tracing::trace;

            pub(crate) struct [< $format InternalMetadata >] {
                pub delegate: MemFloppyDisk,
                pub compression: CompressionType,
                pub ordered_paths: IndexSet<PathBuf>,
            }

            #[derive(Debug)]
            pub struct [< $format FloppyDisk >] {
                delegate: MemFloppyDisk,
                compression: smoosh::CompressionType,
                path: PathBuf,
                ordered_paths: Mutex<IndexSet<PathBuf>>,
            }

            impl [< $format FloppyDisk >] {
                pub async fn open<'a, P: AsRef<Path>>(path: P) -> Result<[< $format FloppyDisk >]> {
                    let path = path.as_ref();
                    let metadata: [< $format InternalMetadata >] = $open(path).await?;
                    Ok(Self {
                        delegate: metadata.delegate,
                        compression: metadata.compression,
                        path: path.to_path_buf(),
                        ordered_paths: Mutex::new(metadata.ordered_paths),
                    })
                }

                pub async fn close(self) -> Result<()> {
                    let ordered_paths = &*self.ordered_paths.lock().await;
                    $close(&self.delegate, &self.path, self.compression, ordered_paths).await
                }

                pub(crate) async fn add_path<P: AsRef<Path> + Send>(&self, path: P) {
                    let path = path.as_ref().to_path_buf();
                    let path = if !path.starts_with("/") {
                        PathBuf::from("/").join(path)
                    } else {
                        path
                    };
                    trace!("adding ordered path: {}", path.display());
                    self.ordered_paths.lock().await.insert(path);
                }

                pub(crate) async fn remove_path<P: AsRef<Path> + Send>(&self, path: P) {
                    let path = path.as_ref().to_path_buf();
                    let path = if !path.starts_with("/") {
                        PathBuf::from("/").join(path)
                    } else {
                        path
                    };
                    trace!("removing ordered path: {}", path.display());
                    self.ordered_paths.lock().await.remove(&path);
                }
            }

            #[async_trait::async_trait]
            impl<'a> FloppyDisk<'a> for [< $format FloppyDisk >] {
                type DirBuilder = [< $format DirBuilder >]<'a>;
                type DirEntry = [< $format DirEntry >];
                type File = [< $format File >];
                type FileType = [< $format FileType >];
                type Metadata = [< $format Metadata >];
                type OpenOptions = [< $format OpenOptions >];
                type Permissions = [< $format Permissions >];
                type ReadDir = [< $format ReadDir >];

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

                async fn copy<P: AsRef<Path> + Send>(&self, from: P, to: P) -> Result<u64> {
                    {
                        let to = to.as_ref();
                        self.add_path(to).await;
                    }
                    self.delegate.copy(from, to).await
                }

                async fn create_dir<P: AsRef<Path> + Send>(&self, path: P) -> Result<()> {
                    {
                        let path = path.as_ref();
                        self.add_path(path).await;
                    }
                    self.delegate.create_dir(path).await
                }

                async fn create_dir_all<P: AsRef<Path> + Send>(&self, path: P) -> Result<()> {
                    {
                        let mut path = path.as_ref();
                        // Since we collect the parent paths in reverse order,
                        // we have to reverse them to ensure they end up in the
                        // stream in the correct order.
                        let mut parents = vec![path.to_path_buf()];
                        while let Some(parent) = path.parent() {
                            parents.push(parent.into());
                            path = parent;
                        }
                        parents.reverse();
                        for parent in parents {
                            self.add_path(parent).await;
                        }
                    }
                    self.delegate.create_dir_all(path).await
                }

                async fn hard_link<P: AsRef<Path> + Send>(&self, src: P, dst: P) -> Result<()> {
                    {let path = dst.as_ref();self.add_path(path.to_path_buf()).await;}
                    self.delegate.hard_link(src, dst).await
                }

                async fn metadata<P: AsRef<Path> + Send>(&self, path: P) -> Result<Self::Metadata> {
                    self.delegate.metadata(path).await.map([< $format Metadata >])
                }

                async fn read<P: AsRef<Path> + Send>(&self, path: P) -> Result<Vec<u8>> {
                    self.delegate.read(path).await
                }

                async fn read_dir<P: AsRef<Path> + Send>(&self, path: P) -> Result<Self::ReadDir> {
                    self.delegate.read_dir(path).await.map([< $format ReadDir >])
                }

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

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

                async fn remove_dir<P: AsRef<Path> + Send>(&self, path: P) -> Result<()> {
                    {let path = path.as_ref();self.remove_path(path.to_path_buf()).await;}
                    self.delegate.remove_dir(path).await
                }

                async fn remove_dir_all<P: AsRef<Path> + Send>(&self, path: P) -> Result<()> {
                    {
                        let mut path = path.as_ref();
                        while let Some(parent) = path.parent() {
                            self.remove_path(parent).await;
                            path = parent;
                        }
                    }
                    self.delegate.remove_dir_all(path).await
                }

                async fn remove_file<P: AsRef<Path> + Send>(&self, path: P) -> Result<()> {
                    {
                        let path = path.as_ref();
                        self.remove_path(path.to_path_buf()).await;
                    }
                    self.delegate.remove_file(path).await
                }

                async fn rename<P: AsRef<Path> + Send>(&self, from: P, to: P) -> Result<()> {
                    {
                        let from = from.as_ref();
                        self.remove_path(from).await;
                    }
                    {
                        let to = to.as_ref();
                        self.add_path(to).await;
                    }
                    self.delegate.rename(from, to).await
                }

                async fn set_permissions<P: AsRef<Path> + Send>(
                    &self,
                    path: P,
                    perm: Self::Permissions,
                ) -> Result<()> {
                    self.delegate.set_permissions(path, perm.0).await
                }

                async fn symlink<P: AsRef<Path> + Send>(&self, src: P, dst: P) -> Result<()> {
                    {
                        let dst = dst.as_ref();
                        self.add_path(dst).await;
                    }
                    self.delegate.symlink(src, dst).await
                }

                async fn symlink_metadata<P: AsRef<Path> + Send>(
                    &self,
                    path: P,
                ) -> Result<Self::Metadata> {
                    self.delegate.symlink_metadata(path).await.map([< $format Metadata >])
                }

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

                async fn write<P: AsRef<Path> + Send>(
                    &self,
                    path: P,
                    contents: impl AsRef<[u8]> + Send,
                ) -> Result<()> {
                    {
                        let path = path.as_ref();
                        self.add_path(path).await;
                    }
                    self.delegate.write(path, contents).await
                }

                fn new_dir_builder(&'a self) -> Self::DirBuilder {
                    [< $format DirBuilder >] {
                        delegate: self,
                        recursive: false,
                        mode: 0o777,
                    }
                }
            }

            #[async_trait::async_trait]
            impl FloppyDiskUnixExt for [< $format FloppyDisk >] {
                async fn chown<P: Into<PathBuf> + Send>(
                    &self,
                    path: P,
                    uid: u32,
                    gid: u32,
                ) -> Result<()> {
                    self.delegate.chown(path, uid, gid).await
                }
            }

            #[derive(Debug)]
            pub struct [< $format DirBuilder >]<'a> {
                #[doc(hidden)] delegate: &'a [< $format FloppyDisk >],
                #[doc(hidden)] recursive: bool,
                #[doc(hidden)] mode: u32,
            }

            #[async_trait::async_trait]
            impl FloppyDirBuilder for [< $format DirBuilder >]<'_> {
                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.delegate.create_dir_all(path).await
                    } else {
                        self.delegate.create_dir(path).await
                    }
                }

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

            #[derive(Debug)]
            #[repr(transparent)]
            pub struct [< $format DirEntry >](#[doc(hidden)] MemDirEntry);

            #[async_trait::async_trait]
            impl<'a> FloppyDirEntry<'a, [< $format FloppyDisk >]> for [< $format DirEntry >] {
                fn path(&self) -> PathBuf {
                    self.0.path()
                }

                fn file_name(&self) -> OsString {
                    self.0.file_name()
                }

                async fn metadata(&self) -> Result<<[< $format FloppyDisk >] as FloppyDisk<'a>>::Metadata> {
                    self.0.metadata().await.map([< $format Metadata >])
                }

                async fn file_type(&self) -> Result<<[< $format FloppyDisk >] as FloppyDisk<'a>>::FileType> {
                    self.0.file_type().await.map([< $format FileType >])
                }

                #[cfg(unix)]
                fn ino(&self) -> u64 {
                    self.0.ino()
                }
            }

            #[derive(Debug)]
            #[repr(transparent)]
            pub struct [< $format File >](#[doc(hidden)] MemFile);

            #[async_trait::async_trait]
            impl<'a> FloppyFile<'a, [< $format FloppyDisk >]> for [< $format File >] {
                async fn sync_all(&mut self) -> Result<()> {
                    self.0.sync_all().await
                }

                async fn sync_data(&mut self) -> Result<()> {
                    self.0.sync_data().await
                }

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

                async fn metadata(&self) -> Result<<[< $format FloppyDisk >] as FloppyDisk>::Metadata> {
                    self.0.metadata().await.map([< $format Metadata >])
                }

                async fn try_clone(&'a self) -> Result<Box<<[< $format FloppyDisk >] as FloppyDisk>::File>> {
                    Ok(Box::new([< $format File >](*self.0.try_clone().await?)))
                }

                async fn set_permissions(
                    &self,
                    perm: <[< $format FloppyDisk >] as FloppyDisk>::Permissions,
                ) -> Result<()> {
                    Ok(self.0.set_permissions(perm.0).await?)
                }

                async fn permissions(&self) -> Result<<[< $format FloppyDisk >] as FloppyDisk>::Permissions> {
                    self.0.permissions().await.map([< $format Permissions >])
                }
            }

            impl AsyncRead for [< $format File >] {
                fn poll_read(
                    self: std::pin::Pin<&mut Self>,
                    cx: &mut std::task::Context<'_>,
                    buf: &mut tokio::io::ReadBuf<'_>,
                ) -> std::task::Poll<std::io::Result<()>> {
                    let this = self.get_mut();
                    let delegate = &mut this.0;
                    pin!(delegate);
                    AsyncRead::poll_read(delegate, cx, buf)
                }
            }

            impl AsyncWrite for [< $format File >] {
                fn poll_write(
                    self: std::pin::Pin<&mut Self>,
                    cx: &mut std::task::Context<'_>,
                    buf: &[u8],
                ) -> std::task::Poll<std::result::Result<usize, std::io::Error>> {
                    let this = self.get_mut();
                    let delegate = &mut this.0;
                    pin!(delegate);
                    AsyncWrite::poll_write(delegate, cx, buf)
                }

                fn poll_flush(
                    self: std::pin::Pin<&mut Self>,
                    cx: &mut std::task::Context<'_>,
                ) -> std::task::Poll<std::result::Result<(), std::io::Error>> {
                    let this = self.get_mut();
                    let delegate = &mut this.0;
                    pin!(delegate);
                    AsyncWrite::poll_flush(delegate, cx)
                }

                fn poll_shutdown(
                    self: std::pin::Pin<&mut Self>,
                    cx: &mut std::task::Context<'_>,
                ) -> std::task::Poll<std::result::Result<(), std::io::Error>> {
                    let this = self.get_mut();
                    let delegate = &mut this.0;
                    pin!(delegate);
                    AsyncWrite::poll_shutdown(delegate, cx)
                }
            }

            impl AsyncSeek for [< $format File >] {
                fn start_seek(
                    self: std::pin::Pin<&mut Self>,
                    position: std::io::SeekFrom,
                ) -> std::io::Result<()> {
                    let this = self.get_mut();
                    let delegate = &mut this.0;
                    pin!(delegate);
                    AsyncSeek::start_seek(delegate, position)
                }

                fn poll_complete(
                    self: std::pin::Pin<&mut Self>,
                    cx: &mut std::task::Context<'_>,
                ) -> std::task::Poll<std::io::Result<u64>> {
                    let this = self.get_mut();
                    let delegate = &mut this.0;
                    pin!(delegate);
                    AsyncSeek::poll_complete(delegate, cx)
                }
            }

            #[derive(Debug)]
            #[repr(transparent)]
            pub struct [< $format FileType >](#[doc(hidden)] MemFileType);

            #[async_trait::async_trait]
            impl FloppyFileType for [< $format FileType >] {
                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)]
            #[repr(transparent)]
            pub struct [< $format Metadata >](#[doc(hidden)] MemMetadata);

            impl<'a> FloppyMetadata<'a, [< $format FloppyDisk >]> for [< $format Metadata >] {
                fn file_type(&self) -> <[< $format FloppyDisk >] as FloppyDisk<'a>>::FileType {
                    [< $format FileType >](self.0.file_type())
                }

                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()
                }

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

                fn permissions(&self) -> <[< $format FloppyDisk >] as FloppyDisk<'a>>::Permissions {
                    [< $format Permissions >](self.0.permissions())
                }

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

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

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

            impl FloppyUnixMetadata for [< $format Metadata >] {
                fn uid(&self) -> Result<u32> {
                    self.0.uid()
                }

                fn gid(&self) -> Result<u32> {
                    self.0.gid()
                }
            }

            #[derive(Debug)]
            pub struct [< $format OpenOptions>](#[doc(hidden)] MemOpenOptions, #[doc(hidden)] bool);

            #[async_trait::async_trait]
            impl<'a> FloppyOpenOptions<'a, [< $format FloppyDisk >]> for [< $format OpenOptions >] {
                fn new() -> Self {
                    Self(MemOpenOptions::new(), false)
                }

                fn read(self, read: bool) -> Self {
                    Self(self.0.read(read), self.1)
                }

                fn write(self, write: bool) -> Self {
                    Self(self.0.write(write), self.1)
                }

                fn append(self, append: bool) -> Self {
                    Self(self.0.append(append), self.1)
                }

                fn truncate(self, truncate: bool) -> Self {
                    Self(self.0.truncate(truncate), self.1)
                }

                fn create(self, create: bool) -> Self {
                    Self(self.0.create(create), create)
                }

                fn create_new(self, create_new: bool) -> Self {
                    Self(self.0.create_new(create_new), create_new)
                }

                async fn open<P: AsRef<Path> + Send>(
                    &self,
                    disk: &'a [< $format FloppyDisk >],
                    path: P,
                ) -> Result<<[< $format  FloppyDisk >] as FloppyDisk<'a>>::File> {
                    if self.1 {
                        let path = path.as_ref();
                        disk.add_path(path).await;
                    }
                    self.0.open(&disk.delegate, path).await.map([< $format File >])
                }
            }

            #[derive(Debug)]
            #[repr(transparent)]
            pub struct [< $format Permissions >](#[doc(hidden)] MemPermissions);

            impl FloppyPermissions for [< $format Permissions >] {
                fn readonly(&self) -> bool {
                    self.0.readonly()
                }

                fn set_readonly(&mut self, readonly: bool) {
                    self.0.set_readonly(readonly)
                }
            }

            impl FloppyUnixPermissions for [< $format Permissions >] {
                fn mode(&self) -> u32 {
                    FloppyUnixPermissions::mode(&self.0)
                }

                fn set_mode(&mut self, mode: u32) {
                    FloppyUnixPermissions::set_mode(&mut self.0, mode)
                }

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

            #[derive(Debug)]
            #[repr(transparent)]
            pub struct [< $format ReadDir >](#[doc(hidden)] MemReadDir);

            #[async_trait::async_trait]
            impl<'a> FloppyReadDir<'a, [< $format FloppyDisk >]> for [< $format ReadDir >] {
                async fn next_entry(
                    &mut self,
                ) -> Result<Option<<[< $format FloppyDisk >] as FloppyDisk<'a>>::DirEntry>> {
                    self.0.next_entry().await.map(|e| e.map([< $format DirEntry >]))
                }
            }

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

                #[test_log::test(tokio::test)]
                async fn test_read_works() -> Result<()> {
                    let archive = crate::util::tests::TempFile::new(concat!("./fixtures/", $fixture)).await?;
                    let disk = [< $format FloppyDisk >]::open(archive.path_view()).await?;

                    let input = disk.read_to_string("/a.txt").await?;
                    assert_eq!("asdf\n", input);
                    disk.close().await?;

                    Ok(())
                }

                #[test_log::test(tokio::test)]
                async fn test_write_works() -> Result<()> {
                    let archive = crate::util::tests::TempFile::new(concat!("./fixtures/", $fixture)).await?;
                    {
                        let disk = [< $format FloppyDisk >]::open(archive.path_view()).await?;
                        disk.write("/b.txt", "wow!!!").await?;
                        disk.close().await?;
                    }
                    {
                        let disk = [< $format FloppyDisk >]::open(archive.path_view()).await?;

                        let input = disk.read_to_string("/b.txt").await?;
                        assert_eq!("wow!!!", input);
                        disk.close().await?;
                    }

                    Ok(())
                }

                #[test_log::test(tokio::test)]
                async fn test_directories_works() -> Result<()> {
                    let archive = crate::util::tests::TempFile::new(concat!("./fixtures/", $fixture)).await?;
                    {
                        let disk = [< $format FloppyDisk >]::open(archive.path_view()).await?;
                        disk.create_dir_all("/test/thing").await?;
                        disk.write("/test/thing/heck.txt", "omg!!!").await?;
                        disk.close().await?;
                    }
                    {
                        let disk = [< $format FloppyDisk >]::open(archive.path_view()).await?;

                        let input = disk.read_to_string("/test/thing/heck.txt").await?;
                        assert_eq!("omg!!!", input);
                        disk.close().await?;
                    }

                    Ok(())
                }

                #[test_log::test(tokio::test)]
                async fn test_many_files_and_directories_works() -> Result<()> {
                    let archive = crate::util::tests::TempFile::new(concat!("./fixtures/", $fixture)).await?;
                    {
                        let disk = [< $format FloppyDisk >]::open(archive.path_view()).await?;
                        disk.create_dir_all("/test/thing").await?;
                        disk.create_dir_all("/a/b/c/d/e/f/g").await?;
                        disk.create_dir_all("/1/2/3/4/5").await?;

                        disk.write("/test/thing/heck.txt", "omg!!!").await?;
                        disk.write("/a/b/c/d/e/f/g/h.txt", "gasp!!!").await?;
                        disk.write("/1/2/3/4/5/6.txt", "wtf!!!").await?;
                        disk.close().await?;
                    }
                    {
                        let disk = [< $format FloppyDisk >]::open(archive.path_view()).await?;

                        let input = disk.read_to_string("/test/thing/heck.txt").await?;
                        assert_eq!("omg!!!", input);

                        let input = disk.read_to_string("/a/b/c/d/e/f/g/h.txt").await?;
                        assert_eq!("gasp!!!", input);

                        let input = disk.read_to_string("/1/2/3/4/5/6.txt").await?;
                        assert_eq!("wtf!!!", input);
                        disk.close().await?;
                    }

                    Ok(())
                }

                #[test_log::test(tokio::test)]
                async fn test_create_new_works() -> Result<()> {
                    let tmp_dir = crate::util::TempDir::new().await?;
                    let archive = tmp_dir.path_view().join("archive.tmp");
                    {
                        let disk = [< $format FloppyDisk >]::open(&archive).await?;
                        disk.write("/a.txt", "asdf\n").await?;
                        disk.close().await?;
                    }

                    {
                        let disk = [< $format FloppyDisk >]::open(&archive).await?;
                        let input = disk.read_to_string("/a.txt").await?;
                        assert_eq!("asdf\n", input);
                        disk.close().await?;
                    }

                    Ok(())
                }
            }
        }
    };
}

use std::path::{Path, PathBuf};

pub(crate) use archive_format;
use tracing::debug;

pub(crate) async fn exists_async<P: AsRef<Path>>(path: P) -> bool {
    let path = path.as_ref();
    tokio::fs::canonicalize(path).await.is_ok()
}

pub(crate) async fn async_file<P: AsRef<Path>>(path: P) -> std::io::Result<tokio::fs::File> {
    let path = path.as_ref();
    let path = tokio::fs::canonicalize(path).await?;
    debug!("open file async: {}", path.display());
    let file = tokio::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(&path)
        .await?;
    file.sync_all().await?;
    Ok(file)
}

pub(crate) struct TempDir {
    path: PathBuf,
}

#[allow(unused)]
impl TempDir {
    pub async fn new() -> std::io::Result<TempDir> {
        let mut path = std::env::temp_dir();
        path.push(format!("flop-workdir-{}", rand::random::<u64>()));
        debug!("creating tempdir: {}", path.display());
        tokio::fs::create_dir_all(&path).await?;

        Ok(TempDir { path })
    }

    pub fn path_view(&self) -> PathBuf {
        self.path.clone()
    }
}

impl Drop for TempDir {
    fn drop(&mut self) {
        debug!("dropping {}!", self.path.display());
        if self.path.exists() {
            std::fs::remove_dir_all(&self.path).unwrap();
        }
    }
}

impl AsRef<Path> for TempDir {
    fn as_ref(&self) -> &Path {
        &self.path
    }
}

impl AsRef<PathBuf> for TempDir {
    fn as_ref(&self) -> &PathBuf {
        &self.path
    }
}

impl std::ops::Deref for TempDir {
    type Target = PathBuf;

    fn deref(&self) -> &Self::Target {
        &self.path
    }
}

#[cfg(test)]
#[allow(unused)]
pub(crate) mod tests {
    use tracing::debug;

    use super::TempDir;
    use std::path::{Path, PathBuf};

    pub(crate) struct TempFile {
        scope: TempDir,
        path: PathBuf,
    }

    impl TempFile {
        pub async fn new<P: AsRef<Path>>(fixture: P) -> std::io::Result<TempFile> {
            let fixture = fixture.as_ref();
            let scope = TempDir::new().await?;
            let file = fixture.file_name().unwrap();
            let mut path = scope.path_view().to_path_buf();
            path.push(file);
            debug!("copying fixture {} to tempfile {}", fixture.display(), path.display());
            tokio::fs::copy(&fixture, &path).await?;

            Ok(TempFile { scope, path })
        }

        pub fn path_view(&self) -> &Path {
            &self.path
        }

        pub fn scope_view(&self) -> &Path {
            &self.scope
        }
    }

    impl Drop for TempFile {
        fn drop(&mut self) {
            debug!("dropping tempfile {}!", &self.path.display());
        }
    }
}