]> git.proxmox.com Git - libgit2.git/blobdiff - src/tree-cache.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / src / tree-cache.c
index 9baa06a9901079ae134858c45e22f47a8c0557d0..04d86fd3691dfcf52b162b4a5dfc91af310c99fb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2012 the libgit2 contributors
+ * Copyright (C) the libgit2 contributors. All rights reserved.
  *
  * This file is part of libgit2, distributed under the GNU GPL v2 with
  * a Linking Exception. For full terms see the included COPYING file.
@@ -7,23 +7,19 @@
 
 #include "tree-cache.h"
 
-static git_tree_cache *find_child(const git_tree_cache *tree, const char *path)
-{
-       size_t i, dirlen;
-       const char *end;
-
-       end = strchr(path, '/');
-       if (end == NULL) {
-               end = strrchr(path, '\0');
-       }
+#include "pool.h"
+#include "tree.h"
 
-       dirlen = end - path;
+static git_tree_cache *find_child(
+       const git_tree_cache *tree, const char *path, const char *end)
+{
+       size_t i, dirlen = end ? (size_t)(end - path) : strlen(path);
 
        for (i = 0; i < tree->children_count; ++i) {
-               const char *childname = tree->children[i]->name;
+               git_tree_cache *child = tree->children[i];
 
-               if (strlen(childname) == dirlen && !memcmp(path, childname, dirlen))
-                       return tree->children[i];
+               if (child->namelen == dirlen && !memcmp(path, child->name, dirlen))
+                       return child;
        }
 
        return NULL;
@@ -36,7 +32,7 @@ void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path)
        if (tree == NULL)
                return;
 
-       tree->entries = -1;
+       tree->entry_count = -1;
 
        while (ptr != NULL) {
                end = strchr(ptr, '/');
@@ -44,11 +40,11 @@ void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path)
                if (end == NULL) /* End of path */
                        break;
 
-               tree = find_child(tree, ptr);
+               tree = find_child(tree, ptr, end);
                if (tree == NULL) /* We don't have that tree */
                        return;
 
-               tree->entries = -1;
+               tree->entry_count = -1;
                ptr = end + 1;
        }
 }
@@ -64,12 +60,11 @@ const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char
        while (1) {
                end = strchr(ptr, '/');
 
-               tree = find_child(tree, ptr);
-               if (tree == NULL) /* Can't find it */
+               tree = find_child(tree, ptr, end);
+               if (tree == NULL) /* Can't find it */
                        return NULL;
-               }
 
-               if (end == NULL || end + 1 == '\0')
+               if (end == NULL || *end + 1 == '\0')
                        return tree;
 
                ptr = end + 1;
@@ -77,71 +72,47 @@ const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char
 }
 
 static int read_tree_internal(git_tree_cache **out,
-               const char **buffer_in, const char *buffer_end, git_tree_cache *parent)
+                             const char **buffer_in, const char *buffer_end,
+                             git_pool *pool)
 {
        git_tree_cache *tree = NULL;
        const char *name_start, *buffer;
        int count;
-       int error = GIT_SUCCESS;
-       size_t name_len;
 
        buffer = name_start = *buffer_in;
 
-       if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL) {
-               error = GIT_EOBJCORRUPTED;
-               goto cleanup;
-       }
-
-       if (++buffer >= buffer_end) {
-               error = GIT_EOBJCORRUPTED;
-               goto cleanup;
-       }
+       if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL)
+               goto corrupted;
 
-       name_len = strlen(name_start);
-       if ((tree = git__malloc(sizeof(git_tree_cache) + name_len + 1)) == NULL)
-               return GIT_ENOMEM;
+       if (++buffer >= buffer_end)
+               goto corrupted;
 
-       memset(tree, 0x0, sizeof(git_tree_cache));
-       tree->parent = parent;
-
-       /* NUL-terminated tree name */
-       memcpy(tree->name, name_start, name_len);
-       tree->name[name_len] = '\0';
+       if (git_tree_cache_new(&tree, name_start, pool) < 0)
+               return -1;
 
        /* Blank-terminated ASCII decimal number of entries in this tree */
