Skip to content

Commit f8a0a22

Browse files
committed
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]>
1 parent dddce2f commit f8a0a22

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

sha1_file.c

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,18 @@ void close_pack_windows(struct packed_git *p)
786786
}
787787
}
788788

789+
static int close_pack_fd(struct packed_git *p)
790+
{
791+
if (p->pack_fd < 0)
792+
return 0;
793+
794+
close(p->pack_fd);
795+
pack_open_fds--;
796+
p->pack_fd = -1;
797+
798+
return 1;
799+
}
800+
789801
/*
790802
* The LRU pack is the one with the oldest MRU window, preferring packs
791803
* with no used windows, or the oldest mtime if it has no windows allocated.
@@ -853,12 +865,8 @@ static int close_one_pack(void)
853865
find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse);
854866
}
855867

856-
if (lru_p) {
857-
close(lru_p->pack_fd);
858-
pack_open_fds--;
859-
lru_p->pack_fd = -1;
860-
return 1;
861-
}
868+
if (lru_p)
869+
return close_pack_fd(lru_p);
862870

863871
return 0;
864872
}
@@ -898,12 +906,7 @@ void free_pack_by_name(const char *pack_name)
898906
p = *pp;
899907
if (strcmp(pack_name, p->pack_name) == 0) {
900908
clear_delta_base_cache();
901-
close_pack_windows(p);
902-
if (p->pack_fd != -1) {
903-
close(p->pack_fd);
904-
pack_open_fds--;
905-
}
906-
close_pack_index(p);
909+
close_pack(p);
907910
free(p->bad_object_sha1);
908911
*pp = p->next;
909912
if (last_found_pack == p)
@@ -1037,11 +1040,7 @@ static int open_packed_git(struct packed_git *p)
10371040
{
10381041
if (!open_packed_git_1(p))
10391042
return 0;
1040-
if (p->pack_fd != -1) {
1041-
close(p->pack_fd);
1042-
pack_open_fds--;
1043-
p->pack_fd = -1;
1044-
}
1043+
close_pack_fd(p);
10451044
return -1;
10461045
}
10471046

@@ -1107,11 +1106,8 @@ unsigned char *use_pack(struct packed_git *p,
11071106
p->pack_name,
11081107
strerror(errno));
11091108
if (!win->offset && win->len == p->pack_size
1110-
&& !p->do_not_close) {
1111-
close(p->pack_fd);
1112-
pack_open_fds--;
1113-
p->pack_fd = -1;
1114-
}
1109+
&& !p->do_not_close)
1110+
close_pack_fd(p);
11151111
pack_mmap_calls++;
11161112
pack_open_windows++;
11171113
if (pack_mapped > peak_pack_mapped)

0 commit comments

Comments
 (0)