[PATCH] ls-tree for listing trees
[git.git] / update-cache.c
index b39185a..d08e895 100644 (file)
@@ -17,12 +17,16 @@ static int allow_add = 0, allow_remove = 0;
 static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st)
 {
        z_stream stream;
-       int max_out_bytes = namelen + st->st_size + 200;
+       unsigned long size = st->st_size;
+       int max_out_bytes = namelen + size + 200;
        void *out = malloc(max_out_bytes);
        void *metadata = malloc(namelen + 200);
-       void *in = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+       void *in;
        SHA_CTX c;
 
+       in = "";
+       if (size)
+               in = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
        close(fd);
        if (!out || (int)(long)in == -1)
                return -1;
@@ -34,7 +38,7 @@ static int index_fd(const char *path, int namelen, struct cache_entry *ce, int f
         * ASCII size + nul byte
         */     
        stream.next_in = metadata;
-       stream.avail_in = 1+sprintf(metadata, "blob %lu", (unsigned long) st->st_size);
+       stream.avail_in = 1+sprintf(metadata, "blob %lu", size);
        stream.next_out = out;
        stream.avail_out = max_out_bytes;
        while (deflate(&stream, 0) == Z_OK)
@@ -44,7 +48,7 @@ static int index_fd(const char *path, int namelen, struct cache_entry *ce, int f
         * File content
         */
        stream.next_in = in;
-       stream.avail_in = st->st_size;
+       stream.avail_in = size;
        while (deflate(&stream, Z_FINISH) == Z_OK)
                /*nothing */;
 
@@ -205,7 +209,7 @@ static void refresh_cache(void)
 /*
  * We fundamentally don't like some paths: we don't want
  * dot or dot-dot anywhere, and in fact, we don't even want
- * any other dot-files (.dircache or anything else). They
+ * any other dot-files (.git or anything else). They
  * are hidden, for chist sake.
  *
  * Also, we don't want double slashes or slashes at the
@@ -230,22 +234,30 @@ inside:
        }
 }
 
+static int remove_lock = 0;
+
+static void remove_lock_file(void)
+{
+       if (remove_lock)
+               unlink(".git/index.lock");
+}
+
 int main(int argc, char **argv)
 {
        int i, newfd, entries;
        int allow_options = 1;
 
+       newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
+       if (newfd < 0)
+               usage("unable to create new cachefile");
+
+       atexit(remove_lock_file);
+       remove_lock = 1;
+
        entries = read_cache();
-       if (entries < 0) {
-               perror("cache corrupted");
-               return -1;
-       }
+       if (entries < 0)
+               usage("cache corrupted");
 
-       newfd = open(".dircache/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
-       if (newfd < 0) {
-               perror("unable to create new cachefile");
-               return -1;
-       }
        for (i = 1 ; i < argc; i++) {
                char *path = argv[i];
 
@@ -272,14 +284,13 @@ int main(int argc, char **argv)
                        fprintf(stderr, "Ignoring path %s\n", argv[i]);
                        continue;
                }
-               if (add_file_to_cache(path)) {
-                       fprintf(stderr, "Unable to add %s to database\n", path);
-                       goto out;
-               }
+               if (add_file_to_cache(path))
+                       usage("Unable to add %s to database", path);
        }
-       if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
-               return 0;
-out:
-       unlink(".dircache/index.lock");
+       if (write_cache(newfd, active_cache, active_nr) ||
+           rename(".git/index.lock", ".git/index"))
+               usage("Unable to write new cachefile");
+
+       remove_lock = 0;
        return 0;
 }