]> git.ipfire.org Git - thirdparty/git.git/blame - fsck.c
fsck: add a simple test for receive.fsck.<msg-id>
[thirdparty/git.git] / fsck.c
CommitLineData
355885d5
MK
1#include "cache.h"
2#include "object.h"
3#include "blob.h"
4#include "tree.h"
5#include "tree-walk.h"
6#include "commit.h"
7#include "tag.h"
8#include "fsck.h"
cec097be 9#include "refs.h"
a18fcc9f 10#include "utf8.h"
355885d5 11
c99ba492
JS
12#define FOREACH_MSG_ID(FUNC) \
13 /* errors */ \
14 FUNC(BAD_DATE, ERROR) \
15 FUNC(BAD_DATE_OVERFLOW, ERROR) \
16 FUNC(BAD_EMAIL, ERROR) \
17 FUNC(BAD_NAME, ERROR) \
18 FUNC(BAD_OBJECT_SHA1, ERROR) \
19 FUNC(BAD_PARENT_SHA1, ERROR) \
20 FUNC(BAD_TAG_OBJECT, ERROR) \
21 FUNC(BAD_TIMEZONE, ERROR) \
22 FUNC(BAD_TREE, ERROR) \
23 FUNC(BAD_TREE_SHA1, ERROR) \
24 FUNC(BAD_TYPE, ERROR) \
25 FUNC(DUPLICATE_ENTRIES, ERROR) \
26 FUNC(MISSING_AUTHOR, ERROR) \
27 FUNC(MISSING_COMMITTER, ERROR) \
28 FUNC(MISSING_EMAIL, ERROR) \
29 FUNC(MISSING_GRAFT, ERROR) \
30 FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
31 FUNC(MISSING_OBJECT, ERROR) \
32 FUNC(MISSING_PARENT, ERROR) \
33 FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
34 FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
35 FUNC(MISSING_TAG, ERROR) \
36 FUNC(MISSING_TAG_ENTRY, ERROR) \
37 FUNC(MISSING_TAG_OBJECT, ERROR) \
38 FUNC(MISSING_TREE, ERROR) \
39 FUNC(MISSING_TYPE, ERROR) \
40 FUNC(MISSING_TYPE_ENTRY, ERROR) \
c9ad147f 41 FUNC(MULTIPLE_AUTHORS, ERROR) \
c99ba492
JS
42 FUNC(NUL_IN_HEADER, ERROR) \
43 FUNC(TAG_OBJECT_NOT_TAG, ERROR) \
44 FUNC(TREE_NOT_SORTED, ERROR) \
45 FUNC(UNKNOWN_TYPE, ERROR) \
46 FUNC(UNTERMINATED_HEADER, ERROR) \
47 FUNC(ZERO_PADDED_DATE, ERROR) \
48 /* warnings */ \
49 FUNC(BAD_FILEMODE, WARN) \
50 FUNC(BAD_TAG_NAME, WARN) \
51 FUNC(EMPTY_NAME, WARN) \
52 FUNC(FULL_PATHNAME, WARN) \
53 FUNC(HAS_DOT, WARN) \
54 FUNC(HAS_DOTDOT, WARN) \
55 FUNC(HAS_DOTGIT, WARN) \
56 FUNC(MISSING_TAGGER_ENTRY, WARN) \
57 FUNC(NULL_SHA1, WARN) \
58 FUNC(ZERO_PADDED_FILEMODE, WARN)
59
60#define MSG_ID(id, msg_type) FSCK_MSG_##id,
61enum fsck_msg_id {
62 FOREACH_MSG_ID(MSG_ID)
63 FSCK_MSG_MAX
64};
65#undef MSG_ID
66
f417eed8
JS
67#define STR(x) #x
68#define MSG_ID(id, msg_type) { STR(id), NULL, FSCK_##msg_type },
c99ba492 69static struct {
f417eed8
JS
70 const char *id_string;
71 const char *downcased;
c99ba492
JS
72 int msg_type;
73} msg_id_info[FSCK_MSG_MAX + 1] = {
74 FOREACH_MSG_ID(MSG_ID)
f417eed8 75 { NULL, NULL, -1 }
c99ba492
JS
76};
77#undef MSG_ID
78
f417eed8
JS
79static int parse_msg_id(const char *text)
80{
81 int i;
82
83 if (!msg_id_info[0].downcased) {
84 /* convert id_string to lower case, without underscores. */
85 for (i = 0; i < FSCK_MSG_MAX; i++) {
86 const char *p = msg_id_info[i].id_string;
87 int len = strlen(p);
88 char *q = xmalloc(len);
89
90 msg_id_info[i].downcased = q;
91 while (*p)
92 if (*p == '_')
93 p++;
94 else
95 *(q)++ = tolower(*(p)++);
96 *q = '\0';
97 }
98 }
99
100 for (i = 0; i < FSCK_MSG_MAX; i++)
101 if (!strcmp(text, msg_id_info[i].downcased))
102 return i;
103
104 return -1;
105}
106
c99ba492
JS
107static int fsck_msg_type(enum fsck_msg_id msg_id,
108 struct fsck_options *options)
109{
110 int msg_type;
111
0282f4dc
JS
112 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
113
114 if (options->msg_type)
115 msg_type = options->msg_type[msg_id];
116 else {
117 msg_type = msg_id_info[msg_id].msg_type;
118 if (options->strict && msg_type == FSCK_WARN)
119 msg_type = FSCK_ERROR;
120 }
c99ba492
JS
121
122 return msg_type;
123}
124
0282f4dc
JS
125static int parse_msg_type(const char *str)
126{
127 if (!strcmp(str, "error"))
128 return FSCK_ERROR;
129 else if (!strcmp(str, "warn"))
130 return FSCK_WARN;
131 else
132 die("Unknown fsck message type: '%s'", str);
133}
134
5d477a33
JS
135int is_valid_msg_type(const char *msg_id, const char *msg_type)
136{
137 if (parse_msg_id(msg_id) < 0)
138 return 0;
139 parse_msg_type(msg_type);
140 return 1;
141}
142
0282f4dc
JS
143void fsck_set_msg_type(struct fsck_options *options,
144 const char *msg_id, const char *msg_type)
145{
146 int id = parse_msg_id(msg_id), type;
147
148 if (id < 0)
149 die("Unhandled message id: %s", msg_id);
150 type = parse_msg_type(msg_type);
151
152 if (!options->msg_type) {
153 int i;
154 int *msg_type = xmalloc(sizeof(int) * FSCK_MSG_MAX);
155 for (i = 0; i < FSCK_MSG_MAX; i++)
156 msg_type[i] = fsck_msg_type(i, options);
157 options->msg_type = msg_type;
158 }
159
160 options->msg_type[id] = type;
161}
162
163void fsck_set_msg_types(struct fsck_options *options, const char *values)
164{
165 char *buf = xstrdup(values), *to_free = buf;
166 int done = 0;
167
168 while (!done) {
169 int len = strcspn(buf, " ,|"), equal;
170
171 done = !buf[len];
172 if (!len) {
173 buf++;
174 continue;
175 }
176 buf[len] = '\0';
177
178 for (equal = 0;
179 equal < len && buf[equal] != '=' && buf[equal] != ':';
180 equal++)
181 buf[equal] = tolower(buf[equal]);
182 buf[equal] = '\0';
183
184 if (equal == len)
185 die("Missing '=': '%s'", buf);
186
187 fsck_set_msg_type(options, buf, buf + equal + 1);
188 buf += len + 1;
189 }
190 free(to_free);
191}
192
71ab8fa8
JS
193static void append_msg_id(struct strbuf *sb, const char *msg_id)
194{
195 for (;;) {
196 char c = *(msg_id)++;
197
198 if (!c)
199 break;
200 if (c != '_')
201 strbuf_addch(sb, tolower(c));
202 else {
203 assert(*msg_id);
204 strbuf_addch(sb, *(msg_id)++);
205 }
206 }
207
208 strbuf_addstr(sb, ": ");
209}
210
c99ba492
JS
211__attribute__((format (printf, 4, 5)))
212static int report(struct fsck_options *options, struct object *object,
213 enum fsck_msg_id id, const char *fmt, ...)
214{
215 va_list ap;
216 struct strbuf sb = STRBUF_INIT;
217 int msg_type = fsck_msg_type(id, options), result;
218
71ab8fa8
JS
219 append_msg_id(&sb, msg_id_info[id].id_string);
220
c99ba492
JS
221 va_start(ap, fmt);
222 strbuf_vaddf(&sb, fmt, ap);
223 result = options->error_func(object, msg_type, sb.buf);
224 strbuf_release(&sb);
225 va_end(ap);
226
227 return result;
228}
229
22410549 230static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
355885d5
MK
231{
232 struct tree_desc desc;
233 struct name_entry entry;
234 int res = 0;
235
236 if (parse_tree(tree))
237 return -1;
238
239 init_tree_desc(&desc, tree->buffer, tree->size);
240 while (tree_entry(&desc, &entry)) {
241 int result;
242
243 if (S_ISGITLINK(entry.mode))
244 continue;
245 if (S_ISDIR(entry.mode))
22410549 246 result = options->walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data, options);
355885d5 247 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
22410549 248 result = options->walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data, options);
355885d5 249 else {
82247e9b 250 result = error("in tree %s: entry %s has bad mode %.6o",
355885d5
MK
251 sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
252 }
253 if (result < 0)
254 return result;
255 if (!res)
256 res = result;
257 }
258 return res;
259}
260
22410549 261static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
355885d5
MK
262{
263 struct commit_list *parents;
264 int res;
265 int result;
266
267 if (parse_commit(commit))
268 return -1;
269
22410549 270 result = options->walk((struct object *)commit->tree, OBJ_TREE, data, options);
355885d5
MK
271 if (result < 0)
272 return result;
273 res = result;
274
275 parents = commit->parents;
276 while (parents) {
22410549 277 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
355885d5
MK
278 if (result < 0)
279 return result;
280 if (!res)
281 res = result;
282 parents = parents->next;
283 }
284 return res;
285}
286
22410549 287static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
355885d5
MK
288{
289 if (parse_tag(tag))
290 return -1;
22410549 291 return options->walk(tag->tagged, OBJ_ANY, data, options);
355885d5
MK
292}
293
22410549 294int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
355885d5
MK
295{
296 if (!obj)
297 return -1;
298 switch (obj->type) {
299 case OBJ_BLOB:
300 return 0;
301 case OBJ_TREE:
22410549 302 return fsck_walk_tree((struct tree *)obj, data, options);
355885d5 303 case OBJ_COMMIT:
22410549 304 return fsck_walk_commit((struct commit *)obj, data, options);
355885d5 305 case OBJ_TAG:
22410549 306 return fsck_walk_tag((struct tag *)obj, data, options);
355885d5
MK
307 default:
308 error("Unknown object type for %s", sha1_to_hex(obj->sha1));
309 return -1;
310 }
311}
ba002f3b
MK
312
313/*
314 * The entries in a tree are ordered in the _path_ order,
315 * which means that a directory entry is ordered by adding
316 * a slash to the end of it.
317 *
318 * So a directory called "a" is ordered _after_ a file
319 * called "a.c", because "a/" sorts after "a.c".
320 */
321#define TREE_UNORDERED (-1)
322#define TREE_HAS_DUPS (-2)
323
324static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
325{
326 int len1 = strlen(name1);
327 int len2 = strlen(name2);
328 int len = len1 < len2 ? len1 : len2;
329 unsigned char c1, c2;
330 int cmp;
331
332 cmp = memcmp(name1, name2, len);
333 if (cmp < 0)
334 return 0;
335 if (cmp > 0)
336 return TREE_UNORDERED;
337
338 /*
339 * Ok, the first <len> characters are the same.
340 * Now we need to order the next one, but turn
341 * a '\0' into a '/' for a directory entry.
342 */
343 c1 = name1[len];
344 c2 = name2[len];
345 if (!c1 && !c2)
346 /*
347 * git-write-tree used to write out a nonsense tree that has
348 * entries with the same name, one blob and one tree. Make
349 * sure we do not have duplicate entries.
350 */
351 return TREE_HAS_DUPS;
352 if (!c1 && S_ISDIR(mode1))
353 c1 = '/';
354 if (!c2 && S_ISDIR(mode2))
355 c2 = '/';
356 return c1 < c2 ? 0 : TREE_UNORDERED;
357}
358
22410549 359static int fsck_tree(struct tree *item, struct fsck_options *options)
ba002f3b
MK
360{
361 int retval;
c479d14a 362 int has_null_sha1 = 0;
ba002f3b
MK
363 int has_full_path = 0;
364 int has_empty_name = 0;
5d34a435
JK
365 int has_dot = 0;
366 int has_dotdot = 0;
5c17f512 367 int has_dotgit = 0;
ba002f3b
MK
368 int has_zero_pad = 0;
369 int has_bad_modes = 0;
370 int has_dup_entries = 0;
371 int not_properly_sorted = 0;
372 struct tree_desc desc;
373 unsigned o_mode;
374 const char *o_name;
ba002f3b
MK
375
376 init_tree_desc(&desc, item->buffer, item->size);
377
378 o_mode = 0;
379 o_name = NULL;
ba002f3b
MK
380
381 while (desc.size) {
382 unsigned mode;
383 const char *name;
c479d14a 384 const unsigned char *sha1;
ba002f3b 385
c479d14a 386 sha1 = tree_entry_extract(&desc, &name, &mode);
ba002f3b 387
effd12ec
HS
388 has_null_sha1 |= is_null_sha1(sha1);
389 has_full_path |= !!strchr(name, '/');
390 has_empty_name |= !*name;
391 has_dot |= !strcmp(name, ".");
392 has_dotdot |= !strcmp(name, "..");
5e519fb8
JH
393 has_dotgit |= (!strcmp(name, ".git") ||
394 is_hfs_dotgit(name) ||
395 is_ntfs_dotgit(name));
ba002f3b
MK
396 has_zero_pad |= *(char *)desc.buffer == '0';
397 update_tree_entry(&desc);
398
399 switch (mode) {
400 /*
401 * Standard modes..
402 */
403 case S_IFREG | 0755:
404 case S_IFREG | 0644:
405 case S_IFLNK:
406 case S_IFDIR:
407 case S_IFGITLINK:
408 break;
409 /*
410 * This is nonstandard, but we had a few of these
411 * early on when we honored the full set of mode
412 * bits..
413 */
414 case S_IFREG | 0664:
22410549 415 if (!options->strict)
ba002f3b
MK
416 break;
417 default:
418 has_bad_modes = 1;
419 }
420
421 if (o_name) {
422 switch (verify_ordered(o_mode, o_name, mode, name)) {
423 case TREE_UNORDERED:
424 not_properly_sorted = 1;
425 break;
426 case TREE_HAS_DUPS:
427 has_dup_entries = 1;
428 break;
429 default:
430 break;
431 }
432 }
433
434 o_mode = mode;
435 o_name = name;
ba002f3b
MK
436 }
437
438 retval = 0;
c479d14a 439 if (has_null_sha1)
c99ba492 440 retval += report(options, &item->object, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
ba002f3b 441 if (has_full_path)
c99ba492 442 retval += report(options, &item->object, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
ba002f3b 443 if (has_empty_name)
c99ba492 444 retval += report(options, &item->object, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
5d34a435 445 if (has_dot)
c99ba492 446 retval += report(options, &item->object, FSCK_MSG_HAS_DOT, "contains '.'");
5d34a435 447 if (has_dotdot)
c99ba492 448 retval += report(options, &item->object, FSCK_MSG_HAS_DOTDOT, "contains '..'");
5c17f512 449 if (has_dotgit)
c99ba492 450 retval += report(options, &item->object, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
ba002f3b 451 if (has_zero_pad)
c99ba492 452 retval += report(options, &item->object, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
ba002f3b 453 if (has_bad_modes)
c99ba492 454 retval += report(options, &item->object, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
ba002f3b 455 if (has_dup_entries)
c99ba492 456 retval += report(options, &item->object, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
ba002f3b 457 if (not_properly_sorted)
c99ba492 458 retval += report(options, &item->object, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
ba002f3b
MK
459 return retval;
460}
461
4d0d8975 462static int require_end_of_header(const void *data, unsigned long size,
22410549 463 struct object *obj, struct fsck_options *options)
4d0d8975
JS
464{
465 const char *buffer = (const char *)data;
466 unsigned long i;
467
468 for (i = 0; i < size; i++) {
469 switch (buffer[i]) {
470 case '\0':
c99ba492
JS
471 return report(options, obj,
472 FSCK_MSG_NUL_IN_HEADER,
473 "unterminated header: NUL at offset %ld", i);
4d0d8975
JS
474 case '\n':
475 if (i + 1 < size && buffer[i + 1] == '\n')
476 return 0;
477 }
478 }
479
c99ba492
JS
480 return report(options, obj,
481 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
4d0d8975
JS
482}
483
22410549 484static int fsck_ident(const char **ident, struct object *obj, struct fsck_options *options)
daae1922 485{
e6826e33 486 const char *p = *ident;
d4b8de04
JK
487 char *end;
488
e6826e33
JS
489 *ident = strchrnul(*ident, '\n');
490 if (**ident == '\n')
491 (*ident)++;
492
493 if (*p == '<')
c99ba492 494 return report(options, obj, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
495 p += strcspn(p, "<>\n");
496 if (*p == '>')
c99ba492 497 return report(options, obj, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
e6826e33 498 if (*p != '<')
c99ba492 499 return report(options, obj, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
e6826e33 500 if (p[-1] != ' ')
c99ba492 501 return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
502 p++;
503 p += strcspn(p, "<>\n");
504 if (*p != '>')
c99ba492 505 return report(options, obj, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
e6826e33
JS
506 p++;
507 if (*p != ' ')
c99ba492 508 return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
e6826e33
JS
509 p++;
510 if (*p == '0' && p[1] != ' ')
c99ba492 511 return report(options, obj, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
e6826e33 512 if (date_overflows(strtoul(p, &end, 10)))
c99ba492 513 return report(options, obj, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
e6826e33 514 if ((end == p || *end != ' '))
c99ba492 515 return report(options, obj, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
e6826e33
JS
516 p = end + 1;
517 if ((*p != '+' && *p != '-') ||
518 !isdigit(p[1]) ||
519 !isdigit(p[2]) ||
520 !isdigit(p[3]) ||
521 !isdigit(p[4]) ||
522 (p[5] != '\n'))
c99ba492 523 return report(options, obj, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
e6826e33 524 p += 6;
daae1922
JN
525 return 0;
526}
527
bc6b8fc1 528static int fsck_commit_buffer(struct commit *commit, const char *buffer,
22410549 529 unsigned long size, struct fsck_options *options)
ba002f3b 530{
ba002f3b
MK
531 unsigned char tree_sha1[20], sha1[20];
532 struct commit_graft *graft;
c9ad147f 533 unsigned parent_count, parent_line_count = 0, author_count;
daae1922 534 int err;
ba002f3b 535
22410549 536 if (require_end_of_header(buffer, size, &commit->object, options))
4d0d8975
JS
537 return -1;
538
cf4fff57 539 if (!skip_prefix(buffer, "tree ", &buffer))
c99ba492 540 return report(options, &commit->object, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
b3584761
JS
541 if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n') {
542 err = report(options, &commit->object, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
543 if (err)
544 return err;
545 }
2d820a61 546 buffer += 41;
cf4fff57 547 while (skip_prefix(buffer, "parent ", &buffer)) {
b3584761
JS
548 if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') {
549 err = report(options, &commit->object, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
550 if (err)
551 return err;
552 }
2d820a61 553 buffer += 41;
9d02150c 554 parent_line_count++;
ba002f3b
MK
555 }
556 graft = lookup_commit_graft(commit->object.sha1);
9d02150c 557 parent_count = commit_list_count(commit->parents);
ba002f3b 558 if (graft) {
9d02150c 559 if (graft->nr_parent == -1 && !parent_count)
ba002f3b 560 ; /* shallow commit */
b3584761
JS
561 else if (graft->nr_parent != parent_count) {
562 err = report(options, &commit->object, FSCK_MSG_MISSING_GRAFT, "graft objects missing");
563 if (err)
564 return err;
565 }
ba002f3b 566 } else {
b3584761
JS
567 if (parent_count != parent_line_count) {
568 err = report(options, &commit->object, FSCK_MSG_MISSING_PARENT, "parent objects missing");
569 if (err)
570 return err;
571 }
ba002f3b 572 }
c9ad147f
JS
573 author_count = 0;
574 while (skip_prefix(buffer, "author ", &buffer)) {
575 author_count++;
576 err = fsck_ident(&buffer, &commit->object, options);
577 if (err)
578 return err;
579 }
580 if (author_count < 1)
581 err = report(options, &commit->object, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
582 else if (author_count > 1)
583 err = report(options, &commit->object, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
daae1922
JN
584 if (err)
585 return err;
cf4fff57 586 if (!skip_prefix(buffer, "committer ", &buffer))
c99ba492 587 return report(options, &commit->object, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
22410549 588 err = fsck_ident(&buffer, &commit->object, options);
daae1922
JN
589 if (err)
590 return err;
ba002f3b 591 if (!commit->tree)
c99ba492 592 return report(options, &commit->object, FSCK_MSG_BAD_TREE, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
ba002f3b
MK
593
594 return 0;
595}
596
90a398bb 597static int fsck_commit(struct commit *commit, const char *data,
22410549 598 unsigned long size, struct fsck_options *options)
bc6b8fc1 599{
90a398bb 600 const char *buffer = data ? data : get_commit_buffer(commit, &size);
22410549 601 int ret = fsck_commit_buffer(commit, buffer, size, options);
90a398bb
JS
602 if (!data)
603 unuse_commit_buffer(commit, buffer);
bc6b8fc1
JK
604 return ret;
605}
606
cec097be 607static int fsck_tag_buffer(struct tag *tag, const char *data,
22410549 608 unsigned long size, struct fsck_options *options)
cec097be
JS
609{
610 unsigned char sha1[20];
611 int ret = 0;
612 const char *buffer;
613 char *to_free = NULL, *eol;
614 struct strbuf sb = STRBUF_INIT;
615
616 if (data)
617 buffer = data;
618 else {
619 enum object_type type;
620
621 buffer = to_free =
622 read_sha1_file(tag->object.sha1, &type, &size);
623 if (!buffer)
c99ba492
JS
624 return report(options, &tag->object,
625 FSCK_MSG_MISSING_TAG_OBJECT,
cec097be
JS
626 "cannot read tag object");
627
628 if (type != OBJ_TAG) {
c99ba492
JS
629 ret = report(options, &tag->object,
630 FSCK_MSG_TAG_OBJECT_NOT_TAG,
cec097be
JS
631 "expected tag got %s",
632 typename(type));
633 goto done;
634 }
635 }
636
22410549 637 if (require_end_of_header(buffer, size, &tag->object, options))
cec097be
JS
638 goto done;
639
640 if (!skip_prefix(buffer, "object ", &buffer)) {
c99ba492 641 ret = report(options, &tag->object, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
cec097be
JS
642 goto done;
643 }
644 if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') {
c99ba492 645 ret = report(options, &tag->object, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
7d7d5b05
JS
646 if (ret)
647 goto done;
cec097be
JS
648 }
649 buffer += 41;
650
651 if (!skip_prefix(buffer, "type ", &buffer)) {
c99ba492 652 ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
cec097be
JS
653 goto done;
654 }
655 eol = strchr(buffer, '\n');
656 if (!eol) {
c99ba492 657 ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
cec097be
JS
658 goto done;
659 }
660 if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
c99ba492 661 ret = report(options, &tag->object, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
cec097be
JS
662 if (ret)
663 goto done;
664 buffer = eol + 1;
665
666 if (!skip_prefix(buffer, "tag ", &buffer)) {
c99ba492 667 ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
cec097be
JS
668 goto done;
669 }
670 eol = strchr(buffer, '\n');
671 if (!eol) {
c99ba492 672 ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
cec097be
JS
673 goto done;
674 }
675 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
676 if (check_refname_format(sb.buf, 0))
c99ba492
JS
677 report(options, &tag->object, FSCK_MSG_BAD_TAG_NAME,
678 "invalid 'tag' name: %.*s",
7add4419 679 (int)(eol - buffer), buffer);
cec097be
JS
680 buffer = eol + 1;
681
682 if (!skip_prefix(buffer, "tagger ", &buffer))
683 /* early tags do not contain 'tagger' lines; warn only */
c99ba492 684 report(options, &tag->object, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
cec097be 685 else
22410549 686 ret = fsck_ident(&buffer, &tag->object, options);
cec097be
JS
687
688done:
689 strbuf_release(&sb);
690 free(to_free);
691 return ret;
692}
693
90a398bb 694static int fsck_tag(struct tag *tag, const char *data,
22410549 695 unsigned long size, struct fsck_options *options)
ba002f3b
MK
696{
697 struct object *tagged = tag->tagged;
698
699 if (!tagged)
c99ba492 700 return report(options, &tag->object, FSCK_MSG_BAD_TAG_OBJECT, "could not load tagged object");
cec097be 701
22410549 702 return fsck_tag_buffer(tag, data, size, options);
ba002f3b
MK
703}
704
90a398bb 705int fsck_object(struct object *obj, void *data, unsigned long size,
22410549 706 struct fsck_options *options)
ba002f3b
MK
707{
708 if (!obj)
c99ba492 709 return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
ba002f3b
MK
710
711 if (obj->type == OBJ_BLOB)
712 return 0;
713 if (obj->type == OBJ_TREE)
22410549 714 return fsck_tree((struct tree *) obj, options);
ba002f3b 715 if (obj->type == OBJ_COMMIT)
90a398bb 716 return fsck_commit((struct commit *) obj, (const char *) data,
22410549 717 size, options);
ba002f3b 718 if (obj->type == OBJ_TAG)
90a398bb 719 return fsck_tag((struct tag *) obj, (const char *) data,
22410549 720 size, options);
ba002f3b 721
c99ba492 722 return report(options, obj, FSCK_MSG_UNKNOWN_TYPE, "unknown type '%d' (internal fsck error)",
ba002f3b
MK
723 obj->type);
724}
d6ffc8d7 725
c99ba492 726int fsck_error_function(struct object *obj, int msg_type, const char *message)
d6ffc8d7 727{
0282f4dc
JS
728 if (msg_type == FSCK_WARN) {
729 warning("object %s: %s", sha1_to_hex(obj->sha1), message);
730 return 0;
731 }
c99ba492 732 error("object %s: %s", sha1_to_hex(obj->sha1), message);
d6ffc8d7
MK
733 return 1;
734}