-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
file.rs
34 lines (31 loc) · 940 Bytes
/
file.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
// Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).
use std::io::Read;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
pub fn list_dir(path: &Path) -> Vec<String> {
let mut v: Vec<_> = std::fs::read_dir(path)
.unwrap_or_else(|err| panic!("Listing dir {:?}: {:?}", path, err))
.map(|entry| {
entry
.expect("Error reading entry")
.file_name()
.to_string_lossy()
.to_string()
})
.collect();
v.sort();
v
}
pub fn contents(path: &Path) -> bytes::Bytes {
let mut contents = Vec::new();
std::fs::File::open(path)
.and_then(|mut f| f.read_to_end(&mut contents))
.expect("Error reading file");
bytes::Bytes::from(contents)
}
pub fn is_executable(path: &Path) -> bool {
std::fs::metadata(path)
.map(|meta| meta.permissions().mode() & 0o100 == 0o100)
.unwrap_or(false)
}