[PATCH] Make nsec checking optional
[git.git] / read-cache.c
index 5453694..f1abae1 100644 (file)
@@ -216,8 +216,25 @@ int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
        int fd;
 
        fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
-       if (fd < 0)
-               return (errno == EEXIST) ? 0 : -1;
+       if (fd < 0) {
+               void *map;
+               static int error(const char * string);
+
+               if (errno != EEXIST)
+                       return -1;
+#ifndef COLLISION_CHECK
+               fd = open(filename, O_RDONLY);
+               if (fd < 0)
+                       return -1;
+               map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
+               if (map == MAP_FAILED)
+                       return -1;
+               if (memcmp(buf, map, size))
+                       return error("SHA1 collision detected!"
+                                       " This is bad, bad, BAD!\a\n");
+#endif
+               return 0;
+       }
        write(fd, buf, size);
        close(fd);
        return 0;
@@ -233,11 +250,20 @@ int cache_match_stat(struct cache_entry *ce, struct stat *st)
 {
        unsigned int changed = 0;
 
-       if (ce->mtime.sec  != (unsigned int)st->st_mtim.tv_sec ||
-           ce->mtime.nsec != (unsigned int)st->st_mtim.tv_nsec)
+       /* nsec seems unreliable - not all filesystems support it, so
+        * as long as it is in the inode cache you get right nsec
+        * but after it gets flushed, you get zero nsec. */
+       if (ce->mtime.sec  != (unsigned int)st->st_mtim.tv_sec
+#ifdef NSEC
+           || ce->mtime.nsec != (unsigned int)st->st_mtim.tv_nsec
+#endif
+           )
                changed |= MTIME_CHANGED;
-       if (ce->ctime.sec  != (unsigned int)st->st_ctim.tv_sec ||
-           ce->ctime.nsec != (unsigned int)st->st_ctim.tv_nsec)
+       if (ce->ctime.sec  != (unsigned int)st->st_ctim.tv_sec
+#ifdef NSEC
+           || ce->ctime.nsec != (unsigned int)st->st_ctim.tv_nsec
+#endif
+           )
                changed |= CTIME_CHANGED;
        if (ce->st_uid != (unsigned int)st->st_uid ||
            ce->st_gid != (unsigned int)st->st_gid)