diff options
author | Junio C Hamano <gitster@pobox.com> | 2017-01-11 02:24:26 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-01-11 02:24:26 +0300 |
commit | 02d0457eb4b9bf656965c5dbb613b8bfaef3316f (patch) | |
tree | 1cec6b47aa64274a4c154a00fefdf5c3d0bcfecc /sha1_file.c | |
parent | e484bcbab1398e9a716e2e0e9de7928d9f796ef2 (diff) | |
parent | b4d065df03049bacfbc40467b60b13e804b7d289 (diff) |
Merge branch 'jc/git-open-cloexec'
The codeflow of setting NOATIME and CLOEXEC on file descriptors Git
opens has been simplified.
We may want to drop the tip one, but we'll see.
* jc/git-open-cloexec:
sha1_file: stop opening files with O_NOATIME
git_open_cloexec(): use fcntl(2) w/ FD_CLOEXEC fallback
git_open(): untangle possible NOATIME and CLOEXEC interactions
Diffstat (limited to 'sha1_file.c')
-rw-r--r-- | sha1_file.c | 46 |
1 files changed, 19 insertions, 27 deletions
diff --git a/sha1_file.c b/sha1_file.c index 1173071859..1eb47f6113 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -28,14 +28,6 @@ #include "mergesort.h" #include "quote.h" -#ifndef O_NOATIME -#if defined(__linux__) && (defined(__i386__) || defined(__PPC__)) -#define O_NOATIME 01000000 -#else -#define O_NOATIME 0 -#endif -#endif - #define SZ_FMT PRIuMAX static inline uintmax_t sz_fmt(size_t s) { return s; } @@ -1611,31 +1603,31 @@ int check_sha1_signature(const unsigned char *sha1, void *map, return hashcmp(sha1, real_sha1) ? -1 : 0; } -int git_open(const char *name) +int git_open_cloexec(const char *name, int flags) { - static int sha1_file_open_flag = O_NOATIME | O_CLOEXEC; - - for (;;) { - int fd; - - errno = 0; - fd = open(name, O_RDONLY | sha1_file_open_flag); - if (fd >= 0) - return fd; + 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 */ - if ((sha1_file_open_flag & O_CLOEXEC) && errno == EINVAL) { - sha1_file_open_flag &= ~O_CLOEXEC; - continue; - } + o_cloexec &= ~O_CLOEXEC; + fd = open(name, flags | o_cloexec); + } - /* Might the failure be due to O_NOATIME? */ - if (errno != ENOENT && (sha1_file_open_flag & O_NOATIME)) { - sha1_file_open_flag &= ~O_NOATIME; - continue; +#if defined(F_GETFL) && defined(F_SETFL) && 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_GETFL); + if (fcntl(fd, F_SETFL, flags | fd_cloexec)) + fd_cloexec = 0; } - return -1; } +#endif + return fd; } static int stat_sha1_file(const unsigned char *sha1, struct stat *st) |