Skip to content

Commit 7b0e7e4

Browse files
reubenwong97sylvestreChrisDryden
authored
sync: return after checking all inputs (#10742)
* sync: return after checking all inputs * fix missing error context * use set_exit_code and update translation templates to show errors * remove thiserror dependency * fix typo --------- Co-authored-by: Sylvestre Ledru <sylvestre@debian.org> Co-authored-by: Chris Dryden <christopher.paul.dryden@gmail.com>
1 parent e4e95a7 commit 7b0e7e4

4 files changed

Lines changed: 26 additions & 12 deletions

File tree

src/uu/sync/locales/en-US.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sync-help-data = sync only file data, no unneeded metadata (Linux only)
77
88
# Error messages
99
sync-error-data-needs-argument = --data needs at least one argument
10-
sync-error-opening-file = error opening { $file }
10+
sync-error-opening-file = error opening { $file }: { $err }
1111
sync-error-no-such-file = error opening { $file }: No such file or directory
1212
sync-error-syncing-file = error syncing { $file }
1313

src/uu/sync/locales/fr-FR.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sync-help-data = synchroniser seulement les données des fichiers, pas les méta
77
88
# Messages d'erreur
99
sync-error-data-needs-argument = --data nécessite au moins un argument
10-
sync-error-opening-file = erreur lors de l'ouverture de { $file }
10+
sync-error-opening-file = erreur lors de l'ouverture de { $file } : { $err }
1111
sync-error-no-such-file = erreur lors de l'ouverture de { $file } : Aucun fichier ou répertoire de ce type
1212
sync-error-syncing-file = erreur lors de la synchronisation de { $file }
1313

src/uu/sync/src/sync.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ use nix::fcntl::{OFlag, open};
1414
use nix::sys::stat::Mode;
1515
use std::path::Path;
1616
use uucore::display::Quotable;
17-
#[cfg(any(target_os = "linux", target_os = "android"))]
18-
use uucore::error::FromIo;
19-
use uucore::error::{UResult, USimpleError};
17+
use uucore::error::{UResult, USimpleError, get_exit_code, set_exit_code};
2018
use uucore::format_usage;
19+
use uucore::show_error;
2120
use uucore::translate;
2221

2322
pub mod options {
@@ -235,23 +234,30 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
235234
let path = Path::new(&f);
236235
if let Err(e) = open(path, OFlag::O_NONBLOCK, Mode::empty()) {
237236
if e != Errno::EACCES || (e == Errno::EACCES && path.is_dir()) {
238-
e.map_err_context(
239-
|| translate!("sync-error-opening-file", "file" => f.quote()),
240-
)?;
237+
show_error!(
238+
"{}",
239+
translate!("sync-error-opening-file", "file" => f.quote(), "err" => e.desc())
240+
);
241+
set_exit_code(1);
241242
}
242243
}
243244
}
244245
#[cfg(not(any(target_os = "linux", target_os = "android")))]
245246
{
246247
if !Path::new(&f).exists() {
247-
return Err(USimpleError::new(
248-
1,
249-
translate!("sync-error-no-such-file", "file" => f.quote()),
250-
));
248+
show_error!(
249+
"{}",
250+
translate!("sync-error-no-such-file", "file" => f.quote())
251+
);
252+
set_exit_code(1);
251253
}
252254
}
253255
}
254256

257+
if get_exit_code() != 0 {
258+
return Err(USimpleError::new(1, ""));
259+
}
260+
255261
#[allow(clippy::if_same_then_else)]
256262
if matches.get_flag(options::FILE_SYSTEM) {
257263
#[cfg(any(target_os = "linux", target_os = "android", target_os = "windows"))]

tests/by-util/test_sync.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ fn test_sync_multiple_files() {
168168
new_ucmd!().arg("--data").arg(&file1).arg(&file2).succeeds();
169169
}
170170

171+
#[test]
172+
fn test_sync_multiple_nonexistent_files() {
173+
let result = new_ucmd!().arg("--data").arg("bad1").arg("bad2").fails();
174+
175+
result.stderr_contains("sync: error opening 'bad1': No such file or directory");
176+
result.stderr_contains("sync: error opening 'bad2': No such file or directory");
177+
}
178+
171179
#[cfg(any(target_os = "linux", target_os = "android"))]
172180
#[test]
173181
fn test_sync_data_fifo_fails_immediately() {

0 commit comments

Comments
 (0)