Skip to content

Commit 23a9e07

Browse files
pcloudsgitster
authored andcommitted
use xfopen() in more places
xfopen() - provides error details - explains error on reading, or writing, or whatever operation - has l10n support - prints file name in the error Some of these are missing in the places that are replaced with xfopen(), which is a clear win. In some other places, it's just less code (not as clearly a win as the previous case but still is). The only slight regresssion is in remote-testsvn, where we don't report the file class (marks files) in the error messages anymore. But since this is a _test_ svn remote transport, I'm not too concerned. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b0a642a commit 23a9e07

File tree

10 files changed

+13
-39
lines changed

10 files changed

+13
-39
lines changed

bisect.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,7 @@ static void read_bisect_paths(struct argv_array *array)
438438
{
439439
struct strbuf str = STRBUF_INIT;
440440
const char *filename = git_path_bisect_names();
441-
FILE *fp = fopen(filename, "r");
442-
443-
if (!fp)
444-
die_errno(_("Could not open file '%s'"), filename);
441+
FILE *fp = xfopen(filename, "r");
445442

446443
while (strbuf_getline_lf(&str, fp) != EOF) {
447444
strbuf_trim(&str);

builtin/am.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,12 +1275,8 @@ static int parse_mail(struct am_state *state, const char *mail)
12751275
die("BUG: invalid value for state->scissors");
12761276
}
12771277

1278-
mi.input = fopen(mail, "r");
1279-
if (!mi.input)
1280-
die("could not open input");
1281-
mi.output = fopen(am_path(state, "info"), "w");
1282-
if (!mi.output)
1283-
die("could not open output 'info'");
1278+
mi.input = xfopen(mail, "r");
1279+
mi.output = xfopen(am_path(state, "info"), "w");
12841280
if (mailinfo(&mi, am_path(state, "msg"), am_path(state, "patch")))
12851281
die("could not parse patch");
12861282

builtin/commit.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,10 +1695,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16951695
if (!reflog_msg)
16961696
reflog_msg = "commit (merge)";
16971697
pptr = commit_list_append(current_head, pptr);
1698-
fp = fopen(git_path_merge_head(), "r");
1699-
if (fp == NULL)
1700-
die_errno(_("could not open '%s' for reading"),
1701-
git_path_merge_head());
1698+
fp = xfopen(git_path_merge_head(), "r");
17021699
while (strbuf_getline_lf(&m, fp) != EOF) {
17031700
struct commit *parent;
17041701

builtin/fast-export.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -905,9 +905,7 @@ static void export_marks(char *file)
905905
static void import_marks(char *input_file)
906906
{
907907
char line[512];
908-
FILE *f = fopen(input_file, "r");
909-
if (!f)
910-
die_errno("cannot read '%s'", input_file);
908+
FILE *f = xfopen(input_file, "r");
911909

912910
while (fgets(line, sizeof(line), f)) {
913911
uint32_t mark;

builtin/fsck.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@ static void check_unreachable_object(struct object *obj)
280280
free(filename);
281281
return;
282282
}
283-
if (!(f = fopen(filename, "w")))
284-
die_errno("Could not open '%s'", filename);
283+
f = xfopen(filename, "w");
285284
if (obj->type == OBJ_BLOB) {
286285
if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
287286
die_errno("Could not write '%s'", filename);

builtin/merge.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,7 @@ static int suggest_conflicts(void)
839839
struct strbuf msgbuf = STRBUF_INIT;
840840

841841
filename = git_path_merge_msg();
842-
fp = fopen(filename, "a");
843-
if (!fp)
844-
die_errno(_("Could not open '%s' for writing"), filename);
842+
fp = xfopen(filename, "a");
845843

846844
append_conflicts_hint(&msgbuf);
847845
fputs(msgbuf.buf, fp);

builtin/pull.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ static void get_merge_heads(struct oid_array *merge_heads)
337337
struct strbuf sb = STRBUF_INIT;
338338
struct object_id oid;
339339

340-
if (!(fp = fopen(filename, "r")))
341-
die_errno(_("could not open '%s' for reading"), filename);
340+
fp = xfopen(filename, "r");
342341
while (strbuf_getline_lf(&sb, fp) != EOF) {
343342
if (get_oid_hex(sb.buf, &oid))
344343
continue; /* invalid line: does not start with SHA1 */

diff.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4071,9 +4071,7 @@ int diff_opt_parse(struct diff_options *options,
40714071
DIFF_OPT_CLR(options, FUNCCONTEXT);
40724072
else if ((argcount = parse_long_opt("output", av, &optarg))) {
40734073
char *path = prefix_filename(prefix, optarg);
4074-
options->file = fopen(path, "w");
4075-
if (!options->file)
4076-
die_errno("Could not open '%s'", path);
4074+
options->file = xfopen(path, "w");
40774075
options->close_file = 1;
40784076
if (options->use_color != GIT_COLOR_ALWAYS)
40794077
options->use_color = GIT_COLOR_NEVER;
@@ -4807,9 +4805,7 @@ void diff_flush(struct diff_options *options)
48074805
*/
48084806
if (options->close_file)
48094807
fclose(options->file);
4810-
options->file = fopen("/dev/null", "w");
4811-
if (!options->file)
4812-
die_errno("Could not open /dev/null");
4808+
options->file = xfopen("/dev/null", "w");
48134809
options->close_file = 1;
48144810
for (i = 0; i < q->nr; i++) {
48154811
struct diff_filepair *p = q->queue[i];

fast-import.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3274,9 +3274,7 @@ static void option_export_pack_edges(const char *edges)
32743274
{
32753275
if (pack_edges)
32763276
fclose(pack_edges);
3277-
pack_edges = fopen(edges, "a");
3278-
if (!pack_edges)
3279-
die_errno("Cannot open '%s'", edges);
3277+
pack_edges = xfopen(edges, "a");
32803278
}
32813279

32823280
static int parse_one_option(const char *option)

remote-testsvn.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,8 @@ static int note2mark_cb(const unsigned char *object_sha1,
124124
static void regenerate_marks(void)
125125
{
126126
int ret;
127-
FILE *marksfile = fopen(marksfilename, "w+");
127+
FILE *marksfile = xfopen(marksfilename, "w+");
128128

129-
if (!marksfile)
130-
die_errno("Couldn't create mark file %s.", marksfilename);
131129
ret = for_each_note(NULL, 0, note2mark_cb, marksfile);
132130
if (ret)
133131
die("Regeneration of marks failed, returned %d.", ret);
@@ -148,9 +146,7 @@ static void check_or_regenerate_marks(int latestrev)
148146
marksfile = fopen(marksfilename, "r");
149147
if (!marksfile) {
150148
regenerate_marks();
151-
marksfile = fopen(marksfilename, "r");
152-
if (!marksfile)
153-
die_errno("cannot read marks file %s!", marksfilename);
149+
marksfile = xfopen(marksfilename, "r");
154150
fclose(marksfile);
155151
} else {
156152
strbuf_addf(&sb, ":%d ", latestrev);

0 commit comments

Comments
 (0)