/* * Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved. */ #include #include #include #include #include "pack.hpp" namespace keel { static ox::Error pathToInode( [[maybe_unused]] keel::Context *ctx, ox::FileSystem *dest, ox::ModelObject *obj) noexcept { auto &o = *obj; auto type = static_cast(o["type"].get()); auto &data = o["data"].get(); ox::String path; switch (type) { case ox::FileAddressType::Path: path = data["path"].get(); break; case ox::FileAddressType::ConstPath: path = data["constPath"].get(); break; case ox::FileAddressType::Inode: case ox::FileAddressType::None: return {}; } if (beginsWith(path, "uuid://")) { #ifndef OX_BARE_METAL const auto uuid = ox::StringView(path).substr(7); path = ctx->uuidToPath[uuid]; #else return OxError(1, "UUID to path conversion not supported on this platform"); #endif } oxRequire(s, dest->stat(path)); oxReturnError(o["type"].set(static_cast(ox::FileAddressType::Inode))); return data.set(2, s.inode); } static ox::Error transformFileAddressesObj(keel::Context *ctx, ox::FileSystem *dest, ox::ModelObject *obj) noexcept; static ox::Error transformFileAddressesVec(keel::Context *ctx, ox::FileSystem *dest, ox::ModelValueVector *v) noexcept; static ox::Error transformFileAddresses(keel::Context *ctx, ox::FileSystem *dest, ox::ModelValue *v) noexcept { if (v->type() == ox::ModelValue::Type::Object) { auto &obj = v->get(); return transformFileAddressesObj(ctx, dest, &obj); } else if (v->type() == ox::ModelValue::Type::Vector) { auto &vec = v->get(); return transformFileAddressesVec(ctx, dest, &vec); } return {}; } static ox::Error transformFileAddressesVec(keel::Context *ctx, ox::FileSystem *dest, ox::ModelValueVector *v) noexcept { for (auto &f : *v) { oxReturnError(transformFileAddresses(ctx, dest, &f)); } return {}; } /** * Convert path references in Claw data to inodes to save space * @return error */ static ox::Error transformFileAddressesObj(keel::Context *ctx, ox::FileSystem *dest, ox::ModelObject *obj) noexcept { if (obj->typeName() == "net.drinkingtea.ox.FileAddress" && obj->typeVersion() == 1) { return pathToInode(ctx, dest, obj); } for (auto &f : *obj) { auto &v = f->value; oxReturnError(transformFileAddresses(ctx, dest, &v)); } return {}; } static ox::Error doTransformations( [[maybe_unused]] keel::Context *ctx, [[maybe_unused]] ox::TypeStore *ts, [[maybe_unused]] ox::FileSystem *dest, [[maybe_unused]] ox::CRStringView filePath) noexcept { #ifndef OX_BARE_METAL // load file oxRequire(s, dest->stat(filePath)); // do transformations oxRequireM(buff, dest->read(s.inode)); for (auto tr : ctx->packTransforms) { oxReturnError(tr(ctx, &buff)); } // transform FileAddresses oxRequireM(obj, keel::readAsset(ts, buff)); oxReturnError(transformFileAddressesObj(ctx, dest, &obj)); oxReturnError(ox::writeClaw(obj).moveTo(&buff)); // write file to dest oxReturnError(dest->write(s.inode, buff.data(), buff.size())); return {}; #else return OxError(1, "Transformations not supported on this platform"); #endif } // claw file transformations are broken out from copy because path to inode // transformations need to be done after the copy to the new FS is complete static ox::Error transformClaw(keel::Context *ctx, ox::TypeStore *ts, ox::FileSystem *dest, ox::CRStringView path) noexcept { // copy oxTracef("pack::transformClaw", "path: {}", path); oxRequire(fileList, dest->ls(path)); for (const auto &name : fileList) { const auto filePath = ox::sfmt("{}{}", path, name); oxRequire(stat, dest->stat(filePath)); if (stat.fileType == ox::FileType::Directory) { const auto dir = ox::sfmt("{}{}/", path, name); oxReturnError(transformClaw(ctx, ts, dest, dir)); } else { oxReturnError(doTransformations(ctx, ts, dest, filePath)); } } return {}; } static ox::Error verifyFile(ox::FileSystem *fs, ox::CRStringView path, const ox::Buffer &expected) noexcept { ox::Buffer buff(expected.size()); oxReturnError(fs->read(path, buff.data(), buff.size())); return OxError(buff == expected ? 0 : 1); } struct VerificationPair { ox::String path; ox::Buffer buff; VerificationPair(ox::String &&pPath, ox::Buffer &&pBuff) noexcept: path(ox::forward(pPath)), buff(ox::forward(pBuff)) { } }; static ox::Error copy(ox::FileSystem *src, ox::FileSystem *dest, ox::CRStringView path, ox::Vector *verificationPairs) noexcept { oxOutf("copying directory: {}\n", path); // copy oxRequire(fileList, src->ls(path)); for (const auto &name : fileList) { auto currentFile = ox::sfmt("{}{}", path, name); if (beginsWith(name, ".")) { continue; } oxOutf("reading {}\n", currentFile); oxRequire(stat, src->stat(currentFile)); if (stat.fileType == ox::FileType::Directory) { oxReturnError(dest->mkdir(currentFile, true)); oxReturnError(copy(src, dest, currentFile + '/', verificationPairs)); } else { // load file oxRequireM(buff, src->read(currentFile)); // write file to dest oxOutf("writing {}\n", currentFile); oxReturnError(dest->write(currentFile, buff.data(), buff.size())); oxReturnError(verifyFile(dest, currentFile, buff)); verificationPairs->emplace_back(std::move(currentFile), std::move(buff)); } } return {}; } static ox::Error copyFS(ox::FileSystem *src, ox::FileSystem *dest) noexcept { ox::Vector verificationPairs; oxReturnError(copy(src, dest, "/", &verificationPairs)); // verify all at once in addition to right after the files are written oxOut("Verifying completed destination\n"); for (const auto &v : verificationPairs) { oxReturnError(verifyFile(dest, v.path, v.buff)); } return {}; } ox::Error pack(keel::Context *ctx, ox::TypeStore *ts, ox::FileSystem *dest) noexcept { oxReturnError(copyFS(ctx->rom.get(), dest)); oxOut("Doing transforms\n"); oxReturnError(transformClaw(ctx, ts, dest, "/")); return {}; } }