Skip to content

Commit e4c7292

Browse files
Kjetil Barvikgitster
authored andcommitted
write_entry(): use fstat() instead of lstat() when file is open
Currently inside write_entry() we do an lstat(path, &st) call on a file which have just been opened inside the exact same function. It should be better to call fstat(fd, &st) on the file while it is open, and it should be at least as fast as the lstat() method. Signed-off-by: Kjetil Barvik <barvik@broadpark.no> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 4857c76 commit e4c7292

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

entry.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
9494
static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
9595
{
9696
unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
97-
int fd, ret;
97+
int fd, ret, fstat_done = 0;
9898
char *new;
9999
struct strbuf buf = STRBUF_INIT;
100100
unsigned long size;
101101
size_t wrote, newsize = 0;
102+
struct stat st;
102103

103104
switch (ce_mode_s_ifmt) {
104105
case S_IFREG:
@@ -145,6 +146,11 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
145146
}
146147

147148
wrote = write_in_full(fd, new, size);
149+
/* use fstat() only when path == ce->name */
150+
if (state->refresh_cache && !to_tempfile && !state->base_dir_len) {
151+
fstat(fd, &st);
152+
fstat_done = 1;
153+
}
148154
close(fd);
149155
free(new);
150156
if (wrote != size)
@@ -161,8 +167,8 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
161167
}
162168

163169
if (state->refresh_cache) {
164-
struct stat st;
165-
lstat(ce->name, &st);
170+
if (!fstat_done)
171+
lstat(ce->name, &st);
166172
fill_stat_cache_info(ce, &st);
167173
}
168174
return 0;

0 commit comments

Comments
 (0)