Skip to content

Commit 71fe5d7

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 11911bf commit 71fe5d7

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

sha1_file.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,18 @@ void close_pack_windows(struct packed_git *p)
753753
}
754754
}
755755

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+
756768
/*
757769
* The LRU pack is the one with the oldest MRU window, preferring packs
758770
* with no used windows, or the oldest mtime if it has no windows allocated.
@@ -820,12 +832,8 @@ static int close_one_pack(void)
820832
find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse);
821833
}
822834

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);
829837

830838
return 0;
831839
}
@@ -866,10 +874,7 @@ void free_pack_by_name(const char *pack_name)
866874
if (strcmp(pack_name, p->pack_name) == 0) {
867875
clear_delta_base_cache();
868876
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);
873878
close_pack_index(p);
874879
free(p->bad_object_sha1);
875880
*pp = p->next;
@@ -1004,11 +1009,7 @@ static int open_packed_git(struct packed_git *p)
10041009
{
10051010
if (!open_packed_git_1(p))
10061011
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);
10121013
return -1;
10131014
}
10141015

@@ -1074,11 +1075,8 @@ unsigned char *use_pack(struct packed_git *p,
10741075
p->pack_name,
10751076
strerror(errno));
10761077
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);
10821080
pack_mmap_calls++;
10831081
pack_open_windows++;
10841082
if (pack_mapped > peak_pack_mapped)

0 commit comments

Comments
 (0)