Coverage Report

Created: 2026-10-08 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/clib/src/common/clib-package.c
Line
Count
Source
1
//
2
// clib-package.c
3
//
4
// Copyright (c) 2014 Stephen Mathieson
5
// Copyright (c) 2014-2020 clib authors
6
// MIT license
7
//
8
9
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
10
#include <unistd.h>
11
#endif
12
13
#include "asprintf/asprintf.h"
14
#include "clib-cache.h"
15
#include "clib-package.h"
16
#include "clib-settings.h"
17
#include "debug/debug.h"
18
#include "fs/fs.h"
19
#include "hash/hash.h"
20
#include "http-get/http-get.h"
21
#include "logger/logger.h"
22
#include "mkdirp/mkdirp.h"
23
#include "parse-repo/parse-repo.h"
24
#include "parson/parson.h"
25
#include "path-join/path-join.h"
26
#include "rimraf/rimraf.h"
27
#include "strdup/strdup.h"
28
#include "substr/substr.h"
29
#include "tempdir/tempdir.h"
30
#include <curl/curl.h>
31
#include <libgen.h>
32
#include <limits.h>
33
#include <stdarg.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37
38
#ifdef HAVE_PTHREADS
39
#include <pthread.h>
40
#endif
41
42
#ifndef DEFAULT_REPO_VERSION
43
195
#define DEFAULT_REPO_VERSION "master"
44
#endif
45
46
#ifndef DEFAULT_REPO_OWNER
47
4.02k
#define DEFAULT_REPO_OWNER "clibs"
48
#endif
49
50
0
#define GITHUB_CONTENT_URL "https://raw.githubusercontent.com/"
51
0
#define GITHUB_CONTENT_URL_WITH_TOKEN "https://%s@raw.githubusercontent.com/"
52
53
#if defined(_WIN32) || defined(WIN32) || defined(__MINGW32__) ||               \
54
    defined(__MINGW64__)
55
#define setenv(k, v, _) _putenv_s(k, v)
56
#define realpath(a, b) _fullpath(a, b, strlen(a))
57
#endif
58
59
static hash_t *visited_packages = 0;
60
61
#ifdef HAVE_PTHREADS
62
typedef struct fetch_package_file_thread_data fetch_package_file_thread_data_t;
63
struct fetch_package_file_thread_data {
64
  clib_package_t *pkg;
65
  const char *dir;
66
  char *file;
67
  int verbose;
68
  pthread_t thread;
69
  pthread_attr_t attr;
70
  void *data;
71
};
72
73
typedef struct clib_package_lock clib_package_lock_t;
74
struct clib_package_lock {
75
  pthread_mutex_t mutex;
76
};
77
78
static clib_package_lock_t lock = {PTHREAD_MUTEX_INITIALIZER};
79
80
#endif
81
82
CURLSH *clib_package_curl_share;
83
debug_t _debugger;
84
85
#define _debug(...)                                                            \
86
318k
  ({                                                                           \
87
318k
    if (!(_debugger.name))                                                     \
88
1
      debug_init(&_debugger, "clib-package");                                  \
89
0
    debug(&_debugger, __VA_ARGS__);                                            \
90
0
  })
91
92
#define E_FORMAT(...)                                                          \
93
0
  ({                                                                           \
94
0
    rc = asprintf(__VA_ARGS__);                                                \
95
0
    if (-1 == rc)                                                              \
96
0
      goto cleanup;                                                            \
97
0
  });
98
99
static clib_package_opts_t opts = {
100
#ifdef HAVE_PTHREADS
101
    .concurrency = MAX_THREADS,
102
#endif
103
    .skip_cache = 1,
104
    .prefix = 0,
105
    .global = 0,
106
    .force = 0,
107
    .token = 0,
108
};
109
110
/**
111
 * Pre-declare prototypes.
112
 */
113
114
static inline char *json_object_get_string_safe(JSON_Object *, const char *);
115
116
static inline char *json_array_get_string_safe(JSON_Array *, int);
117
118
static inline char *clib_package_file_url(const char *, const char *);
119
120
static inline char *clib_package_slug(const char *, const char *, const char *);
121
122
static inline char *clib_package_repo(const char *, const char *);
123
124
static inline list_t *parse_package_deps(JSON_Object *);
125
126
static inline int install_packages(list_t *, const char *, int);
127
128
0
void clib_package_set_opts(clib_package_opts_t o) {
129
0
  if (1 == opts.skip_cache && 0 == o.skip_cache) {
130
0
    opts.skip_cache = 0;
131
0
  } else if (0 == opts.skip_cache && 1 == o.skip_cache) {
132
0
    opts.skip_cache = 1;
133
0
  }
134
135
0
  if (1 == opts.global && 0 == o.global) {
136
0
    opts.global = 0;
137
0
  } else if (0 == opts.global && 1 == o.global) {
138
0
    opts.global = 1;
139
0
  }
140
141
0
  if (1 == opts.force && 0 == o.force) {
142
0
    opts.force = 0;
143
0
  } else if (0 == opts.force && 1 == o.force) {
144
0
    opts.force = 1;
145
0
  }
146
147
0
  if (0 != o.prefix) {
148
0
    if (0 == strlen(o.prefix)) {
149
0
      opts.prefix = 0;
150
0
    } else {
151
0
      opts.prefix = o.prefix;
152
0
    }
153
0
  }
154
155
0
  if (0 != o.token) {
156
0
    if (0 == strlen(o.token)) {
157
0
      opts.token = 0;
158
0
    } else {
159
0
      opts.token = o.token;
160
0
    }
161
0
  }
162
163
0
  if (o.concurrency) {
164
0
    opts.concurrency = o.concurrency;
165
0
  } else if (o.concurrency < 0) {
166
0
    opts.concurrency = 0;
167
0
  }
168
169
0
  if (opts.concurrency < 0) {
170
0
    opts.concurrency = 0;
171
0
  }
172
0
}
173
174
/**
175
 * Create a copy of the result of a `json_object_get_string`
176
 * invocation.  This allows us to `json_value_free()` the
177
 * parent `JSON_Value` without destroying the string.
178
 */
179
180
static inline char *json_object_get_string_safe(JSON_Object *obj,
181
12.9k
                                                const char *key) {
182
12.9k
  const char *val = json_object_get_string(obj, key);
183
12.9k
  if (!val)
184
8.90k
    return NULL;
185
4.04k
  return strdup(val);
186
12.9k
}
187
188
/**
189
 * Create a copy of the result of a `json_array_get_string`
190
 * invocation.  This allows us to `json_value_free()` the
191
 * parent `JSON_Value` without destroying the string.
192
 */
193
194
690k
static inline char *json_array_get_string_safe(JSON_Array *array, int index) {
195
690k
  const char *val = json_array_get_string(array, index);
196
690k
  if (!val)
197
365k
    return NULL;
198
325k
  return strdup(val);
199
690k
}
200
201
/**
202
 * Build a URL for `file` of the package belonging to `url`
203
 */
204
205
0
static inline char *clib_package_file_url(const char *url, const char *file) {
206
0
  if (!url || !file)
207
0
    return NULL;
208
209
0
  int size = strlen(url) + 1    // /
210
0
             + strlen(file) + 1 // \0
211
0
      ;
212
213
0
  char *res = malloc(size);
214
0
  if (res) {
215
0
    memset(res, 0, size);
216
0
    sprintf(res, "%s/%s", url, file);
217
0
  }
218
0
  return res;
219
0
}
220
221
/**
222
 * Build a slug
223
 */
