summaryrefslogtreecommitdiff
path: root/src/pacman/pacman.c
blob: 808d8ad1364e96992714c82207958bcf8866b38c (plain)
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
/*
 *  pacman.c
 * 
 *  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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
 *  USA.
 */

#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libintl.h>
#include <locale.h>
#if defined(__APPLE__)
#include <malloc/malloc.h>
#elif defined(__OpenBSD__) || defined(__APPLE__)
#include <sys/malloc.h>
#elif defined(CYGWIN)
#include <libgen.h> /* basename */
#else
#include <mcheck.h> /* debug */
#endif
#include <time.h>

#include <alpm.h>
#include <alpm_list.h>
/* pacman */
#include "util.h"
#include "log.h"
#include "downloadprog.h"
#include "conf.h"
#include "package.h"
#include "add.h"
#include "remove.h"
#include "upgrade.h"
#include "query.h"
#include "sync.h"
#include "deptest.h"

#if defined(__OpenBSD__) || defined(__APPLE__)
#define BSD
#endif

/* Operations */
enum {
	PM_OP_MAIN = 1,
	PM_OP_ADD,
	PM_OP_REMOVE,
	PM_OP_UPGRADE,
	PM_OP_QUERY,
	PM_OP_SYNC,
	PM_OP_DEPTEST
};

config_t *config;

pmdb_t *db_local;
/* list of targets specified on command line */
static alpm_list_t *pm_targets;

/* Display usage/syntax for the specified operation.
 *     op:     the operation code requested
 *     myname: basename(argv[0])
 */
