Skip to content

Commit

Permalink
try
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Nov 11, 2024
1 parent 816903c commit 7c56dac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
14 changes: 4 additions & 10 deletions src/storage/archive_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,15 @@ fn find_in_sqlite_index(
.optional()
}

fn open_index<P: AsRef<Path> + std::fmt::Debug>(
archive_index_path: P,
) -> Result<Connection, rusqlite::Error> {
Connection::open_with_flags(
archive_index_path,
OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
)
}

#[instrument]
pub(crate) fn find_in_file<P: AsRef<Path> + std::fmt::Debug>(
archive_index_path: P,
search_for: &str,
) -> Result<Option<FileInfo>, rusqlite::Error> {
let connection = open_index(archive_index_path)?;
let connection = Connection::open_with_flags(
archive_index_path,
OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
)?;
find_in_sqlite_index(&connection, search_for)
}

Expand Down
33 changes: 17 additions & 16 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ impl AsyncStorage {
Ok(blob)
}

async fn download_archive_index<P: AsRef<Path>>(
#[instrument]
async fn download_archive_index<P: AsRef<Path> + std::fmt::Debug>(
&self,
local_index_path: P,
remote_index_path: &str,
Expand All @@ -308,7 +309,7 @@ impl AsyncStorage {
tokio::fs::remove_file(&local_index_path).await?;
}

let index_content = self.get(&remote_index_path, usize::MAX).await?.content;
let index_content = self.get(remote_index_path, usize::MAX).await?.content;

tokio::fs::create_dir_all(
local_index_path
Expand All @@ -330,6 +331,7 @@ impl AsyncStorage {
Ok(())
}

#[instrument]
async fn find_in_archive_index(
&self,
archive_path: &str,
Expand Down Expand Up @@ -363,20 +365,19 @@ impl AsyncStorage {
match find_in_file(&local_index_path, path).await {
Ok(result) => Ok(result),
Err(err) => {
if let Some(sqlite_err) = err.downcast_ref::<rusqlite::Error>() {
if let rusqlite::Error::SqliteFailure(internal_err, _) = sqlite_err {
if internal_err.code == rusqlite::ErrorCode::DatabaseCorrupt {
warn!(
?local_index_path,
remote_index_path,
"found corrupt local archive index, redownloading"
);

self.download_archive_index(&local_index_path, &remote_index_path)
.await?;

return find_in_file(&local_index_path, path).await;
}
if let Some(rusqlite::Error::SqliteFailure(sqlite_err, _)) =
err.downcast_ref::<rusqlite::Error>()
{
if sqlite_err.code == rusqlite::ErrorCode::DatabaseCorrupt {
warn!(
?local_index_path,
remote_index_path, "found corrupt local archive index, redownloading"
);

self.download_archive_index(&local_index_path, &remote_index_path)
.await?;

return find_in_file(&local_index_path, path).await;
}
}

Expand Down

0 comments on commit 7c56dac

Please sign in to comment.