From b7e0e5b6f18d8f7700f72c2e5019f85f5f43d0df Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Thu, 25 Jan 2024 23:24:43 +0100 Subject: [PATCH] fix: don't abort on negative elapsed time in packer/indexer --- crates/core/src/blob/packer.rs | 14 ++++++-------- crates/core/src/error.rs | 7 +------ crates/core/src/index/indexer.rs | 21 ++++++++------------- 3 files changed, 15 insertions(+), 27 deletions(-) diff --git a/crates/core/src/blob/packer.rs b/crates/core/src/blob/packer.rs index ea659bff1..b3e173fb3 100644 --- a/crates/core/src/blob/packer.rs +++ b/crates/core/src/blob/packer.rs @@ -1,9 +1,10 @@ use integer_sqrt::IntegerSquareRoot; +use log::warn; use std::{ num::NonZeroU32, sync::{Arc, RwLock}, - time::SystemTime, + time::{Duration, SystemTime}, }; use bytes::{Bytes, BytesMut}; @@ -485,10 +486,8 @@ impl RawPacker { /// # Errors /// /// * [`PackerErrorKind::IntConversionFailed`] - If converting the data length to u64 fails - /// * [`PackerErrorKind::CouldNotGetElapsedTimeFromSystemTime`] - If elapsed time could not be retrieved from system time /// /// [`PackerErrorKind::IntConversionFailed`]: crate::error::PackerErrorKind::IntConversionFailed - /// [`PackerErrorKind::CouldNotGetElapsedTimeFromSystemTime`]: crate::error::PackerErrorKind::CouldNotGetElapsedTimeFromSystemTime fn add_raw( &mut self, data: &[u8], @@ -515,11 +514,10 @@ impl RawPacker { // check if PackFile needs to be saved if self.count >= constants::MAX_COUNT || self.size >= size_limit - || self - .created - .elapsed() - .map_err(PackerErrorKind::CouldNotGetElapsedTimeFromSystemTime)? - >= constants::MAX_AGE + || self.created.elapsed().unwrap_or_else(|err| { + warn!("couldn't get elapsed time from system time: {err:?}"); + Duration::ZERO + }) >= constants::MAX_AGE { self.pack_sizer.add_size(self.index.pack_size()); self.save()?; diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index 6a942ad9e..3df25107f 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -10,7 +10,6 @@ use std::{ ops::RangeInclusive, path::{PathBuf, StripPrefixError}, str::Utf8Error, - time::SystemTimeError, }; #[cfg(not(windows))] @@ -301,7 +300,7 @@ pub enum RepositoryErrorKind { } /// [`IndexErrorKind`] describes the errors that can be returned by processing Indizes -#[derive(Error, Debug, Display)] +#[derive(Error, Debug, Display, Clone, Copy)] pub enum IndexErrorKind { /// blob not found in index BlobInIndexNotFound, @@ -309,8 +308,6 @@ pub enum IndexErrorKind { GettingBlobIndexEntryFromBackendFailed, /// saving IndexFile failed SavingIndexFileFailed, - /// couldn't get elapsed time from system time: {0:?} - CouldNotGetElapsedTimeFromSystemTime(#[from] SystemTimeError), } /// [`BackendAccessErrorKind`] describes the errors that can be returned by the various Backends @@ -463,8 +460,6 @@ pub enum PackerErrorKind { SendingCrossbeamMessageFailedForIndexPack( #[from] crossbeam_channel::SendError<(bytes::Bytes, IndexPack)>, ), - /// couldn't get elapsed time from system time: `{0:?}` - CouldNotGetElapsedTimeFromSystemTime(#[from] SystemTimeError), /// couldn't create binary representation for pack header: `{0:?}` CouldNotCreateBinaryRepresentationForHeader(#[from] PackFileErrorKind), /// failed to write bytes in backend: `{0:?}` diff --git a/crates/core/src/index/indexer.rs b/crates/core/src/index/indexer.rs index 48897879f..b793472b5 100644 --- a/crates/core/src/index/indexer.rs +++ b/crates/core/src/index/indexer.rs @@ -1,12 +1,14 @@ use std::{ collections::BTreeSet, sync::{Arc, RwLock}, - time::SystemTime, + time::{Duration, SystemTime}, }; +use log::warn; + use crate::{ backend::decrypt::DecryptWriteBackend, - error::{IndexErrorKind, RusticResult}, + error::RusticResult, id::Id, repofile::indexfile::{IndexFile, IndexPack}, }; @@ -128,10 +130,8 @@ impl Indexer { /// /// # Errors /// - /// * [`IndexErrorKind::CouldNotGetElapsedTimeFromSystemTime`] - If the elapsed time could not be retrieved from the system time. /// * [`CryptBackendErrorKind::SerializingToJsonByteVectorFailed`] - If the index file could not be serialized. /// - /// [`IndexErrorKind::CouldNotGetElapsedTimeFromSystemTime`]: crate::error::IndexErrorKind::CouldNotGetElapsedTimeFromSystemTime /// [`CryptBackendErrorKind::SerializingToJsonByteVectorFailed`]: crate::error::CryptBackendErrorKind::SerializingToJsonByteVectorFailed pub fn add(&mut self, pack: IndexPack) -> RusticResult<()> { self.add_with(pack, false) @@ -145,10 +145,8 @@ impl Indexer { /// /// # Errors /// - /// * [`IndexErrorKind::CouldNotGetElapsedTimeFromSystemTime`] - If the elapsed time could not be retrieved from the system time. /// * [`CryptBackendErrorKind::SerializingToJsonByteVectorFailed`] - If the index file could not be serialized. /// - /// [`IndexErrorKind::CouldNotGetElapsedTimeFromSystemTime`]: crate::error::IndexErrorKind::CouldNotGetElapsedTimeFromSystemTime /// [`CryptBackendErrorKind::SerializingToJsonByteVectorFailed`]: crate::error::CryptBackendErrorKind::SerializingToJsonByteVectorFailed pub fn add_remove(&mut self, pack: IndexPack) -> RusticResult<()> { self.add_with(pack, true) @@ -163,10 +161,8 @@ impl Indexer { /// /// # Errors /// - /// * [`IndexErrorKind::CouldNotGetElapsedTimeFromSystemTime`] - If the elapsed time could not be retrieved from the system time. /// * [`CryptBackendErrorKind::SerializingToJsonByteVectorFailed`] - If the index file could not be serialized. /// - /// [`IndexErrorKind::CouldNotGetElapsedTimeFromSystemTime`]: crate::error::IndexErrorKind::CouldNotGetElapsedTimeFromSystemTime /// [`CryptBackendErrorKind::SerializingToJsonByteVectorFailed`]: crate::error::CryptBackendErrorKind::SerializingToJsonByteVectorFailed pub fn add_with(&mut self, pack: IndexPack, delete: bool) -> RusticResult<()> { self.count += pack.blobs.len(); @@ -181,11 +177,10 @@ impl Indexer { // check if IndexFile needs to be saved if self.count >= constants::MAX_COUNT - || self - .created - .elapsed() - .map_err(IndexErrorKind::CouldNotGetElapsedTimeFromSystemTime)? - >= constants::MAX_AGE + || self.created.elapsed().unwrap_or_else(|err| { + warn!("couldn't get elapsed time from system time: {err:?}"); + Duration::ZERO + }) >= constants::MAX_AGE { self.save()?; self.reset();