-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathfs.rs
162 lines (140 loc) · 3.72 KB
/
fs.rs
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
use std::{
cmp::Ordering,
fs::{self, FileType},
io::{self, Error},
path::{Path, PathBuf},
};
use crate::{vfs::VfsFile, Filter, Vfs};
#[derive(Default)]
pub struct Fs {}
impl Vfs for Fs {
fn create_dir(&self, path: &Path) -> io::Result<()> {
std::fs::create_dir(path)
}
fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {
std::fs::rename(from, to)
}
fn read_folder(
&self,
path: &Path,
show_system_files: bool,
show_files_filter: &Filter<PathBuf>,
#[cfg(unix)] show_hidden: bool,
#[cfg(windows)] show_drives: bool,
) -> Result<Vec<Box<dyn VfsFile>>, Error> {
std::fs::read_dir(path).map(|entries| {
let mut file_infos: Vec<Box<dyn VfsFile>> = entries
.filter_map(|result| result.ok())
.filter_map(|entry| {
let info: Box<FileInfo> = Box::new(FileInfo::new(entry.path()));
if !info.is_dir() {
if !show_system_files && !info.path.is_file() {
// Do not show system files.
return None;
}
// Filter.
if !(show_files_filter)(&info.path) {
return None;
}
}
#[cfg(unix)]
if !show_hidden && info.get_file_name().starts_with('.') {
return None;
}
let info: Box<dyn VfsFile> = info;
Some(info)
})
.collect();
// Sort with folders before files.
file_infos.sort_by(|a, b| match b.is_dir().cmp(&a.is_dir()) {
Ordering::Less => Ordering::Less,
Ordering::Equal => a.path().file_name().cmp(&b.path().file_name()),
Ordering::Greater => Ordering::Greater,
});
#[cfg(windows)]
let file_infos = match show_drives {
true => {
let drives = get_drives();
let mut infos = Vec::with_capacity(drives.len() + file_infos.len());
for drive in drives {
infos.push(Box::new(FileInfo::new(drive)) as Box<dyn VfsFile>);
}
infos.append(&mut file_infos);
infos
}
false => file_infos,
};
file_infos
})
}
}
#[derive(Clone, Debug, Default)]
pub struct FileInfo {
pub(crate) path: PathBuf,
file_type: Option<FileType>,
pub(crate) selected: bool,
}
impl FileInfo {
pub fn new(path: PathBuf) -> Self {
let file_type = fs::metadata(&path).ok().map(|meta| meta.file_type());
Self {
path,
file_type,
selected: false,
}
}
}
impl VfsFile for FileInfo {
fn is_file(&self) -> bool {
self.file_type.is_some_and(|file_type| file_type.is_file())
}
fn is_dir(&self) -> bool {
self.file_type.is_some_and(|file_type| file_type.is_dir())
}
fn path(&self) -> &Path {
&self.path
}
fn selected(&self) -> bool {
self.selected
}
fn set_selected(&mut self, selected: bool) {
self.selected = selected;
}
fn get_file_name(&self) -> &str {
#[cfg(windows)]
if self.is_dir() && is_drive_root(&self.path) {
return self.path.to_str().unwrap_or_default();
}
self
.path()
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default()
}
}
#[cfg(windows)]
pub fn get_drives() -> Vec<PathBuf> {
let mut drive_names = Vec::new();
let mut drives = unsafe { GetLogicalDrives() };
let mut letter = b'A';
while drives > 0 {
if drives & 1 != 0 {
drive_names.push(format!("{}:\\", letter as char).into());
}
drives >>= 1;
letter += 1;
}
drive_names
}
#[cfg(windows)]
pub fn is_drive_root(path: &Path) -> bool {
path
.to_str()
.filter(|path| &path[1..] == ":\\")
.and_then(|path| path.chars().next())
.map_or(false, |ch| ch.is_ascii_uppercase())
}
#[cfg(windows)]
extern "C" {
pub fn GetLogicalDrives() -> u32;
}