224
225
static inline char *clib_package_slug(const char *author, const char *name,
226
0
                                      const char *version) {
227
0
  int size = strlen(author) + 1    // /
228
0
             + strlen(name) + 1    // @
229
0
             + strlen(version) + 1 // \0
230
0
      ;
231
232
0
  char *slug = malloc(size);
233
0
  if (slug) {
234
0
    memset(slug, '\0', size);
235
0
    sprintf(slug, "%s/%s@%s", author, name, version);
236
0
  }
237
0
  return slug;
238
0
}
239
240
/**
241
 * Load a local package with a manifest.
242
 */
243
244
clib_package_t *clib_package_load_from_manifest(const char *manifest,
245
2.03k
                                                int verbose) {
246
2.03k
  clib_package_t *pkg = NULL;
247
248
2.03k
  if (-1 == fs_exists(manifest)) {
249
0
    logger_error("error", "Missing %s", manifest);
250
0
    return NULL;
251
0
  }
252
253
2.03k
  logger_info("info", "reading local %s", manifest);
254
255
2.03k
  char *json = fs_read(manifest);
256
2.03k
  if (NULL == json)
257
0
    goto e1;
258
259
2.03k
  pkg = clib_package_new(json, verbose);
260
261
2.03k
e1:
262
2.03k
  free(json);
263
264
2.03k
  return pkg;
265
2.03k
}
266
267
/**
268
 * Load a manifest from the current path.
269
 */
270
271
0
clib_package_t *clib_package_load_local_manifest(int verbose) {
272
0
  clib_package_t *pkg = NULL;
273
0
  int i = 0;
274
275
0
  do {
276
0
    const char *name = NULL;
277
0
    name = manifest_names[i];
278
0
    pkg = clib_package_load_from_manifest(name, verbose);
279
0
  } while (pkg == NULL && NULL != manifest_names[++i]);
280
281
0
  return pkg;
282
0
}
283
284
/**
285
 * Build a repo
286
 */
287
288
0
static inline char *clib_package_repo(const char *author, const char *name) {
289
0
  int size = strlen(author) + 1 // /
290
0
             + strlen(name) + 1 // \0
291
0
      ;
292
293
0
  char *repo = malloc(size);
294
0
  if (repo) {
295
0
    memset(repo, '\0', size);
296
0
    sprintf(repo, "%s/%s", author, name);
297
0
  }
298
0
  return repo;
299
0
}
300
301
/**
302
 * Parse the dependencies in the given `obj` into a `list_t`
303
 */
304
305
183
static inline list_t *parse_package_deps(JSON_Object *obj) {
306
183
  list_t *list = NULL;
307
308
183
  if (!obj)
309
0
    goto done;
310
183
  if (!(list = list_new()))
311
0
    goto done;
312
183
  list->free = clib_package_dependency_free;
313
314
4.08k
  for (unsigned int i = 0; i < json_object_get_count(obj); i++) {
315
3.96k
    const char *name = NULL;
316
3.96k
    char *version = NULL;
317
3.96k
    clib_package_dependency_t *dep = NULL;
318
3.96k
    int error = 1;
319
320
3.96k
    if (!(name = json_object_get_name(obj, i)))
321
0
      goto loop_cleanup;
322
3.96k
    if (!(version = json_object_get_string_safe(obj, name)))
323
63
      goto loop_cleanup;
324
3.90k
    if (!(dep = clib_package_dependency_new(name, version)))
325
0
      goto loop_cleanup;
326
327
3.90k
    list_node_t *dep_node = list_node_new(dep);
328
    // note: if we fail to allocate the node itself,
329
    // `dep` will never be pushed on the list
330
3.90k
    if (!dep_node) {
331
0
      clib_package_dependency_free(dep);
332
0
      goto loop_cleanup;
333
0
    }
334
335
3.90k
    if (!(list_rpush(list, dep_node)))
336
0
      goto loop_cleanup;
337
338
3.90k
    error = 0;
339
340
3.96k
  loop_cleanup:
341
3.96k
    if (version)
342
3.90k
      free(version);
343
3.96k
    if (error) {
344
63
      list_destroy(list);
345
63
      list = NULL;
346
63
      break;
347
63
    }
348
3.96k
  }
349
350
183
done:
351
183
  return list;
352
183
}
353
354
0
static inline int install_packages(list_t *list, const char *dir, int verbose) {
355
0
  list_node_t *node = NULL;
356
0
  list_iterator_t *iterator = NULL;
357
0
  int rc = -1;
358
0
  list_t *freelist = NULL;
359
360
0
  if (!list || !dir)
361
0
    goto cleanup;
362
363
0
  iterator = list_iterator_new(list, LIST_HEAD);
364
0
  if (NULL == iterator)
365
0
    goto cleanup;
366
367
0
  freelist = list_new();
368
369
0
  while ((node = list_iterator_next(iterator))) {
370
0
    clib_package_dependency_t *dep = NULL;
371
0
    char *slug = NULL;
372
0
    clib_package_t *pkg = NULL;
373
0
    int error = 1;
374
375
0
    dep = (clib_package_dependency_t *)node->val;
376
0
    slug = clib_package_slug(dep->author, dep->name, dep->version);
377
0
    if (NULL == slug)
378
0
      goto loop_cleanup;
379
380
0
    pkg = clib_package_new_from_slug(slug, verbose);
381
0
    if (NULL == pkg)
382
0
      goto loop_cleanup;
383
384
0
    if (-1 == clib_package_install(pkg, dir, verbose))
385
0
      goto loop_cleanup;
386
387
0
    list_rpush(freelist, list_node_new(pkg));
388
0
    error = 0;
389
390
0
  loop_cleanup:
391
0
    if (slug)
392
0
      free(slug);
393
0
    if (error) {
394
0
      list_iterator_destroy(iterator);
395
0
      iterator = NULL;
396
0
      rc = -1;
397
0
      goto cleanup;
398
0
    }
399
0
  }
400
401
0
  rc = 0;
402
403
0
cleanup:
404
0
  if (iterator)
405
0
    list_iterator_destroy(iterator);
406
407
0
  if (freelist) {
408
0
    iterator = list_iterator_new(freelist, LIST_HEAD);
409
0
    while ((node = list_iterator_next(iterator))) {
410
0
      clib_package_t *pkg = node->val;
411
0
      if (pkg)
412
0
        clib_package_free(pkg);
413
0
    }
414
0
    list_iterator_destroy(iterator);
415
0
    list_destroy(freelist);
416
0
  }
417
0
  return rc;
418
0
}
419
420
#ifdef HAVE_PTHREADS
421
static void curl_lock_callback(CURL *handle, curl_lock_data data,
422
0
                               curl_lock_access access, void *userptr) {
423
0
  pthread_mutex_lock(&lock.mutex);
424
0
}
425
426
static void curl_unlock_callback(CURL *handle, curl_lock_data data,
427
0
                                 curl_lock_access access, void *userptr) {
428
0
  pthread_mutex_unlock(&lock.mutex);
429
0
}
430
431
0
static void init_curl_share() {
432
0
  if (0 == clib_package_curl_share) {
433
0
    pthread_mutex_lock(&lock.mutex);
434
0
    clib_package_curl_share = curl_share_init();
435
0
    curl_share_setopt(clib_package_curl_share, CURLSHOPT_SHARE,
436
0
                      CURL_LOCK_DATA_CONNECT);
437
0
    curl_share_setopt(clib_package_curl_share, CURLSHOPT_LOCKFUNC,
438
0
                      curl_lock_callback);
439
0
    curl_share_setopt(clib_package_curl_share, CURLSHOPT_UNLOCKFUNC,
440
0
                      curl_unlock_callback);
441
0
    curl_share_setopt(clib_package_curl_share, CURLOPT_NETRC,
442
0
                      CURL_NETRC_OPTIONAL);
443
0
    pthread_mutex_unlock(&lock.mutex);
444
0
  }
445
0
}
446
#endif
447
448
/**
449
 * Create a new clib package from the given `json`
450
 */
