dir.c: avoid stat() in valid_cached_dir()
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Wed, 24 Jan 2018 09:30:20 +0000 (16:30 +0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Jan 2018 20:40:13 +0000 (12:40 -0800)
stat() may follow a symlink and return stat data of the link's target
instead of the link itself. We are concerned about the link itself.

It's kind of hard to demonstrate the bug. I think when path->buf is a
symlink, we most likely find that its target's stat data does not
match our cached one, which means we ignore the cache and fall back to
slow path.

This is performance issue, not correctness (though we could still
catch it by verifying test-dump-untracked-cache. The less unlikely
case is, link target stat data matches the cached version and we
incorrectly go fast path, ignoring real data on disk. A test for this
may involve manipulating stat data, which may be not portable.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
dir.c
diff --git a/dir.c b/dir.c
index 3c54366a1730ec37fcc56e185e2362e12d89b566..ee1605f004d71c734dfc8e211493706fa39f2493 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -1739,7 +1739,7 @@ static int valid_cached_dir(struct dir_struct *dir,
         */
        refresh_fsmonitor(istate);
        if (!(dir->untracked->use_fsmonitor && untracked->valid)) {
-               if (stat(path->len ? path->buf : ".", &st)) {
+               if (lstat(path->len ? path->buf : ".", &st)) {
                        invalidate_directory(dir->untracked, untracked);
                        memset(&untracked->stat_data, 0, sizeof(untracked->stat_data));
                        return 0;