Skip to content

Commit 2de381f

Browse files
paskyPetr Baudis
authored andcommitted
[PATCH] Consolidate the error handling
Now there is error() for "library" errors and die() for fatal "application" errors. usage() is now used strictly only for usage errors. Signed-off-by: Petr Baudis <[email protected]>
1 parent bdd4da5 commit 2de381f

13 files changed

+77
-69
lines changed

cache.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ extern int get_sha1_hex(const char *hex, unsigned char *sha1);
104104
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
105105

106106
/* General helper functions */
107-
extern void usage(const char *err, ...);
107+
extern void usage(const char *err);
108+
extern void die(const char *err, ...);
109+
extern int error(const char *err, ...);
110+
108111
extern int cache_name_compare(const char *name1, int len1, const char *name2, int len2);
109112

110113
#endif /* CACHE_H */

cat-file.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,17 @@ int main(int argc, char **argv)
1313
unsigned long size;
1414

1515
if (argc != 3 || get_sha1_hex(argv[2], sha1))
16-
usage("cat-file: cat-file [-t | tagname] <sha1>");
16+
usage("cat-file [-t | tagname] <sha1>");
1717
buf = read_sha1_file(sha1, type, &size);
18-
if (!buf) {
19-
fprintf(stderr, "cat-file %s: bad file\n", argv[2]);
20-
exit(1);
21-
}
18+
if (!buf)
19+
die("cat-file %s: bad file", argv[2]);
2220
if (!strcmp("-t", argv[1])) {
2321
buf = type;
2422
size = strlen(type);
2523
type[size] = '\n';
2624
size++;
2725
} else if (strcmp(type, argv[1])) {
28-
fprintf(stderr, "cat-file %s: bad tag\n", argv[2]);
29-
exit(1); /* bad tag */
26+
die("cat-file %s: bad tag", argv[2]);
3027
}
3128

3229
while (size > 0) {
@@ -37,12 +34,9 @@ int main(int argc, char **argv)
3734
/* Ignore epipe */
3835
if (errno == EPIPE)
3936
break;
40-
fprintf(stderr, "cat-file: %s\n", strerror(errno));
41-
exit(1);
42-
}
43-
if (!ret) {
44-
fprintf(stderr, "cat-file: disk full?");
45-
exit(1);
37+
die("cat-file: %s", strerror(errno));
38+
} else if (!ret) {
39+
die("cat-file: disk full?");
4640
}
4741
size -= ret;
4842
buf += ret;

check-files.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ static void check_file(const char *path)
1818
/* Nonexistent is fine */
1919
if (fd < 0) {
2020
if (errno != ENOENT)
21-
usage("%s: %s", path, strerror(errno));
21+
die("%s: %s", path, strerror(errno));
2222
return;
2323
}
2424

2525
/* Exists but is not in the cache is not fine */
2626
pos = cache_name_pos(path, strlen(path));
2727
if (pos < 0)
28-
usage("preparing to update existing file '%s' not in cache", path);
28+
die("preparing to update existing file '%s' not in cache", path);
2929
ce = active_cache[pos];
3030

3131
if (fstat(fd, &st) < 0)
32-
usage("fstat(%s): %s", path, strerror(errno));
32+
die("fstat(%s): %s", path, strerror(errno));
3333

3434
changed = cache_match_stat(ce, &st);
3535
if (changed)
36-
usage("preparing to update file '%s' not uptodate in cache", path);
36+
die("preparing to update file '%s' not uptodate in cache", path);
3737
}
3838

3939
int main(int argc, char **argv)

checkout-cache.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,21 @@ static int write_entry(struct cache_entry *ce)
7474

7575
new = read_sha1_file(ce->sha1, type, &size);
7676
if (!new || strcmp(type, "blob")) {
77-
fprintf(stderr, "checkout-cache: unable to read sha1 file of %s (%s)\n",
77+
return error("checkout-cache: unable to read sha1 file of %s (%s)",
7878
ce->name, sha1_to_hex(ce->sha1));
79-
return -1;
8079
}
8180
fd = create_file(ce->name, ce->st_mode);
8281
if (fd < 0) {
83-
fprintf(stderr, "checkout-cache: unable to create %s (%s)\n",
84-
ce->name, strerror(errno));
8582
free(new);
86-
return -1;
83+
return error("checkout-cache: unable to create %s (%s)",
84+
ce->name, strerror(errno));
8785
}
8886
wrote = write(fd, new, size);
8987
close(fd);
9088
free(new);
91-
if (wrote == size)
92-
return 0;
93-
fprintf(stderr, "checkout-cache: unable to write %s\n", ce->name);
94-
return -1;
89+
if (wrote != size)
90+
return error("checkout-cache: unable to write %s", ce->name);
91+
return 0;
9592
}
9693

9794
static int checkout_entry(struct cache_entry *ce)
@@ -139,8 +136,7 @@ int main(int argc, char **argv)
139136
int i, force_filename = 0;
140137

