forked from freebsd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkg_jobs.c
2230 lines (1933 loc) · 51.1 KB
/
pkg_jobs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 2011-2012 Baptiste Daroussin <[email protected]>
* Copyright (c) 2011-2012 Julien Laffaye <[email protected]>
* Copyright (c) 2011-2012 Marin Atanasov Nikolov <[email protected]>
* Copyright (c) 2013 Matthew Seaman <[email protected]>
* Copyright (c) 2013-2014 Vsevolod Stakhov <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "pkg_config.h"
#endif
#include <bsd_compat.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <archive.h>
#include <archive_entry.h>
#include <assert.h>
#include <errno.h>
#ifdef HAVE_LIBUTIL_H
#include <libutil.h>
#endif
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <ctype.h>
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#if defined(HAVE_SYS_STATVFS_H)
#include <sys/statvfs.h>
#endif
#include "pkg.h"
#include "private/event.h"
#include "private/pkg.h"
#include "private/pkgdb.h"
#include "private/pkg_jobs.h"
#include "kvec.h"
static int pkg_jobs_find_upgrade(struct pkg_jobs *j, const char *pattern, match_t m);
static int pkg_jobs_fetch(struct pkg_jobs *j);
static bool new_pkg_version(struct pkg_jobs *j);
static int pkg_jobs_check_conflicts(struct pkg_jobs *j);
#define IS_DELETE(j) ((j)->type == PKG_JOBS_DEINSTALL || (j)->type == PKG_JOBS_AUTOREMOVE)
int
pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db)
{
assert(db != NULL);
if ((*j = calloc(1, sizeof(struct pkg_jobs))) == NULL) {
pkg_emit_errno("calloc", "pkg_jobs");
return (EPKG_FATAL);
}
(*j)->universe = pkg_jobs_universe_new(*j);
if ((*j)->universe == NULL) {
free(*j);
return (EPKG_FATAL);
}
(*j)->db = db;
(*j)->type = t;
(*j)->solved = 0;
(*j)->flags = PKG_FLAG_NONE;
return (EPKG_OK);
}
void
pkg_jobs_set_flags(struct pkg_jobs *j, pkg_flags flags)
{
j->flags = flags;
}
int
pkg_jobs_set_repository(struct pkg_jobs *j, const char *ident)
{
if ((pkg_repo_find(ident)) == NULL) {
pkg_emit_error("Unknown repository: %s", ident);
return (EPKG_FATAL);
}
j->reponame = ident;
return (EPKG_OK);
}
int
pkg_jobs_set_destdir(struct pkg_jobs *j, const char *dir)
{
if (dir == NULL)
return (EPKG_FATAL);
j->destdir = dir;
return (EPKG_OK);
}
const char*
pkg_jobs_destdir(struct pkg_jobs *j)
{
return (j->destdir);
}
static void
pkg_jobs_pattern_free(struct job_pattern *jp)
{
free(jp->pattern);
free(jp->path);
free(jp);
}
void
pkg_jobs_request_free(struct pkg_job_request *req)
{
struct pkg_job_request_item *it, *tmp;
if (req != NULL) {
DL_FOREACH_SAFE(req->item, it, tmp) {
free(it);
}
free(req);
}
}
void
pkg_jobs_free(struct pkg_jobs *j)
{
struct pkg_job_request *req, *tmp;
if (j == NULL)
return;
HASH_ITER(hh, j->request_add, req, tmp) {
HASH_DEL(j->request_add, req);
pkg_jobs_request_free(req);
}
HASH_ITER(hh, j->request_delete, req, tmp) {
HASH_DEL(j->request_delete, req);
pkg_jobs_request_free(req);
}
pkg_jobs_universe_free(j->universe);
LL_FREE(j->jobs, free);
HASH_FREE(j->patterns, pkg_jobs_pattern_free);
free(j);
}
static bool
pkg_jobs_maybe_match_file(struct job_pattern *jp, const char *pattern)
{
const char *dot_pos;
char *pkg_path;
assert(jp != NULL);
assert(pattern != NULL);
dot_pos = strrchr(pattern, '.');
if (dot_pos != NULL) {
/*
* Compare suffix with .txz or .tbz
*/
dot_pos ++;
if (strcmp(dot_pos, "txz") == 0 ||
strcmp(dot_pos, "tbz") == 0 ||
strcmp(dot_pos, "tgz") == 0 ||
strcmp(dot_pos, "tar") == 0) {
if ((pkg_path = realpath(pattern, NULL)) != NULL) {
/* Dot pos is one character after the dot */
int len = dot_pos - pattern;
pkg_debug(1, "Jobs> Adding file: %s", pattern);
jp->is_file = true;
jp->path = pkg_path;
jp->pattern = malloc(len);
strlcpy(jp->pattern, pattern, len);
return (true);
}
}
}
else if (strcmp(pattern, "-") == 0) {
/*
* Read package from stdin
*/
jp->is_file = true;
jp->path = strdup(pattern);
jp->pattern = strdup(pattern);
}
return (false);
}
int
pkg_jobs_add(struct pkg_jobs *j, match_t match, char **argv, int argc)
{
struct job_pattern *jp;
int i = 0;
if (j->solved) {
pkg_emit_error("The job has already been solved. "
"Impossible to append new elements");
return (EPKG_FATAL);
}
for (i = 0; i < argc; i++) {
jp = calloc(1, sizeof(struct job_pattern));
if (j->type == PKG_JOBS_DEINSTALL ||
!pkg_jobs_maybe_match_file(jp, argv[i])) {
jp->pattern = strdup(argv[i]);
jp->match = match;
}
HASH_ADD_KEYPTR(hh, j->patterns, jp->pattern, strlen(jp->pattern), jp);
}
if (argc == 0 && match == MATCH_ALL) {
jp = calloc(1, sizeof(struct job_pattern));
jp->pattern = NULL;
jp->match = match;
HASH_ADD_KEYPTR(hh, j->patterns, "all", 3, jp);
}
return (EPKG_OK);
}
bool
pkg_jobs_iter(struct pkg_jobs *jobs, void **iter,
struct pkg **new, struct pkg **old,
int *type)
{
struct pkg_solved *s;
assert(iter != NULL);
if (jobs->jobs == NULL) {
return (false);
}
if (*iter == NULL) {
s = jobs->jobs;
}
else if (*iter == jobs->jobs) {
return (false);
}
else {
s = *iter;
}
*new = s->items[0]->pkg;
*old = s->items[1] ? s->items[1]->pkg : NULL;
*type = s->type;
*iter = s->next ? s->next : jobs->jobs;
return (true);
}
static struct pkg_job_request_item*
pkg_jobs_add_req_from_universe(struct pkg_job_request **head,
struct pkg_job_universe_item *un, bool local, bool automatic)
{
struct pkg_job_request *req;
struct pkg_job_request_item *nit;
struct pkg_job_universe_item *uit;
bool new_req = false;
assert(un != NULL);
HASH_FIND_STR(*head, un->pkg->uid, req);
if (req == NULL) {
req = calloc(1, sizeof(*req));
if (req == NULL) {
pkg_emit_errno("malloc", "struct pkg_job_request");
return (NULL);
}
new_req = true;
req->automatic = automatic;
pkg_debug(4, "add new uid %s to the request", un->pkg->uid);
}
else {
if (req->item->unit == un) {
/* We have exactly the same request, skip it */
return (req->item);
}
}
DL_FOREACH(un, uit) {
if ((uit->pkg->type == PKG_INSTALLED && local) ||
(uit->pkg->type != PKG_INSTALLED && !local)) {
nit = calloc(1, sizeof(*nit));
if (nit == NULL) {
pkg_emit_errno("malloc", "struct pkg_job_request_item");
free(req);
return (NULL);
}
nit->pkg = uit->pkg;
nit->unit = uit;
DL_APPEND(req->item, nit);
}
}
if (new_req) {
if (req->item != NULL) {
HASH_ADD_KEYPTR(hh, *head, un->pkg->uid, strlen(un->pkg->uid), req);
}
else {
free(req);
return (NULL);
}
}
return (req->item);
}
static struct pkg_job_request_item*
pkg_jobs_add_req(struct pkg_jobs *j, struct pkg *pkg)
{
struct pkg_job_request *req, **head;
struct pkg_job_request_item *nit;
struct pkg_job_universe_item *un;
int rc;
assert(pkg != NULL);
if (!IS_DELETE(j)) {
head = &j->request_add;
assert(pkg->type != PKG_INSTALLED);
}
else {
head = &j->request_delete;
assert(pkg->type == PKG_INSTALLED);
}
pkg_debug(4, "universe: add package %s-%s to the request", pkg->name,
pkg->version);
rc = pkg_jobs_universe_add_pkg(j->universe, pkg, false, &un);
if (rc == EPKG_END) {
/*
* This means that we have a package in the universe with the same
* digest. In turn, that means that two upgrade candidates are equal,
* we thus won't do anything with this item, as it is definitely useless
*/
HASH_FIND_STR(*head, pkg->uid, req);
if (req != NULL) {
DL_FOREACH(req->item, nit) {
if (nit->unit == un)
return (nit);
}
}
else {
/*
* We need to add request chain from the universe chain
*/
return (pkg_jobs_add_req_from_universe(head, un, IS_DELETE(j), false));
}
return (NULL);
}
else if (rc == EPKG_FATAL) {
/*
* Something bad has happened
*/
return (NULL);
}
if (pkg->locked) {
pkg_emit_locked(pkg);
return (NULL);
}
HASH_FIND_STR(*head, pkg->uid, req);
nit = calloc(1, sizeof(*nit));
if (nit == NULL) {
pkg_emit_errno("malloc", "struct pkg_job_request_item");
return (NULL);
}
nit->pkg = pkg;
nit->unit = un;
if (req == NULL) {
/* Allocate new unique request item */
req = calloc(1, sizeof(*req));
if (req == NULL) {
pkg_emit_errno("malloc", "struct pkg_job_request");
return (NULL);
}
HASH_ADD_KEYPTR(hh, *head, pkg->uid, strlen(pkg->uid), req);
}
/* Append candidate to the list of candidates */
DL_APPEND(req->item, nit);
return (nit);
}
/*
* Post-process add request and handle flags:
* upgrade - search for upgrades for dependencies and add them to the request
* force - all upgrades are forced
* reverse - try to upgrade reverse deps as well
*/
static void
pkg_jobs_process_add_request(struct pkg_jobs *j)
{
bool force = j->flags & PKG_FLAG_FORCE,
reverse = j->flags & PKG_FLAG_RECURSIVE,
upgrade = j->type == PKG_JOBS_UPGRADE;
struct pkg_job_request *req, *tmp, *found;
struct pkg_job_request_item *it;
struct pkg_job_universe_item *un, *cur;
struct pkg_dep *d;
struct pkg *lp;
int (*deps_func)(const struct pkg *pkg, struct pkg_dep **d);
kvec_t(struct pkg_job_universe_item *) to_process;
if (!upgrade && !reverse)
return;
kv_init(to_process);
HASH_ITER(hh, j->request_add, req, tmp) {
it = req->item;
if (reverse)
deps_func = pkg_rdeps;
else
deps_func = pkg_deps;
d = NULL;
/*
* Here we get deps of local packages only since we are pretty sure
* that they are completely expanded
*/
lp = pkg_jobs_universe_get_local(j->universe,
it->pkg->uid, 0);
while (lp != NULL && deps_func(lp, &d) == EPKG_OK) {
/*
* Do not add duplicated upgrade candidates
*/
HASH_FIND_STR(j->request_add, d->uid, found);
if (found != NULL)
continue;
pkg_debug(4, "adding dependency %s to request", d->uid);
lp = pkg_jobs_universe_get_local(j->universe,
d->uid, 0);
/*
* Here we need to check whether specific remote package
* is newer than a local one
*/
un = pkg_jobs_universe_get_upgrade_candidates(j->universe,
d->uid, lp, force);
if (un == NULL)
continue;
cur = un->prev;
while (cur != un) {
if (cur->pkg->type != PKG_INSTALLED) {
kv_push(typeof(un), to_process, un);
break;
}
cur = cur->prev;
}
}
}
/* Add all items to the request */
for (int i = 0; i < kv_size(to_process); i++) {
un = kv_A(to_process, i);
pkg_jobs_add_req_from_universe(&j->request_add, un, false, true);
}
/* Now recursively process all items checked */
if (kv_size(to_process) > 0)
pkg_jobs_process_add_request(j);
kv_destroy(to_process);
}
/*
* For delete request we merely check rdeps and force flag
*/
static int
pkg_jobs_process_delete_request(struct pkg_jobs *j)
{
bool force = j->flags & PKG_FLAG_FORCE;
struct pkg_job_request *req, *tmp, *found;
struct pkg_dep *d = NULL;
struct pkg *lp;
int rc = EPKG_OK;
kvec_t(struct pkg *) to_process;
if (force)
return (EPKG_OK);
kv_init(to_process);
/*
* Need to add also all reverse deps here
*/
HASH_ITER(hh, j->request_delete, req, tmp) {
d = NULL;
while (pkg_rdeps(req->item->pkg, &d) == EPKG_OK) {
HASH_FIND_STR(j->request_delete, d->uid, found);
if (found)
continue;
lp = pkg_jobs_universe_get_local(j->universe, d->uid, 0);
if (lp) {
if (lp->locked) {
pkg_emit_error("%s is locked, "
"cannot delete %s", lp->name,
req->item->pkg->name);
rc = EPKG_FATAL;
}
kv_push(typeof(lp), to_process, lp);
}
}
}
if (rc == EPKG_FATAL)
return (rc);
for (int i = 0; i < kv_size(to_process); i++) {
lp = kv_A(to_process, i);
if (pkg_jobs_add_req(j, lp) == NULL) {
kv_destroy(to_process);
return (EPKG_FATAL);
}
}
if (kv_size(to_process) > 0)
rc = pkg_jobs_process_delete_request(j);
kv_destroy(to_process);
return (rc);
}
static int
pkg_jobs_set_execute_priority(struct pkg_jobs *j, struct pkg_solved *solved)
{
struct pkg_solved *ts;
if (solved->type == PKG_SOLVED_UPGRADE
&& solved->items[1]->pkg->conflicts != NULL) {
/*
* We have an upgrade request that has some conflicting packages, therefore
* update priorities of local packages and try to update priorities of remote ones
*/
if (solved->items[0]->priority == 0)
pkg_jobs_update_conflict_priority(j->universe, solved);
if (solved->items[1]->priority > solved->items[0]->priority &&
!solved->already_deleted) {
/*
* Split conflicting upgrade request into delete -> upgrade request
*/
ts = calloc(1, sizeof(struct pkg_solved));
if (ts == NULL) {
pkg_emit_errno("calloc", "pkg_solved");
return (EPKG_FATAL);
}
ts->type = PKG_SOLVED_UPGRADE_REMOVE;
ts->items[0] = solved->items[1];
solved->items[1] = NULL;
solved->type = PKG_SOLVED_UPGRADE_INSTALL;
DL_APPEND(j->jobs, ts);
j->count ++;
solved->already_deleted = true;
pkg_debug(2, "split upgrade request for %s",
ts->items[0]->pkg->uid);
return (EPKG_CONFLICT);
}
}
else if (solved->type == PKG_SOLVED_DELETE) {
if (solved->items[0]->priority == 0)
pkg_jobs_update_universe_priority(j->universe, solved->items[0],
PKG_PRIORITY_UPDATE_DELETE);
}
else {
if (solved->items[0]->priority == 0)
pkg_jobs_update_universe_priority(j->universe, solved->items[0],
PKG_PRIORITY_UPDATE_REQUEST);
}
return (EPKG_OK);
}
static int
pkg_jobs_sort_priority(struct pkg_solved *r1, struct pkg_solved *r2)
{
if (r1->items[0]->priority == r2->items[0]->priority) {
if (r1->type == PKG_SOLVED_DELETE && r2->type != PKG_SOLVED_DELETE)
return (-1);
else if (r2->type == PKG_SOLVED_DELETE && r1->type != PKG_SOLVED_DELETE)
return (1);
return (0);
}
return (r2->items[0]->priority - r1->items[0]->priority);
}
static void
pkg_jobs_set_priorities(struct pkg_jobs *j)
{
struct pkg_solved *req;
iter_again:
LL_FOREACH(j->jobs, req) {
req->items[0]->priority = 0;
if (req->items[1] != NULL)
req->items[1]->priority = 0;
}
LL_FOREACH(j->jobs, req) {
if (pkg_jobs_set_execute_priority(j, req) == EPKG_CONFLICT)
goto iter_again;
}
DL_SORT(j->jobs, pkg_jobs_sort_priority);
}
/**
* Test whether package specified is automatic with all its rdeps
* @param j
* @param p
* @return
*/
static bool
pkg_jobs_test_automatic(struct pkg_jobs *j, struct pkg *p)
{
struct pkg_dep *d = NULL;
struct pkg_job_universe_item *unit;
struct pkg *npkg;
bool ret = true;
while (pkg_rdeps(p, &d) == EPKG_OK && ret) {
unit = pkg_jobs_universe_find(j->universe, d->uid);
if (unit != NULL) {
if (!unit->pkg->automatic) {
return (false);
}
npkg = unit->pkg;
}
else {
npkg = pkg_jobs_universe_get_local(j->universe, d->uid,
PKG_LOAD_BASIC|PKG_LOAD_RDEPS|PKG_LOAD_ANNOTATIONS);
if (npkg == NULL)
return (false);
if (!npkg->automatic) {
/*
* Safe to free, as d->uid is not in the universe
*/
pkg_free(npkg);
return (false);
}
if (pkg_jobs_universe_process(j->universe, npkg) != EPKG_OK)
return (false);
}
ret = pkg_jobs_test_automatic(j, npkg);
}
return (ret);
}
static bool
new_pkg_version(struct pkg_jobs *j)
{
struct pkg *p;
const char *uid = "pkg";
pkg_flags old_flags;
bool ret = false;
/* Disable -f for pkg self-check, and restore at end. */
old_flags = j->flags;
j->flags &= ~(PKG_FLAG_FORCE|PKG_FLAG_RECURSIVE);
/* determine local pkgng */
p = pkg_jobs_universe_get_local(j->universe, uid, 0);
if (p == NULL) {
uid = "pkg-devel";
p = pkg_jobs_universe_get_local(j->universe, uid, 0);
}
/* you are using git version skip */
if (p == NULL) {
ret = false;
goto end;
}
/* Use maximum priority for pkg */
if (pkg_jobs_find_upgrade(j, uid, MATCH_EXACT) == EPKG_OK) {
ret = true;
goto end;
}
end:
j->flags = old_flags;
return (ret);
}
static int
pkg_jobs_process_remote_pkg(struct pkg_jobs *j, struct pkg *rp,
struct pkg_job_request_item **req)
{
struct pkg_job_universe_item *nit;
struct pkg_job_request_item *nrit = NULL;
struct pkg *lp = NULL;
if (rp->digest == NULL) {
if (pkg_checksum_calculate(rp, j->db) != EPKG_OK) {
return (EPKG_FATAL);
}
}
if (j->type != PKG_JOBS_FETCH) {
lp = pkg_jobs_universe_get_local(j->universe, rp->uid, 0);
if (lp && lp->locked)
return (EPKG_LOCKED);
}
nit = pkg_jobs_universe_get_upgrade_candidates(j->universe, rp->uid, lp,
j->flags & PKG_FLAG_FORCE);
if (nit != NULL) {
nrit = pkg_jobs_add_req_from_universe(&j->request_add, nit, false, false);
if (req != NULL)
*req = nrit;
}
if (nrit == NULL && lp)
return (EPKG_INSTALLED);
return (nrit != NULL ? EPKG_OK : EPKG_FATAL);
}
static bool
pkg_jobs_has_replacement(struct pkg_jobs *j, const char *uid)
{
struct pkg_job_replace *cur;
LL_FOREACH(j->universe->uid_replaces, cur) {
if (strcmp (cur->new_uid, uid) == 0) {
return (true);
}
}
return (false);
}
static int
pkg_jobs_try_remote_candidate(struct pkg_jobs *j, const char *pattern,
const char *uid, match_t m)
{
struct pkg *p = NULL;
struct pkgdb_it *it;
unsigned flags = PKG_LOAD_BASIC|PKG_LOAD_OPTIONS|PKG_LOAD_DEPS|
PKG_LOAD_REQUIRES|PKG_LOAD_PROVIDES|
PKG_LOAD_SHLIBS_REQUIRED|PKG_LOAD_SHLIBS_PROVIDED|
PKG_LOAD_ANNOTATIONS|PKG_LOAD_CONFLICTS;
int rc = EPKG_FATAL;
struct sbuf *qmsg;
struct pkg_job_universe_item *unit;
if ((it = pkgdb_repo_query(j->db, pattern, m, j->reponame)) == NULL)
return (EPKG_FATAL);
qmsg = sbuf_new_auto();
while (it != NULL && pkgdb_it_next(it, &p, flags) == EPKG_OK) {
if (pkg_jobs_has_replacement(j, p->uid)) {
pkg_debug(1, "replacement %s is already used", p->uid);
continue;
}
sbuf_printf(qmsg, "%s has no direct installation candidates, change it to "
"%s? ", uid, p->uid);
sbuf_finish(qmsg);
if (pkg_emit_query_yesno(true, sbuf_data(qmsg))) {
/* Change the origin of the local package */
pkg_validate(p, j->db);
unit = pkg_jobs_universe_find(j->universe, uid);
if (unit != NULL)
pkg_jobs_universe_change_uid(j->universe, unit, p->uid,
strlen(p->uid), false);
else
assert(0);
rc = EPKG_OK;
pkg_jobs_process_remote_pkg(j, p, NULL);
if (rc == EPKG_OK) {
/* Avoid freeing */
p = NULL;
}
break;
}
sbuf_reset(qmsg);
}
pkg_free(p);
sbuf_free(qmsg);
pkgdb_it_free(it);
return (rc);
}
static int
pkg_jobs_guess_upgrade_candidate(struct pkg_jobs *j, const char *pattern)
{
int rc = EPKG_FATAL;
const char *pos, *opattern = pattern;
char *cpy;
size_t len, olen;
/* First of all, try to search a package with the same name */
pos = strchr(pattern, '/');
if (pos != NULL && pos[1] != '\0') {
if (pkg_jobs_try_remote_candidate(j, pos + 1, opattern, MATCH_EXACT)
== EPKG_OK)
return (EPKG_OK);
pos ++;
pattern = pos;
}
else {
pos = pattern;
}
/* Figure, if we have any numbers at the end of the package */
olen = strlen(pos);
len = olen;
while (len > 0) {
if (isdigit(pos[len - 1]) || pos[len - 1] == '.')
len --;
else
break;
}
if (olen != len) {
/* Try exact pattern without numbers */
cpy = malloc(len + 1);
strlcpy(cpy, pos, len + 1);
if (pkg_jobs_try_remote_candidate(j, cpy, opattern, MATCH_EXACT) != EPKG_OK) {
free(cpy);
cpy = sqlite3_mprintf(" WHERE name REGEXP ('^' || %.*Q || '[0-9.]*$')",
len, pos);
if (pkg_jobs_try_remote_candidate(j, cpy, opattern, MATCH_CONDITION)
== EPKG_OK)
rc = EPKG_OK;
sqlite3_free(cpy);
}
else {
free(cpy);
rc = EPKG_OK;
}
}
return (rc);
}
static int
pkg_jobs_find_upgrade(struct pkg_jobs *j, const char *pattern, match_t m)
{
struct pkg *p = NULL;
struct pkgdb_it *it;
bool found = false;
int rc = EPKG_FATAL;
struct pkg_dep *rdep = NULL;
unsigned flags = PKG_LOAD_BASIC|PKG_LOAD_OPTIONS|PKG_LOAD_DEPS|
PKG_LOAD_REQUIRES|PKG_LOAD_PROVIDES|
PKG_LOAD_SHLIBS_REQUIRED|PKG_LOAD_SHLIBS_PROVIDED|
PKG_LOAD_ANNOTATIONS|PKG_LOAD_CONFLICTS;
struct pkg_job_universe_item *unit = NULL;
if ((it = pkgdb_repo_query(j->db, pattern, m, j->reponame)) == NULL)
rc = EPKG_FATAL;
while (it != NULL && pkgdb_it_next(it, &p, flags) == EPKG_OK) {
rc = pkg_jobs_process_remote_pkg(j, p, NULL);
if (rc == EPKG_FATAL)
break;
else if (rc == EPKG_OK)
found = true;
p = NULL;
}
pkgdb_it_free(it);
if (!found && rc != EPKG_INSTALLED) {
/*
* Here we need to ensure that this package has no
* reverse deps installed
*/
p = pkg_jobs_universe_get_local(j->universe, pattern,
PKG_LOAD_BASIC|PKG_LOAD_RDEPS);
if (p == NULL)
return (EPKG_FATAL);
while(pkg_rdeps(p, &rdep) == EPKG_OK) {
struct pkg *rdep_package;
rdep_package = pkg_jobs_universe_get_local(j->universe, rdep->uid,
PKG_LOAD_BASIC);
if (rdep_package != NULL)
return (EPKG_END);
}
pkg_debug(2, "non-automatic package with pattern %s has not been found in "
"remote repo", pattern);
pkg_jobs_universe_add_pkg(j->universe, p, false, &unit);
rc = pkg_jobs_guess_upgrade_candidate(j, pattern);
}
return (rc);
}
static int
pkg_jobs_check_local_pkg(struct pkg_jobs *j, struct job_pattern *jp)
{
struct pkgdb_it *it;
struct pkg *pkg = NULL;
int rc = EPKG_OK;
it = pkgdb_query(j->db, jp->pattern, jp->match);
if (it != NULL) {
if (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC|PKG_LOAD_ANNOTATIONS) != EPKG_OK)
rc = EPKG_FATAL;
else
pkg_free(pkg);
pkgdb_it_free(it);
}
else {
rc = EPKG_FATAL;
}
return (rc);
}
static int
pkg_jobs_find_remote_pattern(struct pkg_jobs *j, struct job_pattern *jp)
{
int rc = EPKG_OK;
struct pkg *pkg = NULL;
struct pkg_manifest_key *keys = NULL;
struct pkg_job_request *req;
struct job_pattern jfp;
if (!jp->is_file) {
if (j->type == PKG_JOBS_UPGRADE) {
/*
* For upgrade patterns we must ensure that a local package is
* installed as well.
*/
if (pkg_jobs_check_local_pkg(j, jp) != EPKG_OK) {
pkg_emit_error("%s is not installed, therefore upgrade is impossible",
jp->pattern);
return (EPKG_FATAL);
}
}
rc = pkg_jobs_find_upgrade(j, jp->pattern, jp->match);
}
else {
pkg_manifest_keys_new(&keys);
if (pkg_open(&pkg, jp->path, keys, PKG_OPEN_MANIFEST_ONLY) != EPKG_OK) {
rc = EPKG_FATAL;