]> git.ipfire.org Git - thirdparty/git.git/blame - fsck.c
fsck: introduce fsck options
[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
22410549 12static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
355885d5
MK
13{
14 struct tree_desc desc;
15 struct name_entry entry;
16 int res = 0;
17
18 if (parse_tree(tree))
19 return -1;
20
21 init_tree_desc(&desc, tree->buffer, tree->size);
22 while (tree_entry(&desc, &entry)) {
23 int result;
24
25 if (S_ISGITLINK(entry.mode))
26 continue;
27 if (S_ISDIR(entry.mode))
22410549 28 result = options->walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data, options);
355885d5 29 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
22410549 30 result = options->walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data, options);
355885d5 31 else {
82247e9b 32 result = error("in tree %s: entry %s has bad mode %.6o",
355885d5
MK
33 sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
34 }
35 if (result < 0)
36 return result;
37 if (!res)
38 res = result;
39 }
40 return res;
41}
42
22410549 43static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
355885d5
MK
44{
45 struct commit_list *parents;
46 int res;
47 int result;
48
49 if (parse_commit(commit))
50 return -1;
51
22410549 52 result = options->walk((struct object *)commit->tree, OBJ_TREE, data, options);
355885d5
MK
53 if (result < 0)
54 return result;
55 res = result;
56
57 parents = commit->parents;
58 while (parents) {
22410549 59 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
355885d5
MK
60 if (result < 0)
61 return result;
62 if (!res)
63 res = result;
64 parents = parents->next;
65 }
66 return res;
67}
68
22410549 69static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
355885d5
MK
70{
71 if (parse_tag(tag))
72 return -1;
22410549 73 return options->walk(tag->tagged, OBJ_ANY, data, options);
355885d5
MK
74}
75
22410549 76int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
355885d5
MK
77{
78 if (!obj)
79 return -1;
80 switch (obj->type) {
81 case OBJ_BLOB:
82 return 0;
83 case OBJ_TREE:
22410549 84 return fsck_walk_tree((struct tree *)obj, data, options);
355885d5 85 case OBJ_COMMIT:
22410549 86 return fsck_walk_commit((struct commit *)obj, data, options);
355885d5 87 case OBJ_TAG:
22410549 88 return fsck_walk_tag((struct tag *)obj, data, options);
355885d5
MK
89 default:
90 error("Unknown object type for %s", sha1_to_hex(obj->sha1));
91 return -1;
92 }
93}
ba002f3b
MK
94
95/*
96 * The entries in a tree are ordered in the _path_ order,
97 * which means that a directory entry is ordered by adding
98 * a slash to the end of it.
99 *
100 * So a directory called "a" is ordered _after_ a file
101 * called "a.c", because "a/" sorts after "a.c".
102 */
103#define TREE_UNORDERED (-1)
104#define TREE_HAS_DUPS (-2)
105
106static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
107{
108 int len1 = strlen(name1);
109 int len2 = strlen(name2);
110 int len = len1 < len2 ? len1 : len2;
111 unsigned char c1, c2;
112 int cmp;
113
114 cmp = memcmp(name1, name2, len);
115 if (cmp < 0)
116 return 0;
117 if (cmp > 0)
118 return TREE_UNORDERED;
119
120 /*
121 * Ok, the first <len> characters are the same.
122 * Now we need to order the next one, but turn
123 * a '\0' into a '/' for a directory entry.
124 */
125 c1 = name1[len];
126 c2 = name2[len];
127 if (!c1 && !c2)
128 /*
129 * git-write-tree used to write out a nonsense tree that has
130 * entries with the same name, one blob and one tree. Make
131 * sure we do not have duplicate entries.
132 */
133 return TREE_HAS_DUPS;
134 if (!c1 && S_ISDIR(mode1))
135 c1 = '/';
136 if (!c2 && S_ISDIR(mode2))
137 c2 = '/';
138 return c1 < c2 ? 0 : TREE_UNORDERED;
139}
140
22410549 141static int fsck_tree(struct tree *item, struct fsck_options *options)
ba002f3b
MK
142{
143 int retval;
c479d14a 144 int has_null_sha1 = 0;
ba002f3b
MK
145 int has_full_path = 0;
146 int has_empty_name = 0;
5d34a435
JK
147 int has_dot = 0;
148 int has_dotdot = 0;
5c17f512 149 int has_dotgit = 0;
ba002f3b
MK
150 int has_zero_pad = 0;
151 int has_bad_modes = 0;
152 int has_dup_entries = 0;
153 int not_properly_sorted = 0;
154 struct tree_desc desc;
155 unsigned o_mode;
156 const char *o_name;
ba002f3b
MK
157
158 init_tree_desc(&desc, item->buffer, item->size);
159
160 o_mode = 0;
161 o_name = NULL;
ba002f3b
MK
162
163 while (desc.size) {
164 unsigned mode;
165 const char *name;
c479d14a 166 const unsigned char *sha1;
ba002f3b 167
c479d14a 168 sha1 = tree_entry_extract(&desc, &name, &mode);
ba002f3b 169
effd12ec
HS
170 has_null_sha1 |= is_null_sha1(sha1);
171 has_full_path |= !!strchr(name, '/');
172 has_empty_name |= !*name;
173 has_dot |= !strcmp(name, ".");
174 has_dotdot |= !strcmp(name, "..");
5e519fb8
JH
175 has_dotgit |= (!strcmp(name, ".git") ||
176 is_hfs_dotgit(name) ||
177 is_ntfs_dotgit(name));
ba002f3b
MK
178 has_zero_pad |= *(char *)desc.buffer == '0';
179 update_tree_entry(&desc);
180
181 switch (mode) {
182 /*
183 * Standard modes..
184 */
185 case S_IFREG | 0755:
186 case S_IFREG | 0644:
187 case S_IFLNK:
188 case S_IFDIR:
189 case S_IFGITLINK:
190 break;
191 /*
192 * This is nonstandard, but we had a few of these
193 * early on when we honored the full set of mode
194 * bits..
195 */
196 case S_IFREG | 0664:
22410549 197 if (!options->strict)
ba002f3b
MK
198 break;
199 default:
200 has_bad_modes = 1;
201 }
202
203 if (o_name) {
204 switch (verify_ordered(o_mode, o_name, mode, name)) {
205 case TREE_UNORDERED:
206 not_properly_sorted = 1;
207 break;
208 case TREE_HAS_DUPS:
209 has_dup_entries = 1;
210 break;
211 default:
212 break;
213 }
214 }
215
216 o_mode = mode;
217 o_name = name;
ba002f3b
MK
218 }
219
220 retval = 0;
c479d14a 221 if (has_null_sha1)
22410549 222 retval += options->error_func(&item->object, FSCK_WARN, "contains entries pointing to null sha1");
ba002f3b 223 if (has_full_path)
22410549 224 retval += options->error_func(&item->object, FSCK_WARN, "contains full pathnames");
ba002f3b 225 if (has_empty_name)
22410549 226 retval += options->error_func(&item->object, FSCK_WARN, "contains empty pathname");
5d34a435 227 if (has_dot)
22410549 228 retval += options->error_func(&item->object, FSCK_WARN, "contains '.'");
5d34a435 229 if (has_dotdot)
22410549 230 retval += options->error_func(&item->object, FSCK_WARN, "contains '..'");
5c17f512 231 if (has_dotgit)
22410549 232 retval += options->error_func(&item->object, FSCK_WARN, "contains '.git'");
ba002f3b 233 if (has_zero_pad)
22410549 234 retval += options->error_func(&item->object, FSCK_WARN, "contains zero-padded file modes");
ba002f3b 235 if (has_bad_modes)
22410549 236 retval += options->error_func(&item->object, FSCK_WARN, "contains bad file modes");
ba002f3b 237 if (has_dup_entries)
22410549 238 retval += options->error_func(&item->object, FSCK_ERROR, "contains duplicate file entries");
ba002f3b 239 if (not_properly_sorted)
22410549 240 retval += options->error_func(&item->object, FSCK_ERROR, "not properly sorted");
ba002f3b
MK
241 return retval;
242}
243
4d0d8975 244static int require_end_of_header(const void *data, unsigned long size,
22410549 245 struct object *obj, struct fsck_options *options)
4d0d8975
JS
246{
247 const char *buffer = (const char *)data;
248 unsigned long i;
249
250 for (i = 0; i < size; i++) {
251 switch (buffer[i]) {
252 case '\0':
22410549 253 return options->error_func(obj, FSCK_ERROR,
4d0d8975
JS
254 "unterminated header: NUL at offset %d", i);
255 case '\n':
256 if (i + 1 < size && buffer[i + 1] == '\n')
257 return 0;
258 }
259 }
260
22410549 261 return options->error_func(obj, FSCK_ERROR, "unterminated header");
4d0d8975
JS
262}
263
22410549 264static int fsck_ident(const char **ident, struct object *obj, struct fsck_options *options)
daae1922 265{
d4b8de04
JK
266 char *end;
267
53f53cff 268 if (**ident == '<')
22410549 269 return options->error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email");
53f53cff
DI
270 *ident += strcspn(*ident, "<>\n");
271 if (**ident == '>')
22410549 272 return options->error_func(obj, FSCK_ERROR, "invalid author/committer line - bad name");
daae1922 273 if (**ident != '<')
22410549 274 return options->error_func(obj, FSCK_ERROR, "invalid author/committer line - missing email");
53f53cff 275 if ((*ident)[-1] != ' ')
22410549 276 return options->error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email");
daae1922
JN
277 (*ident)++;
278 *ident += strcspn(*ident, "<>\n");
279 if (**ident != '>')
22410549 280 return options->error_func(obj, FSCK_ERROR, "invalid author/committer line - bad email");
daae1922
JN
281 (*ident)++;
282 if (**ident != ' ')
22410549 283 return options->error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before date");
daae1922
JN
284 (*ident)++;
285 if (**ident == '0' && (*ident)[1] != ' ')
22410549 286 return options->error_func(obj, FSCK_ERROR, "invalid author/committer line - zero-padded date");
7ca36d93 287 if (date_overflows(strtoul(*ident, &end, 10)))
22410549 288 return options->error_func(obj, FSCK_ERROR, "invalid author/committer line - date causes integer overflow");
d4b8de04 289 if (end == *ident || *end != ' ')
22410549 290 return options->error_func(obj, FSCK_ERROR, "invalid author/committer line - bad date");
d4b8de04 291 *ident = end + 1;
daae1922
JN
292 if ((**ident != '+' && **ident != '-') ||
293 !isdigit((*ident)[1]) ||
294 !isdigit((*ident)[2]) ||
295 !isdigit((*ident)[3]) ||
296 !isdigit((*ident)[4]) ||
297 ((*ident)[5] != '\n'))
22410549 298 return options->error_func(obj, FSCK_ERROR, "invalid author/committer line - bad time zone");
daae1922
JN
299 (*ident) += 6;
300 return 0;
301}
302
bc6b8fc1 303static int fsck_commit_buffer(struct commit *commit, const char *buffer,
22410549 304 unsigned long size, struct fsck_options *options)
ba002f3b 305{
ba002f3b
MK
306 unsigned char tree_sha1[20], sha1[20];
307 struct commit_graft *graft;
9d02150c 308 unsigned parent_count, parent_line_count = 0;
daae1922 309 int err;
ba002f3b 310
22410549 311 if (require_end_of_header(buffer, size, &commit->object, options))
4d0d8975
JS
312 return -1;
313
cf4fff57 314 if (!skip_prefix(buffer, "tree ", &buffer))
22410549 315 return options->error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line");
2d820a61 316 if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n')
22410549 317 return options->error_func(&commit->object, FSCK_ERROR, "invalid 'tree' line format - bad sha1");
2d820a61 318 buffer += 41;
cf4fff57 319 while (skip_prefix(buffer, "parent ", &buffer)) {
2d820a61 320 if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n')
22410549 321 return options->error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1");
2d820a61 322 buffer += 41;
9d02150c 323 parent_line_count++;
ba002f3b
MK
324 }
325 graft = lookup_commit_graft(commit->object.sha1);
9d02150c 326 parent_count = commit_list_count(commit->parents);
ba002f3b 327 if (graft) {
9d02150c 328 if (graft->nr_parent == -1 && !parent_count)
ba002f3b 329 ; /* shallow commit */
9d02150c 330 else if (graft->nr_parent != parent_count)
22410549 331 return options->error_func(&commit->object, FSCK_ERROR, "graft objects missing");
ba002f3b 332 } else {
9d02150c 333 if (parent_count != parent_line_count)
22410549 334 return options->error_func(&commit->object, FSCK_ERROR, "parent objects missing");
ba002f3b 335 }
cf4fff57 336 if (!skip_prefix(buffer, "author ", &buffer))
22410549
JS
337 return options->error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'author' line");
338 err = fsck_ident(&buffer, &commit->object, options);
daae1922
JN
339 if (err)
340 return err;
cf4fff57 341 if (!skip_prefix(buffer, "committer ", &buffer))
22410549
JS
342 return options->error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'committer' line");
343 err = fsck_ident(&buffer, &commit->object, options);
daae1922
JN
344 if (err)
345 return err;
ba002f3b 346 if (!commit->tree)
22410549 347 return options->error_func(&commit->object, FSCK_ERROR, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
ba002f3b
MK
348
349 return 0;
350}
351
90a398bb 352static int fsck_commit(struct commit *commit, const char *data,
22410549 353 unsigned long size, struct fsck_options *options)
bc6b8fc1 354{
90a398bb 355 const char *buffer = data ? data : get_commit_buffer(commit, &size);
22410549 356 int ret = fsck_commit_buffer(commit, buffer, size, options);
90a398bb
JS
357 if (!data)
358 unuse_commit_buffer(commit, buffer);
bc6b8fc1
JK
359 return ret;
360}
361
cec097be 362static int fsck_tag_buffer(struct tag *tag, const char *data,
22410549 363 unsigned long size, struct fsck_options *options)
cec097be
JS
364{
365 unsigned char sha1[20];
366 int ret = 0;
367 const char *buffer;
368 char *to_free = NULL, *eol;
369 struct strbuf sb = STRBUF_INIT;
370
371 if (data)
372 buffer = data;
373 else {
374 enum object_type type;
375
376 buffer = to_free =
377 read_sha1_file(tag->object.sha1, &type, &size);
378 if (!buffer)
22410549 379 return options->error_func(&tag->object, FSCK_ERROR,
cec097be
JS
380 "cannot read tag object");
381
382 if (type != OBJ_TAG) {
22410549 383 ret = options->error_func(&tag->object, FSCK_ERROR,
cec097be
JS
384 "expected tag got %s",
385 typename(type));
386 goto done;
387 }
388 }
389
22410549 390 if (require_end_of_header(buffer, size, &tag->object, options))
cec097be
JS
391 goto done;
392
393 if (!skip_prefix(buffer, "object ", &buffer)) {
22410549 394 ret = options->error_func(&tag->object, FSCK_ERROR, "invalid format - expected 'object' line");
cec097be
JS
395 goto done;
396 }
397 if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') {
22410549 398 ret = options->error_func(&tag->object, FSCK_ERROR, "invalid 'object' line format - bad sha1");
cec097be
JS
399 goto done;
400 }
401 buffer += 41;
402
403 if (!skip_prefix(buffer, "type ", &buffer)) {
22410549 404 ret = options->error_func(&tag->object, FSCK_ERROR, "invalid format - expected 'type' line");
cec097be
JS
405 goto done;
406 }
407 eol = strchr(buffer, '\n');
408 if (!eol) {
22410549 409 ret = options->error_func(&tag->object, FSCK_ERROR, "invalid format - unexpected end after 'type' line");
cec097be
JS
410 goto done;
411 }
412 if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
22410549 413 ret = options->error_func(&tag->object, FSCK_ERROR, "invalid 'type' value");
cec097be
JS
414 if (ret)
415 goto done;
416 buffer = eol + 1;
417
418 if (!skip_prefix(buffer, "tag ", &buffer)) {
22410549 419 ret = options->error_func(&tag->object, FSCK_ERROR, "invalid format - expected 'tag' line");
cec097be
JS
420 goto done;
421 }
422 eol = strchr(buffer, '\n');
423 if (!eol) {
22410549 424 ret = options->error_func(&tag->object, FSCK_ERROR, "invalid format - unexpected end after 'type' line");
cec097be
JS
425 goto done;
426 }
427 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
428 if (check_refname_format(sb.buf, 0))
22410549 429 options->error_func(&tag->object, FSCK_WARN, "invalid 'tag' name: %.*s",
7add4419 430 (int)(eol - buffer), buffer);
cec097be
JS
431 buffer = eol + 1;
432
433 if (!skip_prefix(buffer, "tagger ", &buffer))
434 /* early tags do not contain 'tagger' lines; warn only */
22410549 435 options->error_func(&tag->object, FSCK_WARN, "invalid format - expected 'tagger' line");
cec097be 436 else
22410549 437 ret = fsck_ident(&buffer, &tag->object, options);
cec097be
JS
438
439done:
440 strbuf_release(&sb);
441 free(to_free);
442 return ret;
443}
444
90a398bb 445static int fsck_tag(struct tag *tag, const char *data,
22410549 446 unsigned long size, struct fsck_options *options)
ba002f3b
MK
447{
448 struct object *tagged = tag->tagged;
449
450 if (!tagged)
22410549 451 return options->error_func(&tag->object, FSCK_ERROR, "could not load tagged object");
cec097be 452
22410549 453 return fsck_tag_buffer(tag, data, size, options);
ba002f3b
MK
454}
455
90a398bb 456int fsck_object(struct object *obj, void *data, unsigned long size,
22410549 457 struct fsck_options *options)
ba002f3b
MK
458{
459 if (!obj)
22410549 460 return options->error_func(obj, FSCK_ERROR, "no valid object to fsck");
ba002f3b
MK
461
462 if (obj->type == OBJ_BLOB)
463 return 0;
464 if (obj->type == OBJ_TREE)
22410549 465 return fsck_tree((struct tree *) obj, options);
ba002f3b 466 if (obj->type == OBJ_COMMIT)
90a398bb 467 return fsck_commit((struct commit *) obj, (const char *) data,
22410549 468 size, options);
ba002f3b 469 if (obj->type == OBJ_TAG)
90a398bb 470 return fsck_tag((struct tag *) obj, (const char *) data,
22410549 471 size, options);
ba002f3b 472
22410549 473 return options->error_func(obj, FSCK_ERROR, "unknown type '%d' (internal fsck error)",
ba002f3b
MK
474 obj->type);
475}
d6ffc8d7
MK
476
477int fsck_error_function(struct object *obj, int type, const char *fmt, ...)
478{
479 va_list ap;
f285a2d7 480 struct strbuf sb = STRBUF_INIT;
d6ffc8d7 481
5dd56489 482 strbuf_addf(&sb, "object %s:", sha1_to_hex(obj->sha1));
d6ffc8d7
MK
483
484 va_start(ap, fmt);
ebeb6090 485 strbuf_vaddf(&sb, fmt, ap);
d6ffc8d7
MK
486 va_end(ap);
487
9db56f71 488 error("%s", sb.buf);
d6ffc8d7
MK
489 strbuf_release(&sb);
490 return 1;
491}