141138
if (read_cache() < 0) {
142-
fprintf(stderr, "Invalid cache\n");
143-
exit(1);
139+
die("invalid cache");
144140
}
145141

146142
for (i = 1; i < argc; i++) {

commit-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ int main(int argc, char **argv)
135135
fprintf(stderr, "Committing initial tree %s\n", argv[1]);
136136
pw = getpwuid(getuid());
137137
if (!pw)
138-
usage("You don't exist. Go away!");
138+
die("You don't exist. Go away!");
139139
realgecos = pw->pw_gecos;
140140
len = strlen(pw->pw_name);
141141
memcpy(realemail, pw->pw_name, len);

diff-tree.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static void update_tree_entry(void **bufp, unsigned long *sizep)
1111
int len = strlen(buf) + 1 + 20;
1212

1313
if (size < len)
14-
usage("corrupt tree file");
14+
die("corrupt tree file");
1515
*bufp = buf + len;
1616
*sizep = size - len;
1717
}
@@ -23,7 +23,7 @@ static const unsigned char *extract(void *tree, unsigned long size, const char *
2323
const char *path = strchr(tree, ' ');
2424

2525
if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
26-
usage("corrupt tree file");
26+
die("corrupt tree file");
2727
*pathp = path+1;
2828
return sha1;
2929
}
@@ -64,7 +64,7 @@ static void show_file(const char *prefix, void *tree, unsigned long size, const
6464

6565
tree = read_sha1_file(sha1, type, &size);
6666
if (!tree || strcmp(type, "tree"))
67-
usage("corrupt tree sha %s", sha1_to_hex(sha1));
67+
die("corrupt tree sha %s", sha1_to_hex(sha1));
6868

6969
show_tree(prefix, tree, size, newbase);
7070

@@ -148,7 +148,7 @@ static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned lon
148148
update_tree_entry(&tree2, &size2);
149149
continue;
150150
}
151-
usage("diff-tree: internal error");
151+
die("diff-tree: internal error");
152152
}
153153
return 0;
154154
}
@@ -162,10 +162,10 @@ static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, co
162162

163163
tree1 = read_sha1_file(old, type, &size1);
164164
if (!tree1 || strcmp(type, "tree"))
165-
usage("unable to read source tree");
165+
die("unable to read source tree (%s)", sha1_to_hex(old));
166166
tree2 = read_sha1_file(new, type, &size2);
167167
if (!tree2 || strcmp(type, "tree"))
168-
usage("unable to read destination tree");
168+
die("unable to read destination tree (%s)", sha1_to_hex(new));
169169
retval = diff_tree(tree1, size1, tree2, size2, base);
170170
free(tree1);
171171
free(tree2);

fsck-cache.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ static int fsck_dir(int i, char *path)
205205
struct dirent *de;
206206

207207
if (!dir) {
208-
fprintf(stderr, "missing sha1 directory '%s'", path);
209-
return -1;
208+
return error("missing sha1 directory '%s'", path);
210209
}
211210

