From: Luciano Rocha Date: Sun, 20 Apr 2008 11:15:31 GMT Subject: [PATCH 02/02/RFC] make use of the stat cache Message-ID: <20080420111531.GB13411@bit.office.eurotux.com> In-Reply-To: <20080420111346.GA13411@bit.office.eurotux.com> Replace stat/lstat calls with cstat/clstat. Signed-off-by: Luciano Rocha --- Makefile | 2 ++ builtin-apply.c | 13 +++++++------ builtin-blame.c | 7 ++++--- builtin-clean.c | 3 ++- builtin-commit.c | 7 ++++--- builtin-count-objects.c | 3 ++- builtin-diff.c | 3 ++- builtin-fetch-pack.c | 5 +++-- builtin-grep.c | 3 ++- builtin-init-db.c | 11 ++++++----- builtin-ls-files.c | 3 ++- builtin-mailsplit.c | 3 ++- builtin-merge-recursive.c | 3 ++- builtin-mv.c | 9 +++++---- builtin-pack-objects.c | 3 ++- builtin-prune.c | 5 +++-- builtin-rerere.c | 15 ++++++++------- builtin-rm.c | 3 ++- builtin-update-index.c | 3 ++- check-racy.c | 3 ++- combine-diff.c | 3 ++- daemon.c | 3 ++- diff-lib.c | 5 +++-- diff.c | 9 +++++---- dir.c | 7 ++++--- entry.c | 11 ++++++----- help.c | 5 +++-- http-push.c | 3 ++- http-walker.c | 3 ++- path.c | 9 +++++---- read-cache.c | 7 ++++--- refs.c | 11 ++++++----- setup.c | 5 +++-- sha1_file.c | 15 ++++++++------- sha1_name.c | 5 +++-- symlinks.c | 3 ++- test-chmtime.c | 3 ++- transport.c | 3 ++- unpack-trees.c | 7 ++++--- xdiff-interface.c | 3 ++- 40 files changed, 134 insertions(+), 93 deletions(-) diff --git a/Makefile b/Makefile index 2cf38f0..7f01b71 100644 --- a/Makefile +++ b/Makefile @@ -374,6 +374,7 @@ LIB_H += tree.h LIB_H += tree-walk.h LIB_H += unpack-trees.h LIB_H += utf8.h +LIB_H += stat-cache.h LIB_OBJS += alias.o LIB_OBJS += alloc.o @@ -465,6 +466,7 @@ LIB_OBJS += write_or_die.o LIB_OBJS += ws.o LIB_OBJS += wt-status.o LIB_OBJS += xdiff-interface.o +LIB_OBJS += stat-cache.o BUILTIN_OBJS += builtin-add.o BUILTIN_OBJS += builtin-annotate.o diff --git a/builtin-apply.c b/builtin-apply.c index caa3f2a..bedc7f4 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -12,6 +12,7 @@ #include "blob.h" #include "delta.h" #include "builtin.h" +#include "stat-cache.h" /* * --check turns on checking that the working tree matches the @@ -2237,7 +2238,7 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry * static int check_to_create_blob(const char *new_name, int ok_if_exists) { struct stat nst; - if (!lstat(new_name, &nst)) { + if (!clstat(new_name, &nst)) { if (S_ISDIR(nst.st_mode) || ok_if_exists) return 0; /* @@ -2289,7 +2290,7 @@ static int check_patch(struct patch *patch, struct patch *prev_patch) unsigned st_mode = 0; if (!cached) - stat_ret = lstat(old_name, &st); + stat_ret = clstat(old_name, &st); if (check_index) { int pos = cache_name_pos(old_name, strlen(old_name)); if (pos < 0) @@ -2311,7 +2312,7 @@ static int check_patch(struct patch *patch, struct patch *prev_patch) if (checkout_entry(ce, &costate, NULL) || - lstat(old_name, &st)) + clstat(old_name, &st)) return -1; } if (!cached && verify_index_match(ce, &st)) @@ -2632,7 +2633,7 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned die("corrupt patch for subproject %s", path); } else { if (!cached) { - if (lstat(path, &st) < 0) + if (clstat(path, &st) < 0) die("unable to stat newly created file %s", path); fill_stat_cache_info(ce, &st); @@ -2651,7 +2652,7 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf, if (S_ISGITLINK(mode)) { struct stat st; - if (!lstat(path, &st) && S_ISDIR(st.st_mode)) + if (!clstat(path, &st) && S_ISDIR(st.st_mode)) return 0; return mkdir(path, 0777); } @@ -2703,7 +2704,7 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned * used to be. */ struct stat st; - if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path))) + if (!clstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path))) errno = EEXIST; } diff --git a/builtin-blame.c b/builtin-blame.c index bfd562d..ca52fa8 100644 --- a/builtin-blame.c +++ b/builtin-blame.c @@ -18,6 +18,7 @@ #include "cache-tree.h" #include "path-list.h" #include "mailmap.h" +#include "stat-cache.h" static char blame_usage[] = "git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S ] [-M] [-C] [-C] [--contents ] [--incremental] [commit] [--] file\n" @@ -1879,7 +1880,7 @@ static void sanity_check_refcnt(struct scoreboard *sb) static int has_path_in_work_tree(const char *path) { struct stat st; - return !lstat(path, &st); + return !clstat(path, &st); } static unsigned parse_score(const char *arg) @@ -2038,12 +2039,12 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con unsigned long fin_size; if (contents_from) { - if (stat(contents_from, &st) < 0) + if (cstat(contents_from, &st) < 0) die("Cannot stat %s", contents_from); read_from = contents_from; } else { - if (lstat(path, &st) < 0) + if (clstat(path, &st) < 0) die("Cannot lstat %s", path); read_from = path; } diff --git a/builtin-clean.c b/builtin-clean.c index 6778a03..97a8ec6 100644 --- a/builtin-clean.c +++ b/builtin-clean.c @@ -11,6 +11,7 @@ #include "dir.h" #include "parse-options.h" #include "quote.h" +#include "stat-cache.h" static int force = -1; /* unset */ @@ -123,7 +124,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix) * recursive directory removal, so lstat() here could * fail with ENOENT. */ - if (lstat(ent->name, &st)) + if (clstat(ent->name, &st)) continue; if (pathspec) { diff --git a/builtin-commit.c b/builtin-commit.c index bcb7aaa..f5ce6f1 100644 --- a/builtin-commit.c +++ b/builtin-commit.c @@ -23,6 +23,7 @@ #include "parse-options.h" #include "path-list.h" #include "unpack-trees.h" +#include "stat-cache.h" static const char * const builtin_commit_usage[] = { "git-commit [options] [--] ...", @@ -430,15 +431,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix) strbuf_add(&sb, buffer + 2, strlen(buffer + 2)); hook_arg1 = "commit"; hook_arg2 = use_message; - } else if (!stat(git_path("MERGE_MSG"), &statbuf)) { + } else if (!cstat(git_path("MERGE_MSG"), &statbuf)) { if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0) die("could not read MERGE_MSG: %s", strerror(errno)); hook_arg1 = "merge"; - } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) { + } else if (!cstat(git_path("SQUASH_MSG"), &statbuf)) { if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0) die("could not read SQUASH_MSG: %s", strerror(errno)); hook_arg1 = "squash"; - } else if (template_file && !stat(template_file, &statbuf)) { + } else if (template_file && !cstat(template_file, &statbuf)) { if (strbuf_read_file(&sb, template_file, 0) < 0) die("could not read %s: %s", template_file, strerror(errno)); diff --git a/builtin-count-objects.c b/builtin-count-objects.c index f00306f..fd4832e 100644 --- a/builtin-count-objects.c +++ b/builtin-count-objects.c @@ -7,6 +7,7 @@ #include "cache.h" #include "builtin.h" #include "parse-options.h" +#include "stat-cache.h" static void count_objects(DIR *d, char *path, int len, int verbose, unsigned long *loose, @@ -40,7 +41,7 @@ static void count_objects(DIR *d, char *path, int len, int verbose, memcpy(path + len + 3, ent->d_name, 38); path[len + 2] = '/'; path[len + 41] = 0; - if (lstat(path, &st) || !S_ISREG(st.st_mode)) + if (clstat(path, &st) || !S_ISREG(st.st_mode)) bad = 1; else (*loose_size) += xsize_t(st.st_blocks); diff --git a/builtin-diff.c b/builtin-diff.c index 7c2a841..fab2f79 100644 --- a/builtin-diff.c +++ b/builtin-diff.c @@ -13,6 +13,7 @@ #include "revision.h" #include "log-tree.h" #include "builtin.h" +#include "stat-cache.h" struct blobinfo { unsigned char sha1[20]; @@ -69,7 +70,7 @@ static int builtin_diff_b_f(struct rev_info *revs, if (argc > 1) usage(builtin_diff_usage); - if (lstat(path, &st)) + if (clstat(path, &st)) die("'%s': %s", path, strerror(errno)); if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) die("'%s': not a regular file or symlink", path); diff --git a/builtin-fetch-pack.c b/builtin-fetch-pack.c index 65350ca..b3aba25 100644 --- a/builtin-fetch-pack.c +++ b/builtin-fetch-pack.c @@ -9,6 +9,7 @@ #include "fetch-pack.h" #include "remote.h" #include "run-command.h" +#include "stat-cache.h" static int transfer_unpack_limit = -1; static int fetch_unpack_limit = -1; @@ -780,7 +781,7 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args, fetch_pack_setup(); memcpy(&args, my_args, sizeof(args)); if (args.depth > 0) { - if (stat(git_path("shallow"), &st)) + if (cstat(git_path("shallow"), &st)) st.st_mtime = 0; } @@ -801,7 +802,7 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args, #ifdef USE_NSEC mtime.usec = st.st_mtim.usec; #endif - if (stat(shallow, &st)) { + if (cstat(shallow, &st)) { if (mtime.sec) die("shallow file was removed during fetch"); } else if (st.st_mtime != mtime.sec diff --git a/builtin-grep.c b/builtin-grep.c index ef29910..1fd1c58 100644 --- a/builtin-grep.c +++ b/builtin-grep.c @@ -11,6 +11,7 @@ #include "tree-walk.h" #include "builtin.h" #include "grep.h" +#include "stat-cache.h" #ifndef NO_EXTERNAL_GREP #ifdef __unix__ @@ -132,7 +133,7 @@ static int grep_file(struct grep_opt *opt, const char *filename) char *data; size_t sz; - if (lstat(filename, &st) < 0) { + if (clstat(filename, &st) < 0) { err_ret: if (errno != ENOENT) error("'%s': %s", filename, strerror(errno)); diff --git a/builtin-init-db.c b/builtin-init-db.c index 2854868..3060c1a 100644 --- a/builtin-init-db.c +++ b/builtin-init-db.c @@ -6,6 +6,7 @@ #include "cache.h" #include "builtin.h" #include "exec_cmd.h" +#include "stat-cache.h" #ifndef DEFAULT_GIT_TEMPLATE_DIR #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates" @@ -56,14 +57,14 @@ static void copy_templates_1(char *path, int baselen, die("insanely long template name %s", de->d_name); memcpy(path + baselen, de->d_name, namelen+1); memcpy(template + template_baselen, de->d_name, namelen+1); - if (lstat(path, &st_git)) { + if (clstat(path, &st_git)) { if (errno != ENOENT) die("cannot stat %s", path); } else exists = 1; - if (lstat(template, &st_template)) + if (clstat(template, &st_template)) die("cannot stat template %s", template); if (S_ISDIR(st_template.st_mode)) { @@ -235,10 +236,10 @@ static int create_default_files(const char *git_dir, const char *template_path) /* Check filemode trustability */ filemode = TEST_FILEMODE; - if (TEST_FILEMODE && !lstat(path, &st1)) { + if (TEST_FILEMODE && !clstat(path, &st1)) { struct stat st2; filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && - !lstat(path, &st2) && + !clstat(path, &st2) && st1.st_mode != st2.st_mode); } git_config_set("core.filemode", filemode ? "true" : "false"); @@ -262,7 +263,7 @@ static int create_default_files(const char *git_dir, const char *template_path) if (!close(xmkstemp(path)) && !unlink(path) && !symlink("testing", path) && - !lstat(path, &st1) && + !clstat(path, &st1) && S_ISLNK(st1.st_mode)) unlink(path); /* good */ else diff --git a/builtin-ls-files.c b/builtin-ls-files.c index dc7eab8..be4a0fa 100644 --- a/builtin-ls-files.c +++ b/builtin-ls-files.c @@ -10,6 +10,7 @@ #include "dir.h" #include "builtin.h" #include "tree.h" +#include "stat-cache.h" static int abbrev; static int show_deleted; @@ -256,7 +257,7 @@ static void show_files(struct dir_struct *dir, const char *prefix) int dtype = ce_to_dtype(ce); if (excluded(dir, ce->name, &dtype) != dir->show_ignored) continue; - err = lstat(ce->name, &st); + err = clstat(ce->name, &st); if (show_deleted && err) show_ce_entry(tag_removed, ce); if (show_modified && ce_modified(ce, &st, 0)) diff --git a/builtin-mailsplit.c b/builtin-mailsplit.c index 46b27cd..838c52f 100644 --- a/builtin-mailsplit.c +++ b/builtin-mailsplit.c @@ -7,6 +7,7 @@ #include "cache.h" #include "builtin.h" #include "path-list.h" +#include "stat-cache.h" static const char git_mailsplit_usage[] = "git-mailsplit [-d] [-f] [-b] -o |..."; @@ -278,7 +279,7 @@ int cmd_mailsplit(int argc, const char **argv, const char *prefix) continue; } - if (stat(arg, &argstat) == -1) { + if (cstat(arg, &argstat) == -1) { error("cannot stat %s (%s)", arg, strerror(errno)); return 1; } diff --git a/builtin-merge-recursive.c b/builtin-merge-recursive.c index 910c0d2..5eb2407 100644 --- a/builtin-merge-recursive.c +++ b/builtin-merge-recursive.c @@ -19,6 +19,7 @@ #include "interpolate.h" #include "attr.h" #include "merge-recursive.h" +#include "stat-cache.h" static int subtree_merge; @@ -470,7 +471,7 @@ static char *unique_path(const char *path, const char *branch) *p = '_'; while (path_list_has_path(¤t_file_set, newpath) || path_list_has_path(¤t_directory_set, newpath) || - lstat(newpath, &st) == 0) + clstat(newpath, &st) == 0) sprintf(p, "_%d", suffix++); path_list_insert(newpath, ¤t_file_set); diff --git a/builtin-mv.c b/builtin-mv.c index 94f6dd2..807984a 100644 --- a/builtin-mv.c +++ b/builtin-mv.c @@ -9,6 +9,7 @@ #include "cache-tree.h" #include "path-list.h" #include "parse-options.h" +#include "stat-cache.h" static const char * const builtin_mv_usage[] = { "git-mv [options] ... ", @@ -98,7 +99,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) if (dest_path[0][0] == '\0') /* special case: "." was normalized to "" */ destination = copy_pathspec(dest_path[0], argv, argc, 1); - else if (!lstat(dest_path[0], &st) && + else if (!clstat(dest_path[0], &st) && S_ISDIR(st.st_mode)) { dest_path[0] = add_slash(dest_path[0]); destination = copy_pathspec(dest_path[0], argv, argc, 1); @@ -118,13 +119,13 @@ int cmd_mv(int argc, const char **argv, const char *prefix) printf("Checking rename of '%s' to '%s'\n", src, dst); length = strlen(src); - if (lstat(src, &st) < 0) + if (clstat(src, &st) < 0) bad = "bad source"; else if (!strncmp(src, dst, length) && (dst[length] == 0 || dst[length] == '/')) { bad = "can not move directory into itself"; } else if ((src_is_dir = S_ISDIR(st.st_mode)) - && lstat(dst, &st) == 0) + && clstat(dst, &st) == 0) bad = "cannot move directory over file"; else if (src_is_dir) { const char *src_w_slash = add_slash(src); @@ -177,7 +178,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) } argc += last - first; } - } else if (lstat(dst, &st) == 0) { + } else if (clstat(dst, &st) == 0) { bad = "destination exists"; if (force) { /* diff --git a/builtin-pack-objects.c b/builtin-pack-objects.c index 777f272..50da2fa 100644 --- a/builtin-pack-objects.c +++ b/builtin-pack-objects.c @@ -16,6 +16,7 @@ #include "list-objects.h" #include "progress.h" #include "refs.h" +#include "stat-cache.h" #ifdef THREADED_DELTA_SEARCH #include "thread-utils.h" @@ -530,7 +531,7 @@ static void write_pack_file(void) * packs then we should modify the mtime of later ones * to preserve this property. */ - if (stat(tmpname, &st) < 0) { + if (cstat(tmpname, &st) < 0) { warning("failed to stat %s: %s", tmpname, strerror(errno)); } else if (!last_mtime) { diff --git a/builtin-prune.c b/builtin-prune.c index 25f9304..ca4f636 100644 --- a/builtin-prune.c +++ b/builtin-prune.c @@ -5,6 +5,7 @@ #include "builtin.h" #include "reachable.h" #include "parse-options.h" +#include "stat-cache.h" static const char * const prune_usage[] = { "git-prune [-n] [--expire