Skip to content

Commit 9ccd94f

Browse files
committed
feat(sync): remove unwrap and align with GNU behavior
1 parent 2a314c7 commit 9ccd94f

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/uu/sync/src/sync.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,18 @@ static ARG_FILES: &str = "files";
2929

3030
#[cfg(unix)]
3131
mod platform {
32+
#[cfg(any(target_os = "linux", target_os = "android"))]
33+
use nix::fcntl::{fcntl, FcntlArg, OFlag};
3234
use nix::unistd::sync;
3335
#[cfg(any(target_os = "linux", target_os = "android"))]
3436
use nix::unistd::{fdatasync, syncfs};
3537
#[cfg(any(target_os = "linux", target_os = "android"))]
3638
use std::fs::File;
39+
#[cfg(any(target_os = "linux", target_os = "android"))]
40+
use std::os::unix::io::AsRawFd;
41+
#[cfg(any(target_os = "linux", target_os = "android"))]
42+
use uucore::error::FromIo;
43+
3744
use uucore::error::UResult;
3845

3946
pub fn do_sync() -> UResult<()> {
@@ -44,7 +51,10 @@ mod platform {
4451
#[cfg(any(target_os = "linux", target_os = "android"))]
4552
pub fn do_syncfs(files: Vec<String>) -> UResult<()> {
4653
for path in files {
47-
let f = File::open(path).unwrap();
54+
let f = File::open(&path).map_err_context(|| path.clone())?;
55+
// Reset O_NONBLOCK flag if it was set (matches GNU behavior)
56+
let fd = f.as_raw_fd();
57+
let _ = fcntl(fd, FcntlArg::F_SETFL(OFlag::empty()));
4858
syncfs(f)?;
4959
}
5060
Ok(())
@@ -53,7 +63,10 @@ mod platform {
5363
#[cfg(any(target_os = "linux", target_os = "android"))]
5464
pub fn do_fdatasync(files: Vec<String>) -> UResult<()> {
5565
for path in files {
56-
let f = File::open(path).unwrap();
66+
let f = File::open(&path).map_err_context(|| path.clone())?;
67+
// Reset O_NONBLOCK flag if it was set (matches GNU behavior)
68+
let fd = f.as_raw_fd();
69+
let _ = fcntl(fd, FcntlArg::F_SETFL(OFlag::empty()));
5770
fdatasync(f)?;
5871
}
5972
Ok(())
@@ -157,15 +170,17 @@ mod platform {
157170

158171
pub fn do_syncfs(files: Vec<String>) -> UResult<()> {
159172
for path in files {
160-
flush_volume(
161-
Path::new(&path)
162-
.components()
163-
.next()
164-
.unwrap()
165-
.as_os_str()
166-
.to_str()
167-
.unwrap(),
168-
)?;
173+
let maybe_first = Path::new(&path).components().next();
174+
let vol_name = match maybe_first {
175+
Some(c) => c.as_os_str().to_string_lossy().into_owned(),
176+
None => {
177+
return Err(USimpleError::new(
178+
1,
179+
translate!("sync-error-no-such-file", "file" => path),
180+
));
181+
}
182+
};
183+
flush_volume(&vol_name)?;
169184
}
170185
Ok(())
171186
}

0 commit comments

Comments
 (0)