scionic-merkletree 2.5.6

Scionic Merkle trees: a folder of files under one root, with identical chunks, files and folders stored once, and any part of it shown and checked without the rest
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
//! Folders on disk, held open.
//!
//! A path is looked up afresh every time it is used. Whoever can write to a
//! folder on the way can swap it for a link between two uses, and send the
//! second use somewhere else. So nothing here is reached by its whole path but
//! the one place a caller names. From there on a folder is held open, and what
//! is in it is reached from the folder itself, one name at a time, and never
//! through a link.
//!
//! * On Unix a held folder is an open descriptor. What is in it is opened
//!   from that descriptor, with links refused by the system itself.
//! * On Windows a held folder is a handle that does not share deletion. The
//!   folder can be neither renamed nor replaced while it is held, and a held
//!   folder keeps the folders above it held. What is in it is opened by its
//!   path, as itself, and refused when it turns out to be a link. The path
//!   is in the form that Windows takes letter for letter, so a name is the
//!   name that was listed and no other.
//!
//! On every system a name is one name: an empty one, a dot name, or one with
//! a separator in it is refused before the system sees it.
//! * Elsewhere a held folder is its path, and no more is promised than the
//!   standard library gives.

use std::ffi::{OsStr, OsString};
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// What a name in a folder is, without following it.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum Found {
    File,
    Folder,
    Link,
    Other,
}

/// Why something in a held folder was not opened.
#[derive(Debug)]
pub(crate) enum Refused {
    /// A link is there.
    Link,
    /// Something other than what was asked for is there.
    Other,
    /// The system said no.
    Io(io::Error),
}

impl From<io::Error> for Refused {
    fn from(error: io::Error) -> Self {
        Self::Io(error)
    }
}

/// A folder that is held open.
pub(crate) struct Held {
    path: PathBuf,
    inner: imp::Inner,
}

impl Held {
    /// Holds the folder at `path`, as the caller names it: a link on the way
    /// to it, or at its end, is the caller's.
    pub(crate) fn open(path: &Path) -> io::Result<Arc<Self>> {
        Ok(Arc::new(Self {
            path: path.to_path_buf(),
            inner: imp::Inner::open(path)?,
        }))
    }

    /// Makes a new folder at `path`, and holds it. Anything that is there
    /// already is an error.
    pub(crate) fn create(path: &Path) -> io::Result<Arc<Self>> {
        Ok(Arc::new(Self {
            path: path.to_path_buf(),
            inner: imp::Inner::create(path)?,
        }))
    }

    /// Where the folder was when it was opened. For messages: nothing is
    /// opened by this.
    pub(crate) fn path(&self) -> &Path {
        &self.path
    }

    /// The names in the folder, and what each is. Nothing is followed.
    pub(crate) fn list(&self) -> io::Result<Vec<(OsString, Found)>> {
        self.inner.list(&self.path)
    }

    /// Holds the folder `name` in this one. A link is refused.
    pub(crate) fn open_folder(self: &Arc<Self>, name: &str) -> Result<Arc<Self>, Refused> {
        let path = self.path.join(name);
        let inner = imp::Inner::open_folder(self, one_name(name)?, &path)?;
        Ok(Arc::new(Self { path, inner }))
    }

    /// Opens the file `name` in this folder, to read. A link is refused, and
    /// so is anything that is not a plain file.
    pub(crate) fn open_file(&self, name: &str) -> Result<File, Refused> {
        self.inner.open_file(one_name(name)?, &self.path.join(name))
    }

    /// Makes the new folder `name` in this one, and holds it.
    pub(crate) fn create_folder(self: &Arc<Self>, name: &str) -> io::Result<Arc<Self>> {
        let path = self.path.join(name);
        let inner = imp::Inner::create_folder(self, one_name(name)?, &path)?;
        Ok(Arc::new(Self { path, inner }))
    }

