Skip to content

Commit 754de17

Browse files
committed
Clean stale git temp files
1 parent 92d8826 commit 754de17

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

Diff for: src/cargo/sources/git/utils.rs

+39
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,8 @@ pub fn fetch(
812812
// request we're about to issue.
813813
maybe_gc_repo(repo)?;
814814

815+
clean_repo_temp_files(repo);
816+
815817
// Translate the reference desired here into an actual list of refspecs
816818
// which need to get fetched. Additionally record if we're fetching tags.
817819
let mut refspecs = Vec::new();
@@ -1001,6 +1003,43 @@ fn maybe_gc_repo(repo: &mut git2::Repository) -> CargoResult<()> {
10011003
reinitialize(repo)
10021004
}
10031005

1006+
/// Removes temporary files left from previous activity.
1007+
///
1008+
/// If libgit2 is interrupted while indexing pack files, it will leave behind
1009+
/// some temporary files that it doesn't clean up. These can be quite large in
1010+
/// size, so this tries to clean things up.
1011+
///
1012+
/// This intentionally ignores errors. This is only an opportunistic cleaning,
1013+
/// and we don't really care if there are issues (there's unlikely anything
1014+
/// that can be done).
1015+
///
1016+
/// The git CLI has similar behavior (its temp files look like
1017+
/// `objects/pack/tmp_pack_9kUSA8`). Those files are normally deleted via `git
1018+
/// prune` which is run by `git gc`. However, it doesn't know about libgit2's
1019+
/// filenames, so they never get cleaned up.
1020+
fn clean_repo_temp_files(repo: &git2::Repository) {
1021+
let path = repo.path().join("objects/pack/pack_git2_*");
1022+
let pattern = match path.to_str() {
1023+
Some(p) => p,
1024+
None => {
1025+
log::warn!("cannot convert {path:?} to a string");
1026+
return;
1027+
}
1028+
};
1029+
let paths = match glob::glob(pattern) {
1030+
Ok(paths) => paths,
1031+
Err(_) => return,
1032+
};
1033+
for path in paths {
1034+
if let Ok(path) = path {
1035+
match paths::remove_file(&path) {
1036+
Ok(_) => log::debug!("removed stale temp git file {path:?}"),
1037+
Err(e) => log::warn!("failed to remove {path:?} while cleaning temp files: {e}"),
1038+
}
1039+
}
1040+
}
1041+
}
1042+
10041043
fn reinitialize(repo: &mut git2::Repository) -> CargoResult<()> {
10051044
// Here we want to drop the current repository object pointed to by `repo`,
10061045
// so we initialize temporary repository in a sub-folder, blow away the

Diff for: tests/testsuite/git.rs

+33
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::sync::Arc;
1010
use std::thread;
1111

1212
use cargo_test_support::paths::{self, CargoPathExt};
13+
use cargo_test_support::registry::Package;
1314
use cargo_test_support::{basic_lib_manifest, basic_manifest, git, main_file, path2url, project};
1415
use cargo_test_support::{sleep_ms, t, Project};
1516

@@ -3619,3 +3620,35 @@ fn _corrupted_checkout(with_cli: bool) {
36193620
e.run();
36203621
assert!(ok.exists());
36213622
}
3623+
3624+
#[cargo_test]
3625+
fn cleans_temp_pack_files() {
3626+
// Checks that cargo removes temp files left by libgit2 when it is
3627+
// interrupted (see clean_repo_temp_files).
3628+
Package::new("bar", "1.0.0").publish();
3629+
let p = project()
3630+
.file(
3631+
"Cargo.toml",
3632+
r#"
3633+
[package]
3634+
name = "foo"
3635+
version = "0.1.0"
3636+
3637+
[dependencies]
3638+
bar = "1.0"
3639+
"#,
3640+
)
3641+
.file("src/lib.rs", "")
3642+
.build();
3643+
p.cargo("fetch").run();
3644+
// Simulate what happens when libgit2 is interrupted while indexing a pack file.
3645+
let tmp_path = super::git_gc::find_index().join(".git/objects/pack/pack_git2_91ab40da04fdc2e7");
3646+
fs::write(&tmp_path, "test").unwrap();
3647+
let mut perms = fs::metadata(&tmp_path).unwrap().permissions();
3648+
perms.set_readonly(true);
3649+
fs::set_permissions(&tmp_path, perms).unwrap();
3650+
3651+
// Trigger an index update.
3652+
p.cargo("generate-lockfile").run();
3653+
assert!(!tmp_path.exists());
3654+
}

Diff for: tests/testsuite/git_gc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use cargo_test_support::registry::Package;
1111

1212
use url::Url;
1313

14-
fn find_index() -> PathBuf {
14+
pub fn find_index() -> PathBuf {
1515
let dir = paths::home().join(".cargo/registry/index");
1616
dir.read_dir().unwrap().next().unwrap().unwrap().path()
1717
}

0 commit comments

Comments
 (0)