Skip to content

Commit eb12dd0

Browse files
committed
update-index: stat updated files earlier
In the update_one(), we check verify_path() on the proposed path before doing anything else. In preparation for having verify_path() look at the file mode, let's stat the file earlier, so we can check the mode accurately. This is made a bit trickier by the fact that this function only does an lstat in a few code paths (the ones that flow down through process_path()). So we can speculatively do the lstat() here and pass the results down, and just use a dummy mode for cases where we won't actually be updating the index from the filesystem. Signed-off-by: Jeff King <[email protected]>
1 parent 641084b commit eb12dd0

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

builtin/update-index.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,9 @@ static int process_directory(const char *path, int len, struct stat *st)
358358
return error("%s: is a directory - add files inside instead", path);
359359
}
360360

361-
static int process_path(const char *path)
361+
static int process_path(const char *path, struct stat *st, int stat_errno)
362362
{
363363
int pos, len;
364-
struct stat st;
365364
const struct cache_entry *ce;
366365

367366
len = strlen(path);
@@ -385,13 +384,13 @@ static int process_path(const char *path)
385384
* First things first: get the stat information, to decide
386385
* what to do about the pathname!
387386
*/
388-
if (lstat(path, &st) < 0)
389-
return process_lstat_error(path, errno);
387+
if (stat_errno)
388+
return process_lstat_error(path, stat_errno);
390389

391-
if (S_ISDIR(st.st_mode))
392-
return process_directory(path, len, &st);
390+
if (S_ISDIR(st->st_mode))
391+
return process_directory(path, len, st);
393392

394-
return add_one_path(ce, path, len, &st);
393+
return add_one_path(ce, path, len, st);
395394
}
396395

397396
static int add_cacheinfo(unsigned int mode, const struct object_id *oid,
@@ -443,6 +442,16 @@ static void chmod_path(char flip, const char *path)
443442

444443
static void update_one(const char *path)
445444
{
445+
int stat_errno = 0;
446+
struct stat st;
447+
448+
if (mark_valid_only || mark_skip_worktree_only || force_remove)
449+
st.st_mode = 0;
450+
else if (lstat(path, &st) < 0) {
451+
st.st_mode = 0;
452+
stat_errno = errno;
453+
} /* else stat is valid */
454+
446455
if (!verify_path(path)) {
447456
fprintf(stderr, "Ignoring path %s\n", path);
448457
return;
@@ -464,7 +473,7 @@ static void update_one(const char *path)
464473
report("remove '%s'", path);
465474
return;
466475
}
467-
if (process_path(path))
476+
if (process_path(path, &st, stat_errno))
468477
die("Unable to process path %s", path);
469478
report("add '%s'", path);
470479
}

0 commit comments

Comments
 (0)