commit f062f877d8dfe3c95e77ac6cc4b012fb81d0c467
parent 7a9aa9e7532e53348c04f0325e4edeab13c0c96a
Author: cfillion <cfillion@users.noreply.github.com>
Date: Wed, 9 Mar 2016 00:54:38 -0500
refactor path prefixes and predefined paths
Diffstat:
10 files changed, 56 insertions(+), 37 deletions(-)
diff --git a/src/about.cpp b/src/about.cpp
@@ -207,7 +207,7 @@ void About::updateInstalledFiles()
set<Path> allFiles;
try {
- Registry reg(Path::prefixCache("registry.db"));
+ Registry reg(Path::prefixRoot(Path::REGISTRY));
for(const Registry::Entry &entry : reg.getEntries(m_index->name())) {
const set<Path> &files = reg.getFiles(entry);
allFiles.insert(files.begin(), files.end());
diff --git a/src/filesystem.cpp b/src/filesystem.cpp
@@ -25,6 +25,8 @@
#include <fstream>
#include <sys/stat.h>
+#include <reaper_plugin_functions.h>
+
#ifdef _WIN32
#include <windows.h>
#endif
@@ -33,18 +35,23 @@ using namespace std;
FILE *FS::open(const Path &path)
{
+ const Path &fullPath = Path::prefixRoot(path);
+
#ifdef _WIN32
FILE *file = nullptr;
- _wfopen_s(&file, make_autostring(path.join()).c_str(), L"rb");
+ _wfopen_s(&file, make_autostring(fullPath.join()).c_str(), L"rb");
return file;
#else
- return fopen(path.join().c_str(), "rb");
+ return fopen(fullPath.join().c_str(), "rb");
#endif
}
bool FS::write(const Path &path, const string &contents)
{
- ofstream file(make_autostring(path.join()), ios_base::binary);
+ mkdir(path.dirname());
+
+ const Path &fullPath = Path::prefixRoot(path);
+ ofstream file(make_autostring(fullPath.join()), ios_base::binary);
if(!file)
return false;
@@ -87,8 +94,7 @@ bool FS::remove(const Path &path)
// Then let's move it somewhere else. And delete it on next startup.
// Windows is so great!
- Path workaroundPath;
- workaroundPath.prepend(Path::CACHE_DIRNAME);
+ Path workaroundPath = Path::CACHE;
workaroundPath.append("old_" + path.last() + ".tmp");
return rename(path, workaroundPath);
@@ -117,15 +123,17 @@ bool FS::removeRecursive(const Path &file)
bool FS::mtime(const Path &path, time_t *time)
{
+ const Path &fullPath = Path::prefixRoot(path);
+
#ifdef _WIN32
struct _stat st;
- if(_wstat(make_autostring(path.join()).c_str(), &st))
+ if(_wstat(make_autostring(fullPath.join()).c_str(), &st))
return false;
#else
struct stat st;
- if(stat(path.join().c_str(), &st))
+ if(stat(fullPath.join().c_str(), &st))
return false;
#endif
@@ -134,6 +142,18 @@ bool FS::mtime(const Path &path, time_t *time)
return true;
}
+bool FS::exists(const Path &path)
+{
+ const Path &fullPath = Path::prefixRoot(path);
+ return file_exists(fullPath.join().c_str());
+}
+
+void FS::mkdir(const Path &path)
+{
+ const Path &fullPath = Path::prefixRoot(path);
+ RecursiveCreateDirectory(fullPath.join().c_str(), 0);
+}
+
string FS::lastError()
{
return strerror(errno);
diff --git a/src/filesystem.hpp b/src/filesystem.hpp
@@ -29,6 +29,8 @@ namespace FS {
bool remove(const Path &);
bool removeRecursive(const Path &);
bool mtime(const Path &, time_t *);
+ bool exists(const Path &);
+ void mkdir(const Path &);
std::string lastError();
};
diff --git a/src/index.cpp b/src/index.cpp
@@ -32,7 +32,7 @@ using namespace std;
Path Index::pathFor(const string &name)
{
- return Path::prefixCache(name + ".xml");
+ return Path::CACHE + (name + ".xml");
}
auto Index::linkTypeFor(const char *rel) -> LinkType
diff --git a/src/path.cpp b/src/path.cpp
@@ -32,9 +32,9 @@ static const char SEPARATOR = '\\';
static const string DOT = ".";
static const string DOTDOT = "..";
-const char *Path::CACHE_DIRNAME = "ReaPack";
-const char *Path::CONFIG_FILE = "reapack.ini";
-const char *Path::REGISTRY_FILE = "registry.db";
+Path Path::CACHE = Path("ReaPack");
+Path Path::CONFIG = Path("reapack.ini");
+Path Path::REGISTRY = Path::CACHE + "registry.db";
Path Path::s_root;
diff --git a/src/path.hpp b/src/path.hpp
@@ -25,14 +25,12 @@ class UseRootPath;
class Path {
public:
- static const char *CACHE_DIRNAME;
- static const char *CONFIG_FILE;
- static const char *REGISTRY_FILE;
+ static Path CACHE;
+ static Path CONFIG;
+ static Path REGISTRY;
- static Path cacheDir() { return s_root + CACHE_DIRNAME; }
static Path prefixRoot(const Path &p) { return s_root + p; }
static Path prefixRoot(const std::string &p) { return s_root + p; }
- static Path prefixCache(const std::string &p) { return cacheDir() + p; }
Path(const std::string &path = std::string());
diff --git a/src/reapack.cpp b/src/reapack.cpp
@@ -41,8 +41,8 @@ const string ReaPack::BUILDTIME = __DATE__ " " __TIME__;
// Surely there must be a better way...
static void CleanupTempFiles()
{
- const auto_string &pattern =
- make_autostring(Path::prefixCache("*.tmp").join());
+ const Path &path = Path::prefixRoot(Path::CACHE + "*.tmp");
+ const auto_string &pattern = make_autostring(path.join());
WIN32_FIND_DATA fd = {};
HANDLE handle = FindFirstFile(pattern.c_str(), &fd);
@@ -70,10 +70,10 @@ ReaPack::ReaPack(REAPER_PLUGIN_HINSTANCE instance)
Download::Init();
- RecursiveCreateDirectory(Path::cacheDir().join().c_str(), 0);
+ FS::mkdir(Path::CACHE);
m_config = new Config;
- m_config->read(Path::prefixRoot(Path::CONFIG_FILE));
+ m_config->read(Path::prefixRoot(Path::CONFIG));
if(m_config->isFirstRun())
manageRemotes();
@@ -372,7 +372,7 @@ void ReaPack::loadIndex(const Remote &remote,
AUTO_STR("Write Failed"), MB_OK);
break;
case Download::Failure:
- if(file_exists(Index::pathFor(remote.name()).join().c_str()))
+ if(FS::exists(Index::pathFor(remote.name())))
load();
else
MessageBox(parent, make_autostring(dl->contents()).c_str(),
@@ -454,7 +454,7 @@ void ReaPack::registerSelf()
REAPACK_FILE, "dummy url", &ver));
try {
- Registry reg(Path::prefixCache(Path::REGISTRY_FILE));
+ Registry reg(Path::prefixRoot(Path::REGISTRY));
reg.push(&ver);
reg.commit();
}
diff --git a/src/task.cpp b/src/task.cpp
@@ -92,7 +92,7 @@ void InstallTask::saveSource(Download *dl, const Source *src)
if(old != m_oldFiles.end())
m_oldFiles.erase(old);
- if(!transaction()->saveFile(dl, Path::prefixRoot(tmpPath))) {
+ if(!transaction()->saveFile(dl, tmpPath)) {
rollback();
return;
}
diff --git a/src/transaction.cpp b/src/transaction.cpp
@@ -33,7 +33,7 @@ using namespace std;
Transaction::Transaction()
: m_isCancelled(false)
{
- m_registry = new Registry(Path::prefixCache(Path::REGISTRY_FILE));
+ m_registry = new Registry(Path::prefixRoot(Path::REGISTRY));
m_downloadQueue.onAbort([=] {
m_isCancelled = true;
@@ -223,7 +223,13 @@ void Transaction::unregisterAll(const Remote &remote)
void Transaction::uninstall(const Remote &remote)
{
inhibit(remote);
- FS::remove(Index::pathFor(remote.name()));
+
+ const Path &indexPath = Index::pathFor(remote.name());
+
+ if(FS::exists(indexPath)) {
+ if(!FS::remove(indexPath))
+ addError(FS::lastError(), indexPath.join());
+ }
const vector<Registry::Entry> &entries = m_registry->getEntries(remote.name());
@@ -235,7 +241,7 @@ void Transaction::uninstall(const Remote &remote)
for(const auto &entry : entries) {
const set<Path> &files = m_registry->getFiles(entry);
for(const Path &path : files) {
- if(file_exists(Path::prefixRoot(path).join().c_str()))
+ if(FS::exists(path))
allFiles.push_back(path);
}
@@ -258,8 +264,6 @@ bool Transaction::saveFile(Download *dl, const Path &path)
return false;
}
- RecursiveCreateDirectory(path.dirname().c_str(), 0);
-
if(!FS::write(path, dl->contents())) {
addError(FS::lastError(), path.join());
return false;
@@ -294,7 +298,7 @@ void Transaction::addError(const string &message, const string &title)
bool Transaction::allFilesExists(const set<Path> &list) const
{
for(const Path &path : list) {
- if(!file_exists(Path::prefixRoot(path).join().c_str()))
+ if(!FS::exists(path))
return false;
}
@@ -365,10 +369,10 @@ void Transaction::registerScript(const HostTicket ®)
else
section = MainSection;
- const std::string &path = Path::prefixRoot(reg.file).join();
+ const std::string &fullPath = Path::prefixRoot(reg.file).join();
const bool isLast = m_regQueue.size() == 1;
- if(!AddRemoveReaScript(reg.add, section, path.c_str(), isLast) && reg.add)
+ if(!AddRemoveReaScript(reg.add, section, fullPath.c_str(), isLast) && reg.add)
addError("Script could not be registered in REAPER.", reg.file);
}
diff --git a/test/path.cpp b/test/path.cpp
@@ -172,8 +172,6 @@ TEST_CASE("path generation utilities", M) {
const Path path("world");
REQUIRE(Path::prefixRoot(path) == Path("world"));
- REQUIRE(Path::cacheDir() == Path("ReaPack"));
- REQUIRE(Path::prefixCache("test") == Path("ReaPack/test"));
{
UseRootPath root("hello");
@@ -181,9 +179,6 @@ TEST_CASE("path generation utilities", M) {
REQUIRE(Path::prefixRoot(path) == Path("hello/world"));
REQUIRE(Path::prefixRoot("world") == Path("hello/world"));
-
- REQUIRE(Path::cacheDir() == Path("hello/ReaPack"));
- REQUIRE(Path::prefixCache("test") == Path("hello/ReaPack/test"));
}
REQUIRE(Path::prefixRoot(path) == Path("world"));