forked from OpenAtom-Linyaps/linyaps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add some class to manage layer. Log:
- Loading branch information
Showing
14 changed files
with
601 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"description": "Configuration file for layer.", | ||
"type": "object", | ||
"properties": { | ||
"version": { | ||
"type": "string" | ||
}, | ||
"info": { | ||
"type": "object" | ||
} | ||
}, | ||
"required": [ | ||
"version", | ||
"info" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Thish file is generated by /tools/run-quicktype.sh | ||
// DO NOT EDIT IT. | ||
|
||
// clang-format off | ||
|
||
// To parse this JSON data, first install | ||
// | ||
// json.hpp https://github.com/nlohmann/json | ||
// | ||
// Then include this file, and then do | ||
// | ||
// Generators.hpp data = nlohmann::json::parse(jsonString); | ||
|
||
#pragma once | ||
|
||
#include <nlohmann/json.hpp> | ||
#include "linglong/package/layer/helper.hpp" | ||
|
||
#include "linglong/package/layer/LayerInfo.hpp" | ||
|
||
namespace linglong { | ||
namespace package { | ||
namespace layer { | ||
void from_json(const json & j, LayerInfo & x); | ||
void to_json(json & j, const LayerInfo & x); | ||
|
||
inline void from_json(const json & j, LayerInfo& x) { | ||
x.info = j.at("info").get<std::map<std::string, nlohmann::json>>(); | ||
x.version = j.at("version").get<std::string>(); | ||
} | ||
|
||
inline void to_json(json & j, const LayerInfo & x) { | ||
j = json::object(); | ||
j["info"] = x.info; | ||
j["version"] = x.version; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// clang-format on |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Thish file is generated by /tools/run-quicktype.sh | ||
// DO NOT EDIT IT. | ||
|
||
// clang-format off | ||
|
||
// To parse this JSON data, first install | ||
// | ||
// json.hpp https://github.com/nlohmann/json | ||
// | ||
// Then include this file, and then do | ||
// | ||
// LayerInfo.hpp data = nlohmann::json::parse(jsonString); | ||
|
||
#pragma once | ||
|
||
#include <nlohmann/json.hpp> | ||
#include "linglong/package/layer/helper.hpp" | ||
|
||
namespace linglong { | ||
namespace package { | ||
namespace layer { | ||
/** | ||
* Configuration file for layer. | ||
*/ | ||
|
||
using nlohmann::json; | ||
|
||
/** | ||
* Configuration file for layer. | ||
*/ | ||
struct LayerInfo { | ||
std::map<std::string, nlohmann::json> info; | ||
std::string version; | ||
}; | ||
} | ||
} | ||
} | ||
|
||
// clang-format on |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Thish file is generated by /tools/run-quicktype.sh | ||
// DO NOT EDIT IT. | ||
|
||
// clang-format off | ||
|
||
// To parse this JSON data, first install | ||
// | ||
// json.hpp https://github.com/nlohmann/json | ||
// | ||
// Then include this file, and then do | ||
// | ||
// helper.hpp data = nlohmann::json::parse(jsonString); | ||
|
||
#pragma once | ||
|
||
#include <nlohmann/json.hpp> | ||
|
||
#include <sstream> | ||
|
||
namespace linglong { | ||
namespace package { | ||
namespace layer { | ||
using nlohmann::json; | ||
|
||
#ifndef NLOHMANN_UNTYPED_linglong_package_layer_HELPER | ||
#define NLOHMANN_UNTYPED_linglong_package_layer_HELPER | ||
inline json get_untyped(const json & j, const char * property) { | ||
if (j.find(property) != j.end()) { | ||
return j.at(property).get<json>(); | ||
} | ||
return json(); | ||
} | ||
|
||
inline json get_untyped(const json & j, std::string property) { | ||
return get_untyped(j, property.data()); | ||
} | ||
#endif | ||
} | ||
} | ||
} | ||
|
||
// clang-format on |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
*/ | ||
|
||
#include "linglong/package/layer_dir.h" | ||
#include "linglong/util/qserializer/json.h" | ||
|
||
namespace linglong::package { | ||
|
||
LayerDir::~LayerDir() | ||
{ | ||
if(cleanup) { | ||
this->removeRecursively(); | ||
} | ||
} | ||
|
||
void LayerDir::setCleanStatus(bool status) | ||
{ | ||
this->cleanup = status; | ||
} | ||
|
||
utils::error::Result<QSharedPointer<Info>> LayerDir::info() const | ||
{ | ||
const auto infoPath = QStringList { this->absolutePath(), "info.json"}.join(QDir::separator()); | ||
auto [info, err] = util::fromJSON<QSharedPointer<Info>>(infoPath); | ||
if (err) { | ||
return LINGLONG_ERR(err.code(), "failed to parse info.json"); | ||
} | ||
|
||
return info; | ||
} | ||
|
||
utils::error::Result<QByteArray> LayerDir::rawInfo() const | ||
{ | ||
const auto infoPath = QStringList { this->absolutePath(), "info.json"}.join(QDir::separator()); | ||
QFile file(infoPath); | ||
if (!file.open(QIODevice::ReadOnly)) { | ||
return LINGLONG_ERR(-1, "failed to open info.json from layer dir"); | ||
} | ||
|
||
QByteArray rawData = file.readAll(); | ||
|
||
file.close(); | ||
return rawData; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
*/ | ||
|
||
#ifndef LINGLONG_PACKAGE_LAYER_DIR_H_ | ||
#define LINGLONG_PACKAGE_LAYER_DIR_H_ | ||
|
||
#include "linglong/package/info.h" | ||
#include "linglong/utils/error/error.h" | ||
|
||
#include <QDir> | ||
|
||
namespace linglong::package { | ||
|
||
class LayerDir : public QDir | ||
{ | ||
public: | ||
using QDir::QDir; | ||
~LayerDir(); | ||
utils::error::Result<QSharedPointer<Info>> info() const; | ||
utils::error::Result<QByteArray> rawInfo() const; | ||
void setCleanStatus(bool status); | ||
|
||
private: | ||
bool cleanup = true; | ||
}; | ||
|
||
} // namespace linglong::package | ||
|
||
#endif /* LINGLONG_PACKAGE_LAYER_DIR_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
*/ | ||
|
||
#include "linglong/package/layer_file.h" | ||
#include "linglong/package/layer_info.h" | ||
|
||
#include <QFileInfo> | ||
|
||
namespace linglong::package { | ||
|
||
using nlohmann::json; | ||
|
||
LayerFile::~LayerFile() | ||
{ | ||
if (this->cleanup) { | ||
this->remove(); | ||
} | ||
} | ||
|
||
void LayerFile::setCleanStatus(bool status) | ||
{ | ||
this->cleanup = status; | ||
} | ||
|
||
utils::error::Result<layer::LayerInfo> LayerFile::layerFileInfo() | ||
{ | ||
auto ret = layerInfoSize(); | ||
if (!ret.has_value()) { | ||
return LINGLONG_EWRAP("failed to get layer file size ", ret.error()); | ||
} | ||
|
||
auto rawData = this->read(qint64(*ret)); | ||
|
||
auto layerInfo = fromJson(rawData); | ||
if (!layerInfo.has_value()) { | ||
return LINGLONG_EWRAP("failed to get layer info", layerInfo.error()); | ||
} | ||
|
||
return layerInfo; | ||
} | ||
|
||
utils::error::Result<quint32> LayerFile::layerInfoSize() | ||
{ | ||
if (!this->isOpen() && !this->open(QIODevice::ReadOnly)) { | ||
return LINGLONG_ERR(-1, "failed to open layer file"); | ||
} | ||
// read from offset 0 everytime | ||
this->seek(0); | ||
|
||
quint32 layerInfoSize; | ||
this->read(reinterpret_cast<char *>(&layerInfoSize), sizeof(quint32)); | ||
|
||
return layerInfoSize; | ||
} | ||
|
||
utils::error::Result<quint32> LayerFile::layerOffset() | ||
{ | ||
auto size = layerInfoSize(); | ||
if (!size.has_value()) { | ||
return LINGLONG_EWRAP("get LayerInfo size failed", size.error()); | ||
} | ||
|
||
return *size + sizeof(quint32); | ||
} | ||
|
||
utils::error::Result<void> LayerFile::saveTo(const QString &destination) | ||
{ | ||
if (!this->copy(destination)) { | ||
return LINGLONG_ERR(-1, QString("failed to save layer file to %1").arg(destination)); | ||
} | ||
|
||
return LINGLONG_OK; | ||
} | ||
|
||
} // namespace linglong::package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
*/ | ||
|
||
#ifndef LINGLONG_PACKAGE_LAYER_FILE_H_ | ||
#define LINGLONG_PACKAGE_LAYER_FILE_H_ | ||
|
||
#include "linglong/package/layer/LayerInfo.hpp" | ||
#include "linglong/utils/error/error.h" | ||
|
||
#include <QFile> | ||
|
||
namespace linglong::package { | ||
|
||
// layer file [LayerInfoSize | LayerInfo | compressedData] | ||
class LayerFile : public QFile | ||
{ | ||
public: | ||
using QFile::QFile; | ||
~LayerFile(); | ||
utils::error::Result<layer::LayerInfo> layerFileInfo(); | ||
|
||
utils::error::Result<quint32> layerOffset(); | ||
|
||
utils::error::Result<void> saveTo(const QString &destination); | ||
|
||
void setCleanStatus(bool status); | ||
|
||
private: | ||
utils::error::Result<quint32> layerInfoSize(); | ||
|
||
bool cleanup = false; | ||
}; | ||
|
||
} // namespace linglong::package | ||
|
||
#endif /* LINGLONG_PACKAGE_LAYER_FILE_H_ */ |
Oops, something went wrong.