|
|
|
@ -1,8 +1,40 @@
|
|
|
|
|
use super::*;
|
|
|
|
|
use std::fs::{
|
|
|
|
|
File, Metadata,
|
|
|
|
|
File, Metadata, OpenOptions,
|
|
|
|
|
};
|
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use std::io::{
|
|
|
|
|
self,
|
|
|
|
|
Read,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Prelude
|
|
|
|
|
{
|
|
|
|
|
file: File,
|
|
|
|
|
stat: Metadata,
|
|
|
|
|
offset: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Prelude{
|
|
|
|
|
pub fn len(&self) -> usize
|
|
|
|
|
{
|
|
|
|
|
usize::try_from(self.stat.len()).expect("Failed to fit file size into pointer size")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convert this job prelude into a job, assigning it this state
|
|
|
|
|
pub fn start(self, state: state::State) -> Job
|
|
|
|
|
{
|
|
|
|
|
Job {
|
|
|
|
|
fd: self.file,
|
|
|
|
|
stat: self.stat,
|
|
|
|
|
offset: self.offset,
|
|
|
|
|
state,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Job
|
|
|
|
|
{
|
|
|
|
@ -16,3 +48,68 @@ pub struct Job
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: job's work :^)
|
|
|
|
|
|
|
|
|
|
impl Job
|
|
|
|
|
{
|
|
|
|
|
pub fn state(&self) -> &state::State
|
|
|
|
|
{
|
|
|
|
|
&self.state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn info(&self) -> (&File, &Metadata)
|
|
|
|
|
{
|
|
|
|
|
(&self.fd, &self.stat)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize
|
|
|
|
|
{
|
|
|
|
|
usize::try_from(self.stat.len()).expect("Failed to fit file size into pointer size")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn start(&self) -> usize
|
|
|
|
|
{
|
|
|
|
|
self.offset
|
|
|
|
|
}
|
|
|
|
|
pub fn end(&self) -> usize
|
|
|
|
|
{
|
|
|
|
|
self.len() + self.offset
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the output slice for this job.
|
|
|
|
|
pub fn output_slice<'a, 'b>(&'a mut self) -> &'b mut [u8]
|
|
|
|
|
where 'a: 'b
|
|
|
|
|
{
|
|
|
|
|
unsafe {
|
|
|
|
|
self.state.slice(self.start() .. self.end())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Read for Job
|
|
|
|
|
{
|
|
|
|
|
#[inline] fn read(&mut self, buf: &mut [u8]) -> io::Result<usize>
|
|
|
|
|
{
|
|
|
|
|
self.fd.read(buf)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a job description for this file.
|
|
|
|
|
///
|
|
|
|
|
/// `sz` is the offset of the *end* of the last job. (or 0 for the first).
|
|
|
|
|
/// `sz` is then updated with this file's size for this method to be used again on the next file.
|
|
|
|
|
pub fn create_from_file(file: impl AsRef<Path>, sz: &mut usize) -> io::Result<Prelude>
|
|
|
|
|
{
|
|
|
|
|
let file = OpenOptions::new()
|
|
|
|
|
.read(true)
|
|
|
|
|
.open(file.as_ref())?;
|
|
|
|
|
|
|
|
|
|
let stat = file.metadata()?;
|
|
|
|
|
let offset = *sz;
|
|
|
|
|
|
|
|
|
|
let prelude = Prelude {
|
|
|
|
|
file, stat, offset,
|
|
|
|
|
};
|
|
|
|
|
*sz += prelude.len();
|
|
|
|
|
|
|
|
|
|
Ok(prelude)
|
|
|
|
|
}
|
|
|
|
|