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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Aug 25, 2021 at 22:16 UTC
66e905b7dd0f4e9dd576be681f30fbaeeb19ec4a
15 files changed
+33
-52
builtin/add.c
+1
-3
@@ -313,9 +313,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
313
rev.diffopt.output_format = DIFF_FORMAT_PATCH;
314
rev.diffopt.use_color = 0;
315
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);
317
rev.diffopt.file = xfdopen(out, "w");
318
rev.diffopt.close_file = 1;
319
if (run_diff_files(&rev, 0))
builtin/archive.c
+1
-3
@@ -12,9 +12,7 @@
12
13
static void create_output_file(const char *output_file)
14
{
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);
16
if (output_fd != 1) {
17
if (dup2(output_fd, 1) < 0)
18
die_errno(_("could not redirect output"));
builtin/bugreport.c
+1
-4
@@ -171,10 +171,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
171
get_populated_hooks(&buffer, !startup_info->have_repository);
172
173
/* 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);
175
176
if (write_in_full(report, buffer.buf, buffer.len) < 0)
177
die_errno(_("unable to write to %s"), report_path.buf);
builtin/commit-tree.c
+1
-3
@@ -88,9 +88,7 @@ static int parse_file_arg_callback(const struct option *opt,
88
if (!strcmp(arg, "-"))
89
fd = 0;
90
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);
92
}
93
if (strbuf_read(buf, fd, 0) < 0)
94
die_errno(_("git commit-tree: failed to read '%s'"), arg);
builtin/hash-object.c
+1
-3
@@ -53,9 +53,7 @@ static void hash_object(const char *path, const char *type, const char *vpath,
53
unsigned flags, int literally)
54
{
55
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);
57
hash_fd(fd, type, vpath, flags, literally);
58
}
59
builtin/index-pack.c
+2
-6
@@ -338,15 +338,11 @@ static const char *open_pack_file(const char *pack_name)
338
"pack/tmp_pack_XXXXXX");
339
pack_name = strbuf_detach(&tmp_file, NULL);
340
} 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);
342
}
343
nothread_data.pack_fd = output_fd;
344
} 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);
346
output_fd = -1;
347
nothread_data.pack_fd = input_fd;
348
}
builtin/mailsplit.c
+1
-3
@@ -75,9 +75,7 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
75
fprintf(stderr, "corrupt mailbox\n");
76
exit(1);
77
}
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);
79
output = xfdopen(fd, "w");
80
81
/* Copy it out, while searching for a line that begins with
builtin/merge.c
+1
-3
@@ -1136,9 +1136,7 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge
1136
merge_names = &fetch_head_file;
1137
1138
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);
1140
1141
if (strbuf_read(merge_names, fd, 0) < 0)
1142
die_errno(_("could not read '%s'"), filename);
builtin/notes.c
+1
-3
@@ -172,9 +172,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
172
173
/* write the template message before editing: */
174
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);
176
177
if (d->given)
178
write_or_die(fd, d->buf.buf, d->buf.len);
builtin/tag.c
+1
-3
@@ -293,9 +293,7 @@ static void create_tag(const struct object_id *object, const char *object_ref,
293
294
/* write the template message before editing: */
295
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);
297
298
if (opt->message_given) {
299
write_or_die(fd, buf->buf, buf->len);
builtin/update-index.c
+1
-3
@@ -95,9 +95,7 @@ static int create_file(const char *path)
95
{
96
int fd;
97
path = get_mtime_path(path);
98
- fd = open(path, O_CREAT | O_RDWR, 0644);
99
- if (fd < 0)
100
- die_errno(_("failed to create file %s"), path);
98
+ fd = xopen(path, O_CREAT | O_RDWR, 0644);
99
return fd;
100
}
101
contrib/coccinelle/xopen.cocci
new
+16
@@ -0,0 +1,16 @@
1
+@@
2
+identifier fd;
3
+identifier die_fn =~ "^(die|die_errno)$";
4
+@@
5
+(
6
+ fd =
7
+- open
8
++ xopen
9
+ (...);
10
+|
11
+ int fd =
12
+- open
13
++ xopen
14
+ (...);
15
+)
16
+- if ( \( fd < 0 \| fd == -1 \) ) { die_fn(...); }
csum-file.c
+2
-6
@@ -131,12 +131,8 @@ struct hashfile *hashfd_check(const char *name)
131
int sink, check;
132
struct hashfile *f;
133
134
- sink = open("/dev/null", O_WRONLY);
135
- if (sink < 0)
136
- die_errno("unable to open /dev/null");
137
- check = open(name, O_RDONLY);
138
- if (check < 0)
139
- die_errno("unable to open '%s'", name);
134
+ sink = xopen("/dev/null", O_WRONLY);
135
+ check = xopen(name, O_RDONLY);
136
f = hashfd(sink, name);
137
f->check_fd = check;
138
f->check_buffer = xmalloc(f->buffer_len);
pack-write.c
+2
-6
@@ -75,9 +75,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
75
index_name = strbuf_detach(&tmp_file, NULL);
76
} else {
77
unlink(index_name);
78
- fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
79
- if (fd < 0)
80
- die_errno("unable to create '%s'", index_name);
78
+ fd = xopen(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
79
}
80
f = hashfd(fd, index_name);
81
}
@@ -256,9 +254,7 @@ const char *write_rev_file_order(const char *rev_name,
254
rev_name = strbuf_detach(&tmp_file, NULL);
255
} else {
256
unlink(rev_name);
259
- fd = open(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
260
- if (fd < 0)
261
- die_errno("unable to create '%s'", rev_name);
257
+ fd = xopen(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
258
}
259
f = hashfd(fd, rev_name);
260
} else if (flags & WRITE_REV_VERIFY) {
run-command.c
+1
-3
@@ -761,9 +761,7 @@ fail_pipe:
761
notify_pipe[0] = notify_pipe[1] = -1;
762
763
if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) {
764
- null_fd = open("/dev/null", O_RDWR | O_CLOEXEC);
765
- if (null_fd < 0)
766
- die_errno(_("open /dev/null failed"));
764
+ null_fd = xopen("/dev/null", O_RDWR | O_CLOEXEC);
765
set_cloexec(null_fd);
766
}
767