Skip to content

Commit e9d983f

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 11dc1fc commit e9d983f

15 files changed

+43
-26
lines changed

attr.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -720,16 +720,13 @@ void git_attr_set_direction(enum git_attr_direction new_direction,
720720

721721
static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
722722
{
723-
FILE *fp = fopen(path, "r");
723+
FILE *fp = fopen_or_warn(path, "r");
724724
struct attr_stack *res;
725725
char buf[2048];
726726
int lineno = 0;
727727

728-
if (!fp) {
729-
if (errno != ENOENT && errno != ENOTDIR)
730-
warn_on_inaccessible(path);
728+
if (!fp)
731729
return NULL;
732-
}
733730
res = xcalloc(1, sizeof(*res));
734731
while (fgets(buf, sizeof(buf), fp)) {
735732
char *bufp = buf;

bisect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ static int is_expected_rev(const struct object_id *oid)
666666
if (stat(filename, &st) || !S_ISREG(st.st_mode))
667667
return 0;
668668

669-
fp = fopen(filename, "r");
669+
fp = fopen_or_warn(filename, "r");
670670
if (!fp)
671671
return 0;
672672

builtin/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,7 @@ static int prepare_lines(struct scoreboard *sb)
20712071
*/
20722072
static int read_ancestry(const char *graft_file)
20732073
{
2074-
FILE *fp = fopen(graft_file, "r");
2074+
FILE *fp = fopen_or_warn(graft_file, "r");
20752075
struct strbuf buf = STRBUF_INIT;
20762076
if (!fp)
20772077
return -1;

commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ struct commit_graft *read_graft_line(char *buf, int len)
167167

168168
static int read_graft_file(const char *graft_file)
169169
{
170-
FILE *fp = fopen(graft_file, "r");
170+
FILE *fp = fopen_or_warn(graft_file, "r");
171171
struct strbuf buf = STRBUF_INIT;
172172
if (!fp)
173173
return -1;

config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
14221422
int ret = -1;
14231423
FILE *f;
14241424

1425-
f = fopen(filename, "r");
1425+
f = fopen_or_warn(filename, "r");
14261426
if (f) {
14271427
flockfile(f);
14281428
ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename, filename, f, data);

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ extern int xmkstemp(char *template);
802802
extern int xmkstemp_mode(char *template, int mode);
803803
extern char *xgetcwd(void);
804804
extern FILE *fopen_for_writing(const char *path);
805+
extern FILE *fopen_or_warn(const char *path, const char *mode);
805806

806807
#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
807808
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))

ident.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ static int add_mailname_host(struct strbuf *buf)
7272
FILE *mailname;
7373
struct strbuf mailnamebuf = STRBUF_INIT;
7474

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)
7977
return -1;
80-
}
78+
8179
if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
8280
if (ferror(mailname))
8381
warning_errno("cannot read /etc/mailname");

remote.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ static const char *skip_spaces(const char *s)
251251
static void read_remotes_file(struct remote *remote)
252252
{
253253
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");
255255

256256
if (!f)
257257
return;
@@ -277,7 +277,7 @@ static void read_branches_file(struct remote *remote)
277277
{
278278
char *frag;
279279
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");
281281

282282
if (!f)
283283
return;

rerere.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static struct rerere_id *new_rerere_id(unsigned char *sha1)
200200
static void read_rr(struct string_list *rr)
201201
{
202202
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");
204204

205205
if (!in)
206206
return;

sequencer.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,8 @@ static void flush_rewritten_pending(void) {
897897
FILE *out;
898898

899899
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"))) {
902902
char *bol = buf.buf, *eol;
903903

904904
while (*bol) {
@@ -917,7 +917,7 @@ static void flush_rewritten_pending(void) {
917917

918918
static void record_in_rewritten(struct object_id *oid,
919919
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");
921921

922922
if (!out)
923923
return;
@@ -1378,7 +1378,7 @@ static int read_populate_todo(struct todo_list *todo_list,
13781378

13791379
if (is_rebase_i(opts)) {
13801380
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");
13821382

13831383
if (strbuf_read_file(&done.buf, rebase_path_done(), 0) > 0 &&
13841384
!parse_insn_buffer(done.buf.buf, &done))

0 commit comments

Comments
 (0)