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).

There is a logic to determine if an errno left by open/fopen
indicates such an ignorable error.  Split it out into a helper
function.  We may want to make it an extern in later steps, as many
hits from "git grep ENOENT" would instead want to be using the same
logic.

Signed-off-by: Junio C Hamano <gits...@pobox.com>
---
 wrapper.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/wrapper.c b/wrapper.c
index b117eb14a4..f1c87ec7ea 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -423,9 +423,23 @@ static void warn_on_inaccessible(const char *path)
        warning_errno(_("unable to access '%s'"), path);
 }
 
+/*
+ * 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 int is_missing_file_error(int errno_) {
+       return (errno_ == ENOENT || errno_ == ENOTDIR);
+}
+
 int warn_on_fopen_errors(const char *path)
 {
-       if (errno != ENOENT && errno != ENOTDIR) {
+       if (!is_missing_file_error(errno)) {
                warn_on_inaccessible(path);
                return -1;
        }
-- 
2.13.0-491-g71cfeddc25

Reply via email to