Skip to content

Commit

Permalink
unset O_DIRECT flag on last chunk if irregular sized
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Mar 17, 2024
1 parent e450ce8 commit b1986d0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/uu/dd/src/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// file that was distributed with this source code.

// spell-checker:ignore fname, ftype, tname, fpath, specfile, testfile, unspec, ifile, ofile, outfile, fullblock, urand, fileio, atoe, atoibm, behaviour, bmax, bremain, cflags, creat, ctable, ctty, datastructures, doesnt, etoa, fileout, fname, gnudd, iconvflags, iseek, nocache, noctty, noerror, nofollow, nolinks, nonblock, oconvflags, oseek, outfile, parseargs, rlen, rmax, rremain, rsofar, rstat, sigusr, wlen, wstat seekable oconv canonicalized fadvise Fadvise FADV DONTNEED ESPIPE bufferedoutput
// spell-checker:ignore GETFL SETFL

mod blocks;
mod bufferedoutput;
Expand Down Expand Up @@ -42,6 +43,8 @@ use std::time::{Duration, Instant};
use clap::{crate_version, Arg, Command};
use gcd::Gcd;
#[cfg(target_os = "linux")]
use nix::fcntl::{fcntl, FcntlArg, OFlag};
#[cfg(target_os = "linux")]
use nix::{
errno::Errno,
fcntl::{posix_fadvise, PosixFadviseAdvice},
Expand Down Expand Up @@ -509,6 +512,20 @@ enum Dest {
}

impl Dest {
fn unset_direct(&mut self) -> io::Result<()> {
match self {
#[cfg(target_os = "linux")]
Self::File(f, _d) => {
let mut mode = OFlag::from_bits_retain(fcntl(f.as_raw_fd(), FcntlArg::F_GETFL)?);
mode.remove(OFlag::O_DIRECT);
nix::fcntl::fcntl(f.as_raw_fd(), FcntlArg::F_SETFL(mode))?;
}
_ => {}
}

Ok(())
}

fn fsync(&mut self) -> io::Result<()> {
match self {
Self::Stdout(stdout) => stdout.flush(),
Expand Down Expand Up @@ -774,7 +791,14 @@ impl<'a> Output<'a> {
let mut writes_partial = 0;
let mut bytes_total = 0;

for chunk in buf.chunks(self.settings.obs) {
let chunk_size = self.settings.obs;
for chunk in buf.chunks(chunk_size) {
if (self.settings.oflags.direct) && (chunk.len() < chunk_size) {
// in case of direct io, only buffers with chunk_size are accepted.
// thus, for writing a (last) buffer with irregular length, we need to switch off the direct io.
self.dst.unset_direct()?;
}

let wlen = self.dst.write(chunk)?;
if wlen < self.settings.obs {
writes_partial += 1;
Expand Down
23 changes: 23 additions & 0 deletions tests/by-util/test_dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1713,3 +1713,26 @@ fn test_reading_partial_blocks_from_fifo_unbuffered() {
let expected = b"0+2 records in\n0+2 records out\n4 bytes copied";
assert!(output.stderr.starts_with(expected));
}

#[test]
fn test_reading_and_writing_with_direct_flag_from_and_to_files_with_irregular_size() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;

ts.ccmd("truncate").args(&["-s", "511", "short"]).succeeds();
ts.ccmd("truncate").args(&["-s", "8192", "in"]).succeeds();
ts.ccmd("truncate").args(&["-s", "8191", "m1"]).succeeds();
ts.ccmd("truncate").args(&["-s", "8193", "p1"]).succeeds();

ts.ucmd()
.args(&["if=in", "oflag=direct", "of=out"])
.succeeds();

for testfile in ["short", "m1", "p1"] {
at.remove("out");
ts.ucmd()
.arg(format!("if={testfile}"))
.args(&["iflag=direct", "oflag=direct", "of=out"])
.succeeds();
}
}

0 comments on commit b1986d0

Please sign in to comment.