Skip to content

Commit dd787c1

Browse files
committed
Merge branch 'tr/die_errno'
* 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()
2 parents 864cd94 + 0721c31 commit dd787c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+225
-207
lines changed

abspath.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ const char *make_absolute_path(const char *path)
4141

4242
if (*buf) {
4343
if (!*cwd && !getcwd(cwd, sizeof(cwd)))
44-
die ("Could not get current working directory");
44+
die_errno ("Could not get current working directory");
4545

4646
if (chdir(buf))
47-
die ("Could not switch to '%s'", buf);
47+
die_errno ("Could not switch to '%s'", buf);
4848
}
4949
if (!getcwd(buf, PATH_MAX))
50-
die ("Could not get current working directory");
50+
die_errno ("Could not get current working directory");
5151

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

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

8080
return buf;
8181
}
@@ -109,7 +109,7 @@ const char *make_nonrelative_path(const char *path)
109109
} else {
110110
const char *cwd = get_pwd_cwd();
111111
if (!cwd)
112-
die("Cannot determine the current working directory");
112+
die_errno("Cannot determine the current working directory");
113113
if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
114114
die("Too long path: %.*s", 60, path);
115115
}

bisect.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static void read_bisect_paths(struct argv_array *array)
461461
FILE *fp = fopen(filename, "r");
462462

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

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

742742
if (fd < 0)
743-
die("could not create file '%s': %s",
744-
filename, strerror(errno));
743+
die_errno("could not create file '%s'", filename);
745744

746745
bisect_rev_hex[len] = '\n';
747746
write_or_die(fd, bisect_rev_hex, len + 1);