    /// Makes the new file `name` in this folder, to write. Anything that is
    /// there already is an error, a link too.
    pub(crate) fn create_file(&self, name: &str) -> io::Result<File> {
        self.inner
            .create_file(one_name(name)?, &self.path.join(name))
    }
}

/// A name in a folder is one name. One that could lead out of the folder, or
/// to a part of a file, is refused before the system sees it: an empty name,
/// a dot name, and a name with a separator or a zero byte in it. On Windows
/// a colon too, which names a second stream of a file's bytes.
fn one_name(name: &str) -> io::Result<&OsStr> {
    let more_than_a_name = name.is_empty()
        || name == "."
        || name == ".."
        || name.contains(['/', '\0'])
        || (cfg!(windows) && name.contains(['\\', ':']));
    if more_than_a_name {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "what is opened or made in a folder is named by one name",
        ));
    }
    Ok(OsStr::new(name))
}

/// The names in the folder at `path`, by the standard library.
#[cfg(not(unix))]
fn list_by_path(path: &Path) -> io::Result<Vec<(OsString, Found)>> {
    let mut listed = Vec::new();
    for entry in std::fs::read_dir(path)? {
        let entry = entry?;
        let kind = entry.file_type()?;
        let found = if kind.is_symlink() {
            Found::Link
        } else if kind.is_dir() {
            Found::Folder
        } else if kind.is_file() {
            Found::File
        } else {
            Found::Other
        };
        listed.push((entry.file_name(), found));
    }
    Ok(listed)
}

/// Why opening `path` failed, for one that had to be `wanted`.
#[cfg(not(unix))]
fn refused_by_path(path: &Path, error: io::Error, wanted: Found) -> Refused {
    match std::fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_symlink() => Refused::Link,
        Ok(metadata) if metadata.is_dir() != (wanted == Found::Folder) => Refused::Other,
        _ => Refused::Io(error),
    }
}

#[cfg(unix)]
mod imp {
    use super::{Found, Held, Refused};
    use rustix::fs::{fstat, mkdirat, openat, statat, AtFlags, Dir, FileType, Mode, OFlags, CWD};
    use rustix::io::Errno;
    use std::ffi::{OsStr, OsString};
    use std::fs::File;
    use std::io;
    use std::os::fd::OwnedFd;
    use std::os::unix::ffi::OsStringExt;
    use std::path::Path;
    use std::sync::Arc;

    pub(super) struct Inner {
        fd: OwnedFd,
    }

    fn found(kind: FileType) -> Found {
        match kind {
            FileType::RegularFile => Found::File,
            FileType::Directory => Found::Folder,
            FileType::Symlink => Found::Link,
            _ => Found::Other,
        }
    }

    /// What `name` is now, without following it.
    fn look(fd: &OwnedFd, name: &OsStr) -> io::Result<Found> {
        let stat = statat(fd, name, AtFlags::SYMLINK_NOFOLLOW)?;
        Ok(found(FileType::from_raw_mode(stat.st_mode)))
    }

    /// Why an open that refuses links failed. Systems differ in the error
    /// they give for a link, so what is there is looked at.
    fn refused(fd: &OwnedFd, name: &OsStr, error: Errno, wanted: Found) -> Refused {
        match look(fd, name) {
            Ok(Found::Link) => Refused::Link,
            Ok(found) if found != wanted => Refused::Other,
            _ => Refused::Io(error.into()),
        }
    }

    fn folder_flags() -> OFlags {
        OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC
    }

    fn all_may() -> Mode {
        Mode::RWXU | Mode::RWXG | Mode::RWXO
    }

    impl Inner {
        pub(super) fn open(path: &Path) -> io::Result<Self> {
            let flags = OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC;
            Ok(Self {
                fd: openat(CWD, path, flags, Mode::empty())?,
            })
        }

        pub(super) fn create(path: &Path) -> io::Result<Self> {
            mkdirat(CWD, path, all_may())?;
            Ok(Self {
                fd: openat(CWD, path, folder_flags(), Mode::empty())?,
            })
        }

