forked from NetBSDfr/pkgin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.c
911 lines (757 loc) · 21.5 KB
/
actions.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
/*
* Copyright (c) 2009-2015 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Emile "iMil" Heitor <[email protected]> .
*
* 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.
* 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
*/
#include "pkgin.h"
#include <sys/ioctl.h>
#include <time.h>
#include <unistd.h>
#if defined(HAVE_SYS_TERMIOS_H) && !defined(__FreeBSD__)
#include <sys/termios.h>
#elif HAVE_TERMIOS_H
#include <termios.h>
#endif
#ifndef LOCALBASE
#define LOCALBASE "/usr/pkg" /* see DISCLAIMER below */
#endif
static int upgrade_type = UPGRADE_NONE, warn_count = 0, err_count = 0;
static uint8_t said = 0;
FILE *err_fp = NULL;
long int rm_filepos = -1;
char pkgtools_flags[5];
#ifndef DEBUG
static char *
verb_flag(const char *flags)
{
strcpy(pkgtools_flags, flags);
if (verbosity)
strlcat(pkgtools_flags, "v", 1);
return pkgtools_flags;
}
#endif
static int
pkg_download(Plisthead *installhead)
{
FILE *fp;
Pkglist *pinstall;
struct stat st;
char pkg_fs[BUFSIZ];
char *p = NULL;
off_t size;
int rc = EXIT_SUCCESS;
SLIST_FOREACH(pinstall, installhead, next) {
/*
* pkgin_install() should have already marked whether this
* package requires downloading or not.
*/
if (!pinstall->download)
continue;
/*
* We don't (yet) support resume so start by explicitly
* removing any existing file. pkgin_install() has already
* checked to see if it's valid, and we know it is not.
*/
(void) snprintf(pkg_fs, BUFSIZ, "%s/%s%s", pkgin_cache,
pinstall->depend, PKG_EXT);
(void) unlink(pkg_fs);
(void) umask(DEF_UMASK);
if (strncmp(pinstall->pkgurl, "file:///", 8) == 0) {
/*
* If this package repository URL is file:// we can
* just symlink rather than copying. We do not support
* file:// URLs with a host component.
*/
p = &pinstall->pkgurl[7];
if (stat(p, &st) != 0) {
fprintf(stderr, MSG_PKG_NOT_AVAIL,
pinstall->depend);
rc = EXIT_FAILURE;
if (check_yesno(DEFAULT_NO) == ANSW_NO)
exit(rc);
pinstall->file_size = -1;
continue;
}
if (symlink(p, pkg_fs) < 0)
errx(EXIT_FAILURE,
"Failed to create symlink %s", pkg_fs);
size = st.st_size;
} else {
/*
* Fetch via HTTP. download_pkg() handles printing
* errors from various failure modes, so we handle
* cleanup only.
*/
if ((fp = fopen(pkg_fs, "w")) == NULL)
err(EXIT_FAILURE, MSG_ERR_OPEN, pkg_fs);
if ((size = download_pkg(pinstall->pkgurl, fp)) == -1) {
(void) fclose(fp);
(void) unlink(pkg_fs);
rc = EXIT_FAILURE;
if (check_yesno(DEFAULT_NO) == ANSW_NO)
exit(rc);
pinstall->file_size = -1;
continue;
}
(void) fclose(fp);
}
/*
* download_pkg() already checked that we received the size
* specified by the server, this checks that it matches what
* is recorded by pkg_summary.
*/
if (size != pinstall->file_size) {
(void) unlink(pkg_fs);
rc = EXIT_FAILURE;
(void) fprintf(stderr, "download error: %s size"
" does not match pkg_summary\n", pinstall->depend);
if (check_yesno(DEFAULT_NO) == ANSW_NO)
exit(rc);
pinstall->file_size = -1;
continue;
}
} /* download loop */
return rc;
}
/**
* \brief Analyse pkgin_errlog for warnings
*/
static void
analyse_pkglog(long int filepos)
{
FILE *err_ro;
char err_line[BUFSIZ];
if (filepos < 0)
return;
err_ro = fopen(pkgin_errlog, "r");
(void)fseek(err_ro, filepos, SEEK_SET);
while (fgets(err_line, BUFSIZ, err_ro) != NULL) {
/* Warning: [...] was built for a platform */
if (strstr(err_line, "Warning") != NULL)
warn_count++;
/* 1 package addition failed */
if (strstr(err_line, "addition failed") != NULL)
err_count++;
/* Can't install dependency */
if (strstr(err_line, "an\'t install") != NULL)
err_count++;
/* unable to verify signature */
if (strstr(err_line, "unable to verify signature") != NULL)
err_count++;
}
fclose(err_ro);
}
/**
* \brief Tags pkgin_errlog with date
*/
#define DATELEN 64
#ifndef DEBUG
static void
log_tag(const char *fmt, ...)
{
va_list ap;
char log_action[BUFSIZ], now_date[DATELEN];
struct tm tim;
time_t now;
now = time(NULL);
tim = *(localtime(&now));
va_start(ap, fmt);
vsnprintf(log_action, BUFSIZ, fmt, ap);
va_end(ap);
(void)strftime(now_date, DATELEN, "%b %d %H:%M:%S", &tim);
fprintf(err_fp, "---%s: %s", now_date, log_action);
fflush(err_fp);
}
#endif
static void
open_pi_log(void)
{
if (!verbosity && !said) {
if ((err_fp = fopen(pkgin_errlog, "a")) == NULL) {
fprintf(stderr, MSG_CANT_OPEN_WRITE,
pkgin_errlog);
exit(EXIT_FAILURE);
}
dup2(fileno(err_fp), STDERR_FILENO);
rm_filepos = ftell(err_fp);
said = 1;
}
}
static void
close_pi_log(void)
{
if (!verbosity) {
analyse_pkglog(rm_filepos);
printf(MSG_WARNS_ERRS, warn_count, err_count);
if (warn_count > 0 || err_count > 0)
printf(MSG_PKG_INSTALL_LOGGING_TO, pkgin_errlog);
}
warn_count = err_count = said = 0;
}
/* package removal */
void
do_pkg_remove(Plisthead *removehead)
{
Pkglist *premove;
/* send pkg_delete stderr to logfile */
open_pi_log();
SLIST_FOREACH(premove, removehead, next) {
/* file not available in the repository */
if (premove->file_size == -1)
continue;
if (premove->depend == NULL)
/* SLIST corruption, badly installed package */
continue;
/* pkg_install cannot be deleted */
if (strcmp(premove->depend, PKG_INSTALL) == 0) {
printf(MSG_NOT_REMOVING, PKG_INSTALL);
continue;
}
printf(MSG_REMOVING, premove->depend);
#ifndef DEBUG
if (!verbosity)
log_tag(MSG_REMOVING, premove->depend);
if (fexec(pkg_delete, verb_flag("-f"), premove->depend, NULL)
!= EXIT_SUCCESS)
err_count++;
#endif
}
close_pi_log();
}
/**
* \fn do_pkg_install
*
* package installation. Don't rely on pkg_add's ability to fetch and
* install as we want to keep control on packages installation order.
* Besides, pkg_add cannot be used to install an "older" package remotely
* i.e. apache 1.3
*/
static int
do_pkg_install(Plisthead *installhead, int op)
{
int rc = EXIT_SUCCESS;
Pkglist *pinstall;
char pkgpath[BUFSIZ];
const char *pmsg;
char *pflags = verb_flag("-DU");
if (op == TOREFRESH)
pmsg = "refreshing %s...\n";
else if (op == TOUPGRADE)
pmsg = "upgrading %s...\n";
else
pmsg = "installing %s...\n";
/* send pkg_add stderr to logfile */
open_pi_log();
SLIST_FOREACH(pinstall, installhead, next) {
/* file not available in the repository */
if (pinstall->file_size == -1)
continue;
printf(pmsg, pinstall->depend);
snprintf(pkgpath, BUFSIZ,
"%s/%s%s", pkgin_cache, pinstall->depend, PKG_EXT);
if (!verbosity)
log_tag(pmsg, pinstall->depend);
if (fexec(pkg_add, pflags, pkgpath, NULL) == EXIT_FAILURE)
rc = EXIT_FAILURE;
}
close_pi_log();
return rc;
}
/* build the output line */
#define DEFAULT_WINSIZE 80
#define MAX_WINSIZE 512
char *
action_list(char *flatlist, char *str)
{
struct winsize winsize;
size_t curlen, cols = 0;
char *endl, *newlist = NULL;
/* XXX: avoid duplicate in progressmeter.c */
/* this is called for every package so no point handling sigwinch */
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
winsize.ws_col != 0) {
if (winsize.ws_col > MAX_WINSIZE)
cols = MAX_WINSIZE;
else
cols = winsize.ws_col;
} else
cols = DEFAULT_WINSIZE;
/*
* If the user requested -n then we print a package per line, otherwise
* we try to fit them indented into the current line length.
*/
if (flatlist == NULL) {
flatlist = xasprintf("%s%s", noflag ? "" : " ", str);
return flatlist;
}
if (str == NULL)
return flatlist;
/*
* No need to calculate line length if -n was requested.
*/
if (noflag) {
newlist = xasprintf("%s\n%s", flatlist, str);
return newlist;
}
endl = strrchr(flatlist, '\n');
if (endl)
curlen = strlen(endl);
else
curlen = strlen(flatlist);
if ((curlen + strlen(str)) >= cols)
newlist = xasprintf("%s\n %s", flatlist, str);
else
newlist = xasprintf("%s %s", flatlist, str);
return newlist;
}
#define H_BUF 6
int
pkgin_install(char **opkgargs, int do_inst)
{
FILE *fp;
int installnum = 0, upgradenum = 0, removenum = 0;
int refreshnum = 0, downloadnum = 0;
int rc = EXIT_SUCCESS;
int privsreqd = PRIVS_PKGINDB;
uint64_t free_space;
int64_t file_size = 0, size_pkg = 0;
size_t len;
ssize_t llen;
Pkglist *pkg;
Pkglist *pimpact;
Plisthead *impacthead; /* impact head */
Plisthead *upgradehead = NULL, *installhead = NULL;
Plisthead *refreshhead = NULL, *downloadhead = NULL;
char **pkgargs, *p;
char *toinstall = NULL, *toupgrade = NULL;
char *torefresh = NULL, *todownload = NULL;
char *unmet_reqs = NULL;
char pkgpath[BUFSIZ], pkgrepo[BUFSIZ], query[BUFSIZ];
char h_psize[H_BUF], h_fsize[H_BUF], h_free[H_BUF];
struct stat st;
/* transform command line globs into pkgnames */
if ((pkgargs = glob_to_pkgarg(opkgargs, &rc)) == NULL) {
printf(MSG_NOTHING_TO_DO);
return rc;
}
if (do_inst)
privsreqd |= PRIVS_PKGDB;
if (!have_privs(privsreqd))
errx(EXIT_FAILURE, MSG_DONT_HAVE_RIGHTS);
/* full impact list */
if ((impacthead = pkg_impact(pkgargs, &rc)) == NULL) {
printf(MSG_NOTHING_TO_DO);
free_list(pkgargs);
return rc;
}
/* check for required files */
if (!pkg_met_reqs(impacthead))
SLIST_FOREACH(pimpact, impacthead, next)
if (pimpact->action == UNMET_REQ)
unmet_reqs =
action_list(unmet_reqs, pimpact->full);
/* browse impact tree */
SLIST_FOREACH(pimpact, impacthead, next) {
/*
* Packages being removed need no special handling, account
* for them and move to the next package.
*/
if (pimpact->action == TOREMOVE) {
removenum++;
continue;
}
/* check for conflicts */
if (pkg_has_conflicts(pimpact))
if (!check_yesno(DEFAULT_NO))
goto installend;
/* XXX: this should be moved higher up, pdb_rec_list assert? */
if (pimpact->file_size <= 0) {
(void) fprintf(stderr, MSG_EMPTY_FILE_SIZE,
pimpact->depend);
continue;
}
/*
* Retrieve the correct repository for the package and save it,
* this is used later by pkg_download().
*/
(void) snprintf(query, BUFSIZ, PKG_URL, pimpact->full);
if (pkgindb_doquery(query, pdb_get_value, pkgrepo) != 0)
errx(EXIT_FAILURE, MSG_PKG_NO_REPO, pimpact->full);
pimpact->pkgurl = xasprintf("%s/%s%s", pkgrepo, pimpact->full,
PKG_EXT);
/*
* If the binary package has not already been downloaded, or
* its size does not match pkg_summary, then mark it to be
* downloaded.
*/
(void) snprintf(pkgpath, BUFSIZ, "%s/%s%s", pkgin_cache,
pimpact->full, PKG_EXT);
if (stat(pkgpath, &st) < 0 || st.st_size != pimpact->file_size)
pimpact->download = 1;
else {
/*
* If the cached package has the correct size, we must
* verify that the BUILD_DATE has not changed, in case
* the sizes happen to be identical.
*/
p = xasprintf("%s -Q BUILD_DATE %s", pkg_info, pkgpath);
if ((fp = popen(p, "r")) == NULL)
err(EXIT_FAILURE, "Cannot execute '%s'", p);
(void) free(p);
for (p = NULL, len = 0;
(llen = getline(&p, &len, fp)) > 0;
(void) free(p), p = NULL, len = 0) {
if (p[llen - 1] == '\n')
p[llen - 1] = '\0';
if (!pkgstr_identical(p, pimpact->build_date))
pimpact->download = 1;
}
(void) pclose(fp);
}
/*
* Don't account for download size if using a file:// repo.
*/
if (pimpact->download) {
downloadnum++;
if (strncmp(pkgrepo, "file:///", 8) != 0)
file_size += pimpact->file_size;
}
if (pimpact->old_size_pkg > 0)
pimpact->size_pkg -= pimpact->old_size_pkg;
size_pkg += pimpact->size_pkg;
switch (pimpact->action) {
case TOREFRESH:
refreshnum++;
break;
case TOUPGRADE:
upgradenum++;
break;
case TOINSTALL:
installnum++;
break;
}
}
(void)humanize_number(h_fsize, H_BUF, file_size, "",
HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
(void)humanize_number(h_psize, H_BUF, size_pkg, "",
HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
/* check disk space */
free_space = fs_room(pkgin_cache);
if (free_space < (uint64_t)file_size) {
(void)humanize_number(h_free, H_BUF, (int64_t)free_space, "",
HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
errx(EXIT_FAILURE, MSG_NO_CACHE_SPACE,
pkgin_cache, h_fsize, h_free);
}
free_space = fs_room(LOCALBASE);
if (size_pkg > 0 && free_space < (uint64_t)size_pkg) {
(void)humanize_number(h_free, H_BUF, (int64_t)free_space, "",
HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
errx(EXIT_FAILURE, MSG_NO_INSTALL_SPACE,
LOCALBASE, h_psize, h_free);
}
/*
* Nothing to install or download, exit early.
*/
if (!do_inst && downloadnum == 0) {
printf(MSG_NOTHING_TO_DO);
return rc;
}
/*
* Separate package lists according to action.
*/
downloadhead = order_download(impacthead);
SLIST_FOREACH(pimpact, downloadhead, next) {
todownload = action_list(todownload, pimpact->depend);
}
refreshhead = order_install(impacthead, TOREFRESH);
SLIST_FOREACH(pkg, refreshhead, next) {
torefresh = action_list(torefresh, pkg->depend);
}
upgradehead = order_install(impacthead, TOUPGRADE);
SLIST_FOREACH(pkg, upgradehead, next) {
toupgrade = action_list(toupgrade, pkg->depend);
}
installhead = order_install(impacthead, TOINSTALL);
SLIST_FOREACH(pkg, installhead, next) {
toinstall = action_list(toinstall, pkg->depend);
}
printf("\n");
if (do_inst) {
if (refreshnum > 0)
printf("%d package%s to refresh:\n%s\n\n", refreshnum,
(refreshnum == 1) ? "" : "s", torefresh);
if (upgradenum > 0)
printf("%d package%s to upgrade:\n%s\n\n", upgradenum,
(upgradenum == 1) ? "" : "s", toupgrade);
if (installnum > 0)
printf("%d package%s to install:\n%s\n\n", installnum,
(installnum == 1) ? "" : "s", toinstall);
printf("%d to refresh, %d to upgrade, %d to install\n",
refreshnum, upgradenum, installnum);
printf("%s to download, %s to install\n", h_fsize, h_psize);
} else {
printf("%d package%s to download:\n%s\n", downloadnum,
(downloadnum == 1) ? "" : "s", todownload);
printf("%s to download\n", h_fsize);
}
if (unmet_reqs != NULL)/* there were unmet requirements */
printf(MSG_REQT_MISSING, unmet_reqs);
if (!noflag)
printf("\n");
if (check_yesno(DEFAULT_YES) == ANSW_NO)
exit(rc);
/*
* First fetch all required packages. If we're only doing downloads
* then we're done, otherwise recalculate to account for failures.
*/
if (downloadnum > 0) {
if (pkg_download(downloadhead) == EXIT_FAILURE)
rc = EXIT_FAILURE;
if (!do_inst)
goto installend;
SLIST_FOREACH(pimpact, downloadhead, next) {
if (pimpact->file_size != -1)
continue;
switch (pimpact->action) {
case TOREFRESH:
refreshnum--;
break;
case TOUPGRADE:
upgradenum--;
break;
case TOINSTALL:
installnum--;
break;
}
}
}
/*
* At this point we're performing installs. Do them in order.
*/
if (refreshnum > 0) {
if (do_pkg_install(refreshhead, TOREFRESH) == EXIT_FAILURE)
rc = EXIT_FAILURE;
}
if (upgradenum > 0) {
if (do_pkg_install(upgradehead, TOUPGRADE) == EXIT_FAILURE)
rc = EXIT_FAILURE;
}
if (installnum > 0) {
if (do_pkg_install(installhead, TOINSTALL) == EXIT_FAILURE)
rc = EXIT_FAILURE;
}
/* pure install, not called by pkgin_upgrade */
if (upgrade_type == UPGRADE_NONE)
(void)update_db(LOCAL_SUMMARY, pkgargs, 1);
installend:
XFREE(toinstall);
XFREE(toupgrade);
XFREE(unmet_reqs);
free_pkglist(&impacthead);
free_pkglist(&upgradehead);
free_pkglist(&installhead);
free_list(pkgargs);
return rc;
}
int
pkgin_remove(char **pkgargs)
{
int deletenum = 0, exists, rc = EXIT_SUCCESS;
Plisthead *pdphead, *removehead;
Pkglist *pdp;
char *todelete = NULL, **ppkgargs, *pkgname, *ppkg;
pdphead = init_head();
if (SLIST_EMPTY(&l_plisthead))
errx(EXIT_FAILURE, MSG_EMPTY_LOCAL_PKGLIST);
/* act on every package passed to the command line */
for (ppkgargs = pkgargs; *ppkgargs != NULL; ppkgargs++) {
if ((pkgname =
find_exact_pkg(&l_plisthead, *ppkgargs)) == NULL) {
printf(MSG_PKG_NOT_INSTALLED, *ppkgargs);
rc = EXIT_FAILURE;
continue;
}
ppkg = xstrdup(pkgname);
trunc_str(ppkg, '-', STR_BACKWARD);
/* record full reverse dependency list for package */
full_dep_tree(ppkg, LOCAL_REVERSE_DEPS, pdphead);
XFREE(ppkg);
exists = 0;
/* check if package have already been recorded */
SLIST_FOREACH(pdp, pdphead, next) {
if (strncmp(pdp->depend, pkgname,
strlen(pdp->depend)) == 0) {
exists = 1;
break;
}
}
if (exists) {
XFREE(pkgname);
continue; /* next pkgarg */
}
/* add package itself */
pdp = malloc_pkglist();
pdp->depend = pkgname;
if (SLIST_EMPTY(pdphead))
/*
* identify unique package,
* don't cut it when ordering
*/
pdp->level = -1;
else
pdp->level = 0;
pdp->name = xstrdup(pdp->depend);
trunc_str(pdp->name, '-', STR_BACKWARD);
SLIST_INSERT_HEAD(pdphead, pdp, next);
} /* for pkgargs */
/* order remove list */
removehead = order_remove(pdphead);
SLIST_FOREACH(pdp, removehead, next) {
deletenum++;
todelete = action_list(todelete, pdp->depend);
}
if (todelete != NULL) {
printf(MSG_PKGS_TO_DELETE, deletenum, todelete);
if (!noflag)
printf("\n");
if (check_yesno(DEFAULT_YES)) {
do_pkg_remove(removehead);
(void)update_db(LOCAL_SUMMARY, NULL, 1);
}
} else
printf(MSG_NO_PKGS_TO_DELETE);
free_pkglist(&removehead);
free_pkglist(&pdphead);
XFREE(todelete);
return rc;
}
/*
* Find best match for a package to be upgraded.
*/
static char *
narrow_match(Pkglist *opkg)
{
Pkglist *pkglist;
char *best_match;
int refresh = 0;
/* for now, best match is old package itself */
best_match = xstrdup(opkg->full);
SLIST_FOREACH(pkglist, &r_plisthead, next) {
/* not the same pkgname, next */
if (!pkgstr_identical(opkg->name, pkglist->name))
continue;
/*
* Do not propose an upgrade if the PKGPATH does not match,
* otherwise users who have requested a specific version of a
* package for which there are usually multiple versions
* available (e.g. nodejs or mysql) would always have it
* replaced by the latest version.
*
* Note that this comparison does allow both PKGPATH values
* to be NULL, for example with local self-built packages, in
* which case we permit an upgrade.
*/
if (!pkgstr_identical(opkg->pkgpath, pkglist->pkgpath))
continue;
/*
* If the package version is identical, check if the BUILD_DATE
* has changed. If it has, we need to refresh the package as
* it has been rebuilt, possibly against newer dependencies.
*/
if (pkgstr_identical(opkg->full, pkglist->full)) {
if (!pkgstr_identical(opkg->build_date,
pkglist->build_date))
refresh = 1;
continue;
}
/* second package is greater */
if (version_check(best_match, pkglist->full) == 2) {
XFREE(best_match);
best_match = xstrdup(pkglist->full);
}
} /* SLIST_FOREACH remoteplisthead */
/* there was no upgrade candidate */
if (strcmp(best_match, opkg->full) == 0 && !refresh)
XFREE(best_match);
return best_match;
}
static char **
record_upgrades(Plisthead *plisthead)
{
Pkglist *pkglist;
size_t count = 0;
char **pkgargs;
SLIST_FOREACH(pkglist, plisthead, next)
count++;
pkgargs = xmalloc((count + 2) * sizeof(char *));
count = 0;
SLIST_FOREACH(pkglist, plisthead, next) {
pkgargs[count] = narrow_match(pkglist);
if (pkgargs[count] == NULL)
continue;
count++;
}
pkgargs[count] = NULL;
return pkgargs;
}
int
pkgin_upgrade(int uptype, int do_inst)
{
Plistnumbered *keeplisthead;
Plisthead *localplisthead;
char **pkgargs;
int rc;
/* used for pkgin_install not to update database, this is done below */
upgrade_type = uptype;
/* record keepable packages */
if ((keeplisthead = rec_pkglist(KEEP_LOCAL_PKGS)) == NULL)
errx(EXIT_FAILURE, MSG_EMPTY_KEEP_LIST);
/* upgrade all packages, not only keepables */
if (uptype == UPGRADE_ALL) {
if (SLIST_EMPTY(&l_plisthead))
errx(EXIT_FAILURE, MSG_EMPTY_LOCAL_PKGLIST);
localplisthead = &l_plisthead;
} else
/* upgrade only keepables packages */
localplisthead = keeplisthead->P_Plisthead;
pkgargs = record_upgrades(localplisthead);
rc = pkgin_install(pkgargs, do_inst);
/*
* full upgrade, we need to record keep-packages
* in order to restore them
*/
if (uptype == UPGRADE_ALL) {
free_list(pkgargs);
/* record keep list */
pkgargs = record_upgrades(keeplisthead->P_Plisthead);
}
(void)update_db(LOCAL_SUMMARY, pkgargs, 1);
free_list(pkgargs);
free_pkglist(&keeplisthead->P_Plisthead);
free(keeplisthead);
return rc;
}