Skip to content

Commit

Permalink
Merge #812 #814
Browse files Browse the repository at this point in the history
812: launch: Clean up instance data if an error occurs during image prep r=townsend2010,ricab a=townsend2010

Fixes #680

814: Teach fmtlib to deal with QString, can remove lots of toStdString() calls r=ricab,townsend2010 a=gerboland

RFC. Do we want?

Co-authored-by: Chris Townsend <[email protected]>
Co-authored-by: Ricardo Abreu <[email protected]>
Co-authored-by: Gerry Boland <[email protected]>
  • Loading branch information
4 people committed May 31, 2019
3 parents 50fe23b + cc5ae88 + e86e6d1 commit 69cfb24
Show file tree
Hide file tree
Showing 44 changed files with 144 additions and 91 deletions.
3 changes: 1 addition & 2 deletions include/multipass/cli/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@

#include <multipass/callable_traits.h>
#include <multipass/cli/return_codes.h>
#include <multipass/format.h>
#include <multipass/rpc/multipass.grpc.pb.h>
#include <multipass/terminal.h>
#include <multipass/utils.h>

#include <QLocalSocket>
#include <QString>

#include <fmt/format.h>

#include <grpc++/grpc++.h>

namespace multipass
Expand Down
47 changes: 47 additions & 0 deletions include/multipass/format.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2019 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_FORMAT_H
#define MULTIPASS_FORMAT_H

#include <fmt/format.h>
#include <fmt/ostream.h>
#include <QString>

namespace fmt
{

template <>
struct formatter<QString>
{
template <typename ParseContext>
constexpr auto parse(ParseContext& ctx)
{
return ctx.begin();
}

template <typename FormatContext>
auto format(const QString& a, FormatContext& ctx)
{
return format_to(ctx.begin(), "{}", a.toStdString()); // TODO: remove the copy?
}
};

} // namespace fmt


#endif // MULTIPASS_FORMAT_H
10 changes: 4 additions & 6 deletions src/cert/ssl_cert_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <openssl/rand.h>
#include <openssl/x509.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <QFile>

Expand All @@ -45,8 +45,7 @@ class WritableFile
explicit WritableFile(const QString& name) : fp{fopen(name.toStdString().c_str(), "wb"), fclose}
{
if (fp == nullptr)
throw std::runtime_error(
fmt::format("failed to open file '{}': {}({})", name.toStdString(), strerror(errno), errno));
throw std::runtime_error(fmt::format("failed to open file '{}': {}({})", name, strerror(errno), errno));
}

FILE* get() const
Expand Down Expand Up @@ -94,8 +93,7 @@ class EVPKey
{
WritableFile file{name};
if (!PEM_write_PrivateKey(file.get(), key.get(), nullptr, nullptr, 0, nullptr, nullptr))
throw std::runtime_error(
fmt::format("Failed writing certificate private key to file '{}'", name.toStdString()));
throw std::runtime_error(fmt::format("Failed writing certificate private key to file '{}'", name));

QFile::setPermissions(name, QFile::ReadOwner);
}
Expand Down Expand Up @@ -179,7 +177,7 @@ class X509Cert
{
WritableFile file{name};
if (!PEM_write_X509(file.get(), x509.get()))
throw std::runtime_error(fmt::format("Failed writing certificate to file '{}'", name.toStdString()));
throw std::runtime_error(fmt::format("Failed writing certificate to file '{}'", name));
}

private:
Expand Down
2 changes: 1 addition & 1 deletion src/client/cli/argparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <multipass/cli/argparser.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <QFileInfo>
#include <QRegExp>
Expand Down
2 changes: 1 addition & 1 deletion src/client/cli/cmd/launch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <multipass/cli/client_platform.h>
#include <multipass/constants.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <yaml-cpp/yaml.h>

Expand Down
2 changes: 1 addition & 1 deletion src/client/cli/cmd/mount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <multipass/logging/log.h>
#include <multipass/sshfs_mount/sftp_server.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <QDir>
#include <QFileInfo>
Expand Down
8 changes: 4 additions & 4 deletions src/client/cli/cmd/transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ mp::ParseCode cmd::Transfer::parse_sources(mp::ArgParser* parser)
QFileInfo source(source_path);
if (!source.exists())
{
cerr << fmt::format("Source path \"{}\" does not exist\n", source_path.toStdString());
cerr << fmt::format("Source path \"{}\" does not exist\n", source_path);
return ParseCode::CommandLineError;
}

