forked from NetBSDfr/pkgin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.c
805 lines (656 loc) · 18.7 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
/* $Id: actions.c,v 1.56 2013/01/31 19:49:34 stacktic Exp $ */
/*
* Copyright (c) 2009, 2010, 2011, 2012, 2013 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 <time.h>
#ifndef LOCALBASE
#define LOCALBASE "/usr/pkg" /* see DISCLAIMER below */
#endif
const char *pkgin_cache = PKGIN_CACHE;
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, in_filepos = -1;
static int
pkg_download(Plisthead *installhead)
{
FILE *fp;
Pkglist *pinstall;
struct stat st;
Dlfile *dlpkg;
char pkg_fs[BUFSIZ], pkg_url[BUFSIZ], query[BUFSIZ];
int rc = EXIT_SUCCESS;
printf(MSG_DOWNLOAD_PKGS);
SLIST_FOREACH(pinstall, installhead, next) {
snprintf(pkg_fs, BUFSIZ,
"%s/%s%s", pkgin_cache, pinstall->depend, PKG_EXT);
/* pkg_info -X -a produces pkg_summary with empty FILE_SIZE,
* people could spend some time blaming on pkgin before finding
* what's really going on.
*/
if (pinstall->file_size == 0)
printf(MSG_EMPTY_FILE_SIZE, pinstall->depend);
/* already fully downloaded */
if (stat(pkg_fs, &st) == 0 &&
st.st_size == pinstall->file_size &&
pinstall->file_size != 0 )
continue;
snprintf(query, BUFSIZ, PKG_URL, pinstall->depend);
/* retrieve repository for package */
if (pkgindb_doquery(query, pdb_get_value, pkg_url) != 0)
errx(EXIT_FAILURE, MSG_PKG_NO_REPO, pinstall->depend);
strlcat(pkg_url, "/", sizeof(pkg_url));
strlcat(pkg_url, pinstall->depend, sizeof(pkg_url));
strlcat(pkg_url, PKG_EXT, sizeof(pkg_url));
/* if pkg's repo URL is file://, just symlink */
if (strncmp(pkg_url, SCHEME_FILE, strlen(SCHEME_FILE)) == 0) {
(void)unlink(pkg_fs);
if (symlink(&pkg_url[strlen(SCHEME_FILE) + 3],
pkg_fs) < 0)
errx(EXIT_FAILURE, MSG_SYMLINK_FAILED, pkg_fs);
printf(MSG_SYMLINKING_PKG, pkg_url);
continue;
}
umask(DEF_UMASK);
if ((fp = fopen(pkg_fs, "w")) == NULL)
err(EXIT_FAILURE, MSG_ERR_OPEN, pkg_fs);
if ((dlpkg = download_file(pkg_url, NULL)) == NULL) {
fprintf(stderr, MSG_PKG_NOT_AVAIL, pinstall->depend);
rc = EXIT_FAILURE;
if (!check_yesno(DEFAULT_NO))
errx(EXIT_FAILURE, MSG_PKG_NOT_AVAIL,
pinstall->depend);
pinstall->file_size = -1;
fclose(fp);
continue;
}
fwrite(dlpkg->buf, dlpkg->size, 1, fp);
fclose(fp);
XFREE(dlpkg->buf);
XFREE(dlpkg);
} /* download loop */
return rc;
}
/**
* \brief Analyse PKG_INSTALL_ERR_LOG for warnings
*/
static void
analyse_pkglog(long int filepos)
{
FILE *err_ro;
char err_line[BUFSIZ];
if (filepos < 0)
return;
err_ro = fopen(PKG_INSTALL_ERR_LOG, "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++;
}
fclose(err_ro);
}
/**
* \brief Tags PKG_INSTALL_ERR_LOG 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(PKG_INSTALL_ERR_LOG, "a")) == NULL) {
fprintf(stderr, MSG_CANT_OPEN_WRITE,
PKG_INSTALL_ERR_LOG);
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, PKG_INSTALL_ERR_LOG);
}
}
/* 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, pkgtools_flags, 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 rc = EXIT_SUCCESS;
Pkglist *pinstall;
char pkgpath[BUFSIZ];
char pi_tmp_flags[5]; /* tmp force flags for pkg_install */
/* send pkg_add stderr to logfile */
open_pi_log();
printf(MSG_INSTALL_PKG);
SLIST_FOREACH(pinstall, installhead, next) {
/* file not available in the repository */
if (pinstall->file_size == -1)
continue;
printf(MSG_INSTALLING, pinstall->depend);
snprintf(pkgpath, BUFSIZ,
"%s/%s%s", pkgin_cache, pinstall->depend, PKG_EXT);
#ifndef DEBUG
if (!verbosity)
log_tag(MSG_INSTALLING, pinstall->depend);
#endif
/* are we upgrading pkg_install ? */
if (pi_upgrade) { /* set in order.c */
/* 1st item on the list, reset the flag */
pi_upgrade = 0;
printf(MSG_UPGRADE_PKG_INSTALL, PKG_INSTALL);
/* set temporary force flags */
strncpy(pi_tmp_flags, "-ffu", 5);
if (verbosity)
/* append verbosity if requested */
strncat(pi_tmp_flags, "v", 2);
if (check_yesno(DEFAULT_YES)) {
#ifndef DEBUG
if (fexec(PKG_ADD, pi_tmp_flags,
pkgpath, NULL) == EXIT_FAILURE)
rc = EXIT_FAILURE;
#endif
} else
continue;
} else {
/* every other package */
#ifndef DEBUG
if (fexec(PKG_ADD, pkgtools_flags, pkgpath, NULL) ==
EXIT_FAILURE)
rc = EXIT_FAILURE;
#endif
}
} /* installation loop */
close_pi_log();
return rc;
}
/* build the output line */
char *
action_list(char *flatlist, char *str)
{
int newsize;
char *newlist = NULL;
if (flatlist == NULL)
XSTRDUP(newlist, str);
else {
if (str == NULL)
return flatlist;
newsize = strlen(str) + strlen(flatlist) + 2;
newlist = realloc(flatlist, newsize * sizeof(char));
strlcat(newlist, " ", newsize);
strlcat(newlist, str, newsize);
}
return newlist;
}
#define H_BUF 6
int
pkgin_install(char **opkgargs, uint8_t do_inst)
{
int installnum = 0, upgradenum = 0, removenum = 0;
int rc = EXIT_SUCCESS;
uint64_t file_size = 0, free_space;
int64_t size_pkg = 0;
Pkglist *premove, *pinstall;
Pkglist *pimpact;
Plisthead *impacthead; /* impact head */
Plisthead *removehead = NULL, *installhead = NULL;
char **pkgargs;
char *toinstall = NULL, *toupgrade = NULL, *toremove = NULL;
char *unmet_reqs = NULL;
char pkgpath[BUFSIZ], pkgurl[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;
}
/* 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) {
/* check for conflicts */
if (pkg_has_conflicts(pimpact))
if (!check_yesno(DEFAULT_NO))
goto installend;
snprintf(pkgpath, BUFSIZ, "%s/%s%s",
pkgin_cache, pimpact->full, PKG_EXT);
snprintf(query, BUFSIZ, PKG_URL, pimpact->full);
/* retrieve repository for package */
if (pkgindb_doquery(query, pdb_get_value, pkgurl) != 0)
errx(EXIT_FAILURE, MSG_PKG_NO_REPO, pimpact->full);
/*
* if package is not already downloaded or size mismatch,
* d/l it
*/
if ((stat(pkgpath, &st) < 0 ||
st.st_size != pimpact->file_size) &&
/* don't update file_size if repo is file:// */
strncmp(pkgurl, SCHEME_FILE, strlen(SCHEME_FILE)) != 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 TOUPGRADE:
upgradenum++;
installnum++;
break;
case TOINSTALL:
installnum++;
break;
case TOREMOVE:
removenum++;
break;
}
}
(void)humanize_number(h_fsize, H_BUF, (int64_t)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 < 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);
}
printf("\n");
if (upgradenum > 0) {
/* record ordered remove list before upgrade */
removehead = order_upgrade_remove(impacthead);
SLIST_FOREACH(premove, removehead, next) {
if (premove->computed == TOUPGRADE) {
toupgrade = action_list(toupgrade,
premove->depend);
#ifdef DEBUG
printf("package: %s - level: %d\n",
premove->depend, premove->level);
#endif
}
}
printf(MSG_PKGS_TO_UPGRADE, upgradenum, toupgrade);
printf("\n");
if (removenum > 0) {
SLIST_FOREACH(premove, removehead, next) {
if (premove->computed == TOREMOVE) {
toremove = action_list(toremove,
premove->depend);
#ifdef DEBUG
printf("package: %s - level: %d\n",
premove->depend, premove->level);
#endif
}
}
/*
* some packages may have been marked as TOREMOVE, then
* discovered as TOUPGRADE
*/
if (toremove != NULL) {
printf(MSG_PKGS_TO_REMOVE, removenum, toremove);
printf("\n");
}
}
} else
printf(MSG_NOTHING_TO_UPGRADE);
if (installnum > 0) {
/* record ordered install list */
installhead = order_install(impacthead);
SLIST_FOREACH(pinstall, installhead, next) {
toinstall = action_list(toinstall, pinstall->depend);
#ifdef DEBUG
printf("package: %s - level: %d\n",
pinstall->depend, pinstall->level);
#endif
}
printf(MSG_PKGS_TO_INSTALL, installnum, toinstall,
h_fsize, h_psize);
printf("\n");
if (unmet_reqs != NULL)/* there were unmet requirements */
printf(MSG_REQT_MISSING, unmet_reqs);
if (check_yesno(DEFAULT_YES) == ANSW_NO)
exit(rc);
/*
* before erasing anything, download packages
* If there was an error while downloading, record it
*/
if (pkg_download(installhead) == EXIT_FAILURE)
rc = EXIT_FAILURE;
if (do_inst) {
/* real install, not a simple download
*
* if there was upgrades, first remove
* old packages
*/
if (upgradenum > 0) {
printf(MSG_RM_UPGRADE_PKGS);
do_pkg_remove(removehead);
}
/*
* then pass ordered install list
* If there was an error while installing,
* record it
*/
if (do_pkg_install(installhead) == EXIT_FAILURE)
rc = EXIT_FAILURE;
/* pure install, not called by pkgin_upgrade */
if (upgrade_type == UPGRADE_NONE)
(void)update_db(LOCAL_SUMMARY, pkgargs);
}
} else
printf(MSG_NOTHING_TO_INSTALL);
installend:
XFREE(toinstall);
XFREE(toupgrade);
free_pkglist(&impacthead, IMPACT);
free_pkglist(&removehead, DEPTREE);
free_pkglist(&installhead, DEPTREE);
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;
}
XSTRDUP(ppkg, 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(DEPTREE);
pdp->depend = pkgname;
if (SLIST_EMPTY(pdphead))
/*
* identify unique package,
* don't cut it when ordering
*/
pdp->level = -1;
else
pdp->level = 0;
XSTRDUP(pdp->name, 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 (check_yesno(DEFAULT_YES)) {
do_pkg_remove(removehead);
(void)update_db(LOCAL_SUMMARY, NULL);
}
analyse_pkglog(rm_filepos);
} else
printf(MSG_NO_PKGS_TO_DELETE);
free_pkglist(&removehead, DEPTREE);
free_pkglist(&pdphead, DEPTREE);
XFREE(todelete);
return rc;
}
/*
* The idea behind this function is to list preferred versions
* of packages. For example, mysql-server-5, apache-2.2.
* It is actually unused, not sure if really needed.
*/
char *
read_preferred(char *pkgname)
{
int matchlen;
FILE *fp;
char match[BUFSIZ], *pref, *p;
if ((fp = fopen(PKGIN_CONF"/"PREFERRED_PKGS, "r")) == NULL)
return NULL;
snprintf(match, BUFSIZ, "%s=", pkgname);
matchlen = strlen(match);
XMALLOC(pref, BUFSIZ * sizeof(char));
while (fgets(pref, BUFSIZ, fp) != NULL) {
/* pref == "foobar=" */
if (strncmp(match, pref, matchlen) == 0) {
p = pref + matchlen;
for (; *p == ' ' || *p == '\t'; p++);
if (*p == '\0')
return NULL;
snprintf(pref, BUFSIZ, "%s-%s", pkgname, p);
pref[strlen(pref) - 1] = '\0';
return pref;
}
}
fclose(fp);
return NULL;
}
/*
* find closest match for packages to be upgraded
* if we have mysql-5.1.10 installed prefer mysql-5.1.20 over
* mysql-5.5.20 when upgrading
*/
static char *
narrow_match(Pkglist *opkg)
{
Pkglist *pkglist;
char *best_match;
/* for now, best match is old package itself */
XSTRDUP(best_match, opkg->full);
SLIST_FOREACH(pkglist, &r_plisthead, next) {
/* not the same pkgname, next */
if (strcmp(opkg->name, pkglist->name) != 0)
continue;
/*
* if PKGPATH does not match, do not try to update
* (mysql 5.1/5.5)
*/
if (strcmp(opkg->pkgpath, pkglist->pkgpath) != 0)
continue;
/* same package version, next */
if (strcmp(opkg->full, pkglist->full) == 0)
continue;
/* second package is greater */
if (version_check(best_match, pkglist->full) == 2) {
XFREE(best_match);
XSTRDUP(best_match, pkglist->full);
}
} /* SLIST_FOREACH remoteplisthead */
/* there was no upgrade candidate */
if (strcmp(best_match, opkg->full) == 0)
XFREE(best_match);
return best_match;
}
static char **
record_upgrades(Plisthead *plisthead)
{
Pkglist *pkglist;
int count = 0;
char **pkgargs;
SLIST_FOREACH(pkglist, plisthead, next)
count++;
XMALLOC(pkgargs, (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)
{
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);
free_list(pkgargs);
free_pkglist(&keeplisthead->P_Plisthead, LIST);
free(keeplisthead);
return rc;
}