git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH v3 06/13] wrapper.c: add and use warn_on_fopen_errors()
Date: Fri, 26 May 2017 12:35:03 +0900	[thread overview]
Message-ID: <20170526033510.1793-7-gitster@pobox.com> (raw)
In-Reply-To: <20170526033510.1793-1-gitster@pobox.com>

From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>

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>
---
 config.c              |  3 +++
 dir.c                 |  6 +++---
 git-compat-util.h     |  2 ++
 t/t1308-config-set.sh |  3 ++-
 wrapper.c             | 10 ++++++++++
 5 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/config.c b/config.c
index b4a3205da3..2894fbb6d0 100644
--- a/config.c
+++ b/config.c
@@ -2640,6 +2640,9 @@ int git_config_rename_section_in_file(const char *config_filename,
 	}
 
 	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 */
 		goto commit_and_out;
 	}
diff --git a/dir.c b/dir.c
index f451bfa48c..be616e884e 100644
--- a/dir.c
+++ b/dir.c
@@ -745,9 +745,9 @@ static int add_excludes(const char *fname, const char *base, int baselen,
 
 	fd = open(fname, O_RDONLY);
 	if (fd < 0 || fstat(fd, &st) < 0) {
-		if (errno != ENOENT)
-			warn_on_inaccessible(fname);
-		if (0 <= fd)
+		if (fd < 0)
+			warn_on_fopen_errors(fname);
+		else
 			close(fd);
 		if (!check_index ||
 		    (buf = read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL)
diff --git a/git-compat-util.h b/git-compat-util.h
index 6be55cf8b3..eb5c18c7fd 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -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 */
 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
 struct tm *git_gmtime(const time_t *);
diff --git a/t/t1308-config-set.sh b/t/t1308-config-set.sh
index 72d5f1f931..df92fdcd40 100755
--- a/t/t1308-config-set.sh
+++ b/t/t1308-config-set.sh
@@ -195,7 +195,8 @@ test_expect_success POSIXPERM,SANITY 'proper error on non-accessible files' '
 	chmod -r .git/config &&
 	test_when_finished "chmod +r .git/config" &&
 	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
 '
 
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;
-- 
2.13.0-491-g71cfeddc25


  parent reply	other threads:[~2017-05-26  3:35 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-22  6:11 What's cooking in git.git (May 2017, #06; Mon, 22) Junio C Hamano
2017-05-22 17:42 ` Jonathan Nieder
2017-05-23  3:33   ` Junio C Hamano
2017-05-23  5:02     ` Jonathan Nieder
2017-05-23  5:14       ` Junio C Hamano
2017-05-25 12:58 ` Duy Nguyen
2017-05-26  2:56   ` Junio C Hamano
2017-05-26  3:34   ` [PATCH v3 00/13] reporting unexpected errors after (f)open Junio C Hamano
2017-05-26  3:34     ` [PATCH v3 01/13] git_fopen: fix a sparse 'not declared' warning Junio C Hamano
2017-05-26  3:34     ` [PATCH v3 02/13] use xfopen() in more places Junio C Hamano
2017-05-26  3:35     ` [PATCH v3 03/13] clone: use xfopen() instead of fopen() Junio C Hamano
2017-05-26  3:35     ` [PATCH v3 04/13] config.mak.uname: set FREAD_READS_DIRECTORIES for Linux and FreeBSD Junio C Hamano
2017-05-26  3:35     ` [PATCH v3 05/13] config.mak.uname: set FREAD_READS_DIRECTORIES for Darwin, too Junio C Hamano
2017-05-30 18:51       ` Stefan Beller
2017-05-30 23:14         ` Junio C Hamano
2017-05-30 23:32           ` Stefan Beller
2017-05-26  3:35     ` Junio C Hamano [this message]
2017-05-26  3:35     ` [PATCH v3 07/13] wrapper.c: add and use fopen_or_warn() Junio C Hamano
2017-05-26  3:35     ` [PATCH v3 08/13] wrapper.c: make warn_on_inaccessible() static Junio C Hamano
2017-05-26  3:35     ` [PATCH v3 09/13] print errno when reporting a system call error Junio C Hamano
2017-05-26  3:35     ` [PATCH v3 10/13] rerere.c: move error_errno() closer to the source system call Junio C Hamano
2017-05-30 18:58       ` Stefan Beller
2017-05-26  3:35     ` [PATCH v3 11/13] log: fix memory leak in open_next_file() Junio C Hamano
2017-05-26  3:35     ` [PATCH v3 12/13] wrapper: factor out is_missing_file_error() Junio C Hamano
2017-05-30  0:31       ` [PATCH 1/2] compat-util: is_missing_file_error() Junio C Hamano
2017-05-30  0:32         ` [PATCH 2/2] treewide: use is_missing_file_error() where ENOENT and ENOTDIR are checked Junio C Hamano
2017-05-26  3:35     ` [PATCH v3 13/13] is_missing_file_error(): work around EINVAL on Windows Junio C Hamano
2017-05-29 20:25       ` [PATCH 1/2] mingw: verify that paths are not mistaken for remote nicknames Johannes Sixt
2017-05-29 20:27         ` [PATCH 2/2] mingw_fopen: report ENOENT for invalid file names Johannes Sixt
2017-05-29 20:40         ` [PATCH 1/2] mingw: verify that paths are not mistaken for remote nicknames Ævar Arnfjörð Bjarmason
2017-05-29 21:02           ` Johannes Sixt
2017-05-29 21:59             ` Ramsay Jones
2017-05-30  0:03               ` Junio C Hamano
2017-05-30 13:40                 ` Ramsay Jones
2017-05-29 23:53             ` Junio C Hamano
2017-05-30  4:46             ` Junio C Hamano
2017-05-30 20:35               ` Johannes Sixt
2017-05-30  0:29         ` Junio C Hamano
2017-05-30 19:13       ` [PATCH v3 13/13] is_missing_file_error(): work around EINVAL on Windows Stefan Beller
2017-05-30 23:17         ` Junio C Hamano
2017-05-30 23:32           ` Stefan Beller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170526033510.1793-7-gitster@pobox.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).