From: Cheng-Ling Lai Date: Tue, 4 Aug 2026 02:12:36 +0000 (+0800) Subject: Improve error handling for CmdMetaParse X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0dac071e10d8a1bdb8e060b6757de1983014b1c7;p=thirdparty%2Fsnapper.git Improve error handling for CmdMetaParse --- diff --git a/client/snbk/CmdMetaParse.cc b/client/snbk/CmdMetaParse.cc index a5451af0..d1b8daba 100644 --- a/client/snbk/CmdMetaParse.cc +++ b/client/snbk/CmdMetaParse.cc @@ -53,6 +53,8 @@ namespace snapper y2err(tmp); for (const string& tmp : cmd.get_stderr()) y2err(tmp); + + SN_THROW(Exception(_("Failed to load info.xml."))); } content = boost::join(cmd.get_stdout(), ""); @@ -64,7 +66,10 @@ namespace snapper string CmdMetaParse::get_checksum() const { if (!content.length()) - return ""; + { + y2err(sformat("The content of %s is empty.", path.c_str())); + SN_THROW(Exception(_("The content of info.xml is empty."))); + } unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256(reinterpret_cast(content.c_str()), content.length(), @@ -83,37 +88,38 @@ namespace snapper SnapshotMeta CmdMetaParse::get_meta() const { - SnapshotMeta meta; - if (!content.length()) { - // The `info.xml` file might be missing. - // Using fallback values for snapshot metadata. - return meta; + y2err(sformat("The content of %s is empty.", path.c_str())); + SN_THROW(Exception(_("The content of info.xml is empty."))); } + SnapshotMeta meta; + XmlFile file(XmlFile::FromString, content); const xmlNode* node = file.getRootElement(); string tmp; if (!getChildValue(node, "type", tmp) || !toValue(tmp, meta.type, true)) { - // Log error for the missing attribute and retain the default value. y2err("The type attribute is missing from " << path); + SN_THROW(Exception(_("The type attribute is missing from `info.xml`."))); } getChildValue(node, "pre_num", meta.pre_num); getChildValue(node, "cleanup", meta.cleanup); - for (const xmlNode* tmp : getChildNodes(node, "userdata")) + for (const xmlNode* tmp_node : getChildNodes(node, "userdata")) { string key, value; - getChildValue(tmp, "key", key); - getChildValue(tmp, "value", value); + getChildValue(tmp_node, "key", key); + getChildValue(tmp_node, "value", value); if (!key.empty()) meta.userdata[key] = value; } + meta.state = SnapshotMeta::State::VALID; + return meta; } diff --git a/client/snbk/CmdMetaParse.h b/client/snbk/CmdMetaParse.h index 790cd3c0..5341895a 100644 --- a/client/snbk/CmdMetaParse.h +++ b/client/snbk/CmdMetaParse.h @@ -34,8 +34,27 @@ namespace snapper using std::string; - struct SnapshotMeta + class SnapshotMeta { + public: + + enum class State + { + /** + * Errors occurred while processing the content of `info.xml`. Either the file + * does not exist or some attributes are missing. The state of `SnapshotMeta` + * is invalid. + */ + INVALID, + + /** + * The content of `info.xml` has been successfully processed. The state of + * `SnapshotMeta` is valid. + */ + VALID + }; + + State state = State::INVALID; SnapshotType type = SnapshotType::SINGLE; unsigned int pre_num = 0; string cleanup;