reflog-expire: Avoid creating new files in a directory inside readdir(3) loop
"git reflog expire --all" opened a directory in $GIT_DIR/logs/, read reflog files in there readdir(3), and rewrote the file by creating a new file and renaming it back inside the loop. This code structure can cause the newly created file to be returned by subsequent call to readdir(3), and fall into an infinite loop in the worst case. This separates the processing to two phase. Running for_each_reflog() to find out and collect all refs, and then iterate over them, calling expire_reflog(). This way, the program would behave exactly the same way as if all the refs were given by the user from the command line. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Jan 25, 2008 at 23:53 UTC
bda3a31cc7982a7ef8ddb5137359d27c3c75ebed
1 file changed
+38
-2
builtin-reflog.c
+38
-2
@@ -34,6 +34,16 @@ struct expire_reflog_cb {
34
struct cmd_reflog_expire_cb *cmd;
35
};
36
37
+struct collected_reflog {
38
+ unsigned char sha1[20];
39
+ char reflog[FLEX_ARRAY];
40
+};
41
+struct collect_reflog_cb {
42
+ struct collected_reflog **e;
43
+ int alloc;
44
+ int nr;
45
+};
46
+
47
#define INCOMPLETE (1u<<10)
48
#define STUDYING (1u<<11)
49
@@ -281,6 +291,20 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
291
return status;
292
}
293
294
+static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
295
+{
296
+ struct collected_reflog *e;
297
+ struct collect_reflog_cb *cb = cb_data;
298
+ size_t namelen = strlen(ref);
299
+
300
+ e = xmalloc(sizeof(*e) + namelen + 1);
301
+ hashcpy(e->sha1, sha1);
302
+ memcpy(e->reflog, ref, namelen + 1);
303
+ ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
304
+ cb->e[cb->nr++] = e;
305
+ return 0;
306
+}
307
+
308
static int reflog_expire_config(const char *var, const char *value)
309
{
310
if (!strcmp(var, "gc.reflogexpire"))
@@ -349,8 +373,20 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
373
putchar('\n');
374
}
375
352
- if (do_all)
353
- status |= for_each_reflog(expire_reflog, &cb);
376
+ if (do_all) {
377
+ struct collect_reflog_cb collected;
378
+ int i;
379
+
380
+ memset(&collected, 0, sizeof(collected));
381
+ for_each_reflog(collect_reflog, &collected);
382
+ for (i = 0; i < collected.nr; i++) {
383
+ struct collected_reflog *e = collected.e[i];
384
+ status |= expire_reflog(e->reflog, e->sha1, 0, &cb);
385
+ free(e);
386
+ }
387
+ free(collected.e);
388
+ }
389
+
390
while (i < argc) {
391
const char *ref = argv[i++];
392
unsigned char sha1[20];