2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2019-11-14 09:18:31 +00:00
|
|
|
use std::io::{Seek, SeekFrom};
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-02-25 11:52:10 +00:00
|
|
|
use super::chunk_stat::*;
|
2018-12-15 13:51:05 +00:00
|
|
|
use super::chunk_store::*;
|
2020-06-26 06:14:45 +00:00
|
|
|
use super::{IndexFile, ChunkReadInfo};
|
2020-09-12 13:10:47 +00:00
|
|
|
use crate::tools;
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-03-27 09:17:03 +00:00
|
|
|
use std::fs::File;
|
2019-11-14 09:18:31 +00:00
|
|
|
use std::io::Write;
|
2018-12-15 13:51:05 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2019-11-14 09:18:31 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::sync::Arc;
|
2019-07-05 07:19:56 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
use super::ChunkInfo;
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-09-11 12:45:43 +00:00
|
|
|
use proxmox::tools::io::ReadExt;
|
2019-11-14 09:18:31 +00:00
|
|
|
use proxmox::tools::Uuid;
|
2019-09-11 12:45:43 +00:00
|
|
|
|
2019-03-11 09:51:48 +00:00
|
|
|
/// Header format definition for fixed index files (`.fidx`)
|
2018-12-15 16:05:49 +00:00
|
|
|
#[repr(C)]
|
2019-02-12 10:50:45 +00:00
|
|
|
pub struct FixedIndexHeader {
|
2019-06-14 12:58:37 +00:00
|
|
|
pub magic: [u8; 8],
|
2018-12-15 16:05:49 +00:00
|
|
|
pub uuid: [u8; 16],
|
2020-09-12 13:10:47 +00:00
|
|
|
pub ctime: i64,
|
2019-06-14 08:34:29 +00:00
|
|
|
/// Sha256 over the index ``SHA256(digest1||digest2||...)``
|
|
|
|
pub index_csum: [u8; 32],
|
2019-06-14 12:58:37 +00:00
|
|
|
pub size: u64,
|
|
|
|
pub chunk_size: u64,
|
|
|
|
reserved: [u8; 4016], // overall size is one page (4096 bytes)
|
2018-12-15 16:05:49 +00:00
|
|
|
}
|
2020-01-21 11:28:01 +00:00
|
|
|
proxmox::static_assert_size!(FixedIndexHeader, 4096);
|
2018-12-15 13:51:05 +00:00
|
|
|
|
|
|
|
// split image into fixed size chunks
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
pub struct FixedIndexReader {
|
2019-03-27 09:17:03 +00:00
|
|
|
_file: File,
|
2019-05-28 04:18:24 +00:00
|
|
|
pub chunk_size: usize,
|
2019-07-04 13:13:22 +00:00
|
|
|
pub size: u64,
|
2019-05-28 06:27:57 +00:00
|
|
|
index_length: usize,
|
2018-12-16 13:44:44 +00:00
|
|
|
index: *mut u8,
|
2019-01-30 17:25:37 +00:00
|
|
|
pub uuid: [u8; 16],
|
2020-09-12 13:10:47 +00:00
|
|
|
pub ctime: i64,
|
2019-06-14 08:34:29 +00:00
|
|
|
pub index_csum: [u8; 32],
|
2018-12-16 13:44:44 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:36:19 +00:00
|
|
|
// `index` is mmap()ed which cannot be thread-local so should be sendable
|
|
|
|
unsafe impl Send for FixedIndexReader {}
|
2019-08-28 08:33:41 +00:00
|
|
|
unsafe impl Sync for FixedIndexReader {}
|
2019-02-28 08:36:19 +00:00
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
impl Drop for FixedIndexReader {
|
2018-12-16 13:44:44 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Err(err) = self.unmap() {
|
2019-07-04 06:09:48 +00:00
|
|
|
eprintln!("Unable to unmap file - {}", err);
|
2018-12-16 13:44:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
impl FixedIndexReader {
|
2019-07-04 06:09:48 +00:00
|
|
|
pub fn open(path: &Path) -> Result<Self, Error> {
|
|
|
|
File::open(path)
|
|
|
|
.map_err(Error::from)
|
|
|
|
.and_then(|file| Self::new(file))
|
|
|
|
.map_err(|err| format_err!("Unable to open fixed index {:?} - {}", path, err))
|
|
|
|
}
|
2018-12-16 13:44:44 +00:00
|
|
|
|
2019-07-04 06:09:48 +00:00
|
|
|
pub fn new(mut file: std::fs::File) -> Result<Self, Error> {
|
2019-07-05 07:19:56 +00:00
|
|
|
file.seek(SeekFrom::Start(0))?;
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
let header_size = std::mem::size_of::<FixedIndexHeader>();
|
2020-11-02 11:34:36 +00:00
|
|
|
|
|
|
|
let rawfd = file.as_raw_fd();
|
|
|
|
let stat = match nix::sys::stat::fstat(rawfd) {
|
|
|
|
Ok(stat) => stat,
|
|
|
|
Err(err) => bail!("fstat failed - {}", err),
|
|
|
|
};
|
|
|
|
|
|
|
|
let size = stat.st_size as usize;
|
|
|
|
|
|
|
|
if size < header_size {
|
|
|
|
bail!("index too small ({})", stat.st_size);
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:45:43 +00:00
|
|
|
let header: Box<FixedIndexHeader> = unsafe { file.read_host_value_boxed()? };
|
2018-12-16 13:44:44 +00:00
|
|
|
|
2019-06-14 12:58:37 +00:00
|
|
|
if header.magic != super::FIXED_SIZED_CHUNK_INDEX_1_0 {
|
2019-07-04 06:09:48 +00:00
|
|
|
bail!("got unknown magic number");
|
2019-01-02 12:13:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 13:13:22 +00:00
|
|
|
let size = u64::from_le(header.size);
|
2020-09-12 13:10:47 +00:00
|
|
|
let ctime = i64::from_le(header.ctime);
|
2019-07-04 13:13:22 +00:00
|
|
|
let chunk_size = u64::from_le(header.chunk_size);
|
2018-12-16 13:44:44 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let index_length = ((size + chunk_size - 1) / chunk_size) as usize;
|
|
|
|
let index_size = index_length * 32;
|
2018-12-16 13:44:44 +00:00
|
|
|
|
2019-01-02 17:14:02 +00:00
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
let expected_index_size = (stat.st_size as usize) - header_size;
|
2019-01-02 17:14:02 +00:00
|
|
|
if index_size != expected_index_size {
|
2019-11-14 09:18:31 +00:00
|
|
|
bail!(
|
|
|
|
"got unexpected file size ({} != {})",
|
|
|
|
index_size,
|
|
|
|
expected_index_size
|
|
|
|
);
|
2019-01-02 17:14:02 +00:00
|
|
|
}
|
2018-12-16 13:44:44 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
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;
|
2018-12-16 13:44:44 +00:00
|
|
|
|
|
|
|
Ok(Self {
|
2019-03-27 09:17:03 +00:00
|
|
|
_file: file,
|
2019-07-04 13:13:22 +00:00
|
|
|
chunk_size: chunk_size as usize,
|
2018-12-16 13:44:44 +00:00
|
|
|
size,
|
2019-05-28 06:27:57 +00:00
|
|
|
index_length,
|
2018-12-16 13:44:44 +00:00
|
|
|
index: data,
|
|
|
|
ctime,
|
|
|
|
uuid: header.uuid,
|
2019-06-14 08:34:29 +00:00
|
|
|
index_csum: header.index_csum,
|
2018-12-16 13:44:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unmap(&mut self) -> Result<(), Error> {
|
2019-11-14 09:18:31 +00:00
|
|
|
if self.index == std::ptr::null_mut() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-12-16 13:44:44 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let index_size = self.index_length * 32;
|
2018-12-16 13:44:44 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
if let Err(err) =
|
|
|
|
unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) }
|
|
|
|
{
|
2019-07-04 06:09:48 +00:00
|
|
|
bail!("unmap file failed - {}", err);
|
2018-12-16 13:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.index = std::ptr::null_mut();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_info(&self) {
|
|
|
|
println!("Size: {}", self.size);
|
|
|
|
println!("ChunkSize: {}", self.chunk_size);
|
2020-09-12 13:10:47 +00:00
|
|
|
|
|
|
|
let mut ctime_str = self.ctime.to_string();
|
|
|
|
if let Ok(s) = proxmox::tools::time::strftime_local("%c",self.ctime) {
|
|
|
|
ctime_str = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("CTime: {}", ctime_str);
|
2018-12-16 13:44:44 +00:00
|
|
|
println!("UUID: {:?}", self.uuid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 13:32:34 +00:00
|
|
|
impl IndexFile for FixedIndexReader {
|
|
|
|
fn index_count(&self) -> usize {
|
2019-05-28 06:27:57 +00:00
|
|
|
self.index_length
|
2019-02-27 13:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn index_digest(&self, pos: usize) -> Option<&[u8; 32]> {
|
2019-05-28 06:27:57 +00:00
|
|
|
if pos >= self.index_length {
|
2019-02-27 13:32:34 +00:00
|
|
|
None
|
|
|
|
} else {
|
2019-11-14 09:18:31 +00:00
|
|
|
Some(unsafe { std::mem::transmute(self.index.add(pos * 32)) })
|
2019-02-27 13:32:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-04 05:57:43 +00:00
|
|
|
|
|
|
|
fn index_bytes(&self) -> u64 {
|
2019-07-04 13:13:22 +00:00
|
|
|
self.size
|
2019-07-04 05:57:43 +00:00
|
|
|
}
|
2020-06-26 06:14:45 +00:00
|
|
|
|
|
|
|
fn chunk_info(&self, pos: usize) -> Option<ChunkReadInfo> {
|
|
|
|
if pos >= self.index_length {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let start = (pos * self.chunk_size) as u64;
|
|
|
|
let mut end = start + self.chunk_size as u64;
|
|
|
|
|
|
|
|
if end > self.size {
|
|
|
|
end = self.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
let digest = self.index_digest(pos).unwrap();
|
|
|
|
Some(ChunkReadInfo {
|
|
|
|
range: start..end,
|
|
|
|
digest: *digest,
|
|
|
|
})
|
|
|
|
}
|
2020-06-29 10:44:45 +00:00
|
|
|
|
|
|
|
fn compute_csum(&self) -> ([u8; 32], u64) {
|
|
|
|
let mut csum = openssl::sha::Sha256::new();
|
|
|
|
let mut chunk_end = 0;
|
|
|
|
for pos in 0..self.index_count() {
|
|
|
|
let info = self.chunk_info(pos).unwrap();
|
|
|
|
chunk_end = info.range.end;
|
|
|
|
csum.update(&info.digest);
|
|
|
|
}
|
|
|
|
let csum = csum.finish();
|
|
|
|
|
|
|
|
(csum, chunk_end)
|
|
|
|
}
|
2020-07-22 13:56:21 +00:00
|
|
|
|
|
|
|
fn chunk_from_offset(&self, offset: u64) -> Option<(usize, u64)> {
|
|
|
|
if offset >= self.size {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some((
|
|
|
|
(offset / self.chunk_size as u64) as usize,
|
2020-07-22 15:46:02 +00:00
|
|
|
offset & (self.chunk_size - 1) as u64 // fast modulo, valid for 2^x chunk_size
|
2020-07-22 13:56:21 +00:00
|
|
|
))
|
|
|
|
}
|
2019-02-27 13:32:34 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
pub struct FixedIndexWriter {
|
2019-01-15 11:36:16 +00:00
|
|
|
store: Arc<ChunkStore>,
|
2019-06-14 08:34:29 +00:00
|
|
|
file: File,
|
2019-03-22 08:42:15 +00:00
|
|
|
_lock: tools::ProcessLockSharedGuard,
|
2018-12-16 12:39:21 +00:00
|
|
|
filename: PathBuf,
|
|
|
|
tmp_filename: PathBuf,
|
2018-12-15 13:51:05 +00:00
|
|
|
chunk_size: usize,
|
|
|
|
size: usize,
|
2019-05-28 06:27:57 +00:00
|
|
|
index_length: usize,
|
2018-12-15 13:51:05 +00:00
|
|
|
index: *mut u8,
|
2019-01-30 17:25:37 +00:00
|
|
|
pub uuid: [u8; 16],
|
2020-09-12 13:10:47 +00:00
|
|
|
pub ctime: i64,
|
2018-12-15 13:51:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 09:20:04 +00:00
|
|
|
// `index` is mmap()ed which cannot be thread-local so should be sendable
|
|
|
|
unsafe impl Send for FixedIndexWriter {}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
impl Drop for FixedIndexWriter {
|
2018-12-16 12:39:21 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
let _ = std::fs::remove_file(&self.tmp_filename); // ignore errors
|
|
|
|
if let Err(err) = self.unmap() {
|
2018-12-16 12:43:19 +00:00
|
|
|
eprintln!("Unable to unmap file {:?} - {}", self.tmp_filename, err);
|
2018-12-16 12:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
impl FixedIndexWriter {
|
2019-10-25 16:03:55 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-11-14 09:18:31 +00:00
|
|
|
pub fn create(
|
|
|
|
store: Arc<ChunkStore>,
|
|
|
|
path: &Path,
|
|
|
|
size: usize,
|
|
|
|
chunk_size: usize,
|
|
|
|
) -> Result<Self, Error> {
|
2019-03-22 08:42:15 +00:00
|
|
|
let shared_lock = store.try_shared_lock()?;
|
|
|
|
|
2018-12-15 13:51:05 +00:00
|
|
|
let full_path = store.relative_path(path);
|
2018-12-16 12:39:21 +00:00
|
|
|
let mut tmp_path = full_path.clone();
|
2019-02-12 10:50:45 +00:00
|
|
|
tmp_path.set_extension("tmp_fidx");
|
2018-12-15 13:51:05 +00:00
|
|
|
|
|
|
|
let mut file = std::fs::OpenOptions::new()
|
2019-11-14 09:18:31 +00:00
|
|
|
.create(true)
|
|
|
|
.truncate(true)
|
2018-12-15 13:51:05 +00:00
|
|
|
.read(true)
|
|
|
|
.write(true)
|
2018-12-16 12:39:21 +00:00
|
|
|
.open(&tmp_path)?;
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
let header_size = std::mem::size_of::<FixedIndexHeader>();
|
2018-12-15 16:05:49 +00:00
|
|
|
|
|
|
|
// todo: use static assertion when available in rust
|
2019-11-14 09:18:31 +00:00
|
|
|
if header_size != 4096 {
|
|
|
|
panic!("got unexpected header size");
|
|
|
|
}
|
2018-12-15 16:05:49 +00:00
|
|
|
|
2020-09-12 13:10:47 +00:00
|
|
|
let ctime = proxmox::tools::time::epoch_i64();
|
2018-12-15 16:05:49 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let uuid = Uuid::generate();
|
2018-12-15 16:05:49 +00:00
|
|
|
|
2018-12-16 12:43:19 +00:00
|
|
|
let buffer = vec![0u8; header_size];
|
2019-11-14 09:18:31 +00:00
|
|
|
let header = unsafe { &mut *(buffer.as_ptr() as *mut FixedIndexHeader) };
|
2018-12-15 16:05:49 +00:00
|
|
|
|
2019-06-14 12:58:37 +00:00
|
|
|
header.magic = super::FIXED_SIZED_CHUNK_INDEX_1_0;
|
2020-09-12 13:10:47 +00:00
|
|
|
header.ctime = i64::to_le(ctime);
|
2019-01-04 07:45:04 +00:00
|
|
|
header.size = u64::to_le(size as u64);
|
|
|
|
header.chunk_size = u64::to_le(chunk_size as u64);
|
2018-12-15 16:05:49 +00:00
|
|
|
header.uuid = *uuid.as_bytes();
|
|
|
|
|
2019-06-14 08:34:29 +00:00
|
|
|
header.index_csum = [0u8; 32];
|
|
|
|
|
2018-12-16 10:48:03 +00:00
|
|
|
file.write_all(&buffer)?;
|
2018-12-15 16:05:49 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let index_length = (size + chunk_size - 1) / chunk_size;
|
|
|
|
let index_size = index_length * 32;
|
2018-12-15 16:05:49 +00:00
|
|
|
nix::unistd::ftruncate(file.as_raw_fd(), (header_size + index_size) as i64)?;
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let data = unsafe {
|
|
|
|
nix::sys::mman::mmap(
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
index_size,
|
|
|
|
nix::sys::mman::ProtFlags::PROT_READ | nix::sys::mman::ProtFlags::PROT_WRITE,
|
|
|
|
nix::sys::mman::MapFlags::MAP_SHARED,
|
|
|
|
file.as_raw_fd(),
|
|
|
|
header_size as i64,
|
|
|
|
)
|
|
|
|
}? as *mut u8;
|
2018-12-15 16:05:49 +00:00
|
|
|
|
2018-12-15 13:51:05 +00:00
|
|
|
Ok(Self {
|
|
|
|
store,
|
2019-06-14 08:34:29 +00:00
|
|
|
file,
|
2019-03-22 08:42:15 +00:00
|
|
|
_lock: shared_lock,
|
2018-12-16 12:39:21 +00:00
|
|
|
filename: full_path,
|
|
|
|
tmp_filename: tmp_path,
|
2018-12-15 13:51:05 +00:00
|
|
|
chunk_size,
|
|
|
|
size,
|
2019-05-28 06:27:57 +00:00
|
|
|
index_length,
|
2018-12-15 13:51:05 +00:00
|
|
|
index: data,
|
2018-12-15 16:05:49 +00:00
|
|
|
ctime,
|
|
|
|
uuid: *uuid.as_bytes(),
|
2018-12-15 13:51:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-28 07:12:38 +00:00
|
|
|
pub fn index_length(&self) -> usize {
|
|
|
|
self.index_length
|
|
|
|
}
|
|
|
|
|
2018-12-16 12:39:21 +00:00
|
|
|
fn unmap(&mut self) -> Result<(), Error> {
|
2019-11-14 09:18:31 +00:00
|
|
|
if self.index == std::ptr::null_mut() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-12-16 12:39:21 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let index_size = self.index_length * 32;
|
2018-12-16 12:39:21 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
if let Err(err) =
|
|
|
|
unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) }
|
|
|
|
{
|
2018-12-16 12:43:19 +00:00
|
|
|
bail!("unmap file {:?} failed - {}", self.tmp_filename, err);
|
2018-12-16 12:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.index = std::ptr::null_mut();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
pub fn close(&mut self) -> Result<[u8; 32], Error> {
|
|
|
|
if self.index == std::ptr::null_mut() {
|
|
|
|
bail!("cannot close already closed index file.");
|
|
|
|
}
|
2018-12-16 12:39:21 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let index_size = self.index_length * 32;
|
2019-06-14 08:34:29 +00:00
|
|
|
let data = unsafe { std::slice::from_raw_parts(self.index, index_size) };
|
2019-11-14 09:18:31 +00:00
|
|
|
let index_csum = openssl::sha::sha256(data);
|
2019-06-14 08:34:29 +00:00
|
|
|
|
2018-12-16 12:39:21 +00:00
|
|
|
self.unmap()?;
|
|
|
|
|
2020-01-21 11:28:01 +00:00
|
|
|
let csum_offset = proxmox::offsetof!(FixedIndexHeader, index_csum);
|
2019-07-05 07:19:56 +00:00
|
|
|
self.file.seek(SeekFrom::Start(csum_offset as u64))?;
|
2019-06-14 08:34:29 +00:00
|
|
|
self.file.write_all(&index_csum)?;
|
|
|
|
self.file.flush()?;
|
|
|
|
|
2018-12-16 12:39:21 +00:00
|
|
|
if let Err(err) = std::fs::rename(&self.tmp_filename, &self.filename) {
|
|
|
|
bail!("Atomic rename file {:?} failed - {}", self.filename, err);
|
|
|
|
}
|
|
|
|
|
2019-06-14 08:34:29 +00:00
|
|
|
Ok(index_csum)
|
2018-12-16 12:39:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 11:40:43 +00:00
|
|
|
pub fn check_chunk_alignment(&self, offset: usize, chunk_len: usize) -> Result<usize, Error> {
|
|
|
|
if offset < chunk_len {
|
|
|
|
bail!("got chunk with small offset ({} < {}", offset, chunk_len);
|
2019-06-13 09:47:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 11:40:43 +00:00
|
|
|
let pos = offset - chunk_len;
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-07-04 11:40:43 +00:00
|
|
|
if offset > self.size {
|
|
|
|
bail!("chunk data exceeds size ({} >= {})", offset, self.size);
|
2018-12-15 13:51:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// last chunk can be smaller
|
2019-11-14 09:18:31 +00:00
|
|
|
if ((offset != self.size) && (chunk_len != self.chunk_size))
|
|
|
|
|| (chunk_len > self.chunk_size)
|
|
|
|
|| (chunk_len == 0)
|
|
|
|
{
|
|
|
|
bail!(
|
|
|
|
"chunk with unexpected length ({} != {}",
|
|
|
|
chunk_len,
|
|
|
|
self.chunk_size
|
|
|
|
);
|
|
|
|
}
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
if pos & (self.chunk_size - 1) != 0 {
|
2019-07-04 11:40:43 +00:00
|
|
|
bail!("got unaligned chunk (pos = {})", pos);
|
2019-06-13 09:47:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 11:40:43 +00:00
|
|
|
Ok(pos / self.chunk_size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: We want to add data out of order, so do not assume any order here.
|
|
|
|
pub fn add_chunk(&mut self, chunk_info: &ChunkInfo, stat: &mut ChunkStat) -> Result<(), Error> {
|
|
|
|
let chunk_len = chunk_info.chunk_len as usize;
|
|
|
|
let offset = chunk_info.offset as usize; // end of chunk
|
|
|
|
|
|
|
|
let idx = self.check_chunk_alignment(offset, chunk_len)?;
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let (is_duplicate, compressed_size) = self
|
|
|
|
.store
|
|
|
|
.insert_chunk(&chunk_info.chunk, &chunk_info.digest)?;
|
2019-02-25 10:36:05 +00:00
|
|
|
|
2019-05-29 05:08:34 +00:00
|
|
|
stat.chunk_count += 1;
|
|
|
|
stat.compressed_size += compressed_size;
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-10-06 08:31:06 +00:00
|
|
|
let digest = &chunk_info.digest;
|
2019-06-13 09:47:23 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
println!(
|
|
|
|
"ADD CHUNK {} {} {}% {} {}",
|
|
|
|
idx,
|
|
|
|
chunk_len,
|
|
|
|
(compressed_size * 100) / (chunk_len as u64),
|
|
|
|
is_duplicate,
|
|
|
|
proxmox::tools::digest_to_hex(digest)
|
|
|
|
);
|
2019-02-25 10:36:05 +00:00
|
|
|
|
|
|
|
if is_duplicate {
|
2019-05-29 05:08:34 +00:00
|
|
|
stat.duplicate_chunks += 1;
|
2019-02-25 10:36:05 +00:00
|
|
|
} else {
|
2019-05-29 05:08:34 +00:00
|
|
|
stat.disk_size += compressed_size;
|
2019-02-25 10:36:05 +00:00
|
|
|
}
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-07-04 11:40:43 +00:00
|
|
|
self.add_digest(idx, digest)
|
2019-03-06 09:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_digest(&mut self, index: usize, digest: &[u8; 32]) -> Result<(), Error> {
|
2019-05-28 06:47:03 +00:00
|
|
|
if index >= self.index_length {
|
2019-11-14 09:18:31 +00:00
|
|
|
bail!(
|
|
|
|
"add digest failed - index out of range ({} >= {})",
|
|
|
|
index,
|
|
|
|
self.index_length
|
|
|
|
);
|
2019-05-28 06:47:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
if self.index == std::ptr::null_mut() {
|
|
|
|
bail!("cannot write to closed index file.");
|
|
|
|
}
|
2019-05-29 05:02:45 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let index_pos = index * 32;
|
2018-12-15 13:51:05 +00:00
|
|
|
unsafe {
|
|
|
|
let dst = self.index.add(index_pos);
|
|
|
|
dst.copy_from_nonoverlapping(digest.as_ptr(), 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-06-23 12:43:10 +00:00
|
|
|
|
|
|
|
pub fn clone_data_from(&mut self, reader: &FixedIndexReader) -> Result<(), Error> {
|
|
|
|
if self.index_length != reader.index_count() {
|
|
|
|
bail!("clone_data_from failed - index sizes not equal");
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..self.index_length {
|
|
|
|
self.add_digest(i, reader.index_digest(i).unwrap())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-15 13:51:05 +00:00
|
|
|
}
|