gitweb: Convert project name to UTF-8
[gitweb.git] / sha1_file.c
index eec4f418bcd07db32bbbdb116c0a3b2aa00be438..1526a28095bdb161fd60554a994aefd7cebee27c 100644 (file)
@@ -552,7 +552,11 @@ void unuse_pack(struct pack_window **w_cursor)
        }
 }
 
-static int open_packed_git(struct packed_git *p)
+/*
+ * Do not call this directly as this leaks p->pack_fd on error return;
+ * call open_packed_git() instead.
+ */
+static int open_packed_git_1(struct packed_git *p)
 {
        struct stat st;
        struct pack_header hdr;
@@ -608,6 +612,17 @@ static int open_packed_git(struct packed_git *p)
        return 0;
 }
 
+static int open_packed_git(struct packed_git *p)
+{
+       if (!open_packed_git_1(p))
+               return 0;
+       if (p->pack_fd != -1) {
+               close(p->pack_fd);
+               p->pack_fd = -1;
+       }
+       return -1;
+}
+
 static int in_window(struct pack_window *win, unsigned long offset)
 {
        /* We must promise at least 20 bytes (one hash) after the
@@ -1148,7 +1163,7 @@ static unsigned long unpack_object_header(struct packed_git *p,
 
        /* use_pack() assures us we have [base, base + 20) available
         * as a range that we can look at at.  (Its actually the hash
-        * size that is assurred.)  With our object header encoding
+        * size that is assured.)  With our object header encoding
         * the maximum deflated object size is 2^137, which is just
         * insane, so we know won't exceed what we have been given.
         */
@@ -1408,6 +1423,18 @@ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e, cons
                }
                offset = find_pack_entry_one(sha1, p);
                if (offset) {
+                       /*
+                        * We are about to tell the caller where they can
+                        * locate the requested object.  We better make
+                        * sure the packfile is still here and can be
+                        * accessed before supplying that answer, as
+                        * it may have been deleted since the index
+                        * was loaded!
+                        */
+                       if (p->pack_fd == -1 && open_packed_git(p)) {
+                               error("packfile %s cannot be accessed", p->pack_name);
+                               continue;
+                       }
                        e->offset = offset;
                        e->p = p;
                        hashcpy(e->sha1, sha1);