Browse Source

Clean up autosave directory of removed modules when saving a patch and closing. Check for magic number of Zstandard format to find out whether a patch is legacy pre-v2.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
9430ed8a65
3 changed files with 41 additions and 19 deletions
  1. +1
    -0
      include/patch.hpp
  2. +14
    -15
      src/context.cpp
  3. +26
    -4
      src/patch.cpp

+ 1
- 0
include/patch.hpp View File

@@ -26,6 +26,7 @@ struct PatchManager {
void saveAsDialog(); void saveAsDialog();
void saveTemplateDialog(); void saveTemplateDialog();
void saveAutosave(); void saveAutosave();
void cleanAutosave();
/** Loads a patch and nothing else. /** Loads a patch and nothing else.
Returns whether the patch was loaded successfully. Returns whether the patch was loaded successfully.
*/ */


+ 14
- 15
src/context.cpp View File

@@ -12,23 +12,22 @@ namespace rack {


Context::~Context() { Context::~Context() {
// Set pointers to NULL so other objects will segfault when attempting to access them // Set pointers to NULL so other objects will segfault when attempting to access them
if (scene)
delete scene;
delete patch;
patch = NULL;

delete scene;
scene = NULL; scene = NULL;
if (event)
delete event;
event = NULL;
if (history)
delete history;
history = NULL;
if (window)
delete window;

delete window;
window = NULL; window = NULL;
if (patch)
delete patch;
patch = NULL;
if (engine)
delete engine;

delete history;
history = NULL;

delete event;
event = NULL;

delete engine;
engine = NULL; engine = NULL;
} }




+ 26
- 4
src/patch.cpp View File

@@ -27,6 +27,7 @@ PatchManager::PatchManager() {




PatchManager::~PatchManager() { PatchManager::~PatchManager() {
cleanAutosave();
} }




@@ -60,7 +61,9 @@ static bool promptClear(std::string text) {


void PatchManager::save(std::string path) { void PatchManager::save(std::string path) {
INFO("Saving patch %s", path.c_str()); INFO("Saving patch %s", path.c_str());
// TEMP
saveAutosave(); saveAutosave();
cleanAutosave();


uint64_t startTime = system::getNanoseconds(); uint64_t startTime = system::getNanoseconds();
// Set compression level to 1 so that a 500MB/s SSD is almost bottlenecked // Set compression level to 1 so that a 500MB/s SSD is almost bottlenecked
@@ -169,16 +172,35 @@ void PatchManager::saveAutosave() {
} }




void PatchManager::cleanAutosave() {
// Remove files and folders in the `autosave/modules` folder that doesn't match a module in the rack.
std::string modulesDir = system::join(asset::autosavePath, "modules");
for (const std::string& entry : system::getEntries(modulesDir)) {
try {
int64_t moduleId = std::stol(system::getFilename(entry));
// Ignore modules that exist in the rack
if (APP->engine->getModule(moduleId))
continue;
}
catch (std::invalid_argument& e) {
}
// Remove the entry.
system::removeRecursively(entry);
}
}


static bool isPatchLegacyPre2(std::string path) { static bool isPatchLegacyPre2(std::string path) {
FILE* f = std::fopen(path.c_str(), "rb"); FILE* f = std::fopen(path.c_str(), "rb");
if (!f) if (!f)
return false; return false;
DEFER({std::fclose(f);}); DEFER({std::fclose(f);});
// Read first byte and check if it's a "{" character.
// TODO Is it possible for .tar.zst files to start with the same character?
char buf[1] = {};
// All Zstandard frames start with this magic number.
char zstdMagic[] = "\x28\xb5\x2f\xfd";
char buf[4] = {};
std::fread(buf, 1, sizeof(buf), f); std::fread(buf, 1, sizeof(buf), f);
return std::memcmp(buf, "{", 1) == 0;
// If the patch file doesn't begin with the magic number, it's a legacy patch.
return std::memcmp(buf, zstdMagic, sizeof(buf)) != 0;
} }






Loading…
Cancel
Save