451
452
2.03k
clib_package_t *clib_package_new(const char *json, int verbose) {
453
2.03k
  clib_package_t *pkg = NULL;
454
2.03k
  JSON_Value *root = NULL;
455
2.03k
  JSON_Object *json_object = NULL;
456
2.03k
  JSON_Array *src = NULL;
457
2.03k
  JSON_Object *deps = NULL;
458
2.03k
  JSON_Object *devs = NULL;
459
2.03k
  int error = 1;
460
461
2.03k
  if (!json) {
462
0
    if (verbose) {
463
0
      logger_error("error", "missing JSON to parse");
464
0
    }
465
0
    goto cleanup;
466
0
  }
467
468
2.03k
  if (!(root = json_parse_string(json))) {
469
1.19k
    if (verbose) {
470
0
      logger_error("error", "unable to parse JSON");
471
0
    }
472
1.19k
    goto cleanup;
473
1.19k
  }
474
475
836
  if (!(json_object = json_value_get_object(root))) {
476
19
    if (verbose) {
477
0
      logger_error("error", "invalid clib.json or package.json file");
478
0
    }
479
19
    goto cleanup;
480
19
  }
481
482
817
  if (!(pkg = malloc(sizeof(clib_package_t)))) {
483
0
    goto cleanup;
484
0
  }
485
486
817
  memset(pkg, 0, sizeof(clib_package_t));
487
488
817
  pkg->json = strdup(json);
489
817
  pkg->name = json_object_get_string_safe(json_object, "name");
490
817
  pkg->repo = json_object_get_string_safe(json_object, "repo");
491
817
  pkg->version = json_object_get_string_safe(json_object, "version");
492
817
  pkg->license = json_object_get_string_safe(json_object, "license");
493
817
  pkg->description = json_object_get_string_safe(json_object, "description");
494
817
  pkg->configure = json_object_get_string_safe(json_object, "configure");
495
817
  pkg->install = json_object_get_string_safe(json_object, "install");
496
817
  pkg->makefile = json_object_get_string_safe(json_object, "makefile");
497
817
  pkg->prefix = json_object_get_string_safe(json_object, "prefix");
498
817
  pkg->flags = json_object_get_string_safe(json_object, "flags");
499
500
817
  if (!pkg->flags) {
501
812
    pkg->flags = json_object_get_string_safe(json_object, "cflags");
502
812
  }
503
504
  // try as array
505
817
  if (!pkg->flags) {
506
811
    JSON_Array *flags = json_object_get_array(json_object, "flags");
507
508
811
    if (!flags) {
509
737
      flags = json_object_get_array(json_object, "cflags");
510
737
    }
511
512
811
    if (flags) {
513
379k
      for (unsigned int i = 0; i < json_array_get_count(flags); i++) {
514
379k
        char *flag = json_array_get_string_safe(flags, i);
515
379k
        if (flag) {
516
13.3k
          char *old_flags = pkg->flags;
517
518
13.3k
          if (!pkg->flags) {
519
36
            pkg->flags = "";
520
36
          }
521
522
13.3k
          if (-1 == asprintf(&pkg->flags, "%s %s", pkg->flags, flag)) {
523
0
            goto cleanup;
524
0
          }
525
526
13.3k
          free(old_flags);
527
13.3k
          free(flag);
528
13.3k
        }
529
379k
      }
530
75
    }
531
811
  }
532
533
817
  if (!pkg->repo && pkg->author && pkg->name) {
534
0
    asprintf(&pkg->repo, "%s/%s", pkg->author, pkg->name);
535
0
    _debug("creating package: %s", pkg->repo);
536
0
  }
537
538
817
  if (!pkg->author) {
539
817
    _debug("unable to determine package author for: %s", pkg->name);
540
817
  }
541
542
  // TODO npm-style "repository" (thlorenz/gumbo-parser.c#1)
543
817
  if (pkg->repo) {
544
123
    pkg->author = parse_repo_owner(pkg->repo, DEFAULT_REPO_OWNER);
545
    // repo name may not be package name (thing.c -> thing)
546
123
    pkg->repo_name = parse_repo_name(pkg->repo);
547
694
  } else {
548
694
    if (verbose) {
549
0
      logger_warn("warning",
550
0
                  "missing repo in clib.json or package.json file for %s",
551
0
                  pkg->name);
552
0
    }
553
694
    pkg->author = NULL;
554
694
    pkg->repo_name = NULL;
555
694
  }
556
557
817
  src = json_object_get_array(json_object, "src");
558
559
817
  if (!src) {
560
749
    src = json_object_get_array(json_object, "files");
561
749
  }
562
563
817
  if (src) {
564
80
    if (!(pkg->src = list_new()))
565
0
      goto cleanup;
566
80
    pkg->src->free = free;
567
311k
    for (unsigned int i = 0; i < json_array_get_count(src); i++) {
568
311k
      char *file = json_array_get_string_safe(src, i);
569
311k
      _debug("file: %s", file);
570
311k
      if (!file)
571
29
        goto cleanup;
572
311k
      if (!(list_rpush(pkg->src, list_node_new(file))))
573
0
        goto cleanup;
574
311k
    }
575
737
  } else {
576
737
    _debug("no src files listed in clib.json or package.json file");
577
737
    pkg->src = NULL;
578
737
  }
579
580
788
  if ((deps = json_object_get_object(json_object, "dependencies"))) {
581
31
    if (!(pkg->dependencies = parse_package_deps(deps))) {
582
4
      goto cleanup;
583
4
    }
584
757
  } else {
585
757
    _debug("no dependencies listed in clib.json or package.json file");
586
757
    pkg->dependencies = NULL;
587
757
  }
588
589
784
  if ((devs = json_object_get_object(json_object, "development"))) {
590
152
    if (!(pkg->development = parse_package_deps(devs))) {
591
59
      goto cleanup;
592
59
    }
593
632
  } else {
594
632
    _debug(
595
632
        "no development dependencies listed in clib.json or package.json file");
596
632
    pkg->development = NULL;
597
632
  }
598
599
725
  error = 0;
600
601
2.03k
cleanup:
602
2.03k
  if (root)
603
836
    json_value_free(root);
604
2.03k
  if (error && pkg) {
605
92
    clib_package_free(pkg);
606
92
    pkg = NULL;
607
92
  }
608
2.03k
  return pkg;
609
725
}
610
611
static clib_package_t *
612
clib_package_new_from_slug_with_package_name(const char *slug, int verbose,
613
0
                                             const char *file) {
614
0
  char *author = NULL;
615
0
  char *name = NULL;
616
0
  char *version = NULL;
617
0
  char *url = NULL;
618
0
  char *json_url = NULL;
619
0
  char *repo = NULL;
620
0
  char *json = NULL;
621
0
  char *log = NULL;
622
0
  http_get_response_t *res = NULL;
623
0
  clib_package_t *pkg = NULL;
624
0
  int retries = 3;
625
626
  // parse chunks
627
0
  if (!slug)
628
0
    goto error;
629
0
  _debug("creating package: %s", slug);
630
0
  if (!(author = parse_repo_owner(slug, DEFAULT_REPO_OWNER)))
631
0
    goto error;
632
0
  if (!(name = parse_repo_name(slug)))
633
0
    goto error;
634
0
  if (!(version = parse_repo_version(slug, DEFAULT_REPO_VERSION)))
635
0
    goto error;
636
0
  if (!(url = clib_package_url(author, name, version)))
637
0
    goto error;
638
0
  if (!(json_url = clib_package_file_url(url, file)))
639
0
    goto error;
640
641
0
  _debug("author: %s", author);
642
0
  _debug("name: %s", name);
643
0
  _debug("version: %s", version);
644
645
0
#ifdef HAVE_PTHREADS
646
0
  pthread_mutex_lock(&lock.mutex);
647
0
#endif
648
  // fetch json
649
0
  if (clib_cache_has_json(author, name, version)) {
650
0
    if (opts.skip_cache) {
651
0
      clib_cache_delete_json(author, name, version);
652
0
      goto download;
653
0
    }
654
655
0
    json = clib_cache_read_json(author, name, version);
656
657
0
    if (!json) {
658
0
      goto download;
659
0
    }
660
661
0
    log = "cache";
662
0
#ifdef HAVE_PTHREADS
663
0
    pthread_mutex_unlock(&lock.mutex);
664
0
#endif
665
0
  } else {
666
0
  download:
667
0
#ifdef HAVE_PTHREADS
668
0
    pthread_mutex_unlock(&lock.mutex);
669
0
#endif
670
0
    if (retries-- <= 0) {
671
0
      goto error;
672
0
    } else {
673
0
#ifdef HAVE_PTHREADS
674
0
      init_curl_share();
675
0
      _debug("GET %s", json_url);
676
      // clean up when retrying
677
0
      http_get_free(res);
678
0
      res = http_get_shared(json_url, clib_package_curl_share);
679
#else
680
      res = http_get(json_url);
681
#endif
682
0
      json = res->data;
683
0
      _debug("status: %d", res->status);
684
0
      if (!res || !res->ok) {
685
0
        goto download;
686
0
      }
687
0
      log = "fetch";
688
0
    }
689
0
  }
690
691
0
  if (verbose) {
692
0
    logger_info(log, "%s/%s:%s", author, name, file);
693
0
  }
694
695
0
  free(json_url);
696
0
  json_url = NULL;
697
0
  free(name);
698
0
  name = NULL;
699
700
0
  if (json) {
701
    // build package
702
0
    pkg = clib_package_new(json, verbose);
703
0
  }
704
705
0
  if (!pkg)
706
0
    goto error;
707
708
  // force version number
709
0
  if (pkg->version) {
710
0
    if (0 != strcmp(version, DEFAULT_REPO_VERSION)) {
711
0
      _debug("forcing version number: %s (%s)", version, pkg->version);
712
0
      free(pkg->version);
713
0
      pkg->version = version;
714
0
    } else {
715
0
      free(version);
716
0
      version = NULL;
717
0
    }
718
0
  } else {
719
0
    pkg->version = version;
720
0
  }
721
722
  // force package author (don't know how this could fail)
723
0
  if (pkg->author) {
724
0
    if (0 != strcmp(author, pkg->author)) {
725
0
      free(pkg->author);
726
0
      pkg->author = author;
727
0
    } else {
728
0
      free(author);
729
0
      author = NULL;
730
0
    }
731
0
  } else {
732
0
    pkg->author = strdup(author);
733
0
  }
734
735
0
  if (!(repo = clib_package_repo(pkg->author, pkg->name))) {
736
0
    goto error;
737
0
  }
738
739
0
  if (pkg->repo) {
740
0
    if (0 != strcmp(repo, pkg->repo)) {
741
0
      free(url);
742
0
      if (!(url = clib_package_url_from_repo(pkg->repo, pkg->version)))
743
0
        goto error;
744
0
    }
745
0
    free(repo);
746
0
    repo = NULL;
747
0
  } else {
748
0
    pkg->repo = repo;
749
0
  }
750
751
0
  pkg->url = url;
752
753
0
#ifdef HAVE_PTHREADS
754
0
  pthread_mutex_lock(&lock.mutex);
755
0
#endif
756
  // cache json
757
0
  if (pkg && pkg->author && pkg->name && pkg->version) {
758
0
    if (-1 ==
759
0
        clib_cache_save_json(pkg->author, pkg->name, pkg->version, json)) {
760
0
      _debug("failed to cache JSON for: %s/%s@%s", pkg->author, pkg->name,
761
0
             pkg->version);
762
0
    } else {
763
0
      _debug("cached json: %s/%s@%s", pkg->author, pkg->name, pkg->version);
764
0
    }
765
0
  }
766
0
#ifdef HAVE_PTHREADS
767
0
  pthread_mutex_unlock(&lock.mutex);
768
0
#endif
769
770
0
  if (res) {
771
0
    http_get_free(res);
772
0
    json = NULL;
773
0
    res = NULL;
774
0
  } else {
775
0
    free(json);
776
0
    json = NULL;
777
0
  }
778
779
0
  return pkg;
780
781
0
error:
782
0
  if (0 == retries) {
783
0
    if (verbose && author && name && file) {
784
0
      logger_warn("warning", "unable to fetch %s/%s:%s", author, name, file);
785
0
    }
786
0
  }
787
788
0
  free(author);
789
0
  free(name);
790
0
  free(version);
791
0
  free(url);
792
0
  free(json_url);
793
0
  free(repo);
794
0
  if (!res && json)
795
0
    free(json);
796
0
  if (res)
797
0
    http_get_free(res);
798
0
  if (pkg)
799
0
    clib_package_free(pkg);
800
0
  return NULL;
801
0
}
802
803
/**
804
 * Create a package from the given repo `slug`
805
 */
