Skip to content

Commit 75048e8

Browse files
committed
registry: Fix a bug with parsing gitlab registries, fix downloading from
public gitlab repositories misc: Cleanup, download from the correct version when downloading from gitlab.
1 parent da49ef6 commit 75048e8

10 files changed

+63
-57
lines changed

src/clib-install.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,7 @@ int main(int argc, char *argv[]) {
371371
memset(prefix, 0, path_max);
372372
realpath(opts.prefix, prefix);
373373
unsigned long int size = strlen(prefix) + 1;
374-
opts.prefix = malloc(size);
375-
memset((void *) opts.prefix, 0, size);
376-
memcpy((void *) opts.prefix, prefix, size);
374+
opts.prefix = strndup(prefix, size);
377375
}
378376

379377
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);
@@ -407,6 +405,7 @@ int main(int argc, char *argv[]) {
407405

408406
curl_global_cleanup();
409407
clib_package_cleanup();
408+
clib_package_free(root_package);
410409

411410
command_free(&program);
412411
return code;

src/clib-update.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "logger/logger.h"
1717
#include "parson/parson.h"
1818
#include "str-replace/str-replace.h"
19+
#include "strdup/strdup.h"
1920
#include "version.h"
2021
#include <clib-package-installer.h>
2122
#include <curl/curl.h>
@@ -339,9 +340,7 @@ int main(int argc, char *argv[]) {
339340
memset(prefix, 0, path_max);
340341
realpath(package_opts.prefix, prefix);
341342
unsigned long int size = strlen(prefix) + 1;
342-
package_opts.prefix = malloc(size);
343-
memset((void *) package_opts.prefix, 0, size);
344-
memcpy((void *) package_opts.prefix, prefix, size);
343+
package_opts.prefix = strndup(prefix, size);
345344
}
346345

347346
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);

src/clib-upgrade.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "logger/logger.h"
1717
#include "parson/parson.h"
1818
#include "str-replace/str-replace.h"
19+
#include "strdup/strdup.h"
1920
#include "tempdir/tempdir.h"
2021
#include "version.h"
2122
#include <asprintf/asprintf.h>
@@ -232,9 +233,7 @@ int main(int argc, char *argv[]) {
232233
memset(prefix, 0, path_max);
233234
realpath(opts.prefix, prefix);
234235
unsigned long int size = strlen(prefix) + 1;
235-
opts.prefix = malloc(size);
236-
memset((void *) opts.prefix, 0, size);
237-
memcpy((void *) opts.prefix, prefix, size);
236+
opts.prefix = strndup(prefix, size);
238237
}
239238

240239
clib_cache_init(CLIB_PACKAGE_CACHE_TIME);

src/common/clib-package-installer.c

