tree.con commit update-cache: remove index lock file on SIGINT (f2a1934)
   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 = malloc(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, 1);
  22}
  23
  24static int read_tree_recursive(void *buffer, unsigned long size,
  25                               const char *base, int baselen, int stage)
  26{
  27        while (size) {
  28                int len = strlen(buffer)+1;
  29                unsigned char *sha1 = buffer + len;
  30                char *path = strchr(buffer, ' ')+1;
  31                unsigned int mode;
  32
  33                if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
  34                        return -1;
  35
  36                buffer = sha1 + 20;
  37                size -= len + 20;
  38
  39                if (S_ISDIR(mode)) {
  40                        int retval;
  41                        int pathlen = strlen(path);
  42                        char *newbase = malloc(baselen + 1 + pathlen);
  43                        void *eltbuf;
  44                        char elttype[20];
  45                        unsigned long eltsize;
  46
  47                        eltbuf = read_sha1_file(sha1, elttype, &eltsize);
  48                        if (!eltbuf || strcmp(elttype, "tree"))
  49                                return -1;
  50                        memcpy(newbase, base, baselen);
  51                        memcpy(newbase + baselen, path, pathlen);
  52                        newbase[baselen + pathlen] = '/';
  53                        retval = read_tree_recursive(eltbuf, eltsize,
  54                                                     newbase,
  55                                                     baselen + pathlen + 1, stage);
  56                        free(eltbuf);
  57                        free(newbase);
  58                        if (retval)
  59                                return -1;
  60                        continue;
  61                }
  62                if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
  63                        return -1;
  64        }
  65        return 0;
  66}
  67
  68int read_tree(void *buffer, unsigned long size, int stage)
  69{
  70        return read_tree_recursive(buffer, size, "", 0, stage);
  71}
  72
  73struct tree *lookup_tree(unsigned char *sha1)
  74{
  75        struct object *obj = lookup_object(sha1);
  76        if (!obj) {
  77                struct tree *ret = malloc(sizeof(struct tree));
  78                memset(ret, 0, sizeof(struct tree));
  79                created_object(sha1, &ret->object);
  80                ret->object.type = tree_type;
  81                return ret;
  82        }
  83        if (obj->type != tree_type) {
  84                error("Object %s is a %s, not a tree", 
  85                      sha1_to_hex(sha1), obj->type);
  86                return NULL;
  87        }
  88        return (struct tree *) obj;
  89}
  90
  91int parse_tree(struct tree *item)
  92{
  93        char type[20];
  94        void *buffer, *bufptr;
  95        unsigned long size;
  96        struct tree_entry_list **list_p;
  97        if (item->object.parsed)
  98                return 0;
  99        item->object.parsed = 1;
 100        buffer = bufptr = read_sha1_file(item->object.sha1, type, &size);
 101        if (!buffer)
 102                return error("Could not read %s",
 103                             sha1_to_hex(item->object.sha1));
 104        if (strcmp(type, tree_type))
 105                return error("Object %s not a tree",
 106                             sha1_to_hex(item->object.sha1));
 107        list_p = &item->entries;
 108        while (size) {
 109                struct object *obj;
 110                struct tree_entry_list *entry;
 111                int len = 1+strlen(bufptr);
 112                unsigned char *file_sha1 = bufptr + len;
 113                char *path = strchr(bufptr, ' ');
 114                unsigned int mode;
 115                if (size < len + 20 || !path || 
 116                    sscanf(bufptr, "%o", &mode) != 1)
 117                        return -1;
 118
 119                entry = malloc(sizeof(struct tree_entry_list));
 120                entry->name = strdup(path + 1);
 121                entry->directory = S_ISDIR(mode);
 122                entry->executable = mode & S_IXUSR;
 123                entry->next = NULL;
 124
 125                /* Warn about trees that don't do the recursive thing.. */
 126                if (strchr(path, '/')) {
 127                        item->has_full_path = 1;
 128                }
 129
 130                bufptr += len + 20;
 131                size -= len + 20;
 132
 133                if (entry->directory) {
 134                        entry->item.tree = lookup_tree(file_sha1);
 135                        obj = &entry->item.tree->object;
 136                } else {
 137                        entry->item.blob = lookup_blob(file_sha1);
 138                        obj = &entry->item.blob->object;
 139                }
 140                if (obj)
 141                        add_ref(&item->object, obj);
 142
 143                *list_p = entry;
 144                list_p = &entry->next;
 145        }
 146        return 0;
 147}