        pub(super) fn list(&self, _path: &Path) -> io::Result<Vec<(OsString, Found)>> {
            let mut listed = Vec::new();
            for entry in Dir::read_from(&self.fd)? {
                let entry = entry?;
                let name = entry.file_name().to_bytes();
                if name == b"." || name == b".." {
                    continue;
                }
                let name = OsString::from_vec(name.to_vec());
                let kind = match entry.file_type() {
                    FileType::Unknown => look(&self.fd, &name)?,
                    kind => found(kind),
                };
                listed.push((name, kind));
            }
            Ok(listed)
        }

        pub(super) fn open_folder(
            above: &Arc<Held>,
            name: &OsStr,
            _path: &Path,
        ) -> Result<Self, Refused> {
            let fd = &above.inner.fd;
            match openat(fd, name, folder_flags(), Mode::empty()) {
                Ok(fd) => Ok(Self { fd }),
                Err(error) => Err(refused(fd, name, error, Found::Folder)),
            }
        }

        pub(super) fn open_file(&self, name: &OsStr, _path: &Path) -> Result<File, Refused> {
            // Not waiting: a pipe that took the file's place would hold a
            // waiting open for ever. A plain file is read the same either way.
            let flags = OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC;
            let fd = match openat(&self.fd, name, flags, Mode::empty()) {
                Ok(fd) => fd,
                Err(error) => return Err(refused(&self.fd, name, error, Found::File)),
            };
            // What was opened is what is read, so it is what has to be a file.
            let stat = fstat(&fd).map_err(io::Error::from)?;
            if FileType::from_raw_mode(stat.st_mode) != FileType::RegularFile {
                return Err(Refused::Other);
            }
            Ok(File::from(fd))
        }

        pub(super) fn create_folder(
            above: &Arc<Held>,
            name: &OsStr,
            _path: &Path,
        ) -> io::Result<Self> {
            let fd = &above.inner.fd;
            mkdirat(fd, name, all_may())?;
            Ok(Self {
                fd: openat(fd, name, folder_flags(), Mode::empty())?,
            })
        }

        pub(super) fn create_file(&self, name: &OsStr, _path: &Path) -> io::Result<File> {
            let flags =
                OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::NOFOLLOW | OFlags::CLOEXEC;
            let mode = Mode::RUSR | Mode::WUSR | Mode::RGRP | Mode::WGRP | Mode::ROTH | Mode::WOTH;
            Ok(File::from(openat(&self.fd, name, flags, mode)?))
        }
    }
}

#[cfg(windows)]
mod imp {
    use super::{list_by_path, refused_by_path, Found, Held, Refused};
    use std::ffi::{OsStr, OsString};
    use std::fs::{self, File, OpenOptions};
    use std::io;
    use std::os::windows::fs::{MetadataExt, OpenOptionsExt};
    use std::path::{Component, Path, PathBuf, Prefix};
    use std::sync::Arc;

    const FILE_SHARE_READ: u32 = 0x1;
    const FILE_SHARE_WRITE: u32 = 0x2;
    /// Opens a link as itself, and not what it leads to.
    const FILE_FLAG_OPEN_REPARSE_POINT: u32 = 0x0020_0000;
    /// Lets a folder be opened as a file is.
    const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x0200_0000;
    /// Some part of the system stands in for what is stored here: a link, or
    /// a file that is fetched when it is read.
    const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x400;

    pub(super) struct Inner {
        /// The folder's path in the form that Windows takes letter for
        /// letter. Everything in the folder is reached through it.
        exact: PathBuf,
        /// Open for as long as the folder is held. It shares reading and
        /// writing and not deleting, so the folder can be neither renamed nor
        /// replaced meanwhile.
        _pin: File,
        /// The folder this one is in, held as long as this one is.
        _above: Option<Arc<Held>>,
    }

