Skip to content

Commit

Permalink
Initial refactor of image vault utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Sploder12 committed Jan 17, 2025
1 parent cbcb5e7 commit 610b326
Show file tree
Hide file tree
Showing 17 changed files with 390 additions and 142 deletions.
3 changes: 3 additions & 0 deletions include/multipass/file_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class FileOps : public Singleton<FileOps>
virtual qint64 write(QFileDevice& file, const QByteArray& data) const;
virtual bool flush(QFile& file) const;

virtual QString remove_extension(const QString& path) const;
virtual bool copy(const QString& from, const QString& to) const;

// QSaveFile operations
virtual bool commit(QSaveFile& file) const;

Expand Down
8 changes: 0 additions & 8 deletions include/multipass/vm_image_vault.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ namespace multipass
class VMImageHost;
namespace vault
{
// Helper functions and classes for all image vault types
QString filename_for(const Path& path);
QString copy(const QString& file_name, const QDir& output_dir);
void delete_file(const Path& path);
QString compute_image_hash(const Path& image_path);
void verify_image_download(const Path& image_path, const QString& image_hash);
QString extract_image(const Path& image_path, const ProgressMonitor& monitor, const bool delete_file = false);
std::unordered_map<std::string, VMImageHost*> configure_image_host_map(const std::vector<VMImageHost*>& image_hosts);

class DeleteOnException
{
Expand Down
87 changes: 87 additions & 0 deletions include/multipass/vm_image_vault_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (C) Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#ifndef MULTIPASS_VM_IMAGE_VAULT_UTILS_H
#define MULTIPASS_VM_IMAGE_VAULT_UTILS_H

#include "file_ops.h"
#include "xz_image_decoder.h"

#include <string>
#include <unordered_map>
#include <vector>

#define MP_IMAGE_VAULT_UTILS \
multipass::ImageVaultUtils \
{ \
}

namespace multipass
{
class VMImageHost;

class ImageVaultUtils
{
public:
using DefaultDecoder = XzImageDecoder;

static QString copy_to_dir(const QString& file, const QDir& output_dir);
static QString compute_hash(QIODevice& device);
static QString compute_file_hash(const QString& file);

template <class Hasher = ImageVaultUtils>
static void verify_file_hash(const QString& file, const QString& hash, const Hasher& hasher = ImageVaultUtils{});

template <class Decoder = DefaultDecoder>
static QString extract_file(const QString& file,
const ProgressMonitor& monitor,
bool delete_original = false,
const Decoder& decoder = DefaultDecoder{});

using HostMap = std::unordered_map<std::string, VMImageHost*>;
using Hosts = std::vector<VMImageHost*>;

static HostMap configure_image_host_map(const Hosts& image_hosts);
};

template <class Hasher>
void ImageVaultUtils::verify_file_hash(const QString& file, const QString& hash, const Hasher& hasher)
{
auto file_hash = hasher.compute_file_hash(file);
}

template <class Decoder>
QString ImageVaultUtils::extract_file(const QString& file,
const ProgressMonitor& monitor,
bool delete_original,
const Decoder& decoder)
{
auto new_path = MP_FILEOPS.remove_extension(file);
decoder.decode_to(file, new_path, monitor);

if (delete_original)
{
QFile qfile{file};
MP_FILEOPS.remove(qfile);
}

return new_path;
}

} // namespace multipass

#endif // MULTIPASS_VM_IMAGE_VAULT_UTILS_H
5 changes: 2 additions & 3 deletions include/multipass/xz_image_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ namespace multipass
class XzImageDecoder
{
public:
XzImageDecoder(const Path& xz_file_path);
XzImageDecoder();

void decode_to(const Path& decoded_file_path, const ProgressMonitor& monitor);
void decode_to(const Path& xz_file_path, const Path& decoded_file_path, const ProgressMonitor& monitor) const;

using XzDecoderUPtr = std::unique_ptr<xz_dec, decltype(xz_dec_end)*>;

private:
QFile xz_file;
XzDecoderUPtr xz_decoder;
};
} // namespace multipass
Expand Down
6 changes: 5 additions & 1 deletion src/daemon/default_vm_image_vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <QtConcurrent/QtConcurrent>

#include <exception>
#include <multipass/file_ops.h>

namespace mp = multipass;
namespace mpl = multipass::logging;
Expand Down Expand Up @@ -170,7 +171,10 @@ void remove_source_images(const mp::VMImage& source_image, const mp::VMImage& pr
{
// The prepare phase may have been a no-op, check and only remove source images
if (source_image.image_path != prepared_image.image_path)
mp::vault::delete_file(source_image.image_path);
{
QFile source_file{source_image.image_path};
MP_FILEOPS.remove(source_file);
}
}

void delete_image_dir(const mp::Path& image_path)
Expand Down
4 changes: 3 additions & 1 deletion src/platform/backends/lxd/lxd_vm_image_vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <QTemporaryDir>

#include <chrono>
#include <multipass/file_ops.h>
#include <thread>

namespace mp = multipass;
Expand Down Expand Up @@ -90,7 +91,8 @@ QString post_process_downloaded_image(const QString& image_path, const mp::Progr

if (original_image_path != new_image_path)
{
mp::vault::delete_file(original_image_path);
QFile original_file{original_image_path};
MP_FILEOPS.remove(original_file);
}

return new_image_path;
Expand Down
11 changes: 11 additions & 0 deletions src/utils/file_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ bool mp::FileOps::flush(QFile& file) const
return file.flush();
}

QString mp::FileOps::remove_extension(const QString& path) const
{
QFileInfo info{path};
return info.dir().path() + info.completeBaseName();
}

bool mp::FileOps::copy(const QString& from, const QString& to) const
{
return QFile::copy(from, to);
}

bool mp::FileOps::commit(QSaveFile& file) const
{
return file.commit();
Expand Down
10 changes: 0 additions & 10 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,6 @@ void mp::utils::validate_server_address(const std::string& address)
throw std::runtime_error(fmt::format("invalid port number in address '{}'", address));
}

std::string mp::utils::filename_for(const std::string& path)
{
return QFileInfo(QString::fromStdString(path)).fileName().toStdString();
}

bool mp::utils::is_dir(const std::string& path)
{
return QFileInfo(QString::fromStdString(path)).isDir();
}

std::string mp::utils::match_line_for(const std::string& output, const std::string& matcher)
{
std::istringstream ss{output};
Expand Down
73 changes: 11 additions & 62 deletions src/utils/vm_image_vault_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <multipass/format.h>
#include <multipass/vm_image_host.h>
#include <multipass/vm_image_vault.h>
#include <multipass/vm_image_vault_utils.h>
#include <multipass/xz_image_decoder.h>

#include <QCryptographicHash>
Expand All @@ -27,77 +28,25 @@

namespace mp = multipass;

QString mp::vault::filename_for(const mp::Path& path)
QString mp::ImageVaultUtils::copy_to_dir(const QString& file, const QDir& output_dir)
{
QFileInfo file_info(path);
return file_info.fileName();
return "";
}

QString mp::vault::copy(const QString& file_name, const QDir& output_dir)
QString mp::ImageVaultUtils::compute_hash(QIODevice& device)
{
if (file_name.isEmpty())
return {};

if (!QFileInfo::exists(file_name))
throw std::runtime_error(fmt::format("{} missing", file_name));

QFileInfo info{file_name};
const auto source_name = info.fileName();
auto new_path = output_dir.filePath(source_name);
QFile::copy(file_name, new_path);
return new_path;
}

void mp::vault::delete_file(const mp::Path& path)
{
QFile file{path};
file.remove();
}

QString mp::vault::compute_image_hash(const mp::Path& image_path)
{
QFile image_file(image_path);
if (!image_file.open(QFile::ReadOnly))
{
throw std::runtime_error("Cannot open image file for computing hash");
}

QCryptographicHash hash(QCryptographicHash::Sha256);
if (!hash.addData(&image_file))
{
throw std::runtime_error("Cannot read image file to compute hash");
}

return hash.result().toHex();
}

void mp::vault::verify_image_download(const mp::Path& image_path, const QString& image_hash)
{
auto computed_hash = compute_image_hash(image_path);

if (computed_hash != image_hash)
{
throw std::runtime_error("Downloaded image hash does not match");
}
return "";
}

QString mp::vault::extract_image(const mp::Path& image_path, const mp::ProgressMonitor& monitor, const bool delete_file)
QString mp::ImageVaultUtils::compute_file_hash(const QString& path)
{
mp::XzImageDecoder xz_decoder(image_path);
QString new_image_path{image_path};

new_image_path.remove(".xz");

xz_decoder.decode_to(new_image_path, monitor);

mp::vault::delete_file(image_path);

return new_image_path;
return "";
}

std::unordered_map<std::string, mp::VMImageHost*>
mp::vault::configure_image_host_map(const std::vector<mp::VMImageHost*>& image_hosts)
mp::ImageVaultUtils::HostMap mp::ImageVaultUtils::configure_image_host_map(const Hosts& image_hosts)
{
return {};
/*
std::unordered_map<std::string, mp::VMImageHost*> remote_image_host_map;
for (const auto& image_host : image_hosts)
Expand All @@ -108,5 +57,5 @@ mp::vault::configure_image_host_map(const std::vector<mp::VMImageHost*>& image_h
}
}
return remote_image_host_map;
return remote_image_host_map;*/
}
8 changes: 5 additions & 3 deletions src/xz_decoder/xz_image_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ bool verify_decode(const xz_ret& ret)
}
} // namespace

mp::XzImageDecoder::XzImageDecoder(const Path& xz_file_path)
: xz_file{xz_file_path}, xz_decoder{xz_dec_init(XZ_DYNALLOC, 1u << 26), xz_dec_end}
mp::XzImageDecoder::XzImageDecoder() : xz_decoder{xz_dec_init(XZ_DYNALLOC, 1u << 26), xz_dec_end}
{
xz_crc32_init();
xz_crc64_init();
}

void mp::XzImageDecoder::decode_to(const Path& decoded_image_path, const ProgressMonitor& monitor)
void mp::XzImageDecoder::decode_to(const Path& xz_file_path,
const Path& decoded_image_path,
const ProgressMonitor& monitor) const
{
QFile xz_file{xz_file_path};
if (!xz_file.open(QIODevice::ReadOnly))
throw std::runtime_error(fmt::format("failed to open {} for reading", xz_file.fileName()));

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ add_executable(multipass_tests
test_sftp_utils.cpp
test_file_ops.cpp
test_recursive_dir_iter.cpp
test_image_vault_utils.cpp
)

target_include_directories(multipass_tests
Expand Down
3 changes: 3 additions & 0 deletions tests/mock_file_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class MockFileOps : public FileOps
MOCK_METHOD(qint64, write, (QFileDevice&, const QByteArray&), (const, override));
MOCK_METHOD(bool, flush, (QFile & file), (const, override));

MOCK_METHOD(QString, remove_extension, (const QString& path), (const, override));
MOCK_METHOD(bool, copy, (const QString&, const QString&), (const, override));

// QSaveFile mock methods
MOCK_METHOD(bool, commit, (QSaveFile&), (const, override));

Expand Down
37 changes: 37 additions & 0 deletions tests/mock_image_decoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#ifndef MULTIPASS_MOCK_IMAGE_DECODER_H
#define MULTIPASS_MOCK_IMAGE_DECODER_H

#include "common.h"

#include <multipass/path.h>
#include <multipass/progress_monitor.h>

namespace multipass::test
{

class MockImageDecoder
{
public:
MOCK_METHOD(void, decode_to, (const Path&, const Path&, const ProgressMonitor&), (const));
};

} // namespace multipass::test

#endif // MULTIPASS_MOCK_IMAGE_DECODER_H
Loading

0 comments on commit 610b326

Please sign in to comment.