Skip to content

Commit 0a7b16c

Browse files
committed
open file with nonblock and add test
1 parent ba1afb0 commit 0a7b16c

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/sync/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ workspace = true
1818
path = "src/sync.rs"
1919

2020
[dependencies]
21+
libc = { workspace = true }
2122
clap = { workspace = true }
2223
uucore = { workspace = true, features = ["wide"] }
2324
fluent = { workspace = true }

src/uu/sync/src/sync.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ mod platform {
3535
#[cfg(any(target_os = "linux", target_os = "android"))]
3636
use nix::unistd::{fdatasync, syncfs};
3737
#[cfg(any(target_os = "linux", target_os = "android"))]
38-
use std::fs::File;
38+
use std::fs::{File, OpenOptions};
3939
#[cfg(any(target_os = "linux", target_os = "android"))]
40-
use uucore::error::FromIo;
40+
use std::os::unix::fs::OpenOptionsExt;
4141
#[cfg(any(target_os = "linux", target_os = "android"))]
4242
use uucore::translate;
4343

@@ -57,7 +57,10 @@ mod platform {
5757
/// Logs a warning if fcntl fails but doesn't abort the operation.
5858
#[cfg(any(target_os = "linux", target_os = "android"))]
5959
fn open_and_reset_nonblock(path: &str) -> UResult<File> {
60-
let f = File::open(path).map_err_context(|| path.to_string())?;
60+
let f = OpenOptions::new()
61+
.read(true)
62+
.custom_flags(libc::O_NONBLOCK)
63+
.open(path)?;
6164
// Reset O_NONBLOCK flag if it was set (matches GNU behavior)
6265
// This is non-critical, so we log errors but don't fail
6366
if let Err(e) = fcntl(&f, FcntlArg::F_SETFL(OFlag::empty())) {

tests/by-util/test_sync.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,21 @@ fn test_sync_multiple_files() {
167167
// Sync both files
168168
new_ucmd!().arg("--data").arg(&file1).arg(&file2).succeeds();
169169
}
170+
171+
#[cfg(any(target_os = "linux", target_os = "android"))]
172+
#[test]
173+
fn test_sync_data_fifo_fails_immediately() {
174+
use std::time::Duration;
175+
use uutests::util::TestScenario;
176+
use uutests::util_name;
177+
178+
let ts = TestScenario::new(util_name!());
179+
let at = &ts.fixtures;
180+
at.mkfifo("testfifo");
181+
182+
ts.ucmd()
183+
.arg("--data")
184+
.arg(at.plus_as_string("testfifo"))
185+
.timeout(Duration::from_secs(2))
186+
.fails();
187+
}

0 commit comments

Comments
 (0)