Browse Source

Add error handling to zip extraction

pull/1639/head
Andrew Belt 7 years ago
parent
commit
c42464c13a
1 changed files with 42 additions and 25 deletions
  1. +42
    -25
      src/plugin.cpp

+ 42
- 25
src/plugin.cpp View File

@@ -235,27 +235,35 @@ static void loadPlugins(std::string path) {
} }
} }


/** Returns 0 if successful */
static int extractZipHandle(zip_t *za, const char *dir) { static int extractZipHandle(zip_t *za, const char *dir) {
int err = 0;
int err;
for (int i = 0; i < zip_get_num_entries(za, 0); i++) { for (int i = 0; i < zip_get_num_entries(za, 0); i++) {
zip_stat_t zs; zip_stat_t zs;
err = zip_stat_index(za, i, 0, &zs); err = zip_stat_index(za, i, 0, &zs);
if (err)
if (err) {
warn("zip_stat_index() failed: error %d", err);
return err; return err;
}
int nameLen = strlen(zs.name); int nameLen = strlen(zs.name);


char path[MAXPATHLEN]; char path[MAXPATHLEN];
snprintf(path, sizeof(path), "%s/%s", dir, zs.name); snprintf(path, sizeof(path), "%s/%s", dir, zs.name);


if (zs.name[nameLen - 1] == '/') { if (zs.name[nameLen - 1] == '/') {
err = mkdir(path, 0755);
if (err && errno != EEXIST)
return err;
if (mkdir(path, 0755)) {
if (errno != EEXIST) {
warn("mkdir(%s) failed: error %d", path, errno);
return errno;
}
}
} }
else { else {
zip_file_t *zf = zip_fopen_index(za, i, 0); zip_file_t *zf = zip_fopen_index(za, i, 0);
if (!zf)
return 1;
if (!zf) {
warn("zip_fopen_index() failed");
return -1;
}


FILE *outFile = fopen(path, "wb"); FILE *outFile = fopen(path, "wb");
if (!outFile) if (!outFile)
@@ -270,24 +278,27 @@ static int extractZipHandle(zip_t *za, const char *dir) {
} }


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


/** Returns 0 if successful */
static int extractZip(const char *filename, const char *path) { static int extractZip(const char *filename, const char *path) {
int err = 0;
int err;
zip_t *za = zip_open(filename, 0, &err); zip_t *za = zip_open(filename, 0, &err);
if (!za)
return 1;
if (!za) {
warn("Could not open zip %s: error %d", filename, err);
return err;
}
defer({ defer({
zip_close(za); zip_close(za);
}); });
if (err)
return err;


err = extractZipHandle(za, path); err = extractZipHandle(za, path);
return err; return err;
@@ -297,15 +308,18 @@ static void extractPackages(std::string path) {
std::string message; std::string message;


for (std::string packagePath : systemListEntries(path)) { for (std::string packagePath : systemListEntries(path)) {
if (stringExtension(packagePath) == "zip") {
info("Extracting package %s", packagePath.c_str());
// Extract package
if (extractZip(packagePath.c_str(), path.c_str())) {
message += stringf("Could not extract package %s\n", packagePath.c_str());
continue;
}
// Remove package
remove(packagePath.c_str());
if (stringExtension(packagePath) != "zip")
continue;
info("Extracting package %s", packagePath.c_str());
// Extract package
if (extractZip(packagePath.c_str(), path.c_str())) {
warn("Package %s failed to extract", packagePath.c_str());
message += stringf("Could not extract package %s\n", packagePath.c_str());
continue;
}
// Remove package
if (remove(packagePath.c_str())) {
warn("Could not delete file %s: error %d", packagePath.c_str(), errno);
} }
} }
if (!message.empty()) { if (!message.empty()) {
@@ -332,9 +346,11 @@ void pluginInit() {


#if RELEASE #if RELEASE
// Copy Fundamental package to plugins directory if folder does not exist // Copy Fundamental package to plugins directory if folder does not exist
std::string fundamentalDest = localPlugins + "/Fundamental.zip";
if (!systemIsDirectory(localPlugins + "/Fundamental") && !systemIsFile(fundamentalDest)) {
systemCopy(assetGlobal("Fundamental.zip"), fundamentalDest);
std::string fundamentalSrc = assetGlobal("Fundamental.zip");
std::string fundamentalDest = assetLocal("plugins/Fundamental.zip");
std::string fundamentalDir = assetLocal("plugins/Fundamental");
if (!systemIsDirectory(fundamentalDir) && !systemIsFile(fundamentalDest)) {
systemCopy(fundamentalSrc, fundamentalDest);
} }
#endif #endif


@@ -362,6 +378,7 @@ void pluginDestroy() {
} }


bool pluginSync(bool dryRun) { bool pluginSync(bool dryRun) {
return false;
if (gToken.empty()) if (gToken.empty())
return false; return false;




Loading…
Cancel
Save