Git fork

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>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
97dc141f 1a99fe80

+34 -36
+1 -1
Makefile
··· 994 994 LIB_OBJS += common-init.o 995 995 LIB_OBJS += compat/nonblock.o 996 996 LIB_OBJS += compat/obstack.o 997 + LIB_OBJS += compat/open.o 997 998 LIB_OBJS += compat/terminal.o 998 999 LIB_OBJS += compiler-tricks/not-constant.o 999 1000 LIB_OBJS += config.o ··· 1812 1813 endif 1813 1814 ifdef OPEN_RETURNS_EINTR 1814 1815 COMPAT_CFLAGS += -DOPEN_RETURNS_EINTR 1815 - COMPAT_OBJS += compat/open.o 1816 1816 endif 1817 1817 ifdef NO_SYMLINK_HEAD 1818 1818 BASIC_CFLAGS += -DNO_SYMLINK_HEAD
-1
commit-graph.c
··· 13 13 #include "refs.h" 14 14 #include "hash-lookup.h" 15 15 #include "commit-graph.h" 16 - #include "object-file.h" 17 16 #include "object-store-ll.h" 18 17 #include "oid-array.h" 19 18 #include "path.h"
+29
compat/open.c
··· 1 1 #include "git-compat-util.h" 2 2 3 + #ifdef OPEN_RETURNS_EINTR 3 4 #undef open 4 5 int git_open_with_retry(const char *path, int flags, ...) 5 6 { ··· 23 24 24 25 return ret; 25 26 } 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 + }
+3
git-compat-util.h
··· 1000 1000 int git_open_with_retry(const char *path, int flag, ...); 1001 1001 #endif 1002 1002 1003 + int git_open_cloexec(const char *name, int flags); 1004 + #define git_open(name) git_open_cloexec(name, O_RDONLY) 1005 + 1003 1006 #ifdef __GLIBC_PREREQ 1004 1007 #if __GLIBC_PREREQ(2, 1) 1005 1008 #define HAVE_STRCHRNUL
+1
meson.build
··· 263 263 'common-init.c', 264 264 'compat/nonblock.c', 265 265 'compat/obstack.c', 266 + 'compat/open.c', 266 267 'compat/terminal.c', 267 268 'compiler-tricks/not-constant.c', 268 269 'config.c',
-1
midx.c
··· 5 5 #include "dir.h" 6 6 #include "hex.h" 7 7 #include "packfile.h" 8 - #include "object-file.h" 9 8 #include "hash-lookup.h" 10 9 #include "midx.h" 11 10 #include "progress.h"
-27
object-file.c
··· 833 833 return !oideq(oid, &real_oid) ? -1 : 0; 834 834 } 835 835 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 - 863 836 /* 864 837 * Find "oid" as a loose object in the local repository or in an alternate. 865 838 * Returns 0 on success, negative on failure.
-3
object-file.h
··· 21 21 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); 22 22 int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags); 23 23 24 - int git_open_cloexec(const char *name, int flags); 25 - #define git_open(name) git_open_cloexec(name, O_RDONLY) 26 - 27 24 /** 28 25 * unpack_loose_header() initializes the data stream needed to unpack 29 26 * a loose object header.
-1
pack-bitmap.c
··· 17 17 #include "packfile.h" 18 18 #include "repository.h" 19 19 #include "trace2.h" 20 - #include "object-file.h" 21 20 #include "object-store-ll.h" 22 21 #include "list-objects-filter-options.h" 23 22 #include "midx.h"
-1
pack-mtimes.c
··· 1 1 #include "git-compat-util.h" 2 2 #include "gettext.h" 3 3 #include "pack-mtimes.h" 4 - #include "object-file.h" 5 4 #include "object-store-ll.h" 6 5 #include "packfile.h" 7 6 #include "strbuf.h"
-1
pack-revindex.c
··· 1 1 #include "git-compat-util.h" 2 2 #include "gettext.h" 3 3 #include "pack-revindex.h" 4 - #include "object-file.h" 5 4 #include "object-store-ll.h" 6 5 #include "packfile.h" 7 6 #include "strbuf.h"