Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

By default, only report progress when stderr is a terminal #283

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions fclones/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,8 +749,16 @@ const fn after_help() -> &'static str {
#[derive(clap::Parser, Debug)]
#[command(about, author, version, after_help = after_help(), max_term_width = 100)]
pub struct Config {
/// Suppress progress reporting
#[arg(short('q'), long)]
/// Override progress reporting, by default (=auto) only report when stderr is a terminal.
/// Possible values: true, false, auto.
#[arg(long, value_name = "VAL", require_equals = true,
value_parser(["auto", "true", "false"]), default_value = "auto",
Comment on lines +752 to +755
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about also adding default_missing_value = "true", num_args = 0..=1, so that --progress == --progress=true, but that would make the help text even longer and harder to understand. And a value parser to convert "true" etc. is also possible, but that makes the argparse logic more complex than the simple thing which it toggles.


hide_possible_values = true, hide_default_value = true)]
pub progress: String,

// compatibility with fclones <= 0.34, overrides --progress
#[arg(short('q'), long, hide = true)]
pub quiet: bool,

/// Find files
Expand Down
4 changes: 1 addition & 3 deletions fclones/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,9 +889,7 @@ fn update_file_locations(ctx: &GroupCtx<'_>, groups: &mut (impl FileCollection +
// Do not print a notice about slower access when fetching file extents has
// failed because a file vanished -- now it will never be accessed anyhow.
const ENOENT_NO_SUCH_FILE: i32 = 2;
if e.raw_os_error()
.map_or(true, |err| err != ENOENT_NO_SUCH_FILE)
{
if e.raw_os_error() != Some(ENOENT_NO_SUCH_FILE) {
handle_fetch_physical_location_err(ctx, &err_counters, fi, e)
}
}
Expand Down
2 changes: 1 addition & 1 deletion fclones/src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ impl FileHasher<'_> {
}
}

impl<'a> Drop for FileHasher<'a> {
impl Drop for FileHasher<'_> {
fn drop(&mut self) {
if let Some(cache) = self.cache.take() {
if let Err(e) = cache.close() {
Expand Down
11 changes: 7 additions & 4 deletions fclones/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::ffi::{OsStr, OsString};
use std::fs::File;
use std::io::{stdin, Write};
use std::io::{stderr, stdin, IsTerminal, Write};
use std::process::exit;
use std::sync::Arc;
use std::{fs, io};
Expand Down Expand Up @@ -251,9 +251,12 @@ fn main() {
}

let mut log = StdLog::new();
if config.quiet {
log.no_progress = true;
}
log.no_progress = match (config.quiet, config.progress.as_str()) {
(true, _) => true,
(_, "false") => true,
(_, "true") => false,
(_, _auto) => !stderr().is_terminal(),
};

let cwd = match std::env::current_dir() {
Ok(cwd) => cwd,
Expand Down
10 changes: 7 additions & 3 deletions fclones/src/reflink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ fn reflink_overwrite(target: &std::path::Path, link: &std::path::Path) -> io::Re
let src = fs::File::open(target)?;

// This operation does not require `.truncate(true)` because the files are already of the same size.
let dest = fs::OpenOptions::new().create(true).write(true).open(link)?;
let dest = fs::OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(link)?;

// From /usr/include/linux/fs.h:
// #define FICLONE _IOW(0x94, 9, int)
Expand Down Expand Up @@ -363,7 +367,7 @@ pub mod test {
}
}

impl<'a> Drop for CrossTest<'a> {
impl Drop for CrossTest<'_> {
fn drop(&mut self) {
*CROSSTEST.lock().unwrap() = false;
}
Expand Down Expand Up @@ -398,7 +402,7 @@ pub mod test {
with_dir(test_root, |root| {
// Always clean up files in /dev/shm, even after failure
struct CleanupGuard<'a>(&'a str);
impl<'a> Drop for CleanupGuard<'a> {
impl Drop for CleanupGuard<'_> {
fn drop(&mut self) {
fs::remove_dir_all(self.0).unwrap();
}
Expand Down
4 changes: 2 additions & 2 deletions fclones/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ mod test {
let input = output.reopen().unwrap();

let mut writer = ReportWriter::new(output, false);
writer.write_as_text(&header1, groups.into_iter()).unwrap();
writer.write_as_text(&header1, groups).unwrap();

let mut reader = TextReportReader::new(BufReader::new(input));
let header2 = reader.read_header().unwrap();
Expand Down Expand Up @@ -841,7 +841,7 @@ mod test {
let input = output.reopen().unwrap();

let mut writer = ReportWriter::new(output, false);
writer.write_as_json(&header1, groups.into_iter()).unwrap();
writer.write_as_json(&header1, groups).unwrap();

let mut reader = JsonReportReader::new(input).unwrap();
let header2 = reader.read_header().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion fclones/src/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Semaphore {
}
}

impl<'a> Drop for SemaphoreGuard<'a> {
impl Drop for SemaphoreGuard<'_> {
fn drop(&mut self) {
self.sem.release();
}
Expand Down
6 changes: 3 additions & 3 deletions fclones/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<'a> Walk<'a> {
for p in roots.into_iter() {
let p = self.absolute(p);
let ignore = ignore.clone();
match fs::metadata(&p.to_path_buf()) {
match fs::metadata(p.to_path_buf()) {
Ok(metadata) if metadata.is_dir() && self.depth == 0 => self.log_warn(format!(
"Skipping directory {} because recursive scan is disabled.",
p.display()
Expand Down Expand Up @@ -384,7 +384,7 @@ impl<'a> Walk<'a> {
}

#[cfg(unix)]
fn sort_dir_entries_by_inode(entries: &mut Vec<DirEntry>) {
fn sort_dir_entries_by_inode(entries: &mut [DirEntry]) {
use rayon::prelude::ParallelSliceMut;
use std::os::unix::fs::DirEntryExt;
entries.par_sort_unstable_by_key(|entry| entry.ino())
Expand Down Expand Up @@ -471,7 +471,7 @@ impl<'a> Walk<'a> {
}
}

impl<'a> Default for Walk<'a> {
impl Default for Walk<'_> {
fn default() -> Self {
Self::new()
}
Expand Down