806
807
0
clib_package_t *clib_package_new_from_slug(const char *slug, int verbose) {
808
0
  clib_package_t *package = NULL;
809
0
  const char *name = NULL;
810
0
  unsigned int i = 0;
811
812
0
  do {
813
0
    name = manifest_names[i];
814
0
    package = clib_package_new_from_slug_with_package_name(slug, verbose, name);
815
0
    if (NULL != package) {
816
0
      package->filename = (char *)name;
817
0
    }
818
0
  } while (NULL != manifest_names[++i] && NULL == package);
819
820
0
  return package;
821
0
}
822
823
/**
824
 * Get a slug for the package `author/name@version`
825
 */
826
827
char *clib_package_url(const char *author, const char *name,
828
0
                       const char *version) {
829
0
  if (!author || !name || !version)
830
0
    return NULL;
831
0
  int size = strlen(GITHUB_CONTENT_URL) + strlen(author) + 1 // /
832
0
             + strlen(name) + 1                              // /
833
0
             + strlen(version) + 1                           // \0
834
0
      ;
835
836
0
  if (0 != opts.token) {
837
0
    size += strlen(opts.token);
838
0
    size += 1; // @
839
0
  }
840
841
0
  char *slug = malloc(size);
842
0
  if (slug) {
843
0
    memset(slug, '\0', size);
844
0
    if (0 != opts.token) {
845
0
      sprintf(slug, GITHUB_CONTENT_URL_WITH_TOKEN "%s/%s/%s", opts.token,
846
0
              author, name, version);
847
0
    } else {
848
0
      sprintf(slug, GITHUB_CONTENT_URL "%s/%s/%s", author, name, version);
849
0
    }
850
0
  }
851
852
0
  return slug;
853
0
}
854
855
0
char *clib_package_url_from_repo(const char *repo, const char *version) {
856
0
  if (!repo || !version)
857
0
    return NULL;
858
0
  int size = strlen(GITHUB_CONTENT_URL) + strlen(repo) + 1 // /
859
0
             + strlen(version) + 1                         // \0
860
0
      ;
861
862
0
  if (0 != opts.token) {
863
0
    size += strlen(opts.token);
864
0
    size += 1; // @
865
0
  }
866
867
0
  char *slug = malloc(size);
868
0
  if (slug) {
869
0
    memset(slug, '\0', size);
870
0
    if (0 != opts.token) {
871
0
      sprintf(slug, GITHUB_CONTENT_URL_WITH_TOKEN "%s/%s", opts.token, repo,
872
0
              version);
873
0
    } else {
874
0
      sprintf(slug, GITHUB_CONTENT_URL "%s/%s", repo, version);
875
0
    }
876
0
  }
877
0
  return slug;
878
0
}
879
880
/**
881
 * Parse the package author from the given `slug`
882
 */
