Skip to content

Commit 8f6b9e4

Browse files
committed
use temporary directory below docs.rs-prefix, so we can move files
1 parent 06a58b6 commit 8f6b9e4

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/storage/archive_index.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,16 @@ pub(crate) fn find_in_file<P: AsRef<Path>>(
288288
}
289289
}
290290

291-
pub(crate) fn convert_to_sqlite_index<P: AsRef<Path>>(path: P) -> Result<TempPath> {
291+
pub(crate) fn convert_to_sqlite_index<P: AsRef<Path>>(
292+
path: P,
293+
tmpdir: impl AsRef<Path>,
294+
) -> Result<TempPath> {
292295
let path = path.as_ref();
293296
let index: Index = { serde_cbor::from_reader(BufReader::new(File::open(path)?))? };
294297

295298
// write the new index into a temporary file so reads from ongoing requests
296299
// can continue on the old index until the new one is fully written.
297-
let tmp_path = tempfile::NamedTempFile::new()?.into_temp_path();
300+
let tmp_path = tempfile::NamedTempFile::new_in(tmpdir)?.into_temp_path();
298301
index
299302
.write_sqlite(&tmp_path)
300303
.context("error writing SQLite index")?;
@@ -351,7 +354,8 @@ mod tests {
351354
.unwrap()
352355
.unwrap();
353356

354-
let sqlite_index_file = convert_to_sqlite_index(cbor_index_file).unwrap();
357+
let temp_dir = tempfile::TempDir::new().unwrap();
358+
let sqlite_index_file = convert_to_sqlite_index(cbor_index_file, &temp_dir).unwrap();
355359
assert!(is_sqlite_file(&sqlite_index_file).unwrap());
356360

357361
let migrated_fi = find_in_file(

src/storage/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ pub(crate) fn source_archive_path(name: &str, version: &str) -> String {
588588
}
589589

590590
#[instrument(skip(storage))]
591-
fn migrate_one(storage: &Storage, archive_path: &str) -> Result<()> {
591+
fn migrate_one(storage: &Storage, archive_path: &str, tmpdir: &Path) -> Result<()> {
592592
// this will also download the index if it doesn't exist locally
593593
let local_index_filename = storage.get_index_filename(archive_path)?;
594594

@@ -599,7 +599,8 @@ fn migrate_one(storage: &Storage, archive_path: &str) -> Result<()> {
599599

600600
info!("converting local index...");
601601
let remote_index_path = format!("{}.index", &archive_path);
602-
let new_index_temp_path = archive_index::convert_to_sqlite_index(&local_index_filename)?;
602+
let new_index_temp_path =
603+
archive_index::convert_to_sqlite_index(&local_index_filename, tmpdir)?;
603604

604605
// first upload to S3, ongoing requests will still use the local CBOR index
605606
info!("uplading to S3...");
@@ -621,6 +622,11 @@ pub fn migrate_old_archive_indexes(
621622
storage: &Storage,
622623
conn: &mut impl postgres::GenericClient,
623624
) -> Result<()> {
625+
let tmpdir = storage.config.prefix.join("archive_cache_tmp");
626+
if !tmpdir.exists() {
627+
fs::create_dir(&tmpdir)?;
628+
}
629+
624630
for row in conn
625631
.query_raw(
626632
"
@@ -643,10 +649,10 @@ pub fn migrate_old_archive_indexes(
643649
let version: &str = row.get(1);
644650
info!("converting archive index for {} {}...", name, version);
645651

646-
if let Err(err) = migrate_one(storage, &rustdoc_archive_path(name, version)) {
652+
if let Err(err) = migrate_one(storage, &rustdoc_archive_path(name, version), &tmpdir) {
647653
error!("error converting rustdoc archive index: {:?}", err);
648654
}
649-
if let Err(err) = migrate_one(storage, &source_archive_path(name, version)) {
655+
if let Err(err) = migrate_one(storage, &source_archive_path(name, version), &tmpdir) {
650656
error!("error converting source archive index: {:?}", err);
651657
}
652658
}

0 commit comments

Comments
 (0)