Skip to content

Commit 97dc141

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1a99fe8 commit 97dc141

File tree

11 files changed

+34
-36
lines changed

11 files changed

+34
-36
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,7 @@ LIB_OBJS += common-exit.o
994994
LIB_OBJS += common-init.o
995995
LIB_OBJS += compat/nonblock.o
996996
LIB_OBJS += compat/obstack.o
997+
LIB_OBJS += compat/open.o
997998
LIB_OBJS += compat/terminal.o
998999
LIB_OBJS += compiler-tricks/not-constant.o
9991000
LIB_OBJS += config.o
@@ -1812,7 +1813,6 @@ ifdef FREAD_READS_DIRECTORIES
18121813
endif
18131814
ifdef OPEN_RETURNS_EINTR
18141815
COMPAT_CFLAGS += -DOPEN_RETURNS_EINTR
1815-
COMPAT_OBJS += compat/open.o
18161816
endif
18171817
ifdef NO_SYMLINK_HEAD
18181818
BASIC_CFLAGS += -DNO_SYMLINK_HEAD

commit-graph.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "refs.h"
1414
#include "hash-lookup.h"
1515
#include "commit-graph.h"
16-
#include "object-file.h"
1716
#include "object-store-ll.h"
1817
#include "oid-array.h"
1918
#include "path.h"

compat/open.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "git-compat-util.h"
22

3+
#ifdef OPEN_RETURNS_EINTR
34
#undef open
45
int git_open_with_retry(const char *path, int flags, ...)
56
{
@@ -23,3 +24,31 @@ int git_open_with_retry(const char *path, int flags, ...)
2324

2425
return ret;
2526
}
27+
#endif
28+
29+
int git_open_cloexec(const char *name, int flags)
30+
{
31+
int fd;
32+
static int o_cloexec = O_CLOEXEC;
33+
34+
fd = open(name, flags | o_cloexec);
35+
if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
36+
/* Try again w/o O_CLOEXEC: the kernel might not support it */
37+
o_cloexec &= ~O_CLOEXEC;
38+
fd = open(name, flags | o_cloexec);
39+
}
40+
41+
#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
42+
{
43+
static int fd_cloexec = FD_CLOEXEC;
44+
45+
if (!o_cloexec && 0 <= fd && fd_cloexec) {
46+
/* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
47+
int flags = fcntl(fd, F_GETFD);
48+
if (fcntl(fd, F_SETFD, flags | fd_cloexec))
49+
fd_cloexec = 0;
50+
}
51+
}
52+
#endif
53+
return fd;
54+
}

git-compat-util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,9 @@ int git_vsnprintf(char *str, size_t maxsize,
10001000
int git_open_with_retry(const char *path, int flag, ...);
10011001
#endif
10021002

1003+
int git_open_cloexec(const char *name, int flags);
1004+
#define git_open(name) git_open_cloexec(name, O_RDONLY)
1005+
10031006
#ifdef __GLIBC_PREREQ
10041007
#if __GLIBC_PREREQ(2, 1)
10051008
#define HAVE_STRCHRNUL

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ libgit_sources = [
263263
'common-init.c',
264264
'compat/nonblock.c',
265265
'compat/obstack.c',
266+
'compat/open.c',
266267
'compat/terminal.c',
267268
'compiler-tricks/not-constant.c',
268269
'config.c',

midx.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "dir.h"
66
#include "hex.h"
77
#include "packfile.h"
8-
#include "object-file.h"
98
#include "hash-lookup.h"
109
#include "midx.h"
1110
#include "progress.h"

object-file.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -833,33 +833,6 @@ int stream_object_signature(struct repository *r, const struct object_id *oid)
833833
return !oideq(oid, &real_oid) ? -1 : 0;
834834
}
835835

836-
int git_open_cloexec(const char *name, int flags)
837-
{
838-
int fd;
839-
static int o_cloexec = O_CLOEXEC;
840-
841-
fd = open(name, flags | o_cloexec);
842-
if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
843-
/* Try again w/o O_CLOEXEC: the kernel might not support it */
844-
o_cloexec &= ~O_CLOEXEC;
845-
fd = open(name, flags | o_cloexec);
846-
}
847-
848-
#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
849-
{
850-
static int fd_cloexec = FD_CLOEXEC;
851-
852-
if (!o_cloexec && 0 <= fd && fd_cloexec) {
853-
/* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
854-
int flags = fcntl(fd, F_GETFD);
855-
if (fcntl(fd, F_SETFD, flags | fd_cloexec))
856-
fd_cloexec = 0;
857-
}
858-
}
859-
#endif
860-
return fd;
861-
}
862-
863836
/*
864837
* Find "oid" as a loose object in the local repository or in an alternate.
865838
* Returns 0 on success, negative on failure.

object-file.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ extern int fetch_if_missing;
2121
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);
2222
int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags);
2323

24-
int git_open_cloexec(const char *name, int flags);
25-
#define git_open(name) git_open_cloexec(name, O_RDONLY)
26-
2724
/**
2825
* unpack_loose_header() initializes the data stream needed to unpack
2926
* a loose object header.

pack-bitmap.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "packfile.h"
1818
#include "repository.h"
1919
#include "trace2.h"
20-
#include "object-file.h"
2120
#include "object-store-ll.h"
2221
#include "list-objects-filter-options.h"
2322
#include "midx.h"

pack-mtimes.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "git-compat-util.h"
22
#include "gettext.h"
33
#include "pack-mtimes.h"
4-
#include "object-file.h"
54
#include "object-store-ll.h"
65
#include "packfile.h"
76
#include "strbuf.h"

0 commit comments

Comments
 (0)