-       if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS || count < -1) {
-               error = GIT_EOBJCORRUPTED;
-               goto cleanup;
-       }
+       if (git__strntol32(&count, buffer, buffer_end - buffer, &buffer, 10) < 0)
+               goto corrupted;
 
-       tree->entries = count;
+       tree->entry_count = count;
 
-       if (*buffer != ' ' || ++buffer >= buffer_end) {
-               error = GIT_EOBJCORRUPTED;
-               goto cleanup;
-       }
+       if (*buffer != ' ' || ++buffer >= buffer_end)
+               goto corrupted;
 
         /* Number of children of the tree, newline-terminated */
-       if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS ||
-               count < 0) {
-               error = GIT_EOBJCORRUPTED;
-               goto cleanup;
-       }
+       if (git__strntol32(&count, buffer, buffer_end - buffer, &buffer, 10) < 0 || count < 0)
+               goto corrupted;
 
        tree->children_count = count;
 
-       if (*buffer != '\n' || ++buffer > buffer_end) {
-               error = GIT_EOBJCORRUPTED;
-               goto cleanup;
-       }
+       if (*buffer != '\n' || ++buffer > buffer_end)
+               goto corrupted;
 
        /* The SHA1 is only there if it's not invalidated */
-       if (tree->entries >= 0) {
+       if (tree->entry_count >= 0) {
                /* 160-bit SHA-1 for this tree and it's children */
-               if (buffer + GIT_OID_RAWSZ > buffer_end) {
-                       error = GIT_EOBJCORRUPTED;
-                       goto cleanup;
-               }
+               if (buffer + GIT_OID_RAWSZ > buffer_end)
+                       goto corrupted;
 
                git_oid_fromraw(&tree->oid, (const unsigned char *)buffer);
                buffer += GIT_OID_RAWSZ;
@@ -149,53 +120,158 @@ static int read_tree_internal(git_tree_cache **out,
 
        /* Parse children: */
        if (tree->children_count > 0) {
-               unsigned int i;
-               int err;
+               size_t i, bufsize;
 
-               tree->children = git__malloc(tree->children_count * sizeof(git_tree_cache *));
-               if (tree->children == NULL)
-                       goto cleanup;
+               GIT_ERROR_CHECK_ALLOC_MULTIPLY(&bufsize, tree->children_count, sizeof(git_tree_cache*));
 
-               for (i = 0; i < tree->children_count; ++i) {
-                       err = read_tree_internal(&tree->children[i], &buffer, buffer_end, tree);
+               tree->children = git_pool_malloc(pool, bufsize);
+               GIT_ERROR_CHECK_ALLOC(tree->children);
+
+               memset(tree->children, 0x0, bufsize);
 
-                       if (err < GIT_SUCCESS)
-                               goto cleanup;
+               for (i = 0; i < tree->children_count; ++i) {
+                       if (read_tree_internal(&tree->children[i], &buffer, buffer_end, pool) < 0)
+                               goto corrupted;
                }
        }
 
        *buffer_in = buffer;
        *out = tree;
-       return GIT_SUCCESS;
+       return 0;
 
- cleanup:
-       git_tree_cache_free(tree);
-       return error;
+ corrupted:
+       git_error_set(GIT_ERROR_INDEX, "corrupted TREE extension in index");
+       return -1;
 }
 
-int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size)
+int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size, git_pool *pool)
 {
        const char *buffer_end = buffer + buffer_size;
+
+       if (read_tree_internal(tree, &buffer, buffer_end, pool) < 0)
+               return -1;
+
+       if (buffer < buffer_end) {
+               git_error_set(GIT_ERROR_INDEX, "corrupted TREE extension in index (unexpected trailing data)");
+               return -1;
+       }
+
+       return 0;
+}
+
+static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_pool *pool)
+{
+       git_repository *repo;
+       size_t i, j, nentries, ntrees, alloc_size;
        int error;
 
-       error = read_tree_internal(tree, &buffer, buffer_end, NULL);
+       repo = git_tree_owner(tree);
+
+       git_oid_cpy(&cache->oid, git_tree_id(tree));
+       nentries = git_tree_entrycount(tree);
+
+       /*
+        * We make sure we know how many trees we need to allocate for
+        * so we don't have to realloc and change the pointers for the
+        * parents.
+        */
+       ntrees = 0;
+       for (i = 0; i < nentries; i++) {
+               const git_tree_entry *entry;
+
+               entry = git_tree_entry_byindex(tree, i);
+               if (git_tree_entry_filemode(entry) == GIT_FILEMODE_TREE)
+                       ntrees++;
+       }
+
+       GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_size, ntrees, sizeof(git_tree_cache *));
+
+       cache->children_count = ntrees;
+       cache->children = git_pool_mallocz(pool, alloc_size);
+       GIT_ERROR_CHECK_ALLOC(cache->children);
 
