Skip to content

Commit fd94d36

Browse files
authored
Merge pull request #7292 from jfinkels/csplit-dont-panic-no-file
csplit: don't panic on missing suppressed file
2 parents aa27efb + 18f9ca9 commit fd94d36

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/uu/csplit/src/csplit.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,12 @@ impl Drop for SplitWriter<'_> {
202202
fn drop(&mut self) {
203203
if self.options.elide_empty_files && self.size == 0 {
204204
let file_name = self.options.split_name.get(self.counter);
205-
remove_file(file_name).expect("Failed to elide split");
205+
// In the case of `echo a | csplit -z - %a%1`, the file
206+
// `xx00` does not exist because the positive offset
207+
// advanced past the end of the input. Since there is no
208+
// file to remove in that case, `remove_file` would return
209+
// an error, so we just ignore it.
210+
let _ = remove_file(file_name);
206211
}
207212
}
208213
}

tests/by-util/test_csplit.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@ fn test_skip_to_match_offset() {
335335
}
336336
}
337337

338+
#[test]
339+
fn test_skip_to_match_offset_suppress_empty() {
340+
let (at, mut ucmd) = at_and_ucmd!();
341+
ucmd.args(&["-z", "-", "%a%1"])
342+
.pipe_in("a\n")
343+
.succeeds()
344+
.no_output();
345+
assert!(!at.file_exists("xx00"));
346+
}
347+
338348
#[test]
339349
fn test_skip_to_match_negative_offset() {
340350
let (at, mut ucmd) = at_and_ucmd!();

0 commit comments

Comments
 (0)