diff --git a/Makefile b/Makefile index c41fc41ef0..bb5407b470 100644 --- a/Makefile +++ b/Makefile @@ -994,6 +994,7 @@ LIB_OBJS += common-exit.o LIB_OBJS += common-init.o LIB_OBJS += compat/nonblock.o LIB_OBJS += compat/obstack.o +LIB_OBJS += compat/open.o LIB_OBJS += compat/terminal.o LIB_OBJS += compiler-tricks/not-constant.o LIB_OBJS += config.o @@ -1812,7 +1813,6 @@ ifdef FREAD_READS_DIRECTORIES endif ifdef OPEN_RETURNS_EINTR COMPAT_CFLAGS += -DOPEN_RETURNS_EINTR - COMPAT_OBJS += compat/open.o endif ifdef NO_SYMLINK_HEAD BASIC_CFLAGS += -DNO_SYMLINK_HEAD diff --git a/commit-graph.c b/commit-graph.c index 3b5bae00af..9621c45497 100644 --- a/commit-graph.c +++ b/commit-graph.c @@ -13,7 +13,6 @@ #include "refs.h" #include "hash-lookup.h" #include "commit-graph.h" -#include "object-file.h" #include "object-store-ll.h" #include "oid-array.h" #include "path.h" diff --git a/compat/open.c b/compat/open.c index eb3754a23b..37ae2b1aeb 100644 --- a/compat/open.c +++ b/compat/open.c @@ -1,5 +1,6 @@ #include "git-compat-util.h" +#ifdef OPEN_RETURNS_EINTR #undef open int git_open_with_retry(const char *path, int flags, ...) { @@ -23,3 +24,31 @@ int git_open_with_retry(const char *path, int flags, ...) return ret; } +#endif + +int git_open_cloexec(const char *name, int flags) +{ + int fd; + static int o_cloexec = O_CLOEXEC; + + fd = open(name, flags | o_cloexec); + if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) { + /* Try again w/o O_CLOEXEC: the kernel might not support it */ + o_cloexec &= ~O_CLOEXEC; + fd = open(name, flags | o_cloexec); + } + +#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC) + { + static int fd_cloexec = FD_CLOEXEC; + + if (!o_cloexec && 0 <= fd && fd_cloexec) { + /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */ + int flags = fcntl(fd, F_GETFD); + if (fcntl(fd, F_SETFD, flags | fd_cloexec)) + fd_cloexec = 0; + } + } +#endif + return fd; +} diff --git a/git-compat-util.h b/git-compat-util.h index cf733b38ac..9273a8ee08 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1000,6 +1000,9 @@ int git_vsnprintf(char *str, size_t maxsize, int git_open_with_retry(const char *path, int flag, ...); #endif +int git_open_cloexec(const char *name, int flags); +#define git_open(name) git_open_cloexec(name, O_RDONLY) + #ifdef __GLIBC_PREREQ #if __GLIBC_PREREQ(2, 1) #define HAVE_STRCHRNUL diff --git a/meson.build b/meson.build index 145d2f7ff9..a55e800b85 100644 --- a/meson.build +++ b/meson.build @@ -263,6 +263,7 @@ libgit_sources = [ 'common-init.c', 'compat/nonblock.c', 'compat/obstack.c', + 'compat/open.c', 'compat/terminal.c', 'compiler-tricks/not-constant.c', 'config.c', diff --git a/midx.c b/midx.c index 807fdf72f7..3d0015f782 100644 --- a/midx.c +++ b/midx.c @@ -5,7 +5,6 @@ #include "dir.h" #include "hex.h" #include "packfile.h" -#include "object-file.h" #include "hash-lookup.h" #include "midx.h" #include "progress.h" diff --git a/object-file.c b/object-file.c index 6228e1c40f..c3e20417f3 100644 --- a/object-file.c +++ b/object-file.c @@ -833,33 +833,6 @@ int stream_object_signature(struct repository *r, const struct object_id *oid) return !oideq(oid, &real_oid) ? -1 : 0; } -int git_open_cloexec(const char *name, int flags) -{ - int fd; - static int o_cloexec = O_CLOEXEC; - - fd = open(name, flags | o_cloexec); - if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) { - /* Try again w/o O_CLOEXEC: the kernel might not support it */ - o_cloexec &= ~O_CLOEXEC; - fd = open(name, flags | o_cloexec); - } - -#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC) - { - static int fd_cloexec = FD_CLOEXEC; - - if (!o_cloexec && 0 <= fd && fd_cloexec) { - /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */ - int flags = fcntl(fd, F_GETFD); - if (fcntl(fd, F_SETFD, flags | fd_cloexec)) - fd_cloexec = 0; - } - } -#endif - return fd; -} - /* * Find "oid" as a loose object in the local repository or in an alternate. * Returns 0 on success, negative on failure. diff --git a/object-file.h b/object-file.h index 922f2bba8c..353d8a85c3 100644 --- a/object-file.h +++ b/object-file.h @@ -21,9 +21,6 @@ extern int fetch_if_missing; int index_fd(struct index_state *istate, struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags); int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags); -int git_open_cloexec(const char *name, int flags); -#define git_open(name) git_open_cloexec(name, O_RDONLY) - /** * unpack_loose_header() initializes the data stream needed to unpack * a loose object header. diff --git a/pack-bitmap.c b/pack-bitmap.c index 7fd78c634e..0dbd7c4ffe 100644 --- a/pack-bitmap.c +++ b/pack-bitmap.c @@ -17,7 +17,6 @@ #include "packfile.h" #include "repository.h" #include "trace2.h" -#include "object-file.h" #include "object-store-ll.h" #include "list-objects-filter-options.h" #include "midx.h" diff --git a/pack-mtimes.c b/pack-mtimes.c index cdf30b8d2b..bcea28e521 100644 --- a/pack-mtimes.c +++ b/pack-mtimes.c @@ -1,7 +1,6 @@ #include "git-compat-util.h" #include "gettext.h" #include "pack-mtimes.h" -#include "object-file.h" #include "object-store-ll.h" #include "packfile.h" #include "strbuf.h" diff --git a/pack-revindex.c b/pack-revindex.c index 038e0c96b1..1ee7b49e20 100644 --- a/pack-revindex.c +++ b/pack-revindex.c @@ -1,7 +1,6 @@ #include "git-compat-util.h" #include "gettext.h" #include "pack-revindex.h" -#include "object-file.h" #include "object-store-ll.h" #include "packfile.h" #include "strbuf.h"