Skip to content

Commit

Permalink
Use die_errno() instead of die() when checking syscalls
Browse files Browse the repository at this point in the history
Lots of die() calls did not actually report the kind of error, which
can leave the user confused as to the real problem.  Use die_errno()
where we check a system/library call that sets errno on failure, or
one of the following that wrap such calls:

  Function              Passes on error from
  --------              --------------------
  odb_pack_keep         open
  read_ancestry         fopen
  read_in_full          xread
  strbuf_read           xread
  strbuf_read_file      open or strbuf_read_file
  strbuf_readlink       readlink
  write_in_full         xwrite

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Thomas Rast authored and Junio C Hamano committed Jun 27, 2009
1 parent d824cbb commit 0721c31
Show file tree
Hide file tree
Showing 30 changed files with 79 additions and 74 deletions.
12 changes: 6 additions & 6 deletions abspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ const char *make_absolute_path(const char *path)

if (*buf) {
if (!*cwd && !getcwd(cwd, sizeof(cwd)))
die ("Could not get current working directory");
die_errno ("Could not get current working directory");

if (chdir(buf))
die ("Could not switch to '%s'", buf);
die_errno ("Could not switch to '%s'", buf);
}
if (!getcwd(buf, PATH_MAX))
die ("Could not get current working directory");
die_errno ("Could not get current working directory");

if (last_elem) {
int len = strlen(buf);
Expand All @@ -63,7 +63,7 @@ const char *make_absolute_path(const char *path)
if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
len = readlink(buf, next_buf, PATH_MAX);
if (len < 0)
die ("Invalid symlink: %s", buf);
die_errno ("Invalid symlink '%s'", buf);
if (PATH_MAX <= len)
die("symbolic link too long: %s", buf);
next_buf[len] = '\0';
Expand All @@ -75,7 +75,7 @@ const char *make_absolute_path(const char *path)
}

if (*cwd && chdir(cwd))
die ("Could not change back to '%s'", cwd);
die_errno ("Could not change back to '%s'", cwd);

return buf;
}
Expand Down Expand Up @@ -109,7 +109,7 @@ const char *make_nonrelative_path(const char *path)
} else {
const char *cwd = get_pwd_cwd();
if (!cwd)
die("Cannot determine the current working directory");
die_errno("Cannot determine the current working directory");
if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
die("Too long path: %.*s", 60, path);
}
Expand Down
2 changes: 1 addition & 1 deletion builtin-add.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ int edit_patch(int argc, const char **argv, const char *prefix)
launch_editor(file, NULL, NULL);

if (stat(file, &st))
die("Could not stat '%s'", file);
die_errno("Could not stat '%s'", file);
if (!st.st_size)
die("Empty patch. Aborted.");

Expand Down
6 changes: 3 additions & 3 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -2823,8 +2823,8 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
} else {
if (!cached) {
if (lstat(path, &st) < 0)
die("unable to stat newly created file %s",
path);
die_errno("unable to stat newly created file '%s'",
path);
fill_stat_cache_info(ce, &st);
}
if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
Expand Down Expand Up @@ -2913,7 +2913,7 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
++nr;
}
}
die("unable to write file %s mode %o", path, mode);
die_errno("unable to write file '%s' mode %o", path, mode);
}

static void create_file(struct patch *patch)
Expand Down
4 changes: 2 additions & 2 deletions builtin-archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ 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("could not create archive file: %s ", output_file);
die_errno("could not create archive file '%s'", output_file);
if (output_fd != 1) {
if (dup2(output_fd, 1) < 0)
die("could not redirect output");
die_errno("could not redirect output");
else
close(output_fd);
}
Expand Down
8 changes: 4 additions & 4 deletions builtin-blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -2008,23 +2008,23 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con

if (contents_from) {
if (stat(contents_from, &st) < 0)
die("Cannot stat %s", contents_from);
die_errno("Cannot stat '%s'", contents_from);
read_from = contents_from;
}
else {
if (lstat(path, &st) < 0)
die("Cannot lstat %s", path);
die_errno("Cannot lstat '%s'", path);
read_from = path;
}
mode = canon_mode(st.st_mode);
switch (st.st_mode & S_IFMT) {
case S_IFREG:
if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
die("cannot open or read %s", read_from);
die_errno("cannot open or read '%s'", read_from);
break;
case S_IFLNK:
if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
die("cannot readlink %s", read_from);
die_errno("cannot readlink '%s'", read_from);
break;
default:
die("unsupported file type %s", read_from);
Expand Down
10 changes: 5 additions & 5 deletions builtin-clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)

dir = opendir(src->buf);
if (!dir)
die("failed to open %s", src->buf);
die_errno("failed to open '%s'", src->buf);

