2023-01-13 12:09:10 -05:00
|
|
|
use super::Cmd;
|
|
|
|
use clap::{Arg, ArgAction, Command};
|
2023-02-04 08:54:27 -05:00
|
|
|
use std::{error::Error, fs::OpenOptions};
|
2022-12-20 12:05:21 -05:00
|
|
|
|
2023-04-17 23:27:57 -04:00
|
|
|
#[derive(Debug)]
|
2023-01-14 02:08:14 -05:00
|
|
|
pub struct Sync;
|
2023-01-13 12:09:10 -05:00
|
|
|
|
2023-01-14 02:08:14 -05:00
|
|
|
impl Cmd for Sync {
|
2023-01-13 12:09:10 -05:00
|
|
|
fn cli(&self) -> clap::Command {
|
|
|
|
Command::new("sync")
|
|
|
|
.about("force completion of pending disk writes (flush cache)")
|
|
|
|
.author("Nathan Fisher")
|
|
|
|
.version(env!("CARGO_PKG_VERSION"))
|
|
|
|
.args([
|
|
|
|
Arg::new("data")
|
|
|
|
.short('d')
|
|
|
|
.long("data")
|
|
|
|
.help("sync only file data, no unneeded metadata")
|
|
|
|
.conflicts_with("fs")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
Arg::new("fs")
|
|
|
|
.short('f')
|
|
|
|
.long("file-system")
|
|
|
|
.help("sync the file systems that contain the files")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
Arg::new("FILE")
|
|
|
|
.help(
|
|
|
|
"If one or more files are specified, sync only them, or \
|
|
|
|
their containing file systems.",
|
|
|
|
)
|
|
|
|
.num_args(0..),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2023-02-04 08:54:27 -05:00
|
|
|
fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn Error>> {
|
2023-01-13 12:09:10 -05:00
|
|
|
if let Some(files) = matches.get_many::<String>("FILE") {
|
|
|
|
for f in files {
|
2023-02-02 23:34:37 -05:00
|
|
|
let mut opts = OpenOptions::new();
|
|
|
|
let opts = opts.read(true).write(true);
|
|
|
|
let fd = opts.open(f)?;
|
2023-01-13 12:09:10 -05:00
|
|
|
if matches.get_flag("data") {
|
2023-02-02 23:34:37 -05:00
|
|
|
unistd::fdatasync(&fd)?;
|
2023-01-13 12:09:10 -05:00
|
|
|
} else if matches.get_flag("fs") {
|
2023-02-02 23:34:37 -05:00
|
|
|
unistd::syncfs(&fd)?;
|
2023-01-13 12:09:10 -05:00
|
|
|
} else {
|
2023-02-02 23:34:37 -05:00
|
|
|
unistd::fsync(&fd)?;
|
2023-01-13 12:09:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-02 23:34:37 -05:00
|
|
|
unistd::sync();
|
2023-01-13 12:09:10 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-05 23:50:59 -05:00
|
|
|
fn path(&self) -> Option<shitbox::Path> {
|
|
|
|
Some(shitbox::Path::Bin)
|
2023-01-13 12:09:10 -05:00
|
|
|
}
|
|
|
|
}
|