Skip to content

Commit

Permalink
:feat: add some oprations for layer
Browse files Browse the repository at this point in the history
add some class to manage layer.

Log:
  • Loading branch information
kamiyadm committed Dec 20, 2023
1 parent 2dc8bd0 commit d91205e
Show file tree
Hide file tree
Showing 14 changed files with 601 additions and 0 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ pfl_add_library(
./src/linglong/package/ref.h
./src/linglong/package_manager/package_manager.cpp
./src/linglong/package_manager/package_manager.h
./src/linglong/package/layer/LayerInfo.hpp
./src/linglong/package/layer/Generators.hpp
./src/linglong/package/layer_dir.h
./src/linglong/package/layer_dir.cpp
./src/linglong/package/layer_file.h
./src/linglong/package/layer_file.cpp
./src/linglong/package/layer_info.h
./src/linglong/package/layer_info.cpp
./src/linglong/package/layer_package.h
./src/linglong/package/layer_package.cpp
./src/linglong/repo/ostree_repo.cpp
./src/linglong/repo/ostree_repo.h
./src/linglong/repo/repo.cpp
Expand Down
17 changes: 17 additions & 0 deletions api/JSON Schema/LayerInfo.json
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"
]
}
41 changes: 41 additions & 0 deletions src/linglong/package/layer/Generators.hpp
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
39 changes: 39 additions & 0 deletions src/linglong/package/layer/LayerInfo.hpp
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
42 changes: 42 additions & 0 deletions src/linglong/package/layer/helper.hpp
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
49 changes: 49 additions & 0 deletions src/linglong/package/layer_dir.cpp
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;
}

}
32 changes: 32 additions & 0 deletions src/linglong/package/layer_dir.h
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_ */
78 changes: 78 additions & 0 deletions src/linglong/package/layer_file.cpp
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
39 changes: 39 additions & 0 deletions src/linglong/package/layer_file.h
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_ */
Loading

0 comments on commit d91205e

Please sign in to comment.