prevent a race where 'gotadmin cleanup' deletes concurrently created objects
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
diff --git a/gotadmin/gotadmin.1 b/gotadmin/gotadmin.1
index 7447f80..5507389 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 p Oc Oo Fl n Oc Oo Fl r Ar repository-path Oc Oo Fl q Oc
+.It Cm cleanup Oo Fl a Oc 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
@@ -258,6 +258,13 @@ The options for
.Cm gotadmin cleanup
are as follows:
.Bl -tag -width Ds
+.It Fl a
+Delete all loose objects.
+By default, objects which are newer than an implementation-defined
+modification timestamp are kept on disk to prevent race conditions
+with other commands that add new objects to the repository while
+.Cm gotadmin cleanup
+is running.
.It Fl p
Instead of purging unreferenced loose objects, remove any pack index files
which do not have a corresponding pack file.
diff --git a/gotadmin/gotadmin.c b/gotadmin/gotadmin.c
index 810a5cc..928fcd8 100644
--- a/gotadmin/gotadmin.c
+++ b/gotadmin/gotadmin.c
@@ -897,7 +897,7 @@ done:
__dead static void
usage_cleanup(void)
{
- fprintf(stderr, "usage: %s cleanup [-p] [-n] [-r repository-path] "
+ fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
"[-q]\n", getprogname());
exit(1);
}
@@ -989,7 +989,7 @@ 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;
+ int remove_lonely_packidx = 0, ignore_mtime = 0;
struct got_cleanup_progress_arg cpa;
struct got_lonely_packidx_progress_arg lpa;
off_t size_before, size_after;
@@ -999,8 +999,11 @@ cmd_cleanup(int argc, char *argv[])
char **extensions;
int nextensions, i;
- while ((ch = getopt(argc, argv, "pr:nq")) != -1) {
+ while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
switch (ch) {
+ case 'a':
+ ignore_mtime = 1;
+ break;
case 'p':
remove_lonely_packidx = 1;
break;
@@ -1071,7 +1074,7 @@ cmd_cleanup(int argc, char *argv[])
cpa.dry_run = dry_run;
cpa.verbosity = verbosity;
error = got_repo_purge_unreferenced_loose_objects(repo,
- &size_before, &size_after, &npacked, dry_run,
+ &size_before, &size_after, &npacked, dry_run, ignore_mtime,
cleanup_progress, &cpa, check_cancelled, NULL);
if (cpa.printed_something)
printf("\n");
diff --git a/include/got_repository_admin.h b/include/got_repository_admin.h
index d1e9be3..8bc7ea5 100644
--- a/include/got_repository_admin.h
+++ b/include/got_repository_admin.h
@@ -76,6 +76,8 @@ typedef const struct got_error *(*got_cleanup_progress_cb)(void *arg,
* Walk objects reachable via references to determine whether any loose
* objects can be removed from disk. Do remove such objects from disk
* unless the dry_run parameter is set.
+ * Do not remove objects with a modification timestamp above an
+ * implementation-defined timestamp threshold, unless ignore_mtime is set.
* Return the disk space size occupied by loose objects before and after
* the operation.
* Return the number of loose objects which are also stored in a pack file.
@@ -83,7 +85,7 @@ typedef const struct got_error *(*got_cleanup_progress_cb)(void *arg,
const struct got_error *
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,
+ int ignore_mtime, 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. */
diff --git a/lib/repository_admin.c b/lib/repository_admin.c
index f315741..deb76e2 100644
--- a/lib/repository_admin.c
+++ b/lib/repository_admin.c
@@ -1028,6 +1028,8 @@ struct purge_loose_object_arg {
int npurged;
off_t size_purged;
int dry_run;
+ time_t max_mtime;
+ int ignore_mtime;
};
static const struct got_error *
@@ -1053,21 +1055,29 @@ purge_loose_object(struct got_object_id *id, void *data, void *arg)
goto done;
}
- if (!a->dry_run) {
- err = got_lockfile_lock(&lf, path, -1);
- if (err)
- goto done;
- if (unlink(path) == -1) {
- err = got_error_from_errno2("unlink", path);
- goto done;
+ /*
+ * Do not delete objects which are younger than our maximum
+ * modification time threshold. This prevents a race where
+ * new objects which are being added to the repository
+ * concurrently would be deleted.
+ */
+ if (a->ignore_mtime || sb.st_mtime <= a->max_mtime) {
+ if (!a->dry_run) {
+ err = got_lockfile_lock(&lf, path, -1);
+ if (err)
+ goto done;
+ if (unlink(path) == -1) {
+ err = got_error_from_errno2("unlink", path);
+ goto done;
+ }
}
- }
- a->npurged++;
- a->size_purged += sb.st_size;
- if (a->progress_cb) {
- err = a->progress_cb(a->progress_arg, a->nloose,
- a->ncommits, a->npurged);
+ a->npurged++;
+ a->size_purged += sb.st_size;
+ if (a->progress_cb) {
+ err = a->progress_cb(a->progress_arg, a->nloose,
+ a->ncommits, a->npurged);
+ }
}
done:
if (fd != -1 && close(fd) == -1 && err == NULL)
@@ -1081,7 +1091,7 @@ done:
const struct got_error *
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,
+ int ignore_mtime, got_cleanup_progress_cb progress_cb, void *progress_arg,
got_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err;
@@ -1090,7 +1100,9 @@ got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
struct got_object_id **referenced_ids;
int i, nreferenced, nloose, ncommits = 0;
struct got_reflist_head refs;
+ struct got_reflist_entry *re;
struct purge_loose_object_arg arg;
+ time_t max_mtime = 0;
TAILQ_INIT(&refs);
@@ -1117,6 +1129,19 @@ got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
err = got_ref_list(&refs, repo, "", got_ref_cmp_by_name, NULL);
if (err)
goto done;
+ if (!ignore_mtime) {
+ TAILQ_FOREACH(re, &refs, entry) {
+ time_t mtime = got_ref_get_mtime(re->ref);
+ if (mtime > max_mtime)
+ max_mtime = mtime;
+ }
+ /*
+ * For safety, keep objects created within 10 minutes
+ * before the youngest reference was created.
+ */
+ if (max_mtime >= 600)
+ max_mtime -= 600;
+ }
err = get_reflist_object_ids(&referenced_ids, &nreferenced,
(1 << GOT_OBJ_TYPE_COMMIT) | (1 << GOT_OBJ_TYPE_TAG),
@@ -1149,6 +1174,8 @@ got_repo_purge_unreferenced_loose_objects(struct got_repository *repo,
arg.size_purged = 0;
arg.ncommits = ncommits;
arg.dry_run = dry_run;
+ arg.max_mtime = max_mtime;
+ arg.ignore_mtime = ignore_mtime;
err = got_object_idset_for_each(loose_ids, purge_loose_object, &arg);
if (err)
goto done;
diff --git a/regress/cmdline/cleanup.sh b/regress/cmdline/cleanup.sh
index 72a0111..b403a13 100755
--- a/regress/cmdline/cleanup.sh
+++ b/regress/cmdline/cleanup.sh
@@ -72,7 +72,7 @@ test_cleanup_unreferenced_loose_objects() {
# cleanup -n should not remove any objects
ls -R $testroot/repo/.git/objects > $testroot/objects-before
- gotadmin cleanup -n -q -r $testroot/repo > $testroot/stdout
+ gotadmin cleanup -a -n -q -r $testroot/repo > $testroot/stdout
echo -n > $testroot/stdout.expected
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
@@ -91,7 +91,7 @@ test_cleanup_unreferenced_loose_objects() {
fi
# cleanup should remove loose objects that belonged to the branch
- gotadmin cleanup -q -r $testroot/repo > $testroot/stdout
+ gotadmin cleanup -a -q -r $testroot/repo > $testroot/stdout
ret="$?"
if [ "$ret" != "0" ]; then
echo "gotadmin cleanup failed unexpectedly" >&2
@@ -169,7 +169,7 @@ test_cleanup_redundant_loose_objects() {
# cleanup -n should not remove any objects
ls -R $testroot/repo/.git/objects > $testroot/objects-before
- gotadmin cleanup -n -q -r $testroot/repo > $testroot/stdout
+ gotadmin cleanup -a -n -q -r $testroot/repo > $testroot/stdout
echo -n > $testroot/stdout.expected
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
@@ -196,7 +196,7 @@ test_cleanup_redundant_loose_objects() {
fi
# cleanup should remove all loose objects
- gotadmin cleanup -q -r $testroot/repo > $testroot/stdout
+ gotadmin cleanup -a -q -r $testroot/repo > $testroot/stdout
ret="$?"
if [ "$ret" != "0" ]; then
echo "gotadmin cleanup failed unexpectedly" >&2
@@ -244,7 +244,7 @@ test_cleanup_precious_objects() {
(cd $testroot/repo && git config extensions.preciousObjects true)
# cleanup should now refuse to purge objects
- gotadmin cleanup -q -r $testroot/repo > $testroot/stdout \
+ gotadmin cleanup -a -q -r $testroot/repo > $testroot/stdout \
2> $testroot/stderr
ret="$?"
if [ "$ret" == "0" ]; then
@@ -294,7 +294,7 @@ test_cleanup_missing_pack_file() {
rm $testroot/repo/.git/objects/pack/pack-$packname
# cleanup should now refuse to purge objects
- gotadmin cleanup -q -r $testroot/repo > $testroot/stdout \
+ gotadmin cleanup -a -q -r $testroot/repo > $testroot/stdout \
2> $testroot/stderr
ret="$?"
if [ "$ret" == "0" ]; then
@@ -317,7 +317,7 @@ test_cleanup_missing_pack_file() {
return 1
fi
- gotadmin cleanup -r $testroot/repo -p -n > $testroot/stdout
+ gotadmin cleanup -a -r $testroot/repo -p -n > $testroot/stdout
ret="$?"
if [ "$ret" != "0" ]; then
echo "gotadmin cleanup failed unexpectedly" >&2
@@ -334,7 +334,7 @@ test_cleanup_missing_pack_file() {
return 1
fi
- gotadmin cleanup -r $testroot/repo -p > $testroot/stdout
+ gotadmin cleanup -a -r $testroot/repo -p > $testroot/stdout
ret="$?"
if [ "$ret" != "0" ]; then
echo "gotadmin cleanup failed unexpectedly" >&2
@@ -351,7 +351,7 @@ test_cleanup_missing_pack_file() {
fi
# cleanup should now attempt to purge objects
- gotadmin cleanup -q -r $testroot/repo > $testroot/stdout \
+ gotadmin cleanup -a -q -r $testroot/repo > $testroot/stdout \
2> $testroot/stderr
ret="$?"
if [ "$ret" != "0" ]; then