883
884
3.90k
char *clib_package_parse_author(const char *slug) {
885
3.90k
  return parse_repo_owner(slug, DEFAULT_REPO_OWNER);
886
3.90k
}
887
888
/**
889
 * Parse the package version from the given `slug`
890
 */
891
892
0
char *clib_package_parse_version(const char *slug) {
893
0
  return parse_repo_version(slug, DEFAULT_REPO_VERSION);
894
0
}
895
896
/**
897
 * Parse the package name from the given `slug`
898
 */
899
900
3.90k
char *clib_package_parse_name(const char *slug) {
901
3.90k
  return parse_repo_name(slug);
902
3.90k
}
903
904
/**
905
 * Create a new package dependency from the given `repo` and `version`
906
 */
907
908
clib_package_dependency_t *clib_package_dependency_new(const char *repo,
909
3.90k
                                                       const char *version) {
910
3.90k
  if (!repo || !version)
911
0
    return NULL;
912
913
3.90k
  clib_package_dependency_t *dep = malloc(sizeof(clib_package_dependency_t));
914
3.90k
  if (!dep) {
915
0
    return NULL;
916
0
  }
917
918
3.90k
  dep->version = 0 == strcmp("*", version) ? strdup(DEFAULT_REPO_VERSION)
919
3.90k
                                           : strdup(version);
920
3.90k
  dep->name = clib_package_parse_name(repo);
921
3.90k
  dep->author = clib_package_parse_author(repo);
922
923
3.90k
  _debug("dependency: %s/%s@%s", dep->author, dep->name, dep->version);
924
3.90k
  return dep;
925
3.90k
}
926
927
static int fetch_package_file_work(clib_package_t *pkg, const char *dir,
928
0
                                   char *file, int verbose) {
929
0
  char *url = NULL;
930
0
  char *path = NULL;
931
0
  int saved = 0;
932
0
  int rc = 0;
933
934
0
  _debug("fetch file: %s/%s", pkg->repo, file);
935
936
0
  if (NULL == pkg) {
937
0
    return 1;
938
0
  }
939
940
0
  if (NULL == pkg->url) {
941
0
    return 1;
942
0
  }
943
944
0
  if (0 == strncmp(file, "http", 4)) {
945
0
    url = strdup(file);
946
0
  } else if (!(url = clib_package_file_url(pkg->url, file))) {
947
0
    return 1;
948
0
  }
949
950
0
  _debug("file URL: %s", url);
951
952
0
  char *base_path = strdup(basename(file));
953
954
0
  if (!base_path) {
955
0
    rc = 1;
956
0
    goto cleanup;
957
0
  }
958
959
0
  path = path_join(dir, base_path);
960
961
0
  free(base_path);
962
963
0
  if (!path) {
964
0
    rc = 1;
965
0
    goto cleanup;
966
0
  }
967
968
0
#ifdef HAVE_PTHREADS
969
0
  pthread_mutex_lock(&lock.mutex);
970
0
#endif
971
972
0
  if (1 == opts.force || -1 == fs_exists(path)) {
973
0
    if (verbose) {
974
0
      logger_info("fetch", "%s:%s", pkg->repo, file);
975
0
      fflush(stdout);
976
0
    }
977
978
0
#ifdef HAVE_PTHREADS
979
0
    pthread_mutex_unlock(&lock.mutex);
980
0
#endif
981
982
0
    rc = http_get_file_shared(url, path, clib_package_curl_share);
983
0
    saved = 1;
984
0
  } else {
985
0
#ifdef HAVE_PTHREADS
986
0
    pthread_mutex_unlock(&lock.mutex);
987
0
#endif
988
0
  }
989
990
0
  if (-1 == rc) {
991
0
    if (verbose) {
992
0
#ifdef HAVE_PTHREADS
993
0
      pthread_mutex_lock(&lock.mutex);
994
0
#endif
995
0
      logger_error("error", "unable to fetch %s:%s", pkg->repo, file);
996
0
      fflush(stderr);
997
0
      rc = 1;
998
0
#ifdef HAVE_PTHREADS
999
0
      pthread_mutex_unlock(&lock.mutex);
1000
0
#endif
1001
0
      goto cleanup;
1002
0
    }
1003
0
  }
1004
1005
0
  if (saved) {
1006
0
    if (verbose) {
1007
0
#ifdef HAVE_PTHREADS
1008
0
      pthread_mutex_lock(&lock.mutex);
1009
0
#endif
1010
0
      logger_info("save", path);
1011
0
      fflush(stdout);
1012
0
#ifdef HAVE_PTHREADS
1013
0
      pthread_mutex_unlock(&lock.mutex);
1014
0
#endif
1015
0
    }
1016
0
  }
1017
1018
0
cleanup:
1019
1020
0
  free(url);
1021
0
  free(path);
1022
0
  return rc;
1023
0
}
1024
1025
#ifdef HAVE_PTHREADS
1026
0
static void *fetch_package_file_thread(void *arg) {
1027
0
  fetch_package_file_thread_data_t *data = arg;
1028
0
  int *status = malloc(sizeof(int));
1029
0
  int rc =
1030
0
      fetch_package_file_work(data->pkg, data->dir, data->file, data->verbose);
1031
0
  *status = rc;
1032
0
  (void)data->pkg->refs--;
1033
0
  pthread_exit((void *)status);
1034
0
  return (void *)(intptr_t)rc;
1035
0
}
1036
#endif
1037
1038
/**
1039
 * Fetch a file associated with the given `pkg`.
1040
 *
1041
 * Returns 0 on success.
1042
 */
