Skip to content

Commit

Permalink
refactor: remove create_dir_all_threadsafe helper
Browse files Browse the repository at this point in the history
as std::fs::create_dir_all is now threadsafe

rust-lang/rust#39799
  • Loading branch information
jqnatividad committed Nov 2, 2024
1 parent 1a3a422 commit d0af83b
Showing 1 changed file with 2 additions and 26 deletions.
28 changes: 2 additions & 26 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::os::unix::process::ExitStatusExt;
use std::{
cmp::min,
env, fs,
fs::File,
fs::{create_dir_all, File},
io::{BufRead, BufReader, BufWriter, Read, Write},
path::{Path, PathBuf},
str,
Expand Down Expand Up @@ -724,30 +724,6 @@ pub fn range(start: Idx, end: Idx, len: Idx, index: Idx) -> Result<(usize, usize
}
}

/// Create a directory recursively, avoiding the race conditions fixed by
/// https://github.com/rust-lang/rust/pull/39799.
#[cfg(any(feature = "feature_capable", feature = "lite"))]
fn create_dir_all_threadsafe(path: &Path) -> std::io::Result<()> {
use std::thread;

// Try 20 times. This shouldn't theoretically need to be any larger
// than the number of nested directories we need to create.
for _ in 0..20 {
match fs::create_dir_all(path) {
// This happens if a directory in `path` doesn't exist when we
// test for it, and another thread creates it before we can.
Err(ref err) if err.kind() == std::io::ErrorKind::AlreadyExists => {},
other => return other,
}
// We probably don't need to sleep at all, because the intermediate
// directory is already created. But let's attempt to back off a
// bit and let the other thread finish.
thread::sleep(std::time::Duration::from_millis(25));
}
// Try one last time, returning whatever happens.
fs::create_dir_all(path)
}

/// Represents a filename template of the form `"{}.csv"`, where `"{}"` is
/// the place to insert the part of the filename generated by `qsv`.
#[cfg(any(feature = "feature_capable", feature = "lite"))]
Expand Down Expand Up @@ -783,7 +759,7 @@ impl FilenameTemplate {
// We may be called concurrently, especially by parallel `qsv
// split`, so be careful to avoid the `create_dir_all` race
// condition.
create_dir_all_threadsafe(parent)?;
create_dir_all(parent)?;
}
let spath = Some(full_path.display().to_string());
Config::new(spath.as_ref()).writer()
Expand Down

0 comments on commit d0af83b

Please sign in to comment.