branch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void create_branch(const char *head,
172172

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

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

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

193193
strbuf_release(&ref);
194194
free(real_ref);

builtin-add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
220220
launch_editor(file, NULL, NULL);
221221

222222
if (stat(file, &st))
223-
die("Could not stat '%s'", file);
223+
die_errno("Could not stat '%s'", file);
224224
if (!st.st_size)
225225
die("Empty patch. Aborted.");
226226

builtin-apply.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ static void say_patch_name(FILE *output, const char *pre,
280280
static void read_patch_file(struct strbuf *sb, int fd)
281281
{
282282
if (strbuf_read(sb, fd, 0) < 0)
283-
die("git apply: read returned %s", strerror(errno));
283+
die_errno("git apply: failed to read");
284284

285285
/*
286286
* Make sure that we have some slop in the buffer
@@ -2823,8 +2823,8 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
28232823
} else {
28242824
if (!cached) {
28252825
if (lstat(path, &st) < 0)
2826-
die("unable to stat newly created file %s",
2827-
path);
2826+
die_errno("unable to stat newly created file '%s'",
2827+
path);
28282828
fill_stat_cache_info(ce, &st);
28292829
}
28302830
if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
@@ -2864,7 +2864,7 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
28642864
strbuf_release(&nbuf);
28652865

28662866
if (close(fd) < 0)
2867-
die("closing file %s: %s", path, strerror(errno));
2867+
die_errno("closing file '%s'", path);
28682868
return 0;
28692869
}
28702870

@@ -2913,7 +2913,7 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
29132913
++nr;
29142914
}
29152915
}
2916-
die("unable to write file %s mode %o", path, mode);
2916+
die_errno("unable to write file '%s' mode %o", path, mode);
29172917
}
29182918

29192919
static void create_file(struct patch *patch)
@@ -3356,7 +3356,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
33563356

33573357
fd = open(arg, O_RDONLY);
33583358
if (fd < 0)
3359-
die("can't open patch '%s': %s", arg, strerror(errno));
3359+
die_errno("can't open patch '%s'", arg);
33603360
read_stdin = 0;
33613361
set_default_whitespace_mode(whitespace_option);
33623362
errs |= apply_patch(fd, arg, options);

builtin-archive.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ static void create_output_file(const char *output_file)
1313
{
1414
int output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
1515
if (output_fd < 0)
16-
die("could not create archive file: %s ", output_file);
16+
die_errno("could not create archive file '%s'", output_file);
1717
if (output_fd != 1) {
1818
if (dup2(output_fd, 1) < 0)
19-
die("could not redirect output");
19+
die_errno("could not redirect output");
2020
else
2121
close(output_fd);
2222
}

builtin-blame.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,23 +2008,23 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
20082008

20092009
if (contents_from) {
20102010
if (stat(contents_from, &st) < 0)
2011-
die("Cannot stat %s", contents_from);
2011+
die_errno("Cannot stat '%s'", contents_from);
20122012
read_from = contents_from;
20132013
}
20142014
else {
20152015
if (lstat(path, &st) < 0)
2016-
die("Cannot lstat %s", path);
2016+
die_errno("Cannot lstat '%s'", path);
20172017
read_from = path;
20182018
}
20192019
mode = canon_mode(st.st_mode);
20202020
switch (st.st_mode & S_IFMT) {
20212021
case S_IFREG:
20222022
if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
2023-
die("cannot open or read %s", read_from);
2023+
die_errno("cannot open or read '%s'", read_from);
20242024
break;
20252025
case S_IFLNK:
20262026
if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
2027-
die("cannot readlink %s", read_from);
2027+
die_errno("cannot readlink '%s'", read_from);
20282028
break;
20292029
default:
20302030
die("unsupported file type %s", read_from);
@@ -2035,7 +2035,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
20352035
contents_from = "standard input";
20362036
mode = 0;
20372037
if (strbuf_read(&buf, 0, 0) < 0)
2038-
die("read error %s from stdin", strerror(errno));
2038+
die_errno("failed to read from stdin");
20392039
}
20402040
convert_to_git(path, buf.buf, buf.len, &buf, 0);
20412041
origin->file.ptr = buf.buf;
@@ -2261,8 +2261,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
22612261
argc = parse_options_end(&ctx);
22622262

22632263
if (revs_file && read_ancestry(revs_file))
2264-
die("reading graft file %s failed: %s",
2265-
revs_file, strerror(errno));
2264+
die_errno("reading graft file '%s' failed", revs_file);
22662265

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

23512350
setup_work_tree();
23522351
if (!has_string_in_work_tree(path))
2353-
die("cannot stat path %s: %s", path, strerror(errno));
2352+
die_errno("cannot stat path '%s'", path);
23542353
}
23552354

23562355
setup_revisions(argc, argv, &revs, NULL);

builtin-clone.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
220220

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

225225
if (mkdir(dest->buf, 0777)) {
226226
if (errno != EEXIST)
227-
die("failed to create directory %s", dest->buf);
227+
die_errno("failed to create directory '%s'", dest->buf);
228228
else if (stat(dest->buf, &buf))
229-
die("failed to stat %s", dest->buf);
229+
die_errno("failed to stat '%s'", dest->buf);
230230
else if (!S_ISDIR(buf.st_mode))
231231
die("%s exists and is not a directory", dest->buf);
232232
}
@@ -252,17 +252,16 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
252252
}
253253

254254
if (unlink(dest->buf) && errno != ENOENT)
255-
die("failed to unlink %s: %s",
256-
dest->buf, strerror(errno));
255+
die_errno("failed to unlink '%s'", dest->buf);
257256
if (!option_no_hardlinks) {
258257
if (!link(src->buf, dest->buf))
259258
continue;
260259
if (option_local)
261-
die("failed to create link %s", dest->buf);
260+
die_errno("failed to create link '%s'", dest->buf);
262261
option_no_hardlinks = 1;
263262
}
264263
if (copy_file(dest->buf, src->buf, 0666))
265-
die("failed to copy file to %s", dest->buf);
264+
die_errno("failed to copy file to '%s'", dest->buf);
266265
}
267266
closedir(dir);
268267
}
@@ -420,11 +419,11 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
420419
if (!option_bare) {
421420
junk_work_tree = work_tree;
422421
if (safe_create_leading_directories_const(work_tree) < 0)
423-
die("could not create leading directories of '%s': %s",
424-
work_tree, strerror(errno));
422+
die_errno("could not create leading directories of '%s'",
423+
work_tree);
425424
if (!dest_exists && mkdir(work_tree, 0755))
426-
die("could not create work tree dir '%s': %s.",
427-
work_tree, strerror(errno));
425+
die_errno("could not create work tree dir '%s'.",
426+
work_tree);
428427
set_git_work_tree(work_tree);
429428
}
430429
junk_git_dir = git_dir;

builtin-commit-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
124124
}
125125

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

129129
if (!commit_tree(buffer.buf, tree_sha1, parents, commit_sha1, NULL)) {
130130
printf("%s\n", sha1_to_hex(commit_sha1));

builtin-commit.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,12 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
434434
if (isatty(0))
435435
fprintf(stderr, "(reading log message from standard input)\n");
436436
if (strbuf_read(&sb, 0, 0) < 0)
437-
die("could not read log from standard input");
437+
die_errno("could not read log from standard input");
438438
hook_arg1 = "message";
439439
} else if (logfile) {
440440
if (strbuf_read_file(&sb, logfile, 0) < 0)
441-
die("could not read log file '%s': %s",
442-
logfile, strerror(errno));
441+
die_errno("could not read log file '%s'",
442+
logfile);
443443
hook_arg1 = "message";
444444
} else if (use_message) {
445445
buffer = strstr(use_message_buffer, "\n\n");
@@ -450,16 +450,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
450450
hook_arg2 = use_message;
451451
} else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
452452
if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
453-
die("could not read MERGE_MSG: %s", strerror(errno));
453+
die_errno("could not read MERGE_MSG");
454454
hook_arg1 = "merge";
455455
} else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
456456
if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
457-
die("could not read SQUASH_MSG: %s", strerror(errno));
457+
die_errno("could not read SQUASH_MSG");
458458
hook_arg1 = "squash";
459459
} else if (template_file && !stat(template_file, &statbuf)) {
460460
if (strbuf_read_file(&sb, template_file, 0) < 0)
461-
die("could not read %s: %s",
462-
template_file, strerror(errno));
461+
die_errno("could not read '%s'", template_file);
463462
hook_arg1 = "template";
464463
}
465464

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

473472
fp = fopen(git_path(commit_editmsg), "w");
474473
if (fp == NULL)
475-
die("could not open %s: %s",
476-
git_path(commit_editmsg), strerror(errno));
474+
die_errno("could not open '%s'", git_path(commit_editmsg));
477475

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

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

502500
strbuf_release(&sb);
503501

@@ -940,8 +938,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
940938
pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
941939
fp = fopen(git_path("MERGE_HEAD"), "r");
942940
if (fp == NULL)
943-
die("could not open %s for reading: %s",
944-
git_path("MERGE_HEAD"), strerror(errno));
941+
die_errno("could not open '%s' for reading",
942+
git_path("MERGE_HEAD"));
945943
while (strbuf_getline(&m, fp, '\n') != EOF) {
946944
unsigned char sha1[20];
947945
if (get_sha1_hex(m.buf, sha1) < 0)
@@ -952,8 +950,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
952950
strbuf_release(&m);
953951
if (!stat(git_path("MERGE_MODE"), &statbuf)) {
954952
if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
955-
die("could not read MERGE_MODE: %s",
956-
strerror(errno));
953+
die_errno("could not read MERGE_MODE");
957954
if (!strcmp(sb.buf, "no-ff"))
958955
allow_fast_forward = 0;
959956
}
@@ -967,8 +964,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
967964
/* Finally, get the commit message */
968965
strbuf_reset(&sb);
969966
if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
967+
int saved_errno = errno;
970968
rollback_index_files();
971-
die("could not read commit message");
969+
die("could not read commit message: %s", strerror(saved_errno));
972970
}
973971

974972
/* Truncate the message just before the diff, if any. */

0 commit comments

Comments
 (0)