Rust implementation of git-build
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

51 lines
1.3 KiB

use std::fs;
use std::os::unix::fs::MetadataExt;
use std::path::Path;
/// Reads the file and strips whitespace (including newlines)
/// Useful for file-based key-value store
pub fn read_or_none(path: &Path) -> Option<String> {
if !path.is_file() {
return None;
}
match fs::read_to_string(path) {
Ok(content) => {
// Remove trailing space/newlines
Some(content.trim().to_string())
}
Err(e) => {
eprintln!("IO ERROR: {}", e);
None
}
}
}
pub fn read_extension(path: &Path, setting: &str) -> Option<String> {
let mut path = path.to_path_buf();
path.set_extension(setting);
read_or_none(&path)
}
/// Returns true when the file exists and has user exec
/// permission. Returns false otherwise.
pub fn is_executable(path: &Path) -> bool {
// Do not match directories
if !path.is_file() {
return false;
}
match fs::metadata(path) {
Ok(stat) => {
let mode = stat.mode();
// Check user exec permission)
if (mode & 0o100) == 0o100 {
true
} else {
false
}
}
Err(e) => {
eprintln!("IO Error: {}", e);
false
}
}
}