reapack

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

commit 3c797a383803e1afb2f237ba75e352267b4f2580
parent c81738cefa6395fad3cad0a9fbe8213d4e09dbaf
Author: cfillion <cfillion@users.noreply.github.com>
Date:   Mon, 18 Jan 2016 21:43:15 -0500

cleanup unneeded files when performing a package update

Diffstat:
Msrc/task.cpp | 46+++++++++++++++++++++++++++-------------------
Msrc/task.hpp | 8+++++---
Msrc/transaction.cpp | 2+-
3 files changed, 33 insertions(+), 23 deletions(-)

diff --git a/src/task.cpp b/src/task.cpp @@ -32,7 +32,7 @@ Task::Task(Transaction *transaction) { } -void Task::install(Version *ver) +void Task::install(Version *ver, const set<Path> &oldFiles) { const auto &sources = ver->sources(); @@ -48,6 +48,8 @@ void Task::install(Version *ver) // skip duplicate files do { it++; } while(it != sources.end() && path == it->first); } + + m_oldFiles = move(oldFiles); } void Task::saveSource(Download *dl, Source *src) @@ -61,6 +63,10 @@ void Task::saveSource(Download *dl, Source *src) m_files.push_back({tmpPath, targetPath}); + const auto old = m_oldFiles.find(targetPath); + if(old != m_oldFiles.end()) + m_oldFiles.erase(old); + const Path path = m_transaction->prefixPath(tmpPath); if(!m_transaction->saveFile(dl, path)) { @@ -83,14 +89,14 @@ void Task::commit() if(m_isCancelled) return; - for(const PathPair &paths : m_files) { - const string tempPath = m_transaction->prefixPath(paths.first).join(); - const string targetPath = m_transaction->prefixPath(paths.second).join(); + for(const Path &path : m_oldFiles) + removeFile(path); - RemoveFile(targetPath); + for(const PathPair &paths : m_files) { + removeFile(paths.second); - if(RenameFile(tempPath, targetPath)) { - m_transaction->addError(strerror(errno), tempPath); + if(renameFile(paths.first, paths.second)) { + m_transaction->addError(strerror(errno), paths.first.join()); // it's a bit late to rollback here as some files might already have been // overwritten. at least we can delete the temporary files @@ -99,35 +105,37 @@ void Task::commit() } } - m_files.clear(); m_onCommit(); } void Task::rollback() { - for(const PathPair &paths : m_files) { - const string tempPath = m_transaction->prefixPath(paths.first).join(); - - RemoveFile(tempPath); - } + for(const PathPair &paths : m_files) + removeFile(paths.first); m_files.clear(); } -int Task::RemoveFile(const std::string &path) +int Task::removeFile(const Path &path) const { + const string &fullPath = m_transaction->prefixPath(path).join(); + #ifdef _WIN32 - return _wremove(make_autostring(path).c_str()); + return _wremove(make_autostring(fullPath).c_str()); #else - return remove(path.c_str()); + return remove(fullPath.c_str()); #endif } -int Task::RenameFile(const std::string &from, const std::string &to) +int Task::renameFile(const Path &from, const Path &to) const { + const string &fullFrom = m_transaction->prefixPath(from).join(); + const string &fullTo = m_transaction->prefixPath(to).join(); + #ifdef _WIN32 - return _wrename(make_autostring(from).c_str(), make_autostring(to).c_str()); + return _wrename(make_autostring(fullFrom).c_str(), + make_autostring(fullTo).c_str()); #else - return rename(from.c_str(), to.c_str()); + return rename(fullFrom.c_str(), fullTo.c_str()); #endif } diff --git a/src/task.hpp b/src/task.hpp @@ -19,6 +19,7 @@ #define REAPACK_TASK_HPP #include <boost/signals2.hpp> +#include <set> #include <vector> class Download; @@ -36,13 +37,13 @@ public: void onCommit(const Callback &callback) { m_onCommit.connect(callback); } - void install(Version *ver); + void install(Version *ver, const std::set<Path> &oldFiles); void commit(); void cancel(); private: - static int RemoveFile(const std::string &); - static int RenameFile(const std::string &, const std::string &); + int removeFile(const Path &) const; + int renameFile(const Path &, const Path &) const; typedef std::pair<Path, Path> PathPair; @@ -54,6 +55,7 @@ private: Transaction *m_transaction; bool m_isCancelled; std::vector<PathPair> m_files; + std::set<Path> m_oldFiles; Signal m_onCommit; }; diff --git a/src/transaction.cpp b/src/transaction.cpp @@ -126,7 +126,7 @@ void Transaction::install() Task *task = new Task(this); try { - task->install(ver); + task->install(ver, m_registry->getFiles(regEntry)); task->onCommit([=] { if(regEntry.status == Registry::UpdateAvailable) m_updates.push_back(entry);