diff options
-rw-r--r-- | compat-util.h | 14 | ||||
-rw-r--r-- | fadvise.c | 8 | ||||
-rw-r--r-- | mincore.c | 2 |
3 files changed, 16 insertions, 8 deletions
diff --git a/compat-util.h b/compat-util.h index d3520e1..60616e2 100644 --- a/compat-util.h +++ b/compat-util.h @@ -56,4 +56,18 @@ static inline off_t cstr_to_off_t(const char *nptr, char **endptr, int base) exit(1); } +static inline int open_noatime(const char *path) +{ + int flags = O_RDONLY | O_NOATIME; + int fd = open(path, flags); + + if (fd < 0) { + if (errno == EPERM && O_NOATIME != 0) + fd = open(path, O_RDONLY); + return fd; + } + + return fd; +} + #endif /* OS_COMPAT_H */ @@ -30,16 +30,10 @@ static void apply_fadvise(const char *path, off_t offset, off_t len, int advice) { int fd; - if ((fd = open(path, O_RDONLY|O_NOATIME)) < 0) { - if (errno == EPERM && O_NOATIME != 0) { - fd = open(path, O_RDONLY); - if (fd >= 0) - goto ok; - } + if ((fd = open_noatime(path) < 0)) { fprintf(stderr, "%s: open(): %s\n", path, strerror(errno)); return; } -ok: if (len <= 0) { /* for compatibility with kernels < 2.6.6 */ @@ -20,7 +20,7 @@ static void mincore_stats(const char *path, off_t offset, off_t len) static const char *fmt = sizeof(void *) == 8 ? "%s: %016lx %x\n": "%s: %08lx %x\n"; - if ((fd = open(path, O_RDONLY|O_NOATIME)) < 0) { + if ((fd = open_noatime(path) < 0)) { fprintf(stderr, "%s: open(): %s\n", path, strerror(errno)); return; } |