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

@@ -3,7 +3,7 @@ use std::{
fs::File,
io::{BufRead, BufReader, Read, Seek},
os::unix::prelude::PermissionsExt,
path::PathBuf,
path::{Path, PathBuf},
sync::atomic::{AtomicU64, Ordering},
time::SystemTime,
};
@@ -51,7 +51,7 @@ pub(super) struct LocalFile {
}
impl LocalFile {
pub fn try_open(path: &PathBuf) -> Result<Self, CliprdrError> {
pub fn try_open(path: &Path) -> Result<Self, CliprdrError> {
let mt = std::fs::metadata(path).map_err(|e| CliprdrError::FileError {
path: path.clone(),
err: e,
@@ -219,7 +219,7 @@ impl LocalFile {
pub(super) fn construct_file_list(paths: &[PathBuf]) -> Result<Vec<LocalFile>, CliprdrError> {
fn constr_file_lst(
path: &PathBuf,
path: &Path,
file_list: &mut Vec<LocalFile>,
visited: &mut HashSet<PathBuf>,
) -> Result<(), CliprdrError> {

View File

@@ -1,5 +1,5 @@
use std::{
path::PathBuf,
path::{Path, PathBuf},
sync::{mpsc::Sender, Arc},
time::Duration,
};
@@ -74,7 +74,7 @@ trait SysClipboard: Send + Sync {
}
#[cfg(target_os = "linux")]
fn get_sys_clipboard(ignore_path: &PathBuf) -> Result<Box<dyn SysClipboard>, CliprdrError> {
fn get_sys_clipboard(ignore_path: &Path) -> Result<Box<dyn SysClipboard>, CliprdrError> {
#[cfg(feature = "wayland")]
{
unimplemented!()
@@ -88,7 +88,7 @@ fn get_sys_clipboard(ignore_path: &PathBuf) -> Result<Box<dyn SysClipboard>, Cli
}
#[cfg(target_os = "macos")]
fn get_sys_clipboard(ignore_path: &PathBuf) -> Result<Box<dyn SysClipboard>, CliprdrError> {
fn get_sys_clipboard(ignore_path: &Path) -> Result<Box<dyn SysClipboard>, CliprdrError> {
use ns_clipboard::*;
let ns_pb = NsPasteboard::new(ignore_path)?;
Ok(Box::new(ns_pb) as Box<_>)

View File

@@ -1,4 +1,7 @@
use std::{collections::BTreeSet, path::PathBuf};
use std::{
collections::BTreeSet,
path::{Path, PathBuf},
};
use cacao::pasteboard::{Pasteboard, PasteboardName};
use hbb_common::log;
@@ -30,7 +33,7 @@ pub struct NsPasteboard {
}
impl NsPasteboard {
pub fn new(ignore_path: &PathBuf) -> Result<Self, CliprdrError> {
pub fn new(ignore_path: &Path) -> Result<Self, CliprdrError> {
Ok(Self {
ignore_path: ignore_path.to_owned(),
former_file_list: Mutex::new(vec![]),

View File

@@ -7,7 +7,7 @@ use crate::CliprdrError;
// url encode and decode is needed
const ENCODE_SET: percent_encoding::AsciiSet = percent_encoding::CONTROLS.add(b' ').remove(b'/');
pub(super) fn encode_path_to_uri(path: &PathBuf) -> io::Result<String> {
pub(super) fn encode_path_to_uri(path: &Path) -> io::Result<String> {
let encoded =
percent_encoding::percent_encode(path.to_str()?.as_bytes(), &ENCODE_SET).to_string();
format!("file://{}", encoded)

View File

@@ -1,4 +1,7 @@
use std::{collections::BTreeSet, path::PathBuf};
use std::{
collections::BTreeSet,
path::{Path, PathBuf},
};
use hbb_common::log;
use once_cell::sync::OnceCell;
@@ -26,7 +29,7 @@ pub struct X11Clipboard {
}
impl X11Clipboard {
pub fn new(ignore_path: &PathBuf) -> Result<Self, CliprdrError> {
pub fn new(ignore_path: &Path) -> Result<Self, CliprdrError> {
let clipboard = get_clip()?;
let text_uri_list = clipboard
.setter

View File

@@ -123,7 +123,7 @@ pub fn get_home_as_string() -> String {
}
fn read_dir_recursive(
path: &PathBuf,
path: &Path,
prefix: &Path,
include_hidden: bool,
) -> ResultType<Vec<FileEntry>> {
@@ -186,7 +186,7 @@ pub fn get_recursive_files(path: &str, include_hidden: bool) -> ResultType<Vec<F
}
fn read_empty_dirs_recursive(
path: &PathBuf,
path: &Path,
prefix: &Path,
include_hidden: bool,
) -> ResultType<Vec<FileDirectory>> {
@@ -854,7 +854,7 @@ pub async fn handle_read_jobs(
Ok(job_log)
}
pub fn remove_all_empty_dir(path: &PathBuf) -> ResultType<()> {
pub fn remove_all_empty_dir(path: &Path) -> ResultType<()> {
let fd = read_dir(path, true)?;
for entry in fd.entries.iter() {
match entry.entry_type.enum_value() {

View File

@@ -1,7 +1,7 @@
use std::{
fs::{self},
io::{Cursor, Read},
path::PathBuf,
path::Path,
};
#[cfg(windows)]
@@ -42,7 +42,7 @@ impl BinaryData {
buf
}
pub fn write_to_file(&self, prefix: &PathBuf) {
pub fn write_to_file(&self, prefix: &Path) {
let p = prefix.join(&self.path);
if let Some(parent) = p.parent() {
if !parent.exists() {
@@ -122,7 +122,7 @@ impl BinaryReader {
}
#[cfg(linux)]
pub fn configure_permission(&self, prefix: &PathBuf) {
pub fn configure_permission(&self, prefix: &Path) {
use std::os::unix::prelude::PermissionsExt;
let exe_path = prefix.join(&self.exe);

View File

@@ -1,7 +1,7 @@
#![windows_subsystem = "windows"]
use std::{
path::PathBuf,
path::{Path, PathBuf},
process::{Command, Stdio},
};
@@ -22,7 +22,7 @@ const APPNAME_RUNTIME_ENV_KEY: &str = "RUSTDESK_APPNAME";
#[cfg(windows)]
const SET_FOREGROUND_WINDOW_ENV_KEY: &str = "SET_FOREGROUND_WINDOW";
fn is_timestamp_matches(dir: &PathBuf, ts: &mut u64) -> bool {
fn is_timestamp_matches(dir: &Path, ts: &mut u64) -> bool {
let Ok(app_metadata) = std::str::from_utf8(APP_METADATA) else {
return true;
};
@@ -50,7 +50,7 @@ fn is_timestamp_matches(dir: &PathBuf, ts: &mut u64) -> bool {
false
}
fn write_meta(dir: &PathBuf, ts: u64) {
fn write_meta(dir: &Path, ts: u64) {
let meta_file = dir.join(APP_METADATA_CONFIG);
if ts != 0 {
let content = format!("{}{}", META_LINE_PREFIX_TIMESTAMP, ts);
@@ -169,13 +169,13 @@ fn main() {
#[cfg(windows)]
mod windows {
use std::{fs, os::windows::process::CommandExt, path::PathBuf, process::Command};
use std::{fs, os::windows::process::CommandExt, path::Path, process::Command};
// Used for privacy mode(magnifier impl).
pub const RUNTIME_BROKER_EXE: &'static str = "C:\\Windows\\System32\\RuntimeBroker.exe";
pub const WIN_TOPMOST_INJECTED_PROCESS_EXE: &'static str = "RuntimeBroker_rustdesk.exe";
pub(super) fn copy_runtime_broker(dir: &PathBuf) {
pub(super) fn copy_runtime_broker(dir: &Path) {
let src = RUNTIME_BROKER_EXE;
let tgt = WIN_TOPMOST_INJECTED_PROCESS_EXE;
let target_file = dir.join(tgt);