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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
|
/*
* pacman.c
*
* Copyright (c) 2006-2009 Pacman Development Team <pacman-dev@archlinux.org>
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
/* special handling of package version for GIT */
#if defined(GIT_VERSION)
#undef PACKAGE_VERSION
#define PACKAGE_VERSION GIT_VERSION
#endif
#include <stdlib.h> /* atoi */
#include <stdio.h>
#include <limits.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/utsname.h> /* uname */
#include <locale.h> /* setlocale */
#include <time.h> /* time_t */
#include <errno.h>
#if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H)
#include <mcheck.h> /* debug tracing (mtrace) */
#endif
/* alpm */
#include <alpm.h>
#include <alpm_list.h>
/* pacman */
#include "pacman.h"
#include "util.h"
#include "callback.h"
#include "conf.h"
#include "package.h"
pmdb_t *db_local;
/* list of targets specified on command line */
static alpm_list_t *pm_targets;
/** Display usage/syntax for the specified operation.
* @param op the operation code requested
* @param myname basename(argv[0])
*/
static void usage(int op, const char * const myname)
{
/* prefetch some strings for usage below, which moves a lot of calls
* out of gettext. */
char const * const str_opt = _("options");
char const * const str_file = _("file(s)");
char const * const str_pkg = _("package(s)");
char const * const str_usg = _("usage");
char const * const str_opr = _("operation");
if(op == PM_OP_MAIN) {
printf("%s: %s <%s> [...]\n", str_usg, myname, str_opr);
printf(_("operations:\n"));
printf(" %s {-h --help}\n", myname);
printf(" %s {-V --version}\n", myname);
printf(" %s {-Q --query} [%s] [%s]\n", myname, str_opt, str_pkg);
printf(" %s {-R --remove} [%s] <%s>\n", myname, str_opt, str_pkg);
printf(" %s {-S --sync} [%s] [%s]\n", myname, str_opt, str_pkg);
printf(" %s {-U --upgrade} [%s] <%s>\n", myname, str_opt, str_file);
printf(_("\nuse '%s {-h --help}' with an operation for available options\n"),
myname);
} else {
if(op == PM_OP_REMOVE) {
printf("%s: %s {-R --remove} [%s] <%s>\n", str_usg, myname, str_opt, str_pkg);
printf("%s:\n", str_opt);
printf(_(" -c, --cascade remove packages and all packages that depend on them\n"));
printf(_(" -d, --nodeps skip dependency checks\n"));
printf(_(" -k, --dbonly only remove database entry, do not remove files\n"));
printf(_(" -n, --nosave remove configuration files as well\n"));
printf(_(" -s, --recursive remove dependencies also (that won't break packages)\n"
" (-ss includes explicitly installed dependencies too)\n"));
printf(_(" -u, --unneeded remove unneeded packages (that won't break packages)\n"));
} else if(op == PM_OP_UPGRADE) {
printf("%s: %s {-U --upgrade} [%s] <%s>\n", str_usg, myname, str_opt, str_file);
printf("%s:\n", str_opt);
printf(_(" --asdeps install packages as non-explicitly installed\n"));
printf(_(" --asexplicit install packages as explicitly installed\n"));
printf(_(" -d, --nodeps skip dependency checks\n"));
printf(_(" -f, --force force install, overwrite conflicting files\n"));
} else if(op == PM_OP_QUERY) {
printf("%s: %s {-Q --query} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
printf("%s:\n", str_opt);
printf(_(" -c, --changelog view the changelog of a package\n"));
printf(_(" -d, --deps list packages installed as dependencies [filter]\n"));
printf(_(" -e, --explicit list packages explicitly installed [filter]\n"));
printf(_(" -g, --groups view all members of a package group\n"));
printf(_(" -i, --info view package information (-ii for backup files)\n"));
printf(_(" -k, --check check that the files owned by the package(s) are present\n"));
printf(_(" -l, --list list the contents of the queried package\n"));
printf(_(" -m, --foreign list installed packages not found in sync db(s) [filter]\n"));
printf(_(" -o, --owns <file> query the package that owns <file>\n"));
printf(_(" -p, --file <package> query a package file instead of the database\n"));
printf(_(" -s, --search <regex> search locally-installed packages for matching strings\n"));
printf(_(" -t, --unrequired list packages not required by any package [filter]\n"));
printf(_(" -u, --upgrades list outdated packages [filter]\n"));
printf(_(" -q, --quiet show less information for query and search\n"));
} else if(op == PM_OP_SYNC) {
printf("%s: %s {-S --sync} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
printf("%s:\n", str_opt);
printf(_(" --asdeps install packages as non-explicitly installed\n"));
printf(_(" --asexplicit install packages as explicitly installed\n"));
printf(_(" -c, --clean remove old packages from cache directory (-cc for all)\n"));
printf(_(" -d, --nodeps skip dependency checks\n"));
printf(_(" -f, --force force install, overwrite conflicting files\n"));
printf(_(" -g, --groups view all members of a package group\n"));
printf(_(" -i, --info view package information\n"));
printf(_(" -l, --list <repo> view a list of packages in a repo\n"));
printf(_(" -p, --print-uris print out URIs for given packages and their dependencies\n"));
printf(_(" -s, --search <regex> search remote repositories for matching strings\n"));
printf(_(" -u, --sysupgrade upgrade installed packages (-uu allows downgrade)\n"));
printf(_(" -w, --downloadonly download packages but do not install/upgrade anything\n"));
printf(_(" -y, --refresh download fresh package databases from the server\n"));
printf(_(" --needed don't reinstall up to date packages\n"));
printf(_(" --ignore <pkg> ignore a package upgrade (can be used more than once)\n"));
printf(_(" --ignoregroup <grp>\n"
" ignore a group upgrade (can be used more than once)\n"));
printf(_(" -q, --quiet show less information for query and search\n"));
}
printf(_(" --config <path> set an alternate configuration file\n"));
printf(_(" --logfile <path> set an alternate log file\n"));
printf(_(" --noconfirm do not ask for any confirmation\n"));
printf(_(" --noprogressbar do not show a progress bar when downloading files\n"));
printf(_(" --noscriptlet do not execute the install scriptlet if one exists\n"));
printf(_(" -v, --verbose be verbose\n"));
printf(_(" --debug display debug messages\n"));
printf(_(" -r, --root <path> set an alternate installation root\n"));
printf(_(" -b, --dbpath <path> set an alternate database location\n"));
printf(_(" --cachedir <dir> set an alternate package cache location\n"));
printf(_(" --arch <arch> set an alternate architecture\n"));
}
}
/** Output pacman version and copyright.
*/
static void version(void)
{
printf("\n");
printf(" .--. Pacman v%s - libalpm v%s\n", PACKAGE_VERSION, alpm_version());
printf("/ _.-' .-. .-. .-. Copyright (C) 2006-2009 Pacman Development Team\n");
printf("\\ '-. '-' '-' '-' Copyright (C) 2002-2006 Judd Vinet\n");
printf(" '--'\n");
printf(_(" This program may be freely redistributed under\n"
" the terms of the GNU General Public License.\n"));
printf("\n");
}
/** Sets up gettext localization. Safe to call multiple times.
*/
/* Inspired by the monotone function localize_monotone. */
#if defined(ENABLE_NLS)
static void localize(void)
{
static int init = 0;
if (!init) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
init = 1;
}
}
#endif
/** Set user agent environment variable.
*/
static void setuseragent(void)
{
char agent[101];
struct utsname un;
uname(&un);
snprintf(agent, 100, "pacman/%s (%s %s) libalpm/%s",
PACKAGE_VERSION, un.sysname, un.machine, alpm_version());
setenv("HTTP_USER_AGENT", agent, 0);
}
static void setarch(const char *arch)
{
if (strcmp(arch, "auto") == 0) {
struct utsname un;
uname(&un);
pm_printf(PM_LOG_DEBUG, "config: architecture: %s\n", un.machine);
alpm_option_set_arch(un.machine);
} else {
pm_printf(PM_LOG_DEBUG, "config: architecture: %s\n", arch);
alpm_option_set_arch(arch);
}
}
/** Free the resources.
*
* @param ret the return value
*/
static void cleanup(int ret) {
/* free alpm library resources */
if(alpm_release() == -1) {
pm_printf(PM_LOG_ERROR, alpm_strerrorlast());
}
/* free memory */
FREELIST(pm_targets);
if(config) {
config_free(config);
config = NULL;
}
exit(ret);
}
/** Write function that correctly handles EINTR.
*/
static ssize_t xwrite(int fd, const void *buf, size_t count)
{
ssize_t ret;
while((ret = write(fd, buf, count)) == -1 && errno == EINTR);
return(ret);
}
/** Catches thrown signals. Performs necessary cleanup to ensure database is
* in a consistant state.
* @param signum the thrown signal
*/
static RETSIGTYPE handler(int signum)
{
int out = fileno(stdout);
int err = fileno(stderr);
if(signum == SIGSEGV) {
const char *msg1 = "error: segmentation fault\n";
const char *msg2 = "Internal pacman error: Segmentation fault.\n"
"Please submit a full bug report with --debug if appropriate.\n";
/* write a error message to out, the rest to err */
xwrite(out, msg1, strlen(msg1));
xwrite(err, msg2, strlen(msg2));
exit(signum);
} else if((signum == SIGINT)) {
const char *msg = "\nInterrupt signal received\n";
xwrite(err, msg, strlen(msg));
if(alpm_trans_interrupt() == 0) {
/* a transaction is being interrupted, don't exit pacman yet. */
return;
}
/* no commiting transaction, we can release it now and then exit pacman */
alpm_trans_release();
/* output a newline to be sure we clear any line we may be on */
xwrite(out, "\n", 1);
}
cleanup(signum);
}
/** Sets all libalpm required paths in one go. Called after the command line
* and inital config file parsing. Once this is complete, we can see if any
* paths were defined. If a rootdir was defined and nothing else, we want all
* of our paths to live under the rootdir that was specified. Safe to call
* multiple times (will only do anything the first time).
*/
static void setlibpaths(void)
{
static int init = 0;
if (!init) {
int ret = 0;
pm_printf(PM_LOG_DEBUG, "setlibpaths() called\n");
/* Configure root path first. If it is set and dbpath/logfile were not
* set, then set those as well to reside under the root. */
if(config->rootdir) {
char path[PATH_MAX];
ret = alpm_option_set_root(config->rootdir);
if(ret != 0) {
pm_printf(PM_LOG_ERROR, _("problem setting rootdir '%s' (%s)\n"),
config->rootdir, alpm_strerrorlast());
cleanup(ret);
}
if(!config->dbpath) {
/* omit leading slash from our static DBPATH, root handles it */
snprintf(path, PATH_MAX, "%s%s", alpm_option_get_root(), DBPATH + 1);
config->dbpath = strdup(path);
}
if(!config->logfile) {
/* omit leading slash from our static LOGFILE path, root handles it */
snprintf(path, PATH_MAX, "%s%s", alpm_option_get_root(), LOGFILE + 1);
config->logfile = strdup(path);
}
}
/* Set other paths if they were configured. Note that unless rootdir
* was left undefined, these two paths (dbpath and logfile) will have
* been set locally above, so the if cases below will now trigger. */
if(config->dbpath) {
ret = alpm_option_set_dbpath(config->dbpath);
if(ret != 0) {
pm_printf(PM_LOG_ERROR, _("problem setting dbpath '%s' (%s)\n"),
config->dbpath, alpm_strerrorlast());
cleanup(ret);
}
}
if(config->logfile) {
ret = alpm_option_set_logfile(config->logfile);
if(ret != 0) {
pm_printf(PM_LOG_ERROR, _("problem setting logfile '%s' (%s)\n"),
config->logfile, alpm_strerrorlast());
cleanup(ret);
}
}
/* add a default cachedir if one wasn't specified */
if(alpm_option_get_cachedirs() == NULL) {
alpm_option_add_cachedir(CACHEDIR);
}
init = 1;
}
}
/** Parse command-line arguments for each operation.
* @param argc argc
* @param argv argv
* @return 0 on success, 1 on error
*/
static int parseargs(int argc, char *argv[])
{
int opt;
int option_index = 0;
static struct option opts[] =
{
{"query", no_argument, 0, 'Q'},
{"remove", no_argument, 0, 'R'},
{"sync", no_argument, 0, 'S'},
{"deptest", no_argument, 0, 'T'}, /* used by makepkg */
{"upgrade", no_argument, 0, 'U'},
{"version", no_argument, 0, 'V'},
{"dbpath", required_argument, 0, 'b'},
{"cascade", no_argument, 0, 'c'},
{"changelog", no_argument, 0, 'c'},
{"clean", no_argument, 0, 'c'},
{"nodeps", no_argument, 0, 'd'},
{"deps", no_argument, 0, 'd'},
{"explicit", no_argument, 0, 'e'},
{"force", no_argument, 0, 'f'},
{"groups", no_argument, 0, 'g'},
{"help", no_argument, 0, 'h'},
{"info", no_argument, 0, 'i'},
{"dbonly", no_argument, 0, 'k'},
{"check", no_argument, 0, 'k'},
{"list", no_argument, 0, 'l'},
{"foreign", no_argument, 0, 'm'},
{"nosave", no_argument, 0, 'n'},
{"owns", no_argument, 0, 'o'},
{"file", no_argument, 0, 'p'},
{"print-uris", no_argument, 0, 'p'},
{"quiet", no_argument, 0, 'q'},
{"root", required_argument, 0, 'r'},
{"recursive", no_argument, 0, 's'},
{"search", no_argument, 0, 's'},
{"unrequired", no_argument, 0, 't'},
{"upgrades", no_argument, 0, 'u'},
{"sysupgrade", no_argument, 0, 'u'},
{"unneeded", no_argument, 0, 'u'},
{"verbose", no_argument, 0, 'v'},
{"downloadonly", no_argument, 0, 'w'},
{"refresh", no_argument, 0, 'y'},
{"noconfirm", no_argument, 0, 1000},
{"config", required_argument, 0, 1001},
{"ignore", required_argument, 0, 1002},
{"debug", optional_argument, 0, 1003},
{"noprogressbar", no_argument, 0, 1004},
{"noscriptlet", no_argument, 0, 1005},
{"ask", required_argument, 0, 1006},
{"cachedir", required_argument, 0, 1007},
{"asdeps", no_argument, 0, 1008},
{"logfile", required_argument, 0, 1009},
{"ignoregroup", required_argument, 0, 1010},
{"needed", no_argument, 0, 1011},
{"asexplicit", no_argument, 0, 1012},
{"arch", required_argument, 0, 1013},
{0, 0, 0, 0}
};
while((opt = getopt_long(argc, argv, "RUQSTr:b:vkhscVfmnoldepqituwygz", opts, &option_index))) {
alpm_list_t *list = NULL, *item = NULL; /* lists for splitting strings */
if(opt < 0) {
break;
}
switch(opt) {
case 0: break;
case 1000: config->noconfirm = 1; break;
case 1001:
if(config->configfile) {
free(config->configfile);
}
config->configfile = strndup(optarg, PATH_MAX);
break;
case 1002:
list = strsplit(optarg, ',');
for(item = list; item; item = alpm_list_next(item)) {
alpm_option_add_ignorepkg((char *)alpm_list_getdata(item));
}
FREELIST(list);
break;
case 1003:
/* debug levels are made more 'human readable' than using a raw logmask
* here, error and warning are set in config_new, though perhaps a
* --quiet option will remove these later */
if(optarg) {
unsigned short debug = atoi(optarg);
switch(debug) {
case 2:
config->logmask |= PM_LOG_FUNCTION; /* fall through */
case 1:
config->logmask |= PM_LOG_DEBUG;
break;
default:
pm_printf(PM_LOG_ERROR, _("'%s' is not a valid debug level\n"),
optarg);
return(1);
}
} else {
config->logmask |= PM_LOG_DEBUG;
}
/* progress bars get wonky with debug on, shut them off */
config->noprogressbar = 1;
break;
case 1004: config->noprogressbar = 1; break;
case 1005: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
case 1006: config->noask = 1; config->ask = atoi(optarg); break;
case 1007:
if(alpm_option_add_cachedir(optarg) != 0) {
pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"),
optarg, alpm_strerrorlast());
return(1);
}
break;
case 1008:
config->flags |= PM_TRANS_FLAG_ALLDEPS;
break;
case 1009:
config->logfile = strndup(optarg, PATH_MAX);
break;
case 1010:
list = strsplit(optarg, ',');
for(item = list; item; item = alpm_list_next(item)) {
alpm_option_add_ignoregrp((char *)alpm_list_getdata(item));
}
FREELIST(list);
break;
case 1011: config->flags |= PM_TRANS_FLAG_NEEDED; break;
case 1012:
config->flags |= PM_TRANS_FLAG_ALLEXPLICIT;
break;
case 1013:
setarch(optarg);
break;
case 'Q': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_QUERY); break;
case 'R': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_REMOVE); break;
case 'S': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_SYNC); break;
case 'T': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DEPTEST); break;
case 'U': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_UPGRADE); break;
case 'V': config->version = 1; break;
case 'b':
config->dbpath = strdup(optarg);
break;
case 'c':
(config->op_s_clean)++;
config->flags |= PM_TRANS_FLAG_CASCADE;
config->op_q_changelog = 1;
break;
case 'd':
config->op_q_deps = 1;
config->flags |= PM_TRANS_FLAG_NODEPS;
break;
case 'e':
config->op_q_explicit = 1;
break;
case 'f': config->flags |= PM_TRANS_FLAG_FORCE; break;
case 'g': (config->group)++; break;
case 'h': config->help = 1; break;
case 'i': (config->op_q_info)++; (config->op_s_info)++; break;
case 'k':
config->flags |= PM_TRANS_FLAG_DBONLY;
config->op_q_check = 1;
break;
case 'l': config->op_q_list = 1; break;
case 'm': config->op_q_foreign = 1; break;
case 'n': config->flags |= PM_TRANS_FLAG_NOSAVE; break;
case 'o': config->op_q_owns = 1; break;
case 'p':
config->op_q_isfile = 1;
config->op_s_printuris = 1;
config->flags |= PM_TRANS_FLAG_NOCONFLICTS;
config->flags |= PM_TRANS_FLAG_NOLOCK;
break;
case 'q':
config->quiet = 1;
break;
case 'r':
config->rootdir = strdup(optarg);
break;
case 's':
config->op_s_search = 1;
config->op_q_search = 1;
if(config->flags & PM_TRANS_FLAG_RECURSE) {
config->flags |= PM_TRANS_FLAG_RECURSEALL;
} else {
config->flags |= PM_TRANS_FLAG_RECURSE;
}
break;
case 't':
config->op_q_unrequired = 1;
break;
case 'u':
(config->op_s_upgrade)++;
config->op_q_upgrade = 1;
config->flags |= PM_TRANS_FLAG_UNNEEDED;
break;
case 'v': (config->verbose)++; break;
case 'w':
config->op_s_downloadonly = 1;
config->flags |= PM_TRANS_FLAG_DOWNLOADONLY;
config->flags |= PM_TRANS_FLAG_NOCONFLICTS;
break;
case 'y': (config->op_s_sync)++; break;
case '?': return(1);
default: return(1);
}
}
if(config->op == 0) {
pm_printf(PM_LOG_ERROR, _("only one operation may be used at a time\n"));
return(1);
}
if(config->help) {
usage(config->op, mbasename(argv[0]));
return(2);
}
if(config->version) {
version();
return(2);
}
while(optind < argc) {
/* add the target to our target array */
pm_targets = alpm_list_add(pm_targets, strdup(argv[optind]));
optind++;
}
return(0);
}
/* helper for being used with setrepeatingoption */
static void option_add_holdpkg(const char *name) {
config->holdpkg = alpm_list_add(config->holdpkg, strdup(name));
}
/* helper for being used with setrepeatingoption */
static void option_add_syncfirst(const char *name) {
config->syncfirst = alpm_list_add(config->syncfirst, strdup(name));
}
/** Add repeating options such as NoExtract, NoUpgrade, etc to libalpm
* settings. Refactored out of the parseconfig code since all of them did
* the exact same thing and duplicated code.
* @param ptr a pointer to the start of the multiple options
* @param option the string (friendly) name of the option, used for messages
* @param optionfunc a function pointer to an alpm_option_add_* function
*/
static void setrepeatingoption(const char *ptr, const char *option,
void (*optionfunc)(const char*))
{
char *p = (char*)ptr;
char *q;
while((q = strchr(p, ' '))) {
*q = '\0';
(*optionfunc)(p);
pm_printf(PM_LOG_DEBUG, "config: %s: %s\n", option, p);
p = q;
p++;
}
(*optionfunc)(p);
pm_printf(PM_LOG_DEBUG, "config: %s: %s\n", option, p);
}
static char *get_filename(const char *url) {
char *filename = strrchr(url, '/');
if(filename != NULL) {
filename++;
}
return(filename);
}
static char *get_destfile(const char *path, const char *filename) {
char *destfile;
/* len = localpath len + filename len + null */
int len = strlen(path) + strlen(filename) + 1;
destfile = calloc(len, sizeof(char));
snprintf(destfile, len, "%s%s", path, filename);
return(destfile);
}
static char *get_tempfile(const char *path, const char *filename) {
char *tempfile;
/* len = localpath len + filename len + '.part' len + null */
int len = strlen(path) + strlen(filename) + 6;
tempfile = calloc(len, sizeof(char));
snprintf(tempfile, len, "%s%s.part", path, filename);
return(tempfile);
}
/** External fetch callback */
int download_with_xfercommand(const char *url, const char *localpath,
time_t mtimeold, time_t *mtimenew) {
int ret = 0;
int retval;
int usepart = 0;
char *parsedcmd,*tempcmd;
char cwd[PATH_MAX];
char *destfile, *tempfile, *filename;
if(!config->xfercommand) {
return -1;
}
filename = get_filename(url);
if(!filename) {
return -1;
}
destfile = get_destfile(localpath, filename);
tempfile = get_tempfile(localpath, filename);
tempcmd = strdup(config->xfercommand);
/* replace all occurrences of %o with fn.part */
if(strstr(tempcmd, "%o")) {
usepart = 1;
parsedcmd = strreplace(tempcmd, "%o", tempfile);
free(tempcmd);
tempcmd = parsedcmd;
}
/* replace all occurrences of %u with the download URL */
parsedcmd = strreplace(tempcmd, "%u", url);
free(tempcmd);
/* cwd to the download directory */
getcwd(cwd, PATH_MAX);
if(chdir(localpath)) {
pm_printf(PM_LOG_WARNING, _("could not chdir to download directory %s\n"), localpath);
ret = -1;
goto cleanup;
}
/* execute the parsed command via /bin/sh -c */
pm_printf(PM_LOG_DEBUG, "running command: %s\n", parsedcmd);
retval = system(parsedcmd);
if(retval == -1) {
pm_printf(PM_LOG_WARNING, _("running XferCommand: fork failed!\n"));
ret = -1;
} else if(retval != 0) {
/* download failed */
pm_printf(PM_LOG_DEBUG, "XferCommand command returned non-zero status "
"code (%d)\n", retval);
ret = -1;
} else {
/* download was successful */
if(usepart) {
rename(tempfile, destfile);
}
ret = 0;
}
cleanup:
chdir(cwd);
if(ret == -1) {
/* hack to let an user the time to cancel a download */
sleep(2);
}
free(destfile);
free(tempfile);
free(parsedcmd);
return(ret);
}
/* The real parseconfig. Called with a null section argument by the publicly
* visible parseconfig so we can recall from within ourself on an include */
static int _parseconfig(const char *file, const char *givensection,
pmdb_t * const givendb)
{
FILE *fp = NULL;
char line[PATH_MAX+1];
int linenum = 0;
char *ptr, *section = NULL;
pmdb_t *db = NULL;
int ret = 0;
pm_printf(PM_LOG_DEBUG, "config: attempting to read file %s\n", file);
fp = fopen(file, "r");
if(fp == NULL) {
pm_printf(PM_LOG_ERROR, _("config file %s could not be read.\n"), file);
return(1);
}
/* if we are passed a section, use it as our starting point */
if(givensection != NULL) {
section = strdup(givensection);
}
/* if we are passed a db, use it as our starting point */
if(givendb != NULL) {
db = givendb;
}
while(fgets(line, PATH_MAX, fp)) {
linenum++;
strtrim(line);
/* ignore whole line and end of line comments */
if(strlen(line) == 0 || line[0] == '#') {
continue;
}
if((ptr = strchr(line, '#'))) {
*ptr = '\0';
}
if(line[0] == '[' && line[strlen(line)-1] == ']') {
/* new config section, skip the '[' */
ptr = line;
ptr++;
if(section) {
free(section);
}
section = strdup(ptr);
section[strlen(section)-1] = '\0';
pm_printf(PM_LOG_DEBUG, "config: new section '%s'\n", section);
if(!strlen(section)) {
pm_printf(PM_LOG_ERROR, _("config file %s, line %d: bad section name.\n"),
file, linenum);
ret = 1;
goto cleanup;
}
/* if we are not looking at the options section, register a db and also
* ensure we have set all of our library paths as the library is too stupid
* at the moment to do lazy opening of the databases */
if(strcmp(section, "options") != 0) {
setlibpaths();
db = alpm_db_register_sync(section);
if(db == NULL) {
pm_printf(PM_LOG_ERROR, _("could not register '%s' database (%s)\n"),
section, alpm_strerrorlast());
ret = 1;
goto cleanup;
}
}
} else {
/* directive */
char *key;
/* strsep modifies the 'line' string: 'key \0 ptr' */
key = line;
ptr = line;
strsep(&ptr, "=");
strtrim(key);
strtrim(ptr);
if(key == NULL) {
pm_printf(PM_LOG_ERROR, _("config file %s, line %d: syntax error in config file- missing key.\n"),
file, linenum);
ret = 1;
goto cleanup;
}
/* For each directive, compare to the camelcase string. */
if(section == NULL) {
pm_printf(PM_LOG_ERROR, _("config file %s, line %d: All directives must belong to a section.\n"),
file, linenum);
ret = 1;
goto cleanup;
}
if(ptr == NULL && strcmp(section, "options") == 0) {
/* directives without settings, all in [options] */
if(strcmp(key, "UseSyslog") == 0) {
alpm_option_set_usesyslog(1);
pm_printf(PM_LOG_DEBUG, "config: usesyslog\n");
} else if(strcmp(key, "ILoveCandy") == 0) {
config->chomp = 1;
pm_printf(PM_LOG_DEBUG, "config: chomp\n");
} else if(strcmp(key, "ShowSize") == 0) {
config->showsize = 1;
pm_printf(PM_LOG_DEBUG, "config: showsize\n");
} else if(strcmp(key, "UseDelta") == 0) {
alpm_option_set_usedelta(1);
pm_printf(PM_LOG_DEBUG, "config: usedelta\n");
} else if(strcmp(key, "TotalDownload") == 0) {
config->totaldownload = 1;
pm_printf(PM_LOG_DEBUG, "config: totaldownload\n");
} else {
pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' not recognized.\n"),
file, linenum, key);
ret = 1;
goto cleanup;
}
} else {
/* directives with settings */
if(strcmp(key, "Include") == 0) {
pm_printf(PM_LOG_DEBUG, "config: including %s\n", ptr);
_parseconfig(ptr, section, db);
/* Ignore include failures... assume non-critical */
} else if(strcmp(section, "options") == 0) {
if(strcmp(key, "NoUpgrade") == 0) {
setrepeatingoption(ptr, "NoUpgrade", alpm_option_add_noupgrade);
} else if(strcmp(key, "NoExtract") == 0) {
setrepeatingoption(ptr, "NoExtract", alpm_option_add_noextract);
} else if(strcmp(key, "IgnorePkg") == 0) {
setrepeatingoption(ptr, "IgnorePkg", alpm_option_add_ignorepkg);
} else if(strcmp(key, "IgnoreGroup") == 0) {
setrepeatingoption(ptr, "IgnoreGroup", alpm_option_add_ignoregrp);
} else if(strcmp(key, "HoldPkg") == 0) {
setrepeatingoption(ptr, "HoldPkg", option_add_holdpkg);
} else if(strcmp(key, "SyncFirst") == 0) {
setrepeatingoption(ptr, "SyncFirst", option_add_syncfirst);
} else if(strcmp(key, "Architecture") == 0) {
if(!alpm_option_get_arch()) {
setarch(ptr);
}
} else if(strcmp(key, "DBPath") == 0) {
/* don't overwrite a path specified on the command line */
if(!config->dbpath) {
config->dbpath = strdup(ptr);
pm_printf(PM_LOG_DEBUG, "config: dbpath: %s\n", ptr);
}
} else if(strcmp(key, "CacheDir") == 0) {
if(alpm_option_add_cachedir(ptr) != 0) {
pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"),
ptr, alpm_strerrorlast());
ret = 1;
goto cleanup;
}
pm_printf(PM_LOG_DEBUG, "config: cachedir: %s\n", ptr);
} else if(strcmp(key, "RootDir") == 0) {
/* don't overwrite a path specified on the command line */
if(!config->rootdir) {
config->rootdir = strdup(ptr);
pm_printf(PM_LOG_DEBUG, "config: rootdir: %s\n", ptr);
}
} else if (strcmp(key, "LogFile") == 0) {
if(!config->logfile) {
config->logfile = strdup(ptr);
pm_printf(PM_LOG_DEBUG, "config: logfile: %s\n", ptr);
}
} else if (strcmp(key, "XferCommand") == 0) {
config->xfercommand = strdup(ptr);
alpm_option_set_fetchcb(download_with_xfercommand);
pm_printf(PM_LOG_DEBUG, "config: xfercommand: %s\n", ptr);
} else if (strcmp(key, "CleanMethod") == 0) {
if (strcmp(ptr, "KeepInstalled") == 0) {
config->cleanmethod = PM_CLEAN_KEEPINST;
} else if (strcmp(ptr, "KeepCurrent") == 0) {
config->cleanmethod = PM_CLEAN_KEEPCUR;
} else {
pm_printf(PM_LOG_ERROR, _("invalid value for 'CleanMethod' : '%s'\n"), ptr);
ret = 1;
goto cleanup;
}
pm_printf(PM_LOG_DEBUG, "config: cleanmethod: %s\n", ptr);
} else {
pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' not recognized.\n"),
file, linenum, key);
ret = 1;
goto cleanup;
}
} else if(strcmp(key, "Server") == 0) {
/* let's attempt a replacement for the current repo */
char *temp = strreplace(ptr, "$repo", section);
/* let's attempt a replacement for the arch */
const char *arch = alpm_option_get_arch();
char *server;
if(arch) {
server = strreplace(temp, "$arch", arch);
free(temp);
} else {
if(strstr(temp, "$arch")) {
free(temp);
pm_printf(PM_LOG_ERROR, _("The mirror '%s' contains the $arch"
" variable, but no Architecture is defined.\n"), ptr);
ret = 1;
goto cleanup;
}
server = temp;
}
if(alpm_db_setserver(db, server) != 0) {
/* pm_errno is set by alpm_db_setserver */
pm_printf(PM_LOG_ERROR, _("could not add server URL to database '%s': %s (%s)\n"),
alpm_db_get_name(db), server, alpm_strerrorlast());
free(server);
ret = 1;
goto cleanup;
}
free(server);
} else {
pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' not recognized.\n"),
file, linenum, key);
ret = 1;
goto cleanup;
}
}
}
}
cleanup:
if(fp) {
fclose(fp);
}
if(section){
free(section);
}
/* call setlibpaths here to ensure we have called it at least once */
setlibpaths();
pm_printf(PM_LOG_DEBUG, "config: finished parsing %s\n", file);
return(ret);
}
/** Parse a configuration file.
* @param file path to the config file.
* @return 0 on success, non-zero on error
*/
static int parseconfig(const char *file)
{
/* call the real parseconfig function with a null section & db argument */
return(_parseconfig(file, NULL, NULL));
}
/** print commandline to logfile
*/
static void cl_to_log(int argc, char* argv[])
{
size_t size = 0;
int i;
for(i = 0; i<argc; i++) {
size += strlen(argv[i]) + 1;
}
char *cl_text = malloc(size);
if(!cl_text)
return;
char *p = cl_text;
for(i = 0; i<argc-1; i++) {
strcpy(p, argv[i]);
p += strlen(argv[i]);
*p++ = ' ';
}
strcpy(p, argv[i]);
alpm_logaction("Running '%s'\n", cl_text);
free(cl_text);
}
/** Main function.
* @param argc argc
* @param argv argv
* @return A return code indicating success, failure, etc.
*/
int main(int argc, char *argv[])
{
int ret = 0;
struct sigaction new_action, old_action;
#if defined(HAVE_GETEUID) && !defined(CYGWIN)
/* geteuid undefined in CYGWIN */
uid_t myuid = geteuid();
#endif
#if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H)
/*setenv("MALLOC_TRACE","pacman.mtrace", 0);*/
mtrace();
#endif
/* Set signal handlers */
/* Set up the structure to specify the new action. */
new_action.sa_handler = handler;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction(SIGINT, NULL, &old_action);
if(old_action.sa_handler != SIG_IGN) {
sigaction(SIGINT, &new_action, NULL);
}
sigaction(SIGTERM, NULL, &old_action);
if(old_action.sa_handler != SIG_IGN) {
sigaction(SIGTERM, &new_action, NULL);
}
sigaction(SIGSEGV, NULL, &old_action);
if(old_action.sa_handler != SIG_IGN) {
sigaction(SIGSEGV, &new_action, NULL);
}
/* i18n init */
#if defined(ENABLE_NLS)
localize();
#endif
/* set user agent for downloading */
setuseragent();
/* init config data */
config = config_new();
/* disable progressbar if the output is redirected */
if(!isatty(1)) {
config->noprogressbar = 1;
}
/* initialize library */
if(alpm_initialize() == -1) {
pm_printf(PM_LOG_ERROR, _("failed to initialize alpm library (%s)\n"),
alpm_strerrorlast());
cleanup(EXIT_FAILURE);
}
/* Setup logging as soon as possible, to print out maximum debugging info */
alpm_option_set_logcb(cb_log);
alpm_option_set_dlcb(cb_dl_progress);
/* define paths to reasonable defaults */
alpm_option_set_root(ROOTDIR);
alpm_option_set_dbpath(DBPATH);
alpm_option_set_logfile(LOGFILE);
/* Priority of options:
* 1. command line
* 2. config file
* 3. compiled-in defaults
* However, we have to parse the command line first because a config file
* location can be specified here, so we need to make sure we prefer these
* options over the config file coming second.
*/
/* parse the command line */
ret = parseargs(argc, argv);
if(ret != 0) {
cleanup(ret);
}
/* parse the config file */
ret = parseconfig(config->configfile);
if(ret != 0) {
cleanup(ret);
}
/* set TotalDownload callback if option enabled */
if(config->totaldownload) {
alpm_option_set_totaldlcb(cb_dl_total);
}
/* noask is meant to be non-interactive */
if(config->noask) {
config->noconfirm = 1;
}
#if defined(HAVE_GETEUID) && !defined(CYGWIN)
/* check if we have sufficient permission for the requested operation */
if(myuid > 0 && needs_root()) {
pm_printf(PM_LOG_ERROR, _("you cannot perform this operation unless you are root.\n"));
cleanup(EXIT_FAILURE);
}
#endif
if(config->verbose > 0) {
alpm_list_t *i;
printf("Root : %s\n", alpm_option_get_root());
printf("Conf File : %s\n", config->configfile);
printf("DB Path : %s\n", alpm_option_get_dbpath());
printf("Cache Dirs: ");
for(i = alpm_option_get_cachedirs(); i; i = alpm_list_next(i)) {
printf("%s ", (char*)alpm_list_getdata(i));
}
printf("\n");
printf("Lock File : %s\n", alpm_option_get_lockfile());
printf("Log File : %s\n", alpm_option_get_logfile());
list_display("Targets :", pm_targets);
}
/* Opening local database */
db_local = alpm_db_register_local();
if(db_local == NULL) {
pm_printf(PM_LOG_ERROR, _("could not register 'local' database (%s)\n"),
alpm_strerrorlast());
cleanup(EXIT_FAILURE);
}
/* Log commandline */
if(needs_root()) {
cl_to_log(argc, argv);
}
/* start the requested operation */
switch(config->op) {
case PM_OP_REMOVE:
ret = pacman_remove(pm_targets);
break;
case PM_OP_UPGRADE:
ret = pacman_upgrade(pm_targets);
break;
case PM_OP_QUERY:
ret = pacman_query(pm_targets);
break;
case PM_OP_SYNC:
ret = pacman_sync(pm_targets);
break;
case PM_OP_DEPTEST:
ret = pacman_deptest(pm_targets);
break;
default:
pm_printf(PM_LOG_ERROR, _("no operation specified (use -h for help)\n"));
ret = EXIT_FAILURE;
}
cleanup(ret);
/* not reached */
return(EXIT_SUCCESS);
}
/* vim: set ts=2 sw=2 noet: */
|