-
Notifications
You must be signed in to change notification settings - Fork 133
/
readdir.rs
155 lines (133 loc) · 4.41 KB
/
readdir.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
#![cfg(not(target_os = "redox"))]
use std::collections::HashMap;
use std::fs::File;
#[cfg(any(target_os = "android", target_os = "linux"))]
use std::mem::MaybeUninit;
use rustix::fs::{Dir, DirEntry};
#[test]
fn dir_entries() {
let tmpdir = tempfile::tempdir().expect("construct tempdir");
let dirfd = File::open(tmpdir.path()).expect("open tempdir as file");
let mut dir = Dir::read_from(dirfd).expect("construct Dir from dirfd");
let entries = read_entries(&mut dir);
assert_eq!(entries.len(), 0, "no files in directory");
let _f1 = File::create(tmpdir.path().join("file1")).expect("create file1");
let entries = read_entries(&mut dir);
assert!(
entries.get("file1").is_some(),
"directory contains `file1`: {:?}",
entries
);
assert_eq!(entries.len(), 1);
let _f2 = File::create(tmpdir.path().join("file2")).expect("create file1");
let entries = read_entries(&mut dir);
assert!(
entries.get("file1").is_some(),
"directory contains `file1`: {:?}",
entries
);
assert!(
entries.get("file2").is_some(),
"directory contains `file2`: {:?}",
entries
);
assert_eq!(entries.len(), 2);
}
fn read_entries(dir: &mut Dir) -> HashMap<String, DirEntry> {
dir.rewind();
let mut out = HashMap::new();
while let Some(err) = dir.read() {
let err = err.expect("non-error entry");
let name = err.file_name().to_str().expect("utf8 filename").to_owned();
if name != "." && name != ".." {
out.insert(name, err);
}
}
out
}
#[cfg(any(target_os = "android", target_os = "linux"))]
fn test_raw_dir(buf: &mut [MaybeUninit<u8>]) {
use std::collections::HashSet;
use std::io::{Seek, SeekFrom};
use rustix::fd::AsFd;
use rustix::fs::RawDir;
fn read_raw_entries<Fd: AsFd>(dir: &mut RawDir<Fd>) -> HashSet<String> {
let mut out = HashSet::new();
while let Some(entry) = dir.next() {
let entry = entry.expect("non-error entry");
let name = entry
.file_name()
.to_str()
.expect("utf8 filename")
.to_owned();
if name != "." && name != ".." {
out.insert(name);
}
}
out
}
let tmpdir = tempfile::tempdir().expect("construct tempdir");
let mut dirfd = File::open(tmpdir.path()).expect("open tempdir as file");
let mut dir = RawDir::new(dirfd.try_clone().unwrap(), buf);
let entries = read_raw_entries(&mut dir);
assert_eq!(entries.len(), 0, "no files in directory");
let _f1 = File::create(tmpdir.path().join("file1")).expect("create file1");
dirfd.seek(SeekFrom::Start(0)).unwrap();
let entries = read_raw_entries(&mut dir);
assert!(
entries.get("file1").is_some(),
"directory contains `file1`: {:?}",
entries
);
assert_eq!(entries.len(), 1);
let _f2 = File::create(tmpdir.path().join("file2")).expect("create file1");
dirfd.seek(SeekFrom::Start(0)).unwrap();
let entries = read_raw_entries(&mut dir);
assert!(
entries.get("file1").is_some(),
"directory contains `file1`: {:?}",
entries
);
assert!(
entries.get("file2").is_some(),
"directory contains `file2`: {:?}",
entries
);
assert_eq!(entries.len(), 2);
}
#[test]
#[cfg(any(target_os = "android", target_os = "linux"))]
fn raw_dir_entries_heap() {
// When we can depend on Rust 1.60, we can use the spare_capacity_mut version instead.
/*
let mut buf = Vec::with_capacity(8192);
test_raw_dir(buf.spare_capacity_mut());
*/
let mut buf = [MaybeUninit::new(0); 8192];
test_raw_dir(&mut buf);
}
#[test]
#[cfg(any(target_os = "android", target_os = "linux"))]
fn raw_dir_entries_stack() {
let mut buf = [MaybeUninit::uninit(); 2048];
test_raw_dir(&mut buf);
}
#[test]
#[cfg(any(target_os = "android", target_os = "linux"))]
fn raw_dir_entries_unaligned() {
let mut buf = [MaybeUninit::uninit(); 2048];
let buf = &mut buf[1..];
assert!(!(buf.as_ptr() as usize).is_power_of_two());
test_raw_dir(buf);
}
#[test]
fn dir_from_openat() {
let dirfd = rustix::fs::openat(
rustix::fs::cwd(),
".",
rustix::fs::OFlags::RDONLY,
rustix::fs::Mode::empty(),
)
.expect("open cwd as file");
let _dir = Dir::read_from(dirfd).expect("construct Dir from dirfd");
}