Skip to content

Commit

Permalink
[api] Restore single unit. Contributes to MER#1285
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Zalevskiy <denis.zalevskiy@jolla.com>
  • Loading branch information
Denis Zalevskiy committed Sep 17, 2015
1 parent dbdd995 commit 24adc29
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions include/vault/vault.hpp
Expand Up @@ -97,10 +97,15 @@ class Vault
std::tuple<QString, bool, QString> backupUnit(const QString &, const QString &);
QString tagSnapshot(const QString &message);
void resetMaster();

std::tuple<QString, bool, QString> restoreUnit
(const QString &, const QString &, const QString &);

private:
bool setState(const QString &state);
bool backupUnit(const QString &home, const QString &unit, const ProgressCallback &callback);
bool restoreUnit(const QString &home, const QString &unit, const ProgressCallback &callback);
void checkout(const QString &);

void setup(const QVariantMap *config);
QString absolutePath(QString const &) const;
Expand Down
19 changes: 18 additions & 1 deletion qml/Vault/vault.cpp
Expand Up @@ -73,11 +73,20 @@ class Worker : public QObject

Q_INVOKABLE void backupUnit(const QString &home, const QString &unit)
{
debug::debug("Backup unit", unit);
debug::debug(home, "- backup unit", unit);
auto res = m_vault->backupUnit(home, unit);
emit progress(Vault::Backup, {{"unit", unit}, {"status", std::get<2>(res)}});
}

Q_INVOKABLE void restoreUnit(const QString &snapshot
, const QString &home
, const QString &unit)
{
debug::debug(home, "- restore unit", unit, "snapshot", snapshot);
auto res = m_vault->restoreUnit(snapshot, home, unit);
emit progress(Vault::Restore, {{"unit", unit}, {"status", std::get<2>(res)}});
}

Q_INVOKABLE void tagSnapshot(const QString &message)
{
debug::debug("Tag snapshot, message:", message);
Expand Down Expand Up @@ -402,4 +411,12 @@ Q_INVOKABLE void Vault::tagSnapshot(const QString &message)
QMetaObject::invokeMethod(m_worker, "tagSnapshot", Q_ARG(QString, message));
}

Q_INVOKABLE void Vault::restoreUnit(const QString &snapshot, const QString &unit)
{
QMetaObject::invokeMethod(m_worker, "restoreUnit"
, Q_ARG(QString, snapshot)
, Q_ARG(QString, m_home)
, Q_ARG(QString, unit));
}

#include "vault.moc"
2 changes: 2 additions & 0 deletions qml/Vault/vault.hpp
Expand Up @@ -66,6 +66,8 @@ class Vault : public QObject
Q_INVOKABLE void backupUnit(const QString &unit);
Q_INVOKABLE void tagSnapshot(const QString &message);

Q_INVOKABLE void restoreUnit(const QString &, const QString &);

signals:
void rootChanged();
void backupHomeChanged();
Expand Down
29 changes: 28 additions & 1 deletion src/vault.cpp
Expand Up @@ -579,6 +579,15 @@ Vault::Result Vault::restore(const Snapshot &snapshot, const QString &home, cons
return res;
}

void Vault::checkout(const QString &snapshotName)
{
auto name = snapshotName.startsWith('>')
? snapshotName
: '>' + snapshotName;
Snapshot snapshot(&m_vcs, name);
m_vcs.checkout(snapshot.tag(), CheckoutOptions::Force);
}

QList<Snapshot> Vault::snapshots() const
{
auto l = lock();
Expand Down Expand Up @@ -696,13 +705,16 @@ struct Unit
};

/// \return (unit, isOk, reason)
std::tuple<QString, bool, QString> Vault::backupUnit(const QString &home, const QString &unit)
std::tuple<QString, bool, QString> Vault::backupUnit
(const QString &home, const QString &unit)
{
auto l = lock();
QString reason;
auto onDone = [&reason](const QString &, const QString &result) {
reason = result;
};
// TODO error check
m_vcs.checkout("master");
auto isOk = backupUnit(home, unit, onDone);
return std::make_tuple(unit, isOk, reason);
}
Expand Down Expand Up @@ -731,6 +743,21 @@ bool Vault::backupUnit(const QString &home, const QString &unit, const ProgressC
return true;
}

/// \return (unit, isOk, reason)
std::tuple<QString, bool, QString> Vault::restoreUnit
(const QString &snapshot, const QString &home, const QString &unit)
{
auto l = lock();
QString reason;
auto onDone = [&reason](const QString &, const QString &result) {
reason = result;
};
// TODO error check
checkout(snapshot);
auto isOk = restoreUnit(home, unit, onDone);
return std::make_tuple(unit, isOk, reason);
}

bool Vault::restoreUnit(const QString &home, const QString &unit, const ProgressCallback &callback)
{
try {
Expand Down

0 comments on commit 24adc29

Please sign in to comment.