summaryrefslogtreecommitdiff
path: root/sha1_file.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2015-07-08 20:33:52 (GMT)
committerJunio C Hamano <gitster@pobox.com>2015-07-08 22:58:28 (GMT)
commit3096b2ecdb91c27cb9e64b692c480deed6c7e77e (patch)
treebb4a0d16e25322070d0ed5d2524c1ce88e156117 /sha1_file.c
parent33d4221c79c89844bed6b9558cc2bc497251ef70 (diff)
downloadgit-3096b2ecdb91c27cb9e64b692c480deed6c7e77e.zip
git-3096b2ecdb91c27cb9e64b692c480deed6c7e77e.tar.gz
git-3096b2ecdb91c27cb9e64b692c480deed6c7e77e.tar.bz2
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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'sha1_file.c')
-rw-r--r--sha1_file.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/sha1_file.c b/sha1_file.c
index c632641..5475d78 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -442,6 +442,7 @@ void prepare_alt_odb(void)
read_info_alternates(get_object_directory(), 0);
}
+/* Returns 1 if we have successfully freshened the file, 0 otherwise. */
static int freshen_file(const char *fn)
{
struct utimbuf t;
@@ -449,11 +450,18 @@ static int freshen_file(const char *fn)
return !utime(fn, &t);
}
+/*
+ * All of the check_and_freshen functions return 1 if the file exists and was
+ * freshened (if freshening was requested), 0 otherwise. If they return
+ * 0, you should not assume that it is safe to skip a write of the object (it
+ * either does not exist on disk, or has a stale mtime and may be subject to
+ * pruning).
+ */
static int check_and_freshen_file(const char *fn, int freshen)
{
if (access(fn, F_OK))
return 0;
- if (freshen && freshen_file(fn))
+ if (freshen && !freshen_file(fn))
return 0;
return 1;
}