Skip to content

Commit e58bef9

Browse files
committed
repository: Fix a bug in the download function where not all threads are properly joined. Fixed some compiler warnings.
1 parent a710988 commit e58bef9

File tree

8 files changed

+35
-29
lines changed

8 files changed

+35
-29
lines changed

deps/http-get/http-get.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static size_t http_get_cb(void *contents, size_t size, size_t nmemb, void *userp
4040
return realsize;
4141
}
4242

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

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

82-
http_get_response_t *http_get(const char *url, const char** headers, int header_count) {
82+
http_get_response_t *http_get(const char *url, const char** const headers, int header_count) {
8383
return http_get_shared(url, NULL, headers, header_count);
8484
}
8585

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

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

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

136-
int http_get_file(const char *url, const char *file, const char** headers, int header_count) {
136+
int http_get_file(const char *url, const char *file, const char** const headers, int header_count) {
137137
return http_get_file_shared(url, file, NULL, NULL, 0);
138138
}
139139

deps/url/url.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ get_part (char *url, const char *format, int l) {
8585
}
8686

8787
url_data_t *
88-
url_parse (char *url) {
88+
url_parse (const char *url) {
8989
url_data_t *data = (url_data_t *) malloc(sizeof(url_data_t));
9090
if (!data) return NULL;
9191

92-
data->href = url;
92+
data->href = strdup(url);
9393
char *tmp_url = strdup(url);
9494
bool is_ssh = false;
9595

@@ -108,7 +108,7 @@ url_parse (char *url) {
108108
int auth_len = 0;
109109
if (strstr(tmp_url, "@")) {
110110
auth = get_part(tmp_url, "%[^@]", protocol_len);
111-
auth_len = strlen(auth);
111+
auth_len = (int)strlen(auth);
112112
if (auth) auth_len++;
113113
}
114114

@@ -267,7 +267,7 @@ url_get_hostname (char *url) {
267267
char *auth = url_get_auth(url);
268268

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

273273
l += (int) strlen(protocol);
@@ -442,6 +442,7 @@ url_data_inspect (url_data_t *data) {
442442
void
443443
url_free (url_data_t *data) {
444444
if (!data) return;
445+
if (data->href) free(data->href);
445446
if (data->auth) free(data->auth);
446447
if (data->protocol) free(data->protocol);
447448
if (data->hostname) free(data->hostname);

deps/url/url.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ typedef struct url_data {
7272
*/
7373

7474
url_data_t *
75-
url_parse (char *url);
75+
url_parse (const char *url);
7676

7777
char *
7878
url_get_protocol (char *url);

scripts/feature-test-pthreads

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
echo '#include <pthread.h>' &&
55
echo 'void *f(void *a) { return 0; }' &&
66
echo 'int main(void) { pthread_t t; return pthread_create(&t, 0, f, 0); }';
7-
} | ${CC:-cc} -o pthread_test -xc -pthread - 2>/dev/null || rm pthread_test
7+
} | ${CC:-cc} -o pthread_test -xc -pthread - 2>/dev/null && rm pthread_test
88
exit $?

src/common/clib-package-installer.c

+13-11
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
302302
char *pkg_dir = NULL;
303303
char *command = NULL;
304304
int rc = 0;
305-
int i = 0;
305+
int thread_index = 0;
306306

307307
#ifdef PATH_MAX
308308
long path_max = PATH_MAX;
@@ -489,30 +489,32 @@ int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
489489
char* package_id = clib_package_get_id(pkg->author, pkg->repo_name);
490490
// TODO, refactor this.
491491
while ((source = list_iterator_next(iterator))) {
492-
handles[i] = repository_download_package_file(pkg->url, package_id, pkg->version, source->val, pkg_dir);
493-
if (handles[i] == NULL) {
492+
handles[thread_index] = repository_download_package_file(pkg->url, package_id, pkg->version, source->val, pkg_dir);
493+
if (handles[thread_index] == NULL) {
494494
list_iterator_destroy(iterator);
495495
iterator = NULL;
496496
rc = -1;
497497
goto cleanup;
498498
}
499499

500500
#ifdef HAVE_PTHREADS
501-
if (i < max) {
502-
i++;
501+
if (thread_index < (max-1)) {
502+
thread_index++;
503503
} else {
504-
while (--i >= 0) {
505-
repository_file_finish_download(handles[i]);
506-
repository_file_free(handles[i]);
504+
for (int j = 0; j <= thread_index; j++) {
505+
repository_file_finish_download(handles[j]);
506+
repository_file_free(handles[j]);
507507
}
508+
thread_index = 0;
508509
}
509510
#endif
510511
}
511512

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

src/common/clib-secrets.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ clib_secrets_t clib_secrets_load_from_file(const char *file) {
5353
}
5454

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

5959
struct clib_secret *secret_struct = malloc(sizeof(struct clib_secret));
6060
secret_struct->hostname = strdup(domain);

src/registry/gitlab-registry.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ list_t *gitlab_registry_fetch(const char *url, const char *hostname, const char
7373
unsigned int size = strlen(key) + strlen(secret) + 2;
7474
char *authentication_header = malloc(size);
7575
snprintf(authentication_header, size, "%s:%s", key, secret);
76-
res = http_get(url, &authentication_header, 1);
76+
res = http_get(url, (const char **) &authentication_header, 1);
7777
}
7878
if (!res->ok) {
7979
return NULL;

src/repository/repository.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <pthread.h>
1919
#include <stdlib.h>
2020
#include <string.h>
21+
#include <strdup/strdup.h>
2122
#include <url/url.h>
2223

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

96-
res = http_get_shared(manifest_url, clib_package_curl_share, &authentication_header, 1);
97+
res = http_get_shared(manifest_url, clib_package_curl_share, (const char **) &authentication_header, 1);
9798
} else {
9899
res = http_get_shared(manifest_url, clib_package_curl_share, NULL, 0);
99100
}
@@ -136,7 +137,8 @@ static int fetch_package_file_work(const char *url, const char *dir, const char
136137
return 1;
137138
}
138139

139-
if (!(path = path_join(dir, basename(file)))) {
140+
char* file_copy = strdup(file);
141+
if (!(path = path_join(dir, basename(file_copy)))) {
140142
rc = 1;
141143
goto cleanup;
142144
}
@@ -159,7 +161,7 @@ static int fetch_package_file_work(const char *url, const char *dir, const char
159161
char *authentication_header = malloc(size);
160162
snprintf(authentication_header, size, "%s:%s", key, secret);
161163

162-
rc = http_get_file_shared(url, path, clib_package_curl_share, &authentication_header, 1);
164+
rc = http_get_file_shared(url, path, clib_package_curl_share, (const char **) &authentication_header, 1);
163165
} else {
164166
rc = http_get_file_shared(url, path, clib_package_curl_share, NULL, 0);
165167
}
@@ -195,8 +197,9 @@ static int fetch_package_file_work(const char *url, const char *dir, const char
195197
}
196198

197199
cleanup:
198-
199200
free(path);
201+
free(file_copy);
202+
200203
return rc;
201204
}
202205

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

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

229-
fetch->url = url;
232+
fetch->url = strdup(url);
230233
fetch->dir = dir;
231234
fetch->file = file;
232235
fetch->secret = secret;

0 commit comments

Comments
 (0)