Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't abort on negative elapsed time in packer/indexer #138

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions crates/core/src/blob/packer.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -485,10 +486,8 @@ impl<BE: DecryptWriteBackend> RawPacker<BE> {
/// # 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],
Expand All @@ -513,13 +512,13 @@ impl<BE: DecryptWriteBackend> RawPacker<BE> {
self.count += 1;

// check if PackFile needs to be saved
let elapsed = self.created.elapsed().unwrap_or_else(|err| {
warn!("couldn't get elapsed time from system time: {err:?}");
Duration::ZERO
});
if self.count >= constants::MAX_COUNT
|| self.size >= size_limit
|| self
.created
.elapsed()
.map_err(PackerErrorKind::CouldNotGetElapsedTimeFromSystemTime)?
>= constants::MAX_AGE
|| elapsed >= constants::MAX_AGE
{
self.pack_sizer.add_size(self.index.pack_size());
self.save()?;
Expand Down
7 changes: 1 addition & 6 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::{
ops::RangeInclusive,
path::{PathBuf, StripPrefixError},
str::Utf8Error,
time::SystemTimeError,
};

#[cfg(not(windows))]
Expand Down Expand Up @@ -301,16 +300,14 @@ 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,
/// failed to get a blob from the backend
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
Expand Down Expand Up @@ -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:?}`
Expand Down
24 changes: 9 additions & 15 deletions crates/core/src/index/indexer.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand Down Expand Up @@ -128,10 +130,8 @@ impl<BE: DecryptWriteBackend> Indexer<BE> {
///
/// # 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)
Expand All @@ -145,10 +145,8 @@ impl<BE: DecryptWriteBackend> Indexer<BE> {
///
/// # 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)
Expand All @@ -163,10 +161,8 @@ impl<BE: DecryptWriteBackend> Indexer<BE> {
///
/// # 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();
Expand All @@ -180,13 +176,11 @@ impl<BE: DecryptWriteBackend> Indexer<BE> {
self.file.add(pack, delete);

// check if IndexFile needs to be saved
if self.count >= constants::MAX_COUNT
|| self
.created
.elapsed()
.map_err(IndexErrorKind::CouldNotGetElapsedTimeFromSystemTime)?
>= constants::MAX_AGE
{
let elapsed = self.created.elapsed().unwrap_or_else(|err| {
warn!("couldn't get elapsed time from system time: {err:?}");
Duration::ZERO
});
if self.count >= constants::MAX_COUNT || elapsed >= constants::MAX_AGE {
self.save()?;
self.reset();
}
Expand Down
Loading