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>
This commit is contained in:

committed by
Junio C Hamano

parent
8e178ec4d0
commit
11dc1fcb3f
3
config.c
3
config.c
@ -2640,6 +2640,9 @@ int git_config_rename_section_in_file(const char *config_filename,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!(config_file = fopen(config_filename, "rb"))) {
|
if (!(config_file = fopen(config_filename, "rb"))) {
|
||||||
|
ret = warn_on_fopen_errors(config_filename);
|
||||||
|
if (ret)
|
||||||
|
goto out;
|
||||||
/* no config file means nothing to rename, no error */
|
/* no config file means nothing to rename, no error */
|
||||||
goto commit_and_out;
|
goto commit_and_out;
|
||||||
}
|
}
|
||||||
|
6
dir.c
6
dir.c
@ -745,9 +745,9 @@ static int add_excludes(const char *fname, const char *base, int baselen,
|
|||||||
|
|
||||||
fd = open(fname, O_RDONLY);
|
fd = open(fname, O_RDONLY);
|
||||||
if (fd < 0 || fstat(fd, &st) < 0) {
|
if (fd < 0 || fstat(fd, &st) < 0) {
|
||||||
if (errno != ENOENT)
|
if (fd < 0)
|
||||||
warn_on_inaccessible(fname);
|
warn_on_fopen_errors(fname);
|
||||||
if (0 <= fd)
|
else
|
||||||
close(fd);
|
close(fd);
|
||||||
if (!check_index ||
|
if (!check_index ||
|
||||||
(buf = read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL)
|
(buf = read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL)
|
||||||
|
@ -1101,6 +1101,8 @@ int access_or_die(const char *path, int mode, unsigned flag);
|
|||||||
|
|
||||||
/* Warn on an inaccessible file that ought to be accessible */
|
/* Warn on an inaccessible file that ought to be accessible */
|
||||||
void warn_on_inaccessible(const char *path);
|
void warn_on_inaccessible(const char *path);
|
||||||
|
/* Warn on an inaccessible file if errno indicates this is an error */
|
||||||
|
int warn_on_fopen_errors(const char *path);
|
||||||
|
|
||||||
#ifdef GMTIME_UNRELIABLE_ERRORS
|
#ifdef GMTIME_UNRELIABLE_ERRORS
|
||||||
struct tm *git_gmtime(const time_t *);
|
struct tm *git_gmtime(const time_t *);
|
||||||
|
@ -195,7 +195,8 @@ test_expect_success POSIXPERM,SANITY 'proper error on non-accessible files' '
|
|||||||
chmod -r .git/config &&
|
chmod -r .git/config &&
|
||||||
test_when_finished "chmod +r .git/config" &&
|
test_when_finished "chmod +r .git/config" &&
|
||||||
echo "Error (-1) reading configuration file .git/config." >expect &&
|
echo "Error (-1) reading configuration file .git/config." >expect &&
|
||||||
test_expect_code 2 test-config configset_get_value foo.bar .git/config 2>actual &&
|
test_expect_code 2 test-config configset_get_value foo.bar .git/config 2>output &&
|
||||||
|
grep "^Error" output >actual &&
|
||||||
test_cmp expect actual
|
test_cmp expect actual
|
||||||
'
|
'
|
||||||
|
|
||||||
|
10
wrapper.c
10
wrapper.c
@ -418,6 +418,16 @@ FILE *fopen_for_writing(const char *path)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int warn_on_fopen_errors(const char *path)
|
||||||
|
{
|
||||||
|
if (errno != ENOENT && errno != ENOTDIR) {
|
||||||
|
warn_on_inaccessible(path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int xmkstemp(char *template)
|
int xmkstemp(char *template)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
Reference in New Issue
Block a user