'What doesn't my thread reach these lines?
I am new to Rust and I am trying to write a simple program that encrypts files with AES once the user provides a Path and a password. The code creates as many threads as the number of virtual CPUs, and encrypts every file in a particular path in a new file with the same name + .encryption at the end.
But for some peculiar reason I haven't yet understood, the last lines of the program are never reached even though there are no errors or warnings. The code looks like this:
fn encrypt_file(
filepath: &str,
pass: &str,
) -> Result<(), io::Error> {
println!("The filepath: {}", filepath);
let text_vec = String::from_utf8_lossy(&fs::read(filepath).unwrap()).as_bytes().to_vec();
println!("File read complete:");
let mut encryptionPassword: String;
if pass.len() < 32 {
let length = 32 - pass.len();
let strr = (0..length).map(|_| "0").collect::<String>().to_owned();
encryptionPassword = format!("{}{}", pass, strr);
} else if pass.len() > 32 {
encryptionPassword = String::from(&pass[..32]);
} else {
encryptionPassword = String::from(pass);
}
println!("encryptionPassword: {:?}",&encryptionPassword);
let ciphertext = enc_file::encrypt_aes(text_vec, &encryptionPassword.as_str()).unwrap();
let enc = ".encrypted";
let dist = format!("{}{}", filepath, enc);
fs::write(&dist, &ciphertext)?;
println!("wrote");
let mut buffer = String::new();
File::open(&dist).unwrap().read_to_string(&mut buffer)?;
println!("file opened");
let decrypted = enc_file::decrypt_aes(buffer.into_bytes().to_vec(), encryptionPassword.as_str());
println!("Decrypted: {:?}",decrypted );
Ok(())
}
fn main() {
let matches = Command::new("Multithreaded encryptor")
.version("0.1.0")
.arg(Arg::new("password")
.short("p".parse().unwrap())
.long("password")
.takes_value(true)
.help("Encryption password"))
.arg(Arg::new("path")
.short("D".parse().unwrap())
.long("path") //Double quotes needed!
.takes_value(true)
.help("Path to files"))
.get_matches();
let pass = matches.value_of("password").unwrap_or_else(
|| { "null" }
);
let path = matches.value_of("path").unwrap_or_else(
|| { process::exit(1); }
);
println!("The password: {}", pass);
println!("The path: {}", path);
let thread_pool = ThreadPool::new(num_cpus::get()); //As many threads as virtual CPUs.
for entry in WalkDir::new(path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| !e.path().is_dir())
{
let filepath = entry.path().to_owned();
let a3 = pass.to_owned();
thread_pool.execute(move || {
encrypt_file(filepath.to_str().unwrap(), a3.as_str()).unwrap();
});
}
}
The " println!("file opened");" line and everything below, hasn't been reached a single time so far, yet there are no errors or warnings. The .encrypted file is successfully created and includes encrypted data, but nothing below it seems to be executed.
There also seems to be a degree of randomness, since sometimes the encrypted file contains no data, but this could be a peculiarity of the underlying file-system...
Solution 1:[1]
In Rust, the program will terminate if the main() function finishes, regardless of whether any other threads are alive.
You need to wait for the outstanding jobs submitted to the pool to finish as the last step in main(). You can do this with ThreadPool::join():
thread_pool.join();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cdhowie |
