2021-08-17 10:07:35 +08:00
|
|
|
|
#include "parsebackuplist.h"
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QTextStream>
|
|
|
|
|
#include <QXmlStreamReader>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
<?xml version='1.0'?>
|
|
|
|
|
<backupList>
|
|
|
|
|
<BackupPoint>
|
|
|
|
|
<Comment>21-07-21 14:14:01</Comment>
|
|
|
|
|
<Time>21-07-21 14:14:03</Time>
|
|
|
|
|
<Uuid>{beecb746-561f-4fa1-99ba-19fb849a1ba7}</Uuid>
|
|
|
|
|
<Size>24.26KB</Size>
|
|
|
|
|
<State>backup finished</State>
|
|
|
|
|
<Type>2</Type>
|
|
|
|
|
</BackupPoint>
|
|
|
|
|
</backupList>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
*/
|
2021-08-26 20:12:48 +08:00
|
|
|
|
void ParseBackupList::backupPointToElementNode(const BackupPoint& backupPoint, QDomDocument& doc, QDomNode& node)
|
2021-08-17 10:07:35 +08:00
|
|
|
|
{
|
2021-08-26 20:12:48 +08:00
|
|
|
|
node.appendChild(createTextElement(doc, COMMENT, backupPoint.m_backupName));
|
|
|
|
|
node.appendChild(createTextElement(doc, TIME, backupPoint.m_time));
|
|
|
|
|
node.appendChild(createTextElement(doc, UUID, backupPoint.m_uuid));
|
|
|
|
|
node.appendChild(createTextElement(doc, SIZE, backupPoint.m_size));
|
|
|
|
|
node.appendChild(createTextElement(doc, STATE, backupPoint.m_state));
|
|
|
|
|
node.appendChild(createTextElement(doc, TYPE, QString::number(backupPoint.m_type)));
|
|
|
|
|
node.appendChild(createTextElement(doc, POSITION, QString::number(backupPoint.m_iPosition)));
|
2021-08-17 10:07:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief createTextElement
|
|
|
|
|
* @param tagName
|
|
|
|
|
* @param text
|
|
|
|
|
* @return <tagName>text</tagName>
|
|
|
|
|
*/
|
2021-08-26 20:12:48 +08:00
|
|
|
|
QDomElement ParseBackupList::createTextElement(QDomDocument& doc, const QString& tagName, const QString& text)
|
2021-08-17 10:07:35 +08:00
|
|
|
|
{
|
2021-08-26 20:12:48 +08:00
|
|
|
|
QDomElement node = doc.createElement(tagName);
|
|
|
|
|
QDomText textNode = doc.createTextNode(text);
|
2021-08-17 10:07:35 +08:00
|
|
|
|
node.appendChild(textNode);
|
|
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
}
|
2021-08-26 20:12:48 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 新增备份节点
|
|
|
|
|
* @param backupPoint, 备份点信息
|
|
|
|
|
* @return SUCCESS成功; XML_PARSE_ERR失败
|
|
|
|
|
*/
|
|
|
|
|
ParseBackupList::ParseResult ParseBackupList::addItem(const BackupPoint& backupPoint)
|
|
|
|
|
{
|
|
|
|
|
QDomDocument doc;
|
|
|
|
|
if (!Doc_setContent(doc))
|
|
|
|
|
return XML_PARSE_ERR;
|
|
|
|
|
|
|
|
|
|
QDomElement backupPointNode = doc.createElement(BACKUPPOINT);
|
|
|
|
|
backupPointToElementNode(backupPoint, doc, backupPointNode);
|
|
|
|
|
|
|
|
|
|
QDomElement root = doc.documentElement();
|
|
|
|
|
root.appendChild(backupPointNode);
|
|
|
|
|
|
|
|
|
|
QFile xmlFile(m_xmlPath);
|
|
|
|
|
if (!xmlFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
|
|
|
|
|
return FAIL;
|
|
|
|
|
|
|
|
|
|
QTextStream out(&xmlFile);
|
|
|
|
|
doc.save(out, QDomNode::NodeType::CDATASectionNode);
|
|
|
|
|
xmlFile.close();
|
|
|
|
|
return SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 更新备份点信息
|
|
|
|
|
* @param backupPoint,新的备份点信息
|
|
|
|
|
* @return SUCCESS成功; XML_PARSE_ERR失败
|
|
|
|
|
*/
|
|
|
|
|
ParseBackupList::ParseResult ParseBackupList::updateItem(const BackupPoint & backupPoint)
|
|
|
|
|
{
|
|
|
|
|
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 eleUuid = node.firstChildElement(UUID);
|
|
|
|
|
if (eleUuid.isNull() || backupPoint.m_uuid != eleUuid.text())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
QDomElement newNode = doc.createElement(BACKUPPOINT);
|
|
|
|
|
backupPointToElementNode(backupPoint, doc, newNode);
|
|
|
|
|
root.replaceChild(newNode, node);
|
|
|
|
|
|
|
|
|
|
break ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFile xmlFile(m_xmlPath);
|
|
|
|
|
if (!xmlFile.open(QIODevice::WriteOnly)) {
|
|
|
|
|
return FAIL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextStream out(&xmlFile);
|
|
|
|
|
doc.save(out, QDomNode::NodeType::EntityReferenceNode);
|
|
|
|
|
xmlFile.close();
|
|
|
|
|
return SUCCESS;
|
|
|
|
|
}
|