| 92 | } |
| 93 | |
| 94 | bool IsBDBFile(const fs::path& path) |
| 95 | { |
| 96 | if (!fs::exists(path)) return false; |
| 97 | |
| 98 | // A Berkeley DB Btree file has at least 4K. |
| 99 | // This check also prevents opening lock files. |
| 100 | std::error_code ec; |
| 101 | auto size = fs::file_size(path, ec); |
| 102 | if (ec) LogWarning("Error reading file_size: %s [%s]", ec.message(), fs::PathToString(path)); |
| 103 | if (size < 4096) return false; |
| 104 | |
| 105 | std::ifstream file{path.std_path(), std::ios::binary}; |
| 106 | if (!file.is_open()) return false; |
| 107 | |
| 108 | file.seekg(12, std::ios::beg); // Magic bytes start at offset 12 |
| 109 | uint32_t data = 0; |
| 110 | file.read((char*) &data, sizeof(data)); // Read 4 bytes of file to compare against magic |
| 111 | |
| 112 | // Berkeley DB Btree magic bytes, from: |
| 113 | // https://github.com/file/file/blob/5824af38469ec1ca9ac3ffd251e7afe9dc11e227/magic/Magdir/database#L74-L75 |
| 114 | // - big endian systems - 00 05 31 62 |
| 115 | // - little endian systems - 62 31 05 00 |
| 116 | return data == 0x00053162 || data == 0x62310500; |
| 117 | } |
| 118 | |
| 119 | bool IsSQLiteFile(const fs::path& path) |
| 120 | { |
no test coverage detected