22 #include <sys/types.h>
35 int err = fstat(fd, &buf);
41 auto ptr = mmap(
nullptr, sz, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
42 CHECK(ptr != reinterpret_cast<void*>(-1));
45 madvise(ptr, sz, MADV_RANDOM | MADV_WILLNEED | MADV_HUGEPAGE);
47 madvise(ptr, sz, MADV_RANDOM | MADV_WILLNEED);
66 int open(
const char* path,
int flags,
int mode) {
78 ::FILE*
popen(
const char* command,
const char*
type) {
86 int32_t
ftruncate(
const int32_t fd, int64_t length) {
90 int safe_open(
const char* path,
int flags, mode_t mode) noexcept {
92 ret =
::open(path, flags, mode);
93 if (ret == -1 && errno == EINTR) {
104 if (ret == -1 && errno == EINTR) {
114 ret = ::fcntl(fd, cmd, fl);
115 if (ret == -1 && errno == EINTR) {
123 ssize_t
safe_read(
const int fd,
void* buffer,
const size_t buffer_size) noexcept {
124 for (ssize_t ret, sz = 0;;) {
125 ret =
::read(fd, &static_cast<char*>(buffer)[sz], buffer_size - sz);
127 if (errno == EINTR) {
136 if (sz == static_cast<ssize_t>(buffer_size)) {
144 ssize_t
safe_write(
const int fd,
const void* buffer,
const size_t buffer_size) noexcept {
145 for (ssize_t ret, sz = 0;;) {
146 ret =
::write(fd, &static_cast<char const*>(buffer)[sz], buffer_size - sz);
148 if (errno == EINTR) {
154 if (sz == static_cast<ssize_t>(buffer_size)) {
165 if (ret == -1 && errno == EINTR) {
int safe_open(const char *path, int flags, mode_t mode) noexcept
int32_t pclose(::FILE *fh)
::FILE * popen(const char *command, const char *type)
ssize_t safe_read(const int fd, void *buffer, const size_t buffer_size) noexcept
size_t write(FILE *f, const size_t offset, const size_t size, const int8_t *buf)
Writes the specified number of bytes to the offset position in file f from buf.
ssize_t safe_write(const int fd, const void *buffer, const size_t buffer_size) noexcept
future< Result > async(Fn &&fn, Args &&...args)
int open(const char *path, int flags, int mode)
::FILE * fopen(const char *filename, const char *mode)
size_t read(FILE *f, const size_t offset, const size_t size, int8_t *buf, const std::string &file_path)
Reads the specified number of bytes from the offset position in file f into buf.
int msync(void *addr, size_t length, bool async)
int32_t safe_ftruncate(const int32_t fd, int64_t length) noexcept
std::string filename(char const *path)
void checked_munmap(void *addr, size_t length)
int32_t ftruncate(const int32_t fd, int64_t length)
int safe_fcntl(int fd, int cmd, struct flock *fl) noexcept
int safe_close(int fd) noexcept
size_t file_size(const int fd)
void * checked_mmap(const int fd, const size_t sz)