Skip to content

Commit 0721c31

Browse files
trastgitster
authored andcommitted
Use die_errno() instead of die() when checking syscalls
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d824cbb commit 0721c31

30 files changed

+79
-74
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
}

builtin-add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -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)

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: 4 additions & 4 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);

builtin-clone.c

Lines changed: 5 additions & 5 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
}
@@ -257,11 +257,11 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
257257
if (!link(src->buf, dest->buf))
258258
continue;
259259
if (option_local)
260-
die("failed to create link %s", dest->buf);
260+
die_errno("failed to create link '%s'", dest->buf);
261261
option_no_hardlinks = 1;
262262
}
263263
if (copy_file(dest->buf, src->buf, 0666))
264-
die("failed to copy file to %s", dest->buf);
264+
die_errno("failed to copy file to '%s'", dest->buf);
265265
}
266266
closedir(dir);
267267
}

builtin-commit.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ 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)
@@ -964,8 +964,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
964964
/* Finally, get the commit message */
965965
strbuf_reset(&sb);
966966
if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
967+
int saved_errno = errno;
967968
rollback_index_files();
968-
die("could not read commit message");
969+
die("could not read commit message: %s", strerror(saved_errno));
969970
}
970971

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

builtin-fast-export.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static void handle_object(const unsigned char *sha1)
119119

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

125125
show_progress();

builtin-fmt-merge-msg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
368368
if (inpath && strcmp(inpath, "-")) {
369369
in = fopen(inpath, "r");
370370
if (!in)
371-
die("cannot open %s", inpath);
371+
die_errno("cannot open '%s'", inpath);
372372
}
373373

374374
if (strbuf_read(&input, fileno(in), 0) < 0)

builtin-fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static void check_unreachable_object(struct object *obj)
217217
return;
218218
}
219219
if (!(f = fopen(filename, "w")))
220-
die("Could not open %s", filename);
220+
die_errno("Could not open '%s'", filename);
221221
if (obj->type == OBJ_BLOB) {
222222
enum object_type type;
223223
unsigned long size;

0 commit comments

Comments
 (0)