Skip to content

Commit ba8bd83

Browse files
peffgitster
authored andcommitted
config: warn on inaccessible files
Before reading a config file, we check "!access(path, R_OK)" to make sure that the file exists and is readable. If it's not, then we silently ignore it. For the case of ENOENT, this is fine, as the presence of the file is optional. For other cases, though, it may indicate a configuration error (e.g., not having permissions to read the file). Let's print a warning in these cases to let the user know. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 889d358 commit ba8bd83

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

builtin/config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
396396
*/
397397
die("$HOME not set");
398398

399-
if (access(user_config, R_OK) &&
400-
xdg_config && !access(xdg_config, R_OK))
399+
if (access_or_warn(user_config, R_OK) &&
400+
xdg_config && !access_or_warn(xdg_config, R_OK))
401401
given_config_file = xdg_config;
402402
else
403403
given_config_file = user_config;

config.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static int handle_path_include(const char *path, struct config_include_data *inc
6060
path = buf.buf;
6161
}
6262

63-
if (!access(path, R_OK)) {
63+
if (!access_or_warn(path, R_OK)) {
6464
if (++inc->depth > MAX_INCLUDE_DEPTH)
6565
die(include_depth_advice, MAX_INCLUDE_DEPTH, path,
6666
cf && cf->name ? cf->name : "the command line");
@@ -939,23 +939,23 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
939939

940940
home_config_paths(&user_config, &xdg_config, "config");
941941

942-
if (git_config_system() && !access(git_etc_gitconfig(), R_OK)) {
942+
if (git_config_system() && !access_or_warn(git_etc_gitconfig(), R_OK)) {
943943
ret += git_config_from_file(fn, git_etc_gitconfig(),
944944
data);
945945
found += 1;
946946
}
947947

948-
if (xdg_config && !access(xdg_config, R_OK)) {
948+
if (xdg_config && !access_or_warn(xdg_config, R_OK)) {
949949
ret += git_config_from_file(fn, xdg_config, data);
950950
found += 1;
951951
}
952952

953-
if (user_config && !access(user_config, R_OK)) {
953+
if (user_config && !access_or_warn(user_config, R_OK)) {
954954
ret += git_config_from_file(fn, user_config, data);
955955
found += 1;
956956
}
957957

958-
if (repo_config && !access(repo_config, R_OK)) {
958+
if (repo_config && !access_or_warn(repo_config, R_OK)) {
959959
ret += git_config_from_file(fn, repo_config, data);
960960
found += 1;
961961
}

git-compat-util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,9 @@ int rmdir_or_warn(const char *path);
604604
*/
605605
int remove_or_warn(unsigned int mode, const char *path);
606606

607+
/* Call access(2), but warn for any error besides ENOENT. */
608+
int access_or_warn(const char *path, int mode);
609+
607610
/* Get the passwd entry for the UID of the current process. */
608611
struct passwd *xgetpwuid_self(void);
609612

wrapper.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,14 @@ int remove_or_warn(unsigned int mode, const char *file)
403403
return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
404404
}
405405

406+
int access_or_warn(const char *path, int mode)
407+
{
408+
int ret = access(path, mode);
409+
if (ret && errno != ENOENT)
410+
warning(_("unable to access '%s': %s"), path, strerror(errno));
411+
return ret;
412+
}
413+
406414
struct passwd *xgetpwuid_self(void)
407415
{
408416
struct passwd *pw;

0 commit comments

Comments
 (0)