builtin/merge.c: drop a parameter that is never used
[gitweb.git] / sha1_file.c
index f1b649742f0d363c252c47f66f6c7083647f48e6..06c809aeeb3a608c09247b4f2146eb246938d6bf 100644 (file)
@@ -807,15 +807,38 @@ void free_pack_by_name(const char *pack_name)
 static unsigned int get_max_fd_limit(void)
 {
 #ifdef RLIMIT_NOFILE
-       struct rlimit lim;
+       {
+               struct rlimit lim;
 
-       if (getrlimit(RLIMIT_NOFILE, &lim))
-               die_errno("cannot get RLIMIT_NOFILE");
+               if (!getrlimit(RLIMIT_NOFILE, &lim))
+                       return lim.rlim_cur;
+       }
+#endif
+
+#ifdef _SC_OPEN_MAX
+       {
+               long open_max = sysconf(_SC_OPEN_MAX);
+               if (0 < open_max)
+                       return open_max;
+               /*
+                * Otherwise, we got -1 for one of the two
+                * reasons:
+                *
+                * (1) sysconf() did not understand _SC_OPEN_MAX
+                *     and signaled an error with -1; or
+                * (2) sysconf() said there is no limit.
+                *
+                * We _could_ clear errno before calling sysconf() to
+                * tell these two cases apart and return a huge number
+                * in the latter case to let the caller cap it to a
+                * value that is not so selfish, but letting the
+                * fallback OPEN_MAX codepath take care of these cases
+                * is a lot simpler.
+                */
+       }
+#endif
 
-       return lim.rlim_cur;
-#elif defined(_SC_OPEN_MAX)
-       return sysconf(_SC_OPEN_MAX);
-#elif defined(OPEN_MAX)
+#ifdef OPEN_MAX
        return OPEN_MAX;
 #else
        return 1; /* see the caller ;-) */
@@ -2126,6 +2149,16 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,
                int i;
                struct delta_base_cache_entry *ent;
 
+               ent = get_delta_base_cache_entry(p, curpos);
+               if (eq_delta_base_cache_entry(ent, p, curpos)) {
+                       type = ent->type;
+                       data = ent->data;
+                       size = ent->size;
+                       clear_delta_base_cache_entry(ent);
+                       base_from_cache = 1;
+                       break;
+               }
+
                if (do_check_packed_object_crc && p->index_version > 1) {
                        struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
                        unsigned long len = revidx[1].offset - obj_offset;
@@ -2140,16 +2173,6 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,
                        }
                }
 
-               ent = get_delta_base_cache_entry(p, curpos);
-               if (eq_delta_base_cache_entry(ent, p, curpos)) {
-                       type = ent->type;
-                       data = ent->data;
-                       size = ent->size;
-                       clear_delta_base_cache_entry(ent);
-                       base_from_cache = 1;
-                       break;
-               }
-
                type = unpack_object_header(p, &w_curs, &curpos, &size);
                if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA)
                        break;
@@ -2483,15 +2506,18 @@ static int sha1_loose_object_info(const unsigned char *sha1,
 
        /*
         * If we don't care about type or size, then we don't
-        * need to look inside the object at all.
+        * need to look inside the object at all. Note that we
+        * do not optimize out the stat call, even if the
+        * caller doesn't care about the disk-size, since our
+        * return value implicitly indicates whether the
+        * object even exists.
         */
        if (!oi->typep && !oi->sizep) {
-               if (oi->disk_sizep) {
-                       struct stat st;
-                       if (stat_sha1_file(sha1, &st) < 0)
-                               return -1;
+               struct stat st;
+               if (stat_sha1_file(sha1, &st) < 0)
+                       return -1;
+               if (oi->disk_sizep)
                        *oi->disk_sizep = st.st_size;
-               }
                return 0;
        }
 
@@ -2514,7 +2540,6 @@ static int sha1_loose_object_info(const unsigned char *sha1,
        return 0;
 }
 
-/* returns enum object_type or negative */
 int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi)
 {
        struct cached_object *co;
@@ -2563,6 +2588,7 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi)
        return 0;
 }
 
+/* returns enum object_type or negative */
 int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
 {
        enum object_type type;
@@ -2857,7 +2883,9 @@ static int create_tmpfile(char *buffer, size_t bufsiz, const char *filename)
                /* Make sure the directory exists */
                memcpy(buffer, filename, dirlen);
                buffer[dirlen-1] = 0;
-               if (mkdir(buffer, 0777) || adjust_shared_perm(buffer))
+               if (mkdir(buffer, 0777) && errno != EEXIST)
+                       return -1;
+               if (adjust_shared_perm(buffer))
                        return -1;
 
                /* Try again */