if (mkdir(dest->buf, 0777)) {
if (errno != EEXIST)
die("failed to create directory %s", dest->buf);
die_errno("failed to create directory '%s'", dest->buf);
else if (stat(dest->buf, &buf))
die("failed to stat %s", dest->buf);
die_errno("failed to stat '%s'", dest->buf);
else if (!S_ISDIR(buf.st_mode))
die("%s exists and is not a directory", dest->buf);
}
Expand Down Expand Up @@ -257,11 +257,11 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
if (!link(src->buf, dest->buf))
continue;
if (option_local)
die("failed to create link %s", dest->buf);
die_errno("failed to create link '%s'", dest->buf);
option_no_hardlinks = 1;
}
if (copy_file(dest->buf, src->buf, 0666))
die("failed to copy file to %s", dest->buf);
die_errno("failed to copy file to '%s'", dest->buf);
}
closedir(dir);
}
Expand Down
5 changes: 3 additions & 2 deletions builtin-commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
if (isatty(0))
fprintf(stderr, "(reading log message from standard input)\n");
if (strbuf_read(&sb, 0, 0) < 0)
die("could not read log from standard input");
die_errno("could not read log from standard input");
hook_arg1 = "message";
} else if (logfile) {
if (strbuf_read_file(&sb, logfile, 0) < 0)
Expand Down Expand Up @@ -964,8 +964,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
/* Finally, get the commit message */
strbuf_reset(&sb);
if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
int saved_errno = errno;
rollback_index_files();
die("could not read commit message");
die("could not read commit message: %s", strerror(saved_errno));
}

/* Truncate the message just before the diff, if any. */
Expand Down
2 changes: 1 addition & 1 deletion builtin-fast-export.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static void handle_object(const unsigned char *sha1)

printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
if (size && fwrite(buf, size, 1, stdout) != 1)
die ("Could not write blob %s", sha1_to_hex(sha1));
die_errno ("Could not write blob '%s'", sha1_to_hex(sha1));
printf("\n");

show_progress();
Expand Down
2 changes: 1 addition & 1 deletion builtin-fmt-merge-msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
if (inpath && strcmp(inpath, "-")) {
in = fopen(inpath, "r");
if (!in)
die("cannot open %s", inpath);
die_errno("cannot open '%s'", inpath);
}

