| #include <fcntl.h> |
| 6 | #include <sys/file.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <unistd.h> |
| 9 |
| 10 | // Each slot is replaced atomically after fsync. flock also serializes separate |
| 11 | // clients/processes, so they cannot both sign against the same journal version. |
| 12 | class WalletJournal { |
| 13 | class File { |
| 14 | public: |
| 15 | int fd; |
| 16 | explicit File(int value) : fd(value) { if (fd < 0) fail(); } |
| 17 | ~File() { ::close(fd); } |
| 18 | File(const File &) = delete; |
| 19 | File &operator=(const File &) = delete; |
| 20 | }; |
| 21 | std::string directory_; |
| 22 |
| 23 | [[noreturn]] static void fail() { throw std::runtime_error("Wallet journal storage failed"); } |
| 24 | static std::string hex(const std::string &value) { |
| 25 | std::string result; |
| 26 | for (unsigned char c : value) { |
| 27 | result += "0123456789abcdef"[c >> 4]; |
| 28 | result += "0123456789abcdef"[c & 15]; |
| 29 | } |
| 30 | return result; |
| 31 | } |
| 32 | static void sync(int fd) { |
| 33 | int result; |
| 34 | do { result = fsync(fd); } while (result < 0 && errno == EINTR); |
| 35 | if (result < 0) fail(); |
| 36 | } |
| 37 | static void mkdir_durable(const std::string &path) { |
| 38 | if (mkdir(path.c_str(), 0700) < 0 && errno != EEXIST) fail(); |
| 39 | // Also sync when recovering a directory created just before a crash. |
| 40 | File parent(open(path.substr(0, path.rfind('/')).c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 41 | sync(parent.fd); |
| 42 | } |
| 43 | static void transfer(int fd, uint8_t *bytes, size_t size, bool writing) { |
| 44 | while (size) { |
| 45 | ssize_t n = writing ? write(fd, bytes, size) : read(fd, bytes, size); |
| 46 | if (n < 0 && errno == EINTR) continue; |
| 47 | if (n <= 0) fail(); |
| 48 | bytes += n; |
| 49 | size -= static_cast<size_t>(n); |
| 50 | } |
| 51 | } |
| 52 | static std::optional<wallet_engine::JournalRecord> load(const std::string &path) { |
| 53 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW); |
| 54 | if (fd < 0 && errno == ENOENT) return std::nullopt; |
| 55 | File file(fd); |
| 56 | struct stat info{}; |
| 57 | if (fstat(fd, &info) < 0 || !S_ISREG(info.st_mode) || info.st_size < 8 || info.st_size > 16 * 1024 * 1024) fail(); |
| 58 | uint8_t header[8]; |
| 59 | transfer(fd, header, sizeof(header), false); |
| 60 | uint64_t version = 0; |
| 61 | for (uint8_t byte : header) version = (version << 8) | byte; |
| 62 | if (!version) fail(); |
| 63 | std::vector<uint8_t> payload(static_cast<size_t>(info.st_size) - 8); |
| 64 | transfer(fd, payload.data(), payload.size(), false); |
| 65 | return wallet_engine::JournalRecord{version, std::move(payload)}; |
| 66 | } |
| 67 | public: |
| 68 | explicit WalletJournal(std::string directory) : directory_(std::move(directory)) {} |
| 69 |
| 70 | wallet_engine::JournalCompareExchangeResult access( |
| 71 | const wallet_engine::JournalKey &key, |
| 72 | const wallet_engine::JournalCompareExchange *mutation = nullptr) const { |
| 73 | mkdir_durable(directory_); |
| 74 | const auto wallet_dir = directory_ + "/" + hex(key.record_id); |
| 75 | mkdir_durable(wallet_dir); |
| 76 | const auto path = wallet_dir + "/" + hex(key.slot); |
| 77 | File lock(open((path + ".lock").c_str(), O_CREAT | O_RDWR | O_CLOEXEC | O_NOFOLLOW, 0600)); |
| 78 | int result; |
| 79 | do { result = flock(lock.fd, LOCK_EX); } while (result < 0 && errno == EINTR); |
| 80 | if (result < 0) fail(); |
| 81 | auto current = load(path); |
| 82 | if (!mutation) return {false, current}; |
| 83 | auto version = current ? std::optional<uint64_t>(current->version) : std::nullopt; |
| 84 | if (version != mutation->expected_version) return {false, current}; |
| 85 | if (!mutation->replacement.version || (version && mutation->replacement.version <= *version) |
| 86 | || mutation->replacement.payload.size() > 16 * 1024 * 1024 - 8) fail(); |
| 87 | const auto temp = path + ".tmp"; |
| 88 | File file(open(temp.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0600)); |
| 89 | uint8_t header[8]; |
| 90 | uint64_t replacement_version = mutation->replacement.version; |
| 91 | for (int i = 7; i >= 0; --i) { |
| 92 | header[i] = static_cast<uint8_t>(replacement_version); |
| 93 | replacement_version >>= 8; |
| 94 | } |
| 95 | transfer(file.fd, header, sizeof(header), true); |
| 96 | const auto &payload = mutation->replacement.payload; |
| 97 | transfer(file.fd, const_cast<uint8_t *>(payload.data()), payload.size(), true); |
| 98 | sync(file.fd); |
| 99 | if (rename(temp.c_str(), path.c_str()) < 0) fail(); |
| 100 | File directory(open(wallet_dir.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 101 | sync(directory.fd); |
| 102 | return {true, mutation->replacement}; |
| 103 | } |
| 104 | }; |
| 105 |