diff --git a/src/applib/CMakeLists.txt b/src/applib/CMakeLists.txt index f1616fa..c9b1a56 100644 --- a/src/applib/CMakeLists.txt +++ b/src/applib/CMakeLists.txt @@ -29,11 +29,11 @@ target_sources(applib PRIVATE gui_utils.h selftest.cpp selftest.h + smartctl_ata_text_parser.cpp + smartctl_ata_text_parser.h smartctl_executor.cpp smartctl_executor_gui.h smartctl_executor.h - smartctl_text_parser.cpp - smartctl_text_parser.h storage_detector.cpp storage_detector.h storage_detector_helpers.h diff --git a/src/applib/examples/example_smartctl_parser.cpp b/src/applib/examples/example_smartctl_parser.cpp index 6a8553a..71fce36 100644 --- a/src/applib/examples/example_smartctl_parser.cpp +++ b/src/applib/examples/example_smartctl_parser.cpp @@ -21,7 +21,7 @@ Copyright: #include "libdebug/libdebug.h" #include "hz/fs.h" #include "applib/ata_storage_property.h" -#include "applib/smartctl_text_parser.h" +#include "applib/smartctl_ata_text_parser.h" @@ -42,7 +42,7 @@ int main(int argc, char* argv[]) return EXIT_FAILURE; } - SmartctlTextParser sp; + SmartctlAtaTextParser sp; if (!sp.parse_full(contents, AtaStorageAttribute::DiskType::Any)) { debug_out_error("app", "Cannot parse file contents: " << sp.get_error_msg() << "\n"); return EXIT_FAILURE; diff --git a/src/applib/selftest.cpp b/src/applib/selftest.cpp index 06eb18a..a3aab27 100644 --- a/src/applib/selftest.cpp +++ b/src/applib/selftest.cpp @@ -16,7 +16,7 @@ Copyright: #include "app_pcrecpp.h" #include "ata_storage_property.h" -#include "smartctl_text_parser.h" +#include "smartctl_ata_text_parser.h" #include "selftest.h" @@ -243,7 +243,7 @@ std::string SelfTest::update(const std::shared_ptr& smartctl_ex return error_msg; AtaStorageAttribute::DiskType disk_type = drive_->get_is_hdd() ? AtaStorageAttribute::DiskType::Hdd : AtaStorageAttribute::DiskType::Ssd; - SmartctlTextParser ps; + SmartctlAtaTextParser ps; if (!ps.parse_full(output, disk_type)) { // try to parse it return ps.get_error_msg(); } diff --git a/src/applib/smartctl_text_parser.cpp b/src/applib/smartctl_ata_text_parser.cpp similarity index 97% rename from src/applib/smartctl_text_parser.cpp rename to src/applib/smartctl_ata_text_parser.cpp index e81ee9a..6506a1e 100644 --- a/src/applib/smartctl_text_parser.cpp +++ b/src/applib/smartctl_ata_text_parser.cpp @@ -20,7 +20,7 @@ Copyright: #include "hz/debug.h" // debug_* #include "app_pcrecpp.h" -#include "smartctl_text_parser.h" +#include "smartctl_ata_text_parser.h" #include "ata_storage_property_descr.h" #include "warning_colors.h" @@ -67,7 +67,7 @@ namespace { // Parse full "smartctl -x" output -bool SmartctlTextParser::parse_full(const std::string& full, AtaStorageAttribute::DiskType disk_type) +bool SmartctlAtaTextParser::parse_full(const std::string& full, AtaStorageAttribute::DiskType disk_type) { this->clear(); // clear previous data @@ -276,7 +276,7 @@ bool SmartctlTextParser::parse_full(const std::string& full, AtaStorageAttribute // Supply output of "smartctl --version" here. // returns false on failure. Non-unix newlines in s are ok. -bool SmartctlTextParser::parse_version(const std::string& s, std::string& version, std::string& version_full) +bool SmartctlAtaTextParser::parse_version(const std::string& s, std::string& version, std::string& version_full) { // e.g. // "smartctl version 5.37" @@ -296,7 +296,7 @@ bool SmartctlTextParser::parse_version(const std::string& s, std::string& versio // check that the version of smartctl output can be parsed with this parser. -bool SmartctlTextParser::check_parsed_version(const std::string& version_str, [[maybe_unused]] const std::string& version_full_str) +bool SmartctlAtaTextParser::check_parsed_version(const std::string& version_str, [[maybe_unused]] const std::string& version_full_str) { // tested with 5.1-xx versions (1 - 18), and 5.[20 - 38]. // note: 5.1-11 (maybe others too) with scsi disk gives non-parsable output (why?). @@ -318,7 +318,7 @@ bool SmartctlTextParser::check_parsed_version(const std::string& version_str, [[ // convert e.g. "1,000,204,886,016 bytes" to 1.00 TB [931.51 GiB, 1000204886016 bytes]. // Note: this property is present since 5.33. -std::string SmartctlTextParser::parse_byte_size(const std::string& str, int64_t& bytes, bool extended) +std::string SmartctlAtaTextParser::parse_byte_size(const std::string& str, int64_t& bytes, bool extended) { // E.g. "500,107,862,016" bytes or "80'060'424'192 bytes" or "80 026 361 856 bytes". // French locale inserts 0xA0 as a separator (non-breaking space, _not_ a valid utf8 char). @@ -371,7 +371,7 @@ std::string SmartctlTextParser::parse_byte_size(const std::string& str, int64_t& // Parse the section part (with "=== .... ===" header) - info or data sections. -bool SmartctlTextParser::parse_section(const std::string& header, const std::string& body) +bool SmartctlAtaTextParser::parse_section(const std::string& header, const std::string& body) { if (app_pcre_match("/START OF INFORMATION SECTION/mi", header)) { return parse_section_info(body); @@ -413,7 +413,7 @@ bool SmartctlTextParser::parse_section(const std::string& header, const std::str // ------------------------------------------------ INFO SECTION -bool SmartctlTextParser::parse_section_info(const std::string& body) +bool SmartctlAtaTextParser::parse_section_info(const std::string& body) { this->set_data_section_info(body); @@ -522,7 +522,7 @@ http://knowledge.seagate.com/articles/en_US/FAQ/213891en // Parse a component (one line) of the info section -bool SmartctlTextParser::parse_section_info_property(AtaStorageProperty& p) +bool SmartctlAtaTextParser::parse_section_info_property(AtaStorageProperty& p) { // ---- Info if (p.section != AtaStorageProperty::Section::info) { @@ -713,7 +713,7 @@ bool SmartctlTextParser::parse_section_info_property(AtaStorageProperty& p) // Parse the Data section (without "===" header) -bool SmartctlTextParser::parse_section_data(const std::string& body) +bool SmartctlAtaTextParser::parse_section_data(const std::string& body) { this->set_data_section_data(body); @@ -864,7 +864,7 @@ bool SmartctlTextParser::parse_section_data(const std::string& body) // -------------------- Health -bool SmartctlTextParser::parse_section_data_subsection_health(const std::string& sub) +bool SmartctlAtaTextParser::parse_section_data_subsection_health(const std::string& sub) { // Health section data (--info and --get=all): /* @@ -907,7 +907,7 @@ Device is: In smartctl database [for details use: -P show] // -------------------- Capabilities -bool SmartctlTextParser::parse_section_data_subsection_capabilities(const std::string& sub_initial) +bool SmartctlAtaTextParser::parse_section_data_subsection_capabilities(const std::string& sub_initial) { // Capabilities section data: /* @@ -1087,7 +1087,7 @@ SCT capabilities: (0x003d) SCT Status supported. // Check the capabilities for internal properties we can use. -bool SmartctlTextParser::parse_section_data_internal_capabilities(AtaStorageProperty& cap_prop) +bool SmartctlAtaTextParser::parse_section_data_internal_capabilities(AtaStorageProperty& cap_prop) { // Some special capabilities we're interested in. @@ -1344,7 +1344,7 @@ bool SmartctlTextParser::parse_section_data_internal_capabilities(AtaStorageProp // -------------------- Attributes -bool SmartctlTextParser::parse_section_data_subsection_attributes(const std::string& sub) +bool SmartctlAtaTextParser::parse_section_data_subsection_attributes(const std::string& sub) { AtaStorageProperty pt; // template for easy copying pt.section = AtaStorageProperty::Section::data; @@ -1564,7 +1564,7 @@ ID# ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE -bool SmartctlTextParser::parse_section_data_subsection_directory_log(const std::string& sub) +bool SmartctlAtaTextParser::parse_section_data_subsection_directory_log(const std::string& sub) { AtaStorageProperty pt; // template for easy copying pt.section = AtaStorageProperty::Section::data; @@ -1616,7 +1616,7 @@ Address Access R/W Size Description -bool SmartctlTextParser::parse_section_data_subsection_error_log(const std::string& sub) +bool SmartctlAtaTextParser::parse_section_data_subsection_error_log(const std::string& sub) { AtaStorageProperty pt; // template for easy copying pt.section = AtaStorageProperty::Section::data; @@ -1805,7 +1805,7 @@ Error 1 [0] occurred at disk power-on lifetime: 1 hours (0 days + 1 hours) // -------------------- Selftest Log -bool SmartctlTextParser::parse_section_data_subsection_selftest_log(const std::string& sub) +bool SmartctlAtaTextParser::parse_section_data_subsection_selftest_log(const std::string& sub) { AtaStorageProperty pt; // template for easy copying pt.section = AtaStorageProperty::Section::data; @@ -1981,7 +1981,7 @@ Num Test_Description Status Remaining LifeTime(hours) LBA // -------------------- Selective Selftest Log -bool SmartctlTextParser::parse_section_data_subsection_selective_selftest_log(const std::string& sub) +bool SmartctlAtaTextParser::parse_section_data_subsection_selective_selftest_log(const std::string& sub) { AtaStorageProperty pt; // template for easy copying pt.section = AtaStorageProperty::Section::data; @@ -2034,7 +2034,7 @@ If Selective self-test is pending on power-up, resume after 0 minute delay. -bool SmartctlTextParser::parse_section_data_subsection_scttemp_log(const std::string& sub) +bool SmartctlAtaTextParser::parse_section_data_subsection_scttemp_log(const std::string& sub) { AtaStorageProperty pt; // template for easy copying pt.section = AtaStorageProperty::Section::data; @@ -2120,7 +2120,7 @@ Index Estimated Time Temperature Celsius -bool SmartctlTextParser::parse_section_data_subsection_scterc_log(const std::string& sub) +bool SmartctlAtaTextParser::parse_section_data_subsection_scterc_log(const std::string& sub) { AtaStorageProperty pt; // template for easy copying pt.section = AtaStorageProperty::Section::data; @@ -2165,7 +2165,7 @@ SCT Error Recovery Control: -bool SmartctlTextParser::parse_section_data_subsection_devstat(const std::string& sub) +bool SmartctlAtaTextParser::parse_section_data_subsection_devstat(const std::string& sub) { AtaStorageProperty pt; // template for easy copying pt.section = AtaStorageProperty::Section::data; @@ -2335,7 +2335,7 @@ Page Offset Size Value Description -bool SmartctlTextParser::parse_section_data_subsection_sataphy(const std::string& sub) +bool SmartctlAtaTextParser::parse_section_data_subsection_sataphy(const std::string& sub) { AtaStorageProperty pt; // template for easy copying pt.section = AtaStorageProperty::Section::data; @@ -2389,21 +2389,33 @@ ID Size Value Description -std::string SmartctlTextParser::get_data_full() const +void SmartctlAtaTextParser::clear() +{ + data_full_.clear(); + data_section_info_.clear(); + data_section_data_.clear(); + error_msg_.clear(); + + properties_.clear(); +} + + + +std::string SmartctlAtaTextParser::get_data_full() const { return data_full_; } -std::string SmartctlTextParser::get_error_msg() const +std::string SmartctlAtaTextParser::get_error_msg() const { return Glib::ustring::compose(_("Cannot parse smartctl output: %1"), error_msg_); } -const std::vector& SmartctlTextParser::get_properties() const +const std::vector& SmartctlAtaTextParser::get_properties() const { return properties_; } @@ -2412,7 +2424,7 @@ const std::vector& SmartctlTextParser::get_properties() cons // adds a property into property list, looks up and sets its description. // Yes, there's no place for this in the Parser, but whatever... -void SmartctlTextParser::add_property(AtaStorageProperty p) +void SmartctlAtaTextParser::add_property(AtaStorageProperty p) { ata_storage_property_autoset_description(p, disk_type_); ata_storage_property_autoset_warning(p); @@ -2423,28 +2435,28 @@ void SmartctlTextParser::add_property(AtaStorageProperty p) -void SmartctlTextParser::set_data_full(const std::string& s) +void SmartctlAtaTextParser::set_data_full(const std::string& s) { data_full_ = s; } -void SmartctlTextParser::set_data_section_info(const std::string& s) +void SmartctlAtaTextParser::set_data_section_info(const std::string& s) { data_section_info_ = s; } -void SmartctlTextParser::set_data_section_data(const std::string& s) +void SmartctlAtaTextParser::set_data_section_data(const std::string& s) { data_section_data_ = s; } -void SmartctlTextParser::set_error_msg(const std::string& s) +void SmartctlAtaTextParser::set_error_msg(const std::string& s) { error_msg_ = s; } diff --git a/src/applib/smartctl_text_parser.h b/src/applib/smartctl_ata_text_parser.h similarity index 89% rename from src/applib/smartctl_text_parser.h rename to src/applib/smartctl_ata_text_parser.h index 76a117a..63b003b 100644 --- a/src/applib/smartctl_text_parser.h +++ b/src/applib/smartctl_ata_text_parser.h @@ -9,8 +9,8 @@ Copyright: /// \weakgroup applib /// @{ -#ifndef SMARTCTL_TEXT_PARSER_H -#define SMARTCTL_TEXT_PARSER_H +#ifndef SMARTCTL_ATA_TEXT_PARSER_H +#define SMARTCTL_ATA_TEXT_PARSER_H #include #include @@ -19,10 +19,10 @@ Copyright: -/// Smartctl parser. +/// Smartctl (S)ATA text output parser. /// Note: ALL parse_* functions (except parse_full() and parse_version()) /// expect data in unix-newline format! -class SmartctlTextParser { +class SmartctlAtaTextParser { public: /// Parse full "smartctl -x" output @@ -41,8 +41,7 @@ class SmartctlTextParser { static std::string parse_byte_size(const std::string& str, int64_t& bytes, bool extended); - // You don't really need to call these functions, use the ones above. - + private: /// Parse the section part (with "=== .... ===" header) - info or data sections. bool parse_section(const std::string& header, const std::string& body); @@ -77,32 +76,14 @@ class SmartctlTextParser { /// Clear parsed data - void clear() - { - data_full_.clear(); - data_section_info_.clear(); - data_section_data_.clear(); - error_msg_.clear(); + void clear(); - properties_.clear(); - } + public: /// Get "full" data, as passed to parse_full(). [[nodiscard]] std::string get_data_full() const; -/* - std::string get_data_section_info() const - { - return data_section_info_; - } - - std::string get_data_section_data() const - { - return data_section_data_; - } -*/ - /// Get parse error message. Call this only if parsing doesn't succeed, /// to get a friendly error message. [[nodiscard]] std::string get_error_msg() const; diff --git a/src/applib/storage_device.cpp b/src/applib/storage_device.cpp index cd73f45..44a16b9 100644 --- a/src/applib/storage_device.cpp +++ b/src/applib/storage_device.cpp @@ -19,7 +19,7 @@ Copyright: #include "app_pcrecpp.h" #include "storage_device.h" -#include "smartctl_text_parser.h" +#include "smartctl_ata_text_parser.h" #include "storage_settings.h" #include "smartctl_executor.h" @@ -145,7 +145,7 @@ std::string StorageDevice::parse_basic_data(bool do_set_properties, bool emit_si } std::string version, version_full; - if (!SmartctlTextParser::parse_version(this->info_output_, version, version_full)) // is this smartctl data at all? + if (!SmartctlAtaTextParser::parse_version(this->info_output_, version, version_full)) // is this smartctl data at all? return _("Cannot get smartctl version information."); // Detect type. note: we can't distinguish between sata and scsi (on linux, for -d ata switch). @@ -171,10 +171,10 @@ std::string StorageDevice::parse_basic_data(bool do_set_properties, bool emit_si smart_enabled_ = false; } else { - // Note: We don't use SmartctlTextParser here, because this information + // Note: We don't use SmartctlAtaTextParser here, because this information // may be in some other format. If this information is valid, only then it's - // passed to SmartctlTextParser. - // Compared to SmartctlTextParser, this one is much looser. + // passed to SmartctlAtaTextParser. + // Compared to SmartctlAtaTextParser, this one is much looser. // Don't put complete messages here - they change across smartctl versions. if (app_pcre_match("/^SMART support is:[ \\t]*Unavailable/mi", info_output_) // cdroms output this @@ -225,7 +225,7 @@ std::string StorageDevice::parse_basic_data(bool do_set_properties, bool emit_si std::string size; if (app_pcre_match("/^User Capacity:[ \\t]*(.*)$/mi", info_output_, &size)) { int64_t bytes = 0; - size_ = SmartctlTextParser::parse_byte_size(size, bytes, false); + size_ = SmartctlAtaTextParser::parse_byte_size(size, bytes, false); } @@ -237,7 +237,7 @@ std::string StorageDevice::parse_basic_data(bool do_set_properties, bool emit_si if (hdd_.has_value()) { disk_type = hdd_.value() ? AtaStorageAttribute::DiskType::Hdd : AtaStorageAttribute::DiskType::Ssd; } - SmartctlTextParser ps; + SmartctlAtaTextParser ps; if (ps.parse_full(this->info_output_, disk_type)) { // try to parse it this->set_properties(ps.get_properties()); // copy to our drive, overwriting old data } @@ -301,14 +301,14 @@ std::string StorageDevice::parse_data() if (hdd_.has_value()) { disk_type = hdd_.value() ? AtaStorageAttribute::DiskType::Hdd : AtaStorageAttribute::DiskType::Ssd; } - SmartctlTextParser ps; + SmartctlAtaTextParser ps; if (ps.parse_full(this->full_output_, disk_type)) { // try to parse it (parse only, set the properties after basic parsing). // refresh basic info too this->info_output_ = ps.get_data_full(); // put data including version information // note: this will clear the non-basic properties! - // this will parse some info that is already parsed by SmartctlTextParser::parse_full(), + // this will parse some info that is already parsed by SmartctlAtaTextParser::parse_full(), // but this one sets the StorageDevice class members, not properties. this->parse_basic_data(false, false); // don't emit signal, we're not complete yet. diff --git a/src/applib/storage_device.h b/src/applib/storage_device.h index 0feee2b..1bb3b1d 100644 --- a/src/applib/storage_device.h +++ b/src/applib/storage_device.h @@ -20,7 +20,7 @@ Copyright: #include "hz/fs_ns.h" #include "ata_storage_property.h" -#include "smartctl_text_parser.h" // prop_list_t +#include "smartctl_ata_text_parser.h" // prop_list_t #include "smartctl_executor.h" diff --git a/src/gsc_main_window.cpp b/src/gsc_main_window.cpp index f944654..5190b5c 100644 --- a/src/gsc_main_window.cpp +++ b/src/gsc_main_window.cpp @@ -21,7 +21,7 @@ Copyright: #include "hz/fs.h" #include "rconfig/rconfig.h" #include "applib/storage_detector.h" -#include "applib/smartctl_text_parser.h" +#include "applib/smartctl_ata_text_parser.h" #include "applib/gui_utils.h" // gui_show_error_dialog #include "applib/smartctl_executor.h" // get_smartctl_binary() #include "applib/smartctl_executor_gui.h" @@ -118,7 +118,7 @@ GscMainWindow::GscMainWindow(BaseObjectType* gtkcobj, Glib::RefPtr } std::string version, version_full; - if (!SmartctlTextParser::parse_version(output, version, version_full)) { + if (!SmartctlAtaTextParser::parse_version(output, version, version_full)) { error_msg = _("Smartctl returned invalid output."); break; }