logo
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
use crate::fs::{
    open_dir_for_reading, open_dir_for_reading_unchecked, open_entry_impl, read_dir_unchecked,
    remove_dir_unchecked, remove_file_unchecked, stat_unchecked, DirEntryInner, FollowSymlinks,
    Metadata, OpenOptions, ReadDir,
};
use io_extras::os::rustix::{AsRawFd, FromRawFd, RawFd};
use io_lifetimes::{AsFd, IntoFd};
use rustix::fs::Dir;
use rustix::io::OwnedFd;
use std::ffi::OsStr;
use std::mem::ManuallyDrop;
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[cfg(target_os = "wasi")]
use std::os::wasi::ffi::OsStrExt;
use std::path::{Component, Path};
use std::sync::{Arc, Mutex};
use std::{fmt, fs, io};

pub(crate) struct ReadDirInner {
    raw_fd: RawFd,

    // `Dir` doesn't implement `AsFd`, because libc `fdopendir` has UB if the
    // file descriptor is used in almost any way, so we hold a separate
    // `OwnedFd` that we can do `as_fd()` on.
    rustix: Arc<Mutex<(Dir, OwnedFd)>>,
}

impl ReadDirInner {
    pub(crate) fn new(start: &fs::File, path: &Path, follow: FollowSymlinks) -> io::Result<Self> {
        let fd = open_dir_for_reading(start, path, follow)?;
        let dir = Dir::read_from(fd.as_fd())?;
        Ok(Self {
            raw_fd: fd.as_fd().as_raw_fd(),
            rustix: Arc::new(Mutex::new((dir, fd.into_fd().into()))),
        })
    }

    pub(crate) fn read_base_dir(start: &fs::File) -> io::Result<Self> {
        // Open ".", to obtain a new independent file descriptor. Don't use
        // `dup` since in that case the resulting file descriptor would share
        // a current position with the original, and `read_dir` calls after
        // the first `read_dir` call wouldn't start from the beginning.
        let fd =
            open_dir_for_reading_unchecked(start, Component::CurDir.as_ref(), FollowSymlinks::No)?;
        let dir = Dir::read_from(fd.as_fd())?;
        Ok(Self {
            raw_fd: fd.as_fd().as_raw_fd(),
            rustix: Arc::new(Mutex::new((dir, fd.into_fd().into()))),
        })
    }

    pub(crate) fn new_unchecked(
        start: &fs::File,
        path: &Path,
        follow: FollowSymlinks,
    ) -> io::Result<Self> {
        let fd = open_dir_for_reading_unchecked(start, path, follow)?;
        let dir = Dir::read_from(fd.as_fd())?;
        Ok(Self {
            raw_fd: fd.as_fd().as_raw_fd(),
            rustix: Arc::new(Mutex::new((dir, fd.into_fd().into()))),
        })
    }

    pub(super) fn open(&self, file_name: &OsStr, options: &OpenOptions) -> io::Result<fs::File> {
        open_entry_impl(&self.as_file_view(), file_name, options)
    }

    pub(super) fn metadata(&self, file_name: &OsStr) -> io::Result<Metadata> {
        stat_unchecked(&self.as_file_view(), file_name.as_ref(), FollowSymlinks::No)
    }

    pub(super) fn remove_file(&self, file_name: &OsStr) -> io::Result<()> {
        remove_file_unchecked(&self.as_file_view(), file_name.as_ref())
    }

    pub(super) fn remove_dir(&self, file_name: &OsStr) -> io::Result<()> {
        remove_dir_unchecked(&self.as_file_view(), file_name.as_ref())
    }

    pub(super) fn self_metadata(&self) -> io::Result<Metadata> {
        Metadata::from_file(&self.as_file_view())
    }

    pub(super) fn read_dir(
        &self,
        file_name: &OsStr,
        follow: FollowSymlinks,
    ) -> io::Result<ReadDir> {
        read_dir_unchecked(&self.as_file_view(), file_name.as_ref(), follow)
    }

    #[allow(unsafe_code)]
    fn as_file_view(&self) -> ManuallyDrop<fs::File> {
        // Safety: `self.rustix` owns the file descriptor. We just hold a
        // copy outside so that we can read it without taking a lock.
        ManuallyDrop::new(unsafe { fs::File::from_raw_fd(self.raw_fd) })
    }
}

impl Iterator for ReadDirInner {
    type Item = io::Result<DirEntryInner>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let entry = self.rustix.lock().unwrap().0.read()?;
            let entry = match entry {
                Ok(entry) => entry,
                Err(e) => return Some(Err(e.into())),
            };
            let file_name = entry.file_name().to_bytes();
            if file_name != Component::CurDir.as_os_str().as_bytes()
                && file_name != Component::ParentDir.as_os_str().as_bytes()
            {
                let clone = Arc::clone(&self.rustix);
                return Some(Ok(DirEntryInner {
                    rustix: entry,
                    read_dir: Self {
                        raw_fd: self.raw_fd,
                        rustix: clone,
                    },
                }));
            }
        }
    }
}

impl fmt::Debug for ReadDirInner {
    // Like libstd's version, but doesn't print the path.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut b = f.debug_struct("ReadDir");
        b.field("raw_fd", &self.raw_fd);
        b.finish()
    }
}