    /// `path` in the form that Windows takes letter for letter.
    ///
    /// Windows changes an ordinary path when it looks it up: a dot or a space
    /// at the end of a name is dropped, and some names are devices wherever
    /// they stand. So a file `a.` would be read as the file `a` beside it. A
    /// path that begins with `\\?\` is looked up as it is written.
    ///
    /// What the caller wrote is first read the way Windows reads it, which is
    /// what the caller means by it. The names that are added to it afterwards
    /// are the ones that a folder listed, and are taken as they are.
    fn exact(path: &Path) -> io::Result<PathBuf> {
        let whole = std::path::absolute(path)?;
        let mut parts = whole.components();
        let mut exact = OsString::from(r"\\?\");
        match parts.next() {
            Some(Component::Prefix(prefix)) => match prefix.kind() {
                // `C:\folder` is `\\?\C:\folder`.
                Prefix::Disk(_) => exact.push(prefix.as_os_str()),
                // `\\server\share\folder` is `\\?\UNC\server\share\folder`.
                Prefix::UNC(server, share) => {
                    exact.push("UNC\\");
                    exact.push(server);
                    exact.push("\\");
                    exact.push(share);
                }
                // `\\.\C:\folder` is `\\?\C:\folder`.
                Prefix::DeviceNS(device) => exact.push(device),
                // It is in that form already.
                Prefix::Verbatim(_) | Prefix::VerbatimUNC(..) | Prefix::VerbatimDisk(_) => {
                    return Ok(whole);
                }
            },
            _ => return Ok(whole),
        }
        exact.push(parts.as_path());
        Ok(PathBuf::from(exact))
    }

    /// Opens the folder at `path` so that it stays what and where it is.
    /// Unless `follow` is set, a link is opened as itself, and refused.
    fn pin(path: &Path, follow: bool) -> Result<File, Refused> {
        let mut flags = FILE_FLAG_BACKUP_SEMANTICS;
        if !follow {
            flags |= FILE_FLAG_OPEN_REPARSE_POINT;
        }
        let pin = OpenOptions::new()
            .read(true)
            .share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE)
            .custom_flags(flags)
            .open(path)
            .map_err(|error| refused_by_path(path, error, Found::Folder))?;
        let metadata = pin.metadata()?;
        if metadata.file_type().is_symlink() {
            return Err(Refused::Link);
        }
        if !metadata.is_dir() {
            return Err(Refused::Other);
        }
        Ok(pin)
    }

    fn as_io(refused: Refused) -> io::Error {
        match refused {
            Refused::Io(error) => error,
            Refused::Link => io::Error::new(
                io::ErrorKind::InvalidInput,
                "a link is there, where a folder has to be",
            ),
            Refused::Other => io::Error::new(
                io::ErrorKind::InvalidInput,
                "something that is no folder is there, where a folder has to be",
            ),
        }
    }

    /// Which file an open file is: the volume, and the number of the file on
    /// it.
    #[derive(Debug, PartialEq, Eq)]
    pub(super) struct Identity {
        volume: u64,
        number: [u8; 16],
    }

