unpack-trees: handle lstat failure for existing directory
authorJonathan Nieder <jrnieder@gmail.com>
Thu, 13 Jan 2011 02:26:36 +0000 (20:26 -0600)
committerJunio C Hamano <gitster@pobox.com>
Thu, 13 Jan 2011 19:25:32 +0000 (11:25 -0800)
When check_leading_path notices no file in the way of the new entry to
be checked out, verify_absent checks whether there is a directory
there or nothing at all.  If that lstat call fails (for example due to
ENOMEM), it assumes ENOENT, meaning a directory with untracked files
would be clobbered in that case.

Check errno after calling lstat, and for conditions other than ENOENT,
just error out.

This is a theoretical race condition.  lstat has to succeed moments
before it fails for there to be trouble.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
unpack-trees.c

index d5a453079a02ff8b7a63a3453d07935e0807c005..3011d9b904015afea46b85da8124729a1b77bd4a 100644 (file)
@@ -1197,12 +1197,16 @@ static int verify_absent_1(struct cache_entry *ce,
 
                return check_ok_to_remove(path, len, DT_UNKNOWN, NULL, &st,
                                error_type, o);
-       } else if (!lstat(ce->name, &st))
+       } else if (lstat(ce->name, &st)) {
+               if (errno != ENOENT)
+                       return error("cannot stat '%s': %s", ce->name,
+                                    strerror(errno));
+               return 0;
+       } else {
                return check_ok_to_remove(ce->name, ce_namelen(ce),
-                               ce_to_dtype(ce), ce, &st,
-                               error_type, o);
-
-       return 0;
+                                         ce_to_dtype(ce), ce, &st,
+                                         error_type, o);
+       }
 }
 
 static int verify_absent(struct cache_entry *ce,