Skip to content

Commit 11dc1fc

Browse files
pcloudsgitster
authored andcommitted
wrapper.c: add and use warn_on_fopen_errors()
In many places, Git warns about an inaccessible file after a fopen() failed. To discern these cases from other cases where we want to warn about inaccessible files, introduce a new helper specifically to test whether fopen() failed because the current user lacks the permission to open file in question. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8e178ec commit 11dc1fc

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,6 +2640,9 @@ int git_config_rename_section_in_file(const char *config_filename,
26402640
}
26412641

26422642
if (!(config_file = fopen(config_filename, "rb"))) {
2643+
ret = warn_on_fopen_errors(config_filename);
2644+
if (ret)
2645+
goto out;
26432646
/* no config file means nothing to rename, no error */
26442647
goto commit_and_out;
26452648
}

dir.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,9 @@ static int add_excludes(const char *fname, const char *base, int baselen,
745745

746746
fd = open(fname, O_RDONLY);
747747
if (fd < 0 || fstat(fd, &st) < 0) {
748-
if (errno != ENOENT)
749-
warn_on_inaccessible(fname);
750-
if (0 <= fd)
748+
if (fd < 0)
749+
warn_on_fopen_errors(fname);
750+
else
751751
close(fd);
752752
if (!check_index ||
753753
(buf = read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL)

git-compat-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,8 @@ int access_or_die(const char *path, int mode, unsigned flag);
11011101

11021102
/* Warn on an inaccessible file that ought to be accessible */
11031103
void warn_on_inaccessible(const char *path);
1104+
/* Warn on an inaccessible file if errno indicates this is an error */
1105+
int warn_on_fopen_errors(const char *path);
11041106

11051107
#ifdef GMTIME_UNRELIABLE_ERRORS
11061108
struct tm *git_gmtime(const time_t *);

t/t1308-config-set.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ test_expect_success POSIXPERM,SANITY 'proper error on non-accessible files' '
195195
chmod -r .git/config &&
196196
test_when_finished "chmod +r .git/config" &&
197197
echo "Error (-1) reading configuration file .git/config." >expect &&
198-
test_expect_code 2 test-config configset_get_value foo.bar .git/config 2>actual &&
198+
test_expect_code 2 test-config configset_get_value foo.bar .git/config 2>output &&
199+
grep "^Error" output >actual &&
199200
test_cmp expect actual
200201
'
201202

wrapper.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ FILE *fopen_for_writing(const char *path)
418418
return ret;
419419
}
420420

421+
int warn_on_fopen_errors(const char *path)
422+
{
423+
if (errno != ENOENT && errno != ENOTDIR) {
424+
warn_on_inaccessible(path);
425+
return -1;
426+
}
427+
428+
return 0;
429+
}
430+
421431
int xmkstemp(char *template)
422432
{
423433
int fd;

0 commit comments

Comments
 (0)