handle pack index files which lack a corresponding pack file ok millert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
diff --git a/gotadmin/gotadmin.1 b/gotadmin/gotadmin.1
index 1290b70..7447f80 100644
--- a/gotadmin/gotadmin.1
+++ b/gotadmin/gotadmin.1
@@ -181,7 +181,7 @@ and a break-down of the number of objects per object type.
.It Cm ls
Short alias for
.Cm listpack .
-.It Cm cleanup Oo Fl n Oc Oo Fl r Ar repository-path Oc Oo Fl q Oc
+.It Cm cleanup Oo Fl p Oc Oo Fl n Oc Oo Fl r Ar repository-path Oc Oo Fl q Oc
Purge unreferenced loose objects from the repository and display
the amount of disk space which has been freed as a result.
.Pp
@@ -238,6 +238,16 @@ will only purge corresponding objects once such references have been
deleted with
.Cm got ref -d .
.Pp
+Some Git repositories contain pack index files which lack a corresponding
+pack file, which is an inconsistent repository state.
+In such cases,
+.Cm gotadmin cleanup -p -n
+will display a list of affected pack index files.
+Whenever possible the missing pack files should be restored.
+If restoring missing pack files is not possible then affected pack index
+files can be removed with
+.Cm gotadmin cleanup -p .
+.Pp
The
.Dq preciousObjects
Git extension is intended to prevent the removal of objects from a repository.
@@ -248,9 +258,12 @@ The options for
.Cm gotadmin cleanup
are as follows:
.Bl -tag -width Ds
+.It Fl p
+Instead of purging unreferenced loose objects, remove any pack index files
+which do not have a corresponding pack file.
.It Fl n
Display the usual progress output and summary information but do not actually
-purge any objects.
+remove any files from disk.
.It Fl r Ar repository-path
Use the repository at the specified path.
If not specified, assume the repository is located at or above the current
diff --git a/gotadmin/gotadmin.c b/gotadmin/gotadmin.c
index 3a8fe22..810a5cc 100644
--- a/gotadmin/gotadmin.c
+++ b/gotadmin/gotadmin.c
@@ -897,8 +897,8 @@ done:
__dead static void
usage_cleanup(void)
{
- fprintf(stderr, "usage: %s cleanup [-n] [-r repository-path] [-q]\n",
- getprogname());
+ fprintf(stderr, "usage: %s cleanup [-p] [-n] [-r repository-path] "
+ "[-q]\n", getprogname());
exit(1);
}
@@ -959,6 +959,29 @@ cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
return NULL;
}
+struct got_lonely_packidx_progress_arg {
+ int verbosity;
+ int printed_something;
+ int dry_run;
+};
+
+static const struct got_error *
+lonely_packidx_progress(void *arg, const char *path)
+{
+ struct got_lonely_packidx_progress_arg *a = arg;
+
+ if (a->verbosity < 0)
+ return NULL;
+
+ if (a->dry_run)
+ printf("%s could be removed\n", path);
+ else
+ printf("%s removed\n", path);
+
+ a->printed_something = 1;
+ return NULL;
+}
+
static const struct got_error *
cmd_cleanup(int argc, char *argv[])
{
@@ -966,7 +989,9 @@ cmd_cleanup(int argc, char *argv[])
char *cwd = NULL, *repo_path = NULL;
struct got_repository *repo = NULL;
int ch, dry_run = 0, npacked = 0, verbosity = 0;
+ int remove_lonely_packidx = 0;
struct got_cleanup_progress_arg cpa;
+ struct got_lonely_packidx_progress_arg lpa;
off_t size_before, size_after;
char scaled_before[FMT_SCALED_STRSIZE];
char scaled_after[FMT_SCALED_STRSIZE];
@@ -974,8 +999,11 @@ cmd_cleanup(int argc, char *argv[])
char **extensions;
int nextensions, i;
- while ((ch = getopt(argc, argv, "r:nq")) != -1) {
+ while ((ch = getopt(argc, argv, "pr:nq")) != -1) {
switch (ch) {
+ case 'p':
+ remove_lonely_packidx = 1;
+ break;
case 'r':
repo_path = realpath(optarg, NULL);
if (repo_path == NULL)
@@ -1028,6 +1056,15 @@ cmd_cleanup(int argc, char *argv[])
}
}
+ if (remove_lonely_packidx) {
+ memset(&lpa, 0, sizeof(lpa));
+ lpa.dry_run = dry_run;
+ lpa.verbosity = verbosity;
+ error = got_repo_remove_lonely_packidx(repo, dry_run,
+ lonely_packidx_progress, &lpa, check_cancelled, NULL);
+ goto done;
+ }
+
memset(&cpa, 0, sizeof(cpa));
cpa.last_ncommits = -1;
cpa.last_npurged = -1;
diff --git a/include/got_error.h b/include/got_error.h
index 851d4ad..48165fc 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -146,6 +146,7 @@
#define GOT_ERR_BAD_SYMLINK 129
#define GOT_ERR_GIT_REPO_EXT 130
#define GOT_ERR_CANNOT_PACK 131
+#define GOT_ERR_LONELY_PACKIDX 132
static const struct got_error {
int code;
@@ -299,6 +300,7 @@ static const struct got_error {
"version control" },
{ GOT_ERR_GIT_REPO_EXT, "unsupported repository format extension" },
{ GOT_ERR_CANNOT_PACK, "not enough objects to pack" },
+ { GOT_ERR_LONELY_PACKIDX, "pack index has no corresponding pack file" },
};
/*
diff --git a/include/got_repository_admin.h b/include/got_repository_admin.h
index 99fa642..d1e9be3 100644
--- a/include/got_repository_admin.h
+++ b/include/got_repository_admin.h
@@ -85,3 +85,13 @@ got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
off_t *size_before, off_t *size_after, int *npacked, int dry_run,
got_cleanup_progress_cb progress_cb, void *progress_arg,
got_cancel_cb cancel_cb, void *cancel_arg);
+
+/* A callback function which gets invoked with cleanup information to print. */
+typedef const struct got_error *(*got_lonely_packidx_progress_cb)(void *arg,
+ const char *path);
+
+/* Remove pack index files which do not have a corresponding pack file. */
+const struct got_error *
+got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
+ got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
+ got_cancel_cb cancel_cb, void *cancel_arg);
diff --git a/lib/got_lib_repository.h b/lib/got_lib_repository.h
index e26649f..7f21b68 100644
--- a/lib/got_lib_repository.h
+++ b/lib/got_lib_repository.h
@@ -93,6 +93,7 @@ const struct got_error*got_repo_cache_tag(struct got_repository *,
struct got_object_id *, struct got_tag_object *);
struct got_tag_object *got_repo_get_cached_tag(struct got_repository *,
struct got_object_id *);
+int got_repo_is_packidx_filename(const char *, size_t);
const struct got_error *got_repo_search_packidx(struct got_packidx **, int *,
struct got_repository *, struct got_object_id *);
const struct got_error *got_repo_cache_pack(struct got_pack **,
diff --git a/lib/pack.c b/lib/pack.c
index 9bb2b1a..9d93bee 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -332,34 +332,55 @@ got_packidx_open(struct got_packidx **packidx,
int dir_fd, const char *relpath, int verify)
{
const struct got_error *err = NULL;
- struct got_packidx *p;
+ struct got_packidx *p = NULL;
+ char *pack_relpath;
struct stat sb;
*packidx = NULL;
+ err = got_packidx_get_packfile_path(&pack_relpath, relpath);
+ if (err)
+ return err;
+
+ /*
+ * Ensure that a corresponding pack file exists.
+ * Some Git repositories have this problem. Git seems to ignore
+ * the existence of lonely pack index files but we do not.
+ */
+ if (fstatat(dir_fd, pack_relpath, &sb, 0) == -1) {
+ if (errno == ENOENT) {
+ err = got_error_fmt(GOT_ERR_LONELY_PACKIDX,
+ "%s", relpath);
+ } else
+ err = got_error_from_errno2("fstatat", pack_relpath);
+ goto done;
+ }
+
p = calloc(1, sizeof(*p));
- if (p == NULL)
- return got_error_from_errno("calloc");
+ if (p == NULL) {
+ err = got_error_from_errno("calloc");
+ goto done;
+ }
p->fd = openat(dir_fd, relpath, O_RDONLY | O_NOFOLLOW);
if (p->fd == -1) {
err = got_error_from_errno2("openat", relpath);
free(p);
- return err;
+ goto done;
}
if (fstat(p->fd, &sb) != 0) {
err = got_error_from_errno2("fstat", relpath);
close(p->fd);
free(p);
- return err;
+ goto done;
}
p->len = sb.st_size;
if (p->len < sizeof(p->hdr)) {
err = got_error(GOT_ERR_BAD_PACKIDX);
close(p->fd);
free(p);
- return err;
+ goto done;
}
p->path_packidx = strdup(relpath);
@@ -381,11 +402,12 @@ got_packidx_open(struct got_packidx **packidx,
err = got_packidx_init_hdr(p, verify);
done:
- if (err)
- got_packidx_close(p);
- else
+ if (err) {
+ if (p)
+ got_packidx_close(p);
+ } else
*packidx = p;
-
+ free(pack_relpath);
return err;
}
diff --git a/lib/repository.c b/lib/repository.c
index 15c0782..3d857e3 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -938,8 +938,8 @@ cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
return NULL;
}
-static int
-is_packidx_filename(const char *name, size_t len)
+int
+got_repo_is_packidx_filename(const char *name, size_t len)
{
if (len != GOT_PACKIDX_NAMELEN)
return 0;
@@ -1008,7 +1008,7 @@ got_repo_search_packidx(struct got_packidx **packidx, int *idx,
while ((dent = readdir(packdir)) != NULL) {
int is_cached = 0;
- if (!is_packidx_filename(dent->d_name, dent->d_namlen))
+ if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
continue;
if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
@@ -1276,7 +1276,7 @@ match_packed_object(struct got_object_id **unique_id,
struct got_packidx *packidx;
struct got_object_qid *qid;
- if (!is_packidx_filename(dent->d_name, dent->d_namlen))
+ if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
continue;
if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
@@ -1952,7 +1952,7 @@ got_repo_get_packfile_info(int *npackfiles, int *nobjects,
}
while ((dent = readdir(packdir)) != NULL) {
- if (!is_packidx_filename(dent->d_name, dent->d_namlen))
+ if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
continue;
if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
diff --git a/lib/repository_admin.c b/lib/repository_admin.c
index c5017d5..42e4e1a 100644
--- a/lib/repository_admin.c
+++ b/lib/repository_admin.c
@@ -1157,3 +1157,103 @@ done:
got_object_idset_free(traversed_ids);
return err;
}
+
+static const struct got_error *
+remove_packidx(int dir_fd, const char *relpath)
+{
+ const struct got_error *err, *unlock_err;
+ struct got_lockfile *lf;
+
+ err = got_lockfile_lock(&lf, relpath, dir_fd);
+ if (err)
+ return err;
+ if (unlinkat(dir_fd, relpath, 0) == -1)
+ err = got_error_from_errno("unlinkat");
+ unlock_err = got_lockfile_unlock(lf, dir_fd);
+ return err ? err : unlock_err;
+}
+
+const struct got_error *
+got_repo_remove_lonely_packidx(struct got_repository *repo, int dry_run,
+ got_lonely_packidx_progress_cb progress_cb, void *progress_arg,
+ got_cancel_cb cancel_cb, void *cancel_arg)
+{
+ const struct got_error *err;
+ DIR *packdir = NULL;
+ struct dirent *dent;
+ char *pack_relpath = NULL;
+ int packdir_fd;
+ struct stat sb;
+
+ packdir_fd = openat(got_repo_get_fd(repo),
+ GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
+ if (packdir_fd == -1) {
+ if (errno == ENOENT)
+ return NULL;
+ return got_error_from_errno_fmt("openat: %s/%s",
+ got_repo_get_path_git_dir(repo),
+ GOT_OBJECTS_PACK_DIR);
+ }
+
+ packdir = fdopendir(packdir_fd);
+ if (packdir == NULL) {
+ err = got_error_from_errno("fdopendir");
+ goto done;
+ }
+
+ while ((dent = readdir(packdir)) != NULL) {
+ if (cancel_cb) {
+ err = cancel_cb(cancel_arg);
+ if (err)
+ goto done;
+ }
+
+ if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
+ continue;
+
+ err = got_packidx_get_packfile_path(&pack_relpath,
+ dent->d_name);
+ if (err)
+ goto done;
+
+ if (fstatat(packdir_fd, pack_relpath, &sb, 0) != -1) {
+ free(pack_relpath);
+ pack_relpath = NULL;
+ continue;
+ }
+ if (errno != ENOENT) {
+ err = got_error_from_errno_fmt("fstatat: %s/%s/%s",
+ got_repo_get_path_git_dir(repo),
+ GOT_OBJECTS_PACK_DIR,
+ pack_relpath);
+ goto done;
+ }
+
+ if (!dry_run) {
+ err = remove_packidx(packdir_fd, dent->d_name);
+ if (err)
+ goto done;
+ }
+ if (progress_cb) {
+ char *path;
+ if (asprintf(&path, "%s/%s/%s",
+ got_repo_get_path_git_dir(repo),
+ GOT_OBJECTS_PACK_DIR,
+ dent->d_name) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
+ err = progress_cb(progress_arg, path);
+ free(path);
+ if (err)
+ goto done;
+ }
+ free(pack_relpath);
+ pack_relpath = NULL;
+ }
+done:
+ if (packdir && closedir(packdir) != 0 && err == NULL)
+ err = got_error_from_errno("closedir");
+ free(pack_relpath);
+ return err;
+}
diff --git a/regress/cmdline/cleanup.sh b/regress/cmdline/cleanup.sh
index b39f8ac..aa49076 100755
--- a/regress/cmdline/cleanup.sh
+++ b/regress/cmdline/cleanup.sh
@@ -264,7 +264,103 @@ test_cleanup_precious_objects() {
test_done "$testroot" "$ret"
}
+test_cleanup_missing_pack_file() {
+ local testroot=`test_init cleanup_missing_pack_file`
+
+ # no pack files should exist yet
+ ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+ echo -n > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ gotadmin pack -r $testroot/repo > $testroot/stdout
+ packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
+ packhash=`echo $packname | sed -e 's:^objects/pack/pack-::' \
+ -e 's/.pack$//'`
+
+ # Some freshly cloned Git repositories suffer from lonely pack index
+ # files. Remove the pack file we just wrote to simulate this issue.
+ rm $testroot/repo/.git/objects/pack/pack-$packname
+
+ # cleanup should now refuse to purge objects
+ gotadmin cleanup -q -r $testroot/repo > $testroot/stdout \
+ 2> $testroot/stderr
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ echo "gotadmin cleanup succeeded unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ echo -n "gotadmin: objects/pack/pack-${packhash}.idx: " \
+ > $testroot/stderr.expected
+ echo "pack index has no corresponding pack file" \
+ >> $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ gotadmin cleanup -r $testroot/repo -p -n > $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "gotadmin cleanup failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+ packidx_path=$testroot/repo/.git/objects/pack/pack-${packhash}.idx
+ echo "$packidx_path could be removed" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ gotadmin cleanup -r $testroot/repo -p > $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "gotadmin cleanup failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+ echo "$packidx_path removed" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ # cleanup should now attempt to purge objects
+ gotadmin cleanup -q -r $testroot/repo > $testroot/stdout \
+ 2> $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "gotadmin cleanup failed unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+ test_done "$testroot" "$ret"
+}
+
test_parseargs "$@"
run_test test_cleanup_unreferenced_loose_objects
run_test test_cleanup_redundant_loose_objects
run_test test_cleanup_precious_objects
+run_test test_cleanup_missing_pack_file