2023-09-08 19:39:00 +08:00
|
|
|
use crate::{CliprdrError, CliprdrServiceContext};
|
2023-09-04 15:38:53 +08:00
|
|
|
|
2023-08-24 22:34:12 +08:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
pub mod windows;
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
pub fn create_cliprdr_context(
|
|
|
|
enable_files: bool,
|
|
|
|
enable_others: bool,
|
|
|
|
response_wait_timeout_secs: u32,
|
|
|
|
) -> crate::ResultType<Box<dyn crate::CliprdrServiceContext>> {
|
2023-09-08 20:37:14 +08:00
|
|
|
let boxed =
|
|
|
|
windows::create_cliprdr_context(enable_files, enable_others, response_wait_timeout_secs)?
|
|
|
|
as Box<_>;
|
|
|
|
Ok(boxed)
|
2023-08-24 22:34:12 +08:00
|
|
|
}
|
2023-09-04 15:38:53 +08:00
|
|
|
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
/// use FUSE for file pasting on these platforms
|
|
|
|
pub mod fuse;
|
2023-10-28 22:43:13 +08:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
pub mod unix;
|
2023-10-29 02:15:16 +08:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
2023-09-04 15:38:53 +08:00
|
|
|
pub fn create_cliprdr_context(
|
|
|
|
enable_files: bool,
|
2023-09-08 19:39:00 +08:00
|
|
|
_enable_others: bool,
|
2023-09-04 15:38:53 +08:00
|
|
|
response_wait_timeout_secs: u32,
|
|
|
|
) -> crate::ResultType<Box<dyn crate::CliprdrServiceContext>> {
|
2023-10-27 20:40:23 +08:00
|
|
|
use std::{fs::Permissions, os::unix::prelude::PermissionsExt};
|
|
|
|
|
|
|
|
use hbb_common::{config::APP_NAME, log};
|
2023-09-09 19:24:38 +08:00
|
|
|
|
2023-09-08 19:39:00 +08:00
|
|
|
if !enable_files {
|
|
|
|
return Ok(Box::new(DummyCliprdrContext {}) as Box<_>);
|
|
|
|
}
|
|
|
|
|
|
|
|
let timeout = std::time::Duration::from_secs(response_wait_timeout_secs as u64);
|
2023-10-27 20:40:23 +08:00
|
|
|
|
|
|
|
let app_name = APP_NAME.read().unwrap().clone();
|
|
|
|
|
|
|
|
let mnt_path = format!("/tmp/{}/{}", app_name, "cliprdr");
|
|
|
|
|
|
|
|
// this function must be called after the main IPC is up
|
|
|
|
std::fs::create_dir(&mnt_path).ok();
|
|
|
|
std::fs::set_permissions(&mnt_path, Permissions::from_mode(0o777)).ok();
|
2023-09-08 19:39:00 +08:00
|
|
|
|
2023-10-07 17:26:20 +08:00
|
|
|
log::info!("clear previously mounted cliprdr FUSE");
|
2023-10-27 20:40:23 +08:00
|
|
|
if let Err(e) = std::process::Command::new("umount").arg(&mnt_path).status() {
|
|
|
|
log::warn!("umount {:?} may fail: {:?}", mnt_path, e);
|
2023-10-07 17:26:20 +08:00
|
|
|
}
|
|
|
|
|
2023-10-29 02:15:16 +08:00
|
|
|
let unix_ctx = unix::ClipboardContext::new(timeout, mnt_path.parse().unwrap())?;
|
2023-10-16 18:42:02 +08:00
|
|
|
log::debug!("start cliprdr FUSE");
|
2023-10-29 02:15:16 +08:00
|
|
|
unix_ctx.run().expect("failed to start cliprdr FUSE");
|
2023-09-08 19:39:00 +08:00
|
|
|
|
2023-10-29 02:15:16 +08:00
|
|
|
Ok(Box::new(unix_ctx) as Box<_>)
|
2023-09-04 15:38:53 +08:00
|
|
|
}
|
2023-09-08 19:39:00 +08:00
|
|
|
|
|
|
|
struct DummyCliprdrContext {}
|
|
|
|
|
|
|
|
impl CliprdrServiceContext for DummyCliprdrContext {
|
|
|
|
fn set_is_stopped(&mut self) -> Result<(), CliprdrError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn empty_clipboard(&mut self, _conn_id: i32) -> Result<bool, CliprdrError> {
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
fn server_clip_file(
|
|
|
|
&mut self,
|
|
|
|
_conn_id: i32,
|
|
|
|
_msg: crate::ClipboardFile,
|
|
|
|
) -> Result<(), crate::CliprdrError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// begin of epoch used by microsoft
|
|
|
|
// 1601-01-01 00:00:00 + LDAP_EPOCH_DELTA*(100 ns) = 1970-01-01 00:00:00
|
2023-10-29 07:15:56 +08:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
2023-09-08 19:39:00 +08:00
|
|
|
const LDAP_EPOCH_DELTA: u64 = 116444772610000000;
|