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;
151152
init_tree_desc(&desc, item->buffer, item->size);
153154
o_mode = 0;
155o_name = NULL;
156157
while (desc.size) {
158unsigned mode;
159const char *name;
160161
tree_entry_extract(&desc, &name, &mode);
162163
if (strchr(name, '/'))
164has_full_path = 1;
165if (!*name)
166has_empty_name = 1;
167has_zero_pad |= *(char *)desc.buffer == '0';
168update_tree_entry(&desc);
169170
switch (mode) {
171/*
172* Standard modes..
173*/
174case S_IFREG | 0755:
175case S_IFREG | 0644:
176case S_IFLNK:
177case S_IFDIR:
178case S_IFGITLINK:
179break;
180/*
181* This is nonstandard, but we had a few of these
182* early on when we honored the full set of mode
183* bits..
184*/
185case S_IFREG | 0664:
186if (!strict)
187break;
188default:
189has_bad_modes = 1;
190}
191192
if (o_name) {
193switch (verify_ordered(o_mode, o_name, mode, name)) {
194case TREE_UNORDERED:
195not_properly_sorted = 1;
196break;
197case TREE_HAS_DUPS:
198has_dup_entries = 1;
199break;
200default:
201break;
202}
203}
204205
o_mode = mode;
206o_name = name;
207}
208209
retval = 0;
210if (has_full_path)
211retval += error_func(&item->object, FSCK_WARN, "contains full pathnames");
212if (has_empty_name)
213retval += error_func(&item->object, FSCK_WARN, "contains empty pathname");
214if (has_zero_pad)
215retval += error_func(&item->object, FSCK_WARN, "contains zero-padded file modes");
216if (has_bad_modes)
217retval += error_func(&item->object, FSCK_WARN, "contains bad file modes");
218if (has_dup_entries)
219retval += error_func(&item->object, FSCK_ERROR, "contains duplicate file entries");
220if (not_properly_sorted)
221retval += error_func(&item->object, FSCK_ERROR, "not properly sorted");
222return retval;
223}
224225
static int fsck_commit(struct commit *commit, fsck_error error_func)
226{
227char *buffer = commit->buffer;
228unsigned char tree_sha1[20], sha1[20];
229struct commit_graft *graft;
230int parents = 0;
231232
if (commit->date == ULONG_MAX)
233return error_func(&commit->object, FSCK_ERROR, "invalid author/committer line");
234235
if (memcmp(buffer, "tree ", 5))
236return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line");
237if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
238return error_func(&commit->object, FSCK_ERROR, "invalid 'tree' line format - bad sha1");
239buffer += 46;
240while (!memcmp(buffer, "parent ", 7)) {
241if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
242return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1");
243buffer += 48;
244parents++;
245}
246graft = lookup_commit_graft(commit->object.sha1);
247if (graft) {
248struct commit_list *p = commit->parents;
249parents = 0;
250while (p) {
251p = p->next;
252parents++;
253}
254if (graft->nr_parent == -1 && !parents)
255; /* shallow commit */
256else if (graft->nr_parent != parents)
257return error_func(&commit->object, FSCK_ERROR, "graft objects missing");
258} else {
259struct commit_list *p = commit->parents;
260while (p && parents) {
261p = p->next;
262parents--;
263}
264if (p || parents)
265return error_func(&commit->object, FSCK_ERROR, "parent objects missing");
266}
267if (memcmp(buffer, "author ", 7))
268return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'author' line");
269if (!commit->tree)
270return error_func(&commit->object, FSCK_ERROR, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
271272
return 0;
273}
274275
static int fsck_tag(struct tag *tag, fsck_error error_func)
276{
277struct object *tagged = tag->tagged;
278279
if (!tagged)
280return error_func(&tag->object, FSCK_ERROR, "could not load tagged object");
281return 0;
282}
283284
int fsck_object(struct object *obj, int strict, fsck_error error_func)
285{
286if (!obj)
287return error_func(obj, FSCK_ERROR, "no valid object to fsck");
288289
if (obj->type == OBJ_BLOB)
290return 0;
291if (obj->type == OBJ_TREE)
292return fsck_tree((struct tree *) obj, strict, error_func);
293if (obj->type == OBJ_COMMIT)
294return fsck_commit((struct commit *) obj, error_func);
295if (obj->type == OBJ_TAG)
296return fsck_tag((struct tag *) obj, error_func);
297298
return error_func(obj, FSCK_ERROR, "unknown type '%d' (internal fsck error)",
299obj->type);
300}
301302
int fsck_error_function(struct object *obj, int type, const char *fmt, ...)
303{
304va_list ap;
305int len;
306struct strbuf sb = STRBUF_INIT;
307308
strbuf_addf(&sb, "object %s:", obj->sha1?sha1_to_hex(obj->sha1):"(null)");
309310
va_start(ap, fmt);
311len = vsnprintf(sb.buf + sb.len, strbuf_avail(&sb), fmt, ap);
312va_end(ap);
313314
if (len < 0)
315len = 0;
316if (len >= strbuf_avail(&sb)) {
317strbuf_grow(&sb, len + 2);
318va_start(ap, fmt);
319len = vsnprintf(sb.buf + sb.len, strbuf_avail(&sb), fmt, ap);
320va_end(ap);
321if (len >= strbuf_avail(&sb))
322die("this should not happen, your snprintf is broken");
323}
324325
error("%s", sb.buf);
326strbuf_release(&sb);
327return 1;
328}