|
4 | 4 | // file that was distributed with this source code.
|
5 | 5 |
|
6 | 6 | // 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
|
| 7 | +// spell-checker:ignore GETFL SETFL |
7 | 8 |
|
8 | 9 | mod blocks;
|
9 | 10 | mod bufferedoutput;
|
@@ -41,6 +42,8 @@ use std::time::{Duration, Instant};
|
41 | 42 |
|
42 | 43 | use clap::{crate_version, Arg, Command};
|
43 | 44 | use gcd::Gcd;
|
| 45 | +#[cfg(any(target_os = "android", target_os = "linux"))] |
| 46 | +use nix::fcntl::{fcntl, FcntlArg, OFlag}; |
44 | 47 | #[cfg(target_os = "linux")]
|
45 | 48 | use nix::{
|
46 | 49 | errno::Errno,
|
@@ -509,6 +512,20 @@ enum Dest {
|
509 | 512 | }
|
510 | 513 |
|
511 | 514 | impl Dest {
|
| 515 | + fn unset_direct(&mut self) -> io::Result<()> { |
| 516 | + match self { |
| 517 | + #[cfg(any(target_os = "linux", target_os = "android"))] |
| 518 | + Self::File(f, _d) => { |
| 519 | + let mut mode = OFlag::from_bits_retain(fcntl(f.as_raw_fd(), FcntlArg::F_GETFL)?); |
| 520 | + mode.remove(OFlag::O_DIRECT); |
| 521 | + nix::fcntl::fcntl(f.as_raw_fd(), FcntlArg::F_SETFL(mode))?; |
| 522 | + } |
| 523 | + _ => {} |
| 524 | + } |
| 525 | + |
| 526 | + Ok(()) |
| 527 | + } |
| 528 | + |
512 | 529 | fn fsync(&mut self) -> io::Result<()> {
|
513 | 530 | match self {
|
514 | 531 | Self::Stdout(stdout) => stdout.flush(),
|
@@ -774,7 +791,14 @@ impl<'a> Output<'a> {
|
774 | 791 | let mut writes_partial = 0;
|
775 | 792 | let mut bytes_total = 0;
|
776 | 793 |
|
777 |
| - for chunk in buf.chunks(self.settings.obs) { |
| 794 | + let chunk_size = self.settings.obs; |
| 795 | + for chunk in buf.chunks(chunk_size) { |
| 796 | + if (self.settings.oflags.direct) && (chunk.len() < chunk_size) { |
| 797 | + // in case of direct io, only buffers with chunk_size are accepted. |
| 798 | + // thus, for writing a (last) buffer with irregular length, we need to switch off the direct io. |
| 799 | + self.dst.unset_direct()?; |
| 800 | + } |
| 801 | + |
778 | 802 | let wlen = self.dst.write(chunk)?;
|
779 | 803 | if wlen < self.settings.obs {
|
780 | 804 | writes_partial += 1;
|
@@ -877,7 +901,7 @@ impl<'a> BlockWriter<'a> {
|
877 | 901 | ///
|
878 | 902 | /// If there is a problem reading from the input or writing to
|
879 | 903 | /// this output.
|
880 |
| -fn dd_copy(mut i: Input, o: Output) -> std::io::Result<()> { |
| 904 | +fn dd_copy(mut i: Input, o: Output) -> UResult<()> { |
881 | 905 | // The read and write statistics.
|
882 | 906 | //
|
883 | 907 | // These objects are counters, initialized to zero. After each
|
@@ -992,11 +1016,18 @@ fn dd_copy(mut i: Input, o: Output) -> std::io::Result<()> {
|
992 | 1016 | // best buffer size for reading based on the number of
|
993 | 1017 | // blocks already read and the number of blocks remaining.
|
994 | 1018 | let loop_bsize = calc_loop_bsize(&i.settings.count, &rstat, &wstat, i.settings.ibs, bsize);
|
995 |
| - let rstat_update = read_helper(&mut i, &mut buf, loop_bsize)?; |
| 1019 | + let rstat_update = read_helper(&mut i, &mut buf, loop_bsize) |
| 1020 | + .map_err_context(|| format!("reading, ls: {loop_bsize}, rbt: {}", rstat.bytes_total))?; |
996 | 1021 | if rstat_update.is_empty() {
|
997 | 1022 | break;
|
998 | 1023 | }
|
999 |
| - let wstat_update = o.write_blocks(&buf)?; |
| 1024 | + let wstat_update = o.write_blocks(&buf).map_err_context(|| { |
| 1025 | + format!( |
| 1026 | + "writing, ls: {}/{loop_bsize}, wbt: {}", |
| 1027 | + buf.len(), |
| 1028 | + wstat.bytes_total |
| 1029 | + ) |
| 1030 | + })?; |
1000 | 1031 |
|
1001 | 1032 | // Discard the system file cache for the read portion of
|
1002 | 1033 | // the input file.
|
@@ -1047,7 +1078,7 @@ fn finalize<T>(
|
1047 | 1078 | prog_tx: &mpsc::Sender<ProgUpdate>,
|
1048 | 1079 | output_thread: thread::JoinHandle<T>,
|
1049 | 1080 | truncate: bool,
|
1050 |
| -) -> std::io::Result<()> { |
| 1081 | +) -> UResult<()> { |
1051 | 1082 | // Flush the output in case a partial write has been buffered but
|
1052 | 1083 | // not yet written.
|
1053 | 1084 | let wstat_update = output.flush()?;
|
@@ -1292,7 +1323,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
1292 | 1323 | }
|
1293 | 1324 | None => Output::new_stdout(&settings)?,
|
1294 | 1325 | };
|
1295 |
| - dd_copy(i, o).map_err_context(|| "IO error".to_string()) |
| 1326 | + dd_copy(i, o) |
1296 | 1327 | }
|
1297 | 1328 |
|
1298 | 1329 | pub fn uu_app() -> Command {
|
|
0 commit comments