Expand All @@ -217,7 +217,7 @@ mp::ParseCode cmd::Transfer::parse_sources(mp::ArgParser* parser)

if (!source.isReadable())
{
cerr << fmt::format("Source path \"{}\" is not readable\n", source_path.toStdString());
cerr << fmt::format("Source path \"{}\" is not readable\n", source_path);
return ParseCode::CommandLineError;
}
}
Expand Down Expand Up @@ -254,15 +254,15 @@ mp::ParseCode cmd::Transfer::parse_destination(mp::ArgParser* parser)
{
if (!destination_file.isWritable())
{
cerr << fmt::format("Destination path \"{}\" is not writable\n", destination_path.toStdString());
cerr << fmt::format("Destination path \"{}\" is not writable\n", destination_path);
return ParseCode::CommandLineError;
}
}
else
{
if (!QFileInfo(destination_file.dir().absolutePath()).isWritable())
{
cerr << fmt::format("Destination path \"{}\" is not writable\n", destination_path.toStdString());
cerr << fmt::format("Destination path \"{}\" is not writable\n", destination_path);
return ParseCode::CommandLineError;
}
else if (sources.size() > 1)
Expand Down
2 changes: 1 addition & 1 deletion src/client/cli/formatter/csv_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <multipass/cli/format_utils.h>

#include <fmt/format.h>
#include <multipass/format.h>

namespace mp = multipass;

Expand Down
2 changes: 1 addition & 1 deletion src/client/cli/formatter/table_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <multipass/cli/format_utils.h>

#include <fmt/format.h>
#include <multipass/format.h>

namespace mp = multipass;

Expand Down
2 changes: 1 addition & 1 deletion src/client/cli/formatter/yaml_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <multipass/cli/format_utils.h>
#include <multipass/utils.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <yaml-cpp/yaml.h>

Expand Down
4 changes: 2 additions & 2 deletions src/daemon/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <multipass/platform.h>
#include <multipass/utils.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <QCommandLineOption>
#include <QCommandLineParser>
Expand Down Expand Up @@ -80,7 +80,7 @@ mp::DaemonConfigBuilder mp::cli::parse(const QCoreApplication& app)
else if (logger == "stderr")
builder.logger = std::make_unique<mpl::StandardLogger>(builder.verbosity_level);
else
throw std::runtime_error(fmt::format("invalid logger option '{}'", logger.toStdString()));
throw std::runtime_error(fmt::format("invalid logger option '{}'", logger));
}

if (parser.isSet(address_option))
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/common_image_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <multipass/logging/log.h>

#include <fmt/format.h>
#include <multipass/format.h>

namespace mp = multipass;
namespace mpl = multipass::logging;
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/custom_image_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <multipass/exceptions/download_exception.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <QMap>
#include <QUrl>
Expand Down
5 changes: 4 additions & 1 deletion src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include <multipass/vm_image_host.h>
#include <multipass/vm_image_vault.h>

#include <fmt/format.h>
#include <multipass/format.h>
#include <yaml-cpp/yaml.h>

#include <QDir>
Expand Down Expand Up @@ -1925,6 +1925,9 @@ void mp::Daemon::create_vm(const CreateRequest* request, grpc::ServerWriter<Crea
catch (const std::exception& e)
{
preparing_instances.erase(name);
release_resources(name);
vm_instances.erase(name);
persist_instances();
status_promise->set_value(grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, e.what(), ""));
}

Expand Down
2 changes: 1 addition & 1 deletion src/daemon/daemon_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <multipass/vm_image_host.h>
#include <multipass/vm_image_vault.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <QCoreApplication>

Expand Down
2 changes: 1 addition & 1 deletion src/daemon/daemon_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <multipass/virtual_machine_factory.h>
#include <multipass/vm_image_host.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <chrono>
#include <stdexcept>
Expand Down
6 changes: 3 additions & 3 deletions src/daemon/default_vm_image_vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <multipass/vm_image.h>
#include <multipass/xz_image_decoder.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <QCryptographicHash>
#include <QJsonArray>
Expand Down Expand Up @@ -179,7 +179,7 @@ QString copy(const QString& file_name, const QDir& output_dir)
return {};

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

