You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB

use super::*;
use std::io::{self, Read};
fn read_into(mut job: impl Read, mut output: impl AsMut<[u8]>) -> io::Result<usize>
{
let output = output.as_mut();
let mut read=0;
while read < output.len() {
match job.read(&mut output[read..])? {
0 => break,
current => read+=current,
}
}
Ok(read)
}
pub fn work(arg::Operation{output, inputs}: arg::Operation) -> Result<(), Box<dyn std::error::Error>>
{
// - create and open output file
let output = open::output(output)?;
// - open and stat all input files in order (`job::create_from_file`).
let inputs = open::input(inputs)?;
if inputs.len() == 0 {
output.into_inner().set_len(0)?; // Just truncate the output and return
return Ok(());
}
let msz = usize::try_from(inputs.iter().map(|x| x.1.len()).sum::<u64>()).expect("Size too large");
let (mut file, _fsz) = {
let mut output = output.complete(msz)?;
let mut off=0;
for open::Input(file, stat) in inputs
{
let sz = usize::try_from(stat.len()).expect("Size too large");
let next = read_into(file, &mut output.as_slice_mut()[off.. (off+sz)])?;
assert_eq!(sz, next, "Failed to read whole file");
println!("{}", next);
off += next;
}
(output.unmap(), off)
};
debug_assert_eq!(_fsz, msz);
{
use std::io::Write;
file.set_len(msz as u64)?;
file.flush()?;
}
Ok(())
}