From d0105d70d9e1bfb58f1c77b6e4be7330332c4887 Mon Sep 17 00:00:00 2001 From: Jiayi Zhao Date: Mon, 18 Feb 2019 20:58:17 -0500 Subject: attempt to fix portability problems between rust std and libc --- src/commands/set_mode.rs | 26 +++++++++++++------------- src/ui.rs | 3 ++- src/unix.rs | 23 +++++++++++++---------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/commands/set_mode.rs b/src/commands/set_mode.rs index a9c91c7..27b0b4a 100644 --- a/src/commands/set_mode.rs +++ b/src/commands/set_mode.rs @@ -11,6 +11,18 @@ use unix; #[derive(Clone, Debug)] pub struct SetMode; +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'), +]; + impl SetMode { pub fn new() -> Self { SetMode @@ -38,20 +50,8 @@ impl SetMode { } ncurses::doupdate(); - 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'), - ]; - if let Some(s) = user_input { - let mut mode: libc::mode_t = 0; + let mut mode: u32 = 0; for (i, ch) in s.chars().enumerate() { if ch == LIBC_PERMISSION_VALS[i].1 { mode |= LIBC_PERMISSION_VALS[i].0; diff --git a/src/ui.rs b/src/ui.rs index 708387f..fc5c76d 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -198,7 +198,8 @@ pub fn wprint_file_info(win: ncurses::WINDOW, file: &structs::JoshutoDirEntry) { ncurses::waddch(win, ' ' as ncurses::chtype); if file.path.is_dir() { - if mode >> 9 & libc::S_IFLNK >> 9 == mode >> 9 { + let is_link: u32 = libc::S_IFLNK.into(); + if mode >> 9 & is_link >> 9 == mode >> 9 { if let Ok(path) = fs::read_link(&file.path) { ncurses::waddstr(win, " -> "); ncurses::waddstr(win, path.to_str().unwrap()); diff --git a/src/unix.rs b/src/unix.rs index 3150d93..92f48e6 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -27,10 +27,11 @@ pub fn get_unix_filetype(mode : u32) -> &'static str } */ -pub fn is_executable(mode: libc::mode_t) -> bool { +pub fn is_executable(mode: u32) -> bool { const LIBC_PERMISSION_VALS: [libc::mode_t; 3] = [libc::S_IXUSR, libc::S_IXGRP, libc::S_IXOTH]; for val in LIBC_PERMISSION_VALS.iter() { + let val: u32 = (*val).into(); if mode & val != 0 { return true; } @@ -38,7 +39,7 @@ pub fn is_executable(mode: libc::mode_t) -> bool { false } -pub fn stringify_mode(mode: libc::mode_t) -> String { +pub fn stringify_mode(mode: u32) -> String { let mut mode_str: String = String::with_capacity(10); const LIBC_FILE_VALS: [(libc::mode_t, char); 7] = [ @@ -64,17 +65,19 @@ pub fn stringify_mode(mode: libc::mode_t) -> String { ]; 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); + + for (val, ch) in LIBC_FILE_VALS.iter() { + let val: u32 = (*val >> 9).into(); + if mode_shifted & val == mode_shifted { + mode_str.push(*ch); break; } } - for val in LIBC_PERMISSION_VALS.iter() { - if mode & val.0 != 0 { - mode_str.push(val.1); + for (val, ch) in LIBC_PERMISSION_VALS.iter() { + let val: u32 = (*val).into(); + if mode & val != 0 { + mode_str.push(*ch); } else { mode_str.push('-'); } @@ -82,7 +85,7 @@ pub fn stringify_mode(mode: libc::mode_t) -> String { mode_str } -pub fn set_mode(path: &Path, mode: libc::mode_t) { +pub fn set_mode(path: &Path, mode: u32) { let os_path = path.as_os_str(); if let Some(s) = os_path.to_str() { let svec: Vec = s.bytes().map(|ch| ch as i8).collect(); -- cgit v1.2.3