Merge branch 'rs/xopen-reports-open-failures'

Error diagnostics improvement.

* rs/xopen-reports-open-failures:
  use xopen() to handle fatal open(2) failures
  xopen: explicitly report creation failures
This commit is contained in:
Junio C Hamano 2021-09-08 13:30:32 -07:00
Родитель c8f491668e 66e905b7dd
Коммит 7b06222619
16 изменённых файлов: 36 добавлений и 53 удалений

Просмотреть файл

@ -319,9 +319,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
rev.diffopt.output_format = DIFF_FORMAT_PATCH; rev.diffopt.output_format = DIFF_FORMAT_PATCH;
rev.diffopt.use_color = 0; rev.diffopt.use_color = 0;
rev.diffopt.flags.ignore_dirty_submodules = 1; rev.diffopt.flags.ignore_dirty_submodules = 1;
out = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0666); out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (out < 0)
die(_("Could not open '%s' for writing."), file);
rev.diffopt.file = xfdopen(out, "w"); rev.diffopt.file = xfdopen(out, "w");
rev.diffopt.close_file = 1; rev.diffopt.close_file = 1;
if (run_diff_files(&rev, 0)) if (run_diff_files(&rev, 0))

Просмотреть файл

@ -12,9 +12,7 @@
static void create_output_file(const char *output_file) static void create_output_file(const char *output_file)
{ {
int output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666); int output_fd = xopen(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (output_fd < 0)
die_errno(_("could not create archive file '%s'"), output_file);
if (output_fd != 1) { if (output_fd != 1) {
if (dup2(output_fd, 1) < 0) if (dup2(output_fd, 1) < 0)
die_errno(_("could not redirect output")); die_errno(_("could not redirect output"));

Просмотреть файл

@ -171,10 +171,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
get_populated_hooks(&buffer, !startup_info->have_repository); get_populated_hooks(&buffer, !startup_info->have_repository);
/* fopen doesn't offer us an O_EXCL alternative, except with glibc. */ /* fopen doesn't offer us an O_EXCL alternative, except with glibc. */
report = open(report_path.buf, O_CREAT | O_EXCL | O_WRONLY, 0666); report = xopen(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);
if (write_in_full(report, buffer.buf, buffer.len) < 0) if (write_in_full(report, buffer.buf, buffer.len) < 0)
die_errno(_("unable to write to %s"), report_path.buf); die_errno(_("unable to write to %s"), report_path.buf);

Просмотреть файл

@ -88,9 +88,7 @@ static int parse_file_arg_callback(const struct option *opt,
if (!strcmp(arg, "-")) if (!strcmp(arg, "-"))
fd = 0; fd = 0;
else { else {
fd = open(arg, O_RDONLY); fd = xopen(arg, O_RDONLY);
if (fd < 0)
die_errno(_("git commit-tree: failed to open '%s'"), arg);
} }
if (strbuf_read(buf, fd, 0) < 0) if (strbuf_read(buf, fd, 0) < 0)
die_errno(_("git commit-tree: failed to read '%s'"), arg); die_errno(_("git commit-tree: failed to read '%s'"), arg);

Просмотреть файл

@ -53,9 +53,7 @@ static void hash_object(const char *path, const char *type, const char *vpath,
unsigned flags, int literally) unsigned flags, int literally)
{ {
int fd; int fd;
fd = open(path, O_RDONLY); fd = xopen(path, O_RDONLY);
if (fd < 0)
die_errno("Cannot open '%s'", path);
hash_fd(fd, type, vpath, flags, literally); hash_fd(fd, type, vpath, flags, literally);
} }

Просмотреть файл

@ -338,15 +338,11 @@ static const char *open_pack_file(const char *pack_name)
"pack/tmp_pack_XXXXXX"); "pack/tmp_pack_XXXXXX");
pack_name = strbuf_detach(&tmp_file, NULL); pack_name = strbuf_detach(&tmp_file, NULL);
} else { } else {
output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600); output_fd = xopen(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
if (output_fd < 0)
die_errno(_("unable to create '%s'"), pack_name);
} }
nothread_data.pack_fd = output_fd; nothread_data.pack_fd = output_fd;
} else { } else {
input_fd = open(pack_name, O_RDONLY); input_fd = xopen(pack_name, O_RDONLY);
if (input_fd < 0)
die_errno(_("cannot open packfile '%s'"), pack_name);
output_fd = -1; output_fd = -1;
nothread_data.pack_fd = input_fd; nothread_data.pack_fd = input_fd;
} }

Просмотреть файл

