git-tar-tree: no more void pointer arithmetic
[git.git] / fsck-objects.c
index 0c5c430..2b1aab4 100644 (file)
@@ -8,54 +8,99 @@
 #include "tag.h"
 #include "refs.h"
 #include "pack.h"
+#include "cache-tree.h"
+#include "tree-walk.h"
 
 #define REACHABLE 0x0001
+#define SEEN      0x0002
 
 static int show_root = 0;
 static int show_tags = 0;
 static int show_unreachable = 0;
-static int standalone = 0;
 static int check_full = 0;
 static int check_strict = 0;
-static int keep_cache_objects = 0; 
+static int keep_cache_objects = 0;
 static unsigned char head_sha1[20];
 
+#ifdef NO_D_INO_IN_DIRENT
+#define SORT_DIRENT 0
+#define DIRENT_SORT_HINT(de) 0
+#else
+#define SORT_DIRENT 1
+#define DIRENT_SORT_HINT(de) ((de)->d_ino)
+#endif
+
+static void objreport(struct object *obj, const char *severity,
+                      const char *err, va_list params)
+{
+       fprintf(stderr, "%s in %s %s: ",
+               severity, typename(obj->type), sha1_to_hex(obj->sha1));
+       vfprintf(stderr, err, params);
+       fputs("\n", stderr);
+}
+
+static int objerror(struct object *obj, const char *err, ...)
+{
+       va_list params;
+       va_start(params, err);
+       objreport(obj, "error", err, params);
+       va_end(params);
+       return -1;
+}
+
+static int objwarning(struct object *obj, const char *err, ...)
+{
+       va_list params;
+       va_start(params, err);
+       objreport(obj, "warning", err, params);
+       va_end(params);
+       return -1;
+}
+
+
 static void check_connectivity(void)
 {
        int i;
 
        /* Look up all the requirements, warn about missing objects.. */
-       for (i = 0; i < nr_objs; i++) {
+       for (i = 0; i < obj_allocs; i++) {
                struct object *obj = objs[i];
-               struct object_list *refs;
+
+               if (!obj)
+                       continue;
 
                if (!obj->parsed) {
-                       if (!standalone && has_sha1_file(obj->sha1))
+                       if (has_sha1_file(obj->sha1))
                                ; /* it is in pack */
                        else
                                printf("missing %s %s\n",
-                                      obj->type, sha1_to_hex(obj->sha1));
+                                      typename(obj->type), sha1_to_hex(obj->sha1));
                        continue;
                }
 
-               for (refs = obj->refs; refs; refs = refs->next) {
-                       if (refs->item->parsed ||
-                           (!standalone && has_sha1_file(refs->item->sha1)))
-                               continue;
-                       printf("broken link from %7s %s\n",
-                              obj->type, sha1_to_hex(obj->sha1));
-                       printf("              to %7s %s\n",
-                              refs->item->type, sha1_to_hex(refs->item->sha1));
+               if (obj->refs) {
+                       const struct object_refs *refs = obj->refs;
+                       unsigned j;
+                       for (j = 0; j < refs->count; j++) {
+                               struct object *ref = refs->ref[j];
+                               if (ref->parsed ||
+                                   (has_sha1_file(ref->sha1)))
+                                       continue;
+                               printf("broken link from %7s %s\n",
+                                      typename(obj->type), sha1_to_hex(obj->sha1));
+                               printf("              to %7s %s\n",
+                                      typename(ref->type), sha1_to_hex(ref->sha1));
+                       }
                }
 
                if (show_unreachable && !(obj->flags & REACHABLE)) {
                        printf("unreachable %s %s\n",
-                              obj->type, sha1_to_hex(obj->sha1));
+                              typename(obj->type), sha1_to_hex(obj->sha1));
                        continue;
                }
 
                if (!obj->used) {
-                       printf("dangling %s %s\n", obj->type, 
+                       printf("dangling %s %s\n", typename(obj->type),
                               sha1_to_hex(obj->sha1));
                }
        }
@@ -72,15 +117,15 @@ static void check_connectivity(void)
 #define TREE_UNORDERED (-1)
 #define TREE_HAS_DUPS  (-2)
 
-static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
+static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
 {
-       int len1 = strlen(a->name);
-       int len2 = strlen(b->name);
+       int len1 = strlen(name1);
+       int len2 = strlen(name2);
        int len = len1 < len2 ? len1 : len2;
        unsigned char c1, c2;
        int cmp;
 
-       cmp = memcmp(a->name, b->name, len);
+       cmp = memcmp(name1, name2, len);
        if (cmp < 0)
                return 0;
        if (cmp > 0)
@@ -91,8 +136,8 @@ static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
         * Now we need to order the next one, but turn
         * a '\0' into a '/' for a directory entry.
         */
-       c1 = a->name[len];
-       c2 = b->name[len];
+       c1 = name1[len];
+       c2 = name2[len];
        if (!c1 && !c2)
                /*
                 * git-write-tree used to write out a nonsense tree that has
@@ -100,9 +145,9 @@ static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
                 * sure we do not have duplicate entries.
                 */
                return TREE_HAS_DUPS;
-       if (!c1 && a->directory)
+       if (!c1 && S_ISDIR(mode1))
                c1 = '/';
-       if (!c2 && b->directory)
+       if (!c2 && S_ISDIR(mode2))
                c2 = '/';
        return c1 < c2 ? 0 : TREE_UNORDERED;
 }
@@ -115,17 +160,32 @@ static int fsck_tree(struct tree *item)
        int has_bad_modes = 0;
        int has_dup_entries = 0;
        int not_properly_sorted = 0;
-       struct tree_entry_list *entry, *last;
+       struct tree_desc desc;
+       unsigned o_mode;
+       const char *o_name;
+       const unsigned char *o_sha1;
+
+       desc.buf = item->buffer;
+       desc.size = item->size;
 
-       last = NULL;
-       for (entry = item->entries; entry; entry = entry->next) {
-               if (strchr(entry->name, '/'))
+       o_mode = 0;
+       o_name = NULL;
+       o_sha1 = NULL;
+       while (desc.size) {
+               unsigned mode;
+               const char *name;
+               const unsigned char *sha1;
+
+               sha1 = tree_entry_extract(&desc, &name, &mode);
+
+               if (strchr(name, '/'))
                        has_full_path = 1;
-               has_zero_pad |= entry->zeropad;
+               has_zero_pad |= *(char *)desc.buf == '0';
+               update_tree_entry(&desc);
 
-               switch (entry->mode) {
+               switch (mode) {
                /*
-                * Standard modes.. 
+                * Standard modes..
                 */
                case S_IFREG | 0755:
                case S_IFREG | 0644:
@@ -144,8 +204,8 @@ static int fsck_tree(struct tree *item)
                        has_bad_modes = 1;
                }
 
-               if (last) {
-                       switch (verify_ordered(last, entry)) {
+               if (o_name) {
+                       switch (verify_ordered(o_mode, o_name, mode, name)) {
                        case TREE_UNORDERED:
                                not_properly_sorted = 1;
                                break;
@@ -157,36 +217,28 @@ static int fsck_tree(struct tree *item)
                        }
                }
 
-               last = entry;
+               o_mode = mode;
+               o_name = name;
+               o_sha1 = sha1;
        }
+       free(item->buffer);
+       item->buffer = NULL;
 
        retval = 0;
        if (has_full_path) {
-               fprintf(stderr, "warning: git-fsck-objects: tree %s "
-                       "has full pathnames in it\n", 
-                       sha1_to_hex(item->object.sha1));
+               objwarning(&item->object, "contains full pathnames");
        }
        if (has_zero_pad) {
-               fprintf(stderr, "warning: git-fsck-objects: tree %s "
-                       "has zero-padded file modes in it\n",
-                       sha1_to_hex(item->object.sha1));
+               objwarning(&item->object, "contains zero-padded file modes");
        }
        if (has_bad_modes) {
-               fprintf(stderr, "warning: git-fsck-objects: tree %s "
-                       "has bad file modes in it\n",
-                       sha1_to_hex(item->object.sha1));
+               objwarning(&item->object, "contains bad file modes");
        }
        if (has_dup_entries) {
-               fprintf(stderr, "error: git-fsck-objects: tree %s "
-                       "has duplicate file entries\n",
-                       sha1_to_hex(item->object.sha1));
-               retval = -1;
+               retval = objerror(&item->object, "contains duplicate file entries");
        }
        if (not_properly_sorted) {
-               fprintf(stderr, "error: git-fsck-objects: tree %s "
-                       "is not properly sorted\n",
-                       sha1_to_hex(item->object.sha1));
-               retval = -1;
+               retval = objerror(&item->object, "not properly sorted");
        }
        return retval;
 }
@@ -194,24 +246,24 @@ static int fsck_tree(struct tree *item)
 static int fsck_commit(struct commit *commit)
 {
        char *buffer = commit->buffer;
-       unsigned char sha1[20];
+       unsigned char tree_sha1[20], sha1[20];
 
        if (memcmp(buffer, "tree ", 5))
-               return -1;
-       if (get_sha1_hex(buffer+5, sha1) || buffer[45] != '\n')
-               return -1;
+               return objerror(&commit->object, "invalid format - expected 'tree' line");
+       if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
+               return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
        buffer += 46;
        while (!memcmp(buffer, "parent ", 7)) {
                if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
-                       return -1;
+                       return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
                buffer += 48;
        }
        if (memcmp(buffer, "author ", 7))
-               return -1;
+               return objerror(&commit->object, "invalid format - expected 'author' line");
        free(commit->buffer);
        commit->buffer = NULL;
        if (!commit->tree)
-               return -1;
+               return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
        if (!commit->parents && show_root)
                printf("root %s\n", sha1_to_hex(commit->object.sha1));
        if (!commit->date)
@@ -225,13 +277,12 @@ static int fsck_tag(struct tag *tag)
        struct object *tagged = tag->tagged;
 
        if (!tagged) {
-               printf("bad object in tag %s\n", sha1_to_hex(tag->object.sha1));
-               return -1;
+               return objerror(&tag->object, "could not load tagged object");
        }
        if (!show_tags)
                return 0;
 
-       printf("tagged %s %s", tagged->type, sha1_to_hex(tagged->sha1));
+       printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1));
        printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
        return 0;
 }
@@ -240,16 +291,20 @@ static int fsck_sha1(unsigned char *sha1)
 {
        struct object *obj = parse_object(sha1);
        if (!obj)
-               return -1;
-       if (obj->type == blob_type)
+               return error("%s: object not found", sha1_to_hex(sha1));
+       if (obj->flags & SEEN)
                return 0;
-       if (obj->type == tree_type)
+       obj->flags |= SEEN;
+       if (obj->type == TYPE_BLOB)
+               return 0;
+       if (obj->type == TYPE_TREE)
                return fsck_tree((struct tree *) obj);
-       if (obj->type == commit_type)
+       if (obj->type == TYPE_COMMIT)
                return fsck_commit((struct commit *) obj);
-       if (obj->type == tag_type)
+       if (obj->type == TYPE_TAG)
                return fsck_tag((struct tag *) obj);
-       return -1;
+       /* By now, parse_object() would've returned NULL instead. */
+       return objerror(obj, "unknown type '%d' (internal fsck error)", obj->type);
 }
 
 /*
@@ -279,14 +334,15 @@ static void fsck_sha1_list(void)
 {
        int i, nr = sha1_list.nr;
 
-       qsort(sha1_list.entry, nr, sizeof(struct sha1_entry *), ino_compare);
+       if (SORT_DIRENT)
+               qsort(sha1_list.entry, nr,
+                     sizeof(struct sha1_entry *), ino_compare);
        for (i = 0; i < nr; i++) {
                struct sha1_entry *entry = sha1_list.entry[i];
                unsigned char *sha1 = entry->sha1;
 
                sha1_list.entry[i] = NULL;
-               if (fsck_sha1(sha1) < 0)
-                       fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
+               fsck_sha1(sha1);
                free(entry);
        }
        sha1_list.nr = 0;
@@ -313,9 +369,8 @@ static int fsck_dir(int i, char *path)
        DIR *dir = opendir(path);
        struct dirent *de;
 
-       if (!dir) {
-               return error("missing sha1 directory '%s'", path);
-       }
+       if (!dir)
+               return 0;
 
        while ((de = readdir(dir)) != NULL) {
                char name[100];
@@ -335,7 +390,7 @@ static int fsck_dir(int i, char *path)
                        memcpy(name+2, de->d_name, len+1);
                        if (get_sha1_hex(name, sha1) < 0)
                                break;
-                       add_sha1_list(sha1, de->d_ino);
+                       add_sha1_list(sha1, DIRENT_SORT_HINT(de));
                        continue;
                }
                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
@@ -352,7 +407,7 @@ static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
 
        obj = lookup_object(sha1);
        if (!obj) {
-               if (!standalone && has_sha1_file(sha1)) {
+               if (has_sha1_file(sha1)) {
                        default_refs++;
                        return 0; /* it is in a pack */
                }
@@ -386,36 +441,50 @@ static void fsck_object_dir(const char *path)
 
 static int fsck_head_link(void)
 {
-       int fd, count;
-       char hex[40];
        unsigned char sha1[20];
-       static char path[PATH_MAX], link[PATH_MAX];
-       const char *git_dir;
-
-       git_dir = getenv(GIT_DIR_ENVIRONMENT);
-       if (!git_dir) git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
-
-       snprintf(path, sizeof(path), "%s/HEAD", git_dir);
-       if (readlink(path, link, sizeof(link)) < 0)
-               return error("HEAD is not a symlink");
-       if (strncmp("refs/heads/", link, 11))
-               return error("HEAD points to something strange (%s)", link);
-       fd = open(path, O_RDONLY);
-       if (fd < 0)
-               return error("HEAD: %s", strerror(errno));
-       count = read(fd, hex, sizeof(hex));
-       close(fd);
-       if (count < 0)
-               return error("HEAD: %s", strerror(errno));
-       if (count < 40 || get_sha1_hex(hex, sha1))
+       const char *git_HEAD = strdup(git_path("HEAD"));
+       const char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 1);
+       int pfxlen = strlen(git_HEAD) - 4; /* strip .../.git/ part */
+
+       if (!git_refs_heads_master)
+               return error("HEAD is not a symbolic ref");
+       if (strncmp(git_refs_heads_master + pfxlen, "refs/heads/", 11))
+               return error("HEAD points to something strange (%s)",
+                            git_refs_heads_master + pfxlen);
+       if (!memcmp(null_sha1, sha1, 20))
                return error("HEAD: not a valid git pointer");
        return 0;
 }
 
+static int fsck_cache_tree(struct cache_tree *it)
+{
+       int i;
+       int err = 0;
+
+       if (0 <= it->entry_count) {
+               struct object *obj = parse_object(it->sha1);
+               if (!obj) {
+                       error("%s: invalid sha1 pointer in cache-tree",
+                             sha1_to_hex(it->sha1));
+                       return 1;
+               }
+               mark_reachable(obj, REACHABLE);
+               obj->used = 1;
+               if (obj->type != TYPE_TREE)
+                       err |= objerror(obj, "non-tree in cache-tree");
+       }
+       for (i = 0; i < it->subtree_nr; i++)
+               err |= fsck_cache_tree(it->down[i]->cache_tree);
+       return err;
+}
+
 int main(int argc, char **argv)
 {
        int i, heads;
 
+       track_object_refs = 1;
+       setup_git_directory();
+
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
 
@@ -435,10 +504,6 @@ int main(int argc, char **argv)
                        keep_cache_objects = 1;
                        continue;
                }
-               if (!strcmp(arg, "--standalone")) {
-                       standalone = 1;
-                       continue;
-               }
                if (!strcmp(arg, "--full")) {
                        check_full = 1;
                        continue;
@@ -448,14 +513,9 @@ int main(int argc, char **argv)
                        continue;
                }
                if (*arg == '-')
-                       usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--standalone | --full] [--strict] <head-sha1>*]");
+                       usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--full] [--strict] <head-sha1>*]");
        }
 
-       if (standalone && check_full)
-               die("Only one of --standalone or --full can be used.");
-       if (standalone)
-               putenv("GIT_ALTERNATE_OBJECT_DIRECTORIES=");
-
        fsck_head_link();
        fsck_object_dir(get_object_directory());
        if (check_full) {
@@ -479,9 +539,7 @@ int main(int argc, char **argv)
                        for (i = 0; i < num; i++) {
                                unsigned char sha1[20];
                                nth_packed_object_sha1(p, i, sha1);
-                               if (fsck_sha1(sha1) < 0)
-                                       fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
-
+                               fsck_sha1(sha1);
                        }
                }
        }
@@ -505,7 +563,7 @@ int main(int argc, char **argv)
                        heads++;
                        continue;
                }
-               error("expected sha1, got %s", arg);
+               error("invalid parameter: expected sha1, got '%s'", arg);
        }
 
        /*
@@ -530,6 +588,8 @@ int main(int argc, char **argv)
                        obj->used = 1;
                        mark_reachable(obj, REACHABLE);
                }
+               if (active_cache_tree)
+                       fsck_cache_tree(active_cache_tree);
        }
 
        check_connectivity();