From 207df6fc406e81bfeebdff7f404bd242ff3f099f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Tue, 7 May 2024 06:48:35 +0200 Subject: Merging upstream version 0.12.2. Signed-off-by: Daniel Baumann --- src/file_format.cc | 78 +++++++++++++----------------------------------------- 1 file changed, 18 insertions(+), 60 deletions(-) (limited to 'src/file_format.cc') diff --git a/src/file_format.cc b/src/file_format.cc index d82b78c..9858c26 100644 --- a/src/file_format.cc +++ b/src/file_format.cc @@ -29,8 +29,6 @@ * @file file_format.hh */ -#include - #include "file_format.hh" #include "archive_manager.hh" @@ -40,77 +38,37 @@ #include "base/lnav_log.hh" #include "config.h" -static bool -is_pcap_header(uint8_t* buffer) -{ - size_t offset = 0; - if (buffer[0] == 0x0a && buffer[1] == 0x0d && buffer[2] == 0x0d - && buffer[3] == 0x0a) - { - offset += sizeof(uint32_t) * 2; - if (buffer[offset + 0] == 0x1a && buffer[offset + 1] == 0x2b - && buffer[offset + 2] == 0x3c && buffer[offset + 3] == 0x4d) - { - return true; - } - - if (buffer[offset + 0] == 0x4d && buffer[offset + 1] == 0x3c - && buffer[offset + 2] == 0x2b && buffer[offset + 3] == 0x1a) - { - return true; - } - return false; - } - - if (buffer[0] == 0xa1 && buffer[1] == 0xb2 && buffer[2] == 0xc3 - && buffer[3] == 0xd4) - { - return true; - } - - if (buffer[0] == 0xd4 && buffer[1] == 0xc3 && buffer[2] == 0xb2 - && buffer[3] == 0xa1) - { - return true; - } - - if (buffer[0] == 0xa1 && buffer[1] == 0xb2 && buffer[2] == 0x3c - && buffer[3] == 0x4d) - { - return true; - } - - if (buffer[0] == 0x4d && buffer[1] == 0x3c && buffer[2] == 0xb2 - && buffer[3] == 0xa1) - { - return true; - } - - return false; -} - file_format_t detect_file_format(const ghc::filesystem::path& filename) { - if (archive_manager::is_archive(filename)) { + auto describe_res = archive_manager::describe(filename); + if (describe_res.isOk() + && describe_res.unwrap().is()) + { return file_format_t::ARCHIVE; } file_format_t retval = file_format_t::UNKNOWN; - auto_fd fd; - - if ((fd = lnav::filesystem::openp(filename, O_RDONLY)) != -1) { + auto open_res = lnav::filesystem::open_file(filename, O_RDONLY); + if (open_res.isErr()) { + log_error("unable to open file for format detection: %s -- %s", + filename.c_str(), + open_res.unwrapErr().c_str()); + } else { + auto fd = open_res.unwrap(); uint8_t buffer[32]; - ssize_t rc; + auto rc = read(fd, buffer, sizeof(buffer)); - if ((rc = read(fd, buffer, sizeof(buffer))) > 0) { + if (rc < 0) { + log_error("unable to read file for format detection: %s -- %s", + filename.c_str(), + strerror(errno)); + } else { static auto SQLITE3_HEADER = "SQLite format 3"; - auto header_frag = string_fragment(buffer, 0, rc); + auto header_frag = string_fragment::from_bytes(buffer, rc); if (header_frag.startswith(SQLITE3_HEADER)) { retval = file_format_t::SQLITE_DB; - } else if (rc > 24 && is_pcap_header(buffer)) { - retval = file_format_t::PCAP; } } } -- cgit v1.2.3