Skip to content

Commit 02d0457

Browse files
committed
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
2 parents e484bcb + b4d065d commit 02d0457

File tree

3 files changed

+22
-36
lines changed

3 files changed

+22
-36
lines changed

cache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,8 @@ extern int write_sha1_file(const void *buf, unsigned long len, const char *type,
11251125
extern int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type, unsigned char *sha1, unsigned flags);
11261126
extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);
11271127
extern int force_object_loose(const unsigned char *sha1, time_t mtime);
1128-
extern int git_open(const char *name);
1128+
extern int git_open_cloexec(const char *name, int flags);
1129+
#define git_open(name) git_open_cloexec(name, O_RDONLY)
11291130
extern void *map_sha1_file(const unsigned char *sha1, unsigned long *size);
11301131
extern int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);
11311132
extern int parse_sha1_header(const char *hdr, unsigned long *sizep);

read-cache.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,7 @@ void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
156156
static int ce_compare_data(const struct cache_entry *ce, struct stat *st)
157157
{
158158
int match = -1;
159-
static int cloexec = O_CLOEXEC;
160-
int fd = open(ce->name, O_RDONLY | cloexec);
161-
162-
if ((cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
163-
/* Try again w/o O_CLOEXEC: the kernel might not support it */
164-
cloexec &= ~O_CLOEXEC;
165-
fd = open(ce->name, O_RDONLY | cloexec);
166-
}
159+
int fd = git_open_cloexec(ce->name, O_RDONLY);
167160

168161
if (fd >= 0) {
169162
unsigned char sha1[20];

sha1_file.c

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@
2828
#include "mergesort.h"
2929
#include "quote.h"
3030

31-
#ifndef O_NOATIME
32-
#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
33-
#define O_NOATIME 01000000
34-
#else
35-
#define O_NOATIME 0
36-
#endif
37-
#endif
38-
3931
#define SZ_FMT PRIuMAX
4032
static inline uintmax_t sz_fmt(size_t s) { return s; }
4133

@@ -1611,31 +1603,31 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
16111603
return hashcmp(sha1, real_sha1) ? -1 : 0;
16121604
}
16131605

1614-
int git_open(const char *name)
1606+
int git_open_cloexec(const char *name, int flags)
16151607
{
1616-
static int sha1_file_open_flag = O_NOATIME | O_CLOEXEC;
1617-
1618-
for (;;) {
1619-
int fd;
1620-
1621-
errno = 0;
1622-
fd = open(name, O_RDONLY | sha1_file_open_flag);
1623-
if (fd >= 0)
1624-
return fd;
1608+
int fd;
1609+
static int o_cloexec = O_CLOEXEC;
16251610

1611+
fd = open(name, flags | o_cloexec);
1612+
if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
16261613
/* Try again w/o O_CLOEXEC: the kernel might not support it */
1627-
if ((sha1_file_open_flag & O_CLOEXEC) && errno == EINVAL) {
1628-
sha1_file_open_flag &= ~O_CLOEXEC;
1629-
continue;
1630-
}
1614+
o_cloexec &= ~O_CLOEXEC;
1615+
fd = open(name, flags | o_cloexec);
1616+
}
16311617

1632-
/* Might the failure be due to O_NOATIME? */
1633-
if (errno != ENOENT && (sha1_file_open_flag & O_NOATIME)) {
1634-
sha1_file_open_flag &= ~O_NOATIME;
1635-
continue;
1618+
#if defined(F_GETFL) && defined(F_SETFL) && defined(FD_CLOEXEC)
1619+
{
1620+
static int fd_cloexec = FD_CLOEXEC;
1621+
1622+
if (!o_cloexec && 0 <= fd && fd_cloexec) {
1623+
/* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
1624+
int flags = fcntl(fd, F_GETFL);
1625+
if (fcntl(fd, F_SETFL, flags | fd_cloexec))
1626+
fd_cloexec = 0;
16361627
}
1637-
return -1;
16381628
}
1629+
#endif
1630+
return fd;
16391631
}
16401632

16411633
static int stat_sha1_file(const unsigned char *sha1, struct stat *st)

0 commit comments

Comments
 (0)