git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 03/10] object-file: move `git_open_cloexec()` to "compat/open.c"

From
Patrick Steinhardt <ps@pks.im>
Date
Apr 15, 2025, 09:38 UTC
Message-ID
<20250415-pks-split-object-file-v3-3-6aa7db7ad7b0@pks.im>
In-Reply-To
<20250415-pks-split-object-file-v3-0-6aa7db7ad7b0@pks.im>

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>
---
 Makefile          |  2 +-
 commit-graph.c    |  1 -
 compat/open.c     | 29 +++++++++++++++++++++++++++++
 git-compat-util.h |  3 +++
 meson.build       |  1 +
 midx.c            |  1 -
 object-file.c     | 27 ---------------------------
 object-file.h     |  3 ---
 pack-bitmap.c     |  1 -
 pack-mtimes.c     |  1 -
 pack-revindex.c   |  1 -
 11 files changed, 34 insertions(+), 36 deletions(-)
diff --git a/Makefile b/Makefile
index c41fc41ef0e..bb5407b4703 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 3b5bae00af9..9621c454972 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 eb3754a23b8..37ae2b1aeb9 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 cf733b38acd..9273a8ee087 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 145d2f7ff9e..a55e800b85b 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 807fdf72f7b..3d0015f7828 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 6228e1c40f8..c3e20417f3f 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 922f2bba8c9..353d8a85c33 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 7fd78c634ef..0dbd7c4ffe1 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 cdf30b8d2b0..bcea28e521d 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 038e0c96b1c..1ee7b49e206 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"
-- 
2.49.0.805.g082f7c87e0.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 43 of 53 in “Split up "object-file.c"”
  1. 0/9 Split up "object-file.c"Patrick Steinhardt, Apr 8, 2025
  2. 1/9 object-file: move `safe_create_leading_directories()` into "dir.c"Patrick Steinhardt, Apr 8, 2025
  3. Elijah NewrenApr 9, 2025
  4. Patrick SteinhardtApr 11, 2025
  5. Elijah NewrenApr 11, 2025
  6. Patrick SteinhardtApr 15, 2025
  7. Junio C HamanoApr 15, 2025
  8. 2/9 object-file: move `git_open_cloexec()` to "compat/open.c"Patrick Steinhardt, Apr 8, 2025
  9. 3/9 object-file: move `xmmap()` into "wrapper.c"Patrick Steinhardt, Apr 8, 2025
  10. Elijah NewrenApr 9, 2025
  11. 4/9 object-file: split out functions relating to object store subsystemPatrick Steinhardt, Apr 8, 2025
  12. 5/9 object-file: split up concerns of `HASH_*` flagsPatrick Steinhardt, Apr 8, 2025
  13. 6/9 object-file: split out functions relating to index subsystemPatrick Steinhardt, Apr 8, 2025
  14. 7/9 object: split out functions relating to object store subsystemPatrick Steinhardt, Apr 8, 2025
  15. 8/9 object-store: remove global array of cached objectsPatrick Steinhardt, Apr 8, 2025
  16. 9/9 object-store: merge "object-store-ll.h" and "object-store.h"Patrick Steinhardt, Apr 8, 2025
  17. Junio C HamanoApr 8, 2025
  18. Patrick SteinhardtApr 11, 2025
  19. Elijah NewrenApr 9, 2025
  20. Patrick SteinhardtApr 11, 2025
  21. 0/9 Split up "object-file.c"Patrick Steinhardt, Apr 11, 2025
  22. 1/9 object-file: move `safe_create_leading_directories()` into "dir.c"Patrick Steinhardt, Apr 11, 2025
  23. Junio C HamanoApr 11, 2025
  24. Eric SunshineApr 11, 2025
  25. Patrick SteinhardtApr 15, 2025
  26. Junio C HamanoApr 15, 2025
  27. 2/9 object-file: move `git_open_cloexec()` to "compat/open.c"Patrick Steinhardt, Apr 11, 2025
  28. 3/9 object-file: move `xmmap()` into "wrapper.c"Patrick Steinhardt, Apr 11, 2025
  29. 4/9 object-file: split out functions relating to object store subsystemPatrick Steinhardt, Apr 11, 2025
  30. 5/9 object-file: split up concerns of `HASH_*` flagsPatrick Steinhardt, Apr 11, 2025
  31. 6/9 object-file: split out functions relating to index subsystemPatrick Steinhardt, Apr 11, 2025
  32. Jeff KingApr 12, 2025
  33. Junio C HamanoApr 14, 2025
  34. Patrick SteinhardtApr 15, 2025
  35. 7/9 object: split out functions relating to object store subsystemPatrick Steinhardt, Apr 11, 2025
  36. 8/9 object-store: remove global array of cached objectsPatrick Steinhardt, Apr 11, 2025
  37. Junio C HamanoApr 11, 2025
  38. Patrick SteinhardtApr 15, 2025
  39. 9/9 object-store: merge "object-store-ll.h" and "object-store.h"Patrick Steinhardt, Apr 11, 2025
  40. 00/10 Split up "object-file.c"Patrick Steinhardt, Apr 15, 2025
  41. 01/10 object-file: move `mkdir_in_gitdir()` into "path.c"Patrick Steinhardt, Apr 15, 2025
  42. 02/10 object-file: move `safe_create_leading_directories()` into "path.c"Patrick Steinhardt, Apr 15, 2025
  43. 03/10 object-file: move `git_open_cloexec()` to "compat/open.c"Patrick Steinhardt, Apr 15, 2025
  44. 04/10 object-file: move `xmmap()` into "wrapper.c"Patrick Steinhardt, Apr 15, 2025
  45. 05/10 object-file: split out functions relating to object store subsystemPatrick Steinhardt, Apr 15, 2025
  46. 06/10 object-file: split up concerns of `HASH_*` flagsPatrick Steinhardt, Apr 15, 2025
  47. 07/10 object-file: drop `index_blob_stream()`Patrick Steinhardt, Apr 15, 2025
  48. 08/10 object: split out functions relating to object store subsystemPatrick Steinhardt, Apr 15, 2025
  49. 09/10 object-store: remove global array of cached objectsPatrick Steinhardt, Apr 15, 2025
  50. 10/10 object-store: merge "object-store-ll.h" and "object-store.h"Patrick Steinhardt, Apr 15, 2025
  51. Elijah NewrenApr 16, 2025
  52. Patrick SteinhardtApr 16, 2025
  53. Junio C HamanoApr 16, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.