reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

commit 0b52a8ffc26f7f45f3c3ccacc553fdc46b90a859
parent a913bc7607cb154319ff59b28a1aa13252255dac
Author: cfillion <cfillion@users.noreply.github.com>
Date:   Sun, 13 Dec 2015 12:44:15 -0500

split PackageTransaction in its own file

Diffstat:
Asrc/pkgtransaction.cpp | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/pkgtransaction.hpp | 46++++++++++++++++++++++++++++++++++++++++++++++
Msrc/transaction.cpp | 101+------------------------------------------------------------------------------
Msrc/transaction.hpp | 34----------------------------------
4 files changed, 155 insertions(+), 134 deletions(-)

diff --git a/src/pkgtransaction.cpp b/src/pkgtransaction.cpp @@ -0,0 +1,108 @@ +#include "pkgtransaction.hpp" + +#include "path.hpp" +#include "transaction.hpp" + +#include <cerrno> +#include <cstdio> + +using namespace std; + +bool PackageTransaction::isInstalled(Version *ver, const Path &root) +{ + // TODO + // file_exists(installPath(pkg).join().c_str()); + return true; +} + +PackageTransaction::PackageTransaction(Transaction *transaction) + : m_transaction(transaction), m_isCancelled(false) +{ +} + +void PackageTransaction::install(Version *ver) +{ + const size_t sourcesSize = ver->sources().size(); + + for(size_t i = 0; i < sourcesSize; i++) { + Source *src = ver->source(i); + + Download *dl = new Download(src->fullName(), src->url()); + dl->onFinish(bind(&PackageTransaction::saveSource, this, dl, src)); + + m_remaining.push_back(dl); + m_transaction->downloadQueue()->push(dl); + + // executing finish after the download is deleted + // prevents the download queue from being deleted before the download is + dl->onFinish(bind(&PackageTransaction::finish, this)); + } +} + +void PackageTransaction::saveSource(Download *dl, Source *src) +{ + m_remaining.erase(remove(m_remaining.begin(), m_remaining.end(), dl)); + + if(m_isCancelled) + return; + + const Path targetPath = src->targetPath(); + Path tmpPath = targetPath; + tmpPath[tmpPath.size() - 1] += ".new"; + + m_files.push_back({tmpPath, targetPath}); + + const Path path = m_transaction->prefixPath(tmpPath); + + if(!m_transaction->saveFile(dl, path)) { + cancel(); + return; + } +} + +void PackageTransaction::finish() +{ + if(!m_remaining.empty()) + return; + + m_onFinish(); +} + +void PackageTransaction::cancel() +{ + m_isCancelled = true; + + for(Download *dl : m_remaining) + dl->abort(); + + rollback(); +} + +void PackageTransaction::commit() +{ + for(const PathPair &paths : m_files) { + const string tempPath = m_transaction->prefixPath(paths.first).join(); + const string targetPath = m_transaction->prefixPath(paths.second).join(); + + if(rename(tempPath.c_str(), targetPath.c_str())) { + m_transaction->addError(strerror(errno), targetPath); + rollback(); + return; + } + } + + m_files.clear(); + m_onCommit(); +} + +void PackageTransaction::rollback() +{ + for(const PathPair &paths : m_files) { + const string tempPath = m_transaction->prefixPath(paths.first).join(); + + if(remove(tempPath.c_str())) + m_transaction->addError(strerror(errno), tempPath); + } + + m_files.clear(); +} diff --git a/src/pkgtransaction.hpp b/src/pkgtransaction.hpp @@ -0,0 +1,46 @@ +#ifndef REAPACK_PKG_TRANSACTION_HPP +#define REAPACK_PKG_TRANSACTION_HPP + +#include <boost/signals2.hpp> +#include <vector> + +class Download; +class Source; +class Transaction; +class Path; +class Version; + +class PackageTransaction { +public: + static bool isInstalled(Version *, const Path &root); + + typedef boost::signals2::signal<void ()> Signal; + typedef Signal::slot_type Callback; + + PackageTransaction(Transaction *parent); + + void onCommit(const Callback &callback) { m_onCommit.connect(callback); } + void onFinish(const Callback &callback) { m_onFinish.connect(callback); } + + void install(Version *ver); + void commit(); + void cancel(); + +private: + typedef std::pair<Path, Path> PathPair; + + void finish(); + void rollback(); + + void saveSource(Download *, Source *); + + Transaction *m_transaction; + bool m_isCancelled; + std::vector<Download *> m_remaining; + std::vector<PathPair> m_files; + + Signal m_onCommit; + Signal m_onFinish; +}; + +#endif diff --git a/src/transaction.cpp b/src/transaction.cpp @@ -1,8 +1,8 @@ #include "transaction.hpp" #include "errors.hpp" +#include "pkgtransaction.hpp" -#include <cstdio> #include <fstream> #include <reaper_plugin_functions.h> @@ -169,102 +169,3 @@ Path Transaction::prefixPath(const Path &input) const { return m_root + input; } - -bool PackageTransaction::isInstalled(Version *ver, const Path &root) -{ - // TODO - // file_exists(installPath(pkg).join().c_str()); - return true; -} - -PackageTransaction::PackageTransaction(Transaction *transaction) - : m_transaction(transaction), m_isCancelled(false) -{ -} - -void PackageTransaction::install(Version *ver) -{ - const size_t sourcesSize = ver->sources().size(); - - for(size_t i = 0; i < sourcesSize; i++) { - Source *src = ver->source(i); - - Download *dl = new Download(src->fullName(), src->url()); - dl->onFinish(bind(&PackageTransaction::saveSource, this, dl, src)); - - m_remaining.push_back(dl); - m_transaction->downloadQueue()->push(dl); - - // executing finish after the download is deleted - // prevents the download queue from being deleted before the download is - dl->onFinish(bind(&PackageTransaction::finish, this)); - } -} - -void PackageTransaction::saveSource(Download *dl, Source *src) -{ - m_remaining.erase(remove(m_remaining.begin(), m_remaining.end(), dl)); - - if(m_isCancelled) - return; - - const Path targetPath = src->targetPath(); - Path tmpPath = targetPath; - tmpPath[tmpPath.size() - 1] += ".new"; - - m_files.push_back({tmpPath, targetPath}); - - const Path path = m_transaction->prefixPath(tmpPath); - - if(!m_transaction->saveFile(dl, path)) { - cancel(); - return; - } -} - -void PackageTransaction::finish() -{ - if(!m_remaining.empty()) - return; - - m_onFinish(); -} - -void PackageTransaction::cancel() -{ - m_isCancelled = true; - - for(Download *dl : m_remaining) - dl->abort(); - - rollback(); -} - -void PackageTransaction::commit() -{ - for(const PathPair &paths : m_files) { - const string tempPath = m_transaction->prefixPath(paths.first).join(); - const string targetPath = m_transaction->prefixPath(paths.second).join(); - - if(rename(tempPath.c_str(), targetPath.c_str())) { - m_transaction->addError(strerror(errno), targetPath); - rollback(); - return; - } - } - - m_files.clear(); - m_onCommit(); -} - -void PackageTransaction::rollback() -{ - for(const PathPair &paths : m_files) { - const string tempPath = m_transaction->prefixPath(paths.first).join(); - - if(remove(tempPath.c_str())) - m_transaction->addError(strerror(errno), tempPath); - } - - m_files.clear(); -} diff --git a/src/transaction.hpp b/src/transaction.hpp @@ -8,7 +8,6 @@ #include "remote.hpp" #include <boost/signals2.hpp> -#include <vector> class PackageTransaction; @@ -75,37 +74,4 @@ private: Signal m_onFinish; }; -class PackageTransaction { -public: - static bool isInstalled(Version *, const Path &root); - - typedef boost::signals2::signal<void ()> Signal; - typedef Signal::slot_type Callback; - - PackageTransaction(Transaction *parent); - - void onCommit(const Callback &callback) { m_onCommit.connect(callback); } - void onFinish(const Callback &callback) { m_onFinish.connect(callback); } - - void install(Version *ver); - void commit(); - void cancel(); - -private: - typedef std::pair<Path, Path> PathPair; - - void finish(); - void rollback(); - - void saveSource(Download *, Source *); - - Transaction *m_transaction; - bool m_isCancelled; - std::vector<Download *> m_remaining; - std::vector<PathPair> m_files; - - Signal m_onCommit; - Signal m_onFinish; -}; - #endif