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>
This commit is contained in:
parent
11dc1fcb3f
commit
e9d983f116
7
attr.c
7
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;
|
||||
|
2
bisect.c
2
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;
|
||||
|
||||
|
@ -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;
|
||||
|
2
commit.c
2
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;
|
||||
|
2
config.c
2
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);
|
||||
|
@ -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)))
|
||||
|
8
ident.c
8
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");
|
||||
|
4
remote.c
4
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;
|
||||
|
2
rerere.c
2
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;
|
||||
|
@ -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))
|
||||
|
@ -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. */
|
||||
|
||||
|
@ -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
|
||||
'
|
||||
|
@ -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
|
||||
'
|
||||
|
||||
|
11
wrapper.c
11
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;
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user