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 <gitster@pobox.com>
Junio C Hamano committed
May 26, 2017 at 12:09 UTC
dc5a18b3643553cacd6f1a3e4bff6a678b067055
1 file changed
+15
git-compat-util.h
+15
@@ -1115,6 +1115,21 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);
1115
#define getc_unlocked(fh) getc(fh)
1116
#endif
1117
1118
+/*
1119
+ * Our code often opens a path to an optional file, to work on its
1120
+ * contents when we can successfully open it. We can ignore a failure
1121
+ * to open if such an optional file does not exist, but we do want to
1122
+ * report a failure in opening for other reasons (e.g. we got an I/O
1123
+ * error, or the file is there, but we lack the permission to open).
1124
+ *
1125
+ * Call this function after seeing an error from open() or fopen() to
1126
+ * see if the errno indicates a missing file that we can safely ignore.
1127
+ */
1128
+static inline int is_missing_file_error(int errno_)
1129
+{
1130
+ return (errno_ == ENOENT || errno_ == ENOTDIR);
1131
+}
1132
+
1133
extern int cmd_main(int, const char **);
1134
1135
#endif