Skip to content

Commit

Permalink
repository: Fix a bug in the download function where not all threads …
Browse files Browse the repository at this point in the history
…are properly joined. Fixed some compiler warnings.
  • Loading branch information
nouwaarom committed Dec 26, 2021
1 parent a710988 commit e58bef9
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 29 deletions.
8 changes: 4 additions & 4 deletions deps/http-get/http-get.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static size_t http_get_cb(void *contents, size_t size, size_t nmemb, void *userp
return realsize;
}

http_get_response_t *http_get_shared(const char *url, CURLSH *share, const char** headers, int header_count) {
http_get_response_t *http_get_shared(const char *url, CURLSH *share, const char** const headers, int header_count) {
CURL *req = curl_easy_init();

http_get_response_t *res = malloc(sizeof(http_get_response_t));
Expand Down Expand Up @@ -79,7 +79,7 @@ http_get_response_t *http_get_shared(const char *url, CURLSH *share, const char*
* Perform an HTTP(S) GET on `url`
*/

http_get_response_t *http_get(const char *url, const char** headers, int header_count) {
http_get_response_t *http_get(const char *url, const char** const headers, int header_count) {
return http_get_shared(url, NULL, headers, header_count);
}

Expand All @@ -97,7 +97,7 @@ static size_t http_get_file_cb(void *ptr, size_t size, size_t nmemb, void *strea
* Request `url` and save to `file`
*/

int http_get_file_shared(const char *url, const char *file, CURLSH *share, const char** headers, int header_count) {
int http_get_file_shared(const char *url, const char *file, CURLSH *share, const char** const headers, int header_count) {
CURL *req = curl_easy_init();
if (!req) return -1;

Expand Down Expand Up @@ -133,7 +133,7 @@ int http_get_file_shared(const char *url, const char *file, CURLSH *share, const
return (200 == status && CURLE_ABORTED_BY_CALLBACK != res) ? 0 : -1;
}

int http_get_file(const char *url, const char *file, const char** headers, int header_count) {
int http_get_file(const char *url, const char *file, const char** const headers, int header_count) {
return http_get_file_shared(url, file, NULL, NULL, 0);
}

Expand Down
9 changes: 5 additions & 4 deletions deps/url/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ get_part (char *url, const char *format, int l) {
}

url_data_t *
url_parse (char *url) {
url_parse (const char *url) {
url_data_t *data = (url_data_t *) malloc(sizeof(url_data_t));
if (!data) return NULL;

data->href = url;
data->href = strdup(url);
char *tmp_url = strdup(url);
bool is_ssh = false;

Expand All @@ -108,7 +108,7 @@ url_parse (char *url) {
int auth_len = 0;
if (strstr(tmp_url, "@")) {
auth = get_part(tmp_url, "%[^@]", protocol_len);
auth_len = strlen(auth);
auth_len = (int)strlen(auth);
if (auth) auth_len++;
}

Expand Down Expand Up @@ -267,7 +267,7 @@ url_get_hostname (char *url) {
char *auth = url_get_auth(url);

if (!protocol) return NULL;
if (auth) l += strlen(auth) + 1; // add one @ symbol
if (auth) l += (int)strlen(auth) + 1; // add one @ symbol
if (auth) free(auth);

l += (int) strlen(protocol);
Expand Down Expand Up @@ -442,6 +442,7 @@ url_data_inspect (url_data_t *data) {
void
url_free (url_data_t *data) {
if (!data) return;
if (data->href) free(data->href);
if (data->auth) free(data->auth);
if (data->protocol) free(data->protocol);
if (data->hostname) free(data->hostname);
Expand Down
2 changes: 1 addition & 1 deletion deps/url/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ typedef struct url_data {
*/

url_data_t *
url_parse (char *url);
url_parse (const char *url);

char *
url_get_protocol (char *url);
Expand Down
2 changes: 1 addition & 1 deletion scripts/feature-test-pthreads
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
echo '#include <pthread.h>' &&
echo 'void *f(void *a) { return 0; }' &&
echo 'int main(void) { pthread_t t; return pthread_create(&t, 0, f, 0); }';
} | ${CC:-cc} -o pthread_test -xc -pthread - 2>/dev/null || rm pthread_test
} | ${CC:-cc} -o pthread_test -xc -pthread - 2>/dev/null && rm pthread_test
exit $?
24 changes: 13 additions & 11 deletions src/common/clib-package-installer.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
char *pkg_dir = NULL;
char *command = NULL;
int rc = 0;
int i = 0;
int thread_index = 0;

#ifdef PATH_MAX
long path_max = PATH_MAX;
Expand Down Expand Up @@ -489,30 +489,32 @@ int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
char* package_id = clib_package_get_id(pkg->author, pkg->repo_name);
// TODO, refactor this.
while ((source = list_iterator_next(iterator))) {
handles[i] = repository_download_package_file(pkg->url, package_id, pkg->version, source->val, pkg_dir);
if (handles[i] == NULL) {
handles[thread_index] = repository_download_package_file(pkg->url, package_id, pkg->version, source->val, pkg_dir);
if (handles[thread_index] == NULL) {
list_iterator_destroy(iterator);
iterator = NULL;
rc = -1;
goto cleanup;
}

#ifdef HAVE_PTHREADS
if (i < max) {
i++;
if (thread_index < (max-1)) {
thread_index++;
} else {
while (--i >= 0) {
repository_file_finish_download(handles[i]);
repository_file_free(handles[i]);
for (int j = 0; j <= thread_index; j++) {
repository_file_finish_download(handles[j]);
repository_file_free(handles[j]);
}
thread_index = 0;
}
#endif
}

#ifdef HAVE_PTHREADS
while (--i >= 0) {
repository_file_finish_download(handles[i]);
repository_file_free(handles[i]);
// Here thread_index is one higher than the actual thread index.
for (int j = 0; j < thread_index; j++) {
repository_file_finish_download(handles[j]);
repository_file_free(handles[j]);
}
#endif

Expand Down
4 changes: 2 additions & 2 deletions src/common/clib-secrets.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ clib_secrets_t clib_secrets_load_from_file(const char *file) {
}

for (unsigned int i = 0; i < json_object_get_count(json_object); i++) {
char *domain = json_object_get_name(json_object, i);
char *secret = json_object_get_string(json_object, domain);
const char *domain = json_object_get_name(json_object, i);
const char *secret = json_object_get_string(json_object, domain);

struct clib_secret *secret_struct = malloc(sizeof(struct clib_secret));
secret_struct->hostname = strdup(domain);
Expand Down
2 changes: 1 addition & 1 deletion src/registry/gitlab-registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ list_t *gitlab_registry_fetch(const char *url, const char *hostname, const char
unsigned int size = strlen(key) + strlen(secret) + 2;
char *authentication_header = malloc(size);
snprintf(authentication_header, size, "%s:%s", key, secret);
res = http_get(url, &authentication_header, 1);
res = http_get(url, (const char **) &authentication_header, 1);
}
if (!res->ok) {
return NULL;
Expand Down
13 changes: 8 additions & 5 deletions src/repository/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <strdup/strdup.h>
#include <url/url.h>

static debug_t _debugger;
Expand Down Expand Up @@ -93,7 +94,7 @@ http_get_response_t *repository_fetch_package_manifest(const char *package_url,
char *authentication_header = malloc(size);
snprintf(authentication_header, size, "%s:%s", key, secret);

res = http_get_shared(manifest_url, clib_package_curl_share, &authentication_header, 1);
res = http_get_shared(manifest_url, clib_package_curl_share, (const char **) &authentication_header, 1);
} else {
res = http_get_shared(manifest_url, clib_package_curl_share, NULL, 0);
}
Expand Down Expand Up @@ -136,7 +137,8 @@ static int fetch_package_file_work(const char *url, const char *dir, const char
return 1;
}

if (!(path = path_join(dir, basename(file)))) {
char* file_copy = strdup(file);
if (!(path = path_join(dir, basename(file_copy)))) {
rc = 1;
goto cleanup;
}
Expand All @@ -159,7 +161,7 @@ static int fetch_package_file_work(const char *url, const char *dir, const char
char *authentication_header = malloc(size);
snprintf(authentication_header, size, "%s:%s", key, secret);

rc = http_get_file_shared(url, path, clib_package_curl_share, &authentication_header, 1);
rc = http_get_file_shared(url, path, clib_package_curl_share, (const char **) &authentication_header, 1);
} else {
rc = http_get_file_shared(url, path, clib_package_curl_share, NULL, 0);
}
Expand Down Expand Up @@ -195,8 +197,9 @@ static int fetch_package_file_work(const char *url, const char *dir, const char
}

cleanup:

free(path);
free(file_copy);

return rc;
}

Expand Down Expand Up @@ -226,7 +229,7 @@ static int fetch_package_file(const char *url, const char *dir, const char *file

memset(fetch, 0, sizeof(*fetch));

fetch->url = url;
fetch->url = strdup(url);
fetch->dir = dir;
fetch->file = file;
fetch->secret = secret;
Expand Down

0 comments on commit e58bef9

Please sign in to comment.