@ -75,9 +75,7 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
fprintf(stderr, "corrupt mailbox\n"); fprintf(stderr, "corrupt mailbox\n");
exit(1); exit(1);
} }
fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666); fd = xopen(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd < 0)
die_errno("cannot open output file '%s'", name);
output = xfdopen(fd, "w"); output = xfdopen(fd, "w");
/* Copy it out, while searching for a line that begins with /* Copy it out, while searching for a line that begins with

Просмотреть файл

@ -1138,9 +1138,7 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge
merge_names = &fetch_head_file; merge_names = &fetch_head_file;
filename = git_path_fetch_head(the_repository); filename = git_path_fetch_head(the_repository);
fd = open(filename, O_RDONLY); fd = xopen(filename, O_RDONLY);
if (fd < 0)
die_errno(_("could not open '%s' for reading"), filename);
if (strbuf_read(merge_names, fd, 0) < 0) if (strbuf_read(merge_names, fd, 0) < 0)
die_errno(_("could not read '%s'"), filename); die_errno(_("could not read '%s'"), filename);

Просмотреть файл

@ -172,9 +172,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
/* write the template message before editing: */ /* write the template message before editing: */
d->edit_path = git_pathdup("NOTES_EDITMSG"); d->edit_path = git_pathdup("NOTES_EDITMSG");
fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600); fd = xopen(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
if (fd < 0)
die_errno(_("could not create file '%s'"), d->edit_path);
if (d->given) if (d->given)
write_or_die(fd, d->buf.buf, d->buf.len); write_or_die(fd, d->buf.buf, d->buf.len);

Просмотреть файл

@ -293,9 +293,7 @@ static void create_tag(const struct object_id *object, const char *object_ref,
/* write the template message before editing: */ /* write the template message before editing: */
path = git_pathdup("TAG_EDITMSG"); path = git_pathdup("TAG_EDITMSG");
fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); fd = xopen(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
if (fd < 0)
die_errno(_("could not create file '%s'"), path);
if (opt->message_given) { if (opt->message_given) {
write_or_die(fd, buf->buf, buf->len); write_or_die(fd, buf->buf, buf->len);

Просмотреть файл

@ -95,9 +95,7 @@ static int create_file(const char *path)
{ {
int fd; int fd;
path = get_mtime_path(path); path = get_mtime_path(path);
fd = open(path, O_CREAT | O_RDWR, 0644); fd = xopen(path, O_CREAT | O_RDWR, 0644);
if (fd < 0)
die_errno(_("failed to create file %s"), path);
return fd; return fd;
} }

Просмотреть файл

@ -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(...); }

Просмотреть файл

@ -131,12 +131,8 @@ struct hashfile *hashfd_check(const char *name)
int sink, check; int sink, check;
struct hashfile *f; struct hashfile *f;
sink = open("/dev/null", O_WRONLY); sink = xopen("/dev/null", O_WRONLY);
if (sink < 0) check = xopen(name, O_RDONLY);
die_errno("unable to open /dev/null");
check = open(name, O_RDONLY);
if (check < 0)
die_errno("unable to open '%s'", name);
f = hashfd(sink, name); f = hashfd(sink, name);
f->check_fd = check; f->check_fd = check;
f->check_buffer = xmalloc(f->buffer_len); f->check_buffer = xmalloc(f->buffer_len);

Просмотреть файл

@ -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); index_name = strbuf_detach(&tmp_file, NULL);
} else { } else {
unlink(index_name); unlink(index_name);
fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600); fd = xopen(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
if (fd < 0)
die_errno("unable to create '%s'", index_name);
} }
f = hashfd(fd, index_name); 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); rev_name = strbuf_detach(&tmp_file, NULL);
} else { } else {
unlink(rev_name); unlink(rev_name);
fd = open(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600); fd = xopen(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
if (fd < 0)
die_errno("unable to create '%s'", rev_name);
} }
f = hashfd(fd, rev_name); f = hashfd(fd, rev_name);
} else if (flags & WRITE_REV_VERIFY) { } else if (flags & WRITE_REV_VERIFY) {

Просмотреть файл

@ -761,9 +761,7 @@ fail_pipe:
notify_pipe[0] = notify_pipe[1] = -1; notify_pipe[0] = notify_pipe[1] = -1;
if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) { if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) {
null_fd = open("/dev/null", O_RDWR | O_CLOEXEC); null_fd = xopen("/dev/null", O_RDWR | O_CLOEXEC);
if (null_fd < 0)
die_errno(_("open /dev/null failed"));
set_cloexec(null_fd); set_cloexec(null_fd);
} }

Просмотреть файл

@ -193,7 +193,9 @@ int xopen(const char *path, int oflag, ...)
if (errno == EINTR) if (errno == EINTR)
continue; continue;
if ((oflag & O_RDWR) == O_RDWR) if ((oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
die_errno(_("unable to create '%s'"), path);
else if ((oflag & O_RDWR) == O_RDWR)
die_errno(_("could not open '%s' for reading and writing"), path); die_errno(_("could not open '%s' for reading and writing"), path);
else if ((oflag & O_WRONLY) == O_WRONLY) else if ((oflag & O_WRONLY) == O_WRONLY)
die_errno(_("could not open '%s' for writing"), path); die_errno(_("could not open '%s' for writing"), path);