    /// Which file `file` is. The long form has a 128-bit number, and every
    /// volume of the newer kinds has it. The short form has a 64-bit number,
    /// and every volume has it, but ReFS does not promise that its short
    /// number is one file's alone.
    ///
    /// Neither can be asked of the standard library, so this is the one place
    /// in the crate with `unsafe` code.
    #[allow(unsafe_code)]
    pub(super) fn identity(file: &File, long: bool) -> io::Result<Identity> {
        use std::os::windows::io::AsRawHandle;
        use windows_sys::Win32::Storage::FileSystem::{
            FileIdInfo, GetFileInformationByHandle, GetFileInformationByHandleEx,
            BY_HANDLE_FILE_INFORMATION, FILE_ID_INFO,
        };
        let handle = file.as_raw_handle();
        if long {
            let mut info = FILE_ID_INFO::default();
            // SAFETY: `handle` is open for as long as `file` is borrowed, and
            // the system writes no more than the size it is given into `info`,
            // which is a `FILE_ID_INFO` of that size.
            let done = unsafe {
                GetFileInformationByHandleEx(
                    handle,
                    FileIdInfo,
                    (&raw mut info).cast(),
                    size_of::<FILE_ID_INFO>() as u32,
                )
            };
            if done == 0 {
                return Err(io::Error::last_os_error());
            }
            return Ok(Identity {
                volume: info.VolumeSerialNumber,
                number: info.FileId.Identifier,
            });
        }
        let mut info = BY_HANDLE_FILE_INFORMATION::default();
        // SAFETY: `handle` is open for as long as `file` is borrowed, and
        // `info` is the `BY_HANDLE_FILE_INFORMATION` that the call fills.
        let done = unsafe { GetFileInformationByHandle(handle, &mut info) };
        if done == 0 {
            return Err(io::Error::last_os_error());
        }
        let mut number = [0; 16];
        number[..4].copy_from_slice(&info.nFileIndexLow.to_le_bytes());
        number[4..8].copy_from_slice(&info.nFileIndexHigh.to_le_bytes());
        Ok(Identity {
            volume: u64::from(info.dwVolumeSerialNumber),
            number,
        })
    }

    /// Whether two open files are one file. Both are asked in the same form:
    /// the long one where both volumes have it, and the short one otherwise.
    /// Where a file's identity is not to be had, nothing says that the two
    /// are one, and that is an error.
    pub(super) fn one_file(left: &File, right: &File) -> io::Result<bool> {
        if let (Ok(left), Ok(right)) = (identity(left, true), identity(right, true)) {
            return Ok(left == right);
        }
        Ok(identity(left, false)? == identity(right, false)?)
    }

    /// Opens `path` as itself, and says whether it is a plain file with
    /// nothing standing in for it. A link is refused, and so is a folder.
    fn open_as_itself(path: &Path, held: bool) -> Result<(File, bool), Refused> {
        let mut options = OpenOptions::new();
        options
            .read(true)
            .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT);
        if held {
            options.share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE);
        }
        let file = options
            .open(path)
            .map_err(|error| refused_by_path(path, error, Found::File))?;
        let metadata = file.metadata()?;
        if metadata.file_type().is_symlink() {
            return Err(Refused::Link);
        }
        if metadata.is_dir() {
            return Err(Refused::Other);
        }
        let plain = metadata.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT == 0;
        Ok((file, plain))
    }

    impl Inner {
        /// The exact path of `name` in this folder. The name is added as it
        /// is: nothing reads it as a path.
        fn within(&self, name: &OsStr) -> PathBuf {
            let mut path = self.exact.clone().into_os_string();
            if !path.as_encoded_bytes().ends_with(b"\\") {
                path.push("\\");
            }
            path.push(name);
            PathBuf::from(path)
        }

        pub(super) fn open(path: &Path) -> io::Result<Self> {
            let exact = exact(path)?;
            Ok(Self {
                _pin: pin(&exact, true).map_err(as_io)?,
                exact,
                _above: None,
            })
        }

        pub(super) fn create(path: &Path) -> io::Result<Self> {
            let exact = exact(path)?;
            fs::create_dir(&exact)?;
            Ok(Self {
                _pin: pin(&exact, false).map_err(as_io)?,
                exact,
                _above: None,
            })
        }

        pub(super) fn list(&self, _path: &Path) -> io::Result<Vec<(OsString, Found)>> {
            list_by_path(&self.exact)
        }

        pub(super) fn open_folder(
            above: &Arc<Held>,
            name: &OsStr,
            _path: &Path,
        ) -> Result<Self, Refused> {
            let exact = above.inner.within(name);
            Ok(Self {
                _pin: pin(&exact, false)?,
                exact,
                _above: Some(Arc::clone(above)),
            })
        }

        pub(super) fn open_file(&self, name: &OsStr, _path: &Path) -> Result<File, Refused> {
            let exact = self.within(name);
            let (file, plain) = open_as_itself(&exact, false)?;
            if plain {
                // For a plain file, opening it as itself is opening it.
                return Ok(file);
            }
            // Something stands in for this file, as for one that is fetched
            // when it is read, and opened as itself it may not read as the
            // file does. So it is held where it is, looked at again, and
            // opened in the usual way while it cannot be renamed or replaced.
            drop(file);
            let (held, _) = open_as_itself(&exact, true)?;
            let file = File::open(&exact)?;
            // Holding the file keeps its name on it. It does not keep what
            // stands in for it from being changed into a link, which the
            // usual way of opening then follows. So what was opened has to be
            // the file that is held, and not another one.
            if !one_file(&held, &file)? {
                return Err(Refused::Link);
            }
            drop(held);
            Ok(file)
        }

        pub(super) fn create_folder(
            above: &Arc<Held>,
            name: &OsStr,
            _path: &Path,
        ) -> io::Result<Self> {
            let exact = above.inner.within(name);
            fs::create_dir(&exact)?;
            Ok(Self {
                _pin: pin(&exact, false).map_err(as_io)?,
                exact,
                _above: Some(Arc::clone(above)),
            })
        }

        pub(super) fn create_file(&self, name: &OsStr, _path: &Path) -> io::Result<File> {
            // A link that is there under the name is not gone through: the
            // name is taken, and that is an error.
            OpenOptions::new()
                .write(true)
                .create_new(true)
                .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT)
                .open(self.within(name))
        }
    }
}

