document --pretty=tformat: option
[gitweb.git] / sha1_file.c
index 9679040d62189304392554d0d3cfd561ed5a7e45..f311c79e50bce4cf39cd1c2c51f63974517a2f5c 100644 (file)
@@ -177,7 +177,7 @@ char *sha1_file_name(const unsigned char *sha1)
 }
 
 static char *sha1_get_pack_name(const unsigned char *sha1,
-                               char **name, char **base)
+                               char **name, char **base, const char *which)
 {
        static const char hex[] = "0123456789abcdef";
        char *buf;
@@ -187,7 +187,8 @@ static char *sha1_get_pack_name(const unsigned char *sha1,
                const char *sha1_file_directory = get_object_directory();
                int len = strlen(sha1_file_directory);
                *base = xmalloc(len + 60);
-               sprintf(*base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
+               sprintf(*base, "%s/pack/pack-1234567890123456789012345678901234567890.%s",
+                       sha1_file_directory, which);
                *name = *base + len + 11;
        }
 
@@ -206,14 +207,14 @@ char *sha1_pack_name(const unsigned char *sha1)
 {
        static char *name, *base;
 
-       return sha1_get_pack_name(sha1, &name, &base);
+       return sha1_get_pack_name(sha1, &name, &base, "pack");
 }
 
 char *sha1_pack_index_name(const unsigned char *sha1)
 {
        static char *name, *base;
 
-       return sha1_get_pack_name(sha1, &name, &base);
+       return sha1_get_pack_name(sha1, &name, &base, "idx");
 }
 
 struct alternate_object_database *alt_odb_list;
@@ -2104,6 +2105,15 @@ int hash_sha1_file(const void *buf, unsigned long len, const char *type,
        return 0;
 }
 
+/* Finalize a file on disk, and close it. */
+static void close_sha1_file(int fd)
+{
+       /* For safe-mode, we could fsync_or_die(fd, "sha1 file") here */
+       fchmod(fd, 0444);
+       if (close(fd) != 0)
+               die("unable to write sha1 file");
+}
+
 static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
                              void *buf, unsigned long len, time_t mtime)
 {
@@ -2169,9 +2179,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
 
        if (write_buffer(fd, compressed, size) < 0)
                die("unable to write sha1 file");
-       fchmod(fd, 0444);
-       if (close(fd))
-               die("unable to write sha1 file");
+       close_sha1_file(fd);
        free(compressed);
 
        if (mtime) {
@@ -2349,9 +2357,7 @@ int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
        } while (1);
        inflateEnd(&stream);
 
-       fchmod(local, 0444);
-       if (close(local) != 0)
-               die("unable to write sha1 file");
+       close_sha1_file(local);
        SHA1_Final(real_sha1, &c);
        if (ret != Z_STREAM_END) {
                unlink(tmpfile);