Skip to content

Commit

Permalink
split: handling system limit on open files
Browse files Browse the repository at this point in the history
  • Loading branch information
zhitkoff committed Nov 24, 2023
1 parent 64d4773 commit 322edf9
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 79 deletions.
43 changes: 29 additions & 14 deletions src/uu/split/src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,37 @@ impl Drop for FilterWriter {
pub fn instantiate_current_writer(
filter: &Option<String>,
filename: &str,
is_new: bool,
) -> Result<BufWriter<Box<dyn Write>>> {
match filter {
None => Ok(BufWriter::new(Box::new(
// write to the next file
std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(std::path::Path::new(&filename))
.map_err(|_| {
Error::new(
ErrorKind::Other,
format!("unable to open '{filename}'; aborting"),
)
})?,
) as Box<dyn Write>)),
None => {
let file = if is_new {
// create new file
std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(std::path::Path::new(&filename))
.map_err(|_| {
Error::new(
ErrorKind::Other,
format!("unable to open '{filename}'; aborting"),
)
})?
} else {
// re-open file that we previously created to append to it
std::fs::OpenOptions::new()
.append(true)
.open(std::path::Path::new(&filename))
.map_err(|_| {
Error::new(
ErrorKind::Other,
format!("unable to re-open '{filename}'; aborting"),
)
})?
};
Ok(BufWriter::new(Box::new(file) as Box<dyn Write>))
}
Some(ref filter_command) => Ok(BufWriter::new(Box::new(
// spawn a shell command and write to it
FilterWriter::new(filter_command, filename)?,
Expand Down
23 changes: 18 additions & 5 deletions src/uu/split/src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ use uucore::fs;
pub fn instantiate_current_writer(
_filter: &Option<String>,
filename: &str,
is_new: bool,
) -> Result<BufWriter<Box<dyn Write>>> {
Ok(BufWriter::new(Box::new(
// write to the next file
let file = if is_new {
// create new file
std::fs::OpenOptions::new()
.write(true)
.create(true)
Expand All @@ -25,10 +26,22 @@ pub fn instantiate_current_writer(
.map_err(|_| {
Error::new(
ErrorKind::Other,
format!("'{filename}' would overwrite input; aborting"),
format!("unable to open '{filename}'; aborting"),
)
})?,
) as Box<dyn Write>))
})?
} else {
// re-open file that we previously created to append to it
std::fs::OpenOptions::new()
.append(true)
.open(std::path::Path::new(&filename))
.map_err(|_| {
Error::new(
ErrorKind::Other,
format!("unable to re-open '{filename}'; aborting"),
)
})?
};
Ok(BufWriter::new(Box::new(file) as Box<dyn Write>))
}

pub fn paths_refer_to_same_file(p1: &str, p2: &str) -> bool {
Expand Down
Loading

0 comments on commit 322edf9

Please sign in to comment.