diff options
author | Caleb Bassi <calebjbassi@gmail.com> | 2019-02-15 04:43:09 -0800 |
---|---|---|
committer | Caleb Bassi <calebjbassi@gmail.com> | 2019-02-15 05:56:09 -0800 |
commit | 29b3922f9efb9f277641a63f8f1719a15cbb8d16 (patch) | |
tree | d919ba11fa5c754565c64c43cb1614f7ae4d2546 /src/unix.rs | |
parent | 3853eef2d052460982903038daac7abd2b71d12e (diff) |
refactor: project layout
Diffstat (limited to 'src/unix.rs')
-rw-r--r-- | src/unix.rs | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/unix.rs b/src/unix.rs new file mode 100644 index 0000000..b7d228f --- /dev/null +++ b/src/unix.rs @@ -0,0 +1,145 @@ +extern crate libc; +extern crate ncurses; + +use std::path::{Path, PathBuf}; +use std::process; + +use config::mimetype; + +/* +pub const fn is_reg(mode: u32) -> bool +{ + mode >> 9 & S_IFREG >> 9 == mode >> 9 +} + +pub fn get_unix_filetype(mode : u32) -> &'static str +{ + match mode & BITMASK { + S_IFBLK => "inode/blockdevice", + S_IFCHR => "inode/chardevice", + S_IFDIR => "inode/directory", + S_IFIFO => "inode/fifo", + S_IFLNK => "inode/symlink", + S_IFSOCK => "inode/socket", + S_IFREG => "inode/regular", + _ => "unknown", + } +} +*/ + +pub fn is_executable(mode: u32) -> bool { + const LIBC_PERMISSION_VALS: [u32; 3] = [libc::S_IXUSR, libc::S_IXGRP, libc::S_IXOTH]; + + for val in LIBC_PERMISSION_VALS.iter() { + if mode & val != 0 { + return true; + } + } + return false; +} + +pub fn stringify_mode(mode: libc::mode_t) -> String { + let mut mode_str: String = String::with_capacity(10); + + const LIBC_FILE_VALS: [(libc::mode_t, char); 7] = [ + (libc::S_IFREG, '-'), + (libc::S_IFDIR, 'd'), + (libc::S_IFLNK, 'l'), + (libc::S_IFSOCK, 's'), + (libc::S_IFBLK, 'b'), + (libc::S_IFCHR, 'c'), + (libc::S_IFIFO, 'f'), + ]; + + const LIBC_PERMISSION_VALS: [(libc::mode_t, char); 9] = [ + (libc::S_IRUSR, 'r'), + (libc::S_IWUSR, 'w'), + (libc::S_IXUSR, 'x'), + (libc::S_IRGRP, 'r'), + (libc::S_IWGRP, 'w'), + (libc::S_IXGRP, 'x'), + (libc::S_IROTH, 'r'), + (libc::S_IWOTH, 'w'), + (libc::S_IXOTH, 'x'), + ]; + + let mode_shifted = mode >> 9; + for val in LIBC_FILE_VALS.iter() { + let val_shifted = val.0 >> 9; + if mode_shifted & val_shifted == mode_shifted { + mode_str.push(val.1); + break; + } + } + + for val in LIBC_PERMISSION_VALS.iter() { + if mode & val.0 != 0 { + mode_str.push(val.1); + } else { + mode_str.push('-'); + } + } + mode_str +} + +pub fn set_mode(path: &Path, mode: libc::mode_t) { + let os_path = path.as_os_str(); + if let Some(s) = os_path.to_str() { + let svec: Vec<i8> = s.bytes().map(|ch| ch as i8).collect(); + unsafe { + libc::chmod(svec.as_ptr(), mode); + } + } +} + +pub fn open_with_entry(paths: &Vec<PathBuf>, entry: &mimetype::JoshutoMimetypeEntry) { + let program = entry.program.clone(); + + let mut command = process::Command::new(program); + if let Some(args) = entry.args.as_ref() { + for arg in args { + command.arg(arg.clone()); + } + } + for path in paths { + command.arg(path.as_os_str()); + } + if let Some(true) = entry.silent { + command.stdout(process::Stdio::null()); + command.stderr(process::Stdio::null()); + } + + match command.spawn() { + Ok(mut handle) => { + if let Some(true) = entry.fork { + } else { + match handle.wait() { + Ok(_) => {} + Err(e) => eprintln!("{}", e), + } + } + } + Err(e) => eprintln!("{}", e), + } +} + +pub fn open_with_args(paths: &Vec<PathBuf>, args: &Vec<String>) { + let program = args[0].clone(); + let args_len = args.len(); + + let mut command = process::Command::new(program); + for i in 1..args_len { + command.arg(args[i].clone()); + } + for path in paths { + command.arg(path.as_os_str()); + } + + match command.spawn() { + Ok(mut handle) => match handle.wait() { + Ok(_) => {} + Err(e) => eprintln!("{}", e), + }, + Err(e) => eprintln!("{}", e), + } +} |