diqiu50 commented on code in PR #6321: URL: https://github.com/apache/gravitino/pull/6321#discussion_r1942462187
########## clients/filesystem-fuse/src/main.rs: ########## @@ -16,49 +16,214 @@ * specific language governing permissions and limitations * under the License. */ -use fuse3::Errno; +mod command_args; + +use crate::command_args::Commands; +use clap::Parser; +use daemonize::Daemonize; use gvfs_fuse::config::AppConfig; -use gvfs_fuse::{gvfs_mount, gvfs_unmount}; +use gvfs_fuse::{gvfs_mount, gvfs_unmount, LOG_FILE_NAME, PID_FILE_NAME}; use log::{error, info}; +use std::fs::{create_dir, OpenOptions}; +use std::io; +use std::path::Path; +use std::process::Command; +use tokio::runtime::Runtime; use tokio::signal; +use tokio::signal::unix::{signal, SignalKind}; -#[tokio::main] -async fn main() -> fuse3::Result<()> { - tracing_subscriber::fmt().init(); +fn init_work_dirs(config: &AppConfig, mount_point: &str) -> io::Result<()> { + let data_dir_name = Path::new(&config.fuse.data_dir).to_path_buf(); + let data_dir_name = data_dir_name.canonicalize()?; + if !data_dir_name.exists() { + Err(io::Error::new( + io::ErrorKind::NotFound, + format!("Data directory {} not found", &config.fuse.data_dir), + ))? + }; - // todo need inmprove the args parsing - let args: Vec<String> = std::env::args().collect(); - let (mount_point, mount_from, config_path) = match args.len() { - 4 => (args[1].clone(), args[2].clone(), args[3].clone()), - _ => { - error!("Usage: {} <mount_point> <mount_from> <config>", args[0]); - return Err(Errno::from(libc::EINVAL)); - } + let mount_point_name = data_dir_name.join(mount_point); + if !mount_point_name.exists() { + create_dir(&mount_point_name)? + }; + + let log_dir_name = data_dir_name.join(&config.fuse.log_dir); + if !log_dir_name.exists() { + create_dir(&log_dir_name)? }; - //todo(read config file from args) - let config = AppConfig::from_file(Some(&config_path)); - if let Err(e) = &config { - error!("Failed to load config: {:?}", e); - return Err(Errno::from(libc::EINVAL)); + Ok(()) +} + +fn make_daemon(config: &AppConfig) -> io::Result<()> { + let data_dir_name = Path::new(&config.fuse.data_dir); + let log_dir_name = data_dir_name.join(&config.fuse.log_dir); + let log_file_name = log_dir_name.join(LOG_FILE_NAME); + let log_file = OpenOptions::new() + .create(true) + .append(true) + .open(&log_file_name) + .unwrap(); + let log_err_file = OpenOptions::new().append(true).open(log_file_name).unwrap(); + + let pid_file_name = data_dir_name.join(PID_FILE_NAME); + + let daemonize = Daemonize::new() + .pid_file(pid_file_name) + .chown_pid_file(true) + .working_directory(data_dir_name) + .stdout(log_file) + .stderr(log_err_file); + + match daemonize.start() { + Ok(_) => info!("Gvfs-fuse Daemon started successfully"), + Err(e) => { + return Err(io::Error::new( + io::ErrorKind::Other, + format!("Gvfs-fuse Daemon failed to start: {:?}", e), + )) + } } - let config = config.unwrap(); - let handle = tokio::spawn(async move { - let result = gvfs_mount(&mount_point, &mount_from, &config).await; - if let Err(e) = result { - error!("Failed to mount gvfs: {:?}", e); - return Err(Errno::from(libc::EINVAL)); + Ok(()) +} + +fn mount_fuse(config: AppConfig, mount_point: String, target: String) -> io::Result<()> { + let rt = Runtime::new()?; + rt.block_on(async { + let handle = tokio::spawn(async move { + let result = gvfs_mount(&mount_point, &target, &config).await; + if let Err(e) = result { + error!("Failed to mount gvfs: {:?}", e); + return Err(io::Error::from(io::ErrorKind::InvalidInput)); + } + Ok(()) + }); + + let mut term_signal = signal(SignalKind::terminate())?; + tokio::select! { + _ = handle => {} + _ = signal::ctrl_c() => { + info!("Received Ctrl+C, unmounting gvfs...") + } + _ = term_signal.recv()=> { + info!("Received SIGTERM, unmounting gvfs...") + } } + + let _ = gvfs_unmount().await; Ok(()) - }); + }) +} - tokio::select! { - _ = handle => {} - _ = signal::ctrl_c() => { - info!("Received Ctrl+C, unmounting gvfs..."); +#[cfg(target_os = "macos")] Review Comment: Linux also supported. see the other `do_umount` function -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@gravitino.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org