Merge branch 'jc/noent-notdir'
authorJunio C Hamano <gitster@pobox.com>
Tue, 13 Jun 2017 20:47:06 +0000 (13:47 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 13 Jun 2017 20:47:07 +0000 (13:47 -0700)
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.

* jc/noent-notdir:
treewide: use is_missing_file_error() where ENOENT and ENOTDIR are checked
compat-util: is_missing_file_error()

apply.c
builtin/rm.c
builtin/update-index.c
diff-lib.c
dir.c
git-compat-util.h
setup.c
sha1_name.c
wrapper.c
diff --git a/apply.c b/apply.c
index c49cef0637355d33a7a1636fb283210784f14e02..854faa67795bcd356b33420f46260f208c2ec047 100644 (file)
--- 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;
index 7c323d01235bf8bbb341004c843761dad99590d2..b39f10fcb64f047090967dbf8b31120d2beb1b67 100644 (file)
@@ -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;
index ebfc09faa0d604218af8f5815af5e5fee5915158..f99b1e5790b9b6aeafaa88f438a8a50f1538c2fe 100644 (file)
@@ -257,7 +257,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));
 }
index 2982bf055acb6049a8cd6007244d60d985b7e619..76c8f185cd032b92e010b95e4d92e3b08a6ab18a 100644 (file)
@@ -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 9efcf1eab689bd0e637ea1b961b261eb97862b04..70f2de38f23ce5f9e4c107cfdb0f7901dfd22b84 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -2337,7 +2337,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, '/');
index 4b7dcf21adbe7064963cf446bde7018629ea2c10..e83fd2eb07729561d03566cdc312636525a83ee6 100644 (file)
@@ -1134,6 +1134,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
diff --git a/setup.c b/setup.c
index e3f7699a902aed20a83820067cf913df2f3750a9..ba6e855178847a37044eacceae0ebcb17d8e990b 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -150,7 +150,7 @@ int check_filename(const char *prefix, const char *arg)
                free(to_free);
                return 1; /* file exists */
        }
-       if (errno == ENOENT || errno == ENOTDIR) {
+       if (is_missing_file_error(errno)) {
                free(to_free);
                return 0; /* file does not exist */
        }
index e9ffe685d58366dc3802986519e3e2fd22c151a4..5126853bb5bda5f291039588db96064ffbd1ee5c 100644 (file)
@@ -1408,7 +1408,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,
@@ -1473,7 +1473,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);
 
index d83741770949f457b364896b6ff8632c0a700d69..708e98a96585f8f9a800e9ddae942b94ea7372b8 100644 (file)
--- 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)