|
900 | 900 | <span id="900">900</span>
|
901 | 901 | <span id="901">901</span>
|
902 | 902 | <span id="902">902</span>
|
| 903 | +<span id="903">903</span> |
| 904 | +<span id="904">904</span> |
| 905 | +<span id="905">905</span> |
| 906 | +<span id="906">906</span> |
| 907 | +<span id="907">907</span> |
| 908 | +<span id="908">908</span> |
| 909 | +<span id="909">909</span> |
| 910 | +<span id="910">910</span> |
| 911 | +<span id="911">911</span> |
| 912 | +<span id="912">912</span> |
| 913 | +<span id="913">913</span> |
| 914 | +<span id="914">914</span> |
| 915 | +<span id="915">915</span> |
| 916 | +<span id="916">916</span> |
| 917 | +<span id="917">917</span> |
| 918 | +<span id="918">918</span> |
| 919 | +<span id="919">919</span> |
| 920 | +<span id="920">920</span> |
| 921 | +<span id="921">921</span> |
| 922 | +<span id="922">922</span> |
| 923 | +<span id="923">923</span> |
| 924 | +<span id="924">924</span> |
903 | 925 | </pre><pre class="rust"><code><span class="comment">// This file is part of the uutils coreutils package.
|
904 | 926 | //
|
905 | 927 | // (c) Tyler Steele <tyler.steele@protonmail.com>
|
|
1226 | 1248 | <span class="self">Self</span>::File(f, <span class="kw">_</span>) => f.seek(io::SeekFrom::Start(n)),
|
1227 | 1249 | }
|
1228 | 1250 | }
|
| 1251 | + |
| 1252 | + <span class="doccomment">/// Truncate the underlying file to the current stream position, if possible. |
| 1253 | + </span><span class="kw">fn </span>truncate(<span class="kw-2">&mut </span><span class="self">self</span>) -> io::Result<()> { |
| 1254 | + <span class="kw">match </span><span class="self">self </span>{ |
| 1255 | + <span class="self">Self</span>::Stdout(<span class="kw">_</span>) => <span class="prelude-val">Ok</span>(()), |
| 1256 | + <span class="self">Self</span>::File(f, <span class="kw">_</span>) => { |
| 1257 | + <span class="kw">let </span>pos = f.stream_position()<span class="question-mark">?</span>; |
| 1258 | + f.set_len(pos) |
| 1259 | + } |
| 1260 | + } |
| 1261 | + } |
1229 | 1262 | }
|
1230 | 1263 |
|
1231 | 1264 | <span class="doccomment">/// Decide whether the given buffer is all zeros.
|
|
1308 | 1341 | // suppress the error by calling `Result::ok()`. This matches
|
1309 | 1342 | // the behavior of GNU `dd` when given the command-line
|
1310 | 1343 | // argument `of=/dev/null`.
|
1311 |
| - |
1312 | 1344 | </span><span class="kw">if </span>!settings.oconv.notrunc {
|
1313 | 1345 | dst.set_len(settings.seek).ok();
|
1314 | 1346 | }
|
|
1472 | 1504 | <span class="comment">// Flush the output, if configured to do so.
|
1473 | 1505 | </span><span class="self">self</span>.sync()<span class="question-mark">?</span>;
|
1474 | 1506 |
|
| 1507 | + <span class="comment">// Truncate the file to the final cursor location. |
| 1508 | + // |
| 1509 | + // Calling `set_len()` may result in an error (for example, |
| 1510 | + // when calling it on `/dev/null`), but we don't want to |
| 1511 | + // terminate the process when that happens. Instead, we |
| 1512 | + // suppress the error by calling `Result::ok()`. This matches |
| 1513 | + // the behavior of GNU `dd` when given the command-line |
| 1514 | + // argument `of=/dev/null`. |
| 1515 | + </span><span class="kw">if </span>!<span class="self">self</span>.settings.oconv.notrunc { |
| 1516 | + <span class="self">self</span>.dst.truncate().ok(); |
| 1517 | + } |
| 1518 | + |
1475 | 1519 | <span class="comment">// Print the final read/write statistics.
|
1476 | 1520 | </span><span class="kw">let </span>prog_update = ProgUpdate::new(rstat, wstat, start.elapsed(), <span class="bool-val">true</span>);
|
1477 | 1521 | prog_tx.send(prog_update).unwrap_or(());
|
|
0 commit comments