-       if (buffer < buffer_end)
-               return GIT_EOBJCORRUPTED;
+       j = 0;
+       for (i = 0; i < nentries; i++) {
+               const git_tree_entry *entry;
+               git_tree *subtree;
 
-       return error;
+               entry = git_tree_entry_byindex(tree, i);
+               if (git_tree_entry_filemode(entry) != GIT_FILEMODE_TREE) {
+                       cache->entry_count++;
+                       continue;
+               }
+
+               if ((error = git_tree_cache_new(&cache->children[j], git_tree_entry_name(entry), pool)) < 0)
+                       return error;
+
+               if ((error = git_tree_lookup(&subtree, repo, git_tree_entry_id(entry))) < 0)
+                       return error;
+
+               error = read_tree_recursive(cache->children[j], subtree, pool);
+               git_tree_free(subtree);
+               cache->entry_count += cache->children[j]->entry_count;
+               j++;
+
+               if (error < 0)
+                       return error;
+       }
+
+       return 0;
+}
+
+int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_pool *pool)
+{
+       int error;
+       git_tree_cache *cache;
+
+       if ((error = git_tree_cache_new(&cache, "", pool)) < 0)
+               return error;
+
+       if ((error = read_tree_recursive(cache, tree, pool)) < 0)
+               return error;
+
+       *out = cache;
+       return 0;
 }
 
-void git_tree_cache_free(git_tree_cache *tree)
+int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool)
 {
-       unsigned int i;
+       size_t name_len, alloc_size;
+       git_tree_cache *tree;
 
-       if (tree == NULL)
-               return;
+       name_len = strlen(name);
 
-       for (i = 0; i < tree->children_count; ++i)
-               git_tree_cache_free(tree->children[i]);
+       GIT_ERROR_CHECK_ALLOC_ADD3(&alloc_size, sizeof(git_tree_cache), name_len, 1);
+
+       tree = git_pool_malloc(pool, alloc_size);
+       GIT_ERROR_CHECK_ALLOC(tree);
+
+       memset(tree, 0x0, sizeof(git_tree_cache));
+       /* NUL-terminated tree name */
+       tree->namelen = name_len;
+       memcpy(tree->name, name, name_len);
+       tree->name[name_len] = '\0';
+
+       *out = tree;
+       return 0;
+}
+
+static void write_tree(git_buf *out, git_tree_cache *tree)
+{
+       size_t i;
+
+       git_buf_printf(out, "%s%c%"PRIdZ" %"PRIuZ"\n", tree->name, 0, tree->entry_count, tree->children_count);
+
+       if (tree->entry_count != -1)
+               git_buf_put(out, (const char *) &tree->oid, GIT_OID_RAWSZ);
+
+       for (i = 0; i < tree->children_count; i++)
+               write_tree(out, tree->children[i]);
+}
+
+int git_tree_cache_write(git_buf *out, git_tree_cache *tree)
+{
+       write_tree(out, tree);
 
-       git__free(tree->children);
-       git__free(tree);
+       return git_buf_oom(out) ? -1 : 0;
 }