1043
1044
static int fetch_package_file(clib_package_t *pkg, const char *dir, char *file,
1045
0
                              int verbose, void **data) {
1046
#ifndef HAVE_PTHREADS
1047
  return fetch_package_file_work(pkg, dir, file, verbose);
1048
#else
1049
0
  fetch_package_file_thread_data_t *fetch = malloc(sizeof(*fetch));
1050
0
  int rc = 0;
1051
1052
0
  if (0 == fetch) {
1053
0
    return -1;
1054
0
  }
1055
1056
0
  *data = 0;
1057
1058
0
  memset(fetch, 0, sizeof(*fetch));
1059
1060
0
  fetch->pkg = pkg;
1061
0
  fetch->dir = dir;
1062
0
  fetch->file = file;
1063
0
  fetch->verbose = verbose;
1064
1065
0
  rc = pthread_attr_init(&fetch->attr);
1066
1067
0
  if (0 != rc) {
1068
0
    free(fetch);
1069
0
    return rc;
1070
0
  }
1071
1072
0
  (void)pkg->refs++;
1073
0
  rc = pthread_create(&fetch->thread, NULL, fetch_package_file_thread, fetch);
1074
1075
0
  if (0 != rc) {
1076
0
    pthread_attr_destroy(&fetch->attr);
1077
0
    free(fetch);
1078
0
    return rc;
1079
0
  }
1080
1081
0
  rc = pthread_attr_destroy(&fetch->attr);
1082
1083
0
  if (0 != rc) {
1084
0
    pthread_cancel(fetch->thread);
1085
0
    free(fetch);
1086
0
    return rc;
1087
0
  }
1088
1089
0
  *data = fetch;
1090
1091
0
  return rc;
1092
0
#endif
1093
0
}
1094
1095
0
static void set_prefix(clib_package_t *pkg, long path_max) {
1096
0
  if (NULL != opts.prefix || NULL != pkg->prefix) {
1097
0
    char path[path_max];
1098
0
    memset(path, 0, path_max);
1099
1100
0
    if (opts.prefix) {
1101
0
      realpath(opts.prefix, path);
1102
0
    } else {
1103
0
      realpath(pkg->prefix, path);
1104
0
    }
1105
1106
0
    _debug("env: PREFIX: %s", path);
1107
0
    setenv("PREFIX", path, 1);
1108
0
    mkdirp(path, 0777);
1109
0
  }
1110
0
}
1111
1112
int clib_package_install_executable(clib_package_t *pkg, const char *dir,
1113
0
                                    int verbose) {
1114
0
#ifdef PATH_MAX
1115
0
  long path_max = PATH_MAX;
1116
#elif defined(_PC_PATH_MAX)
1117
  long path_max = pathconf(dir, _PC_PATH_MAX);
1118
#else
1119
  long path_max = 4096;
1120
#endif
1121
1122
0
  int rc;
1123
0
  char *url = NULL;
1124
0
  char *file = NULL;
1125
0
  char *tarball = NULL;
1126
0
  char *command = NULL;
1127
0
  char *unpack_dir = NULL;
1128
0
  char *deps = NULL;
1129
0
  char *tmp = NULL;
1130
0
  char *reponame = NULL;
1131
0
  char dir_path[path_max];
1132
1133
0
  _debug("install executable %s", pkg->repo);
1134
1135
0
  tmp = gettempdir();
1136
1137
0
  if (NULL == tmp) {
1138
0
    if (verbose) {
1139
0
      logger_error("error", "gettempdir() out of memory");
1140
0
    }
1141
0
    return -1;
1142
0
  }
1143
1144
0
  if (!pkg->repo) {
1145
0
    if (verbose) {
1146
0
      logger_error("error", "repo field required to install executable");
1147
0
    }
1148
0
    return -1;
1149
0
  }
1150
1151
0
  reponame = strrchr(pkg->repo, '/');
1152
0
  if (reponame && *reponame != '\0')
1153
0
    reponame++;
1154
0
  else {
1155
0
    if (verbose) {
1156
0
      logger_error("error",
1157
0
                   "malformed repo field, must be in the form user/pkg");
1158
0
    }
1159
0
    return -1;
1160
0
  }
1161
1162
0
  E_FORMAT(&url, "https://github.com/%s/archive/%s.tar.gz", pkg->repo,
1163
0
           pkg->version);
1164
1165
0
  E_FORMAT(&file, "%s-%s.tar.gz", reponame, pkg->version);
1166
1167
0
  E_FORMAT(&tarball, "%s/%s", tmp, file);
1168
1169
0
  rc = http_get_file_shared(url, tarball, clib_package_curl_share);
1170
1171
0
  if (0 != rc) {
1172
0
    if (verbose) {
1173
0
      logger_error("error", "download failed for '%s@%s' - HTTP GET '%s'",
1174
0
                   pkg->repo, pkg->version, url);
1175
0
    }
1176
1177
0
    goto cleanup;
1178
0
  }
1179
1180
0
  E_FORMAT(&command, "cd %s && gzip -dc %s | tar x", tmp, file);
1181
1182
0
  _debug("download url: %s", url);
1183
0
  _debug("file: %s", file);
1184
0
  _debug("tarball: %s", tarball);
1185
0
  _debug("command(extract): %s", command);
1186
1187
  // cheap untar
1188
0
  rc = system(command);
1189
0
  if (0 != rc)
1190
0
    goto cleanup;
1191
1192
0
  free(command);
1193
0
  command = NULL;
1194
1195
0
  set_prefix(pkg, path_max);
1196
1197
0
  const char *configure = pkg->configure;
1198
1199
0
  if (0 == configure) {
1200
0
    configure = ":";
1201
0
  }
1202
1203
0
  memset(dir_path, 0, path_max);
1204
0
  realpath(dir, dir_path);
1205
1206
0
  char *version = pkg->version;
1207
0
  if ('v' == version[0]) {
1208
0
    (void)version++;
1209
0
  }
1210
1211
0
  E_FORMAT(&unpack_dir, "%s/%s-%s", tmp, reponame, version);
1212
1213
0
  _debug("dir: %s", unpack_dir);
1214
1215
0
  if (pkg->dependencies) {
1216
0
    E_FORMAT(&deps, "%s/deps", unpack_dir);
1217
0
    _debug("deps: %s", deps);
1218
0
    rc = clib_package_install_dependencies(pkg, deps, verbose);
1219
0
    if (-1 == rc)
1220
0
      goto cleanup;
1221
0
  }
1222
1223
0
  if (!opts.global && pkg->makefile) {
1224
0
    E_FORMAT(&command, "cp -fr %s/%s/%s %s", dir_path, pkg->name,
1225
0
             basename(pkg->makefile), unpack_dir);
1226
1227
0
    rc = system(command);
1228
0
    if (0 != rc) {
1229
0
      goto cleanup;
1230
0
    }
1231
1232
0
    free(command);
1233
0
  }
1234
1235
0
  if (pkg->flags) {
1236
0
    char *flags = NULL;
1237
#ifdef _GNU_SOURCE
1238
    char *cflags = secure_getenv("CFLAGS");
1239
#else
1240
0
    char *cflags = getenv("CFLAGS");
1241
0
#endif
1242
1243
0
    if (cflags) {
1244
0
      asprintf(&flags, "%s %s", cflags, pkg->flags);
1245
0
    } else {
1246
0
      asprintf(&flags, "%s", pkg->flags);
1247
0
    }
1248
1249
0
    setenv("CFLAGS", cflags, 1);
1250
0
  }
1251
1252
0
  E_FORMAT(&command, "cd %s && %s", unpack_dir, pkg->install);
1253
1254
0
  _debug("command(install): %s", command);
1255
0
  rc = system(command);
1256
1257
0
cleanup:
1258
0
  free(tmp);
1259
0
  free(command);
1260
0
  free(tarball);
1261
0
  free(file);
1262
0
  free(url);
1263
0
  return rc;
1264
0
}
1265
1266
/**
1267
 * Install the given `pkg` in `dir`
1268
 */
1269
1270
0
int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
1271
0
  list_iterator_t *iterator = NULL;
1272
0
  char *package_json = NULL;
1273
0
  char *pkg_dir = NULL;
1274
0
  char *command = NULL;
1275
0
  int pending = 0;
1276
0
  int rc = 0;
1277
0
  int i = 0;
1278
1279
0
#ifdef PATH_MAX
1280
0
  long path_max = PATH_MAX;
1281
#elif defined(_PC_PATH_MAX)
1282
  long path_max = pathconf(dir, _PC_PATH_MAX);
1283
#else
1284
  long path_max = 4096;
1285
#endif
1286
1287
0
#ifdef HAVE_PTHREADS
1288
0
  int max = opts.concurrency;
1289
0
#endif
1290
1291
#ifdef CLIB_PACKAGE_PREFIX
1292
  if (0 == opts.prefix) {
1293
#ifdef HAVE_PTHREADS
1294
    pthread_mutex_lock(&lock.mutex);
1295
#endif
1296
    opts.prefix = CLIB_PACKAGE_PREFIX;
1297
#ifdef HAVE_PTHREADS
1298
    pthread_mutex_unlock(&lock.mutex);
1299
#endif
1300
  }
1301
#endif
1302
1303
0
  if (0 == opts.prefix) {
1304
0
#ifdef HAVE_PTHREADS
1305
0
    pthread_mutex_lock(&lock.mutex);
1306
0
#endif
1307
#ifdef _GNU_SOURCE
1308
    char *prefix = secure_getenv("PREFIX");
1309
#else
1310
0
    char *prefix = getenv("PREFIX");
1311
0
#endif
1312
1313
0
    if (prefix) {
1314
0
      opts.prefix = prefix;
1315
0
    }
1316
0
#ifdef HAVE_PTHREADS
1317
0
    pthread_mutex_unlock(&lock.mutex);
1318
0
#endif
1319
0
  }
