From dc5a18b3643553cacd6f1a3e4bff6a678b067055 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 26 May 2017 12:09:01 +0900 Subject: [PATCH 1/2] compat-util: is_missing_file_error() Our code often opens a path to an optional file, to work on its contents when we can successfully open it. We can ignore a failure to open if such an optional file does not exist, but we do want to report a failure in opening for other reasons (e.g. we got an I/O error, or the file is there, but we lack the permission to open). The exact errors we need to ignore are ENOENT (obviously) and ENOTDIR (less obvious). Instead of repeating comparison of errno with these two constants, introduce a helper function to do so. Signed-off-by: Junio C Hamano --- git-compat-util.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/git-compat-util.h b/git-compat-util.h index 8a4a3f85e7..8a3c680626 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1115,6 +1115,21 @@ struct tm *git_gmtime_r(const time_t *, struct tm *); #define getc_unlocked(fh) getc(fh) #endif +/* + * Our code often opens a path to an optional file, to work on its + * contents when we can successfully open it. We can ignore a failure + * to open if such an optional file does not exist, but we do want to + * report a failure in opening for other reasons (e.g. we got an I/O + * error, or the file is there, but we lack the permission to open). + * + * Call this function after seeing an error from open() or fopen() to + * see if the errno indicates a missing file that we can safely ignore. + */ +static inline int is_missing_file_error(int errno_) +{ + return (errno_ == ENOENT || errno_ == ENOTDIR); +} + extern int cmd_main(int, const char **); #endif From c7054209d65db430bdbcb2243288e63cea3e417c Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 30 May 2017 09:23:33 +0900 Subject: [PATCH 2/2] treewide: use is_missing_file_error() where ENOENT and ENOTDIR are checked Using the is_missing_file_error() helper introduced in the previous step, update all hits from $ git grep -e ENOENT --and -e ENOTDIR There are codepaths that only check ENOENT, and it is possible that some of them should be checking both. Updating them is kept out of this step deliberately, as we do not want to change behaviour in this step. Signed-off-by: Junio C Hamano --- apply.c | 2 +- builtin/rm.c | 2 +- builtin/update-index.c | 2 +- diff-lib.c | 2 +- dir.c | 2 +- setup.c | 2 +- sha1_name.c | 4 ++-- wrapper.c | 4 ++-- 8 files changed, 10 insertions(+), 10 deletions(-) diff --git a/apply.c b/apply.c index 0e2caeab9c..59bb3497de 100644 --- a/apply.c +++ b/apply.c @@ -3741,7 +3741,7 @@ static int check_to_create(struct apply_state *state, return 0; return EXISTS_IN_WORKTREE; - } else if ((errno != ENOENT) && (errno != ENOTDIR)) { + } else if (!is_missing_file_error(errno)) { return error_errno("%s", new_name); } return 0; diff --git a/builtin/rm.c b/builtin/rm.c index fb79dcab18..30c4332c68 100644 --- a/builtin/rm.c +++ b/builtin/rm.c @@ -129,7 +129,7 @@ static int check_local_mod(struct object_id *head, int index_only) ce = active_cache[pos]; if (lstat(ce->name, &st) < 0) { - if (errno != ENOENT && errno != ENOTDIR) + if (!is_missing_file_error(errno)) warning_errno(_("failed to stat '%s'"), ce->name); /* It already vanished from the working tree */ continue; diff --git a/builtin/update-index.c b/builtin/update-index.c index d530e89368..4e9402984a 100644 --- a/builtin/update-index.c +++ b/builtin/update-index.c @@ -253,7 +253,7 @@ static int remove_one_path(const char *path) */ static int process_lstat_error(const char *path, int err) { - if (err == ENOENT || err == ENOTDIR) + if (is_missing_file_error(err)) return remove_one_path(path); return error("lstat(\"%s\"): %s", path, strerror(err)); } diff --git a/diff-lib.c b/diff-lib.c index 52447466b5..88fc71e89e 100644 --- a/diff-lib.c +++ b/diff-lib.c @@ -29,7 +29,7 @@ static int check_removed(const struct cache_entry *ce, struct stat *st) { if (lstat(ce->name, st) < 0) { - if (errno != ENOENT && errno != ENOTDIR) + if (!is_missing_file_error(errno)) return -1; return 1; } diff --git a/dir.c b/dir.c index aeeb5ce104..98efe9d1c5 100644 --- a/dir.c +++ b/dir.c @@ -2235,7 +2235,7 @@ int remove_path(const char *name) { char *slash; - if (unlink(name) && errno != ENOENT && errno != ENOTDIR) + if (unlink(name) && !is_missing_file_error(errno)) return -1; slash = strrchr(name, '/'); diff --git a/setup.c b/setup.c index 8f64fbdfb2..bb6a2c1beb 100644 --- a/setup.c +++ b/setup.c @@ -147,7 +147,7 @@ int check_filename(const char *prefix, const char *arg) name = arg; if (!lstat(name, &st)) return 1; /* file exists */ - if (errno == ENOENT || errno == ENOTDIR) + if (is_missing_file_error(errno)) return 0; /* file does not exist */ die_errno("failed to stat '%s'", arg); } diff --git a/sha1_name.c b/sha1_name.c index 26ceec1d79..af7500037d 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -1406,7 +1406,7 @@ static void diagnose_invalid_sha1_path(const char *prefix, if (file_exists(filename)) die("Path '%s' exists on disk, but not in '%.*s'.", filename, object_name_len, object_name); - if (errno == ENOENT || errno == ENOTDIR) { + if (is_missing_file_error(errno)) { char *fullname = xstrfmt("%s%s", prefix, filename); if (!get_tree_entry(tree_sha1, fullname, @@ -1471,7 +1471,7 @@ static void diagnose_invalid_index_path(int stage, if (file_exists(filename)) die("Path '%s' exists on disk, but not in the index.", filename); - if (errno == ENOENT || errno == ENOTDIR) + if (is_missing_file_error(errno)) die("Path '%s' does not exist (neither on disk nor in the index).", filename); diff --git a/wrapper.c b/wrapper.c index 0542fc7582..2fbbd81359 100644 --- a/wrapper.c +++ b/wrapper.c @@ -583,8 +583,8 @@ void warn_on_inaccessible(const char *path) static int access_error_is_ok(int err, unsigned flag) { - return err == ENOENT || err == ENOTDIR || - ((flag & ACCESS_EACCES_OK) && err == EACCES); + return (is_missing_file_error(err) || + ((flag & ACCESS_EACCES_OK) && err == EACCES)); } int access_or_warn(const char *path, int mode, unsigned flag)