wrapper.c: add and use fopen_or_warn()

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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed May 3, 2017 at 17:16 UTC e9d983f116c7de43f40a49aae60ebfe107f153ec
15 files changed +43 -26
attr.c
+2 -5
@@ -720,16 +720,13 @@ void git_attr_set_direction(enum git_attr_direction new_direction,
720
721 static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
722 {
723 - FILE *fp = fopen(path, "r");
723 + FILE *fp = fopen_or_warn(path, "r");
724 struct attr_stack *res;
725 char buf[2048];
726 int lineno = 0;
727
728 - if (!fp) {
729 - if (errno != ENOENT && errno != ENOTDIR)
730 - warn_on_inaccessible(path);
728 + if (!fp)
729 return NULL;
732 - }
730 res = xcalloc(1, sizeof(*res));
731 while (fgets(buf, sizeof(buf), fp)) {
732 char *bufp = buf;
bisect.c
+1 -1
@@ -666,7 +666,7 @@ static int is_expected_rev(const struct object_id *oid)
666 if (stat(filename, &st) || !S_ISREG(st.st_mode))
667 return 0;
668
669 - fp = fopen(filename, "r");
669 + fp = fopen_or_warn(filename, "r");
670 if (!fp)
671 return 0;
672
builtin/blame.c
+1 -1
@@ -2071,7 +2071,7 @@ static int prepare_lines(struct scoreboard *sb)
2071 */
2072 static int read_ancestry(const char *graft_file)
2073 {
2074 - FILE *fp = fopen(graft_file, "r");
2074 + FILE *fp = fopen_or_warn(graft_file, "r");
2075 struct strbuf buf = STRBUF_INIT;
2076 if (!fp)
2077 return -1;
commit.c
+1 -1
@@ -167,7 +167,7 @@ bad_graft_data:
167
168 static int read_graft_file(const char *graft_file)
169 {
170 - FILE *fp = fopen(graft_file, "r");
170 + FILE *fp = fopen_or_warn(graft_file, "r");
171 struct strbuf buf = STRBUF_INIT;
172 if (!fp)
173 return -1;
config.c
+1 -1
@@ -1422,7 +1422,7 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
1422 int ret = -1;
1423 FILE *f;
1424
1425 - f = fopen(filename, "r");
1425 + f = fopen_or_warn(filename, "r");
1426 if (f) {
1427 flockfile(f);
1428 ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename, filename, f, data);
git-compat-util.h
+1
@@ -802,6 +802,7 @@ extern int xmkstemp(char *template);
802 extern int xmkstemp_mode(char *template, int mode);
803 extern char *xgetcwd(void);
804 extern FILE *fopen_for_writing(const char *path);
805 +extern FILE *fopen_or_warn(const char *path, const char *mode);
806
807 #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
808 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
ident.c
+3 -5
@@ -72,12 +72,10 @@ static int add_mailname_host(struct strbuf *buf)
72 FILE *mailname;
73 struct strbuf mailnamebuf = STRBUF_INIT;
74
75 - mailname = fopen("/etc/mailname", "r");
76 - if (!mailname) {
77 - if (errno != ENOENT)
78 - warning_errno("cannot open /etc/mailname");
75 + mailname = fopen_or_warn("/etc/mailname", "r");
76 + if (!mailname)
77 return -1;
80 - }
78 +
79 if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
80 if (ferror(mailname))
81 warning_errno("cannot read /etc/mailname");
remote.c
+2 -2
@@ -251,7 +251,7 @@ static const char *skip_spaces(const char *s)
251 static void read_remotes_file(struct remote *remote)
252 {
253 struct strbuf buf = STRBUF_INIT;
254 - FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
254 + FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r");
255
256 if (!f)
257 return;
@@ -277,7 +277,7 @@ static void read_branches_file(struct remote *remote)
277 {
278 char *frag;
279 struct strbuf buf = STRBUF_INIT;
280 - FILE *f = fopen(git_path("branches/%s", remote->name), "r");
280 + FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
281
282 if (!f)
283 return;
rerere.c
+1 -1
@@ -200,7 +200,7 @@ static struct rerere_id *new_rerere_id(unsigned char *sha1)
200 static void read_rr(struct string_list *rr)
201 {
202 struct strbuf buf = STRBUF_INIT;
203 - FILE *in = fopen(git_path_merge_rr(), "r");
203 + FILE *in = fopen_or_warn(git_path_merge_rr(), "r");
204
205 if (!in)
206 return;
sequencer.c
+4 -4
@@ -897,8 +897,8 @@ static void flush_rewritten_pending(void) {
897 FILE *out;
898
899 if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), 82) > 0 &&
900 - !get_sha1("HEAD", newsha1) &&
901 - (out = fopen(rebase_path_rewritten_list(), "a"))) {
900 + !get_sha1("HEAD", newsha1) &&
901 + (out = fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
902 char *bol = buf.buf, *eol;
903
904 while (*bol) {
@@ -917,7 +917,7 @@ static void flush_rewritten_pending(void) {
917
918 static void record_in_rewritten(struct object_id *oid,
919 enum todo_command next_command) {
920 - FILE *out = fopen(rebase_path_rewritten_pending(), "a");
920 + FILE *out = fopen_or_warn(rebase_path_rewritten_pending(), "a");
921
922 if (!out)
923 return;
@@ -1378,7 +1378,7 @@ static int read_populate_todo(struct todo_list *todo_list,
1378
1379 if (is_rebase_i(opts)) {
1380 struct todo_list done = TODO_LIST_INIT;
1381 - FILE *f = fopen(rebase_path_msgtotal(), "w");
1381 + FILE *f = fopen_or_warn(rebase_path_msgtotal(), "w");
1382
1383 if (strbuf_read_file(&done.buf, rebase_path_done(), 0) > 0 &&
1384 !parse_insn_buffer(done.buf.buf, &done))
server-info.c
+1 -1
@@ -133,7 +133,7 @@ static int read_pack_info_file(const char *infofile)
133 char line[1000];
134 int old_cnt = 0;
135
136 - fp = fopen(infofile, "r");
136 + fp = fopen_or_warn(infofile, "r");
137 if (!fp)
138 return 1; /* nonexistent is not an error. */
139
t/t1308-config-set.sh
+2
@@ -187,6 +187,7 @@ test_expect_success 'proper error on directory "files"' '
187 echo "Error (-1) reading configuration file a-directory." >expect &&
188 mkdir a-directory &&
189 test_expect_code 2 test-config configset_get_value foo.bar a-directory 2>output &&
190 + grep "^warning:" output &&
191 grep "^Error" output >actual &&
192 test_cmp expect actual
193 '
@@ -196,6 +197,7 @@ test_expect_success POSIXPERM,SANITY 'proper error on non-accessible files' '
197 test_when_finished "chmod +r .git/config" &&
198 echo "Error (-1) reading configuration file .git/config." >expect &&
199 test_expect_code 2 test-config configset_get_value foo.bar .git/config 2>output &&
200 + grep "^warning:" output &&
201 grep "^Error" output >actual &&
202 test_cmp expect actual
203 '
t/t5512-ls-remote.sh
+10 -3
@@ -85,8 +85,15 @@ test_expect_success 'use branch.<name>.remote if possible' '
85 '
86
87 test_expect_success 'confuses pattern as remote when no remote specified' '
88 - cat >exp <<-\EOF &&
89 - fatal: '\''refs*master'\'' does not appear to be a git repository
88 + if test_have_prereq MINGW
89 + then
90 + # Windows does not like asterisks in pathname
91 + does_not_exist=master
92 + else
93 + does_not_exist="refs*master"
94 + fi &&
95 + cat >exp <<-EOF &&
96 + fatal: '\''$does_not_exist'\'' does not appear to be a git repository
97 fatal: Could not read from remote repository.
98
99 Please make sure you have the correct access rights
@@ -98,7 +105,7 @@ test_expect_success 'confuses pattern as remote when no remote specified' '
105 # fetch <branch>.
106 # We could just as easily have used "master"; the "*" emphasizes its
107 # role as a pattern.
101 - test_must_fail git ls-remote refs*master >actual 2>&1 &&
108 + test_must_fail git ls-remote "$does_not_exist" >actual 2>&1 &&
109 test_i18ncmp exp actual
110 '
111
wrapper.c
+11
@@ -428,6 +428,17 @@ int warn_on_fopen_errors(const char *path)
428 return 0;
429 }
430
431 +FILE *fopen_or_warn(const char *path, const char *mode)
432 +{
433 + FILE *fp = fopen(path, mode);
434 +
435 + if (fp)
436 + return fp;
437 +
438 + warn_on_fopen_errors(path);
439 + return NULL;
440 +}
441 +
442 int xmkstemp(char *template)
443 {
444 int fd;
wt-status.c
+2 -1
@@ -1065,7 +1065,8 @@ static void show_am_in_progress(struct wt_status *s,
1065 static char *read_line_from_git_path(const char *filename)
1066 {
1067 struct strbuf buf = STRBUF_INIT;
1068 - FILE *fp = fopen(git_path("%s", filename), "r");
1068 + FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1069 +
1070 if (!fp) {
1071 strbuf_release(&buf);
1072 return NULL;