1#include "duckdb/storage/magic_bytes.hpp"
2#include "duckdb/common/local_file_system.hpp"
3#include "duckdb/storage/storage_info.hpp"
4
5namespace duckdb {
6
7DataFileType MagicBytes::CheckMagicBytes(FileSystem *fs_p, const string &path) {
8 LocalFileSystem lfs;
9 FileSystem &fs = fs_p ? *fs_p : lfs;
10 if (!fs.FileExists(filename: path)) {
11 return DataFileType::FILE_DOES_NOT_EXIST;
12 }
13 auto handle = fs.OpenFile(path, flags: FileFlags::FILE_FLAGS_READ);
14
15 constexpr const idx_t MAGIC_BYTES_READ_SIZE = 16;
16 char buffer[MAGIC_BYTES_READ_SIZE];
17
18 handle->Read(buffer, nr_bytes: MAGIC_BYTES_READ_SIZE);
19 if (memcmp(s1: buffer, s2: "SQLite format 3\0", n: 16) == 0) {
20 return DataFileType::SQLITE_FILE;
21 }
22 if (memcmp(s1: buffer, s2: "PAR1", n: 4) == 0) {
23 return DataFileType::PARQUET_FILE;
24 }
25 if (memcmp(s1: buffer + MainHeader::MAGIC_BYTE_OFFSET, s2: MainHeader::MAGIC_BYTES, n: MainHeader::MAGIC_BYTE_SIZE) == 0) {
26 return DataFileType::DUCKDB_FILE;
27 }
28 return DataFileType::FILE_DOES_NOT_EXIST;
29}
30
31} // namespace duckdb
32