1use clap::Parser;
2use rustutils_runnable::Runnable;
3use std::error::Error;
4use std::fs::File;
5use std::path::PathBuf;
6pub use syncfs::SyncFilesystem;
7
8mod syncfs;
9
10#[derive(Parser, Clone, Debug)]
12#[clap(author, version, about, long_about = None)]
13pub struct Sync {
14 #[clap(short, long)]
16 data: bool,
17 #[clap(short, long, conflicts_with = "data")]
19 file_system: bool,
20 files: Vec<PathBuf>,
22}
23
24impl Runnable for Sync {
25 fn run(&self) -> Result<(), Box<dyn Error>> {
26 if self.files.len() == 0 {
27 #[cfg(unix)]
28 nix::unistd::sync();
29 #[cfg(target_arch = "wasm32")]
30 return Err(String::from("syncing the filesystem is unsupported for wasm32").into());
31 } else {
32 for file in &self.files {
33 let file = File::open(file)?;
34 if self.file_system {
35 file.sync_filesystem()?;
36 } else if self.data {
37 file.sync_data()?;
38 } else {
39 file.sync_all()?;
40 }
41 }
42 }
43 Ok(())
44 }
45}