2018-12-31 16:30:08 +00:00
|
|
|
use failure::*;
|
|
|
|
|
2019-01-25 09:58:28 +00:00
|
|
|
use crate::tools;
|
2019-02-27 13:32:34 +00:00
|
|
|
use super::IndexFile;
|
2019-02-25 11:52:10 +00:00
|
|
|
use super::chunk_stat::*;
|
2018-12-31 16:30:08 +00:00
|
|
|
use super::chunk_store::*;
|
|
|
|
use super::chunker::*;
|
|
|
|
|
2019-01-15 10:38:26 +00:00
|
|
|
use std::sync::Arc;
|
2019-01-02 11:54:40 +00:00
|
|
|
use std::io::{Read, Write, BufWriter};
|
2018-12-31 16:30:08 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
use uuid::Uuid;
|
2019-01-11 07:41:33 +00:00
|
|
|
//use chrono::{Local, TimeZone};
|
2018-12-31 16:30:08 +00:00
|
|
|
|
2019-02-12 13:13:31 +00:00
|
|
|
/// Header format definition for dynamic index files (`.dixd`)
|
2018-12-31 16:30:08 +00:00
|
|
|
#[repr(C)]
|
2019-02-12 11:05:33 +00:00
|
|
|
pub struct DynamicIndexHeader {
|
2019-02-12 13:13:31 +00:00
|
|
|
/// The string `PROXMOX-DIDX`
|
2018-12-31 16:30:08 +00:00
|
|
|
pub magic: [u8; 12],
|
|
|
|
pub version: u32,
|
|
|
|
pub uuid: [u8; 16],
|
|
|
|
pub ctime: u64,
|
2019-01-02 11:56:04 +00:00
|
|
|
reserved: [u8; 4056], // overall size is one page (4096 bytes)
|
2018-12-31 16:30:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 13:27:04 +00:00
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
pub struct DynamicIndexReader {
|
2019-01-15 11:36:16 +00:00
|
|
|
store: Arc<ChunkStore>,
|
2019-01-18 15:50:15 +00:00
|
|
|
_file: File,
|
2019-01-30 17:25:37 +00:00
|
|
|
pub size: usize,
|
2019-01-02 13:27:04 +00:00
|
|
|
filename: PathBuf,
|
|
|
|
index: *const u8,
|
|
|
|
index_entries: usize,
|
2019-01-30 17:25:37 +00:00
|
|
|
pub uuid: [u8; 16],
|
|
|
|
pub ctime: u64,
|
2019-01-02 13:27:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:36:19 +00:00
|
|
|
// `index` is mmap()ed which cannot be thread-local so should be sendable
|
|
|
|
// FIXME: Introduce an mmap wrapper type for this?
|
2019-02-12 11:05:33 +00:00
|
|
|
unsafe impl Send for DynamicIndexReader {}
|
2019-01-20 08:39:32 +00:00
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
impl Drop for DynamicIndexReader {
|
2019-01-02 13:27:04 +00:00
|
|
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Err(err) = self.unmap() {
|
|
|
|
eprintln!("Unable to unmap file {:?} - {}", self.filename, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
impl DynamicIndexReader {
|
2019-01-02 13:27:04 +00:00
|
|
|
|
2019-01-15 11:36:16 +00:00
|
|
|
pub fn open(store: Arc<ChunkStore>, path: &Path) -> Result<Self, Error> {
|
2019-01-02 13:27:04 +00:00
|
|
|
|
|
|
|
let full_path = store.relative_path(path);
|
|
|
|
|
|
|
|
let mut file = std::fs::File::open(&full_path)?;
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
let header_size = std::mem::size_of::<DynamicIndexHeader>();
|
2019-01-02 13:27:04 +00:00
|
|
|
|
|
|
|
// todo: use static assertion when available in rust
|
|
|
|
if header_size != 4096 { bail!("got unexpected header size for {:?}", path); }
|
|
|
|
|
|
|
|
let mut buffer = vec![0u8; header_size];
|
|
|
|
file.read_exact(&mut buffer)?;
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
let header = unsafe { &mut * (buffer.as_ptr() as *mut DynamicIndexHeader) };
|
2019-01-02 13:27:04 +00:00
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
if header.magic != *b"PROXMOX-DIDX" {
|
2019-01-02 13:27:04 +00:00
|
|
|
bail!("got unknown magic number for {:?}", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
let version = u32::from_le(header.version);
|
|
|
|
if version != 1 {
|
|
|
|
bail!("got unsupported version number ({}) for {:?}", version, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
let ctime = u64::from_le(header.ctime);
|
|
|
|
|
|
|
|
let rawfd = file.as_raw_fd();
|
|
|
|
|
|
|
|
let stat = match nix::sys::stat::fstat(rawfd) {
|
|
|
|
Ok(stat) => stat,
|
|
|
|
Err(err) => bail!("fstat {:?} failed - {}", path, err),
|
|
|
|
};
|
|
|
|
|
|
|
|
let size = stat.st_size as usize;
|
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
let index_size = size - header_size;
|
2019-01-02 13:27:04 +00:00
|
|
|
if (index_size % 40) != 0 {
|
|
|
|
bail!("got unexpected file size for {:?}", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
rawfd,
|
|
|
|
header_size as i64) }? as *const u8;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
store,
|
|
|
|
filename: full_path,
|
2019-01-18 15:50:15 +00:00
|
|
|
_file: file,
|
2019-01-02 13:27:04 +00:00
|
|
|
size,
|
|
|
|
index: data,
|
|
|
|
index_entries: index_size/40,
|
|
|
|
ctime,
|
|
|
|
uuid: header.uuid,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unmap(&mut self) -> Result<(), Error> {
|
|
|
|
|
|
|
|
if self.index == std::ptr::null_mut() { return Ok(()); }
|
|
|
|
|
2019-01-04 08:28:41 +00:00
|
|
|
if let Err(err) = unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, self.index_entries*40) } {
|
2019-01-02 13:27:04 +00:00
|
|
|
bail!("unmap file {:?} failed - {}", self.filename, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.index = std::ptr::null_mut();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-05 13:47:56 +00:00
|
|
|
#[inline]
|
|
|
|
fn chunk_end(&self, pos: usize) -> u64 {
|
|
|
|
if pos >= self.index_entries {
|
|
|
|
panic!("chunk index out of range");
|
|
|
|
}
|
|
|
|
unsafe { *(self.index.add(pos*40) as *const u64) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn chunk_digest(&self, pos: usize) -> &[u8] {
|
|
|
|
if pos >= self.index_entries {
|
|
|
|
panic!("chunk index out of range");
|
|
|
|
}
|
|
|
|
unsafe { std::slice::from_raw_parts(self.index.add(pos*40+8), 32) }
|
|
|
|
}
|
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
pub fn mark_used_chunks(&self, _status: &mut GarbageCollectionStatus) -> Result<(), Error> {
|
2019-01-02 13:27:04 +00:00
|
|
|
|
|
|
|
for pos in 0..self.index_entries {
|
2019-01-05 13:47:56 +00:00
|
|
|
let digest = self.chunk_digest(pos);
|
2019-01-02 13:27:04 +00:00
|
|
|
if let Err(err) = self.store.touch_chunk(digest) {
|
|
|
|
bail!("unable to access chunk {}, required by {:?} - {}",
|
2019-01-25 09:58:28 +00:00
|
|
|
tools::digest_to_hex(digest), self.filename, err);
|
2019-01-02 13:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-01-04 11:50:54 +00:00
|
|
|
|
|
|
|
pub fn dump_catar(&self, mut writer: Box<Write>) -> Result<(), Error> {
|
|
|
|
|
2019-01-04 16:16:56 +00:00
|
|
|
let mut buffer = Vec::with_capacity(1024*1024);
|
|
|
|
|
2019-01-04 11:50:54 +00:00
|
|
|
for pos in 0..self.index_entries {
|
2019-01-11 07:41:33 +00:00
|
|
|
let _end = self.chunk_end(pos);
|
2019-01-05 13:47:56 +00:00
|
|
|
let digest = self.chunk_digest(pos);
|
|
|
|
//println!("Dump {:08x}", end );
|
2019-01-04 16:16:56 +00:00
|
|
|
self.store.read_chunk(digest, &mut buffer)?;
|
|
|
|
writer.write_all(&buffer)?;
|
2019-01-04 11:50:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-01-05 13:47:56 +00:00
|
|
|
|
|
|
|
fn binary_search(
|
|
|
|
&self,
|
|
|
|
start_idx: usize,
|
|
|
|
start: u64,
|
|
|
|
end_idx: usize,
|
|
|
|
end: u64,
|
|
|
|
offset: u64
|
|
|
|
) -> Result<usize, Error> {
|
|
|
|
|
|
|
|
if (offset >= end) || (offset < start) {
|
|
|
|
bail!("offset out of range");
|
|
|
|
}
|
|
|
|
|
|
|
|
if end_idx == start_idx {
|
|
|
|
return Ok(start_idx); // found
|
|
|
|
}
|
|
|
|
let middle_idx = (start_idx + end_idx)/2;
|
|
|
|
let middle_end = self.chunk_end(middle_idx);
|
|
|
|
|
|
|
|
if offset < middle_end {
|
|
|
|
return self.binary_search(start_idx, start, middle_idx, middle_end, offset);
|
|
|
|
} else {
|
|
|
|
return self.binary_search(middle_idx + 1, middle_end, end_idx, end, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 13:32:34 +00:00
|
|
|
impl IndexFile for DynamicIndexReader {
|
|
|
|
fn index_count(&self) -> usize {
|
|
|
|
self.index_entries
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index_digest(&self, pos: usize) -> Option<&[u8; 32]> {
|
|
|
|
if pos >= self.index_entries {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(unsafe {
|
|
|
|
std::mem::transmute(self.chunk_digest(pos).as_ptr())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
pub struct BufferedDynamicReader {
|
|
|
|
index: DynamicIndexReader,
|
2019-01-05 13:47:56 +00:00
|
|
|
archive_size: u64,
|
|
|
|
read_buffer: Vec<u8>,
|
|
|
|
buffered_chunk_idx: usize,
|
|
|
|
buffered_chunk_start: u64,
|
|
|
|
read_offset: u64,
|
2019-01-02 13:27:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
impl BufferedDynamicReader {
|
2019-01-05 13:47:56 +00:00
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
pub fn new(index: DynamicIndexReader) -> Self {
|
2019-01-05 13:47:56 +00:00
|
|
|
|
|
|
|
let archive_size = index.chunk_end(index.index_entries - 1);
|
|
|
|
Self {
|
|
|
|
index: index,
|
|
|
|
archive_size: archive_size,
|
|
|
|
read_buffer: Vec::with_capacity(1024*1024),
|
|
|
|
buffered_chunk_idx: 0,
|
|
|
|
buffered_chunk_start: 0,
|
|
|
|
read_offset: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn archive_size(&self) -> u64 { self.archive_size }
|
|
|
|
|
2019-01-05 16:28:20 +00:00
|
|
|
fn buffer_chunk(&mut self, idx: usize) -> Result<(), Error> {
|
|
|
|
|
2019-01-20 08:39:32 +00:00
|
|
|
let index = &self.index;
|
2019-01-05 16:28:20 +00:00
|
|
|
let end = index.chunk_end(idx);
|
|
|
|
let digest = index.chunk_digest(idx);
|
|
|
|
index.store.read_chunk(digest, &mut self.read_buffer)?;
|
|
|
|
|
|
|
|
self.buffered_chunk_idx = idx;
|
|
|
|
self.buffered_chunk_start = end - (self.read_buffer.len() as u64);
|
|
|
|
//println!("BUFFER {} {}", self.buffered_chunk_start, end);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 08:17:04 +00:00
|
|
|
impl crate::tools::BufferedRead for BufferedDynamicReader {
|
2019-01-05 16:28:20 +00:00
|
|
|
|
|
|
|
fn buffered_read(&mut self, offset: u64) -> Result<&[u8], Error> {
|
2019-01-05 13:47:56 +00:00
|
|
|
|
2019-01-06 08:17:28 +00:00
|
|
|
if offset == self.archive_size { return Ok(&self.read_buffer[0..0]); }
|
|
|
|
|
2019-01-05 13:47:56 +00:00
|
|
|
let buffer_len = self.read_buffer.len();
|
2019-01-20 08:39:32 +00:00
|
|
|
let index = &self.index;
|
2019-01-05 13:47:56 +00:00
|
|
|
|
|
|
|
// optimization for sequential read
|
|
|
|
if buffer_len > 0 &&
|
|
|
|
((self.buffered_chunk_idx + 1) < index.index_entries) &&
|
|
|
|
(offset >= (self.buffered_chunk_start + (self.read_buffer.len() as u64)))
|
|
|
|
{
|
|
|
|
let next_idx = self.buffered_chunk_idx + 1;
|
|
|
|
let next_end = index.chunk_end(next_idx);
|
|
|
|
if offset < next_end {
|
2019-01-10 10:19:54 +00:00
|
|
|
self.buffer_chunk(next_idx)?;
|
2019-01-05 13:47:56 +00:00
|
|
|
let buffer_offset = (offset - self.buffered_chunk_start) as usize;
|
|
|
|
return Ok(&self.read_buffer[buffer_offset..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_len == 0) ||
|
|
|
|
(offset < self.buffered_chunk_start) ||
|
|
|
|
(offset >= (self.buffered_chunk_start + (self.read_buffer.len() as u64)))
|
|
|
|
{
|
|
|
|
let end_idx = index.index_entries - 1;
|
|
|
|
let end = index.chunk_end(end_idx);
|
|
|
|
let idx = index.binary_search(0, 0, end_idx, end, offset)?;
|
2019-01-10 10:19:54 +00:00
|
|
|
self.buffer_chunk(idx)?;
|
2019-01-05 13:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let buffer_offset = (offset - self.buffered_chunk_start) as usize;
|
|
|
|
Ok(&self.read_buffer[buffer_offset..])
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-01-02 13:27:04 +00:00
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
impl std::io::Read for BufferedDynamicReader {
|
2019-01-06 09:04:45 +00:00
|
|
|
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
|
|
|
|
|
|
|
|
use std::io::{Error, ErrorKind};
|
2019-02-28 08:17:04 +00:00
|
|
|
use crate::tools::BufferedRead;
|
2019-01-06 09:04:45 +00:00
|
|
|
|
|
|
|
let data = match self.buffered_read(self.read_offset) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(err) => return Err(Error::new(ErrorKind::Other, err.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let n = if data.len() > buf.len() { buf.len() } else { data.len() };
|
|
|
|
|
|
|
|
unsafe { std::ptr::copy_nonoverlapping(data.as_ptr(), buf.as_mut_ptr(), n); }
|
|
|
|
|
|
|
|
self.read_offset += n as u64;
|
|
|
|
|
|
|
|
return Ok(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
impl std::io::Seek for BufferedDynamicReader {
|
2019-01-06 08:35:39 +00:00
|
|
|
|
|
|
|
fn seek(&mut self, pos: std::io::SeekFrom) -> Result<u64, std::io::Error> {
|
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
use std::io::{SeekFrom};
|
2019-01-06 08:35:39 +00:00
|
|
|
|
|
|
|
let new_offset = match pos {
|
|
|
|
SeekFrom::Start(start_offset) => start_offset as i64,
|
|
|
|
SeekFrom::End(end_offset) => (self.archive_size as i64)+ end_offset,
|
|
|
|
SeekFrom::Current(offset) => (self.read_offset as i64) + offset,
|
|
|
|
};
|
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
use std::io::{Error, ErrorKind};
|
2019-01-06 08:35:39 +00:00
|
|
|
if (new_offset < 0) || (new_offset > (self.archive_size as i64)) {
|
|
|
|
return Err(Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
format!("seek is out of range {} ([0..{}])", new_offset, self.archive_size)));
|
|
|
|
}
|
|
|
|
self.read_offset = new_offset as u64;
|
|
|
|
|
|
|
|
Ok(self.read_offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
pub struct DynamicIndexWriter {
|
2019-01-15 10:38:26 +00:00
|
|
|
store: Arc<ChunkStore>,
|
2018-12-31 16:30:08 +00:00
|
|
|
chunker: Chunker,
|
2019-01-02 11:54:40 +00:00
|
|
|
writer: BufWriter<File>,
|
2019-01-02 10:02:56 +00:00
|
|
|
closed: bool,
|
2018-12-31 16:30:08 +00:00
|
|
|
filename: PathBuf,
|
|
|
|
tmp_filename: PathBuf,
|
2019-01-30 17:25:37 +00:00
|
|
|
pub uuid: [u8; 16],
|
|
|
|
pub ctime: u64,
|
2018-12-31 16:30:08 +00:00
|
|
|
|
2019-02-25 11:52:10 +00:00
|
|
|
stat: ChunkStat,
|
|
|
|
|
2018-12-31 16:30:08 +00:00
|
|
|
chunk_offset: usize,
|
|
|
|
last_chunk: usize,
|
|
|
|
chunk_buffer: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
impl Drop for DynamicIndexWriter {
|
2019-01-15 10:38:26 +00:00
|
|
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let _ = std::fs::remove_file(&self.tmp_filename); // ignore errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
impl DynamicIndexWriter {
|
2018-12-31 16:30:08 +00:00
|
|
|
|
2019-01-15 10:38:26 +00:00
|
|
|
pub fn create(store: Arc<ChunkStore>, path: &Path, chunk_size: usize) -> Result<Self, Error> {
|
2018-12-31 16:30:08 +00:00
|
|
|
|
|
|
|
let full_path = store.relative_path(path);
|
|
|
|
let mut tmp_path = full_path.clone();
|
2019-02-12 11:05:33 +00:00
|
|
|
tmp_path.set_extension("tmp_didx");
|
2018-12-31 16:30:08 +00:00
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
let file = std::fs::OpenOptions::new()
|
2018-12-31 16:30:08 +00:00
|
|
|
.create(true).truncate(true)
|
|
|
|
.read(true)
|
|
|
|
.write(true)
|
|
|
|
.open(&tmp_path)?;
|
|
|
|
|
2019-01-02 11:54:40 +00:00
|
|
|
let mut writer = BufWriter::with_capacity(1024*1024, file);
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
let header_size = std::mem::size_of::<DynamicIndexHeader>();
|
2018-12-31 16:30:08 +00:00
|
|
|
|
|
|
|
// todo: use static assertion when available in rust
|
|
|
|
if header_size != 4096 { panic!("got unexpected header size"); }
|
|
|
|
|
|
|
|
let ctime = std::time::SystemTime::now().duration_since(
|
|
|
|
std::time::SystemTime::UNIX_EPOCH)?.as_secs();
|
|
|
|
|
|
|
|
let uuid = Uuid::new_v4();
|
|
|
|
|
|
|
|
let mut buffer = vec![0u8; header_size];
|
2019-02-12 11:05:33 +00:00
|
|
|
let header = crate::tools::map_struct_mut::<DynamicIndexHeader>(&mut buffer)?;
|
2018-12-31 16:30:08 +00:00
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
header.magic = *b"PROXMOX-DIDX";
|
2018-12-31 16:30:08 +00:00
|
|
|
header.version = u32::to_le(1);
|
|
|
|
header.ctime = u64::to_le(ctime);
|
|
|
|
header.uuid = *uuid.as_bytes();
|
|
|
|
|
2019-01-02 11:54:40 +00:00
|
|
|
writer.write_all(&buffer)?;
|
2018-12-31 16:30:08 +00:00
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
store,
|
|
|
|
chunker: Chunker::new(chunk_size),
|
2019-01-02 11:54:40 +00:00
|
|
|
writer: writer,
|
2019-01-02 10:02:56 +00:00
|
|
|
closed: false,
|
2018-12-31 16:30:08 +00:00
|
|
|
filename: full_path,
|
|
|
|
tmp_filename: tmp_path,
|
|
|
|
ctime,
|
|
|
|
uuid: *uuid.as_bytes(),
|
|
|
|
|
2019-02-25 11:52:10 +00:00
|
|
|
stat: ChunkStat::new(0),
|
|
|
|
|
2018-12-31 16:30:08 +00:00
|
|
|
chunk_offset: 0,
|
|
|
|
last_chunk: 0,
|
|
|
|
chunk_buffer: Vec::with_capacity(chunk_size*4),
|
|
|
|
})
|
|
|
|
}
|
2019-01-02 10:02:56 +00:00
|
|
|
|
|
|
|
pub fn close(&mut self) -> Result<(), Error> {
|
|
|
|
|
|
|
|
if self.closed {
|
|
|
|
bail!("cannot close already closed archive index file {:?}", self.filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.closed = true;
|
|
|
|
|
|
|
|
self.write_chunk_buffer()?;
|
|
|
|
|
2019-01-02 11:54:40 +00:00
|
|
|
self.writer.flush()?;
|
2019-01-02 10:02:56 +00:00
|
|
|
|
2019-02-25 11:52:10 +00:00
|
|
|
self.stat.size = self.chunk_offset as u64;
|
|
|
|
|
|
|
|
// add size of index file
|
|
|
|
self.stat.size += (self.stat.chunk_count*40 + std::mem::size_of::<DynamicIndexHeader>()) as u64;
|
|
|
|
|
|
|
|
println!("STAT: {:?}", self.stat);
|
|
|
|
|
2019-01-02 10:02:56 +00:00
|
|
|
// fixme:
|
|
|
|
|
|
|
|
if let Err(err) = std::fs::rename(&self.tmp_filename, &self.filename) {
|
|
|
|
bail!("Atomic rename file {:?} failed - {}", self.filename, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:52:10 +00:00
|
|
|
pub fn stat(&self) -> &ChunkStat {
|
|
|
|
&self.stat
|
|
|
|
}
|
|
|
|
|
2019-01-02 10:02:56 +00:00
|
|
|
fn write_chunk_buffer(&mut self) -> Result<(), std::io::Error> {
|
|
|
|
|
|
|
|
use std::io::{Error, ErrorKind};
|
|
|
|
|
|
|
|
let chunk_size = self.chunk_buffer.len();
|
|
|
|
|
|
|
|
if chunk_size == 0 { return Ok(()); }
|
|
|
|
|
|
|
|
let expected_chunk_size = self.chunk_offset - self.last_chunk;
|
|
|
|
if expected_chunk_size != self.chunk_buffer.len() {
|
|
|
|
return Err(Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
format!("wrong chunk size {} != {}", expected_chunk_size, chunk_size)));
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:52:10 +00:00
|
|
|
self.stat.chunk_count += 1;
|
2019-02-19 14:19:12 +00:00
|
|
|
|
2019-01-02 10:02:56 +00:00
|
|
|
self.last_chunk = self.chunk_offset;
|
|
|
|
|
|
|
|
match self.store.insert_chunk(&self.chunk_buffer) {
|
2019-02-25 10:36:05 +00:00
|
|
|
Ok((is_duplicate, digest, compressed_size)) => {
|
|
|
|
|
2019-02-25 11:52:10 +00:00
|
|
|
self.stat.compressed_size += compressed_size;
|
2019-02-25 10:36:05 +00:00
|
|
|
if is_duplicate {
|
2019-02-25 11:52:10 +00:00
|
|
|
self.stat.duplicate_chunks += 1;
|
2019-02-25 10:36:05 +00:00
|
|
|
} else {
|
2019-02-25 11:52:10 +00:00
|
|
|
self.stat.disk_size += compressed_size;
|
2019-02-25 10:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
println!("ADD CHUNK {:016x} {} {}% {} {}", self.chunk_offset, chunk_size,
|
|
|
|
(compressed_size*100)/(chunk_size as u64), is_duplicate, tools::digest_to_hex(&digest));
|
2019-02-25 12:50:45 +00:00
|
|
|
self.add_chunk(self.chunk_offset as u64, &digest)?;
|
2019-01-02 10:02:56 +00:00
|
|
|
self.chunk_buffer.truncate(0);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
self.chunk_buffer.truncate(0);
|
|
|
|
return Err(Error::new(ErrorKind::Other, err.to_string()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-25 12:50:45 +00:00
|
|
|
|
|
|
|
pub fn add_chunk(&mut self, offset: u64, digest: &[u8; 32]) -> Result<(), std::io::Error> {
|
|
|
|
self.writer.write(unsafe { &std::mem::transmute::<u64, [u8;8]>(offset.to_le()) })?;
|
|
|
|
self.writer.write(digest)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-31 16:30:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
impl Write for DynamicIndexWriter {
|
2018-12-31 16:30:08 +00:00
|
|
|
|
|
|
|
fn write(&mut self, data: &[u8]) -> std::result::Result<usize, std::io::Error> {
|
|
|
|
|
|
|
|
let chunker = &mut self.chunker;
|
|
|
|
|
|
|
|
let pos = chunker.scan(data);
|
|
|
|
|
|
|
|
if pos > 0 {
|
|
|
|
self.chunk_buffer.extend(&data[0..pos]);
|
|
|
|
self.chunk_offset += pos;
|
|
|
|
|
2019-01-02 10:02:56 +00:00
|
|
|
self.write_chunk_buffer()?;
|
|
|
|
Ok(pos)
|
2018-12-31 16:30:08 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
self.chunk_offset += data.len();
|
|
|
|
self.chunk_buffer.extend(data);
|
2019-01-02 10:02:56 +00:00
|
|
|
Ok(data.len())
|
2018-12-31 16:30:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> std::result::Result<(), std::io::Error> {
|
|
|
|
|
2018-12-31 17:01:07 +00:00
|
|
|
use std::io::{Error, ErrorKind};
|
|
|
|
|
2019-01-02 10:02:56 +00:00
|
|
|
Err(Error::new(ErrorKind::Other, "please use close() instead of flush()"))
|
2018-12-31 16:30:08 +00:00
|
|
|
}
|
|
|
|
}
|