Skip to content

Commit

Permalink
Merge branch 'tr/die_errno'
Browse files Browse the repository at this point in the history
* tr/die_errno:
  Use die_errno() instead of die() when checking syscalls
  Convert existing die(..., strerror(errno)) to die_errno()
  die_errno(): double % in strerror() output just in case
  Introduce die_errno() that appends strerror(errno) to die()
  • Loading branch information
Junio C Hamano committed Jul 6, 2009
2 parents 864cd94 + 0721c31 commit dd787c1
Show file tree
Hide file tree
Showing 60 changed files with 225 additions and 207 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
5 changes: 2 additions & 3 deletions bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ static void read_bisect_paths(struct argv_array *array)
FILE *fp = fopen(filename, "r");

if (!fp)
die("Could not open file '%s': %s", filename, strerror(errno));
die_errno("Could not open file '%s'", filename);

while (strbuf_getline(&str, fp, '\n') != EOF) {
char *quoted;
Expand Down Expand Up @@ -740,8 +740,7 @@ static void mark_expected_rev(char *bisect_rev_hex)
int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);

if (fd < 0)
die("could not create file '%s': %s",
filename, strerror(errno));
die_errno("could not create file '%s'", filename);

bisect_rev_hex[len] = '\n';
write_or_die(fd, bisect_rev_hex, len + 1);
Expand Down
4 changes: 2 additions & 2 deletions branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void create_branch(const char *head,

lock = lock_any_ref_for_update(ref.buf, NULL, 0);
if (!lock)
die("Failed to lock ref for update: %s.", strerror(errno));
die_errno("Failed to lock ref for update");

if (reflog)
log_all_ref_updates = 1;
Expand All @@ -188,7 +188,7 @@ void create_branch(const char *head,
setup_tracking(name, real_ref, track);

if (write_ref_sha1(lock, sha1, msg) < 0)
die("Failed to write ref: %s.", strerror(errno));
die_errno("Failed to write ref");

strbuf_release(&ref);
free(real_ref);
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 @@ static 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
12 changes: 6 additions & 6 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ static void say_patch_name(FILE *output, const char *pre,
static void read_patch_file(struct strbuf *sb, int fd)
{
if (strbuf_read(sb, fd, 0) < 0)
die("git apply: read returned %s", strerror(errno));
die_errno("git apply: failed to read");

/*
* Make sure that we have some slop in the buffer
Expand Down 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 @@ -2864,7 +2864,7 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
strbuf_release(&nbuf);

if (close(fd) < 0)
die("closing file %s: %s", path, strerror(errno));
die_errno("closing file '%s'", path);
return 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 Expand Up @@ -3356,7 +3356,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)

fd = open(arg, O_RDONLY);
if (fd < 0)
die("can't open patch '%s': %s", arg, strerror(errno));
die_errno("can't open patch '%s'", arg);
read_stdin = 0;
set_default_whitespace_mode(whitespace_option);
errs |= apply_patch(fd, arg, options);
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
15 changes: 7 additions & 8 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 All @@ -2035,7 +2035,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
contents_from = "standard input";
mode = 0;
if (strbuf_read(&buf, 0, 0) < 0)
die("read error %s from stdin", strerror(errno));
die_errno("failed to read from stdin");
}
convert_to_git(path, buf.buf, buf.len, &buf, 0);
origin->file.ptr = buf.buf;
Expand Down Expand Up @@ -2261,8 +2261,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
argc = parse_options_end(&ctx);

if (revs_file && read_ancestry(revs_file))
die("reading graft file %s failed: %s",
revs_file, strerror(errno));
die_errno("reading graft file '%s' failed", revs_file);

if (cmd_is_annotate) {
output_option |= OUTPUT_ANNOTATE_COMPAT;
Expand Down Expand Up @@ -2350,7 +2349,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)

setup_work_tree();
if (!has_string_in_work_tree(path))
die("cannot stat path %s: %s", path, strerror(errno));
die_errno("cannot stat path '%s'", path);
}

setup_revisions(argc, argv, &revs, NULL);
Expand Down
21 changes: 10 additions & 11 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 All @@ -252,17 +252,16 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
}

if (unlink(dest->buf) && errno != ENOENT)
die("failed to unlink %s: %s",
dest->buf, strerror(errno));
die_errno("failed to unlink '%s'", dest->buf);
if (!option_no_hardlinks) {
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 Expand Up @@ -420,11 +419,11 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
if (!option_bare) {
junk_work_tree = work_tree;
if (safe_create_leading_directories_const(work_tree) < 0)
die("could not create leading directories of '%s': %s",
work_tree, strerror(errno));
die_errno("could not create leading directories of '%s'",
work_tree);
if (!dest_exists && mkdir(work_tree, 0755))
die("could not create work tree dir '%s': %s.",
work_tree, strerror(errno));
die_errno("could not create work tree dir '%s'.",
work_tree);
set_git_work_tree(work_tree);
}
junk_git_dir = git_dir;
Expand Down
2 changes: 1 addition & 1 deletion builtin-commit-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
}

if (strbuf_read(&buffer, 0, 0) < 0)
die("git commit-tree: read returned %s", strerror(errno));
die_errno("git commit-tree: failed to read");

if (!commit_tree(buffer.buf, tree_sha1, parents, commit_sha1, NULL)) {
printf("%s\n", sha1_to_hex(commit_sha1));
Expand Down
28 changes: 13 additions & 15 deletions builtin-commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,12 @@ 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)
die("could not read log file '%s': %s",
logfile, strerror(errno));
die_errno("could not read log file '%s'",
logfile);
hook_arg1 = "message";
} else if (use_message) {
buffer = strstr(use_message_buffer, "\n\n");
Expand All @@ -450,16 +450,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
hook_arg2 = use_message;
} else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
die("could not read MERGE_MSG: %s", strerror(errno));
die_errno("could not read MERGE_MSG");
hook_arg1 = "merge";
} else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
die("could not read SQUASH_MSG: %s", strerror(errno));
die_errno("could not read SQUASH_MSG");
hook_arg1 = "squash";
} else if (template_file && !stat(template_file, &statbuf)) {
if (strbuf_read_file(&sb, template_file, 0) < 0)
die("could not read %s: %s",
template_file, strerror(errno));
die_errno("could not read '%s'", template_file);
hook_arg1 = "template";
}

Expand All @@ -472,8 +471,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix)

fp = fopen(git_path(commit_editmsg), "w");
if (fp == NULL)
die("could not open %s: %s",
git_path(commit_editmsg), strerror(errno));
die_errno("could not open '%s'", git_path(commit_editmsg));

if (cleanup_mode != CLEANUP_NONE)
stripspace(&sb, 0);
Expand All @@ -497,7 +495,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
}

if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
die("could not write commit template: %s", strerror(errno));
die_errno("could not write commit template");

strbuf_release(&sb);

Expand Down Expand Up @@ -940,8 +938,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
fp = fopen(git_path("MERGE_HEAD"), "r");
if (fp == NULL)
die("could not open %s for reading: %s",
git_path("MERGE_HEAD"), strerror(errno));
die_errno("could not open '%s' for reading",
git_path("MERGE_HEAD"));
while (strbuf_getline(&m, fp, '\n') != EOF) {
unsigned char sha1[20];
if (get_sha1_hex(m.buf, sha1) < 0)
Expand All @@ -952,8 +950,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
strbuf_release(&m);
if (!stat(git_path("MERGE_MODE"), &statbuf)) {
if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
die("could not read MERGE_MODE: %s",
strerror(errno));
die_errno("could not read MERGE_MODE");
if (!strcmp(sb.buf, "no-ff"))
allow_fast_forward = 0;
}
Expand All @@ -967,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
4 changes: 2 additions & 2 deletions builtin-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ int cmd_config(int argc, const char **argv, const char *unused_prefix)
check_argc(argc, 0, 0);
if (git_config(show_all_config, NULL) < 0) {
if (config_exclusive_filename)
die("unable to read config file %s: %s",
config_exclusive_filename, strerror(errno));
die_errno("unable to read config file '%s'",
config_exclusive_filename);
else
die("error processing config file(s)");
}
Expand Down
2 changes: 1 addition & 1 deletion builtin-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static int builtin_diff_b_f(struct rev_info *revs,
usage(builtin_diff_usage);

if (lstat(path, &st))
die("'%s': %s", path, strerror(errno));
die_errno("failed to stat '%s'", path);
if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
die("'%s': not a regular file or symlink", path);

Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -451,7 +451,7 @@ static void import_marks(char *input_file)
char line[512];
FILE *f = fopen(input_file, "r");
if (!f)
die("cannot read %s: %s", input_file, strerror(errno));
die_errno("cannot read '%s'", input_file);

while (fgets(line, sizeof(line), f)) {
uint32_t mark;
Expand Down
Loading

0 comments on commit dd787c1

Please sign in to comment.