212211
while ((de = readdir(dir)) != NULL) {

ls-tree.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ static int list(unsigned char *sha1)
1313

1414
buffer = read_sha1_file(sha1, type, &size);
1515
if (!buffer)
16-
usage("unable to read sha1 file");
16+
die("unable to read sha1 file");
1717
if (strcmp(type, "tree"))
18-
usage("expected a 'tree' node");
18+
die("expected a 'tree' node");
1919
while (size) {
2020
int len = strlen(buffer)+1;
2121
unsigned char *sha1 = buffer + len;
@@ -24,7 +24,7 @@ static int list(unsigned char *sha1)
2424
unsigned char *type;
2525

2626
if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
27-
usage("corrupt 'tree' file");
27+
die("corrupt 'tree' file");
2828
buffer = sha1 + 20;
2929
size -= len + 20;
3030
/* XXX: We do some ugly mode heuristics here.
@@ -48,6 +48,6 @@ int main(int argc, char **argv)
4848
if (!sha1_file_directory)
4949
sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
5050
if (list(sha1) < 0)
51-
usage("list failed");
51+
die("list failed");
5252
return 0;
5353
}

read-cache.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,47 @@
33
*
44
* Copyright (C) Linus Torvalds, 2005
55
*/
6+
#include <stdarg.h>
67
#include "cache.h"
78

89
const char *sha1_file_directory = NULL;
910
struct cache_entry **active_cache = NULL;
1011
unsigned int active_nr = 0, active_alloc = 0;
1112

12-
void usage(const char *err, ...)
13+
void usage(const char *err)
1314
{
14-
va_list args;
15-
char string[200];
15+
fprintf(stderr, "usage: %s\n", err);
16+
exit(1);
17+
}
18+
19+
static void report(const char *prefix, const char *err, va_list params)
20+
{
21+
fputs(prefix, stderr);
22+
vfprintf(stderr, err, params);
23+
fputs("\n", stderr);
24+
}
1625

17-
va_start(args, err);
18-
vsnprintf(string, sizeof(string), err, args);
19-
va_end(args);
20-
fprintf(stderr, "%s\n", string);
26+
void die(const char *err, ...)
27+
{
28+
va_list params;
29+
30+
va_start(params, err);
31+
report("fatal: ", err, params);
32+
va_end(params);
2133
exit(1);
2234
}
2335

36+
int error(const char *err, ...)
37+
{
38+
va_list params;
39+
40+
va_start(params, err);
41+
report("error: ", err, params);
42+
va_end(params);
43+
return -1;
44+
}
45+
46+
2447
static unsigned hexval(char c)
2548
{
2649
if (c >= '0' && c <= '9')
@@ -218,7 +241,6 @@ int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
218241
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
219242
if (fd < 0) {
220243
void *map;
221-
static int error(const char * string);
222244

223245
if (errno != EEXIST)
224246
return -1;
@@ -240,12 +262,6 @@ int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
240262
return 0;
241263
}
242264

243-
static int error(const char * string)
244-
{
245-
fprintf(stderr, "error: %s\n", string);
246-
return -1;
247-
}
248-
249265
int cache_match_stat(struct cache_entry *ce, struct stat *st)
250266
{
251267
unsigned int changed = 0;

read-tree.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ int main(int argc, char **argv)
7878

7979
newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
8080
if (newfd < 0)
81-
usage("unable to create new cachefile");
81+
die("unable to create new cachefile");
8282
atexit(remove_lock_file);
8383
remove_lock = 1;
8484

@@ -88,19 +88,19 @@ int main(int argc, char **argv)
8888
/* "-m" stands for "merge" current directory cache */
8989
if (!strcmp(arg, "-m")) {
9090
if (active_cache)
91-
usage("read-tree: cannot merge old cache on top of new");
91+
die("read-tree: cannot merge old cache on top of new");
9292
if (read_cache() < 0)
93-
usage("read-tree: corrupt directory cache");
93+
die("read-tree: corrupt directory cache");
9494
continue;
9595
}
9696
if (get_sha1_hex(arg, sha1) < 0)
9797
usage("read-tree [-m] <sha1>");
9898
if (read_tree(sha1, "", 0) < 0)
99-
usage("failed to unpack tree object %s", arg);
99+
die("failed to unpack tree object %s", arg);
100100
}
101101
if (write_cache(newfd, active_cache, active_nr) ||
102102
rename(".git/index.lock", ".git/index"))
103-
usage("unable to write new index file");
103+
die("unable to write new index file");
104104
remove_lock = 0;
105105
return 0;
106106
}

rev-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static void read_cache_file(const char *path)
179179
char line[500];
180180

181181
if (!file)
182-
usage("bad revtree cache file (%s)", path);
182+
die("bad revtree cache file (%s)", path);
183183

184184
while (fgets(line, sizeof(line), file)) {
185185
unsigned long date;

update-cache.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,14 @@ int main(int argc, char **argv)
249249

250250
newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
251251
if (newfd < 0)
252-
usage("unable to create new cachefile");
252+
die("unable to create new cachefile");
253253

254254
atexit(remove_lock_file);
255255
remove_lock = 1;
256256

257257
entries = read_cache();
258258
if (entries < 0)
259-
usage("cache corrupted");
259+
die("cache corrupted");
260260

261261
for (i = 1 ; i < argc; i++) {
262262
char *path = argv[i];
@@ -278,18 +278,18 @@ int main(int argc, char **argv)
278278
refresh_cache();
279279
continue;
280280
}
281-
usage("unknown option %s", path);
281+
die("unknown option %s", path);
282282
}
283283
if (!verify_path(path)) {
284284
fprintf(stderr, "Ignoring path %s\n", argv[i]);
285285
continue;
286286
}
287287
if (add_file_to_cache(path))
288-
usage("Unable to add %s to database", path);
288+
die("Unable to add %s to database", path);
289289
}
290290
if (write_cache(newfd, active_cache, active_nr) ||
291291
rename(".git/index.lock", ".git/index"))
292-
usage("Unable to write new cachefile");
292+
die("Unable to write new cachefile");
293293

294294
remove_lock = 0;
295295
return 0;

write-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ int main(int argc, char **argv)
106106
unsigned char sha1[20];
107107

108108
if (entries <= 0)
109-
usage("no cache contents to write");
109+
die("write-tree: no cache contents to write");
110110
if (write_tree(active_cache, entries, "", 0, sha1) != entries)
111-
usage("write-tree: internal error");
111+
die("write-tree: internal error");
112112
printf("%s\n", sha1_to_hex(sha1));
113113
return 0;
114114
}

0 commit comments

Comments
 (0)