Idiomatic way to generate a file of a given size

Hello all!

I am working on a pet project to make a random file, i.e. dd if=/dev/urandom in Rust. Since the file size is only known in runtime, I have been thinking about using array as buffer and count the number time the buffer has been filled and do some special processing on the last buffer. @upsuper suggested a mmap based approach. However, since memmap uses unsafe, I am not entirely comfortable to tango with it.

What is the idiomatic way to generate a file of a given size?

Writing random data into a buffer that periodically gets written to a file is probably going to be the fastest and easiest way to do this.

If you really want to squeeze the last bit of performance out of your machine you could use mmap. But it’s probably not worth the hassle unless you’re writing gigabytes at a time and have identified that as a bottleneck.

This is what I came up with:

extern crate rand;

use std::fs::File;
use std::io::{BufWriter, Write};
use std::cmp;
use rand::Rng;

fn main() {
    let size = get_size();

    let f = File::create("/tmp/whatever.txt").unwrap();
    let mut writer = BufWriter::new(f);
    
    let mut rng = rand::thread_rng();
    let mut buffer = [0; 1024];
    let mut remaining_size = size;
    
    while remaining_size > 0 {
        let to_write = cmp::min(remaining_size, buffer.len());
        let buffer=  &mut buffer[..to_write];
        rng.fill(buffer);
        writer.write(buffer).unwrap();
        
        remaining_size -= to_write;
    }
}

fn get_size() -> usize {
    42
}
2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.