Browse Source

Move plugin.cpp's extractZip to system::unzipToFolder().

tags/v1.1.5
Andrew Belt 5 years ago
parent
commit
a629561f58
3 changed files with 78 additions and 72 deletions
  1. +5
    -0
      include/system.hpp
  2. +2
    -72
      src/plugin.cpp
  3. +71
    -0
      src/system.cpp

+ 5
- 0
include/system.hpp View File

@@ -47,6 +47,11 @@ The launched process will continue running if the current process is closed.
*/
void runProcessDetached(const std::string& path);
std::string getOperatingSystemInfo();
/** Unzips a ZIP file to a folder.
The folder must exist.
Returns 0 if successful.
*/
int unzipToFolder(const std::string& zipPath, const std::string& dir);


} // namespace system


+ 2
- 72
src/plugin.cpp View File

@@ -17,8 +17,6 @@
#include <map>
#include <stdexcept>

#define ZIP_STATIC
#include <zip.h>
#include <jansson.h>

#if defined ARCH_WIN
@@ -174,74 +172,6 @@ static void loadPlugins(std::string path) {
}
}

/** Returns 0 if successful */
static int extractZipHandle(zip_t* za, std::string dir) {
int err;
for (int i = 0; i < zip_get_num_entries(za, 0); i++) {
zip_stat_t zs;
err = zip_stat_index(za, i, 0, &zs);
if (err) {
WARN("zip_stat_index() failed: error %d", err);
return err;
}

std::string path = dir + "/" + zs.name;

if (path[path.size() - 1] == '/') {
system::createDirectory(path);
// HACK
// Create and delete file to update the directory's mtime.
std::string tmpPath = path + "/.tmp";
FILE* tmpFile = fopen(tmpPath.c_str(), "w");
fclose(tmpFile);
std::remove(tmpPath.c_str());
}
else {
zip_file_t* zf = zip_fopen_index(za, i, 0);
if (!zf) {
WARN("zip_fopen_index() failed");
return -1;
}

FILE* outFile = fopen(path.c_str(), "wb");
if (!outFile)
continue;

while (1) {
char buffer[1 << 15];
int len = zip_fread(zf, buffer, sizeof(buffer));
if (len <= 0)
break;
fwrite(buffer, 1, len, outFile);
}

err = zip_fclose(zf);
if (err) {
WARN("zip_fclose() failed: error %d", err);
return err;
}
fclose(outFile);
}
}
return 0;
}

/** Returns 0 if successful */
static int extractZip(std::string filename, std::string path) {
int err;
zip_t* za = zip_open(filename.c_str(), 0, &err);
if (!za) {
WARN("Could not open zip %s: error %d", filename.c_str(), err);
return err;
}
DEFER({
zip_close(za);
});

err = extractZipHandle(za, path);
return err;
}

static void extractPackages(std::string path) {
std::string message;

@@ -250,7 +180,7 @@ static void extractPackages(std::string path) {
continue;
INFO("Extracting package %s", packagePath.c_str());
// Extract package
if (extractZip(packagePath, path)) {
if (system::unzipToFolder(packagePath, path)) {
WARN("Package %s failed to extract", packagePath.c_str());
message += string::f("Could not extract package %s\n", packagePath.c_str());
continue;
@@ -289,7 +219,7 @@ void init() {
std::string fundamentalDir = asset::pluginsPath + "/Fundamental";
if (!settings::devMode && !getPlugin("Fundamental") && system::isFile(fundamentalSrc)) {
INFO("Extracting bundled Fundamental package");
extractZip(fundamentalSrc.c_str(), asset::pluginsPath.c_str());
system::unzipToFolder(fundamentalSrc.c_str(), asset::pluginsPath.c_str());
loadPlugin(fundamentalDir);
}



+ 71
- 0
src/system.cpp View File

@@ -27,6 +27,9 @@
#include <dbghelp.h>
#endif

#define ZIP_STATIC
#include <zip.h>


namespace rack {
namespace system {
@@ -324,5 +327,73 @@ std::string getOperatingSystemInfo() {
}


int unzipToFolder(const std::string& zipPath, const std::string& dir) {
int err;
// Open ZIP file
zip_t* za = zip_open(zipPath.c_str(), 0, &err);
if (!za) {
WARN("Could not open ZIP file %s: error %d", zipPath.c_str(), err);
return err;
}
DEFER({
zip_close(za);
});

// Iterate ZIP entries
for (int i = 0; i < zip_get_num_entries(za, 0); i++) {
zip_stat_t zs;
err = zip_stat_index(za, i, 0, &zs);
if (err) {
WARN("zip_stat_index() failed: error %d", err);
return err;
}

std::string path = dir + "/" + zs.name;

if (path[path.size() - 1] == '/') {
// Create directory
system::createDirectory(path);
// HACK
// Create and delete file to update the directory's mtime.
std::string tmpPath = path + "/.tmp";
FILE* tmpFile = fopen(tmpPath.c_str(), "w");
fclose(tmpFile);
std::remove(tmpPath.c_str());
}
else {
// Open ZIP entry
zip_file_t* zf = zip_fopen_index(za, i, 0);
if (!zf) {
WARN("zip_fopen_index() failed");
return -1;
}
DEFER({
zip_fclose(zf);
});

// Create file
FILE* outFile = fopen(path.c_str(), "wb");
if (!outFile) {
WARN("Could not create file %s", path.c_str());
return -1;
}
DEFER({
fclose(outFile);
});

// Read buffer and copy to file
while (true) {
char buffer[1 << 15];
int len = zip_fread(zf, buffer, sizeof(buffer));
if (len <= 0)
break;
fwrite(buffer, 1, len, outFile);
}
}
}
return 0;
}


} // namespace system
} // namespace rack

Loading…
Cancel
Save