diff --git a/pclsync/psynclib.c b/pclsync/psynclib.c index 8bda933..62ecb0e 100644 --- a/pclsync/psynclib.c +++ b/pclsync/psynclib.c @@ -1737,136 +1737,6 @@ int psync_modify_share(psync_shareid_t shareid, uint32_t permissions, return result; } -static unsigned long psync_parse_version(const char *currentversion) { - unsigned long cv, cm; - cv = cm = 0; - while (1) { - if (*currentversion == '.') { - cv = (cv + cm) * 100; - cm = 0; - } else if (*currentversion == 0) - return cv + cm; - else if (*currentversion >= '0' && *currentversion <= '9') - cm = cm * 10 + *currentversion - '0'; - else - debug(D_WARNING, "invalid characters in version string: %s", - currentversion); - currentversion++; - } -} - -psync_new_version_t *psync_check_new_version_str(const char *os, - const char *currentversion) { - return psync_check_new_version(os, psync_parse_version(currentversion)); -} - -static psync_new_version_t *psync_res_to_ver(const binresult *res, - char *localpath) { - psync_new_version_t *ver; - const char *notes, *versionstr; - size_t lurl, lnotes, lversion, llpath, llocalpath; - const binresult *cres, *pres, *hres; - char *ptr; - unsigned long usize; - cres = papi_find_result2(res, "download", PARAM_HASH); - lurl = sizeof("https://") - 1; - pres = papi_find_result2(cres, "path", PARAM_STR); - lurl += pres->length; - hres = papi_find_result2(cres, "hosts", PARAM_ARRAY)->array[0]; - lurl += hres->length; - lurl = (lurl + sizeof(void *)) / sizeof(void *) * sizeof(void *); - usize = papi_find_result2(cres, "size", PARAM_NUM)->num; - cres = papi_find_result2(res, "notes", PARAM_STR); - notes = cres->str; - lnotes = (cres->length + sizeof(void *)) / sizeof(void *) * sizeof(void *); - cres = papi_find_result2(res, "versionstr", PARAM_STR); - versionstr = cres->str; - lversion = (cres->length + sizeof(void *)) / sizeof(void *) * sizeof(void *); - if (localpath) { - llpath = strlen(localpath); - llocalpath = (llpath + sizeof(void *)) / sizeof(void *) * sizeof(void *); - } else - llpath = llocalpath = 0; - ver = (psync_new_version_t *)psync_malloc(sizeof(psync_new_version_t) + lurl + - lnotes + lversion + llocalpath); - ptr = (char *)(ver + 1); - ver->url = ptr; - memcpy(ptr, "https://", sizeof("https://") - 1); - ptr += sizeof("https://") - 1; - memcpy(ptr, hres->str, hres->length); - ptr += hres->length; - memcpy(ptr, pres->str, pres->length + 1); - ptr = (char *)ver->url + lurl; - memcpy(ptr, notes, lnotes); - ver->notes = ptr; - ptr += lnotes; - memcpy(ptr, versionstr, lversion); - ver->versionstr = ptr; - if (localpath) { - ptr += lversion; - memcpy(ptr, localpath, llpath + 1); - ver->localpath = ptr; - } else - ver->localpath = NULL; - ver->version = papi_find_result2(res, "version", PARAM_NUM)->num; - ver->updatesize = usize; - return ver; -} - -int check_new_version_on_us_socket(binresult **pres, const char *os, - unsigned long currentversion) { - binparam params[] = {PAPI_STR("os", os), PAPI_NUM("version", currentversion)}; - psock_t *api; - binresult *res; - int usessl; - uint64_t result; - - usessl = psync_setting_get_bool(_PS(usessl)); - api = psock_connect("binapi.pcloud.com", - usessl ? PSYNC_API_PORT_SSL : PSYNC_API_PORT, - usessl); - if (unlikely(!api)) { - return -1; - } - res = papi_send2(api, "getlastversion", params); - if (likely(res)) - psync_apipool_release(api); - else { - psync_apipool_release_bad(api); - return -1; - } - result = papi_find_result2(res, "result", PARAM_NUM)->num; - if (result) { - debug(D_WARNING, "command %s returned code %u", "getlastversion", - (unsigned)result); - psync_process_api_error(result); - } - if (result) - psync_free(res); - else - *pres = res; - return (int)result; -} - -psync_new_version_t *psync_check_new_version(const char *os, - unsigned long currentversion) { - psync_new_version_t *ver; - binresult *res; - int ret; - ret = check_new_version_on_us_socket(&res, os, currentversion); - if (ret) { - debug(D_WARNING, "getlastversion returned %d", ret); - return NULL; - } - if (!papi_find_result2(res, "newversion", PARAM_BOOL)->num) { - psync_free(res); - return NULL; - } - ver = psync_res_to_ver(res, NULL); - psync_free(res); - return ver; -} - static void psync_del_all_except(void *ptr, ppath_fast_stat *st) { const char **nmarr; char *fp; @@ -1900,123 +1770,6 @@ static char *psync_filename_from_res(const binresult *res) { return ret; } -static int psync_download_new_version(const binresult *res, char **lpath) { - const char *host; - psync_http_socket *sock; - char *buff, *filename; - uint64_t size; - struct stat st; - int fd; - int rd; - char cookie[128]; - sock = psync_http_connect_multihost( - papi_find_result2(res, "hosts", PARAM_ARRAY), &host); - if (unlikely_log(!sock)) - return -1; - psync_slprintf(cookie, sizeof(cookie), "Cookie: dwltag=%s\015\012", - papi_find_result2(res, "dwltag", PARAM_STR)->str); - if (unlikely_log(psync_http_request( - sock, host, papi_find_result2(res, "path", PARAM_STR)->str, 0, 0, - cookie))) { - psync_http_close(sock); - return -1; - } - if (unlikely_log(psync_http_next_request(sock))) { - psync_http_close(sock); - return 1; - } - size = papi_find_result2(res, "size", PARAM_NUM)->num; - filename = psync_filename_from_res(res); - if (unlikely_log(!filename)) { - psync_http_close(sock); - return 1; - } - if (!stat(filename, &st) && pfile_stat_size(&st) == size) { - *lpath = filename; - psync_http_close(sock); - return 0; - } - if (unlikely_log( - (fd = pfile_open(filename, O_WRONLY, O_CREAT | O_TRUNC)) == - INVALID_HANDLE_VALUE)) { - psync_free(filename); - psync_http_close(sock); - return 1; - } - buff = (char *)psync_malloc(PSYNC_COPY_BUFFER_SIZE); - while (size) { - rd = psync_http_request_readall(sock, buff, PSYNC_COPY_BUFFER_SIZE); - if (unlikely_log(rd <= 0 || pfile_write(fd, buff, rd) != rd)) - break; - size -= rd; - } - psync_free(buff); - pfile_close(fd); - psync_http_close(sock); - if (unlikely_log(size)) { - psync_free(filename); - return -1; - } - *lpath = filename; - return 0; -} - -psync_new_version_t * -psync_check_new_version_download_str(const char *os, - const char *currentversion) { - return psync_check_new_version_download(os, - psync_parse_version(currentversion)); -} - -psync_new_version_t * -psync_check_new_version_download(const char *os, unsigned long currentversion) { - psync_new_version_t *ver; - binresult *res; - char *lfilename; - int ret; - ret = check_new_version_on_us_socket(&res, os, currentversion); - if (unlikely(ret == -1)) - do { - debug(D_WARNING, "could not connect to server, sleeping"); - psys_sleep_milliseconds(10000); - ret = check_new_version_on_us_socket(&res, os, currentversion); - } while (ret == -1); - if (ret) { - debug(D_WARNING, "getlastversion returned %d", ret); - return NULL; - } - if (!papi_find_result2(res, "newversion", PARAM_BOOL)->num) { - psync_free(res); - return NULL; - } - ret = psync_download_new_version( - papi_find_result2(res, "download", PARAM_HASH), &lfilename); - if (unlikely(ret == -1)) - do { - debug(D_WARNING, "could not download update, sleeping"); - psys_sleep_milliseconds(10000); - ret = psync_download_new_version( - papi_find_result2(res, "download", PARAM_HASH), &lfilename); - } while (ret == -1); - if (unlikely_log(ret)) { - psync_free(res); - return NULL; - } - debug(D_NOTICE, "update downloaded to %s", lfilename); - ver = psync_res_to_ver(res, lfilename); - psync_free(lfilename); - psync_free(res); - return ver; -} - -void psync_run_new_version(psync_new_version_t *ver) { - debug(D_NOTICE, "running %s", ver->localpath); - if (pfile_run_update(ver->localpath)) - return; - psync_destroy(); - exit(0); -} - static int psync_upload_result(binresult *res, psync_fileid_t *fileid) { uint64_t result; result = papi_find_result2(res, "result", PARAM_NUM)->num;