if (strbuf_read(&input, fileno(in), 0) < 0)
Expand Down
2 changes: 1 addition & 1 deletion builtin-fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ static void check_unreachable_object(struct object *obj)
return;
}
if (!(f = fopen(filename, "w")))
die("Could not open %s", filename);
die_errno("Could not open '%s'", filename);
if (obj->type == OBJ_BLOB) {
enum object_type type;
unsigned long size;
Expand Down
21 changes: 11 additions & 10 deletions builtin-init-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ static void copy_templates_1(char *path, int baselen,
memcpy(template + template_baselen, de->d_name, namelen+1);
if (lstat(path, &st_git)) {
if (errno != ENOENT)
die("cannot stat %s", path);
die_errno("cannot stat '%s'", path);
}
else
exists = 1;

if (lstat(template, &st_template))
die("cannot stat template %s", template);
die_errno("cannot stat template '%s'", template);

if (S_ISDIR(st_template.st_mode)) {
DIR *subdir = opendir(template);
int baselen_sub = baselen + namelen;
int template_baselen_sub = template_baselen + namelen;
if (!subdir)
die("cannot opendir %s", template);
die_errno("cannot opendir '%s'", template);
path[baselen_sub++] =
template[template_baselen_sub++] = '/';
path[baselen_sub] =
Expand All @@ -91,16 +91,17 @@ static void copy_templates_1(char *path, int baselen,
int len;
len = readlink(template, lnk, sizeof(lnk));
if (len < 0)
die("cannot readlink %s", template);
die_errno("cannot readlink '%s'", template);
if (sizeof(lnk) <= len)
die("insanely long symlink %s", template);
lnk[len] = 0;
if (symlink(lnk, path))
die("cannot symlink %s %s", lnk, path);
die_errno("cannot symlink '%s' '%s'", lnk, path);
}
else if (S_ISREG(st_template.st_mode)) {
if (copy_file(path, template, st_template.st_mode))
die("cannot copy %s to %s", template, path);
die_errno("cannot copy '%s' to '%s'", template,
path);
}
else
error("ignoring template %s", template);
Expand Down Expand Up @@ -350,7 +351,7 @@ static int guess_repository_type(const char *git_dir)
if (!strcmp(".", git_dir))
return 1;
if (!getcwd(cwd, sizeof(cwd)))
die("cannot tell cwd");
die_errno("cannot tell cwd");
if (!strcmp(git_dir, cwd))
return 1;
/*
Expand Down Expand Up @@ -440,11 +441,11 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
if (!git_work_tree_cfg) {
git_work_tree_cfg = xcalloc(PATH_MAX, 1);
if (!getcwd(git_work_tree_cfg, PATH_MAX))
die ("Cannot access current working directory.");
die_errno ("Cannot access current working directory");
}
if (access(get_git_work_tree(), X_OK))
die ("Cannot access work tree '%s'",
get_git_work_tree());
die_errno ("Cannot access work tree '%s'",
get_git_work_tree());
}

set_git_dir(make_absolute_path(git_dir));
Expand Down
4 changes: 2 additions & 2 deletions builtin-log.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
if (use_stdout)
die("standard output, or directory, which one?");
if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
die("Could not create directory %s",
output_directory);
die_errno("Could not create directory '%s'",
output_directory);
}

if (rev.pending.nr == 1) {
Expand Down
6 changes: 3 additions & 3 deletions builtin-mailsplit.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)

fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd < 0)
die("cannot open output file %s", name);
die_errno("cannot open output file '%s'", name);
output = fdopen(fd, "w");

/* Copy it out, while searching for a line that begins with
Expand All @@ -91,15 +91,15 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
int is_partial = len && buf[len-1] != '\n';

if (fwrite(buf, 1, len, output) != len)
die("cannot write output");
die_errno("cannot write output");

len = read_line_with_nul(buf, sizeof(buf), mbox);
if (len == 0) {
if (feof(mbox)) {
status = 1;
break;
}
die("cannot read mbox");
die_errno("cannot read mbox");
}
if (!is_partial && !is_bare && is_from_line(buf, len))
break; /* done with one message */
Expand Down
21 changes: 12 additions & 9 deletions builtin-merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ static void squash_message(void)
printf("Squash commit -- not updating HEAD\n");
fd = open(git_path("SQUASH_MSG"), O_WRONLY | O_CREAT, 0666);
if (fd < 0)
die("Could not write to %s", git_path("SQUASH_MSG"));
die_errno("Could not write to '%s'", git_path("SQUASH_MSG"));

init_revisions(&rev, NULL);
rev.ignore_merges = 1;
Expand Down Expand Up @@ -764,7 +764,8 @@ static int suggest_conflicts(void)

fp = fopen(git_path("MERGE_MSG"), "a");
if (!fp)
die("Could not open %s for writing", git_path("MERGE_MSG"));
die_errno("Could not open '%s' for writing",
git_path("MERGE_MSG"));
fprintf(fp, "\nConflicts:\n");
for (pos = 0; pos < active_nr; pos++) {
struct cache_entry *ce = active_cache[pos];
Expand Down Expand Up @@ -1186,27 +1187,29 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
sha1_to_hex(j->item->object.sha1));
fd = open(git_path("MERGE_HEAD"), O_WRONLY | O_CREAT, 0666);
if (fd < 0)
die("Could open %s for writing",
git_path("MERGE_HEAD"));
die_errno("Could not open '%s' for writing",
git_path("MERGE_HEAD"));
if (write_in_full(fd, buf.buf, buf.len) != buf.len)
die("Could not write to %s", git_path("MERGE_HEAD"));
die_errno("Could not write to '%s'", git_path("MERGE_HEAD"));
close(fd);
strbuf_addch(&merge_msg, '\n');
fd = open(git_path("MERGE_MSG"), O_WRONLY | O_CREAT, 0666);
if (fd < 0)
die("Could open %s for writing", git_path("MERGE_MSG"));
die_errno("Could not open '%s' for writing",
git_path("MERGE_MSG"));
if (write_in_full(fd, merge_msg.buf, merge_msg.len) !=
merge_msg.len)
die("Could not write to %s", git_path("MERGE_MSG"));
die_errno("Could not write to '%s'", git_path("MERGE_MSG"));
close(fd);
fd = open(git_path("MERGE_MODE"), O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
die("Could open %s for writing", git_path("MERGE_MODE"));
die_errno("Could not open '%s' for writing",
git_path("MERGE_MODE"));
strbuf_reset(&buf);
if (!allow_fast_forward)
strbuf_addf(&buf, "no-ff");
if (write_in_full(fd, buf.buf, buf.len) != buf.len)
die("Could not write to %s", git_path("MERGE_MODE"));
die_errno("Could not write to '%s'", git_path("MERGE_MODE"));
close(fd);
}

Expand Down
2 changes: 1 addition & 1 deletion builtin-rev-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
continue;
}
if (!getcwd(cwd, PATH_MAX))
die("unable to get current working directory");
die_errno("unable to get current working directory");
printf("%s/.git\n", cwd);
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion builtin-revert.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static void add_to_msg(const char *string)
{
int len = strlen(string);
if (write_in_full(msg_fd, string, len) < 0)
die ("Could not write to MERGE_MSG");
die_errno ("Could not write to MERGE_MSG");
}

static void add_message_to_msg(const char *message)
Expand Down
2 changes: 1 addition & 1 deletion builtin-stripspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
strip_comments = 1;

if (strbuf_read(&buf, 0, 1024) < 0)
die("could not read the input");
die_errno("could not read the input");

stripspace(&buf, strip_comments);

Expand Down
2 changes: 1 addition & 1 deletion builtin-tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
else {
if (!strcmp(msgfile, "-")) {
if (strbuf_read(&buf, 0, 1024) < 0)
die("cannot read %s", msgfile);
die_errno("cannot read '%s'", msgfile);
} else {
if (strbuf_read_file(&buf, msgfile, 1024) < 0)
die_errno("could not open or read '%s'",
Expand Down
2 changes: 1 addition & 1 deletion builtin-tar-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)

n = write_in_full(1, content + 11, 41);
if (n < 41)
die("git get-tar-commit-id: write error");
die_errno("git get-tar-commit-id: write error");

return 0;
}
Loading

0 comments on commit 0721c31

Please sign in to comment.