]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object-file: move `git_open_cloexec()` to "compat/open.c"
authorPatrick Steinhardt <ps@pks.im>
Tue, 15 Apr 2025 09:38:16 +0000 (11:38 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 15 Apr 2025 15:24:35 +0000 (08:24 -0700)
The `git_open_cloexec()` wrapper function provides the ability to open a
file with `O_CLOEXEC` in a platform-agnostic way. This function is
provided by "object-file.c" even though it is not specific to the object
subsystem at all.

Move the file into "compat/open.c". This file already exists before this
commit, but has only been compiled conditionally depending on whether or
not open(3p) may return EINTR. With this change we now unconditionally
compile the object, but wrap `git_open_with_retry()` in an ifdef.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Makefile
commit-graph.c
compat/open.c
git-compat-util.h
meson.build
midx.c
object-file.c
object-file.h
pack-bitmap.c
pack-mtimes.c
pack-revindex.c

index c41fc41ef0e4b4376030f76c2fc4b6e8074508d4..bb5407b4703798df49d75994be1f2cacc5dd4121 100644 (file)
--- 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
index 3b5bae00af930b2f386ecb56b810347e142b1ccb..9621c4549729577674878fee1b33831fd29fb16f 100644 (file)
@@ -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"
index eb3754a23b8f62d0d3de5489c4431653d098491b..37ae2b1aeb9249a66d939f4c1754ff2f6137fe60 100644 (file)
@@ -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;
+}
index cf733b38acdea071644c0f9181cd69e7416bf89d..9273a8ee0874393e1147d3f2ce9daa191e337006 100644 (file)
@@ -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
index 145d2f7ff9e7ed2cbf410aa4f4031e2f02013489..a55e800b85b903327ca7d6a6b18020ea01fcbf5e 100644 (file)
@@ -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 807fdf72f7b81cbaab07480ff97e257c3a6bf9a6..3d0015f782818c2037b90bc94fb35551f20c2812 100644 (file)
--- 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"
index 6228e1c40f8fc4576b2ac8c009557907802fc7c1..c3e20417f3f71eeef8394f76728becf413818009 100644 (file)
@@ -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.
index 922f2bba8c9156324e73c3c18e1c9a88935ed2d9..353d8a85c3347ef8e1fae80e42dae25ea0941930 100644 (file)
@@ -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.
index 7fd78c634ef1426744e801f86a645eeb6d624830..0dbd7c4ffe161a8ab7871b4eeedc5c2eac12a425 100644 (file)
@@ -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"
index cdf30b8d2b0e809db7ee7e219b39863fad702bd4..bcea28e521dfb3bb329f9b285e3c7e2a17e82586 100644 (file)
@@ -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"
index 038e0c96b1ca5efd4ede3b81138543657812fae1..1ee7b49e206ad44646180e1d6cc9125f0461e292 100644 (file)
@@ -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"