13#include "absl/status/status.h"
14#include "absl/strings/str_cat.h"
21[[noreturn]]
void Fail(
const char* what) {
22 std::fprintf(stderr,
"smollergraph: %s: %s\n", what, std::strerror(errno));
29 if (fd_ >= 0) close(fd_);
33 return Adopt(open(
path.c_str(), O_RDWR | O_CREAT, 0644),
path);
37 char path[] =
"/tmp/smollergraph-XXXXXX";
38 int fd = mkstemp(
path);
40 if (fd >= 0) unlink(
path);
41 return Adopt(fd,
"a temporary file");
44absl::Status Pager::Adopt(
int fd, std::string_view name) {
46 if (fd < 0 || fstat(fd, &info) != 0) {
49 absl::ErrnoToStatus(errno, absl::StrCat(
"can't open ", name));
50 if (fd >= 0) close(fd);
53 if (fd_ >= 0) close(fd_);
56 return absl::OkStatus();
60 off_t offset =
static_cast<off_t
>(id) *
kPageSize;
61 if (pread(fd_, page.data(),
kPageSize, offset) !=
63 Fail(
"can't read a page");
69 off_t offset =
static_cast<off_t
>(id) *
kPageSize;
70 if (pwrite(fd_, page.data(),
kPageSize, offset) !=
72 Fail(
"can't write a page");
74 if (
id >= page_count_) page_count_ =
id + 1;
std::array< std::byte, kPageSize > Page
constexpr size_t kPageSize
What the database reads and writes at a time: 4 KiB, the size of a block on most file systems and dis...