Skip to content

Commit

Permalink
fixup! dd: add support for seeking in output FIFOs
Browse files Browse the repository at this point in the history
  • Loading branch information
jfinkels committed Nov 20, 2022
1 parent b717aa0 commit 322cea0
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/uu/dd/src/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,21 +346,33 @@ impl Dest {
fn fsync(&mut self) -> io::Result<()> {
match self {
Self::Stdout(stdout) => stdout.flush(),
Self::File(f, _) | Self::Fifo(f) => {
Self::File(f, _) => {
f.flush()?;
f.sync_all()
}
#[cfg(any(target_os = "linux", target_os = "android"))]
Self::Fifo(f) => {
f.flush()?;
f.sync_all()
}
#[cfg(any(target_os = "linux", target_os = "android"))]
Self::Sink => Ok(()),
}
}

fn fdatasync(&mut self) -> io::Result<()> {
match self {
Self::Stdout(stdout) => stdout.flush(),
Self::File(f, _) | Self::Fifo(f) => {
Self::File(f, _) => {
f.flush()?;
f.sync_data()
}
#[cfg(any(target_os = "linux", target_os = "android"))]
Self::Fifo(f) => {
f.flush()?;
f.sync_data()
}
#[cfg(any(target_os = "linux", target_os = "android"))]
Self::Sink => Ok(()),
}
}
Expand All @@ -369,10 +381,12 @@ impl Dest {
match self {
Self::Stdout(stdout) => io::copy(&mut io::repeat(0).take(n), stdout),
Self::File(f, _) => f.seek(io::SeekFrom::Start(n)),
#[cfg(any(target_os = "linux", target_os = "android"))]
Self::Fifo(f) => {
// Seeking in a named pipe means *reading* from the pipe.
io::copy(&mut f.take(n), &mut io::sink())
}
#[cfg(any(target_os = "linux", target_os = "android"))]
Self::Sink => Ok(0),
}
}
Expand All @@ -394,16 +408,22 @@ impl Write for Dest {
f.seek(io::SeekFrom::Current(seek_amt))?;
Ok(buf.len())
}
Self::File(f, _) | Self::Fifo(f) => f.write(buf),
Self::File(f, _) => f.write(buf),
Self::Stdout(stdout) => stdout.write(buf),
#[cfg(any(target_os = "linux", target_os = "android"))]
Self::Fifo(f) => f.write(buf),
#[cfg(any(target_os = "linux", target_os = "android"))]
Self::Sink => Ok(buf.len()),
}
}

fn flush(&mut self) -> io::Result<()> {
match self {
Self::Stdout(stdout) => stdout.flush(),
Self::File(f, _) | Self::Fifo(f) => f.flush(),
Self::File(f, _) => f.flush(),
#[cfg(any(target_os = "linux", target_os = "android"))]
Self::Fifo(f) => f.flush(),
#[cfg(any(target_os = "linux", target_os = "android"))]
Self::Sink => Ok(()),
}
}
Expand Down

0 comments on commit 322cea0

Please sign in to comment.