Skip to content

Commit 93dd544

Browse files
committed
Merge branch 'jc/noent-notdir'
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()
2 parents 41dd433 + c705420 commit 93dd544

File tree

9 files changed

+25
-10
lines changed

9 files changed

+25
-10
lines changed

apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3741,7 +3741,7 @@ static int check_to_create(struct apply_state *state,
37413741
return 0;
37423742

37433743
return EXISTS_IN_WORKTREE;
3744-
} else if ((errno != ENOENT) && (errno != ENOTDIR)) {
3744+
} else if (!is_missing_file_error(errno)) {
37453745
return error_errno("%s", new_name);
37463746
}
37473747
return 0;

builtin/rm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static int check_local_mod(struct object_id *head, int index_only)
129129
ce = active_cache[pos];
130130

131131
if (lstat(ce->name, &st) < 0) {
132-
if (errno != ENOENT && errno != ENOTDIR)
132+
if (!is_missing_file_error(errno))
133133
warning_errno(_("failed to stat '%s'"), ce->name);
134134
/* It already vanished from the working tree */
135135
continue;

builtin/update-index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ static int remove_one_path(const char *path)
257257
*/
258258
static int process_lstat_error(const char *path, int err)
259259
{
260-
if (err == ENOENT || err == ENOTDIR)
260+
if (is_missing_file_error(err))
261261
return remove_one_path(path);
262262
return error("lstat(\"%s\"): %s", path, strerror(err));
263263
}

diff-lib.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
static int check_removed(const struct cache_entry *ce, struct stat *st)
3030
{
3131
if (lstat(ce->name, st) < 0) {
32-
if (errno != ENOENT && errno != ENOTDIR)
32+
if (!is_missing_file_error(errno))
3333
return -1;
3434
return 1;
3535
}

dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2337,7 +2337,7 @@ int remove_path(const char *name)
23372337
{
23382338
char *slash;
23392339

2340-
if (unlink(name) && errno != ENOENT && errno != ENOTDIR)
2340+
if (unlink(name) && !is_missing_file_error(errno))
23412341
return -1;
23422342

23432343
slash = strrchr(name, '/');

git-compat-util.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,21 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);
11341134
#define getc_unlocked(fh) getc(fh)
11351135
#endif
11361136

1137+
/*
1138+
* Our code often opens a path to an optional file, to work on its
1139+
* contents when we can successfully open it. We can ignore a failure
1140+
* to open if such an optional file does not exist, but we do want to
1141+
* report a failure in opening for other reasons (e.g. we got an I/O
1142+
* error, or the file is there, but we lack the permission to open).
1143+
*
1144+
* Call this function after seeing an error from open() or fopen() to
1145+
* see if the errno indicates a missing file that we can safely ignore.
1146+
*/
1147+
static inline int is_missing_file_error(int errno_)
1148+
{
1149+
return (errno_ == ENOENT || errno_ == ENOTDIR);
1150+
}
1151+
11371152
extern int cmd_main(int, const char **);
11381153

11391154
#endif

setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ int check_filename(const char *prefix, const char *arg)
150150
free(to_free);
151151
return 1; /* file exists */
152152
}
153-
if (errno == ENOENT || errno == ENOTDIR) {
153+
if (is_missing_file_error(errno)) {
154154
free(to_free);
155155
return 0; /* file does not exist */
156156
}

sha1_name.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ static void diagnose_invalid_sha1_path(const char *prefix,
14081408
if (file_exists(filename))
14091409
die("Path '%s' exists on disk, but not in '%.*s'.",
14101410
filename, object_name_len, object_name);
1411-
if (errno == ENOENT || errno == ENOTDIR) {
1411+
if (is_missing_file_error(errno)) {
14121412
char *fullname = xstrfmt("%s%s", prefix, filename);
14131413

14141414
if (!get_tree_entry(tree_sha1, fullname,
@@ -1473,7 +1473,7 @@ static void diagnose_invalid_index_path(int stage,
14731473

14741474
if (file_exists(filename))
14751475
die("Path '%s' exists on disk, but not in the index.", filename);
1476-
if (errno == ENOENT || errno == ENOTDIR)
1476+
if (is_missing_file_error(errno))
14771477
die("Path '%s' does not exist (neither on disk nor in the index).",
14781478
filename);
14791479

wrapper.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,8 @@ void warn_on_inaccessible(const char *path)
583583

584584
static int access_error_is_ok(int err, unsigned flag)
585585
{
586-
return err == ENOENT || err == ENOTDIR ||
587-
((flag & ACCESS_EACCES_OK) && err == EACCES);
586+
return (is_missing_file_error(err) ||
587+
((flag & ACCESS_EACCES_OK) && err == EACCES));
588588
}
589589

590590
int access_or_warn(const char *path, int mode, unsigned flag)

0 commit comments

Comments
 (0)