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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed May 3, 2017 at 17:16 UTC 11dc1fcb3fa53f5a46486daa7cb38ed387153f2e
5 files changed +20 -4
config.c
+3
@@ -2640,6 +2640,9 @@ int git_config_rename_section_in_file(const char *config_filename,
2640 }
2641
2642 if (!(config_file = fopen(config_filename, "rb"))) {
2643 + ret = warn_on_fopen_errors(config_filename);
2644 + if (ret)
2645 + goto out;
2646 /* no config file means nothing to rename, no error */
2647 goto commit_and_out;
2648 }
dir.c
+3 -3
@@ -745,9 +745,9 @@ static int add_excludes(const char *fname, const char *base, int baselen,
745
746 fd = open(fname, O_RDONLY);
747 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
751 close(fd);
752 if (!check_index ||
753 (buf = read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL)
git-compat-util.h
+2
@@ -1101,6 +1101,8 @@ int access_or_die(const char *path, int mode, unsigned flag);
1101
1102 /* Warn on an inaccessible file that ought to be accessible */
1103 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);
1106
1107 #ifdef GMTIME_UNRELIABLE_ERRORS
1108 struct tm *git_gmtime(const time_t *);
t/t1308-config-set.sh
+2 -1
@@ -195,7 +195,8 @@ test_expect_success POSIXPERM,SANITY 'proper error on non-accessible files' '
195 chmod -r .git/config &&
196 test_when_finished "chmod +r .git/config" &&
197 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 &&
200 test_cmp expect actual
201 '
202
wrapper.c
+10
@@ -418,6 +418,16 @@ FILE *fopen_for_writing(const char *path)
418 return ret;
419 }
420
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 +
431 int xmkstemp(char *template)
432 {
433 int fd;