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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Aug 21, 2012 at 02:10 UTC
ba8bd8300a544959159f6bd3a7e03ac54f85ea3a
4 files changed
+18
-7
builtin/config.c
+2
-2
@@ -396,8 +396,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
396
*/
397
die("$HOME not set");
398
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))
401
given_config_file = xdg_config;
402
else
403
given_config_file = user_config;
config.c
+5
-5
@@ -60,7 +60,7 @@ static int handle_path_include(const char *path, struct config_include_data *inc
60
path = buf.buf;
61
}
62
63
- if (!access(path, R_OK)) {
63
+ if (!access_or_warn(path, R_OK)) {
64
if (++inc->depth > MAX_INCLUDE_DEPTH)
65
die(include_depth_advice, MAX_INCLUDE_DEPTH, path,
66
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)
939
940
home_config_paths(&user_config, &xdg_config, "config");
941
942
- if (git_config_system() && !access(git_etc_gitconfig(), R_OK)) {
942
+ if (git_config_system() && !access_or_warn(git_etc_gitconfig(), R_OK)) {
943
ret += git_config_from_file(fn, git_etc_gitconfig(),
944
data);
945
found += 1;
946
}
947
948
- if (xdg_config && !access(xdg_config, R_OK)) {
948
+ if (xdg_config && !access_or_warn(xdg_config, R_OK)) {
949
ret += git_config_from_file(fn, xdg_config, data);
950
found += 1;
951
}
952
953
- if (user_config && !access(user_config, R_OK)) {
953
+ if (user_config && !access_or_warn(user_config, R_OK)) {
954
ret += git_config_from_file(fn, user_config, data);
955
found += 1;
956
}
957
958
- if (repo_config && !access(repo_config, R_OK)) {
958
+ if (repo_config && !access_or_warn(repo_config, R_OK)) {
959
ret += git_config_from_file(fn, repo_config, data);
960
found += 1;
961
}
git-compat-util.h
+3
@@ -604,6 +604,9 @@ int rmdir_or_warn(const char *path);
604
*/
605
int remove_or_warn(unsigned int mode, const char *path);
606
607
+/* Call access(2), but warn for any error besides ENOENT. */
608
+int access_or_warn(const char *path, int mode);
609
+
610
/* Get the passwd entry for the UID of the current process. */
611
struct passwd *xgetpwuid_self(void);
612
wrapper.c
+8
@@ -403,6 +403,14 @@ int remove_or_warn(unsigned int mode, const char *file)
403
return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
404
}
405
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
+
414
struct passwd *xgetpwuid_self(void)
415
{
416
struct passwd *pw;