diff --git a/core-helper.c b/core-helper.c index dca273296..3829b7a2a 100644 --- a/core-helper.c +++ b/core-helper.c @@ -2073,6 +2073,26 @@ ssize_t stress_system_write( return ret; } +/* + * stress_system_discard() + * read and discard contents of a given file + */ +ssize_t stress_system_discard(const char *path) +{ + int fd; + ssize_t ret; + + if (UNLIKELY(!path)) + return -EINVAL; + fd = open(path, O_RDONLY); + if (UNLIKELY(fd < 0)) + return -errno; + ret = stress_read_discard(fd); + (void)close(fd); + + return ret; +} + /* * stress_system_read() * read a buffer from a /sys or /proc entry @@ -3295,6 +3315,25 @@ int stress_get_unused_uid(uid_t *uid) } #endif +/* + * stress_read_discard(cont int fd) + * read and discard contents of file fd + */ +ssize_t stress_read_discard(const int fd) +{ + ssize_t rbytes = 0, ret; + + do { + char buffer[4096]; + + ret = read(fd, buffer, sizeof(buffer)); + if (ret > 0) + rbytes += ret; + } while (ret > 0); + + return rbytes; +} + /* * stress_read_buffer() * In addition to read() this function makes sure all bytes have been diff --git a/core-helper.h b/core-helper.h index 241661f9e..3a6a00c45 100644 --- a/core-helper.h +++ b/core-helper.h @@ -103,6 +103,7 @@ extern WARN_UNUSED int stress_cache_alloc(const char *name) NONNULL(1); extern void stress_cache_free(void); extern ssize_t stress_system_write(const char *path, const char *buf, const size_t buf_len); +extern ssize_t stress_system_discard(const char *path); extern WARN_UNUSED ssize_t stress_system_read(const char *path, char *buf, const size_t buf_len); extern WARN_UNUSED bool stress_is_prime64(const uint64_t n); @@ -142,6 +143,7 @@ extern WARN_UNUSED int stress_dirent_list_prune(struct dirent **dlist, const int extern WARN_UNUSED bool stress_warn_once_hash(const char *filename, const int line) NONNULL(1); extern WARN_UNUSED uint16_t stress_ipv4_checksum(uint16_t *ptr, const size_t sz); extern WARN_UNUSED int stress_get_unused_uid(uid_t *uid); +extern ssize_t stress_read_discard(const int fd); extern WARN_UNUSED ssize_t stress_read_buffer(const int fd, void* buffer, const ssize_t size, const bool ignore_sig_eintr); extern WARN_UNUSED ssize_t stress_write_buffer(const int fd, const void* buffer,