Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cursor.rs « src - github.com/windirstat/walkdir.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b73643855bb757330ac68e04769526a17270bb8c (plain)
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
use std::cmp;
use std::fmt;
use std::io;
use std::path::{Path, PathBuf};

#[cfg(unix)]
use crate::os::unix as os;
#[cfg(windows)]
use crate::os::windows as os;

#[derive(Debug)]
pub struct Cursor {
    options: Options,
    stack: Vec<DirCursor>,

    root: bool,
    current: PathBuf,
    file_type: Option<FileType>,
}

impl Cursor {
    pub fn new<P: Into<PathBuf>>(root: P) -> Cursor {
        Cursor {
            options: Options::default(),
            stack: vec![],
            root: true,
            current: root.into(),
            file_type: None,
        }
    }

    pub fn reset<P: Into<PathBuf>>(root: P) {
        unimplemented!()
    }

    pub fn read(&mut self) -> io::Result<Option<CursorEntry>> {
        if let Some(ft) = self.file_type.take() {
            if !ft.is_dir() {
                self.current.pop();
            }
        } else {
            let ft = os::stat(self.current.clone())?.file_type().into_api();
            if ft.is_dir() {
                self.push();
            }
            self.file_type = Some(ft);
            return Ok(Some(CursorEntry { cursor: self }));
        }
        while !self.stack.is_empty() {
            let dcur = self.stack.last_mut().unwrap();
            match dcur.read() {
                None => {
                    self.stack.pop().unwrap();
                    // If the stack is empty, then we've reached the root.
                    // At this point, `current` is just the original root path,
                    // so we should not pop anything from it.
                    if !self.stack.is_empty() {
                        self.current.pop();
                    }
                }
                Some(Err(err)) => return Err(err),
                Some(Ok(dent)) => {
                    let name = dent.file_name_os();
                    if name == "." || name == ".." {
                        continue;
                    }
                    self.current.push(name);
                    self.file_type =
                        Some(dent.file_type().unwrap().into_api());
                    if dent.file_type().unwrap().is_dir() {
                        self.push();
                    }
                    return Ok(Some(CursorEntry { cursor: self }));
                }
            }
        }
        Ok(None)
    }

    fn push(&mut self) {
        let res = os::Dir::open(self.current.clone());
        self.stack.push(DirCursor(res.map_err(Some)));
    }
}

#[derive(Debug)]
struct DirCursor(Result<os::Dir, Option<io::Error>>);

impl DirCursor {
    fn read(&mut self) -> Option<io::Result<os::DirEntry>> {
        match self.0 {
            Err(ref mut err) => err.take().map(Err),
            Ok(ref mut dir) => dir.read(),
        }
    }
}

#[derive(Debug)]
pub struct CursorEntry<'a> {
    cursor: &'a mut Cursor,
}

impl<'a> CursorEntry<'a> {
    pub fn path(&self) -> &Path {
        &self.cursor.current
    }

    pub fn file_type(&self) -> FileType {
        self.cursor.file_type.unwrap()
    }
}

#[derive(Debug)]
struct Options {
    follow_links: bool,
    max_open: usize,
    min_depth: usize,
    max_depth: usize,
    sorter: Option<Sorter>,
    contents_first: bool,
    same_file_system: bool,
}

impl Default for Options {
    fn default() -> Options {
        Options {
            follow_links: false,
            max_open: 10,
            min_depth: 0,
            max_depth: std::usize::MAX,
            sorter: None,
            contents_first: false,
            same_file_system: false,
        }
    }
}

struct Sorter(
    Box<FnMut(&DirEntry, &DirEntry) -> cmp::Ordering + Send + Sync + 'static>,
);

impl fmt::Debug for Sorter {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "<sort function>")
    }
}

#[derive(Debug)]
pub struct DirEntry {
    os: os::DirEntry,
    file_type: FileType,
}

impl DirEntry {}

#[derive(Clone, Copy, Debug)]
pub struct FileType(os::FileType);

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

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

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

impl From<os::FileType> for FileType {
    fn from(osft: os::FileType) -> FileType {
        FileType(osft)
    }
}