2 * Eddie Kohler, Yandong Mao, Robert Morris
3 * Copyright (c) 2012-2014 President and Fellows of Harvard College
4 * Copyright (c) 2012-2014 Massachusetts Institute of Technology
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, subject to the conditions
9 * listed in the Masstree LICENSE file. These conditions include: you must
10 * preserve this copyright notice, and you cannot mention the copyright
11 * holders in advertising related to the Software without their permission.
12 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13 * notice is a summary of the Masstree LICENSE file; the license in that file
17 #define KVDB_FILE_HH 1
19 #include <sys/types.h>
24 safe_read(int fd, void *buf, size_t count)
27 while (pos != count) {
28 ssize_t x = ::read(fd, buf, count - pos);
29 if (x != -1 && x != 0) {
30 buf = reinterpret_cast<char *>(buf) + x;
34 else if (errno != EINTR && pos == 0)
36 else if (errno != EINTR)
43 safe_write(int fd, const void *buf, size_t count)
46 while (pos != count) {
47 ssize_t x = ::write(fd, buf, count - pos);
48 if (x != -1 && x != 0) {
49 buf = reinterpret_cast<const char *>(buf) + x;
53 else if (errno != EINTR && pos == 0)
55 else if (errno != EINTR)
62 checked_write(int fd, const void *buf, size_t count)
64 ssize_t x = safe_write(fd, buf, count);
65 always_assert(size_t(x) == count);
68 template <typename T> inline void
69 checked_write(int fd, const T *x)
71 checked_write(fd, reinterpret_cast<const void *>(x), sizeof(*x));
75 lcdf::String read_file_contents(int fd);
76 lcdf::String read_file_contents(const char *filename);
77 int sync_write_file_contents(const char *filename, const lcdf::String &contents,
79 int atomic_write_file_contents(const char *filename, const lcdf::String &contents,