From 7a908ebf1beeb52b015263f345deb8ce5cb3e77b Mon Sep 17 00:00:00 2001 From: Chee Yee Date: Sat, 10 Feb 2024 00:27:18 -0800 Subject: [PATCH] improve speed of installing from compress file by increase read buffer --- source/actions.cpp | 6 +++--- source/installer.cpp | 23 ----------------------- source/installer.h | 1 - source/server/http_server.cpp | 10 ++++++++-- source/zip_util.cpp | 10 ++++++++-- source/zip_util.h | 2 +- 6 files changed, 20 insertions(+), 32 deletions(-) diff --git a/source/actions.cpp b/source/actions.cpp index def6f80..16b428f 100644 --- a/source/actions.cpp +++ b/source/actions.cpp @@ -786,7 +786,7 @@ namespace Actions void *ExtractArchivePkg(void *argp) { ssize_t len; - char buffer[ARCHIVE_TRANSFER_SIZE]; + char *buffer = (char*) malloc(ARCHIVE_TRANSFER_SIZE); ArchivePkgInstallData *install_data = (ArchivePkgInstallData*) argp; SplitFile *sp = install_data->split_file; @@ -795,7 +795,7 @@ namespace Actions sp->Open(); while (!install_data->stop_write_thread) { - len = archive_read_data(install_data->archive_entry->archive, buffer, sizeof buffer); + len = archive_read_data(install_data->archive_entry->archive, buffer, ARCHIVE_TRANSFER_SIZE); if (len == 0) break; @@ -814,7 +814,7 @@ namespace Actions } sp->Close(); - + free(buffer); return NULL; } diff --git a/source/installer.cpp b/source/installer.cpp index 83c9f38..4404c59 100644 --- a/source/installer.cpp +++ b/source/installer.cpp @@ -33,12 +33,6 @@ static bool s_bgft_initialized = false; static std::map archive_pkg_install_data_list; -struct BackgroundArchiveInstallInfo -{ - ArchivePkgInstallData *pkg_data; - std::string path; -}; - namespace INSTALLER { int Init(void) @@ -870,21 +864,4 @@ namespace INSTALLER } - void *InstallArchivePkgThread(void *argp) - { - BackgroundArchiveInstallInfo *bg_install_info = (BackgroundArchiveInstallInfo*) argp; - InstallArchivePkg(bg_install_info->path , bg_install_info->pkg_data); - free(bg_install_info); - - return NULL; - } - - void StartInstallArchivePkg(const std::string &path, ArchivePkgInstallData* pkg_data) - { - BackgroundArchiveInstallInfo *bg_install_info = (BackgroundArchiveInstallInfo*) malloc(sizeof(BackgroundArchiveInstallInfo)); - memset(bg_install_info, 0, sizeof(BackgroundArchiveInstallInfo)); - bg_install_info->pkg_data = pkg_data; - bg_install_info->path = path; - int res = pthread_create(&bk_install_thid, NULL, InstallArchivePkgThread, bg_install_info); - } } diff --git a/source/installer.h b/source/installer.h index 7e88768..f1e29a6 100644 --- a/source/installer.h +++ b/source/installer.h @@ -149,5 +149,4 @@ namespace INSTALLER void AddArchivePkgInstallData(const std::string &hash, ArchivePkgInstallData *pkg_data); void RemoveArchivePkgInstallData(const std::string &hash); bool InstallArchivePkg(const std::string &path, ArchivePkgInstallData* pkg_data); - void StartInstallArchivePkg(const std::string &path, ArchivePkgInstallData* pkg_data); } \ No newline at end of file diff --git a/source/server/http_server.cpp b/source/server/http_server.cpp index 59c7f32..1d9c2ce 100644 --- a/source/server/http_server.cpp +++ b/source/server/http_server.cpp @@ -1193,10 +1193,16 @@ namespace HttpServer install_data->split_file = sp; install_data->stop_write_thread = false; - int res = pthread_create(&install_data->thread, NULL, Actions::ExtractArchivePkg, install_data); + int ret = pthread_create(&install_data->thread, NULL, Actions::ExtractArchivePkg, install_data); - INSTALLER::InstallArchivePkg(entry->filename, install_data); + ret = INSTALLER::InstallArchivePkg(entry->filename, install_data); free(entry); + + if (ret == 0) + { + failed(res, 200, lang_strings[STR_FAIL_INSTALL_FROM_URL_MSG]); + return; + } } else { diff --git a/source/zip_util.cpp b/source/zip_util.cpp index 91622f7..07a9282 100644 --- a/source/zip_util.cpp +++ b/source/zip_util.cpp @@ -288,29 +288,35 @@ namespace ZipUtil static int extract2fd(struct archive *a, const std::string &pathname, int fd) { ssize_t len; - unsigned char buffer[ARCHIVE_TRANSFER_SIZE]; + unsigned char *buffer = (unsigned char *) malloc(ARCHIVE_TRANSFER_SIZE); /* loop over file contents and write to fd */ for (int n = 0;; n++) { - len = archive_read_data(a, buffer, sizeof buffer); + len = archive_read_data(a, buffer, ARCHIVE_TRANSFER_SIZE); if (len == 0) + { + free(buffer); return 1; + } if (len < 0) { sprintf(status_message, "error archive_read_data('%s')", pathname.c_str()); + free(buffer); return 0; } if (write(fd, buffer, len) != len) { sprintf(status_message, "error write('%s')", pathname.c_str()); + free(buffer); return 0; } } + free(buffer); return 1; } diff --git a/source/zip_util.h b/source/zip_util.h index 3b951f8..fd5aa67 100644 --- a/source/zip_util.h +++ b/source/zip_util.h @@ -10,7 +10,7 @@ #include "common.h" #include "fs.h" -#define ARCHIVE_TRANSFER_SIZE (512 * 1024) +#define ARCHIVE_TRANSFER_SIZE 5242880 static uint8_t MAGIC_ZIP_1[4] = {0x50, 0x4B, 0x03, 0x04}; static uint8_t MAGIC_ZIP_2[4] = {0x50, 0x4B, 0x05, 0x06};