QFileInfo info{file_name};
const auto source_name = info.fileName();
Expand Down Expand Up @@ -288,7 +288,7 @@ mp::VMImage mp::DefaultVMImageVault::fetch_image(const FetchType& fetch_type, co
VMImage source_image, vm_image;

if (!QFile::exists(image_url.path()))
throw std::runtime_error(fmt::format("Custom image `{}` does not exist.", image_url.path().toStdString()));
throw std::runtime_error(fmt::format("Custom image `{}` does not exist.", image_url.path()));

source_image.image_path = image_url.path();

Expand Down
2 changes: 1 addition & 1 deletion src/daemon/delayed_shutdown_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <multipass/delayed_shutdown_timer.h>
#include <multipass/logging/log.h>

#include <fmt/format.h>
#include <multipass/format.h>

namespace mp = multipass;
namespace mpl = multipass::logging;
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/ubuntu_image_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include <multipass/exceptions/download_exception.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <QUrl>

Expand Down
2 changes: 1 addition & 1 deletion src/logging/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <multipass/logging/log.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <QByteArray>
#include <QString>
Expand Down
2 changes: 1 addition & 1 deletion src/logging/standard_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <multipass/utils.h>

#include <fmt/format.h>
#include <multipass/format.h>

namespace mp = multipass;
namespace mpl = multipass::logging;
Expand Down
7 changes: 3 additions & 4 deletions src/metrics/metrics_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <multipass/metrics_provider.h>
#include <multipass/utils.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <QDateTime>
#include <QEventLoop>
Expand Down Expand Up @@ -61,13 +61,12 @@ void post_request(const QUrl& metrics_url, const QByteArray& body)
{
auto error_msg = QJsonDocument::fromJson(buff).object();
mpl::log(mpl::Level::info, category,
fmt::format("Metrics error: {} - {}", error_msg["code"].toString().toStdString(),
error_msg["message"].toString().toStdString()));
fmt::format("Metrics error: {} - {}", error_msg["code"].toString(), error_msg["message"].toString()));
}
else if (reply->error() != QNetworkReply::NoError)
{
QObject::disconnect(reply.get(), 0, 0, 0);
throw std::runtime_error(fmt::format("Metrics error: {}", reply->errorString().toStdString()));
throw std::runtime_error(fmt::format("Metrics error: {}", reply->errorString()));
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/network/url_downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <multipass/exceptions/download_exception.h>
#include <multipass/logging/log.h>

#include <fmt/format.h>
#include <multipass/format.h>

#include <QDir>
#include <QEventLoop>
Expand Down Expand Up @@ -134,8 +134,7 @@ void mp::URLDownloader::download_to(const QUrl& url, const QString& file_name, i

if (file.write(reply->readAll()) < 0)
{
mpl::log(mpl::Level::error, category,
fmt::format("error writing image: {}", file.errorString().toStdString()));
mpl::log(mpl::Level::error, category, fmt::format("error writing image: {}", file.errorString()));
reply->abort();
}
download_timeout.start();
Expand Down
4 changes: 2 additions & 2 deletions src/platform/backends/libvirt/libvirt_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <QDir>
#include <QXmlStreamReader>

#include <fmt/format.h>
#include <multipass/format.h>

#include <libvirt/virterror.h>

Expand Down Expand Up @@ -128,7 +128,7 @@ auto generate_xml_config_for(const mp::VirtualMachineDescription& desc, const st
{
auto snap_path = QDir(snap);
snap_path.cd("../current");
qemu_path = fmt::format("{}{}", snap_path.path().toStdString(), qemu_path);
qemu_path = fmt::format("{}{}", snap_path.path(), qemu_path);
}

return fmt::format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <multipass/virtual_machine_description.h>
#include <shared/linux/backend_utils.h>

#include <fmt/format.h>
#include <multipass/format.h>

namespace mp = multipass;

Expand Down
Loading

0 comments on commit 69cfb24

Please sign in to comment.