sha1_file: consolidate code to close a pack's file descriptor

There was a lot of repeated code to close the file descriptor of a given pack. Let's just refactor this code into a single function. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Oct 5, 2015 at 22:30 UTC 71fe5d7fb03c0db6edcae39a0312bae2c014a818
1 file changed +18 -20
sha1_file.c
+18 -20
@@ -753,6 +753,18 @@ void close_pack_windows(struct packed_git *p)
753 }
754 }
755
756 +static int close_pack_fd(struct packed_git *p)
757 +{
758 + if (p->pack_fd < 0)
759 + return 0;
760 +
761 + close(p->pack_fd);
762 + pack_open_fds--;
763 + p->pack_fd = -1;
764 +
765 + return 1;
766 +}
767 +
768 /*
769 * The LRU pack is the one with the oldest MRU window, preferring packs
770 * with no used windows, or the oldest mtime if it has no windows allocated.
@@ -820,12 +832,8 @@ static int close_one_pack(void)
832 find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse);
833 }
834
823 - if (lru_p) {
824 - close(lru_p->pack_fd);
825 - pack_open_fds--;
826 - lru_p->pack_fd = -1;
827 - return 1;
828 - }
835 + if (lru_p)
836 + return close_pack_fd(lru_p);
837
838 return 0;
839 }
@@ -866,10 +874,7 @@ void free_pack_by_name(const char *pack_name)
874 if (strcmp(pack_name, p->pack_name) == 0) {
875 clear_delta_base_cache();
876 close_pack_windows(p);
869 - if (p->pack_fd != -1) {
870 - close(p->pack_fd);
871 - pack_open_fds--;
872 - }
877 + close_pack_fd(p);
878 close_pack_index(p);
879 free(p->bad_object_sha1);
880 *pp = p->next;
@@ -1004,11 +1009,7 @@ static int open_packed_git(struct packed_git *p)
1009 {
1010 if (!open_packed_git_1(p))
1011 return 0;
1007 - if (p->pack_fd != -1) {
1008 - close(p->pack_fd);
1009 - pack_open_fds--;
1010 - p->pack_fd = -1;
1011 - }
1012 + close_pack_fd(p);
1013 return -1;
1014 }
1015
@@ -1074,11 +1075,8 @@ unsigned char *use_pack(struct packed_git *p,
1075 p->pack_name,
1076 strerror(errno));
1077 if (!win->offset && win->len == p->pack_size
1077 - && !p->do_not_close) {
1078 - close(p->pack_fd);
1079 - pack_open_fds--;
1080 - p->pack_fd = -1;
1081 - }
1078 + && !p->do_not_close)
1079 + close_pack_fd(p);
1080 pack_mmap_calls++;
1081 pack_open_windows++;
1082 if (pack_mapped > peak_pack_mapped)