refactor: replace &PathBuf with &Path to enhance generality (#10332)

This commit is contained in:
Integral
2024-12-23 20:28:04 +08:00
committed by GitHub
parent 7289dbc80f
commit 49dabd3533
13 changed files with 44 additions and 40 deletions

View File

@@ -27,7 +27,7 @@ use include_dir::{include_dir, Dir};
use objc::rc::autoreleasepool;
use objc::{class, msg_send, sel, sel_impl};
use scrap::{libc::c_void, quartz::ffi::*};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
static PRIVILEGES_SCRIPTS_DIR: Dir =
include_dir!("$CARGO_MANIFEST_DIR/src/platform/privileges_scripts");
@@ -661,7 +661,7 @@ pub fn hide_dock() {
}
#[inline]
fn get_server_start_time_of(p: &Process, path: &PathBuf) -> Option<i64> {
fn get_server_start_time_of(p: &Process, path: &Path) -> Option<i64> {
let cmd = p.cmd();
if cmd.len() <= 1 {
return None;
@@ -679,7 +679,7 @@ fn get_server_start_time_of(p: &Process, path: &PathBuf) -> Option<i64> {
}
#[inline]
fn get_server_start_time(sys: &mut System, path: &PathBuf) -> Option<(i64, Pid)> {
fn get_server_start_time(sys: &mut System, path: &Path) -> Option<(i64, Pid)> {
sys.refresh_processes_specifics(ProcessRefreshKind::new());
for (_, p) in sys.processes() {
if let Some(t) = get_server_start_time_of(p, path) {

View File

@@ -1460,15 +1460,13 @@ fn to_le(v: &mut [u16]) -> &[u8] {
unsafe { v.align_to().1 }
}
fn get_undone_file(tmp: &PathBuf) -> ResultType<PathBuf> {
let mut tmp1 = tmp.clone();
tmp1.set_file_name(format!(
fn get_undone_file(tmp: &Path) -> ResultType<PathBuf> {
Ok(tmp.with_file_name(format!(
"{}.undone",
tmp.file_name()
.ok_or(anyhow!("Failed to get filename of {:?}", tmp))?
.to_string_lossy()
));
Ok(tmp1)
)))
}
fn run_cmds(cmds: String, show: bool, tip: &str) -> ResultType<()> {
@@ -1933,7 +1931,7 @@ pub fn create_process_with_logon(user: &str, pwd: &str, exe: &str, arg: &str) ->
return Ok(());
}
pub fn set_path_permission(dir: &PathBuf, permission: &str) -> ResultType<()> {
pub fn set_path_permission(dir: &Path, permission: &str) -> ResultType<()> {
std::process::Command::new("icacls")
.arg(dir.as_os_str())
.arg("/grant")

View File

@@ -452,7 +452,7 @@ pub(super) mod install {
use std::{
fs::File,
io::{BufReader, BufWriter, Write},
path::PathBuf,
path::Path,
};
use zip::ZipArchive;
@@ -488,7 +488,7 @@ pub(super) mod install {
Ok(())
}
fn download_file(id: &str, url: &str, filename: &PathBuf) -> bool {
fn download_file(id: &str, url: &str, filename: &Path) -> bool {
let file = match File::create(filename) {
Ok(f) => f,
Err(e) => {
@@ -505,7 +505,7 @@ pub(super) mod install {
true
}
fn do_install_file(filename: &PathBuf, target_dir: &PathBuf) -> ResultType<()> {
fn do_install_file(filename: &Path, target_dir: &Path) -> ResultType<()> {
let mut zip = ZipArchive::new(BufReader::new(File::open(filename)?))?;
for i in 0..zip.len() {
let mut file = zip.by_index(i)?;

View File

@@ -13,7 +13,7 @@ use serde_derive::Serialize;
use std::{
collections::{HashMap, HashSet},
ffi::{c_char, c_void},
path::PathBuf,
path::Path,
sync::{Arc, RwLock},
};
@@ -299,7 +299,7 @@ pub(super) fn load_plugins(uninstalled_ids: &HashSet<String>) -> ResultType<()>
Ok(())
}
fn load_plugin_dir(dir: &PathBuf) {
fn load_plugin_dir(dir: &Path) {
log::debug!("Begin load plugin dir: {}", dir.display());
if let Ok(rd) = std::fs::read_dir(dir) {
for entry in rd {

View File

@@ -15,7 +15,7 @@ use shared_memory::*;
use std::{
mem::size_of,
ops::{Deref, DerefMut},
path::PathBuf,
path::Path,
sync::{Arc, Mutex},
time::Duration,
};
@@ -92,7 +92,7 @@ impl SharedMemory {
}
};
log::info!("Create shared memory, size: {}, flink: {}", size, flink);
set_path_permission(&PathBuf::from(flink), "F").ok();
set_path_permission(Path::new(&flink), "F").ok();
Ok(SharedMemory { inner: shmem })
}
@@ -586,8 +586,8 @@ pub mod client {
let mut exe = std::env::current_exe()?.to_string_lossy().to_string();
#[cfg(feature = "flutter")]
{
if let Some(dir) = PathBuf::from(&exe).parent() {
if set_path_permission(&PathBuf::from(dir), "RX").is_err() {
if let Some(dir) = Path::new(&exe).parent() {
if set_path_permission(Path::new(dir), "RX").is_err() {
*SHMEM.lock().unwrap() = None;
bail!("Failed to set permission of {:?}", dir);
}