1320
1321
0
  if (0 == visited_packages) {
1322
0
#ifdef HAVE_PTHREADS
1323
0
    pthread_mutex_lock(&lock.mutex);
1324
0
#endif
1325
1326
0
    visited_packages = hash_new();
1327
    // initial write because sometimes `hash_set()` crashes
1328
0
    hash_set(visited_packages, strdup(""), "");
1329
1330
0
#ifdef HAVE_PTHREADS
1331
0
    pthread_mutex_unlock(&lock.mutex);
1332
0
#endif
1333
0
  }
1334
1335
0
  if (0 == opts.force && pkg && pkg->name) {
1336
0
#ifdef HAVE_PTHREADS
1337
0
    pthread_mutex_lock(&lock.mutex);
1338
0
#endif
1339
1340
0
    if (hash_has(visited_packages, pkg->name)) {
1341
0
#ifdef HAVE_PTHREADS
1342
0
      pthread_mutex_unlock(&lock.mutex);
1343
0
#endif
1344
0
      return 0;
1345
0
    }
1346
1347
0
#ifdef HAVE_PTHREADS
1348
0
    pthread_mutex_unlock(&lock.mutex);
1349
0
#endif
1350
0
  }
1351
1352
0
#ifdef HAVE_PTHREADS
1353
0
  fetch_package_file_thread_data_t **fetchs = 0;
1354
0
  if (NULL != pkg && NULL != pkg->src) {
1355
0
    if (pkg->src->len > 0) {
1356
0
      fetchs = malloc(pkg->src->len * sizeof(fetch_package_file_thread_data_t));
1357
0
    }
1358
0
  }
1359
1360
0
  if (fetchs) {
1361
0
    memset(fetchs, 0, pkg->src->len * sizeof(fetch_package_file_thread_data_t));
1362
0
  }
1363
1364
0
#endif
1365
1366
0
  if (!pkg || !dir) {
1367
0
    rc = -1;
1368
0
    goto cleanup;
1369
0
  }
1370
1371
0
  set_prefix(pkg, path_max);
1372
1373
0
  if (!(pkg_dir = path_join(dir, pkg->name))) {
1374
0
    rc = -1;
1375
0
    goto cleanup;
1376
0
  }
1377
1378
0
  if (!opts.global) {
1379
0
    _debug("mkdir -p %s", pkg_dir);
1380
    // create directory for pkg
1381
0
    if (-1 == mkdirp(pkg_dir, 0777)) {
1382
0
      rc = -1;
1383
0
      goto cleanup;
1384
0
    }
1385
0
  }
1386
1387
0
  if (NULL == pkg->url) {
1388
0
    pkg->url = clib_package_url(pkg->author, pkg->repo_name, pkg->version);
1389
1390
0
    if (NULL == pkg->url) {
1391
0
      rc = -1;
1392
0
      goto cleanup;
1393
0
    }
1394
0
  }
1395
1396
  // write clib.json or package.json
1397
0
  if (!(package_json = path_join(pkg_dir, pkg->filename))) {
1398
0
    rc = -1;
1399
0
    goto cleanup;
1400
0
  }
1401
1402
0
  if (!opts.global && NULL != pkg->src) {
1403
0
    _debug("write: %s", package_json);
1404
0
    if (-1 == fs_write(package_json, pkg->json)) {
1405
0
      if (verbose) {
1406
0
        logger_error("error", "Failed to write %s", package_json);
1407
0
      }
1408
1409
0
      rc = -1;
1410
0
      goto cleanup;
1411
0
    }
1412
0
  }
1413
1414
0
  if (pkg->name) {
1415
0
#ifdef HAVE_PTHREADS
1416
0
    pthread_mutex_lock(&lock.mutex);
1417
0
#endif
1418
0
    if (!hash_has(visited_packages, pkg->name)) {
1419
0
      hash_set(visited_packages, strdup(pkg->name), "t");
1420
0
    }
1421
0
#ifdef HAVE_PTHREADS
1422
0
    pthread_mutex_unlock(&lock.mutex);
1423
0
#endif
1424
0
  }
1425
1426
  // fetch makefile
1427
0
  if (!opts.global && pkg->makefile) {
1428
0
    _debug("fetch: %s/%s", pkg->repo, pkg->makefile);
1429
0
    void *fetch = 0;
1430
0
    rc = fetch_package_file(pkg, pkg_dir, pkg->makefile, verbose, &fetch);
1431
0
    if (0 != rc) {
1432
0
      goto cleanup;
1433
0
    }
1434
1435
0
#ifdef HAVE_PTHREADS
1436
0
    if (0 != fetch) {
1437
0
      fetch_package_file_thread_data_t *data = fetch;
1438
0
      int *status;
1439
0
      pthread_join(data->thread, (void **)&status);
1440
0
      if (NULL != status) {
1441
0
        rc = *status;
1442
0
        free(status);
1443
0
        status = 0;
1444
0
        if (0 != rc) {
1445
0
          rc = 0;
1446
0
          logger_warn("warning", "unable to fetch Makefile (%s) for '%s'",
1447
0
                      pkg->makefile, pkg->name);
1448
0
        }
1449
0
      }
1450
0
    }
1451
0
#endif
1452
0
  }
1453
1454
  // if no sources are listed, just install
1455
0
  if (opts.global || NULL == pkg->src)
1456
0
    goto install;
1457
1458
0
#ifdef HAVE_PTHREADS
1459
0
  pthread_mutex_lock(&lock.mutex);
1460
0
#endif
1461
1462
0
  if (clib_cache_has_package(pkg->author, pkg->name, pkg->version)) {
1463
0
    if (opts.skip_cache) {
1464
0
      clib_cache_delete_package(pkg->author, pkg->name, pkg->version);
1465
0
#ifdef HAVE_PTHREADS
1466
0
      pthread_mutex_unlock(&lock.mutex);
1467
0
#endif
1468
0
      goto download;
1469
0
    }
1470
1471
0
    if (0 != clib_cache_load_package(pkg->author, pkg->name, pkg->version,
1472
0
                                     pkg_dir)) {
1473
0
#ifdef HAVE_PTHREADS
1474
0
      pthread_mutex_unlock(&lock.mutex);
1475
0
#endif
1476
0
      goto download;
1477
0
    }
1478
1479
0
    if (verbose) {
1480
0
      logger_info("cache", pkg->repo);
1481
0
    }
1482
1483
0
#ifdef HAVE_PTHREADS
1484
0
    pthread_mutex_unlock(&lock.mutex);
1485
0
#endif
1486
1487
0
    goto install;
1488
0
  }
1489
1490
0
#ifdef HAVE_PTHREADS
1491
0
  pthread_mutex_unlock(&lock.mutex);
1492
0
#endif
1493
1494
0
download:
1495
1496
0
  iterator = list_iterator_new(pkg->src, LIST_HEAD);
1497
0
  list_node_t *source;
