Skip to content

Instantly share code, notes, and snippets.

@hugefiver
Last active September 1, 2021 07:34
Show Gist options
  • Save hugefiver/504f63f39ef0fd6d0e87e3a972891705 to your computer and use it in GitHub Desktop.
Save hugefiver/504f63f39ef0fd6d0e87e3a972891705 to your computer and use it in GitHub Desktop.
use std::{env::args, fs::{self, remove_file}, io::Write};
fn main() {
let argv = args();
let f: Vec<String> = argv.collect();
let (path, size_str, turn_str) = (&f[1].clone(), f[2].clone(), f[3].clone());
let turn = if let Ok(turn) = turn_str.parse::<u64>(){
turn
} else {
1
};
let (num, ll) = size_str.split_at(size_str.len()-1);
let get_num = |x: &str| {x.parse::<f64>().unwrap_or(0.0)};
let the_num = ||{get_num(num)};
let float_lshift = |x: f64, y:u8| {
let b = x.floor();
((b as u64) << y) + ((x-b) * ((1<<y) as f64)) as u64
};
let size: u64 = match ll {
"k" | "K" => float_lshift(the_num(), 10),
"M" | "m" => float_lshift(the_num(), 20),
"g" | "G" => float_lshift(the_num(), 30),
_ => get_num(num).floor() as u64,
};
let buf = if size <= 16<<20 {
&[0;16<<10][..]
} else {
&[0;16<<20][..]
};
for i in 1..=turn {
println!("Turn: {}", i);
let mut file = fs::File::create(path).unwrap();
let mut written: u64 = 0;
while written < size {
file.write_all(buf).expect("write error");
written += buf.len() as u64;
}
if remove_file(path).is_err() {
println!("remove failed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment