| 37 | // global functions |
| 38 | |
| 39 | FILE *OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly) { |
| 40 | if (pos.IsNull()) |
| 41 | return nullptr; |
| 42 | boost::filesystem::path path = GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile); |
| 43 | boost::filesystem::create_directories(path.parent_path()); |
| 44 | FILE *file = fopen(path.string().c_str(), "rb+"); |
| 45 | if (!file && !fReadOnly) |
| 46 | file = fopen(path.string().c_str(), "wb+"); |
| 47 | if (!file) { |
| 48 | LogPrint(BCLog::ERROR, "Unable to open file %s\n", path.string()); |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | if (pos.nPos) { |
| 53 | if (fseek(file, pos.nPos, SEEK_SET)) { |
| 54 | LogPrint(BCLog::ERROR, "Unable to seek to position %u of %s\n", pos.nPos, path.string()); |
| 55 | fclose(file); |
| 56 | return nullptr; |
| 57 | } |
| 58 | } |
| 59 | return file; |
| 60 | } |
| 61 | |
| 62 | FILE *OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) { |
| 63 | return OpenDiskFile(pos, "blk", fReadOnly); |
no test coverage detected