#[cfg(not(any(unix, windows)))]
mod imp {
    use super::{list_by_path, refused_by_path, Found, Held, Refused};
    use std::ffi::{OsStr, OsString};
    use std::fs::{self, File, OpenOptions};
    use std::io;
    use std::path::Path;
    use std::sync::Arc;

    pub(super) struct Inner;

    /// What is at `path` has to be what was asked for, and no link.
    fn check(path: &Path, wanted: Found) -> Result<(), Refused> {
        let metadata = fs::symlink_metadata(path)?;
        if metadata.file_type().is_symlink() {
            return Err(Refused::Link);
        }
        let is = if metadata.is_dir() {
            Found::Folder
        } else if metadata.is_file() {
            Found::File
        } else {
            Found::Other
        };
        if is == wanted {
            Ok(())
        } else {
            Err(Refused::Other)
        }
    }

    impl Inner {
        pub(super) fn open(path: &Path) -> io::Result<Self> {
            fs::read_dir(path).map(|_| Self)
        }

        pub(super) fn create(path: &Path) -> io::Result<Self> {
            fs::create_dir(path).map(|()| Self)
        }

        pub(super) fn list(&self, path: &Path) -> io::Result<Vec<(OsString, Found)>> {
            list_by_path(path)
        }

        pub(super) fn open_folder(
            _above: &Arc<Held>,
            _name: &OsStr,
            path: &Path,
        ) -> Result<Self, Refused> {
            check(path, Found::Folder).map(|()| Self)
        }

        pub(super) fn open_file(&self, _name: &OsStr, path: &Path) -> Result<File, Refused> {
            check(path, Found::File)?;
            File::open(path).map_err(|error| refused_by_path(path, error, Found::File))
        }

        pub(super) fn create_folder(
            _above: &Arc<Held>,
            _name: &OsStr,
            path: &Path,
        ) -> io::Result<Self> {
            fs::create_dir(path).map(|()| Self)
        }

        pub(super) fn create_file(&self, _name: &OsStr, path: &Path) -> io::Result<File> {
            OpenOptions::new().write(true).create_new(true).open(path)
        }
    }
}

#[cfg(test)]
mod tests;