| 544 | // Errors are -1 |
| 545 | template <typename CacheEntryType> |
| 546 | int |
| 547 | LoadRefCountCacheFromPath(RefCountCache<CacheEntryType> &cache, const std::string &filepath, |
| 548 | CacheEntryType *(*load_func)(char *, unsigned int)) |
| 549 | { |
| 550 | // If we have no load method, then we can't load anything so lets just stop right here |
| 551 | if (load_func == nullptr) { |
| 552 | return -1; // TODO: some specific error code |
| 553 | } |
| 554 | |
| 555 | int fd = open(filepath.c_str(), O_RDONLY); |
| 556 | if (fd < 0) { |
| 557 | Warning("Unable to open file %s; [Error]: %s", filepath.c_str(), strerror(errno)); |
| 558 | return -1; |
| 559 | } |
| 560 | |
| 561 | // read in the header |
| 562 | RefCountCacheHeader tmpHeader = RefCountCacheHeader(); |
| 563 | int read_ret = read(fd, reinterpret_cast<char *>(&tmpHeader), sizeof(RefCountCacheHeader)); |
| 564 | if (read_ret != sizeof(RefCountCacheHeader)) { |
| 565 | UnixSocket{fd}.close(); |
| 566 | Warning("Error reading cache header from disk (expected %ld): %d", sizeof(RefCountCacheHeader), read_ret); |
| 567 | return -1; |
| 568 | } |
| 569 | if (!cache.get_header().compatible(&tmpHeader)) { |
| 570 | UnixSocket{fd}.close(); |
| 571 | Warning("Incompatible cache at %s, not loading.", filepath.c_str()); |
| 572 | return -1; // TODO: specific code for incompatible |
| 573 | } |
| 574 | |
| 575 | RefCountCacheItemMeta tmpValue = RefCountCacheItemMeta(0, 0); |
| 576 | while (true) { // TODO: better loop |
| 577 | read_ret = read(fd, reinterpret_cast<char *>(&tmpValue), sizeof(tmpValue)); |
| 578 | if (read_ret != sizeof(tmpValue)) { |
| 579 | break; |
| 580 | } |
| 581 | char buf[tmpValue.size]; |
| 582 | read_ret = read(fd, reinterpret_cast<char *>(&buf), tmpValue.size); |
| 583 | if (read_ret != static_cast<int>(tmpValue.size)) { |
| 584 | Warning("Encountered error reading item from cache: %d", read_ret); |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | CacheEntryType *newItem = load_func(reinterpret_cast<char *>(&buf), tmpValue.size); |
| 589 | if (newItem != nullptr) { |
| 590 | cache.put(tmpValue.key, newItem, tmpValue.size - sizeof(CacheEntryType)); |
| 591 | } |
| 592 | }; |
| 593 | |
| 594 | UnixSocket{fd}.close(); |
| 595 | return 0; |
| 596 | } |
nothing calls this directly
no test coverage detected