From 11dc1fcb3fa53f5a46486daa7cb38ed387153f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 3 May 2017 17:16:49 +0700 Subject: wrapper.c: add and use warn_on_fopen_errors() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano --- wrapper.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'wrapper.c') diff --git a/wrapper.c b/wrapper.c index d837417709..20c25e7e65 100644 --- a/wrapper.c +++ b/wrapper.c @@ -418,6 +418,16 @@ FILE *fopen_for_writing(const char *path) 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 fd; -- cgit v1.2.3 From e9d983f116c7de43f40a49aae60ebfe107f153ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 3 May 2017 17:16:50 +0700 Subject: wrapper.c: add and use fopen_or_warn() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When fopen() returns NULL, it could be because the given path does not exist, but it could also be some other errors and the caller has to check. Add a wrapper so we don't have to repeat the same error check everywhere. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- wrapper.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'wrapper.c') diff --git a/wrapper.c b/wrapper.c index 20c25e7e65..6e513c904a 100644 --- a/wrapper.c +++ b/wrapper.c @@ -428,6 +428,17 @@ int warn_on_fopen_errors(const char *path) return 0; } +FILE *fopen_or_warn(const char *path, const char *mode) +{ + FILE *fp = fopen(path, mode); + + if (fp) + return fp; + + warn_on_fopen_errors(path); + return NULL; +} + int xmkstemp(char *template) { int fd; -- cgit v1.2.3 From 382fb07f7bbb1f94fa3e1293d5df151725a886a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Mon, 8 May 2017 17:40:37 +0700 Subject: wrapper.c: make warn_on_inaccessible() static After the last patch, this function is not used outside anymore. Keep it static. Noticed-by: Ramsay Jones Signed-off-by: Junio C Hamano --- wrapper.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'wrapper.c') diff --git a/wrapper.c b/wrapper.c index 6e513c904a..b117eb14a4 100644 --- a/wrapper.c +++ b/wrapper.c @@ -418,6 +418,11 @@ FILE *fopen_for_writing(const char *path) return ret; } +static void warn_on_inaccessible(const char *path) +{ + warning_errno(_("unable to access '%s'"), path); +} + int warn_on_fopen_errors(const char *path) { if (errno != ENOENT && errno != ENOTDIR) { @@ -597,11 +602,6 @@ int remove_or_warn(unsigned int mode, const char *file) return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file); } -void warn_on_inaccessible(const char *path) -{ - warning_errno(_("unable to access '%s'"), path); -} - static int access_error_is_ok(int err, unsigned flag) { return err == ENOENT || err == ENOTDIR || -- cgit v1.2.3