]> git.ipfire.org Git - thirdparty/git.git/commitdiff
use xopen() to handle fatal open(2) failures
authorRené Scharfe <l.s.r@web.de>
Wed, 25 Aug 2021 20:16:46 +0000 (22:16 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 25 Aug 2021 21:39:08 +0000 (14:39 -0700)
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>
15 files changed:
builtin/add.c
builtin/archive.c
builtin/bugreport.c
builtin/commit-tree.c
builtin/hash-object.c
builtin/index-pack.c
builtin/mailsplit.c
builtin/merge.c
builtin/notes.c
builtin/tag.c
builtin/update-index.c
contrib/coccinelle/xopen.cocci [new file with mode: 0644]
csum-file.c
pack-write.c
run-command.c

index 09e684585d9ff25e708434000e09f807b1d30a45..c37c95b45b323d69326b9ebede6886dff685f9c2 100644 (file)
@@ -313,9 +313,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
        rev.diffopt.output_format = DIFF_FORMAT_PATCH;
        rev.diffopt.use_color = 0;
        rev.diffopt.flags.ignore_dirty_submodules = 1;
-       out = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
-       if (out < 0)
-               die(_("Could not open '%s' for writing."), file);
+       out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
        rev.diffopt.file = xfdopen(out, "w");
        rev.diffopt.close_file = 1;
        if (run_diff_files(&rev, 0))
index 45d11669aae459caf95bb6b4737558297debae89..7176b041b6d85b5760c91f94fcdde551a38d147f 100644 (file)
@@ -12,9 +12,7 @@
 
 static void create_output_file(const char *output_file)
 {
-       int output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
-       if (output_fd < 0)
-               die_errno(_("could not create archive file '%s'"), output_file);
+       int output_fd = xopen(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
        if (output_fd != 1) {
                if (dup2(output_fd, 1) < 0)
                        die_errno(_("could not redirect output"));
index 9915a5841def8c5c86a844efe9960cebbef31717..06ed10dc92d5e59e81721b62293c011d66b2e186 100644 (file)
@@ -171,10 +171,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
        get_populated_hooks(&buffer, !startup_info->have_repository);
 
        /* fopen doesn't offer us an O_EXCL alternative, except with glibc. */
-       report = open(report_path.buf, O_CREAT | O_EXCL | O_WRONLY, 0666);
-
-       if (report < 0)
-               die(_("couldn't create a new file at '%s'"), report_path.buf);
+       report = xopen(report_path.buf, O_CREAT | O_EXCL | O_WRONLY, 0666);
 
        if (write_in_full(report, buffer.buf, buffer.len) < 0)
                die_errno(_("unable to write to %s"), report_path.buf);
index 1031b9a491c5cec1411ff822402e1fd39b9fca4f..63ea3229333c8766d51c34be9aec3f4d1bab3cd9 100644 (file)
@@ -88,9 +88,7 @@ static int parse_file_arg_callback(const struct option *opt,
        if (!strcmp(arg, "-"))
                fd = 0;
        else {
-               fd = open(arg, O_RDONLY);
-               if (fd < 0)
-                       die_errno(_("git commit-tree: failed to open '%s'"), arg);
+               fd = xopen(arg, O_RDONLY);
        }
        if (strbuf_read(buf, fd, 0) < 0)
                die_errno(_("git commit-tree: failed to read '%s'"), arg);
index 640ef4ded595a3a21579a5630e04c3e6a412420d..2e6e2ddd0c3bc6d5ff9cbe281800f2277652f4c9 100644 (file)
@@ -53,9 +53,7 @@ static void hash_object(const char *path, const char *type, const char *vpath,
                        unsigned flags, int literally)
 {
        int fd;
-       fd = open(path, O_RDONLY);
-       if (fd < 0)
-               die_errno("Cannot open '%s'", path);
+       fd = xopen(path, O_RDONLY);
        hash_fd(fd, type, vpath, flags, literally);
 }
 
index 8336466865cbef122d552da29611196a1164bcbc..6cc48902170e32c578f50d27bba5af6ea8568ab8 100644 (file)
@@ -338,15 +338,11 @@ static const char *open_pack_file(const char *pack_name)
                                                "pack/tmp_pack_XXXXXX");
                        pack_name = strbuf_detach(&tmp_file, NULL);
                } else {
-                       output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
-                       if (output_fd < 0)
-                               die_errno(_("unable to create '%s'"), pack_name);
+                       output_fd = xopen(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
                }
                nothread_data.pack_fd = output_fd;
        } else {
-               input_fd = open(pack_name, O_RDONLY);
-               if (input_fd < 0)
-                       die_errno(_("cannot open packfile '%s'"), pack_name);
+               input_fd = xopen(pack_name, O_RDONLY);
                output_fd = -1;
                nothread_data.pack_fd = input_fd;
        }
index 664400b8169b673f735e77f7dd86474f1665a3ae..7baef30569fce8e6b4a7353067bb4aba00561557 100644 (file)
@@ -75,9 +75,7 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
                fprintf(stderr, "corrupt mailbox\n");
                exit(1);
        }
-       fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
-       if (fd < 0)
-               die_errno("cannot open output file '%s'", name);
+       fd = xopen(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
        output = xfdopen(fd, "w");
 
        /* Copy it out, while searching for a line that begins with
index 22f23990b37b6af8756daa5bcf7a1a7bbef2049c..47614d8070e38163d501663fd418eb5579749af9 100644 (file)
@@ -1136,9 +1136,7 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge
                merge_names = &fetch_head_file;
 
        filename = git_path_fetch_head(the_repository);
-       fd = open(filename, O_RDONLY);
-       if (fd < 0)
-               die_errno(_("could not open '%s' for reading"), filename);
+       fd = xopen(filename, O_RDONLY);
 
        if (strbuf_read(merge_names, fd, 0) < 0)
                die_errno(_("could not read '%s'"), filename);
index 74bba39ca8293208d3d577a47e2617a11d856c20..71c59583a17f8da6eb948248e19a262781091f28 100644 (file)
@@ -172,9 +172,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
 
                /* write the template message before editing: */
                d->edit_path = git_pathdup("NOTES_EDITMSG");
-               fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
-               if (fd < 0)
-                       die_errno(_("could not create file '%s'"), d->edit_path);
+               fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
 
                if (d->given)
                        write_or_die(fd, d->buf.buf, d->buf.len);
index 82fcfc0982423f3c7ee90fe43d30295023cc7873..87552ae6ac54ca48405d3b55ece2bfd5bfef513b 100644 (file)
@@ -293,9 +293,7 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 
                /* write the template message before editing: */
                path = git_pathdup("TAG_EDITMSG");
-               fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
-               if (fd < 0)
-                       die_errno(_("could not create file '%s'"), path);
+               fd = xopen(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
 
                if (opt->message_given) {
                        write_or_die(fd, buf->buf, buf->len);
index f1f16f2de526d907cc8c5b41da64a7eef3d22a7d..187203e8bb53cbeaf80d2f4b89c04134a2a5c95f 100644 (file)
@@ -95,9 +95,7 @@ static int create_file(const char *path)
 {
        int fd;
        path = get_mtime_path(path);
-       fd = open(path, O_CREAT | O_RDWR, 0644);
-       if (fd < 0)
-               die_errno(_("failed to create file %s"), path);
+       fd = xopen(path, O_CREAT | O_RDWR, 0644);
        return fd;
 }
 
diff --git a/contrib/coccinelle/xopen.cocci b/contrib/coccinelle/xopen.cocci
new file mode 100644 (file)
index 0000000..814d7b8
--- /dev/null
@@ -0,0 +1,16 @@
+@@
+identifier fd;
+identifier die_fn =~ "^(die|die_errno)$";
+@@
+(
+  fd =
+- open
++ xopen
+  (...);
+|
+  int fd =
+- open
++ xopen
+  (...);
+)
+- if ( \( fd < 0 \| fd == -1 \) ) { die_fn(...); }
index c951cf82774a8a1f4ea67b3a2eb49ea686d79da6..26e8a6df44e9415ffe02fc612ec045c6fa32b032 100644 (file)
@@ -131,12 +131,8 @@ struct hashfile *hashfd_check(const char *name)
        int sink, check;
        struct hashfile *f;
 
-       sink = open("/dev/null", O_WRONLY);
-       if (sink < 0)
-               die_errno("unable to open /dev/null");
-       check = open(name, O_RDONLY);
-       if (check < 0)
-               die_errno("unable to open '%s'", name);
+       sink = xopen("/dev/null", O_WRONLY);
+       check = xopen(name, O_RDONLY);
        f = hashfd(sink, name);
        f->check_fd = check;
        f->check_buffer = xmalloc(f->buffer_len);
index f1fc3ecafadef49d73e6efe7451c683d6e3ccd39..2767b786192fe5eb37a1f2a978d4cc2d41861b25 100644 (file)
@@ -75,9 +75,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
                        index_name = strbuf_detach(&tmp_file, NULL);
                } else {
                        unlink(index_name);
-                       fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
-                       if (fd < 0)
-                               die_errno("unable to create '%s'", index_name);
+                       fd = xopen(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
                }
                f = hashfd(fd, index_name);
        }
@@ -256,9 +254,7 @@ const char *write_rev_file_order(const char *rev_name,
                        rev_name = strbuf_detach(&tmp_file, NULL);
                } else {
                        unlink(rev_name);
-                       fd = open(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
-                       if (fd < 0)
-                               die_errno("unable to create '%s'", rev_name);
+                       fd = xopen(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
                }
                f = hashfd(fd, rev_name);
        } else if (flags & WRITE_REV_VERIFY) {
index f72e72cce73f1a1decbdd99b098f050f37bea41f..2961f7e55efdc1dc7b675fa8ff2da3e5b35d5442 100644 (file)
@@ -761,9 +761,7 @@ fail_pipe:
                notify_pipe[0] = notify_pipe[1] = -1;
 
        if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) {
-               null_fd = open("/dev/null", O_RDWR | O_CLOEXEC);
-               if (null_fd < 0)
-                       die_errno(_("open /dev/null failed"));
+               null_fd = xopen("/dev/null", O_RDWR | O_CLOEXEC);
                set_cloexec(null_fd);
        }