From 36ae36282a9436c3944807c9f64356c63d298c38 Mon Sep 17 00:00:00 2001 From: David Knaack Date: Sun, 2 Aug 2020 18:42:38 +0200 Subject: [PATCH] refactor(directory/lock): fix clippy, replace str with Path (#1544) --- src/modules/directory.rs | 6 +++--- src/modules/utils/directory_nix.rs | 7 ++++--- src/modules/utils/directory_win.rs | 11 ++++++----- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/modules/directory.rs b/src/modules/directory.rs index 51297bd4..f0e1f4b4 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -107,7 +107,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { .map(|variable| match variable { "path" => Some(Ok(&final_dir_string)), "read_only" => { - if is_readonly_dir(context.current_dir.to_str()?) { + if is_readonly_dir(&context.current_dir) { Some(Ok(&lock_symbol)) } else { None @@ -129,12 +129,12 @@ pub fn module<'a>(context: &'a Context) -> Option> { Some(module) } -fn is_readonly_dir(path: &str) -> bool { +fn is_readonly_dir(path: &Path) -> bool { match directory_utils::is_write_allowed(path) { Ok(res) => !res, Err(e) => { log::debug!( - "Failed to detemine read only status of directory '{}': {}", + "Failed to detemine read only status of directory '{:?}': {}", path, e ); diff --git a/src/modules/utils/directory_nix.rs b/src/modules/utils/directory_nix.rs index 87f94b7e..214e06b6 100644 --- a/src/modules/utils/directory_nix.rs +++ b/src/modules/utils/directory_nix.rs @@ -3,6 +3,7 @@ use nix::unistd::{Gid, Uid}; use std::fs; use std::os::unix::fs::MetadataExt; use std::os::unix::fs::PermissionsExt; +use std::path::Path; /// Checks if the current user can write to the `folder_path`. /// @@ -12,7 +13,7 @@ use std::os::unix::fs::PermissionsExt; /// 2a) (not implemented on macOS) one of the supplementary groups of the current user is the /// directory group owner and whether it has write access /// 3) 'others' part of the access mask has the write access -pub fn is_write_allowed(folder_path: &str) -> Result { +pub fn is_write_allowed(folder_path: &Path) -> Result { let meta = fs::metadata(folder_path).map_err(|_| "Unable to stat() directory")?; let perms = meta.permissions().mode(); @@ -52,9 +53,9 @@ mod tests { #[test] #[ignore] fn read_only_test() { - assert_eq!(is_write_allowed("/etc"), Ok(false)); + assert_eq!(is_write_allowed(Path::new("/etc")), Ok(false)); assert_eq!( - is_write_allowed("/i_dont_exist"), + is_write_allowed(Path::new("/i_dont_exist")), Err("Unable to stat() directory") ); } diff --git a/src/modules/utils/directory_win.rs b/src/modules/utils/directory_win.rs index decf1eb9..033bfb33 100644 --- a/src/modules/utils/directory_win.rs +++ b/src/modules/utils/directory_win.rs @@ -1,9 +1,9 @@ extern crate winapi; -use std::ffi::OsStr; use std::iter; use std::mem; use std::os::windows::ffi::OsStrExt; +use std::path::Path; use winapi::ctypes::c_void; use winapi::shared::minwindef::{BOOL, DWORD}; use winapi::um::handleapi; @@ -21,8 +21,9 @@ use winapi::um::winnt::{ /// First, the function extracts DACL from the given directory and then calls `AccessCheck` against /// the current process access token and directory's security descriptor. /// Does not work for network drives and always returns true -pub fn is_write_allowed(folder_path: &str) -> std::result::Result { - let folder_name: Vec = OsStr::new(folder_path) +pub fn is_write_allowed(folder_path: &Path) -> std::result::Result { + let folder_name: Vec = folder_path + .as_os_str() .encode_wide() .chain(iter::once(0)) .collect(); @@ -131,6 +132,6 @@ extern "system" { fn PathIsNetworkPathW(pszPath: LPCWSTR) -> BOOLEAN; } -fn is_network_path(folder_path: &Vec) -> bool { - return unsafe { PathIsNetworkPathW(folder_path.as_ptr()) } == 1; +fn is_network_path(folder_path: &[u16]) -> bool { + unsafe { PathIsNetworkPathW(folder_path.as_ptr()) == 1 } }