Skip to content

Commit 66e905b

Browse files
rscharfegitster
authored andcommitted
use xopen() to handle fatal open(2) failures
Add and apply a semantic patch for using xopen() instead of calling open(2) and die() or die_errno() explicitly. This makes the error messages more consistent and shortens the code. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a7439d0 commit 66e905b

File tree

15 files changed

+33
-52
lines changed

15 files changed

+33
-52
lines changed

builtin/add.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
313313
rev.diffopt.output_format = DIFF_FORMAT_PATCH;
314314
rev.diffopt.use_color = 0;
315315
rev.diffopt.flags.ignore_dirty_submodules = 1;
316-
out = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
317-
if (out < 0)
318-
die(_("Could not open '%s' for writing."), file);
316+
out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
319317
rev.diffopt.file = xfdopen(out, "w");
320318
rev.diffopt.close_file = 1;
321319
if (run_diff_files(&rev, 0))

builtin/archive.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313
static void create_output_file(const char *output_file)
1414
{
15-
int output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
16-
if (output_fd < 0)
17-
die_errno(_("could not create archive file '%s'"), output_file);
15+
int output_fd = xopen(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
1816
if (output_fd != 1) {
1917
if (dup2(output_fd, 1) < 0)
2018
die_errno(_("could not redirect output"));

builtin/bugreport.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
171171
get_populated_hooks(&buffer, !startup_info->have_repository);
172172

173173
/* fopen doesn't offer us an O_EXCL alternative, except with glibc. */
174-
report = open(report_path.buf, O_CREAT | O_EXCL | O_WRONLY, 0666);
175-
176-
if (report < 0)
177-
die(_("couldn't create a new file at '%s'"), report_path.buf);
174+
report = xopen(report_path.buf, O_CREAT | O_EXCL | O_WRONLY, 0666);
178175

179176
if (write_in_full(report, buffer.buf, buffer.len) < 0)
180177
die_errno(_("unable to write to %s"), report_path.buf);

builtin/commit-tree.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ static int parse_file_arg_callback(const struct option *opt,
8888
if (!strcmp(arg, "-"))
8989
fd = 0;
9090
else {
91-
fd = open(arg, O_RDONLY);
92-
if (fd < 0)
93-
die_errno(_("git commit-tree: failed to open '%s'"), arg);
91+
fd = xopen(arg, O_RDONLY);
9492
}
9593
if (strbuf_read(buf, fd, 0) < 0)
9694
die_errno(_("git commit-tree: failed to read '%s'"), arg);

builtin/hash-object.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ static void hash_object(const char *path, const char *type, const char *vpath,
5353
unsigned flags, int literally)
5454
{
5555
int fd;
56-
fd = open(path, O_RDONLY);
57-
if (fd < 0)
58-
die_errno("Cannot open '%s'", path);
56+
fd = xopen(path, O_RDONLY);
5957
hash_fd(fd, type, vpath, flags, literally);
6058
}
6159

builtin/index-pack.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,11 @@ static const char *open_pack_file(const char *pack_name)
338338
"pack/tmp_pack_XXXXXX");
339339
pack_name = strbuf_detach(&tmp_file, NULL);
340340
} else {
341-
output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
342-
if (output_fd < 0)
343-
die_errno(_("unable to create '%s'"), pack_name);
341+
output_fd = xopen(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
344342
}
345343
nothread_data.pack_fd = output_fd;
346344
} else {
347-
input_fd = open(pack_name, O_RDONLY);
348-
if (input_fd < 0)
349-
die_errno(_("cannot open packfile '%s'"), pack_name);
345+
input_fd = xopen(pack_name, O_RDONLY);
350346
output_fd = -1;
351347
nothread_data.pack_fd = input_fd;
352348
}

builtin/mailsplit.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
7575
fprintf(stderr, "corrupt mailbox\n");
7676
exit(1);
7777
}
78-
fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
79-
if (fd < 0)
80-
die_errno("cannot open output file '%s'", name);
78+
fd = xopen(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
8179
output = xfdopen(fd, "w");
8280

8381
/* Copy it out, while searching for a line that begins with

builtin/merge.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,9 +1136,7 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge
11361136
merge_names = &fetch_head_file;
11371137

11381138
filename = git_path_fetch_head(the_repository);
1139-
fd = open(filename, O_RDONLY);
1140-
if (fd < 0)
1141-
die_errno(_("could not open '%s' for reading"), filename);
1139+
fd = xopen(filename, O_RDONLY);
11421140

11431141
if (strbuf_read(merge_names, fd, 0) < 0)
11441142
die_errno(_("could not read '%s'"), filename);

builtin/notes.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
172172

173173
/* write the template message before editing: */
174174
d->edit_path = git_pathdup("NOTES_EDITMSG");
175-
fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
176-
if (fd < 0)
177-
die_errno(_("could not create file '%s'"), d->edit_path);
175+
fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
178176

179177
if (d->given)
180178
write_or_die(fd, d->buf.buf, d->buf.len);

builtin/tag.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,7 @@ static void create_tag(const struct object_id *object, const char *object_ref,
293293

294294
/* write the template message before editing: */
295295
path = git_pathdup("TAG_EDITMSG");
296-
fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
297-
if (fd < 0)
298-
die_errno(_("could not create file '%s'"), path);
296+
fd = xopen(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
299297

300298
if (opt->message_given) {
301299
write_or_die(fd, buf->buf, buf->len);

0 commit comments

Comments
 (0)