start implementing ImageIndexReader
This commit is contained in:
		@ -2,10 +2,11 @@ use failure::*;
 | 
			
		||||
 | 
			
		||||
use super::chunk_store::*;
 | 
			
		||||
 | 
			
		||||
use std::io::Write;
 | 
			
		||||
use std::io::{Read, Write};
 | 
			
		||||
use std::path::{Path, PathBuf};
 | 
			
		||||
use std::os::unix::io::AsRawFd;
 | 
			
		||||
use uuid::Uuid;
 | 
			
		||||
use chrono::{Local, TimeZone};
 | 
			
		||||
 | 
			
		||||
#[repr(C)]
 | 
			
		||||
pub struct ImageIndexHeader {
 | 
			
		||||
@ -14,11 +15,100 @@ pub struct ImageIndexHeader {
 | 
			
		||||
    pub uuid: [u8; 16],
 | 
			
		||||
    pub ctime: u64,
 | 
			
		||||
    pub size: u64,
 | 
			
		||||
    reserved: [u8; 4048], // oversall size is one page (4096 bytes)
 | 
			
		||||
    pub chunk_size: u64,
 | 
			
		||||
    reserved: [u8; 4040], // oversall size is one page (4096 bytes)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// split image into fixed size chunks
 | 
			
		||||
 | 
			
		||||
pub struct ImageIndexReader<'a> {
 | 
			
		||||
    store: &'a mut ChunkStore,
 | 
			
		||||
    filename: PathBuf,
 | 
			
		||||
    chunk_size: usize,
 | 
			
		||||
    size: usize,
 | 
			
		||||
    index: *mut u8,
 | 
			
		||||
    uuid: [u8; 16],
 | 
			
		||||
    ctime: u64,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl <'a> Drop for ImageIndexReader<'a> {
 | 
			
		||||
 | 
			
		||||
    fn drop(&mut self) {
 | 
			
		||||
        if let Err(err) = self.unmap() {
 | 
			
		||||
            eprintln!("Unable to unmap file {:?} - {}", self.filename, err);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl <'a> ImageIndexReader<'a> {
 | 
			
		||||
 | 
			
		||||
    pub fn open(store: &'a mut ChunkStore, path: &Path) -> Result<Self, Error> {
 | 
			
		||||
 | 
			
		||||
        let full_path = store.relative_path(path);
 | 
			
		||||
 | 
			
		||||
        let mut file = std::fs::File::open(&full_path)?;
 | 
			
		||||
 | 
			
		||||
        let header_size = std::mem::size_of::<ImageIndexHeader>();
 | 
			
		||||
 | 
			
		||||
        // todo: use static assertion when available in rust
 | 
			
		||||
        if header_size != 4096 { panic!("got unexpected header size"); }
 | 
			
		||||
 | 
			
		||||
        let mut buffer = vec![0u8; header_size];
 | 
			
		||||
        file.read_exact(&mut buffer)?;
 | 
			
		||||
 | 
			
		||||
        let header = unsafe { &mut * (buffer.as_ptr() as *mut ImageIndexHeader) };
 | 
			
		||||
 | 
			
		||||
        let size = u64::from_be(header.size) as usize;
 | 
			
		||||
        let ctime = u64::from_be(header.ctime);
 | 
			
		||||
        let chunk_size = u64::from_be(header.chunk_size) as usize;
 | 
			
		||||
 | 
			
		||||
        let index_size = ((size + chunk_size - 1)/chunk_size)*32;
 | 
			
		||||
 | 
			
		||||
        // fixme: verify file size ?
 | 
			
		||||
 | 
			
		||||
        let data = unsafe { nix::sys::mman::mmap(
 | 
			
		||||
            std::ptr::null_mut(),
 | 
			
		||||
            index_size,
 | 
			
		||||
            nix::sys::mman::ProtFlags::PROT_READ,
 | 
			
		||||
            nix::sys::mman::MapFlags::MAP_PRIVATE,
 | 
			
		||||
            file.as_raw_fd(),
 | 
			
		||||
            header_size as i64) }? as *mut u8;
 | 
			
		||||
 | 
			
		||||
        Ok(Self {
 | 
			
		||||
            store,
 | 
			
		||||
            filename: full_path,
 | 
			
		||||
            chunk_size,
 | 
			
		||||
            size,
 | 
			
		||||
            index: data,
 | 
			
		||||
            ctime,
 | 
			
		||||
            uuid: header.uuid,
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn unmap(&mut self) -> Result<(), Error> {
 | 
			
		||||
 | 
			
		||||
        if self.index == std::ptr::null_mut() { return Ok(()); }
 | 
			
		||||
 | 
			
		||||
        let index_size = ((self.size + self.chunk_size - 1)/self.chunk_size)*32;
 | 
			
		||||
 | 
			
		||||
        if let Err(err) = unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) } {
 | 
			
		||||
            bail!("unmap file {:?} failed - {}", self.filename, err);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        self.index = std::ptr::null_mut();
 | 
			
		||||
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn print_info(&self) {
 | 
			
		||||
        println!("Filename: {:?}", self.filename);
 | 
			
		||||
        println!("Size: {}", self.size);
 | 
			
		||||
        println!("ChunkSize: {}", self.chunk_size);
 | 
			
		||||
        println!("CTime: {}", Local.timestamp(self.ctime as i64, 0).format("%c"));
 | 
			
		||||
        println!("UUID: {:?}", self.uuid);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct ImageIndexWriter<'a> {
 | 
			
		||||
    store: &'a mut ChunkStore,
 | 
			
		||||
    filename: PathBuf,
 | 
			
		||||
@ -54,7 +144,7 @@ impl <'a> ImageIndexWriter<'a> {
 | 
			
		||||
            .write(true)
 | 
			
		||||
            .open(&tmp_path)?;
 | 
			
		||||
 | 
			
		||||
        let chunk_size = 64*1024;
 | 
			
		||||
        let chunk_size = 64*1024; // fixed size for now??
 | 
			
		||||
 | 
			
		||||
        let header_size = std::mem::size_of::<ImageIndexHeader>();
 | 
			
		||||
 | 
			
		||||
@ -73,6 +163,7 @@ impl <'a> ImageIndexWriter<'a> {
 | 
			
		||||
        header.version = u32::to_be(1);
 | 
			
		||||
        header.ctime = u64::to_be(ctime);
 | 
			
		||||
        header.size = u64::to_be(size as u64);
 | 
			
		||||
        header.chunk_size = u64::to_be(chunk_size as u64);
 | 
			
		||||
        header.uuid = *uuid.as_bytes();
 | 
			
		||||
 | 
			
		||||
        file.write_all(&buffer)?;
 | 
			
		||||
 | 
			
		||||
@ -33,19 +33,26 @@ fn backup_file(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
 | 
			
		||||
 | 
			
		||||
    println!("Backup file '{}' to '{}'", filename, store);
 | 
			
		||||
 | 
			
		||||
    let file = std::fs::File::open(filename)?;
 | 
			
		||||
    let stat = nix::sys::stat::fstat(file.as_raw_fd())?;
 | 
			
		||||
    if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
 | 
			
		||||
    let size = stat.st_size as usize;
 | 
			
		||||
    let target = "test1.idx";
 | 
			
		||||
 | 
			
		||||
    let mut index = ImageIndexWriter::create(&mut chunk_store, "test1.idx".as_ref(), size)?;
 | 
			
		||||
    {
 | 
			
		||||
        let file = std::fs::File::open(filename)?;
 | 
			
		||||
        let stat = nix::sys::stat::fstat(file.as_raw_fd())?;
 | 
			
		||||
        if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
 | 
			
		||||
        let size = stat.st_size as usize;
 | 
			
		||||
 | 
			
		||||
    tools::file_chunker(file, 64*1024, |pos, chunk| {
 | 
			
		||||
        index.add_chunk(pos, chunk)?;
 | 
			
		||||
        Ok(true)
 | 
			
		||||
    })?;
 | 
			
		||||
        let mut index = ImageIndexWriter::create(&mut chunk_store, target.as_ref(), size)?;
 | 
			
		||||
 | 
			
		||||
    index.close()?; // commit changes
 | 
			
		||||
        tools::file_chunker(file, 64*1024, |pos, chunk| {
 | 
			
		||||
            index.add_chunk(pos, chunk)?;
 | 
			
		||||
            Ok(true)
 | 
			
		||||
        })?;
 | 
			
		||||
 | 
			
		||||
        index.close()?; // commit changes
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let idx = ImageIndexReader::open(&mut chunk_store, target.as_ref())?;
 | 
			
		||||
    idx.print_info();
 | 
			
		||||
 | 
			
		||||
    Ok(Value::Null)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user