Skip to content

Commit 3096b2e

Browse files
peffgitster
authored andcommitted
check_and_freshen_file: fix reversed success-check
When we want to write out a loose object file, we have always first made sure we don't already have the object somewhere. Since 33d4221 (write_sha1_file: freshen existing objects, 2014-10-15), we also update the timestamp on the file, so that a simultaneous prune knows somebody is likely to reference it soon. If our utime() call fails, we treat this the same as not having the object in the first place; the safe thing to do is write out another copy. However, the loose-object check accidentally inverts the utime() check; it returns failure _only_ when the utime() call actually succeeded. Thus it was failing to protect us there, and in the normal case where utime() succeeds, it caused us to pointlessly write out and link the object. This passed our freshening tests, because writing out the new object is certainly _one_ way of updating its utime. So the normal case was inefficient, but not wrong. While we're here, let's also drop a comment in front of the check_and_freshen functions, making a note of their return type (since it is not our usual "0 for success, -1 for error"). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 33d4221 commit 3096b2e

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

sha1_file.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,18 +442,26 @@ void prepare_alt_odb(void)
442442
read_info_alternates(get_object_directory(), 0);
443443
}
444444

445+
/* Returns 1 if we have successfully freshened the file, 0 otherwise. */
445446
static int freshen_file(const char *fn)
446447
{
447448
struct utimbuf t;
448449
t.actime = t.modtime = time(NULL);
449450
return !utime(fn, &t);
450451
}
451452

453+
/*
454+
* All of the check_and_freshen functions return 1 if the file exists and was
455+
* freshened (if freshening was requested), 0 otherwise. If they return
456+
* 0, you should not assume that it is safe to skip a write of the object (it
457+
* either does not exist on disk, or has a stale mtime and may be subject to
458+
* pruning).
459+
*/
452460
static int check_and_freshen_file(const char *fn, int freshen)
453461
{
454462
if (access(fn, F_OK))
455463
return 0;
456-
if (freshen && freshen_file(fn))
464+
if (freshen && !freshen_file(fn))
457465
return 0;
458466
return 1;
459467
}

0 commit comments

Comments
 (0)