fsck.con commit http: prompt for credentials on failed POST (b81401c)
   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"
   9
  10static int fsck_walk_tree(struct tree *tree, fsck_walk_func walk, void *data)
  11{
  12        struct tree_desc desc;
  13        struct name_entry entry;
  14        int res = 0;
  15
  16        if (parse_tree(tree))
  17                return -1;
  18
  19        init_tree_desc(&desc, tree->buffer, tree->size);
  20        while (tree_entry(&desc, &entry)) {
  21                int result;
  22
  23                if (S_ISGITLINK(entry.mode))
  24                        continue;
  25                if (S_ISDIR(entry.mode))
  26                        result = walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data);
  27                else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
  28                        result = walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data);
  29                else {
  30                        result = error("in tree %s: entry %s has bad mode %.6o\n",
  31                                        sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
  32                }
  33                if (result < 0)
  34                        return result;
  35                if (!res)
  36                        res = result;
  37        }
  38        return res;
  39}
  40
  41static int fsck_walk_commit(struct commit *commit, fsck_walk_func walk, void *data)
  42{
  43        struct commit_list *parents;
  44        int res;
  45        int result;
  46
  47        if (parse_commit(commit))
  48                return -1;
  49
  50        result = walk((struct object *)commit->tree, OBJ_TREE, data);
  51        if (result < 0)
  52                return result;
  53        res = result;
  54
  55        parents = commit->parents;
  56        while (parents) {
  57                result = walk((struct object *)parents->item, OBJ_COMMIT, data);
  58                if (result < 0)
  59                        return result;
  60                if (!res)
  61                        res = result;
  62                parents = parents->next;
  63        }
  64        return res;
  65}
  66
  67static int fsck_walk_tag(struct tag *tag, fsck_walk_func walk, void *data)
  68{
  69        if (parse_tag(tag))
  70                return -1;
  71        return walk(tag->tagged, OBJ_ANY, data);
  72}
  73
  74int fsck_walk(struct object *obj, fsck_walk_func walk, void *data)
  75{
  76        if (!obj)
  77                return -1;
  78        switch (obj->type) {
  79        case OBJ_BLOB:
  80                return 0;
  81        case OBJ_TREE:
  82                return fsck_walk_tree((struct tree *)obj, walk, data);
  83        case OBJ_COMMIT:
  84                return fsck_walk_commit((struct commit *)obj, walk, data);
  85        case OBJ_TAG:
  86                return fsck_walk_tag((struct tag *)obj, walk, data);
  87        default:
  88                error("Unknown object type for %s", sha1_to_hex(obj->sha1));
  89                return -1;
  90        }
  91}
  92
  93/*
  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)
 103
 104static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
 105{
 106        int len1 = strlen(name1);
 107        int len2 = strlen(name2);
 108        int len = len1 < len2 ? len1 : len2;
 109        unsigned char c1, c2;
 110        int cmp;
 111
 112        cmp = memcmp(name1, name2, len);
 113        if (cmp < 0)
 114                return 0;
 115        if (cmp > 0)
 116                return TREE_UNORDERED;
 117
 118        /*
 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         */
 123        c1 = name1[len];
 124        c2 = name2[len];
 125        if (!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                 */
 131                return TREE_HAS_DUPS;
 132        if (!c1 && S_ISDIR(mode1))
 133                c1 = '/';
 134        if (!c2 && S_ISDIR(mode2))
 135                c2 = '/';
 136        return c1 < c2 ? 0 : TREE_UNORDERED;
 137}
 138
 139static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
 140{
 141        int retval;
 142        int has_full_path = 0;
 143        int has_empty_name = 0;
 144        int has_zero_pad = 0;
 145        int has_bad_modes = 0;
 146        int has_dup_entries = 0;
 147        int not_properly_sorted = 0;
 148        struct tree_desc desc;
 149        unsigned o_mode;
 150        const char *o_name;
 151
 152        init_tree_desc(&desc, item->buffer, item->size);
 153
 154        o_mode = 0;
 155        o_name = NULL;
 156
 157        while (desc.size) {
 158                unsigned mode;
 159                const char *name;
 160
 161                tree_entry_extract(&desc, &name, &mode);
 162
 163                if (strchr(name, '/'))
 164                        has_full_path = 1;
 165                if (!*name)
 166                        has_empty_name = 1;
 167                has_zero_pad |= *(char *)desc.buffer == '0';
 168                update_tree_entry(&desc);
 169
 170                switch (mode) {
 171                /*
 172                 * Standard modes..
 173                 */
 174                case S_IFREG | 0755:
 175                case S_IFREG | 0644:
 176                case S_IFLNK:
 177                case S_IFDIR:
 178                case S_IFGITLINK:
 179                        break;
 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                 */
 185                case S_IFREG | 0664:
 186                        if (!strict)
 187                                break;
 188                default:
 189                        has_bad_modes = 1;
 190                }
 191
 192                if (o_name) {
 193                        switch (verify_ordered(o_mode, o_name, mode, name)) {
 194                        case TREE_UNORDERED:
 195                                not_properly_sorted = 1;
 196                                break;
 197                        case TREE_HAS_DUPS:
 198                                has_dup_entries = 1;
 199                                break;
 200                        default:
 201                                break;
 202                        }
 203                }
 204
 205                o_mode = mode;
 206                o_name = name;
 207        }
 208
 209        retval = 0;
 210        if (has_full_path)
 211                retval += error_func(&item->object, FSCK_WARN, "contains full pathnames");
 212        if (has_empty_name)
 213                retval += error_func(&item->object, FSCK_WARN, "contains empty pathname");
 214        if (has_zero_pad)
 215                retval += error_func(&item->object, FSCK_WARN, "contains zero-padded file modes");
 216        if (has_bad_modes)
 217                retval += error_func(&item->object, FSCK_WARN, "contains bad file modes");
 218        if (has_dup_entries)
 219                retval += error_func(&item->object, FSCK_ERROR, "contains duplicate file entries");
 220        if (not_properly_sorted)
 221                retval += error_func(&item->object, FSCK_ERROR, "not properly sorted");
 222        return retval;
 223}
 224
 225static int fsck_ident(char **ident, struct object *obj, fsck_error error_func)
 226{
 227        if (**ident == '<')
 228                return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email");
 229        *ident += strcspn(*ident, "<>\n");
 230        if (**ident == '>')
 231                return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad name");
 232        if (**ident != '<')
 233                return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing email");
 234        if ((*ident)[-1] != ' ')
 235                return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email");
 236        (*ident)++;
 237        *ident += strcspn(*ident, "<>\n");
 238        if (**ident != '>')
 239                return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad email");
 240        (*ident)++;
 241        if (**ident != ' ')
 242                return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before date");
 243        (*ident)++;
 244        if (**ident == '0' && (*ident)[1] != ' ')
 245                return error_func(obj, FSCK_ERROR, "invalid author/committer line - zero-padded date");
 246        *ident += strspn(*ident, "0123456789");
 247        if (**ident != ' ')
 248                return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad date");
 249        (*ident)++;
 250        if ((**ident != '+' && **ident != '-') ||
 251            !isdigit((*ident)[1]) ||
 252            !isdigit((*ident)[2]) ||
 253            !isdigit((*ident)[3]) ||
 254            !isdigit((*ident)[4]) ||
 255            ((*ident)[5] != '\n'))
 256                return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad time zone");
 257        (*ident) += 6;
 258        return 0;
 259}
 260
 261static int fsck_commit(struct commit *commit, fsck_error error_func)
 262{
 263        char *buffer = commit->buffer;
 264        unsigned char tree_sha1[20], sha1[20];
 265        struct commit_graft *graft;
 266        int parents = 0;
 267        int err;
 268
 269        if (commit->date == ULONG_MAX)
 270                return error_func(&commit->object, FSCK_ERROR, "invalid author/committer line");
 271
 272        if (memcmp(buffer, "tree ", 5))
 273                return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line");
 274        if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
 275                return error_func(&commit->object, FSCK_ERROR, "invalid 'tree' line format - bad sha1");
 276        buffer += 46;
 277        while (!memcmp(buffer, "parent ", 7)) {
 278                if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
 279                        return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1");
 280                buffer += 48;
 281                parents++;
 282        }
 283        graft = lookup_commit_graft(commit->object.sha1);
 284        if (graft) {
 285                struct commit_list *p = commit->parents;
 286                parents = 0;
 287                while (p) {
 288                        p = p->next;
 289                        parents++;
 290                }
 291                if (graft->nr_parent == -1 && !parents)
 292                        ; /* shallow commit */
 293                else if (graft->nr_parent != parents)
 294                        return error_func(&commit->object, FSCK_ERROR, "graft objects missing");
 295        } else {
 296                struct commit_list *p = commit->parents;
 297                while (p && parents) {
 298                        p = p->next;
 299                        parents--;
 300                }
 301                if (p || parents)
 302                        return error_func(&commit->object, FSCK_ERROR, "parent objects missing");
 303        }
 304        if (memcmp(buffer, "author ", 7))
 305                return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'author' line");
 306        buffer += 7;
 307        err = fsck_ident(&buffer, &commit->object, error_func);
 308        if (err)
 309                return err;
 310        if (memcmp(buffer, "committer ", strlen("committer ")))
 311                return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'committer' line");
 312        buffer += strlen("committer ");
 313        err = fsck_ident(&buffer, &commit->object, error_func);
 314        if (err)
 315                return err;
 316        if (!commit->tree)
 317                return error_func(&commit->object, FSCK_ERROR, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
 318
 319        return 0;
 320}
 321
 322static int fsck_tag(struct tag *tag, fsck_error error_func)
 323{
 324        struct object *tagged = tag->tagged;
 325
 326        if (!tagged)
 327                return error_func(&tag->object, FSCK_ERROR, "could not load tagged object");
 328        return 0;
 329}
 330
 331int fsck_object(struct object *obj, int strict, fsck_error error_func)
 332{
 333        if (!obj)
 334                return error_func(obj, FSCK_ERROR, "no valid object to fsck");
 335
 336        if (obj->type == OBJ_BLOB)
 337                return 0;
 338        if (obj->type == OBJ_TREE)
 339                return fsck_tree((struct tree *) obj, strict, error_func);
 340        if (obj->type == OBJ_COMMIT)
 341                return fsck_commit((struct commit *) obj, error_func);
 342        if (obj->type == OBJ_TAG)
 343                return fsck_tag((struct tag *) obj, error_func);
 344
 345        return error_func(obj, FSCK_ERROR, "unknown type '%d' (internal fsck error)",
 346                          obj->type);
 347}
 348
 349int fsck_error_function(struct object *obj, int type, const char *fmt, ...)
 350{
 351        va_list ap;
 352        struct strbuf sb = STRBUF_INIT;
 353
 354        strbuf_addf(&sb, "object %s:", sha1_to_hex(obj->sha1));
 355
 356        va_start(ap, fmt);
 357        strbuf_vaddf(&sb, fmt, ap);
 358        va_end(ap);
 359
 360        error("%s", sb.buf);
 361        strbuf_release(&sb);
 362        return 1;
 363}