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"910static 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;1516if (parse_tree(tree))17return -1;1819init_tree_desc(&desc, tree->buffer, tree->size);20while (tree_entry(&desc, &entry)) {21int result;2223if (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}4041static int fsck_walk_commit(struct commit *commit, fsck_walk_func walk, void *data)42{43struct commit_list *parents;44int res;45int result;4647if (parse_commit(commit))48return -1;4950result = walk((struct object *)commit->tree, OBJ_TREE, data);51if (result < 0)52return result;53res = result;5455parents = 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}6667static 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}7374int 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 adding96* a slash to the end of it.97*98* So a directory called "a" is ordered _after_ a file99* called "a.c", because "a/" sorts after "a.c".100*/101#define TREE_UNORDERED (-1)102#define TREE_HAS_DUPS (-2)103104static 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;111112cmp = 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 turn121* 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 has128* entries with the same name, one blob and one tree. Make129* 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}138139static 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;152153init_tree_desc(&desc, item->buffer, item->size);154155o_mode = 0;156o_name = NULL;157o_sha1 = NULL;158159while (desc.size) {160unsigned mode;161const char *name;162const unsigned char *sha1;163164sha1 = tree_entry_extract(&desc, &name, &mode);165166if (strchr(name, '/'))167has_full_path = 1;168if (!*name)169has_empty_name = 1;170has_zero_pad |= *(char *)desc.buffer == '0';171update_tree_entry(&desc);172173switch (mode) {174/*175* Standard modes..176*/177case S_IFREG | 0755:178case S_IFREG | 0644:179case S_IFLNK:180case S_IFDIR:181case S_IFGITLINK:182break;183/*184* This is nonstandard, but we had a few of these185* early on when we honored the full set of mode186* bits..187*/188case S_IFREG | 0664:189if (!strict)190break;191default:192has_bad_modes = 1;193}194195if (o_name) {196switch (verify_ordered(o_mode, o_name, mode, name)) {197case TREE_UNORDERED:198not_properly_sorted = 1;199break;200case TREE_HAS_DUPS:201has_dup_entries = 1;202break;203default:204break;205}206}207208o_mode = mode;209o_name = name;210o_sha1 = sha1;211}212213retval = 0;214if (has_full_path)215retval += error_func(&item->object, FSCK_WARN, "contains full pathnames");216if (has_empty_name)217retval += error_func(&item->object, FSCK_WARN, "contains empty pathname");218if (has_zero_pad)219retval += error_func(&item->object, FSCK_WARN, "contains zero-padded file modes");220if (has_bad_modes)221retval += error_func(&item->object, FSCK_WARN, "contains bad file modes");222if (has_dup_entries)223retval += error_func(&item->object, FSCK_ERROR, "contains duplicate file entries");224if (not_properly_sorted)225retval += error_func(&item->object, FSCK_ERROR, "not properly sorted");226return retval;227}228229static int fsck_commit(struct commit *commit, fsck_error error_func)230{231char *buffer = commit->buffer;232unsigned char tree_sha1[20], sha1[20];233struct commit_graft *graft;234int parents = 0;235236if (!commit->date)237return error_func(&commit->object, FSCK_ERROR, "invalid author/committer line");238239if (memcmp(buffer, "tree ", 5))240return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line");241if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')242return error_func(&commit->object, FSCK_ERROR, "invalid 'tree' line format - bad sha1");243buffer += 46;244while (!memcmp(buffer, "parent ", 7)) {245if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')246return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1");247buffer += 48;248parents++;249}250graft = lookup_commit_graft(commit->object.sha1);251if (graft) {252struct commit_list *p = commit->parents;253parents = 0;254while (p) {255p = p->next;256parents++;257}258if (graft->nr_parent == -1 && !parents)259; /* shallow commit */260else if (graft->nr_parent != parents)261return error_func(&commit->object, FSCK_ERROR, "graft objects missing");262} else {263struct commit_list *p = commit->parents;264while (p && parents) {265p = p->next;266parents--;267}268if (p || parents)269return error_func(&commit->object, FSCK_ERROR, "parent objects missing");270}271if (memcmp(buffer, "author ", 7))272return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'author' line");273if (!commit->tree)274return error_func(&commit->object, FSCK_ERROR, "could not load commit's tree %s", sha1_to_hex(tree_sha1));275276return 0;277}278279static int fsck_tag(struct tag *tag, fsck_error error_func)280{281struct object *tagged = tag->tagged;282283if (!tagged)284return error_func(&tag->object, FSCK_ERROR, "could not load tagged object");285return 0;286}287288int fsck_object(struct object *obj, int strict, fsck_error error_func)289{290if (!obj)291return error_func(obj, FSCK_ERROR, "no valid object to fsck");292293if (obj->type == OBJ_BLOB)294return 0;295if (obj->type == OBJ_TREE)296return fsck_tree((struct tree *) obj, strict, error_func);297if (obj->type == OBJ_COMMIT)298return fsck_commit((struct commit *) obj, error_func);299if (obj->type == OBJ_TAG)300return fsck_tag((struct tag *) obj, error_func);301302return error_func(obj, FSCK_ERROR, "unknown type '%d' (internal fsck error)",303obj->type);304}305306int fsck_error_function(struct object *obj, int type, const char *fmt, ...)307{308va_list ap;309int len;310struct strbuf sb = STRBUF_INIT;311312strbuf_addf(&sb, "object %s:", obj->sha1?sha1_to_hex(obj->sha1):"(null)");313314va_start(ap, fmt);315len = vsnprintf(sb.buf + sb.len, strbuf_avail(&sb), fmt, ap);316va_end(ap);317318if (len < 0)319len = 0;320if (len >= strbuf_avail(&sb)) {321strbuf_grow(&sb, len + 2);322va_start(ap, fmt);323len = vsnprintf(sb.buf + sb.len, strbuf_avail(&sb), fmt, ap);324va_end(ap);325if (len >= strbuf_avail(&sb))326die("this should not happen, your snprintf is broken");327}328329error("%s", sb.buf);330strbuf_release(&sb);331return 1;332}