@matheusbsbs / matheus-bagliano / commits / e4c7292353
Fork 0

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>

Kjetil Barvik committed Feb 9, 2009 at 21:54 UTC e4c7292353dbef39feac1c6a60c5cde9140520a6
1 file changed +9 -3
entry.c
+9 -3
@@ -94,11 +94,12 @@ static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
94 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
95 {
96 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
97 - int fd, ret;
97 + int fd, ret, fstat_done = 0;
98 char *new;
99 struct strbuf buf = STRBUF_INIT;
100 unsigned long size;
101 size_t wrote, newsize = 0;
102 + struct stat st;
103
104 switch (ce_mode_s_ifmt) {
105 case S_IFREG:
@@ -145,6 +146,11 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
146 }
147
148 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 + }
154 close(fd);
155 free(new);
156 if (wrote != size)
@@ -161,8 +167,8 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
167 }
168
169 if (state->refresh_cache) {
164 - struct stat st;
165 - lstat(ce->name, &st);
170 + if (!fstat_done)
171 + lstat(ce->name, &st);
172 fill_stat_cache_info(ce, &st);
173 }
174 return 0;