#include "parsebackuplist.h" #include #include #include #include /* 21-07-21 14:14:01 {beecb746-561f-4fa1-99ba-19fb849a1ba7} 24.26KB backup finished 2 */ #define BACKUPLIST "backupList" #define BACKUPPOINT "BackupPoint" #define COMMENT "Comment" #define TIME "Time" #define UUID "Uuid" #define SIZE "Size" #define STATE "State" #define TYPE "Type" #define POSITION "Position" #define STATUE_BACKUP_FINESHED "backup finished" ParseBackupList::ParseBackupList(const QString& xmlPath) : m_xmlPath(xmlPath) { QFile xmlFile(m_xmlPath); if (!xmlFile.exists() || 0 == xmlFile.size()) { InitXml(); } } /** * @brief 初始化xml文件 * @return */ bool ParseBackupList::InitXml() { QFile xmlFile(m_xmlPath); if (!xmlFile.open(QIODevice::ReadWrite | QIODevice::Truncate)) return false; QDomDocument doc; QDomProcessingInstruction ins = doc.createProcessingInstruction("xml", "version=\'1.0\'"); doc.appendChild(ins); QDomElement root = doc.createElement(BACKUPLIST); doc.appendChild(root); QTextStream out(&xmlFile); doc.save(out, QDomNode::NodeType::CDATASectionNode); xmlFile.close(); return true; } /** * @brief 校验xml格式是否正确 * @return */ bool ParseBackupList::isXmlCorrect() { QFile xmlFile(m_xmlPath); if (!xmlFile.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Open file " << m_xmlPath << " failure"; return false; } QXmlStreamReader reader(&xmlFile); while (!reader.atEnd()) { if (reader.isStartElement()) { } reader.readNext(); } if (reader.hasError()) { qDebug() << "xml parse error!"; xmlFile.close(); return false; } xmlFile.close(); return true; } /** * @brief 读取xml文件 * @param doc * @return */ bool ParseBackupList::Doc_setContent(QDomDocument& doc) { QFile xmlFile(m_xmlPath); if (!xmlFile.open(QIODevice::ReadOnly)) { qDebug() << " open backuplist.xml failed!"; return false; } QString errStr; int errLine; int errCol; if (!doc.setContent(&xmlFile, false, &errStr, &errLine, &errCol)) { qDebug() << QString("parse backuplist.xml error at line %1, column %2:%3").arg(errLine).arg(errCol).arg(errStr); xmlFile.close(); return false; } xmlFile.close(); return true; } /** * @brief 出厂备份修改backuplist.xml,仅保留出厂备份信息 * @param factoryBackupUuid,出厂备份的uuid * @return ParseResult枚举类型 * @author zhaominyong * @since 2021/06/27 */ ParseBackupList::ParseResult ParseBackupList::updateForFactoryRestore(const QString& factoryBackupUuid) { QDomDocument doc; if (!Doc_setContent(doc)) return XML_PARSE_ERR; QDomElement root = doc.documentElement(); QDomNodeList list = root.childNodes(); for (int i = 0; i < list.count(); i++) { QDomNode node = list.at(i); if (!node.isElement()) continue ; QDomElement element = node.toElement(); QDomNodeList nodes = element.elementsByTagName(UUID); if (0 < nodes.count()) { QDomElement uuidElement = nodes.at(0).toElement(); QString tag = uuidElement.tagName(); QString text = uuidElement.text(); if (uuidElement.text() != factoryBackupUuid) { root.removeChild(node); --i; } } } QFile xmlFile(m_xmlPath); if (!xmlFile.open(QIODevice::WriteOnly)) { qDebug() << "update state failed"; return FAIL; } QTextStream out(&xmlFile); doc.save(out, QDomNode::NodeType::EntityReferenceNode); out.flush(); xmlFile.close(); return SUCCESS; } /** * @brief 根据comment查找uuid * @param comment * @return uuid */ QString ParseBackupList::findUuidByComment(const QString& comment) { QDomDocument doc; if (!Doc_setContent(doc)) return ""; QDomElement root = doc.documentElement(); QDomNodeList list = root.childNodes(); for (int i = 0; i < list.count(); i++) { QDomNode node = list.at(i); if (!node.isElement()) continue; QDomElement eleComment = node.firstChildElement(COMMENT); if (eleComment.isNull()) continue; if (comment != eleComment.text()) continue; QDomElement eleUuid = node.firstChildElement(UUID); if (eleUuid.isNull()) return ""; return eleUuid.text(); } return ""; } /** * @brief 根据Uuid查找备份点信息 * @param Uuid * @return 备份点信息 */ ParseBackupList::BackupPoint ParseBackupList::findBackupPointByUuid(const QString& Uuid) { BackupPoint backupPoint; QDomDocument doc; if (!Doc_setContent(doc)) return backupPoint; QDomElement root = doc.documentElement(); QDomNodeList list = root.childNodes(); for (int i = 0; i < list.count(); i++) { QDomNode node = list.at(i); if (!node.isElement()) continue; QDomElement eleUuid = node.firstChildElement(UUID); if (eleUuid.isNull() || Uuid != eleUuid.text()) continue; elementNodeToBackupPoint(node.toElement(), backupPoint); return backupPoint; } return backupPoint; } /** * @brief 获取最后一次系统备份 * @return */ ParseBackupList::BackupPoint ParseBackupList::getLastSysBackupPoint() { BackupPoint backupPoint; QDomDocument doc; if (!Doc_setContent(doc)) return backupPoint; QDomElement root = doc.documentElement(); QDomNodeList list = root.childNodes(); for (int i = 0; i < list.count(); i++) { QDomNode node = list.at(i); if (!node.isElement()) continue; QDomElement eleType = node.firstChildElement(TYPE); if (eleType.isNull() || (BackupType::BACKUP_SYSTEM != eleType.text().toInt() && BackupType::INC_BACKUP_SYSTEM != eleType.text().toInt())) continue; QDomElement eleState = node.firstChildElement(STATE); QString type = eleState.text(); if (eleState.isNull() || eleState.text() != QString(STATUE_BACKUP_FINESHED)) continue; elementNodeToBackupPoint(node.toElement(), backupPoint); } return backupPoint; } /** * @brief elementNode --> BackupPoint * @param node, QDomElement * @param backupPoint, BackupPoint */ void ParseBackupList::elementNodeToBackupPoint(const QDomElement& node, BackupPoint& backupPoint) { QDomElement eleUuid = node.firstChildElement(UUID); if (!eleUuid.isNull()) backupPoint.m_uuid = eleUuid.text(); QDomElement eleComment = node.firstChildElement(COMMENT); if (!eleComment.isNull()) backupPoint.m_backupName = eleComment.text(); QDomElement eleTime = node.firstChildElement(TIME); if (!eleTime.isNull()) backupPoint.m_time = eleTime.text(); QDomElement eleSize = node.firstChildElement(SIZE); if (!eleSize.isNull()) backupPoint.m_size = eleSize.text(); QDomElement eleState = node.firstChildElement(STATE); if (!eleState.isNull()) backupPoint.m_state = eleState.text(); QDomElement eleType = node.firstChildElement(TYPE); if (!eleType.isNull()) backupPoint.m_type = eleType.text().toInt(); QDomElement elePosition = node.firstChildElement(POSITION); if (!elePosition.isNull()) backupPoint.m_iPosition = elePosition.text().toInt(); } /** * @brief backupPoint --> ElementNode * @param backupPoint, BackupPoint * @param node, QDomElement */ void ParseBackupList::backupPointToElementNode(const BackupPoint& backupPoint, QDomElement& node) { node.appendChild(createTextElement(COMMENT, backupPoint.m_backupName)); node.appendChild(createTextElement(TIME, backupPoint.m_time)); node.appendChild(createTextElement(UUID, backupPoint.m_uuid)); node.appendChild(createTextElement(SIZE, backupPoint.m_size)); node.appendChild(createTextElement(STATE, backupPoint.m_state)); node.appendChild(createTextElement(TYPE, QString::number(backupPoint.m_type))); node.appendChild(createTextElement(POSITION, QString::number(backupPoint.m_iPosition))); } /** * @brief createTextElement * @param tagName * @param text * @return text */ QDomElement ParseBackupList::createTextElement(const QString& tagName, const QString& text) { QDomElement node; node.setTagName(tagName); QDomText textNode; textNode.setData(text); node.appendChild(textNode); return node; }