| 80 | } |
| 81 | |
| 82 | bool IsBDBFile(const fs::path& path) |
| 83 | { |
| 84 | if (!fs::exists(path)) return false; |
| 85 | |
| 86 | // A Berkeley DB Btree file has at least 4K. |
| 87 | // This check also prevents opening lock files. |
| 88 | std::error_code ec; |
| 89 | auto size = fs::file_size(path, ec); |
| 90 | if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(path)); |
| 91 | if (size < 4096) return false; |
| 92 | |
| 93 | std::ifstream file{path, std::ios::binary}; |
| 94 | if (!file.is_open()) return false; |
| 95 | |
| 96 | file.seekg(12, std::ios::beg); // Magic bytes start at offset 12 |
| 97 | uint32_t data = 0; |
| 98 | file.read((char*) &data, sizeof(data)); // Read 4 bytes of file to compare against magic |
| 99 | |
| 100 | // Berkeley DB Btree magic bytes, from: |
| 101 | // https://github.com/file/file/blob/5824af38469ec1ca9ac3ffd251e7afe9dc11e227/magic/Magdir/database#L74-L75 |
| 102 | // - big endian systems - 00 05 31 62 |
| 103 | // - little endian systems - 62 31 05 00 |
| 104 | return data == 0x00053162 || data == 0x62310500; |
| 105 | } |
| 106 | |
| 107 | bool IsSQLiteFile(const fs::path& path) |
| 108 | { |
no test coverage detected