-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathlib.rs
More file actions
431 lines (374 loc) · 12.9 KB
/
Copy pathlib.rs
File metadata and controls
431 lines (374 loc) · 12.9 KB
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
//! This crate provides common functionality for Roc to interface with `std::io`
use roc_io_error::{IOErr, IOErrTag};
use roc_std::{RocBox, RocList, RocResult, RocStr};
use roc_std_heap::ThreadSafeRefcountedResourceHeap;
use std::borrow::Cow;
use std::ffi::OsStr;
use std::fs::File;
use std::io::{BufRead, BufReader, ErrorKind, Read, Write};
use std::path::Path;
use std::sync::OnceLock;
use std::{env, io};
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt; // used for is_executable, is_readable, is_writable
pub fn heap() -> &'static ThreadSafeRefcountedResourceHeap<BufReader<File>> {
static FILE_HEAP: OnceLock<ThreadSafeRefcountedResourceHeap<BufReader<File>>> = OnceLock::new();
FILE_HEAP.get_or_init(|| {
let default_max_files = 65536;
let max_files = env::var("ROC_BASIC_CLI_MAX_FILES")
.map(|v| v.parse().unwrap_or(default_max_files))
.unwrap_or(default_max_files);
ThreadSafeRefcountedResourceHeap::new(max_files)
.expect("Failed to allocate mmap for file handle references.")
})
}
pub fn file_write_utf8(roc_path: &RocList<u8>, roc_str: &RocStr) -> RocResult<(), IOErr> {
write_slice(roc_path, roc_str.as_str().as_bytes())
}
pub fn file_write_bytes(roc_path: &RocList<u8>, roc_bytes: &RocList<u8>) -> RocResult<(), IOErr> {
write_slice(roc_path, roc_bytes.as_slice())
}
fn write_slice(roc_path: &RocList<u8>, bytes: &[u8]) -> RocResult<(), IOErr> {
match File::create(path_from_roc_path(roc_path)) {
Ok(mut file) => match file.write_all(bytes) {
Ok(()) => RocResult::ok(()),
Err(err) => RocResult::err(err.into()),
},
Err(err) => RocResult::err(err.into()),
}
}
#[repr(C)]
pub struct InternalPathType {
is_dir: bool,
is_file: bool,
is_sym_link: bool,
}
pub fn path_type(roc_path: &RocList<u8>) -> RocResult<InternalPathType, IOErr> {
let path = path_from_roc_path(roc_path);
match path.symlink_metadata() {
Ok(m) => RocResult::ok(InternalPathType {
is_dir: m.is_dir(),
is_file: m.is_file(),
is_sym_link: m.is_symlink(),
}),
Err(err) => RocResult::err(err.into()),
}
}
#[cfg(target_family = "unix")]
pub fn path_from_roc_path(bytes: &RocList<u8>) -> Cow<'_, Path> {
use std::os::unix::ffi::OsStrExt;
let os_str = OsStr::from_bytes(bytes.as_slice());
Cow::Borrowed(Path::new(os_str))
}
#[cfg(target_family = "windows")]
pub fn path_from_roc_path(bytes: &RocList<u8>) -> Cow<'_, Path> {
use std::os::windows::ffi::OsStringExt;
let bytes = bytes.as_slice();
assert_eq!(bytes.len() % 2, 0);
let characters: &[u16] =
unsafe { std::slice::from_raw_parts(bytes.as_ptr().cast(), bytes.len() / 2) };
let os_string = std::ffi::OsString::from_wide(characters);
Cow::Owned(std::path::PathBuf::from(os_string))
}
pub fn file_read_bytes(roc_path: &RocList<u8>) -> RocResult<RocList<u8>, IOErr> {
// TODO: write our own duplicate of `read_to_end` that directly fills a `RocList<u8>`.
// This adds an extra O(n) copy.
let mut bytes = Vec::new();
match File::open(path_from_roc_path(roc_path)) {
Ok(mut file) => match file.read_to_end(&mut bytes) {
Ok(_bytes_read) => RocResult::ok(RocList::from(bytes.as_slice())),
Err(err) => RocResult::err(err.into()),
},
Err(err) => RocResult::err(err.into()),
}
}
pub fn file_reader(roc_path: &RocList<u8>, size: u64) -> RocResult<RocBox<()>, IOErr> {
match File::open(path_from_roc_path(roc_path)) {
Ok(file) => {
let buf_reader = if size > 0 {
BufReader::with_capacity(size as usize, file)
} else {
BufReader::new(file)
};
let heap = heap();
let alloc_result = heap.alloc_for(buf_reader);
match alloc_result {
Ok(out) => RocResult::ok(out),
Err(err) => RocResult::err(err.into()),
}
}
Err(err) => RocResult::err(err.into()),
}
}
pub fn file_read_line(data: RocBox<()>) -> RocResult<RocList<u8>, IOErr> {
let buf_reader: &mut BufReader<File> = ThreadSafeRefcountedResourceHeap::box_to_resource(data);
let mut buffer = RocList::empty();
match read_until(buf_reader, b'\n', &mut buffer) {
Ok(..) => {
// Note: this returns an empty list when no bytes were read, e.g. End Of File
RocResult::ok(buffer)
}
Err(err) => RocResult::err(err.into()),
}
}
pub fn read_until<R: BufRead + ?Sized>(
r: &mut R,
delim: u8,
buf: &mut RocList<u8>,
) -> io::Result<usize> {
let mut read = 0;
loop {
let (done, used) = {
let available = match r.fill_buf() {
Ok(n) => n,
Err(ref e) if matches!(e.kind(), ErrorKind::Interrupted) => continue,
Err(e) => return Err(e),
};
match memchr::memchr(delim, available) {
Some(i) => {
buf.extend_from_slice(&available[..=i]);
(true, i + 1)
}
None => {
buf.extend_from_slice(available);
(false, available.len())
}
}
};
r.consume(used);
read += used;
if done || used == 0 {
return Ok(read);
}
}
}
pub fn file_delete(roc_path: &RocList<u8>) -> RocResult<(), IOErr> {
match std::fs::remove_file(path_from_roc_path(roc_path)) {
Ok(()) => RocResult::ok(()),
Err(err) => RocResult::err(err.into()),
}
}
/// Note: If the path is a directory or symlink, you probably don't want to call this function.
pub fn file_size_in_bytes(roc_path: &RocList<u8>) -> RocResult<u64, IOErr> {
let rust_path = path_from_roc_path(roc_path);
let metadata_res = std::fs::metadata(rust_path);
match metadata_res {
Ok(metadata) => {
RocResult::ok(metadata.len())
}
Err(err) => {
RocResult::err(err.into())
}
}
}
pub fn file_is_executable(roc_path: &RocList<u8>) -> RocResult<bool, IOErr> {
let rust_path = path_from_roc_path(roc_path);
#[cfg(unix)]
{
let metadata_res = std::fs::metadata(rust_path);
match metadata_res {
Ok(metadata) => {
let permissions = metadata.permissions();
RocResult::ok(permissions.mode() & 0o111 != 0)
}
Err(err) => {
RocResult::err(err.into())
}
}
}
#[cfg(windows)]
{
RocResult::err(IOErr{
msg: "Not yet implemented on windows.".into(),
tag: IOErrTag::Unsupported,
})
}
}
pub fn file_is_readable(roc_path: &RocList<u8>) -> RocResult<bool, IOErr> {
let rust_path = path_from_roc_path(roc_path);
#[cfg(unix)]
{
let metadata_res = std::fs::metadata(rust_path);
match metadata_res {
Ok(metadata) => {
let permissions = metadata.permissions();
RocResult::ok(permissions.mode() & 0o400 != 0)
}
Err(err) => {
RocResult::err(err.into())
}
}
}
#[cfg(windows)]
{
RocResult::err(IOErr{
msg: "Not yet implemented on windows.".into(),
tag: IOErrTag::Unsupported,
})
}
}
pub fn file_is_writable(roc_path: &RocList<u8>) -> RocResult<bool, IOErr> {
let rust_path = path_from_roc_path(roc_path);
#[cfg(unix)]
{
let metadata_res = std::fs::metadata(rust_path);
match metadata_res {
Ok(metadata) => {
let permissions = metadata.permissions();
RocResult::ok(permissions.mode() & 0o200 != 0)
}
Err(err) => {
RocResult::err(err.into())
}
}
}
#[cfg(windows)]
{
RocResult::err(IOErr{
msg: "Not yet implemented on windows.".into(),
tag: IOErrTag::Unsupported,
})
}
}
pub fn file_time_accessed(roc_path: &RocList<u8>) -> RocResult<roc_std::U128, IOErr> {
let rust_path = path_from_roc_path(roc_path);
let metadata_res = std::fs::metadata(rust_path);
match metadata_res {
Ok(metadata) => {
let accessed = metadata.accessed();
match accessed {
Ok(time) => {
RocResult::ok(
roc_std::U128::from(
time.duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()
)
)
}
Err(err) => {
RocResult::err(err.into())
}
}
}
Err(err) => {
RocResult::err(err.into())
}
}
}
pub fn file_time_modified(roc_path: &RocList<u8>) -> RocResult<roc_std::U128, IOErr> {
let rust_path = path_from_roc_path(roc_path);
let metadata_res = std::fs::metadata(rust_path);
match metadata_res {
Ok(metadata) => {
let modified = metadata.modified();
match modified {
Ok(time) => {
RocResult::ok(
roc_std::U128::from(
time.duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()
)
)
}
Err(err) => {
RocResult::err(err.into())
}
}
}
Err(err) => {
RocResult::err(err.into())
}
}
}
pub fn file_time_created(roc_path: &RocList<u8>) -> RocResult<roc_std::U128, IOErr> {
let rust_path = path_from_roc_path(roc_path);
let metadata_res = std::fs::metadata(rust_path);
match metadata_res {
Ok(metadata) => {
let created = metadata.created();
match created {
Ok(time) => {
RocResult::ok(
roc_std::U128::from(
time.duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()
)
)
}
Err(err) => {
RocResult::err(err.into())
}
}
}
Err(err) => {
RocResult::err(err.into())
}
}
}
pub fn file_rename(from_path: &RocList<u8>, to_path: &RocList<u8>) -> RocResult<(), IOErr> {
let rust_from_path = path_from_roc_path(from_path);
let rust_to_path = path_from_roc_path(to_path);
match std::fs::rename(rust_from_path, rust_to_path) {
Ok(()) => RocResult::ok(()),
Err(err) => RocResult::err(err.into()),
}
}
pub fn dir_list(roc_path: &RocList<u8>) -> RocResult<RocList<RocList<u8>>, IOErr> {
let path = path_from_roc_path(roc_path);
if path.is_dir() {
let dir = match std::fs::read_dir(path) {
Ok(dir) => dir,
Err(err) => return RocResult::err(err.into()),
};
let mut entries = Vec::new();
for entry in dir.flatten() {
let path = entry.path();
let str = path.as_os_str();
entries.push(os_str_to_roc_path(str));
}
RocResult::ok(RocList::from_iter(entries))
} else {
RocResult::err(IOErr {
msg: "NotADirectory".into(),
tag: IOErrTag::Other,
})
}
}
pub fn dir_create(roc_path: &RocList<u8>) -> RocResult<(), IOErr> {
match std::fs::create_dir(path_from_roc_path(roc_path)) {
Ok(_) => RocResult::ok(()),
Err(err) => RocResult::err(err.into()),
}
}
pub fn dir_create_all(roc_path: &RocList<u8>) -> RocResult<(), IOErr> {
match std::fs::create_dir_all(path_from_roc_path(roc_path)) {
Ok(_) => RocResult::ok(()),
Err(err) => RocResult::err(err.into()),
}
}
pub fn dir_delete_empty(roc_path: &RocList<u8>) -> RocResult<(), IOErr> {
match std::fs::remove_dir(path_from_roc_path(roc_path)) {
Ok(_) => RocResult::ok(()),
Err(err) => RocResult::err(err.into()),
}
}
pub fn dir_delete_all(roc_path: &RocList<u8>) -> RocResult<(), IOErr> {
match std::fs::remove_dir_all(path_from_roc_path(roc_path)) {
Ok(_) => RocResult::ok(()),
Err(err) => RocResult::err(err.into()),
}
}
pub fn hard_link(path_from: &RocList<u8>, path_to: &RocList<u8>) -> RocResult<(), IOErr> {
match std::fs::hard_link(path_from_roc_path(path_from), path_from_roc_path(path_to)) {
Ok(_) => RocResult::ok(()),
Err(err) => RocResult::err(err.into()),
}
}
#[cfg(target_family = "unix")]
pub fn os_str_to_roc_path(os_str: &OsStr) -> RocList<u8> {
use std::os::unix::ffi::OsStrExt;
RocList::from(os_str.as_bytes())
}
#[cfg(target_family = "windows")]
pub fn os_str_to_roc_path(os_str: &OsStr) -> RocList<u8> {
use std::os::windows::ffi::OsStrExt;
let bytes: Vec<_> = os_str.encode_wide().flat_map(|c| c.to_be_bytes()).collect();
RocList::from(bytes.as_slice())
}