diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-07 04:48:35 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-07 04:48:35 +0000 |
commit | 207df6fc406e81bfeebdff7f404bd242ff3f099f (patch) | |
tree | a1a796b056909dd0a04ffec163db9363a8757808 /src/file_format.cc | |
parent | Releasing progress-linux version 0.11.2-1~progress7.99u1. (diff) | |
download | lnav-207df6fc406e81bfeebdff7f404bd242ff3f099f.tar.xz lnav-207df6fc406e81bfeebdff7f404bd242ff3f099f.zip |
Merging upstream version 0.12.2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/file_format.cc | 78 |
1 files changed, 18 insertions, 60 deletions
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 <unordered_map> - #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<archive_manager::archive_info>()) + { 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; } } } |