use super::chunk_stat::*;
use super::chunk_store::*;
-use super::{IndexFile, ChunkReadInfo};
-use crate::tools::{self, epoch_now_u64};
+use super::{ChunkReadInfo, IndexFile};
+use crate::tools;
-use chrono::{Local, TimeZone};
use std::fs::File;
use std::io::Write;
use std::os::unix::io::AsRawFd;
pub struct FixedIndexHeader {
pub magic: [u8; 8],
pub uuid: [u8; 16],
- pub ctime: u64,
+ pub ctime: i64,
/// Sha256 over the index ``SHA256(digest1||digest2||...)``
pub index_csum: [u8; 32],
pub size: u64,
index_length: usize,
index: *mut u8,
pub uuid: [u8; 16],
- pub ctime: u64,
+ pub ctime: i64,
pub index_csum: [u8; 32],
}
pub fn open(path: &Path) -> Result<Self, Error> {
File::open(path)
.map_err(Error::from)
- .and_then(|file| Self::new(file))
+ .and_then(Self::new)
.map_err(|err| format_err!("Unable to open fixed index {:?} - {}", path, err))
}
pub fn new(mut file: std::fs::File) -> Result<Self, Error> {
- if let Err(err) =
- nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockSharedNonblock)
- {
- bail!("unable to get shared lock - {}", err);
- }
-
file.seek(SeekFrom::Start(0))?;
let header_size = std::mem::size_of::<FixedIndexHeader>();
+
+ let stat = match nix::sys::stat::fstat(file.as_raw_fd()) {
+ 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);
+ }
+
let header: Box<FixedIndexHeader> = unsafe { file.read_host_value_boxed()? };
if header.magic != super::FIXED_SIZED_CHUNK_INDEX_1_0 {
}
let size = u64::from_le(header.size);
- let ctime = u64::from_le(header.ctime);
+ let ctime = i64::from_le(header.ctime);
let chunk_size = u64::from_le(header.chunk_size);
let index_length = ((size + chunk_size - 1) / chunk_size) as usize;
let index_size = index_length * 32;
- let rawfd = file.as_raw_fd();
-
- let stat = match nix::sys::stat::fstat(rawfd) {
- Ok(stat) => stat,
- Err(err) => bail!("fstat failed - {}", err),
- };
-
let expected_index_size = (stat.st_size as usize) - header_size;
if index_size != expected_index_size {
bail!(
pub fn print_info(&self) {
println!("Size: {}", self.size);
println!("ChunkSize: {}", self.chunk_size);
- println!(
- "CTime: {}",
- Local.timestamp(self.ctime as i64, 0).format("%c")
- );
+
+ 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);
println!("UUID: {:?}", self.uuid);
}
}
Some((
(offset / self.chunk_size as u64) as usize,
- offset & (self.chunk_size - 1) as u64 // fast modulo, valid for 2^x chunk_size
+ offset & (self.chunk_size - 1) as u64, // fast modulo, valid for 2^x chunk_size
))
}
}
index_length: usize,
index: *mut u8,
pub uuid: [u8; 16],
- pub ctime: u64,
+ pub ctime: i64,
}
// `index` is mmap()ed which cannot be thread-local so should be sendable
panic!("got unexpected header size");
}
- let ctime = epoch_now_u64()?;
+ let ctime = proxmox::tools::time::epoch_i64();
let uuid = Uuid::generate();
let header = unsafe { &mut *(buffer.as_ptr() as *mut FixedIndexHeader) };
header.magic = super::FIXED_SIZED_CHUNK_INDEX_1_0;
- header.ctime = u64::to_le(ctime);
+ header.ctime = i64::to_le(ctime);
header.size = u64::to_le(size as u64);
header.chunk_size = u64::to_le(chunk_size as u64);
header.uuid = *uuid.as_bytes();