aboutsummaryrefslogtreecommitdiffstats
path: root/compat/open.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2025-04-15 11:38:16 +0200
committerJunio C Hamano <gitster@pobox.com>2025-04-15 08:24:35 -0700
commit97dc141fd676e7079c2fd51e3bea2681a5b9f824 (patch)
treefb8a738d18be7814be0ed6dc2ad07e4e84d3cbfc /compat/open.c
parentobject-file: move `safe_create_leading_directories()` into "path.c" (diff)
downloadgit-97dc141fd676e7079c2fd51e3bea2681a5b9f824.tar.gz
git-97dc141fd676e7079c2fd51e3bea2681a5b9f824.zip
object-file: move `git_open_cloexec()` to "compat/open.c"
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>
Diffstat (limited to 'compat/open.c')
-rw-r--r--compat/open.c29
1 files changed, 29 insertions, 0 deletions
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;
+}