static void usage(int op, char *myname)
{
	if(op == PM_OP_MAIN) {
		printf(_("usage:  %s {-h --help}\n"), myname);
		printf(_("        %s {-V --version}\n"), myname);
		printf(_("        %s {-A --add}     [options] <file>\n"), myname);
		printf(_("        %s {-R --remove}  [options] <package>\n"), myname);
		printf(_("        %s {-U --upgrade} [options] <file>\n"), myname);
		printf(_("        %s {-F --freshen} [options] <file>\n"), myname);
		printf(_("        %s {-Q --query}   [options] [package]\n"), myname);
		printf(_("        %s {-S --sync}    [options] [package]\n"), myname);
		printf(_("\nuse '%s --help' with other options for more syntax\n"), myname);
	} else {
		if(op == PM_OP_ADD) {
			printf(_("usage:  %s {-A --add} [options] <file>\n"), myname);
			printf(_("options:\n"));
			printf(_("  -d, --nodeps        skip dependency checks\n"));
			printf(_("  -f, --force         force install, overwrite conflicting files\n"));
		} else if(op == PM_OP_REMOVE) {
			printf(_("usage:  %s {-R --remove} [options] <package>\n"), myname);
			printf(_("options:\n"));
			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"));
		} else if(op == PM_OP_UPGRADE) {
			if(config->flags & PM_TRANS_FLAG_FRESHEN) {
				printf(_("usage:  %s {-F --freshen} [options] <file>\n"), myname);
			} else {
				printf(_("usage:  %s {-U --upgrade} [options] <file>\n"), myname);
			}
			printf(_("options:\n"));
			printf(_("  -d, --nodeps        skip dependency checks\n"));
			printf(_("  -f, --force         force install, overwrite conflicting files\n"));
		} else if(op == PM_OP_QUERY) {
			printf(_("usage:  %s {-Q --query} [options] [package]\n"), myname);
			printf(_("options:\n"));
			printf(_("  -c, --changelog     view the changelog of a package\n"));
			printf(_("  -e, --orphans       list all packages installed as dependencies but no longer\n"));
			printf(_("                      required by any package\n"));
			printf(_("  -g, --groups        view all members of a package group\n"));
			printf(_("  -i, --info          view package information\n"));
			printf(_("  -l, --list          list the contents of the queried package\n"));
			printf(_("  -m, --foreign       list all packages that were not found in the sync db(s)\n"));
			printf(_("  -o, --owns <file>   query the package that owns <file>\n"));
			printf(_("  -p, --file          query the package file [package] instead of the database\n"));
			printf(_("  -s, --search        search locally-installed packages for matching strings\n"));
		} else if(op == PM_OP_SYNC) {
			printf(_("usage:  %s {-S --sync} [options] [package]\n"), myname);
			printf(_("options:\n"));
			printf(_("  -c, --clean         remove old packages from cache directory (use -cc for all)\n"));
			printf(_("  -d, --nodeps        skip dependency checks\n"));
			printf(_("  -e, --dependsonly   install dependencies only\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(_("  -p, --print-uris    print out URIs for given packages and their dependencies\n"));
			printf(_("  -s, --search        search remote repositories for matching strings\n"));
			printf(_("  -u, --sysupgrade    upgrade all packages that are out of date\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(_("      --ignore <pkg>  ignore a package upgrade (can be used more than once)\n"));
		}
		printf(_("      --config <path> set an alternate configuration file\n"));
		printf(_("      --noconfirm     do not ask for anything confirmation\n"));
		printf(_("      --ask  <number> pre-specify answers for questions (see manpage)\n"));
		printf(_("      --noprogressbar do not show a progress bar when downloading files\n"));
		printf(_("      --noscriptlet   do not execute the install scriptlet if there is any\n"));
		printf(_("  -v, --verbose       be verbose\n"));
		printf(_("  -r, --root <path>   set an alternate installation root\n"));
		printf(_("  -b, --dbpath <path> set an alternate database location\n"));
	}
}

/* Version
 */
static void version()
{
	printf("\n");
	printf(" .--.                  Pacman v%s - libalpm v%s\n", PACKAGE_VERSION, PM_VERSION);
	printf("/ _.-' .-.  .-.  .-.   Copyright (C) 2002-2006 Judd Vinet <jvinet@zeroflux.org>\n");
	printf("\\  '-. '-'  '-'  '-'   & Frugalware developers <frugalware-devel@frugalware.org>\n");
	printf(" '--'                  \n");
	printf(_("                       This program may be freely redistributed under\n"));
	printf(_("                       the terms of the GNU General Public License\n"));
	printf("\n");
}

static void cleanup(int signum)
{
	if(signum==SIGSEGV)
	{
		fprintf(stderr, "Internal pacman error: Segmentation fault\n"
			"Please submit a full bug report, with the given package if appropriate.\n");
		exit(signum);
	} else if((signum == SIGINT) && (alpm_trans_release() == -1)
						&& (pm_errno == PM_ERR_TRANS_COMMITING)) {
		return;
	}
	if(signum != 0 && config->op_d_vertest == 0) {
		fprintf(stderr, "\n");
	}

	/* free alpm library resources */
	if(alpm_release() == -1) {
		ERR(NL, "%s\n", alpm_strerror(pm_errno));
	}

	/* free memory */
	FREELIST(pm_targets);
	FREECONF(config);

	/* This fixes up any missing newlines (neednl) */
	MSG(NL, "");

	exit(signum);
}

/* Parse command-line arguments for each operation
 *     argc: argc
 *     argv: argv
 *     
 * Returns: 0 on success, 1 on error
 */
static int parseargs(int argc, char *argv[])
{
	int opt;
	int option_index = 0;
	static struct option opts[] =
	{
		{"add",        no_argument,       0, 'A'},
		{"resolve",    no_argument,       0, 'D'}, /* used by 'makepkg -s' */
		{"freshen",    no_argument,       0, 'F'},
		{"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'},
		{"vertest",    no_argument,       0, 'Y'}, /* does the same as the 'vercmp' binary */
		{"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'},
		{"dependsonly",no_argument,       0, 'e'},
		{"orphans",    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'},
		{"list",       no_argument,       0, 'l'},
		{"nosave",     no_argument,       0, 'n'},
		{"foreign",    no_argument,       0, 'm'},
		{"owns",       no_argument,       0, 'o'},
		{"file",       no_argument,       0, 'p'},
		{"print-uris", no_argument,       0, 'p'},
		{"root",       required_argument, 0, 'r'},
		{"recursive",  no_argument,       0, 's'},
		{"search",     no_argument,       0, 's'},
		{"sysupgrade", 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",      required_argument, 0, 1003},
		{"noprogressbar",  no_argument,   0, 1004},
		{"noscriptlet", no_argument,      0, 1005},
		{"ask",        required_argument, 0, 1006},
		{0, 0, 0, 0}
	};
	char root[PATH_MAX];
	struct stat st;

	while((opt = getopt_long(argc, argv, "ARUFQSTDYr:b:vkhscVfmnoldepiuwyg", opts, &option_index))) {
		if(opt < 0) {
			break;
		}
		switch(opt) {
			case 0: break;
			case 1000: config->noconfirm = 1; break;
			case 1001:
				if(config->configfile) {
					free(config->configfile);
				}
				#if defined(__OpenBSD__) || defined(__APPLE__)
				config->configfile = strdup(optarg);
				#else
				config->configfile = strndup(optarg, PATH_MAX);
				#endif
				break;
			case 1002: config->op_s_ignore = alpm_list_add(config->op_s_ignore, strdup(optarg)); break;
			case 1003: config->debug = atoi(optarg); 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 'A': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_ADD); break;
			case 'D':
				config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DEPTEST);
				config->op_d_resolve = 1;
				config->flags |= PM_TRANS_FLAG_ALLDEPS;
				break;
			case 'F':
				config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_UPGRADE);
				config->flags |= PM_TRANS_FLAG_FRESHEN;
				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 'Y':
				config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DEPTEST);
				config->op_d_vertest = 1;
				break;
			case 'b':
			  if(stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
					pm_fprintf(stderr, NL, _("error: '%s' is not a valid db path\n"), optarg);
					exit(EXIT_FAILURE);
				}
				alpm_option_set_dbpath(optarg);
				config->dbpath = alpm_option_get_dbpath(optarg);
				break;
			case 'c':
				(config->op_s_clean)++;
				config->flags |= PM_TRANS_FLAG_CASCADE;
				config->op_q_changelog = 1;
				break;
			case 'd': config->flags |= PM_TRANS_FLAG_NODEPS; break;
			case 'e':
				config->op_q_orphans = 1;
				config->flags |= PM_TRANS_FLAG_DEPENDSONLY;
				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; 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->flags |= PM_TRANS_FLAG_PRINTURIS;
				break;
			case 'r':
			  printf("setting root path=%s\n", optarg);
				if(realpath(optarg, root) == NULL) {
					perror(_("bad root path"));
					return(1);
				}
				config->root = strdup(root);
				break;
			case 's':
				config->op_s_search = 1;
				config->op_q_search = 1;
				config->flags |= PM_TRANS_FLAG_RECURSE;
				break;
			case 'u': config->op_s_upgrade = 1; 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) {
		ERR(NL, _("only one operation may be used at a time\n"));
		return(1);
	}

	if(config->help) {
		usage(config->op, basename(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);
}

int main(int argc, char *argv[])
{
	int ret = 0;
	char *lang = NULL;
#ifndef CYGWIN
	uid_t myuid;
#endif

#if defined(PACMAN_DEBUG) && !defined(CYGWIN) && !defined(BSD)
	/*setenv("MALLOC_TRACE","pacman.mtrace", 0);*/
	mtrace();
#endif
	/* set signal handlers */
	signal(SIGINT, cleanup);
	signal(SIGTERM, cleanup);
	signal(SIGSEGV, cleanup);

	/* i18n init */
	lang=getenv("LC_ALL");
	if(lang==NULL || lang[0]=='\0')
		lang=getenv("LC_MESSAGES");
	if (lang==NULL || lang[0]=='\0')
		lang=getenv("LANG");

	setlocale(LC_ALL, lang);
	/* workaround for tr_TR */
	if(lang && !strcmp(lang, "tr_TR"))
		setlocale(LC_CTYPE, "C");
	bindtextdomain("pacman", "/usr/share/locale");
	textdomain("pacman");

	/* init config data */
	config = config_new();
	config->op = PM_OP_MAIN;
	config->debug |= PM_LOG_ERROR;
	config->debug |= PM_LOG_WARNING;
	/* disable progressbar if the output is redirected */
	if(!isatty(1)) {
		config->noprogressbar = 1;
	}

	/* parse the command line */
	ret = parseargs(argc, argv);
	if(ret != 0) {
		config_free(config);
		exit(ret);
	}

#ifndef CYGWIN
	/* see if we're root or not */
	myuid = geteuid();
#ifndef FAKEROOT
	if(!myuid && getenv("FAKEROOTKEY")) {
		/* fakeroot doesn't count, we're non-root */
		myuid = 99;
	}
#endif

	/* check if we have sufficient permission for the requested operation */
	if(myuid > 0) {
		if(config->op != PM_OP_MAIN && config->op != PM_OP_QUERY && config->op != PM_OP_DEPTEST) {
			if((config->op == PM_OP_SYNC && !config->op_s_sync &&
					(config->op_s_search || config->group || config->op_q_list || config->op_q_info
					 || config->flags & PM_TRANS_FLAG_PRINTURIS))
				 || (config->op == PM_OP_DEPTEST && !config->op_d_resolve)
				 || (config->root != NULL)) {
				/* special case: PM_OP_SYNC can be used w/ config->op_s_search by any user */
				/* special case: ignore root user check if -r is specified, fall back on
				 * normal FS checking */
			} else {
				ERR(NL, _("you cannot perform this operation unless you are root.\n"));
				config_free(config);
				exit(EXIT_FAILURE);
			}
		}
	}
#endif

	if(config->root == NULL) {
		config->root = strdup(PM_ROOT);
	}

	/* initialize pm library */
	if(alpm_initialize(config->root) == -1) {
		ERR(NL, _("failed to initilize alpm library (%s)\n"), alpm_strerror(pm_errno));
		cleanup(1);
	}

	/* Setup logging as soon as possible, to print out maximum debugging info */
	alpm_option_set_logmask(config->debug);
	alpm_option_set_logcb(cb_log);

	if(config->configfile == NULL) {
		config->configfile = strdup(PACCONF);
	}

	if(alpm_parse_config(config->configfile, NULL, "") != 0) {
		ERR(NL, _("failed to parse config (%s)\n"), alpm_strerror(pm_errno));
		cleanup(1);
	}

	/* set library parameters */
	alpm_option_set_dlcb(log_progress);

	config->dbpath = alpm_option_get_dbpath();
	config->cachedir = alpm_option_get_cachedir();

	alpm_list_t *i;
	for(i = config->op_s_ignore; i; i = alpm_list_next(i)) {
		alpm_option_add_ignorepkg(alpm_list_getdata(i));
	}
	
	if(config->verbose > 0) {
		printf("Root  : %s\n", config->root);
		printf("DBPath: %s\n", config->dbpath);
		list_display(_("Targets:"), pm_targets);
	}

	/* Opening local database */
	db_local = alpm_db_register("local");
	if(db_local == NULL) {
		ERR(NL, _("could not register 'local' database (%s)\n"), alpm_strerror(pm_errno));
		cleanup(1);
	}

	if(alpm_list_count(pm_targets) == 0 && !(config->op == PM_OP_QUERY || (config->op == PM_OP_SYNC
	   && (config->op_s_sync || config->op_s_upgrade || config->op_s_clean || config->group 
	   || config->op_q_list)))) {
		ERR(NL, _("no targets specified (use -h for help)\n"));
		cleanup(1);
	}

	/* start the requested operation */
	switch(config->op) {
		case PM_OP_ADD:     ret = pacman_add(pm_targets);     break;
		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:
			ERR(NL, _("no operation specified (use -h for help)\n"));
			ret = 1;
	}

	cleanup(ret);
	/* not reached */
	return(0);
}

/* vim: set ts=2 sw=2 noet: */