drop uuid crate dependency
proxmox::tools now has a Uuid module using the native libuuid. Adds build dependency: libuuid1 (which is a Pre-Depends of util-linux, so always installed anyway). Drops uuid + 16 more crate dependencies. Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
58ea88c8ab
commit
f569acc5e2
|
@ -44,7 +44,6 @@ tokio-net = { version = "0.2.0-alpha.4", features = ["signal"] }
|
||||||
tokio-openssl = "0.4.0-alpha.2"
|
tokio-openssl = "0.4.0-alpha.2"
|
||||||
tower-service = "0.3.0-alpha.1"
|
tower-service = "0.3.0-alpha.1"
|
||||||
url = "1.7"
|
url = "1.7"
|
||||||
uuid = { version = "0.7", features = ["v4"] }
|
|
||||||
valgrind_request = { version = "1.1", optional = true }
|
valgrind_request = { version = "1.1", optional = true }
|
||||||
walkdir = "2"
|
walkdir = "2"
|
||||||
xdg = "2.2"
|
xdg = "2.2"
|
||||||
|
|
|
@ -5,14 +5,15 @@ Maintainer: Proxmox Support Team <support@proxmox.com>
|
||||||
Build-Depends: bash-completion,
|
Build-Depends: bash-completion,
|
||||||
clang,
|
clang,
|
||||||
debhelper (>= 10),
|
debhelper (>= 10),
|
||||||
|
latexmk,
|
||||||
libfuse3-dev,
|
libfuse3-dev,
|
||||||
libpam0g-dev,
|
libpam0g-dev,
|
||||||
libsystemd-dev,
|
libsystemd-dev,
|
||||||
|
libuuid1,
|
||||||
libzstd-dev (>= 1.3.8),
|
libzstd-dev (>= 1.3.8),
|
||||||
pkg-config,
|
pkg-config,
|
||||||
python3-docutils,
|
python3-docutils,
|
||||||
python3-sphinx,
|
python3-sphinx,
|
||||||
latexmk,
|
|
||||||
Standards-Version: 3.9.5
|
Standards-Version: 3.9.5
|
||||||
Homepage: https://www.proxmox.com
|
Homepage: https://www.proxmox.com
|
||||||
|
|
||||||
|
|
|
@ -6,16 +6,16 @@ use std::path::{Path, PathBuf};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use failure::*;
|
use failure::*;
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
use proxmox::tools::io::ReadExt;
|
use proxmox::tools::io::ReadExt;
|
||||||
|
use proxmox::tools::uuid::Uuid;
|
||||||
use proxmox::tools::vec;
|
use proxmox::tools::vec;
|
||||||
|
|
||||||
use super::Chunker;
|
|
||||||
use super::IndexFile;
|
|
||||||
use super::chunk_stat::ChunkStat;
|
use super::chunk_stat::ChunkStat;
|
||||||
use super::chunk_store::ChunkStore;
|
use super::chunk_store::ChunkStore;
|
||||||
use super::read_chunk::ReadChunk;
|
use super::read_chunk::ReadChunk;
|
||||||
|
use super::Chunker;
|
||||||
|
use super::IndexFile;
|
||||||
use super::{DataBlob, DataChunkBuilder};
|
use super::{DataBlob, DataChunkBuilder};
|
||||||
use crate::tools;
|
use crate::tools;
|
||||||
|
|
||||||
|
@ -52,7 +52,6 @@ unsafe impl Send for DynamicIndexReader {}
|
||||||
unsafe impl Sync for DynamicIndexReader {}
|
unsafe impl Sync for DynamicIndexReader {}
|
||||||
|
|
||||||
impl Drop for DynamicIndexReader {
|
impl Drop for DynamicIndexReader {
|
||||||
|
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Err(err) = self.unmap() {
|
if let Err(err) = self.unmap() {
|
||||||
eprintln!("Unable to unmap dynamic index - {}", err);
|
eprintln!("Unable to unmap dynamic index - {}", err);
|
||||||
|
@ -61,9 +60,7 @@ impl Drop for DynamicIndexReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DynamicIndexReader {
|
impl DynamicIndexReader {
|
||||||
|
|
||||||
pub fn open(path: &Path) -> Result<Self, Error> {
|
pub fn open(path: &Path) -> Result<Self, Error> {
|
||||||
|
|
||||||
File::open(path)
|
File::open(path)
|
||||||
.map_err(Error::from)
|
.map_err(Error::from)
|
||||||
.and_then(|file| Self::new(file))
|
.and_then(|file| Self::new(file))
|
||||||
|
@ -71,8 +68,9 @@ impl DynamicIndexReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(mut file: std::fs::File) -> Result<Self, Error> {
|
pub fn new(mut file: std::fs::File) -> Result<Self, Error> {
|
||||||
|
if let Err(err) =
|
||||||
if let Err(err) = nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockSharedNonblock) {
|
nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockSharedNonblock)
|
||||||
|
{
|
||||||
bail!("unable to get shared lock - {}", err);
|
bail!("unable to get shared lock - {}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,19 +97,22 @@ impl DynamicIndexReader {
|
||||||
bail!("got unexpected file size");
|
bail!("got unexpected file size");
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = unsafe { nix::sys::mman::mmap(
|
let data = unsafe {
|
||||||
std::ptr::null_mut(),
|
nix::sys::mman::mmap(
|
||||||
index_size,
|
std::ptr::null_mut(),
|
||||||
nix::sys::mman::ProtFlags::PROT_READ,
|
index_size,
|
||||||
nix::sys::mman::MapFlags::MAP_PRIVATE,
|
nix::sys::mman::ProtFlags::PROT_READ,
|
||||||
rawfd,
|
nix::sys::mman::MapFlags::MAP_PRIVATE,
|
||||||
header_size as i64) }? as *const u8;
|
rawfd,
|
||||||
|
header_size as i64,
|
||||||
|
)
|
||||||
|
}? as *const u8;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
_file: file,
|
_file: file,
|
||||||
size,
|
size,
|
||||||
index: data,
|
index: data,
|
||||||
index_entries: index_size/40,
|
index_entries: index_size / 40,
|
||||||
ctime,
|
ctime,
|
||||||
uuid: header.uuid,
|
uuid: header.uuid,
|
||||||
index_csum: header.index_csum,
|
index_csum: header.index_csum,
|
||||||
|
@ -119,10 +120,13 @@ impl DynamicIndexReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unmap(&mut self) -> Result<(), Error> {
|
fn unmap(&mut self) -> Result<(), Error> {
|
||||||
|
if self.index == std::ptr::null_mut() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
if self.index == std::ptr::null_mut() { return Ok(()); }
|
if let Err(err) = unsafe {
|
||||||
|
nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, self.index_entries * 40)
|
||||||
if let Err(err) = unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, self.index_entries*40) } {
|
} {
|
||||||
bail!("unmap dynamic index failed - {}", err);
|
bail!("unmap dynamic index failed - {}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,15 +143,15 @@ impl DynamicIndexReader {
|
||||||
let start = if pos == 0 {
|
let start = if pos == 0 {
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
unsafe { *(self.index.add((pos-1)*40) as *const u64) }
|
unsafe { *(self.index.add((pos - 1) * 40) as *const u64) }
|
||||||
};
|
};
|
||||||
|
|
||||||
let end = unsafe { *(self.index.add(pos*40) as *const u64) };
|
let end = unsafe { *(self.index.add(pos * 40) as *const u64) };
|
||||||
|
|
||||||
let mut digest = std::mem::MaybeUninit::<[u8; 32]>::uninit();
|
let mut digest = std::mem::MaybeUninit::<[u8; 32]>::uninit();
|
||||||
unsafe {
|
unsafe {
|
||||||
std::ptr::copy_nonoverlapping(
|
std::ptr::copy_nonoverlapping(
|
||||||
self.index.add(pos*40+8),
|
self.index.add(pos * 40 + 8),
|
||||||
(*digest.as_mut_ptr()).as_mut_ptr(),
|
(*digest.as_mut_ptr()).as_mut_ptr(),
|
||||||
32,
|
32,
|
||||||
);
|
);
|
||||||
|
@ -162,7 +166,7 @@ impl DynamicIndexReader {
|
||||||
if pos >= self.index_entries {
|
if pos >= self.index_entries {
|
||||||
panic!("chunk index out of range");
|
panic!("chunk index out of range");
|
||||||
}
|
}
|
||||||
unsafe { *(self.index.add(pos*40) as *const u64) }
|
unsafe { *(self.index.add(pos * 40) as *const u64) }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -170,13 +174,12 @@ impl DynamicIndexReader {
|
||||||
if pos >= self.index_entries {
|
if pos >= self.index_entries {
|
||||||
panic!("chunk index out of range");
|
panic!("chunk index out of range");
|
||||||
}
|
}
|
||||||
let slice = unsafe { std::slice::from_raw_parts(self.index.add(pos*40+8), 32) };
|
let slice = unsafe { std::slice::from_raw_parts(self.index.add(pos * 40 + 8), 32) };
|
||||||
slice.try_into().unwrap()
|
slice.try_into().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compute checksum and data size
|
/// Compute checksum and data size
|
||||||
pub fn compute_csum(&self) -> ([u8; 32], u64) {
|
pub fn compute_csum(&self) -> ([u8; 32], u64) {
|
||||||
|
|
||||||
let mut csum = openssl::sha::Sha256::new();
|
let mut csum = openssl::sha::Sha256::new();
|
||||||
let mut chunk_end = 0;
|
let mut chunk_end = 0;
|
||||||
for pos in 0..self.index_entries {
|
for pos in 0..self.index_entries {
|
||||||
|
@ -213,9 +216,8 @@ impl DynamicIndexReader {
|
||||||
start: u64,
|
start: u64,
|
||||||
end_idx: usize,
|
end_idx: usize,
|
||||||
end: u64,
|
end: u64,
|
||||||
offset: u64
|
offset: u64,
|
||||||
) -> Result<usize, Error> {
|
) -> Result<usize, Error> {
|
||||||
|
|
||||||
if (offset >= end) || (offset < start) {
|
if (offset >= end) || (offset < start) {
|
||||||
bail!("offset out of range");
|
bail!("offset out of range");
|
||||||
}
|
}
|
||||||
|
@ -223,7 +225,7 @@ impl DynamicIndexReader {
|
||||||
if end_idx == start_idx {
|
if end_idx == start_idx {
|
||||||
return Ok(start_idx); // found
|
return Ok(start_idx); // found
|
||||||
}
|
}
|
||||||
let middle_idx = (start_idx + end_idx)/2;
|
let middle_idx = (start_idx + end_idx) / 2;
|
||||||
let middle_end = self.chunk_end(middle_idx);
|
let middle_end = self.chunk_end(middle_idx);
|
||||||
|
|
||||||
if offset < middle_end {
|
if offset < middle_end {
|
||||||
|
@ -243,9 +245,7 @@ impl IndexFile for DynamicIndexReader {
|
||||||
if pos >= self.index_entries {
|
if pos >= self.index_entries {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(unsafe {
|
Some(unsafe { std::mem::transmute(self.chunk_digest(pos).as_ptr()) })
|
||||||
std::mem::transmute(self.chunk_digest(pos).as_ptr())
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -268,26 +268,25 @@ pub struct BufferedDynamicReader<S> {
|
||||||
read_offset: u64,
|
read_offset: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <S: ReadChunk> BufferedDynamicReader<S> {
|
impl<S: ReadChunk> BufferedDynamicReader<S> {
|
||||||
|
|
||||||
pub fn new(index: DynamicIndexReader, store: S) -> Self {
|
pub fn new(index: DynamicIndexReader, store: S) -> Self {
|
||||||
|
|
||||||
let archive_size = index.chunk_end(index.index_entries - 1);
|
let archive_size = index.chunk_end(index.index_entries - 1);
|
||||||
Self {
|
Self {
|
||||||
store,
|
store,
|
||||||
index,
|
index,
|
||||||
archive_size,
|
archive_size,
|
||||||
read_buffer: Vec::with_capacity(1024*1024),
|
read_buffer: Vec::with_capacity(1024 * 1024),
|
||||||
buffered_chunk_idx: 0,
|
buffered_chunk_idx: 0,
|
||||||
buffered_chunk_start: 0,
|
buffered_chunk_start: 0,
|
||||||
read_offset: 0,
|
read_offset: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn archive_size(&self) -> u64 { self.archive_size }
|
pub fn archive_size(&self) -> u64 {
|
||||||
|
self.archive_size
|
||||||
|
}
|
||||||
|
|
||||||
fn buffer_chunk(&mut self, idx: usize) -> Result<(), Error> {
|
fn buffer_chunk(&mut self, idx: usize) -> Result<(), Error> {
|
||||||
|
|
||||||
let index = &self.index;
|
let index = &self.index;
|
||||||
let (start, end, digest) = index.chunk_info(idx)?;
|
let (start, end, digest) = index.chunk_info(idx)?;
|
||||||
|
|
||||||
|
@ -295,8 +294,12 @@ impl <S: ReadChunk> BufferedDynamicReader<S> {
|
||||||
|
|
||||||
let data = self.store.read_chunk(&digest)?;
|
let data = self.store.read_chunk(&digest)?;
|
||||||
|
|
||||||
if (end - start) != data.len() as u64 {
|
if (end - start) != data.len() as u64 {
|
||||||
bail!("read chunk with wrong size ({} != {}", (end - start), data.len());
|
bail!(
|
||||||
|
"read chunk with wrong size ({} != {}",
|
||||||
|
(end - start),
|
||||||
|
data.len()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.read_buffer.clear();
|
self.read_buffer.clear();
|
||||||
|
@ -310,19 +313,19 @@ impl <S: ReadChunk> BufferedDynamicReader<S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <S: ReadChunk> crate::tools::BufferedRead for BufferedDynamicReader<S> {
|
impl<S: ReadChunk> crate::tools::BufferedRead for BufferedDynamicReader<S> {
|
||||||
|
|
||||||
fn buffered_read(&mut self, offset: u64) -> Result<&[u8], Error> {
|
fn buffered_read(&mut self, offset: u64) -> Result<&[u8], Error> {
|
||||||
|
if offset == self.archive_size {
|
||||||
if offset == self.archive_size { return Ok(&self.read_buffer[0..0]); }
|
return Ok(&self.read_buffer[0..0]);
|
||||||
|
}
|
||||||
|
|
||||||
let buffer_len = self.read_buffer.len();
|
let buffer_len = self.read_buffer.len();
|
||||||
let index = &self.index;
|
let index = &self.index;
|
||||||
|
|
||||||
// optimization for sequential read
|
// optimization for sequential read
|
||||||
if buffer_len > 0 &&
|
if buffer_len > 0
|
||||||
((self.buffered_chunk_idx + 1) < index.index_entries) &&
|
&& ((self.buffered_chunk_idx + 1) < index.index_entries)
|
||||||
(offset >= (self.buffered_chunk_start + (self.read_buffer.len() as u64)))
|
&& (offset >= (self.buffered_chunk_start + (self.read_buffer.len() as u64)))
|
||||||
{
|
{
|
||||||
let next_idx = self.buffered_chunk_idx + 1;
|
let next_idx = self.buffered_chunk_idx + 1;
|
||||||
let next_end = index.chunk_end(next_idx);
|
let next_end = index.chunk_end(next_idx);
|
||||||
|
@ -333,36 +336,40 @@ impl <S: ReadChunk> crate::tools::BufferedRead for BufferedDynamicReader<S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer_len == 0) ||
|
if (buffer_len == 0)
|
||||||
(offset < self.buffered_chunk_start) ||
|
|| (offset < self.buffered_chunk_start)
|
||||||
(offset >= (self.buffered_chunk_start + (self.read_buffer.len() as u64)))
|
|| (offset >= (self.buffered_chunk_start + (self.read_buffer.len() as u64)))
|
||||||
{
|
{
|
||||||
let end_idx = index.index_entries - 1;
|
let end_idx = index.index_entries - 1;
|
||||||
let end = index.chunk_end(end_idx);
|
let end = index.chunk_end(end_idx);
|
||||||
let idx = index.binary_search(0, 0, end_idx, end, offset)?;
|
let idx = index.binary_search(0, 0, end_idx, end, offset)?;
|
||||||
self.buffer_chunk(idx)?;
|
self.buffer_chunk(idx)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let buffer_offset = (offset - self.buffered_chunk_start) as usize;
|
let buffer_offset = (offset - self.buffered_chunk_start) as usize;
|
||||||
Ok(&self.read_buffer[buffer_offset..])
|
Ok(&self.read_buffer[buffer_offset..])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <S: ReadChunk> std::io::Read for BufferedDynamicReader<S> {
|
impl<S: ReadChunk> std::io::Read for BufferedDynamicReader<S> {
|
||||||
|
|
||||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
|
||||||
|
|
||||||
use std::io::{Error, ErrorKind};
|
|
||||||
use crate::tools::BufferedRead;
|
use crate::tools::BufferedRead;
|
||||||
|
use std::io::{Error, ErrorKind};
|
||||||
|
|
||||||
let data = match self.buffered_read(self.read_offset) {
|
let data = match self.buffered_read(self.read_offset) {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(err) => return Err(Error::new(ErrorKind::Other, err.to_string())),
|
Err(err) => return Err(Error::new(ErrorKind::Other, err.to_string())),
|
||||||
};
|
};
|
||||||
|
|
||||||
let n = if data.len() > buf.len() { buf.len() } else { data.len() };
|
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); }
|
unsafe {
|
||||||
|
std::ptr::copy_nonoverlapping(data.as_ptr(), buf.as_mut_ptr(), n);
|
||||||
|
}
|
||||||
|
|
||||||
self.read_offset += n as u64;
|
self.read_offset += n as u64;
|
||||||
|
|
||||||
|
@ -370,13 +377,11 @@ impl <S: ReadChunk> std::io::Read for BufferedDynamicReader<S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <S: ReadChunk> std::io::Seek for BufferedDynamicReader<S> {
|
impl<S: ReadChunk> std::io::Seek for BufferedDynamicReader<S> {
|
||||||
|
|
||||||
fn seek(&mut self, pos: SeekFrom) -> Result<u64, std::io::Error> {
|
fn seek(&mut self, pos: SeekFrom) -> Result<u64, std::io::Error> {
|
||||||
|
|
||||||
let new_offset = match pos {
|
let new_offset = match pos {
|
||||||
SeekFrom::Start(start_offset) => start_offset as i64,
|
SeekFrom::Start(start_offset) => start_offset as i64,
|
||||||
SeekFrom::End(end_offset) => (self.archive_size as i64)+ end_offset,
|
SeekFrom::End(end_offset) => (self.archive_size as i64) + end_offset,
|
||||||
SeekFrom::Current(offset) => (self.read_offset as i64) + offset,
|
SeekFrom::Current(offset) => (self.read_offset as i64) + offset,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -384,7 +389,11 @@ impl <S: ReadChunk> std::io::Seek for BufferedDynamicReader<S> {
|
||||||
if (new_offset < 0) || (new_offset > (self.archive_size as i64)) {
|
if (new_offset < 0) || (new_offset > (self.archive_size as i64)) {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
ErrorKind::Other,
|
ErrorKind::Other,
|
||||||
format!("seek is out of range {} ([0..{}])", new_offset, self.archive_size)));
|
format!(
|
||||||
|
"seek is out of range {} ([0..{}])",
|
||||||
|
new_offset, self.archive_size
|
||||||
|
),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
self.read_offset = new_offset as u64;
|
self.read_offset = new_offset as u64;
|
||||||
|
|
||||||
|
@ -406,16 +415,13 @@ pub struct DynamicIndexWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for DynamicIndexWriter {
|
impl Drop for DynamicIndexWriter {
|
||||||
|
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
let _ = std::fs::remove_file(&self.tmp_filename); // ignore errors
|
let _ = std::fs::remove_file(&self.tmp_filename); // ignore errors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DynamicIndexWriter {
|
impl DynamicIndexWriter {
|
||||||
|
|
||||||
pub fn create(store: Arc<ChunkStore>, path: &Path) -> Result<Self, Error> {
|
pub fn create(store: Arc<ChunkStore>, path: &Path) -> Result<Self, Error> {
|
||||||
|
|
||||||
let shared_lock = store.try_shared_lock()?;
|
let shared_lock = store.try_shared_lock()?;
|
||||||
|
|
||||||
let full_path = store.relative_path(path);
|
let full_path = store.relative_path(path);
|
||||||
|
@ -423,22 +429,26 @@ impl DynamicIndexWriter {
|
||||||
tmp_path.set_extension("tmp_didx");
|
tmp_path.set_extension("tmp_didx");
|
||||||
|
|
||||||
let file = std::fs::OpenOptions::new()
|
let file = std::fs::OpenOptions::new()
|
||||||
.create(true).truncate(true)
|
.create(true)
|
||||||
|
.truncate(true)
|
||||||
.read(true)
|
.read(true)
|
||||||
.write(true)
|
.write(true)
|
||||||
.open(&tmp_path)?;
|
.open(&tmp_path)?;
|
||||||
|
|
||||||
let mut writer = BufWriter::with_capacity(1024*1024, file);
|
let mut writer = BufWriter::with_capacity(1024 * 1024, file);
|
||||||
|
|
||||||
let header_size = std::mem::size_of::<DynamicIndexHeader>();
|
let header_size = std::mem::size_of::<DynamicIndexHeader>();
|
||||||
|
|
||||||
// todo: use static assertion when available in rust
|
// todo: use static assertion when available in rust
|
||||||
if header_size != 4096 { panic!("got unexpected header size"); }
|
if header_size != 4096 {
|
||||||
|
panic!("got unexpected header size");
|
||||||
|
}
|
||||||
|
|
||||||
let ctime = std::time::SystemTime::now().duration_since(
|
let ctime = std::time::SystemTime::now()
|
||||||
std::time::SystemTime::UNIX_EPOCH)?.as_secs();
|
.duration_since(std::time::SystemTime::UNIX_EPOCH)?
|
||||||
|
.as_secs();
|
||||||
|
|
||||||
let uuid = Uuid::new_v4();
|
let uuid = Uuid::generate();
|
||||||
|
|
||||||
let mut buffer = vec::zeroed(header_size);
|
let mut buffer = vec::zeroed(header_size);
|
||||||
let header = crate::tools::map_struct_mut::<DynamicIndexHeader>(&mut buffer)?;
|
let header = crate::tools::map_struct_mut::<DynamicIndexHeader>(&mut buffer)?;
|
||||||
|
@ -471,10 +481,12 @@ impl DynamicIndexWriter {
|
||||||
self.store.insert_chunk(chunk, digest)
|
self.store.insert_chunk(chunk, digest)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close(&mut self) -> Result<[u8; 32], Error> {
|
pub fn close(&mut self) -> Result<[u8; 32], Error> {
|
||||||
|
|
||||||
if self.closed {
|
if self.closed {
|
||||||
bail!("cannot close already closed archive index file {:?}", self.filename);
|
bail!(
|
||||||
|
"cannot close already closed archive index file {:?}",
|
||||||
|
self.filename
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.closed = true;
|
self.closed = true;
|
||||||
|
@ -490,7 +502,6 @@ impl DynamicIndexWriter {
|
||||||
self.writer.write_all(&index_csum)?;
|
self.writer.write_all(&index_csum)?;
|
||||||
self.writer.flush()?;
|
self.writer.flush()?;
|
||||||
|
|
||||||
|
|
||||||
if let Err(err) = std::fs::rename(&self.tmp_filename, &self.filename) {
|
if let Err(err) = std::fs::rename(&self.tmp_filename, &self.filename) {
|
||||||
bail!("Atomic rename file {:?} failed - {}", self.filename, err);
|
bail!("Atomic rename file {:?} failed - {}", self.filename, err);
|
||||||
}
|
}
|
||||||
|
@ -501,10 +512,13 @@ impl DynamicIndexWriter {
|
||||||
// fixme: rename to add_digest
|
// fixme: rename to add_digest
|
||||||
pub fn add_chunk(&mut self, offset: u64, digest: &[u8; 32]) -> Result<(), Error> {
|
pub fn add_chunk(&mut self, offset: u64, digest: &[u8; 32]) -> Result<(), Error> {
|
||||||
if self.closed {
|
if self.closed {
|
||||||
bail!("cannot write to closed dynamic index file {:?}", self.filename);
|
bail!(
|
||||||
|
"cannot write to closed dynamic index file {:?}",
|
||||||
|
self.filename
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let offset_le: &[u8; 8] = unsafe { &std::mem::transmute::<u64, [u8;8]>(offset.to_le()) };
|
let offset_le: &[u8; 8] = unsafe { &std::mem::transmute::<u64, [u8; 8]>(offset.to_le()) };
|
||||||
|
|
||||||
if let Some(ref mut csum) = self.csum {
|
if let Some(ref mut csum) = self.csum {
|
||||||
csum.update(offset_le);
|
csum.update(offset_le);
|
||||||
|
@ -531,7 +545,6 @@ pub struct DynamicChunkWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DynamicChunkWriter {
|
impl DynamicChunkWriter {
|
||||||
|
|
||||||
pub fn new(index: DynamicIndexWriter, chunk_size: usize) -> Self {
|
pub fn new(index: DynamicIndexWriter, chunk_size: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
index,
|
index,
|
||||||
|
@ -540,7 +553,7 @@ impl DynamicChunkWriter {
|
||||||
stat: ChunkStat::new(0),
|
stat: ChunkStat::new(0),
|
||||||
chunk_offset: 0,
|
chunk_offset: 0,
|
||||||
last_chunk: 0,
|
last_chunk: 0,
|
||||||
chunk_buffer: Vec::with_capacity(chunk_size*4),
|
chunk_buffer: Vec::with_capacity(chunk_size * 4),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -548,8 +561,7 @@ impl DynamicChunkWriter {
|
||||||
&self.stat
|
&self.stat
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close(&mut self) -> Result<(), Error> {
|
pub fn close(&mut self) -> Result<(), Error> {
|
||||||
|
|
||||||
if self.closed {
|
if self.closed {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
@ -563,16 +575,18 @@ impl DynamicChunkWriter {
|
||||||
self.stat.size = self.chunk_offset as u64;
|
self.stat.size = self.chunk_offset as u64;
|
||||||
|
|
||||||
// add size of index file
|
// add size of index file
|
||||||
self.stat.size += (self.stat.chunk_count*40 + std::mem::size_of::<DynamicIndexHeader>()) as u64;
|
self.stat.size +=
|
||||||
|
(self.stat.chunk_count * 40 + std::mem::size_of::<DynamicIndexHeader>()) as u64;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_chunk_buffer(&mut self) -> Result<(), Error> {
|
fn write_chunk_buffer(&mut self) -> Result<(), Error> {
|
||||||
|
|
||||||
let chunk_size = self.chunk_buffer.len();
|
let chunk_size = self.chunk_buffer.len();
|
||||||
|
|
||||||
if chunk_size == 0 { return Ok(()); }
|
if chunk_size == 0 {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
let expected_chunk_size = self.chunk_offset - self.last_chunk;
|
let expected_chunk_size = self.chunk_offset - self.last_chunk;
|
||||||
if expected_chunk_size != self.chunk_buffer.len() {
|
if expected_chunk_size != self.chunk_buffer.len() {
|
||||||
|
@ -589,7 +603,6 @@ impl DynamicChunkWriter {
|
||||||
|
|
||||||
match self.index.insert_chunk(&chunk, &digest) {
|
match self.index.insert_chunk(&chunk, &digest) {
|
||||||
Ok((is_duplicate, compressed_size)) => {
|
Ok((is_duplicate, compressed_size)) => {
|
||||||
|
|
||||||
self.stat.compressed_size += compressed_size;
|
self.stat.compressed_size += compressed_size;
|
||||||
if is_duplicate {
|
if is_duplicate {
|
||||||
self.stat.duplicate_chunks += 1;
|
self.stat.duplicate_chunks += 1;
|
||||||
|
@ -597,8 +610,14 @@ impl DynamicChunkWriter {
|
||||||
self.stat.disk_size += compressed_size;
|
self.stat.disk_size += compressed_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("ADD CHUNK {:016x} {} {}% {} {}", self.chunk_offset, chunk_size,
|
println!(
|
||||||
(compressed_size*100)/(chunk_size as u64), is_duplicate, proxmox::tools::digest_to_hex(&digest));
|
"ADD CHUNK {:016x} {} {}% {} {}",
|
||||||
|
self.chunk_offset,
|
||||||
|
chunk_size,
|
||||||
|
(compressed_size * 100) / (chunk_size as u64),
|
||||||
|
is_duplicate,
|
||||||
|
proxmox::tools::digest_to_hex(&digest)
|
||||||
|
);
|
||||||
self.index.add_chunk(self.chunk_offset as u64, &digest)?;
|
self.index.add_chunk(self.chunk_offset as u64, &digest)?;
|
||||||
self.chunk_buffer.truncate(0);
|
self.chunk_buffer.truncate(0);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -612,9 +631,7 @@ impl DynamicChunkWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Write for DynamicChunkWriter {
|
impl Write for DynamicChunkWriter {
|
||||||
|
|
||||||
fn write(&mut self, data: &[u8]) -> std::result::Result<usize, std::io::Error> {
|
fn write(&mut self, data: &[u8]) -> std::result::Result<usize, std::io::Error> {
|
||||||
|
|
||||||
let chunker = &mut self.chunker;
|
let chunker = &mut self.chunker;
|
||||||
|
|
||||||
let pos = chunker.scan(data);
|
let pos = chunker.scan(data);
|
||||||
|
@ -624,10 +641,12 @@ impl Write for DynamicChunkWriter {
|
||||||
self.chunk_offset += pos;
|
self.chunk_offset += pos;
|
||||||
|
|
||||||
if let Err(err) = self.write_chunk_buffer() {
|
if let Err(err) = self.write_chunk_buffer() {
|
||||||
return Err(std::io::Error::new(std::io::ErrorKind::Other, err.to_string()));
|
return Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::Other,
|
||||||
|
err.to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
Ok(pos)
|
Ok(pos)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
self.chunk_offset += data.len();
|
self.chunk_offset += data.len();
|
||||||
self.chunk_buffer.extend_from_slice(data);
|
self.chunk_buffer.extend_from_slice(data);
|
||||||
|
@ -636,6 +655,9 @@ impl Write for DynamicChunkWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) -> std::result::Result<(), std::io::Error> {
|
fn flush(&mut self) -> std::result::Result<(), std::io::Error> {
|
||||||
Err(std::io::Error::new(std::io::ErrorKind::Other, "please use close() instead of flush()"))
|
Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::Other,
|
||||||
|
"please use close() instead of flush()",
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +1,24 @@
|
||||||
use failure::*;
|
use failure::*;
|
||||||
use std::io::{Seek, SeekFrom};
|
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
use std::io::{Seek, SeekFrom};
|
||||||
|
|
||||||
use crate::tools;
|
|
||||||
use super::IndexFile;
|
|
||||||
use super::chunk_stat::*;
|
use super::chunk_stat::*;
|
||||||
use super::chunk_store::*;
|
use super::chunk_store::*;
|
||||||
|
use super::IndexFile;
|
||||||
|
use crate::tools;
|
||||||
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::io::Write;
|
|
||||||
use std::fs::File;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::os::unix::io::AsRawFd;
|
|
||||||
use uuid::Uuid;
|
|
||||||
use chrono::{Local, TimeZone};
|
use chrono::{Local, TimeZone};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::os::unix::io::AsRawFd;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use super::ChunkInfo;
|
|
||||||
use super::read_chunk::*;
|
use super::read_chunk::*;
|
||||||
|
use super::ChunkInfo;
|
||||||
|
|
||||||
use proxmox::tools::io::ReadExt;
|
use proxmox::tools::io::ReadExt;
|
||||||
|
use proxmox::tools::Uuid;
|
||||||
|
|
||||||
/// Header format definition for fixed index files (`.fidx`)
|
/// Header format definition for fixed index files (`.fidx`)
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
|
@ -52,7 +52,6 @@ unsafe impl Send for FixedIndexReader {}
|
||||||
unsafe impl Sync for FixedIndexReader {}
|
unsafe impl Sync for FixedIndexReader {}
|
||||||
|
|
||||||
impl Drop for FixedIndexReader {
|
impl Drop for FixedIndexReader {
|
||||||
|
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Err(err) = self.unmap() {
|
if let Err(err) = self.unmap() {
|
||||||
eprintln!("Unable to unmap file - {}", err);
|
eprintln!("Unable to unmap file - {}", err);
|
||||||
|
@ -61,9 +60,7 @@ impl Drop for FixedIndexReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FixedIndexReader {
|
impl FixedIndexReader {
|
||||||
|
|
||||||
pub fn open(path: &Path) -> Result<Self, Error> {
|
pub fn open(path: &Path) -> Result<Self, Error> {
|
||||||
|
|
||||||
File::open(path)
|
File::open(path)
|
||||||
.map_err(Error::from)
|
.map_err(Error::from)
|
||||||
.and_then(|file| Self::new(file))
|
.and_then(|file| Self::new(file))
|
||||||
|
@ -71,8 +68,9 @@ impl FixedIndexReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(mut file: std::fs::File) -> Result<Self, Error> {
|
pub fn new(mut file: std::fs::File) -> Result<Self, Error> {
|
||||||
|
if let Err(err) =
|
||||||
if let Err(err) = nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockSharedNonblock) {
|
nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockSharedNonblock)
|
||||||
|
{
|
||||||
bail!("unable to get shared lock - {}", err);
|
bail!("unable to get shared lock - {}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,8 +87,8 @@ impl FixedIndexReader {
|
||||||
let ctime = u64::from_le(header.ctime);
|
let ctime = u64::from_le(header.ctime);
|
||||||
let chunk_size = u64::from_le(header.chunk_size);
|
let chunk_size = u64::from_le(header.chunk_size);
|
||||||
|
|
||||||
let index_length = ((size + chunk_size - 1)/chunk_size) as usize;
|
let index_length = ((size + chunk_size - 1) / chunk_size) as usize;
|
||||||
let index_size = index_length*32;
|
let index_size = index_length * 32;
|
||||||
|
|
||||||
let rawfd = file.as_raw_fd();
|
let rawfd = file.as_raw_fd();
|
||||||
|
|
||||||
|
@ -101,16 +99,23 @@ impl FixedIndexReader {
|
||||||
|
|
||||||
let expected_index_size = (stat.st_size as usize) - header_size;
|
let expected_index_size = (stat.st_size as usize) - header_size;
|
||||||
if index_size != expected_index_size {
|
if index_size != expected_index_size {
|
||||||
bail!("got unexpected file size ({} != {})", index_size, expected_index_size);
|
bail!(
|
||||||
|
"got unexpected file size ({} != {})",
|
||||||
|
index_size,
|
||||||
|
expected_index_size
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = unsafe { nix::sys::mman::mmap(
|
let data = unsafe {
|
||||||
std::ptr::null_mut(),
|
nix::sys::mman::mmap(
|
||||||
index_size,
|
std::ptr::null_mut(),
|
||||||
nix::sys::mman::ProtFlags::PROT_READ,
|
index_size,
|
||||||
nix::sys::mman::MapFlags::MAP_PRIVATE,
|
nix::sys::mman::ProtFlags::PROT_READ,
|
||||||
file.as_raw_fd(),
|
nix::sys::mman::MapFlags::MAP_PRIVATE,
|
||||||
header_size as i64) }? as *mut u8;
|
file.as_raw_fd(),
|
||||||
|
header_size as i64,
|
||||||
|
)
|
||||||
|
}? as *mut u8;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
_file: file,
|
_file: file,
|
||||||
|
@ -125,12 +130,15 @@ impl FixedIndexReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unmap(&mut self) -> Result<(), Error> {
|
fn unmap(&mut self) -> Result<(), Error> {
|
||||||
|
if self.index == std::ptr::null_mut() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
if self.index == std::ptr::null_mut() { return Ok(()); }
|
let index_size = self.index_length * 32;
|
||||||
|
|
||||||
let index_size = self.index_length*32;
|
if let Err(err) =
|
||||||
|
unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) }
|
||||||
if let Err(err) = unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) } {
|
{
|
||||||
bail!("unmap file failed - {}", err);
|
bail!("unmap file failed - {}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,7 +148,6 @@ impl FixedIndexReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn chunk_info(&self, pos: usize) -> Result<(u64, u64, [u8; 32]), Error> {
|
pub fn chunk_info(&self, pos: usize) -> Result<(u64, u64, [u8; 32]), Error> {
|
||||||
|
|
||||||
if pos >= self.index_length {
|
if pos >= self.index_length {
|
||||||
bail!("chunk index out of range");
|
bail!("chunk index out of range");
|
||||||
}
|
}
|
||||||
|
@ -154,7 +161,7 @@ impl FixedIndexReader {
|
||||||
let mut digest = std::mem::MaybeUninit::<[u8; 32]>::uninit();
|
let mut digest = std::mem::MaybeUninit::<[u8; 32]>::uninit();
|
||||||
unsafe {
|
unsafe {
|
||||||
std::ptr::copy_nonoverlapping(
|
std::ptr::copy_nonoverlapping(
|
||||||
self.index.add(pos*32),
|
self.index.add(pos * 32),
|
||||||
(*digest.as_mut_ptr()).as_mut_ptr(),
|
(*digest.as_mut_ptr()).as_mut_ptr(),
|
||||||
32,
|
32,
|
||||||
);
|
);
|
||||||
|
@ -168,7 +175,7 @@ impl FixedIndexReader {
|
||||||
if pos >= self.index_length {
|
if pos >= self.index_length {
|
||||||
panic!("chunk index out of range");
|
panic!("chunk index out of range");
|
||||||
}
|
}
|
||||||
let slice = unsafe { std::slice::from_raw_parts(self.index.add(pos*32), 32) };
|
let slice = unsafe { std::slice::from_raw_parts(self.index.add(pos * 32), 32) };
|
||||||
slice.try_into().unwrap()
|
slice.try_into().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,7 +185,7 @@ impl FixedIndexReader {
|
||||||
panic!("chunk index out of range");
|
panic!("chunk index out of range");
|
||||||
}
|
}
|
||||||
|
|
||||||
let end = ((pos+1) * self.chunk_size) as u64;
|
let end = ((pos + 1) * self.chunk_size) as u64;
|
||||||
if end > self.size {
|
if end > self.size {
|
||||||
self.size
|
self.size
|
||||||
} else {
|
} else {
|
||||||
|
@ -188,11 +195,10 @@ impl FixedIndexReader {
|
||||||
|
|
||||||
/// Compute checksum and data size
|
/// Compute checksum and data size
|
||||||
pub fn compute_csum(&self) -> ([u8; 32], u64) {
|
pub fn compute_csum(&self) -> ([u8; 32], u64) {
|
||||||
|
|
||||||
let mut csum = openssl::sha::Sha256::new();
|
let mut csum = openssl::sha::Sha256::new();
|
||||||
let mut chunk_end = 0;
|
let mut chunk_end = 0;
|
||||||
for pos in 0..self.index_length {
|
for pos in 0..self.index_length {
|
||||||
chunk_end = ((pos+1) * self.chunk_size) as u64;
|
chunk_end = ((pos + 1) * self.chunk_size) as u64;
|
||||||
let digest = self.chunk_digest(pos);
|
let digest = self.chunk_digest(pos);
|
||||||
csum.update(digest);
|
csum.update(digest);
|
||||||
}
|
}
|
||||||
|
@ -204,7 +210,10 @@ impl FixedIndexReader {
|
||||||
pub fn print_info(&self) {
|
pub fn print_info(&self) {
|
||||||
println!("Size: {}", self.size);
|
println!("Size: {}", self.size);
|
||||||
println!("ChunkSize: {}", self.chunk_size);
|
println!("ChunkSize: {}", self.chunk_size);
|
||||||
println!("CTime: {}", Local.timestamp(self.ctime as i64, 0).format("%c"));
|
println!(
|
||||||
|
"CTime: {}",
|
||||||
|
Local.timestamp(self.ctime as i64, 0).format("%c")
|
||||||
|
);
|
||||||
println!("UUID: {:?}", self.uuid);
|
println!("UUID: {:?}", self.uuid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -218,7 +227,7 @@ impl IndexFile for FixedIndexReader {
|
||||||
if pos >= self.index_length {
|
if pos >= self.index_length {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(unsafe { std::mem::transmute(self.index.add(pos*32)) })
|
Some(unsafe { std::mem::transmute(self.index.add(pos * 32)) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,7 +254,6 @@ pub struct FixedIndexWriter {
|
||||||
unsafe impl Send for FixedIndexWriter {}
|
unsafe impl Send for FixedIndexWriter {}
|
||||||
|
|
||||||
impl Drop for FixedIndexWriter {
|
impl Drop for FixedIndexWriter {
|
||||||
|
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
let _ = std::fs::remove_file(&self.tmp_filename); // ignore errors
|
let _ = std::fs::remove_file(&self.tmp_filename); // ignore errors
|
||||||
if let Err(err) = self.unmap() {
|
if let Err(err) = self.unmap() {
|
||||||
|
@ -255,10 +263,13 @@ impl Drop for FixedIndexWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FixedIndexWriter {
|
impl FixedIndexWriter {
|
||||||
|
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
#[allow(clippy::cast_ptr_alignment)]
|
||||||
pub fn create(store: Arc<ChunkStore>, path: &Path, size: usize, chunk_size: usize) -> Result<Self, Error> {
|
pub fn create(
|
||||||
|
store: Arc<ChunkStore>,
|
||||||
|
path: &Path,
|
||||||
|
size: usize,
|
||||||
|
chunk_size: usize,
|
||||||
|
) -> Result<Self, Error> {
|
||||||
let shared_lock = store.try_shared_lock()?;
|
let shared_lock = store.try_shared_lock()?;
|
||||||
|
|
||||||
let full_path = store.relative_path(path);
|
let full_path = store.relative_path(path);
|
||||||
|
@ -266,7 +277,8 @@ impl FixedIndexWriter {
|
||||||
tmp_path.set_extension("tmp_fidx");
|
tmp_path.set_extension("tmp_fidx");
|
||||||
|
|
||||||
let mut file = std::fs::OpenOptions::new()
|
let mut file = std::fs::OpenOptions::new()
|
||||||
.create(true).truncate(true)
|
.create(true)
|
||||||
|
.truncate(true)
|
||||||
.read(true)
|
.read(true)
|
||||||
.write(true)
|
.write(true)
|
||||||
.open(&tmp_path)?;
|
.open(&tmp_path)?;
|
||||||
|
@ -274,15 +286,18 @@ impl FixedIndexWriter {
|
||||||
let header_size = std::mem::size_of::<FixedIndexHeader>();
|
let header_size = std::mem::size_of::<FixedIndexHeader>();
|
||||||
|
|
||||||
// todo: use static assertion when available in rust
|
// todo: use static assertion when available in rust
|
||||||
if header_size != 4096 { panic!("got unexpected header size"); }
|
if header_size != 4096 {
|
||||||
|
panic!("got unexpected header size");
|
||||||
|
}
|
||||||
|
|
||||||
let ctime = std::time::SystemTime::now().duration_since(
|
let ctime = std::time::SystemTime::now()
|
||||||
std::time::SystemTime::UNIX_EPOCH)?.as_secs();
|
.duration_since(std::time::SystemTime::UNIX_EPOCH)?
|
||||||
|
.as_secs();
|
||||||
|
|
||||||
let uuid = Uuid::new_v4();
|
let uuid = Uuid::generate();
|
||||||
|
|
||||||
let buffer = vec![0u8; header_size];
|
let buffer = vec![0u8; header_size];
|
||||||
let header = unsafe { &mut * (buffer.as_ptr() as *mut FixedIndexHeader) };
|
let header = unsafe { &mut *(buffer.as_ptr() as *mut FixedIndexHeader) };
|
||||||
|
|
||||||
header.magic = super::FIXED_SIZED_CHUNK_INDEX_1_0;
|
header.magic = super::FIXED_SIZED_CHUNK_INDEX_1_0;
|
||||||
header.ctime = u64::to_le(ctime);
|
header.ctime = u64::to_le(ctime);
|
||||||
|
@ -294,17 +309,20 @@ impl FixedIndexWriter {
|
||||||
|
|
||||||
file.write_all(&buffer)?;
|
file.write_all(&buffer)?;
|
||||||
|
|
||||||
let index_length = (size + chunk_size - 1)/chunk_size;
|
let index_length = (size + chunk_size - 1) / chunk_size;
|
||||||
let index_size = index_length*32;
|
let index_size = index_length * 32;
|
||||||
nix::unistd::ftruncate(file.as_raw_fd(), (header_size + index_size) as i64)?;
|
nix::unistd::ftruncate(file.as_raw_fd(), (header_size + index_size) as i64)?;
|
||||||
|
|
||||||
let data = unsafe { nix::sys::mman::mmap(
|
let data = unsafe {
|
||||||
std::ptr::null_mut(),
|
nix::sys::mman::mmap(
|
||||||
index_size,
|
std::ptr::null_mut(),
|
||||||
nix::sys::mman::ProtFlags::PROT_READ | nix::sys::mman::ProtFlags::PROT_WRITE,
|
index_size,
|
||||||
nix::sys::mman::MapFlags::MAP_SHARED,
|
nix::sys::mman::ProtFlags::PROT_READ | nix::sys::mman::ProtFlags::PROT_WRITE,
|
||||||
file.as_raw_fd(),
|
nix::sys::mman::MapFlags::MAP_SHARED,
|
||||||
header_size as i64) }? as *mut u8;
|
file.as_raw_fd(),
|
||||||
|
header_size as i64,
|
||||||
|
)
|
||||||
|
}? as *mut u8;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
store,
|
store,
|
||||||
|
@ -326,12 +344,15 @@ impl FixedIndexWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unmap(&mut self) -> Result<(), Error> {
|
fn unmap(&mut self) -> Result<(), Error> {
|
||||||
|
if self.index == std::ptr::null_mut() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
if self.index == std::ptr::null_mut() { return Ok(()); }
|
let index_size = self.index_length * 32;
|
||||||
|
|
||||||
let index_size = self.index_length*32;
|
if let Err(err) =
|
||||||
|
unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) }
|
||||||
if let Err(err) = unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) } {
|
{
|
||||||
bail!("unmap file {:?} failed - {}", self.tmp_filename, err);
|
bail!("unmap file {:?} failed - {}", self.tmp_filename, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,13 +361,14 @@ impl FixedIndexWriter {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close(&mut self) -> Result<[u8; 32], Error> {
|
pub fn close(&mut self) -> Result<[u8; 32], Error> {
|
||||||
|
if self.index == std::ptr::null_mut() {
|
||||||
|
bail!("cannot close already closed index file.");
|
||||||
|
}
|
||||||
|
|
||||||
if self.index == std::ptr::null_mut() { bail!("cannot close already closed index file."); }
|
let index_size = self.index_length * 32;
|
||||||
|
|
||||||
let index_size = self.index_length*32;
|
|
||||||
let data = unsafe { std::slice::from_raw_parts(self.index, index_size) };
|
let data = unsafe { std::slice::from_raw_parts(self.index, index_size) };
|
||||||
let index_csum = openssl::sha::sha256(data);
|
let index_csum = openssl::sha::sha256(data);
|
||||||
|
|
||||||
self.unmap()?;
|
self.unmap()?;
|
||||||
|
|
||||||
|
@ -363,7 +385,6 @@ impl FixedIndexWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn check_chunk_alignment(&self, offset: usize, chunk_len: usize) -> Result<usize, Error> {
|
pub fn check_chunk_alignment(&self, offset: usize, chunk_len: usize) -> Result<usize, Error> {
|
||||||
|
|
||||||
if offset < chunk_len {
|
if offset < chunk_len {
|
||||||
bail!("got chunk with small offset ({} < {}", offset, chunk_len);
|
bail!("got chunk with small offset ({} < {}", offset, chunk_len);
|
||||||
}
|
}
|
||||||
|
@ -375,12 +396,18 @@ impl FixedIndexWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// last chunk can be smaller
|
// last chunk can be smaller
|
||||||
if ((offset != self.size) && (chunk_len != self.chunk_size)) ||
|
if ((offset != self.size) && (chunk_len != self.chunk_size))
|
||||||
(chunk_len > self.chunk_size) || (chunk_len == 0) {
|
|| (chunk_len > self.chunk_size)
|
||||||
bail!("chunk with unexpected length ({} != {}", chunk_len, self.chunk_size);
|
|| (chunk_len == 0)
|
||||||
}
|
{
|
||||||
|
bail!(
|
||||||
|
"chunk with unexpected length ({} != {}",
|
||||||
|
chunk_len,
|
||||||
|
self.chunk_size
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if pos & (self.chunk_size-1) != 0 {
|
if pos & (self.chunk_size - 1) != 0 {
|
||||||
bail!("got unaligned chunk (pos = {})", pos);
|
bail!("got unaligned chunk (pos = {})", pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -389,22 +416,28 @@ impl FixedIndexWriter {
|
||||||
|
|
||||||
// Note: We want to add data out of order, so do not assume any order here.
|
// 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> {
|
pub fn add_chunk(&mut self, chunk_info: &ChunkInfo, stat: &mut ChunkStat) -> Result<(), Error> {
|
||||||
|
|
||||||
let chunk_len = chunk_info.chunk_len as usize;
|
let chunk_len = chunk_info.chunk_len as usize;
|
||||||
let offset = chunk_info.offset as usize; // end of chunk
|
let offset = chunk_info.offset as usize; // end of chunk
|
||||||
|
|
||||||
let idx = self.check_chunk_alignment(offset, chunk_len)?;
|
let idx = self.check_chunk_alignment(offset, chunk_len)?;
|
||||||
|
|
||||||
let (is_duplicate, compressed_size) =
|
let (is_duplicate, compressed_size) = self
|
||||||
self.store.insert_chunk(&chunk_info.chunk, &chunk_info.digest)?;
|
.store
|
||||||
|
.insert_chunk(&chunk_info.chunk, &chunk_info.digest)?;
|
||||||
|
|
||||||
stat.chunk_count += 1;
|
stat.chunk_count += 1;
|
||||||
stat.compressed_size += compressed_size;
|
stat.compressed_size += compressed_size;
|
||||||
|
|
||||||
let digest = &chunk_info.digest;
|
let digest = &chunk_info.digest;
|
||||||
|
|
||||||
println!("ADD CHUNK {} {} {}% {} {}", idx, chunk_len,
|
println!(
|
||||||
(compressed_size*100)/(chunk_len as u64), is_duplicate, proxmox::tools::digest_to_hex(digest));
|
"ADD CHUNK {} {} {}% {} {}",
|
||||||
|
idx,
|
||||||
|
chunk_len,
|
||||||
|
(compressed_size * 100) / (chunk_len as u64),
|
||||||
|
is_duplicate,
|
||||||
|
proxmox::tools::digest_to_hex(digest)
|
||||||
|
);
|
||||||
|
|
||||||
if is_duplicate {
|
if is_duplicate {
|
||||||
stat.duplicate_chunks += 1;
|
stat.duplicate_chunks += 1;
|
||||||
|
@ -416,14 +449,19 @@ impl FixedIndexWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_digest(&mut self, index: usize, digest: &[u8; 32]) -> Result<(), Error> {
|
pub fn add_digest(&mut self, index: usize, digest: &[u8; 32]) -> Result<(), Error> {
|
||||||
|
|
||||||
if index >= self.index_length {
|
if index >= self.index_length {
|
||||||
bail!("add digest failed - index out of range ({} >= {})", index, self.index_length);
|
bail!(
|
||||||
|
"add digest failed - index out of range ({} >= {})",
|
||||||
|
index,
|
||||||
|
self.index_length
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.index == std::ptr::null_mut() { bail!("cannot write to closed index file."); }
|
if self.index == std::ptr::null_mut() {
|
||||||
|
bail!("cannot write to closed index file.");
|
||||||
|
}
|
||||||
|
|
||||||
let index_pos = index*32;
|
let index_pos = index * 32;
|
||||||
unsafe {
|
unsafe {
|
||||||
let dst = self.index.add(index_pos);
|
let dst = self.index.add(index_pos);
|
||||||
dst.copy_from_nonoverlapping(digest.as_ptr(), 32);
|
dst.copy_from_nonoverlapping(digest.as_ptr(), 32);
|
||||||
|
@ -443,26 +481,25 @@ pub struct BufferedFixedReader<S> {
|
||||||
read_offset: u64,
|
read_offset: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <S: ReadChunk> BufferedFixedReader<S> {
|
impl<S: ReadChunk> BufferedFixedReader<S> {
|
||||||
|
|
||||||
pub fn new(index: FixedIndexReader, store: S) -> Self {
|
pub fn new(index: FixedIndexReader, store: S) -> Self {
|
||||||
|
|
||||||
let archive_size = index.size;
|
let archive_size = index.size;
|
||||||
Self {
|
Self {
|
||||||
store,
|
store,
|
||||||
index,
|
index,
|
||||||
archive_size,
|
archive_size,
|
||||||
read_buffer: Vec::with_capacity(1024*1024),
|
read_buffer: Vec::with_capacity(1024 * 1024),
|
||||||
buffered_chunk_idx: 0,
|
buffered_chunk_idx: 0,
|
||||||
buffered_chunk_start: 0,
|
buffered_chunk_start: 0,
|
||||||
read_offset: 0,
|
read_offset: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn archive_size(&self) -> u64 { self.archive_size }
|
pub fn archive_size(&self) -> u64 {
|
||||||
|
self.archive_size
|
||||||
|
}
|
||||||
|
|
||||||
fn buffer_chunk(&mut self, idx: usize) -> Result<(), Error> {
|
fn buffer_chunk(&mut self, idx: usize) -> Result<(), Error> {
|
||||||
|
|
||||||
let index = &self.index;
|
let index = &self.index;
|
||||||
let (start, end, digest) = index.chunk_info(idx)?;
|
let (start, end, digest) = index.chunk_info(idx)?;
|
||||||
|
|
||||||
|
@ -470,8 +507,12 @@ impl <S: ReadChunk> BufferedFixedReader<S> {
|
||||||
|
|
||||||
let data = self.store.read_chunk(&digest)?;
|
let data = self.store.read_chunk(&digest)?;
|
||||||
|
|
||||||
if (end - start) != data.len() as u64 {
|
if (end - start) != data.len() as u64 {
|
||||||
bail!("read chunk with wrong size ({} != {}", (end - start), data.len());
|
bail!(
|
||||||
|
"read chunk with wrong size ({} != {}",
|
||||||
|
(end - start),
|
||||||
|
data.len()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.read_buffer.clear();
|
self.read_buffer.clear();
|
||||||
|
@ -485,19 +526,19 @@ impl <S: ReadChunk> BufferedFixedReader<S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <S: ReadChunk> crate::tools::BufferedRead for BufferedFixedReader<S> {
|
impl<S: ReadChunk> crate::tools::BufferedRead for BufferedFixedReader<S> {
|
||||||
|
|
||||||
fn buffered_read(&mut self, offset: u64) -> Result<&[u8], Error> {
|
fn buffered_read(&mut self, offset: u64) -> Result<&[u8], Error> {
|
||||||
|
if offset == self.archive_size {
|
||||||
if offset == self.archive_size { return Ok(&self.read_buffer[0..0]); }
|
return Ok(&self.read_buffer[0..0]);
|
||||||
|
}
|
||||||
|
|
||||||
let buffer_len = self.read_buffer.len();
|
let buffer_len = self.read_buffer.len();
|
||||||
let index = &self.index;
|
let index = &self.index;
|
||||||
|
|
||||||
// optimization for sequential read
|
// optimization for sequential read
|
||||||
if buffer_len > 0 &&
|
if buffer_len > 0
|
||||||
((self.buffered_chunk_idx + 1) < index.index_length) &&
|
&& ((self.buffered_chunk_idx + 1) < index.index_length)
|
||||||
(offset >= (self.buffered_chunk_start + (self.read_buffer.len() as u64)))
|
&& (offset >= (self.buffered_chunk_start + (self.read_buffer.len() as u64)))
|
||||||
{
|
{
|
||||||
let next_idx = self.buffered_chunk_idx + 1;
|
let next_idx = self.buffered_chunk_idx + 1;
|
||||||
let next_end = index.chunk_end(next_idx);
|
let next_end = index.chunk_end(next_idx);
|
||||||
|
@ -508,34 +549,38 @@ impl <S: ReadChunk> crate::tools::BufferedRead for BufferedFixedReader<S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer_len == 0) ||
|
if (buffer_len == 0)
|
||||||
(offset < self.buffered_chunk_start) ||
|
|| (offset < self.buffered_chunk_start)
|
||||||
(offset >= (self.buffered_chunk_start + (self.read_buffer.len() as u64)))
|
|| (offset >= (self.buffered_chunk_start + (self.read_buffer.len() as u64)))
|
||||||
{
|
{
|
||||||
let idx = (offset / index.chunk_size as u64) as usize;
|
let idx = (offset / index.chunk_size as u64) as usize;
|
||||||
self.buffer_chunk(idx)?;
|
self.buffer_chunk(idx)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let buffer_offset = (offset - self.buffered_chunk_start) as usize;
|
let buffer_offset = (offset - self.buffered_chunk_start) as usize;
|
||||||
Ok(&self.read_buffer[buffer_offset..])
|
Ok(&self.read_buffer[buffer_offset..])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <S: ReadChunk> std::io::Read for BufferedFixedReader<S> {
|
impl<S: ReadChunk> std::io::Read for BufferedFixedReader<S> {
|
||||||
|
|
||||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
|
||||||
|
|
||||||
use std::io::{Error, ErrorKind};
|
|
||||||
use crate::tools::BufferedRead;
|
use crate::tools::BufferedRead;
|
||||||
|
use std::io::{Error, ErrorKind};
|
||||||
|
|
||||||
let data = match self.buffered_read(self.read_offset) {
|
let data = match self.buffered_read(self.read_offset) {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(err) => return Err(Error::new(ErrorKind::Other, err.to_string())),
|
Err(err) => return Err(Error::new(ErrorKind::Other, err.to_string())),
|
||||||
};
|
};
|
||||||
|
|
||||||
let n = if data.len() > buf.len() { buf.len() } else { data.len() };
|
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); }
|
unsafe {
|
||||||
|
std::ptr::copy_nonoverlapping(data.as_ptr(), buf.as_mut_ptr(), n);
|
||||||
|
}
|
||||||
|
|
||||||
self.read_offset += n as u64;
|
self.read_offset += n as u64;
|
||||||
|
|
||||||
|
@ -543,13 +588,11 @@ impl <S: ReadChunk> std::io::Read for BufferedFixedReader<S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <S: ReadChunk> Seek for BufferedFixedReader<S> {
|
impl<S: ReadChunk> Seek for BufferedFixedReader<S> {
|
||||||
|
|
||||||
fn seek(&mut self, pos: SeekFrom) -> Result<u64, std::io::Error> {
|
fn seek(&mut self, pos: SeekFrom) -> Result<u64, std::io::Error> {
|
||||||
|
|
||||||
let new_offset = match pos {
|
let new_offset = match pos {
|
||||||
SeekFrom::Start(start_offset) => start_offset as i64,
|
SeekFrom::Start(start_offset) => start_offset as i64,
|
||||||
SeekFrom::End(end_offset) => (self.archive_size as i64)+ end_offset,
|
SeekFrom::End(end_offset) => (self.archive_size as i64) + end_offset,
|
||||||
SeekFrom::Current(offset) => (self.read_offset as i64) + offset,
|
SeekFrom::Current(offset) => (self.read_offset as i64) + offset,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -557,7 +600,11 @@ impl <S: ReadChunk> Seek for BufferedFixedReader<S> {
|
||||||
if (new_offset < 0) || (new_offset > (self.archive_size as i64)) {
|
if (new_offset < 0) || (new_offset > (self.archive_size as i64)) {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
ErrorKind::Other,
|
ErrorKind::Other,
|
||||||
format!("seek is out of range {} ([0..{}])", new_offset, self.archive_size)));
|
format!(
|
||||||
|
"seek is out of range {} ([0..{}])",
|
||||||
|
new_offset, self.archive_size
|
||||||
|
),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
self.read_offset = new_offset as u64;
|
self.read_offset = new_offset as u64;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue