8dc27e3b7354b8b8cc7d7491621929c78325a645
   1#include "tree.h"
   2#include "blob.h"
   3#include "cache.h"
   4#include <stdlib.h>
   5
   6const char *tree_type = "tree";
   7
   8static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
   9{
  10        int len = strlen(pathname);
  11        unsigned int size = cache_entry_size(baselen + len);
  12        struct cache_entry *ce = xmalloc(size);
  13
  14        memset(ce, 0, size);
  15
  16        ce->ce_mode = create_ce_mode(mode);
  17        ce->ce_flags = create_ce_flags(baselen + len, stage);
  18        memcpy(ce->name, base, baselen);
  19        memcpy(ce->name + baselen, pathname, len+1);
  20        memcpy(ce->sha1, sha1, 20);
  21        return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
  22}
  23
  24static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, char **paths)
  25{
  26        char *match;
  27        int pathlen;
  28
  29        if (!paths)
  30                return 1;
  31        pathlen = strlen(path);
  32        while ((match = *paths++) != NULL) {
  33                int matchlen = strlen(match);
  34
  35                if (baselen >= matchlen) {
  36                        /* If it doesn't match, move along... */
  37                        if (strncmp(base, match, matchlen))
  38                                continue;
  39                        /* The base is a subdirectory of a path which was specified. */
  40                        return 1;
  41                }
  42
  43                /* Does the base match? */
  44                if (strncmp(base, match, baselen))
  45                        continue;
  46
  47                match += baselen;
  48                matchlen -= baselen;
  49
  50                if (pathlen > matchlen)
  51                        continue;
  52
  53                if (matchlen > pathlen) {
  54                        if (match[pathlen] != '/')
  55                                continue;
  56                        if (!S_ISDIR(mode))
  57                                continue;
  58                }
  59
  60                if (strncmp(path, match, pathlen))
  61                        continue;
  62        }
  63        return 0;
  64}
  65
  66static int read_tree_recursive(void *buffer, unsigned long size,
  67                               const char *base, int baselen,
  68                               int stage, char **match)
  69{
  70        while (size) {
  71                int len = strlen(buffer)+1;
  72                unsigned char *sha1 = buffer + len;
  73                char *path = strchr(buffer, ' ')+1;
  74                unsigned int mode;
  75
  76                if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
  77                        return -1;
  78
  79                buffer = sha1 + 20;
  80                size -= len + 20;
  81
  82                if (!match_tree_entry(base, baselen, path, mode, match))
  83                        continue;
  84
  85                if (S_ISDIR(mode)) {
  86                        int retval;
  87                        int pathlen = strlen(path);
  88                        char *newbase;
  89                        void *eltbuf;
  90                        char elttype[20];
  91                        unsigned long eltsize;
  92
  93                        eltbuf = read_sha1_file(sha1, elttype, &eltsize);
  94                        if (!eltbuf || strcmp(elttype, "tree")) {
  95                                if (eltbuf) free(eltbuf);
  96                                return -1;
  97                        }
  98                        newbase = xmalloc(baselen + 1 + pathlen);
  99                        memcpy(newbase, base, baselen);
 100                        memcpy(newbase + baselen, path, pathlen);
 101                        newbase[baselen + pathlen] = '/';
 102                        retval = read_tree_recursive(eltbuf, eltsize,
 103                                                     newbase,
 104                                                     baselen + pathlen + 1,
 105                                                     stage, match);
 106                        free(eltbuf);
 107                        free(newbase);
 108                        if (retval)
 109                                return -1;
 110                        continue;
 111                }
 112                if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
 113                        return -1;
 114        }
 115        return 0;
 116}
 117
 118int read_tree(void *buffer, unsigned long size, int stage, char **match)
 119{
 120        return read_tree_recursive(buffer, size, "", 0, stage, match);
 121}
 122
 123struct tree *lookup_tree(const unsigned char *sha1)
 124{
 125        struct object *obj = lookup_object(sha1);
 126        if (!obj) {
 127                struct tree *ret = xmalloc(sizeof(struct tree));
 128                memset(ret, 0, sizeof(struct tree));
 129                created_object(sha1, &ret->object);
 130                ret->object.type = tree_type;
 131                return ret;
 132        }
 133        if (!obj->type)
 134                obj->type = tree_type;
 135        if (obj->type != tree_type) {
 136                error("Object %s is a %s, not a tree", 
 137                      sha1_to_hex(sha1), obj->type);
 138                return NULL;
 139        }
 140        return (struct tree *) obj;
 141}
 142
 143int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
 144{
 145        void *bufptr = buffer;
 146        struct tree_entry_list **list_p;
 147
 148        if (item->object.parsed)
 149                return 0;
 150        item->object.parsed = 1;
 151        list_p = &item->entries;
 152        while (size) {
 153                struct object *obj;
 154                struct tree_entry_list *entry;
 155                int len = 1+strlen(bufptr);
 156                unsigned char *file_sha1 = bufptr + len;
 157                char *path = strchr(bufptr, ' ');
 158                unsigned int mode;
 159                if (size < len + 20 || !path || 
 160                    sscanf(bufptr, "%o", &mode) != 1)
 161                        return -1;
 162
 163                entry = xmalloc(sizeof(struct tree_entry_list));
 164                entry->name = strdup(path + 1);
 165                entry->directory = S_ISDIR(mode) != 0;
 166                entry->executable = (mode & S_IXUSR) != 0;
 167                entry->symlink = S_ISLNK(mode) != 0;
 168                entry->mode = mode;
 169                entry->next = NULL;
 170
 171                bufptr += len + 20;
 172                size -= len + 20;
 173
 174                if (entry->directory) {
 175                        entry->item.tree = lookup_tree(file_sha1);
 176                        obj = &entry->item.tree->object;
 177                } else {
 178                        entry->item.blob = lookup_blob(file_sha1);
 179                        obj = &entry->item.blob->object;
 180                }
 181                if (obj)
 182                        add_ref(&item->object, obj);
 183                entry->parent = NULL; /* needs to be filled by the user */
 184                *list_p = entry;
 185                list_p = &entry->next;
 186        }
 187        return 0;
 188}
 189
 190int parse_tree(struct tree *item)
 191{
 192         char type[20];
 193         void *buffer;
 194         unsigned long size;
 195         int ret;
 196
 197        if (item->object.parsed)
 198                return 0;
 199        buffer = read_sha1_file(item->object.sha1, type, &size);
 200        if (!buffer)
 201                return error("Could not read %s",
 202                             sha1_to_hex(item->object.sha1));
 203        if (strcmp(type, tree_type)) {
 204                free(buffer);
 205                return error("Object %s not a tree",
 206                             sha1_to_hex(item->object.sha1));
 207        }
 208        ret = parse_tree_buffer(item, buffer, size);
 209        free(buffer);
 210        return ret;
 211}