1498
1499
0
  while ((source = list_iterator_next(iterator))) {
1500
0
    void *fetch = NULL;
1501
0
    rc = fetch_package_file(pkg, pkg_dir, source->val, verbose, &fetch);
1502
1503
0
    if (0 != rc) {
1504
0
      list_iterator_destroy(iterator);
1505
0
      iterator = NULL;
1506
0
      rc = -1;
1507
0
      goto cleanup;
1508
0
    }
1509
1510
0
#ifdef HAVE_PTHREADS
1511
0
    if (i < 0) {
1512
0
      i = 0;
1513
0
    }
1514
1515
0
    fetchs[i] = fetch;
1516
1517
0
    (void)pending++;
1518
1519
0
    if (i < (max - 1)) {
1520
0
      (void)i++;
1521
0
    } else {
1522
0
      for (int j = 0; j <= i; j++) {
1523
0
        fetch_package_file_thread_data_t *data = fetchs[j];
1524
0
        int *status;
1525
0
        pthread_join(data->thread, (void **)&status);
1526
0
        free(data);
1527
0
        fetchs[j] = NULL;
1528
1529
0
        (void)pending--;
1530
1531
0
        if (NULL != status) {
1532
0
          rc = *status;
1533
0
          free(status);
1534
0
          status = 0;
1535
0
        }
1536
1537
0
        if (0 != rc) {
1538
0
          rc = -1;
1539
0
          goto cleanup;
1540
0
        }
1541
0
      }
1542
0
      i = 0;
1543
0
    }
1544
0
#endif
1545
0
  }
1546
1547
0
#ifdef HAVE_PTHREADS
1548
  // Here there are i-1 threads running.
1549
0
  for (int j = 0; j < i; j++) {
1550
0
    fetch_package_file_thread_data_t *data = fetchs[j];
1551
0
    int *status;
1552
1553
0
    pthread_join(data->thread, (void **)&status);
1554
1555
0
    (void)pending--;
1556
0
    free(data);
1557
0
    fetchs[j] = NULL;
1558
1559
0
    if (NULL != status) {
1560
0
      rc = *status;
1561
0
      free(status);
1562
0
      status = 0;
1563
0
    }
1564
1565
0
    if (0 != rc) {
1566
0
      rc = -1;
1567
0
      goto cleanup;
1568
0
    }
1569
0
  }
1570
0
#endif
1571
1572
0
install:
1573
0
  if (pkg->configure) {
1574
0
    E_FORMAT(&command, "cd %s/%s && %s", dir, pkg->name, pkg->configure);
1575
1576
0
    _debug("command(configure): %s", command);
1577
1578
0
    rc = system(command);
1579
0
    if (0 != rc)
1580
0
      goto cleanup;
1581
0
  }
1582
1583
0
  if (0 == rc && pkg->install) {
1584
0
    rc = clib_package_install_executable(pkg, dir, verbose);
1585
0
  }
1586
1587
0
  if (0 == rc) {
1588
0
    rc = clib_package_install_dependencies(pkg, dir, verbose);
1589
0
  }
1590
1591
0
#ifdef HAVE_PTHREADS
1592
0
  pthread_mutex_lock(&lock.mutex);
1593
0
#endif
1594
0
  if (0 == rc) {
1595
0
    clib_cache_save_package(pkg->author, pkg->name, pkg->version, pkg_dir);
1596
0
    _debug("cached package: %s/%s@%s", pkg->author, pkg->name, pkg->version);
1597
0
  }
1598
0
#ifdef HAVE_PTHREADS
1599
0
  pthread_mutex_unlock(&lock.mutex);
1600
0
#endif
1601
1602
0
cleanup:
1603
0
  if (pkg_dir) {
1604
0
    if (0 != rc) {
1605
0
      rimraf(pkg_dir);
1606
0
      _debug("deleted inconsistent package dir: %s", pkg_dir);
1607
0
    }
1608
1609
0
    free(pkg_dir);
1610
0
  }
1611
0
  if (package_json)
1612
0
    free(package_json);
1613
0
  if (iterator)
1614
0
    list_iterator_destroy(iterator);
1615
0
  if (command)
1616
0
    free(command);
1617
0
#ifdef HAVE_PTHREADS
1618
0
  if (NULL != pkg && NULL != pkg->src) {
1619
0
    if (pkg->src->len > 0) {
1620
0
      if (fetchs) {
1621
0
        free(fetchs);
1622
0
      }
1623
0
    }
1624
0
  }
1625
0
  fetchs = NULL;
1626
0
#endif
1627
1628
0
#ifdef HAVE_PTHREADS
1629
0
  pthread_mutex_lock(&lock.mutex);
1630
0
#endif
1631
0
  if (0 != rc && pkg) {
1632
0
    clib_cache_delete_json(pkg->author, pkg->name, pkg->version);
1633
0
    _debug("deleted json cache: %s/%s@%s", pkg->author, pkg->name,
1634
0
           pkg->version);
1635
0
  }
1636
0
#ifdef HAVE_PTHREADS
1637
0
  pthread_mutex_unlock(&lock.mutex);
1638
0
#endif
1639
1640
0
  return rc;
1641
0
}
1642
1643
/**
1644
 * Install the given `pkg`'s dependencies in `dir`
1645
 */
1646
1647
int clib_package_install_dependencies(clib_package_t *pkg, const char *dir,
1648
0
                                      int verbose) {
1649
0
  if (!pkg || !dir)
1650
0
    return -1;
1651
0
  if (NULL == pkg->dependencies)
1652
0
    return 0;
1653
1654
0
  return install_packages(pkg->dependencies, dir, verbose);
1655
0
}
1656
1657
/**
1658
 * Install the given `pkg`'s development dependencies in `dir`
1659
 */
1660
1661
int clib_package_install_development(clib_package_t *pkg, const char *dir,
1662
0
                                     int verbose) {
1663
0
  if (!pkg || !dir)
1664
0
    return -1;
1665
0
  if (NULL == pkg->development)
1666
0
    return 0;
1667
1668
0
  return install_packages(pkg->development, dir, verbose);
1669
0
}
1670
1671
/**
1672
 * Free a clib package
1673
 */
1674
1675
817
void clib_package_free(clib_package_t *pkg) {
1676
817
  if (NULL == pkg) {
1677
0
    return;
1678
0
  }
1679
1680
817
  if (0 != pkg->refs) {
1681
0
    return;
1682
0
  }
1683
1684
817
#define FREE(k)                                                                \
1685
10.6k
  if (pkg->k) {                                                                \
1686
1.17k
    free(pkg->k);                                                              \
1687
1.17k
    pkg->k = 0;                                                                \
1688
1.17k
  }
1689
817
  FREE(author);
1690
817
  FREE(description);
1691
817
  FREE(install);
1692
817
  FREE(json);
1693
817
  FREE(license);
1694
817
  FREE(name);
1695
817
  FREE(makefile);
1696
817
  FREE(configure);
1697
817
  FREE(repo);
1698
817
  FREE(repo_name);
1699
817
  FREE(url);
1700
817
  FREE(version);
1701
817
  FREE(flags);
1702
817
#undef FREE
1703
1704
817
  if (pkg->src)
1705
80
    list_destroy(pkg->src);
1706
817
  pkg->src = 0;
1707
1708
817
  if (pkg->dependencies)
1709
27
    list_destroy(pkg->dependencies);
1710
817
  pkg->dependencies = 0;
1711
1712
817
  if (pkg->development)
1713
93
    list_destroy(pkg->development);
1714
817
  pkg->development = 0;
1715
1716
817
  free(pkg);
1717
817
  pkg = 0;
1718
817
}
1719
1720
3.90k
void clib_package_dependency_free(void *_dep) {
1721
3.90k
  clib_package_dependency_t *dep = (clib_package_dependency_t *)_dep;
1722
3.90k
  free(dep->name);
1723
3.90k
  free(dep->author);
1724
3.90k
  free(dep->version);
1725
3.90k
  free(dep);
1726
3.90k
}
1727
1728
0
void clib_package_cleanup() {
1729
0
  if (0 != visited_packages) {
1730
0
    hash_each(visited_packages, {
1731
0
      free((void *)key);
1732
0
      (void)val;
1733
0
    });
1734
1735
0
    hash_free(visited_packages);
1736
0
    visited_packages = 0;
1737
0
  }
1738
1739
0
  curl_share_cleanup(clib_package_curl_share);
1740
0
}