From e8cc50b935935ab3862f6479568050e9ec10b758 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 11 Sep 2017 19:53:59 -0400 Subject: [PATCH] Remove .zip plugin file after extracting it --- src/plugin.cpp | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/plugin.cpp b/src/plugin.cpp index 5da33e38..c054faf5 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -124,17 +124,13 @@ void pluginDestroy() { // CURL and libzip helpers //////////////////// - -static void extractZip(const char *filename, const char *dir) { +static int extractZipHandle(zip_t *za, const char *dir) { int err = 0; - zip_t *za = zip_open(filename, 0, &err); - if (!za) return; - if (err) goto cleanup; - 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) goto cleanup; + if (err) + return err; int nameLen = strlen(zs.name); char path[MAXPATHLEN]; @@ -142,11 +138,13 @@ static void extractZip(const char *filename, const char *dir) { if (zs.name[nameLen - 1] == '/') { err = mkdir(path, 0755); - if (err) goto cleanup; + if (err) + return err; } else { zip_file_t *zf = zip_fopen_index(za, i, 0); - if (!zf) goto cleanup; + if (!zf) + return 1; FILE *outFile = fopen(path, "wb"); if (!outFile) @@ -161,13 +159,26 @@ static void extractZip(const char *filename, const char *dir) { } err = zip_fclose(zf); - assert(!err); + if (err) + return err; fclose(outFile); } } + return 0; +} + +static int extractZip(const char *filename, const char *dir) { + int err = 0; + zip_t *za = zip_open(filename, 0, &err); + if (!za) + return 1; + + if (!err) { + err = extractZipHandle(za, dir); + } -cleanup: zip_close(za); + return err; } //////////////////// @@ -236,9 +247,13 @@ static void pluginRefreshPlugin(json_t *pluginJ) { bool success = requestDownload(download, filename, &downloadProgress); if (success) { // Unzip file - extractZip(filename.c_str(), "plugins"); - // Load plugin - loadPlugin(slug); + int err = extractZip(filename.c_str(), "plugins"); + if (!err) { + // Load plugin + loadPlugin(slug); + // Delete zip + remove(filename.c_str()); + } } downloadName = "";