+7-25
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static inline int install_packages(list_t *list, const char *dir, int verbose) {
9898
registry_package_ptr_t package_info = registry_manager_find_package(registries, package_id);
9999
if (!package_info) {
100100
logger_error("package-installer", "Package %s not found in any registry.", package_id);
101-
return -1;
101+
goto loop_cleanup;
102102
}
103103

104104
pkg = clib_package_new_from_slug_and_url(slug, registry_package_get_href(package_info), verbose);
@@ -112,6 +112,7 @@ static inline int install_packages(list_t *list, const char *dir, int verbose) {
112112
error = 0;
113113

114114
loop_cleanup:
115+
free(package_id);
115116
if (slug)
116117
free(slug);
117118
if (error) {
@@ -300,7 +301,6 @@ int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
300301
char *package_json = NULL;
301302
char *pkg_dir = NULL;
302303
char *command = NULL;
303-
int pending = 0;
304304
int rc = 0;
305305
int i = 0;
306306

@@ -485,41 +485,25 @@ int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
485485

486486
iterator = list_iterator_new(pkg->src, LIST_HEAD);
487487
list_node_t *source;
488-
repository_file_handle_t *handles = malloc(pkg->src->len * sizeof(repository_file_handle_t));
488+
repository_file_handle_t *handles = malloc(max * sizeof(repository_file_handle_t));
489+
char* package_id = clib_package_get_id(pkg->author, pkg->repo_name);
490+
// TODO, refactor this.
489491
while ((source = list_iterator_next(iterator))) {
490-
handles[i] = repository_download_package_file(pkg->url, clib_package_get_id(pkg->author, pkg->repo_name), pkg->version, source->val, pkg_dir);
491-
492+
handles[i] = repository_download_package_file(pkg->url, package_id, pkg->version, source->val, pkg_dir);
492493
if (handles[i] == NULL) {
493494
list_iterator_destroy(iterator);
494495
iterator = NULL;
495496
rc = -1;
496497
goto cleanup;
497498
}
498499

499-
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
500-
struct timespec ts = {0, 1000 * 1000 * 10};
501-
nanosleep(&ts, NULL);
502-
#endif
503-
504500
#ifdef HAVE_PTHREADS
505-
if (i < 0) {
506-
i = 0;
507-
}
508-
509-
(void) pending++;
510-
511501
if (i < max) {
512-
(void) i++;
502+
i++;
513503
} else {
514504
while (--i >= 0) {
515505
repository_file_finish_download(handles[i]);
516506
repository_file_free(handles[i]);
517-
(void) pending--;
518-
519-
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
520-
struct timespec ts = {0, 1000 * 1000 * 10};
521-
nanosleep(&ts, NULL);
522-
#endif
523507
}
524508
}
525509
#endif
@@ -528,8 +512,6 @@ int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
528512
#ifdef HAVE_PTHREADS
529513
while (--i >= 0) {
530514
repository_file_finish_download(handles[i]);
531-
532-
(void) pending--;
533515
repository_file_free(handles[i]);
534516
}
535517
#endif

src/registry/gitlab-registry.c

+18-9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@
88
#include "http-get/http-get.h"
99
#include "registry-internal.h"
1010
#include <curl/curl.h>
11-
#include <string.h>
1211
#include <strdup/strdup.h>
12+
#include <string.h>
13+
14+
static char *string_split(char *in, char sep) {
15+
char *next_sep = strchr(in, sep);
16+
if (next_sep == NULL) {
17+
return next_sep;
18+
}
19+
*next_sep = '\0';
20+
return next_sep + sizeof(char);
21+
}
1322

1423
/**
1524
* Parse a list of packages from the given `html`
@@ -21,7 +30,7 @@ static list_t *gitlab_registry_parse(const char *hostname, const char *html) {
2130
char *input = strdup(html);
2231
char *line = input;
2332
char *category = NULL;
24-
while ((line = strtok(line, "\n"))) {
33+
while ((line = string_split(line, '\n'))) {
2534
char *dash_position = strstr(line, "-");
2635
// The line starts with a dash, so we expect a package.
2736
if (dash_position != NULL && dash_position - line < 4) {
@@ -58,14 +67,14 @@ static list_t *gitlab_registry_parse(const char *hostname, const char *html) {
5867
list_t *gitlab_registry_fetch(const char *url, const char *hostname, const char *secret) {
5968
http_get_response_t *res;
6069
if (secret == NULL) {
61-
return NULL;
70+
res = http_get(url, NULL, 0);
71+
} else {
72+
char *key = "PRIVATE-TOKEN";
73+
unsigned int size = strlen(key) + strlen(secret) + 2;
74+
char *authentication_header = malloc(size);
75+
snprintf(authentication_header, size, "%s:%s", key, secret);
76+
res = http_get(url, &authentication_header, 1);
6277
}
63-
64-
char *key = "PRIVATE-TOKEN";
65-
unsigned int size = strlen(key) + strlen(secret) + 2;
66-
char *authentication_header = malloc(size);
67-
snprintf(authentication_header, size, "%s:%s", key, secret);
68-
res = http_get(url, &authentication_header, 1);
6978
if (!res->ok) {
7079
return NULL;
7180
}

src/registry/registry-manager.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void registry_manager_fetch_registries(registries_t registries) {
4242
registry_ptr_t reg;
4343
while ((reg = registry_iterator_next(it))) {
4444
if (!registry_fetch(reg)) {
45-
printf("REGISTRY: could not list packages from. %s\n", registry_get_url(reg));
45+
printf("REGISTRY: could not list packages from. %s secret is: %s\n", registry_get_url(reg), registry_get_secret(reg));
4646
}
4747
}
4848
registry_iterator_destroy(it);

src/registry/registry.c

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ const char *registry_get_url(registry_ptr_t registry) {
9696
return registry->url;
9797
}
9898

99+
const char* registry_get_secret(registry_ptr_t registry) {
100+
return registry->secret;
101+
}
102+
99103
bool registry_fetch(registry_ptr_t registry) {
100104
switch (registry->type) {
101105
case REGISTRY_TYPE_GITLAB:

src/registry/registry.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,17 @@ bool registry_fetch(registry_ptr_t registry);
3737
/**
3838
* Get the url for the registry
3939
* @param registry
40-
* @return
40+
* @return the url
4141
*/
4242
const char* registry_get_url(registry_ptr_t registry);
4343

44+
/**
45+
* Get the secret for this registry
46+
* @param registry
47+
* @return the secret or NULL if there is no secret.
48+
*/
49+
const char* registry_get_secret(registry_ptr_t registry);
50+
4451
/**
4552
* An iterator through the packages in the registry.
4653
*/

src/repository/gitlab-repository.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99
#include <stdlib.h>
1010
#include <string.h>
1111
#include <url/url.h>
12+
#include "str-replace/str-replace.h"
1213

13-
#define GITLAB_API_V4_URL "https://%s/api/v4%s/repository/files/%s/raw?ref=master"
14+
#define GITLAB_API_V4_URL "https://%s/api/v4%s/repository/files/%s/raw?ref=%s"
1415

1516
// GET :hostname/api/v4/projects/:id/repository/files/:file_path/raw
16-
char* gitlab_repository_get_url_for_file(const char*package_url, const char* slug, const char* version, const char *file, const char* secret) {
17+
char *gitlab_repository_get_url_for_file(const char *package_url, const char *slug, const char *version, const char *file, const char *secret) {
1718
url_data_t *parsed = url_parse(package_url);
1819

19-
int size = strlen(parsed->hostname) + strlen(parsed->pathname) + strlen(file) + 64;
20+
char* encoded_filename = str_replace(file, "/", "%2F");
21+
22+
size_t size = strlen(parsed->hostname) + strlen(parsed->pathname) + strlen(encoded_filename) + strlen(GITLAB_API_V4_URL) + strlen(version) + 1;
2023
char *url = malloc(size);
2124
if (url) {
22-
snprintf(url, size, GITLAB_API_V4_URL, parsed->hostname, parsed->pathname, file);
25+
snprintf(url, size, GITLAB_API_V4_URL, parsed->hostname, parsed->pathname, encoded_filename, version);
2326
}
2427

2528
url_free(parsed);

src/repository/repository.c

+12-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static debug_t _debugger;
2929
})
3030

3131
struct repository_file_t {
32-
const char *url;
32+
char *url;
3333
const char *dir;
3434
const char *file;
3535
const char *secret;
@@ -87,7 +87,7 @@ http_get_response_t *repository_fetch_package_manifest(const char *package_url,
8787
char *manifest_url = repository_create_url_for_file(package_url, package_id, version, manifest_file, secret);
8888

8989
http_get_response_t *res;
90-
if (strstr(package_url, "gitlab") != NULL) {
90+
if (secret && strstr(package_url, "gitlab") != NULL) {
9191
char *key = "PRIVATE-TOKEN";
9292
unsigned int size = strlen(key) + strlen(secret) + 2;
9393
char *authentication_header = malloc(size);
@@ -98,6 +98,8 @@ http_get_response_t *repository_fetch_package_manifest(const char *package_url,
9898
res = http_get_shared(manifest_url, clib_package_curl_share, NULL, 0);
9999
}
100100

101+
free(manifest_url);
102+
101103
return res;
102104
}
103105

@@ -116,7 +118,11 @@ repository_file_handle_t repository_download_package_file(const char *package_ur
116118

117119
void repository_file_finish_download(repository_file_handle_t file) {
118120
void *rc;
119-
pthread_join(file->thread, &rc);
121+
int success = pthread_join(file->thread, &rc);
122+
if (success != 0) {
123+
printf("Failed to join thread.\n");
124+
}
125+
free(rc);
120126
}
121127

122128
void repository_file_free(repository_file_handle_t file) {
@@ -133,8 +139,6 @@ static int fetch_package_file_work(const char *url, const char *dir, const char
133139
return 1;
134140
}
135141

136-
_debug("file URL: %s", url);
137-
138142
if (!(path = path_join(dir, basename(file)))) {
139143
rc = 1;
140144
goto cleanup;
@@ -145,14 +149,14 @@ static int fetch_package_file_work(const char *url, const char *dir, const char
145149
#endif
146150

147151
if (package_opts.force || -1 == fs_exists(path)) {
148-
_debug("repository", "fetching %s", url);
152+
_debug("fetching %s", url);
149153
fflush(stdout);
150154

151155
#ifdef HAVE_PTHREADS
152156
pthread_mutex_unlock(&mutex);
153157
#endif
154158

155-
if (strstr(url, "gitlab") != NULL) {
159+
if (secret && strstr(url, "gitlab") != NULL) {
156160
char *key = "PRIVATE-TOKEN";
157161
unsigned int size = strlen(key) + strlen(secret) + 2;
158162
char *authentication_header = malloc(size);
@@ -186,7 +190,7 @@ static int fetch_package_file_work(const char *url, const char *dir, const char
186190
#ifdef HAVE_PTHREADS
187191
pthread_mutex_lock(&mutex);
188192
#endif
189-
_debug("repository", "saved %s", path);
193+
_debug("saved %s", path);
190194
fflush(stdout);
191195
#ifdef HAVE_PTHREADS
192196
pthread_mutex_unlock(&mutex);

0 commit comments

Comments
 (0)