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"
910
static int fsck_walk_tree(struct tree *tree, fsck_walk_func walk, void *data)
11{
12struct tree_desc desc;
13struct name_entry entry;
14int res = 0;
1516
if (parse_tree(tree))
17return -1;
1819
init_tree_desc(&desc, tree->buffer, tree->size);
20while (tree_entry(&desc, &entry)) {
21int result;
2223
if (S_ISGITLINK(entry.mode))
24continue;
25if (S_ISDIR(entry.mode))
26result = walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data);
27else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
28result = walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data);
29else {
30result = error("in tree %s: entry %s has bad mode %.6o\n",
31sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
32}
33if (result < 0)
34return result;
35if (!res)
36res = result;
37}
38return res;
39}
4041
static int fsck_walk_commit(struct commit *commit, fsck_walk_func walk, void *data)
42{
43struct commit_list *parents;
44int res;
45int result;
4647
if (parse_commit(commit))
48return -1;
4950
result = walk((struct object *)commit->tree, OBJ_TREE, data);
51if (result < 0)
52return result;
53res = result;
5455
parents = commit->parents;
56while (parents) {
57result = walk((struct object *)parents->item, OBJ_COMMIT, data);
58if (result < 0)
59return result;
60if (!res)
61res = result;
62parents = parents->next;
63}
64return res;
65}
6667
static int fsck_walk_tag(struct tag *tag, fsck_walk_func walk, void *data)
68{
69if (parse_tag(tag))
70return -1;
71return walk(tag->tagged, OBJ_ANY, data);
72}
7374
int fsck_walk(struct object *obj, fsck_walk_func walk, void *data)
75{
76if (!obj)
77return -1;
78switch (obj->type) {
79case OBJ_BLOB:
80return 0;
81case OBJ_TREE:
82return fsck_walk_tree((struct tree *)obj, walk, data);
83case OBJ_COMMIT:
84return fsck_walk_commit((struct commit *)obj, walk, data);
85case OBJ_TAG:
86return fsck_walk_tag((struct tag *)obj, walk, data);
87default:
88error("Unknown object type for %s", sha1_to_hex(obj->sha1));
89return -1;
90}
91}
9293
/*
94* The entries in a tree are ordered in the _path_ order,
95* which means that a directory entry is ordered by adding
96* a slash to the end of it.
97*
98* So a directory called "a" is ordered _after_ a file
99* called "a.c", because "a/" sorts after "a.c".
100*/
101#define TREE_UNORDERED (-1)
102#define TREE_HAS_DUPS (-2)
103104
static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
105{
106int len1 = strlen(name1);
107int len2 = strlen(name2);
108int len = len1 < len2 ? len1 : len2;
109unsigned char c1, c2;
110int cmp;
111112
cmp = memcmp(name1, name2, len);
113if (cmp < 0)
114return 0;
115if (cmp > 0)
116return TREE_UNORDERED;
117118
/*
119* Ok, the first <len> characters are the same.
120* Now we need to order the next one, but turn
121* a '\0' into a '/' for a directory entry.
122*/
123c1 = name1[len];
124c2 = name2[len];
125if (!c1 && !c2)
126/*
127* git-write-tree used to write out a nonsense tree that has
128* entries with the same name, one blob and one tree. Make
129* sure we do not have duplicate entries.
130*/
131return TREE_HAS_DUPS;
132if (!c1 && S_ISDIR(mode1))
133c1 = '/';
134if (!c2 && S_ISDIR(mode2))
135c2 = '/';
136return c1 < c2 ? 0 : TREE_UNORDERED;
137}
138139
static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
140{
141int retval;
142int has_full_path = 0;
143int has_empty_name = 0;
144int has_zero_pad = 0;
145int has_bad_modes = 0;
146int has_dup_entries = 0;
147int not_properly_sorted = 0;
148struct tree_desc desc;
149unsigned o_mode;
150const char *o_name;
151const unsigned char *o_sha1;
152153
init_tree_desc(&desc, item->buffer, item->size);
154155
o_mode = 0;
156o_name = NULL;
157o_sha1 = NULL;
158if (!desc.size)
159return error_func(&item->object, FSCK_ERROR, "empty tree");
160161
while (desc.size) {
162unsigned mode;
163const char *name;
164const unsigned char *sha1;
165166
sha1 = tree_entry_extract(&desc, &name, &mode);
167168
if (strchr(name, '/'))
169has_full_path = 1;
170if (!*name)
171has_empty_name = 1;
172has_zero_pad |= *(char *)desc.buffer == '0';
173update_tree_entry(&desc);
174175
switch (mode) {
176/*
177* Standard modes..
178*/
179case S_IFREG | 0755:
180case S_IFREG | 0644:
181case S_IFLNK:
182case S_IFDIR:
183case S_IFGITLINK:
184break;
185/*
186* This is nonstandard, but we had a few of these
187* early on when we honored the full set of mode
188* bits..
189*/
190case S_IFREG | 0664:
191if (!strict)
192break;
193default:
194has_bad_modes = 1;
195}
196197
if (o_name) {
198switch (verify_ordered(o_mode, o_name, mode, name)) {
199case TREE_UNORDERED:
200not_properly_sorted = 1;
201break;
202case TREE_HAS_DUPS:
203has_dup_entries = 1;
204break;
205default:
206break;
207}
208}
209210
o_mode = mode;
211o_name = name;
212o_sha1 = sha1;
213}
214215
retval = 0;
216if (has_full_path)
217retval += error_func(&item->object, FSCK_WARN, "contains full pathnames");
218if (has_empty_name)
219retval += error_func(&item->object, FSCK_WARN, "contains empty pathname");
220if (has_zero_pad)
221retval += error_func(&item->object, FSCK_WARN, "contains zero-padded file modes");
222if (has_bad_modes)
223retval += error_func(&item->object, FSCK_WARN, "contains bad file modes");
224if (has_dup_entries)
225retval += error_func(&item->object, FSCK_ERROR, "contains duplicate file entries");
226if (not_properly_sorted)
227retval += error_func(&item->object, FSCK_ERROR, "not properly sorted");
228return retval;
229}
230231
static int fsck_commit(struct commit *commit, fsck_error error_func)
232{
233char *buffer = commit->buffer;
234unsigned char tree_sha1[20], sha1[20];
235struct commit_graft *graft;
236int parents = 0;
237238
if (!commit->date)
239return error_func(&commit->object, FSCK_ERROR, "invalid author/committer line");
240241
if (memcmp(buffer, "tree ", 5))
242return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line");
243if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
244return error_func(&commit->object, FSCK_ERROR, "invalid 'tree' line format - bad sha1");
245buffer += 46;
246while (!memcmp(buffer, "parent ", 7)) {
247if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
248return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1");
249buffer += 48;
250parents++;
251}
252graft = lookup_commit_graft(commit->object.sha1);
253if (graft) {
254struct commit_list *p = commit->parents;
255parents = 0;
256while (p) {
257p = p->next;
258parents++;
259}
260if (graft->nr_parent == -1 && !parents)
261; /* shallow commit */
262else if (graft->nr_parent != parents)
263return error_func(&commit->object, FSCK_ERROR, "graft objects missing");
264} else {
265struct commit_list *p = commit->parents;
266while (p && parents) {
267p = p->next;
268parents--;
269}
270if (p || parents)
271return error_func(&commit->object, FSCK_ERROR, "parent objects missing");
272}
273if (memcmp(buffer, "author ", 7))
274return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'author' line");
275if (!commit->tree)
276return error_func(&commit->object, FSCK_ERROR, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
277278
return 0;
279}
280281
static int fsck_tag(struct tag *tag, fsck_error error_func)
282{
283struct object *tagged = tag->tagged;
284285
if (!tagged)
286return error_func(&tag->object, FSCK_ERROR, "could not load tagged object");
287return 0;
288}
289290
int fsck_object(struct object *obj, int strict, fsck_error error_func)
291{
292if (!obj)
293return error_func(obj, FSCK_ERROR, "no valid object to fsck");
294295
if (obj->type == OBJ_BLOB)
296return 0;
297if (obj->type == OBJ_TREE)
298return fsck_tree((struct tree *) obj, strict, error_func);
299if (obj->type == OBJ_COMMIT)
300return fsck_commit((struct commit *) obj, error_func);
301if (obj->type == OBJ_TAG)
302return fsck_tag((struct tag *) obj, error_func);
303304
return error_func(obj, FSCK_ERROR, "unknown type '%d' (internal fsck error)",
305obj->type);
306}
307308
int fsck_error_function(struct object *obj, int type, const char *fmt, ...)
309{
310va_list ap;
311int len;
312struct strbuf sb;
313314
strbuf_init(&sb, 0);
315strbuf_addf(&sb, "object %s:", obj->sha1?sha1_to_hex(obj->sha1):"(null)");
316317
va_start(ap, fmt);
318len = vsnprintf(sb.buf + sb.len, strbuf_avail(&sb), fmt, ap);
319va_end(ap);
320321
if (len < 0)
322len = 0;
323if (len >= strbuf_avail(&sb)) {
324strbuf_grow(&sb, len + 2);
325va_start(ap, fmt);
326len = vsnprintf(sb.buf + sb.len, strbuf_avail(&sb), fmt, ap);
327va_end(ap);
328if (len >= strbuf_avail(&sb))
329die("this should not happen, your snprintf is broken");
330}
331332
error(sb.buf);
333strbuf_release(&sb);
334return 1;
335}