OSDN Git Service

wrapper.c: add and use fopen_or_warn()
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Wed, 3 May 2017 10:16:50 +0000 (17:16 +0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 26 May 2017 03:33:56 +0000 (12:33 +0900)
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>
15 files changed:
attr.c
bisect.c
builtin/blame.c
commit.c
config.c
git-compat-util.h
ident.c
remote.c
rerere.c
sequencer.c
server-info.c
t/t1308-config-set.sh
t/t5512-ls-remote.sh
wrapper.c
wt-status.c

diff --git a/attr.c b/attr.c
index 7e21344..821203e 100644 (file)
--- a/attr.c
+++ b/attr.c
@@ -720,16 +720,13 @@ void git_attr_set_direction(enum git_attr_direction new_direction,
 
 static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
 {
-       FILE *fp = fopen(path, "r");
+       FILE *fp = fopen_or_warn(path, "r");
        struct attr_stack *res;
        char buf[2048];
        int lineno = 0;
 
-       if (!fp) {
-               if (errno != ENOENT && errno != ENOTDIR)
-                       warn_on_inaccessible(path);
+       if (!fp)
                return NULL;
-       }
        res = xcalloc(1, sizeof(*res));
        while (fgets(buf, sizeof(buf), fp)) {
                char *bufp = buf;
index 469a3e9..bb28bf6 100644 (file)
--- a/bisect.c
+++ b/bisect.c
@@ -666,7 +666,7 @@ static int is_expected_rev(const struct object_id *oid)
        if (stat(filename, &st) || !S_ISREG(st.st_mode))
                return 0;
 
-       fp = fopen(filename, "r");
+       fp = fopen_or_warn(filename, "r");
        if (!fp)
                return 0;
 
index 07506a3..34445d7 100644 (file)
@@ -2071,7 +2071,7 @@ static int prepare_lines(struct scoreboard *sb)
  */
 static int read_ancestry(const char *graft_file)
 {
-       FILE *fp = fopen(graft_file, "r");
+       FILE *fp = fopen_or_warn(graft_file, "r");
        struct strbuf buf = STRBUF_INIT;
        if (!fp)
                return -1;
index 73c78c2..3eeda08 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -167,7 +167,7 @@ bad_graft_data:
 
 static int read_graft_file(const char *graft_file)
 {
-       FILE *fp = fopen(graft_file, "r");
+       FILE *fp = fopen_or_warn(graft_file, "r");
        struct strbuf buf = STRBUF_INIT;
        if (!fp)
                return -1;
index 2894fbb..e54d99d 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1422,7 +1422,7 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
        int ret = -1;
        FILE *f;
 
-       f = fopen(filename, "r");
+       f = fopen_or_warn(filename, "r");
        if (f) {
                flockfile(f);
                ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename, filename, f, data);
index eb5c18c..f74b401 100644 (file)
@@ -802,6 +802,7 @@ extern int xmkstemp(char *template);
 extern int xmkstemp_mode(char *template, int mode);
 extern char *xgetcwd(void);
 extern FILE *fopen_for_writing(const char *path);
+extern FILE *fopen_or_warn(const char *path, const char *mode);
 
 #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
diff --git a/ident.c b/ident.c
index bea871c..91c7609 100644 (file)
--- a/ident.c
+++ b/ident.c
@@ -72,12 +72,10 @@ static int add_mailname_host(struct strbuf *buf)
        FILE *mailname;
        struct strbuf mailnamebuf = STRBUF_INIT;
 
-       mailname = fopen("/etc/mailname", "r");
-       if (!mailname) {
-               if (errno != ENOENT)
-                       warning_errno("cannot open /etc/mailname");
+       mailname = fopen_or_warn("/etc/mailname", "r");
+       if (!mailname)
                return -1;
-       }
+
        if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
                if (ferror(mailname))
                        warning_errno("cannot read /etc/mailname");
index 801137c..1f2453d 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -251,7 +251,7 @@ static const char *skip_spaces(const char *s)
 static void read_remotes_file(struct remote *remote)
 {
        struct strbuf buf = STRBUF_INIT;
-       FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
+       FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r");
 
        if (!f)
                return;
@@ -277,7 +277,7 @@ static void read_branches_file(struct remote *remote)
 {
        char *frag;
        struct strbuf buf = STRBUF_INIT;
-       FILE *f = fopen(git_path("branches/%s", remote->name), "r");
+       FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
 
        if (!f)
                return;
index 3bd55ca..971bfed 100644 (file)
--- a/rerere.c
+++ b/rerere.c
@@ -200,7 +200,7 @@ static struct rerere_id *new_rerere_id(unsigned char *sha1)
 static void read_rr(struct string_list *rr)
 {
        struct strbuf buf = STRBUF_INIT;
-       FILE *in = fopen(git_path_merge_rr(), "r");
+       FILE *in = fopen_or_warn(git_path_merge_rr(), "r");
 
        if (!in)
                return;
index 10c3b4f..11b5c7c 100644 (file)
@@ -897,8 +897,8 @@ static void flush_rewritten_pending(void) {
        FILE *out;
 
        if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), 82) > 0 &&
-                       !get_sha1("HEAD", newsha1) &&
-                       (out = fopen(rebase_path_rewritten_list(), "a"))) {
+           !get_sha1("HEAD", newsha1) &&
+           (out = fopen_or_warn(rebase_path_rewritten_list(), "a"))) {
                char *bol = buf.buf, *eol;
 
                while (*bol) {
@@ -917,7 +917,7 @@ static void flush_rewritten_pending(void) {
 
 static void record_in_rewritten(struct object_id *oid,
                enum todo_command next_command) {
-       FILE *out = fopen(rebase_path_rewritten_pending(), "a");
+       FILE *out = fopen_or_warn(rebase_path_rewritten_pending(), "a");
 
        if (!out)
                return;
@@ -1378,7 +1378,7 @@ static int read_populate_todo(struct todo_list *todo_list,
 
        if (is_rebase_i(opts)) {
                struct todo_list done = TODO_LIST_INIT;
-               FILE *f = fopen(rebase_path_msgtotal(), "w");
+               FILE *f = fopen_or_warn(rebase_path_msgtotal(), "w");
 
                if (strbuf_read_file(&done.buf, rebase_path_done(), 0) > 0 &&
                                !parse_insn_buffer(done.buf.buf, &done))
index f6c1a3d..e01ac15 100644 (file)
@@ -133,7 +133,7 @@ static int read_pack_info_file(const char *infofile)
        char line[1000];
        int old_cnt = 0;
 
-       fp = fopen(infofile, "r");
+       fp = fopen_or_warn(infofile, "r");
        if (!fp)
                return 1; /* nonexistent is not an error. */
 
index df92fdc..e495a61 100755 (executable)
@@ -187,6 +187,7 @@ test_expect_success 'proper error on directory "files"' '
        echo "Error (-1) reading configuration file a-directory." >expect &&
        mkdir a-directory &&
        test_expect_code 2 test-config configset_get_value foo.bar a-directory 2>output &&
+       grep "^warning:" output &&
        grep "^Error" output >actual &&
        test_cmp expect actual
 '
@@ -196,6 +197,7 @@ test_expect_success POSIXPERM,SANITY 'proper error on non-accessible files' '
        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>output &&
+       grep "^warning:" output &&
        grep "^Error" output >actual &&
        test_cmp expect actual
 '
index 94fc9be..02106c9 100755 (executable)
@@ -85,8 +85,15 @@ test_expect_success 'use branch.<name>.remote if possible' '
 '
 
 test_expect_success 'confuses pattern as remote when no remote specified' '
-       cat >exp <<-\EOF &&
-       fatal: '\''refs*master'\'' does not appear to be a git repository
+       if test_have_prereq MINGW
+       then
+               # Windows does not like asterisks in pathname
+               does_not_exist=master
+       else
+               does_not_exist="refs*master"
+       fi &&
+       cat >exp <<-EOF &&
+       fatal: '\''$does_not_exist'\'' does not appear to be a git repository
        fatal: Could not read from remote repository.
 
        Please make sure you have the correct access rights
@@ -98,7 +105,7 @@ test_expect_success 'confuses pattern as remote when no remote specified' '
        # fetch <branch>.
        # We could just as easily have used "master"; the "*" emphasizes its
        # role as a pattern.
-       test_must_fail git ls-remote refs*master >actual 2>&1 &&
+       test_must_fail git ls-remote "$does_not_exist" >actual 2>&1 &&
        test_i18ncmp exp actual
 '
 
index 20c25e7..6e513c9 100644 (file)
--- 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;
index 0375484..cdf9f5e 100644 (file)
@@ -1065,7 +1065,8 @@ static void show_am_in_progress(struct wt_status *s,
 static char *read_line_from_git_path(const char *filename)
 {
        struct strbuf buf = STRBUF_INIT;
-       FILE *fp = fopen(git_path("%s", filename), "r");
+       FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
+
        if (!fp) {
                strbuf_release(&buf);
                return NULL;