Skip to content

Commit ca23bd2

Browse files
committed
Merge branch 'js/use-sc-open-max'
Introduce get_max_fd_limit() to absorb platforms that do not have getrlimit(RLIMIT_NOFILE) and/or sysconf(_SC_OPEN_MAX). * js/use-sc-open-max: sha1_file.c: introduce get_max_fd_limit() helper
2 parents a795b32 + a078826 commit ca23bd2

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

sha1_file.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,24 @@ void free_pack_by_name(const char *pack_name)
731731
}
732732
}
733733

734+
static unsigned int get_max_fd_limit(void)
735+
{
736+
#ifdef RLIMIT_NOFILE
737+
struct rlimit lim;
738+
739+
if (getrlimit(RLIMIT_NOFILE, &lim))
740+
die_errno("cannot get RLIMIT_NOFILE");
741+
742+
return lim.rlim_cur;
743+
#elif defined(_SC_OPEN_MAX)
744+
return sysconf(_SC_OPEN_MAX);
745+
#elif defined(OPEN_MAX)
746+
return OPEN_MAX;
747+
#else
748+
return 1; /* see the caller ;-) */
749+
#endif
750+
}
751+
734752
/*
735753
* Do not call this directly as this leaks p->pack_fd on error return;
736754
* call open_packed_git() instead.
@@ -747,13 +765,7 @@ static int open_packed_git_1(struct packed_git *p)
747765
return error("packfile %s index unavailable", p->pack_name);
748766

749767
if (!pack_max_fds) {
750-
struct rlimit lim;
751-
unsigned int max_fds;
752-
753-
if (getrlimit(RLIMIT_NOFILE, &lim))
754-
die_errno("cannot get RLIMIT_NOFILE");
755-
756-
max_fds = lim.rlim_cur;
768+
unsigned int max_fds = get_max_fd_limit();
757769

758770
/* Save 3 for stdin/stdout/stderr, 22 for work */
759771
if (25 < max_fds)

0 commit comments

Comments
 (0)