src/main.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Jul 02 11:31:45 2009 +0100 (2009-07-02)
changeset 371 d7eea3164151
parent 363 c75a2d5caae9
child 372 6e93e5485947
permissions -rw-r--r--
Test uninstall scripts survive merge
rhughes@241
     1
/*
rhughes@241
     2
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
rhughes@241
     3
 * Copyright (C) 2008  Red Hat, Inc
ali@351
     4
 * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
rhughes@241
     5
 *
rhughes@241
     6
 * This program is free software; you can redistribute it and/or modify
rhughes@241
     7
 * it under the terms of the GNU General Public License as published by
rhughes@241
     8
 * the Free Software Foundation; either version 2 of the License, or
rhughes@241
     9
 * (at your option) any later version.
rhughes@241
    10
 *
rhughes@241
    11
 * This program is distributed in the hope that it will be useful,
rhughes@241
    12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rhughes@241
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
rhughes@241
    14
 * GNU General Public License for more details.
rhughes@241
    15
 *
rhughes@241
    16
 * You should have received a copy of the GNU General Public License along
rhughes@241
    17
 * with this program; if not, write to the Free Software Foundation, Inc.,
rhughes@241
    18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
rhughes@241
    19
 */
rhughes@241
    20
krh@305
    21
#define _GNU_SOURCE
krh@305
    22
ali@321
    23
#include "config.h"
ali@321
    24
rhughes@241
    25
#include <stdlib.h>
rhughes@241
    26
#include <stddef.h>
rhughes@241
    27
#include <stdio.h>
rhughes@241
    28
#include <stdint.h>
rhughes@241
    29
#include <string.h>
rhughes@241
    30
#include <sys/stat.h>
rhughes@241
    31
#include <unistd.h>
rhughes@241
    32
#include <fcntl.h>
rhughes@241
    33
#include <dirent.h>
ali@325
    34
#include <limits.h>
ali@321
    35
#ifdef HAVE_CURL
rhughes@241
    36
#include <curl/curl.h>
ali@321
    37
#endif
rhughes@241
    38
#include <fnmatch.h>
rhughes@241
    39
#include <errno.h>
rhughes@241
    40
#include "razor.h"
rhughes@241
    41
richard@310
    42
static const char system_repo_filename[] = "system.rzdb";
richard@310
    43
static const char next_repo_filename[] = "system-next.rzdb";
richard@310
    44
static const char rawhide_repo_filename[] = "rawhide.rzdb";
richard@310
    45
static const char updated_repo_filename[] = "system-updated.rzdb";
krh@317
    46
static const char *install_root = "";
rhughes@241
    47
static const char *repo_filename = system_repo_filename;
rhughes@241
    48
static const char *yum_url;
rhughes@241
    49
krh@271
    50
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
krh@271
    51
ali@363
    52
static int
ali@369
    53
update_packages(struct razor_transaction *trans, struct razor_set *system,
ali@369
    54
		struct razor_set *next, struct razor_relocations *relocations);
ali@363
    55
krh@281
    56
static struct razor_package_iterator *
krh@281
    57
create_iterator_from_argv(struct razor_set *set, int argc, const char *argv[])
rhughes@241
    58
{
krh@281
    59
	struct razor_package_query *query;
krh@281
    60
	struct razor_package_iterator *iter;
rhughes@241
    61
	struct razor_package *package;
richard@302
    62
	const char *name, *pattern;
krh@281
    63
	int i, count;
rhughes@241
    64
krh@281
    65
	if (argc == 0)
krh@281
    66
		return razor_package_iterator_create(set);
krh@281
    67
krh@281
    68
	query = razor_package_query_create(set);
krh@281
    69
krh@281
    70
	for (i = 0; i < argc; i++) {
krh@281
    71
		iter = razor_package_iterator_create(set);
krh@281
    72
		pattern = argv[i];
krh@281
    73
		count = 0;
richard@307
    74
		while (razor_package_iterator_next(iter, &package,
richard@307
    75
						   RAZOR_DETAIL_NAME, &name,
richard@307
    76
						   RAZOR_DETAIL_LAST)) {
krh@281
    77
			if (fnmatch(pattern, name, 0) != 0)
krh@281
    78
				continue;
krh@281
    79
krh@281
    80
			razor_package_query_add_package(query, package);
krh@281
    81
			count++;
krh@281
    82
		}
krh@281
    83
		razor_package_iterator_destroy(iter);
krh@281
    84
krh@281
    85
		if (count == 0)
krh@281
    86
			fprintf(stderr,
krh@281
    87
				"no package matches \"%s\"\n", pattern);
rhughes@241
    88
	}
rhughes@241
    89
krh@281
    90
	return razor_package_query_finish(query);
krh@281
    91
}
krh@281
    92
krh@281
    93
#define LIST_PACKAGES_ONLY_NAMES 0x01
krh@281
    94
krh@281
    95
static void
krh@281
    96
list_packages(struct razor_package_iterator *iter, uint32_t flags)
krh@281
    97
{
krh@281
    98
	struct razor_package *package;
krh@281
    99
	const char *name, *version, *arch;
krh@281
   100
krh@281
   101
	while (razor_package_iterator_next(iter, &package,
richard@302
   102
					   RAZOR_DETAIL_NAME, &name,
richard@302
   103
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   104
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   105
					   RAZOR_DETAIL_LAST)) {
krh@281
   106
		if (flags & LIST_PACKAGES_ONLY_NAMES)
rhughes@241
   107
			printf("%s\n", name);
rhughes@241
   108
		else
rhughes@241
   109
			printf("%s-%s.%s\n", name, version, arch);
rhughes@241
   110
	}
krh@281
   111
}
krh@281
   112
krh@281
   113
static int
krh@281
   114
command_list(int argc, const char *argv[])
krh@281
   115
{
krh@281
   116
	struct razor_package_iterator *pi;
krh@281
   117
	struct razor_set *set;
krh@281
   118
	uint32_t flags = 0;
krh@281
   119
	int i = 0;
krh@281
   120
krh@281
   121
	if (i < argc && strcmp(argv[i], "--only-names") == 0) {
krh@281
   122
		flags |= LIST_PACKAGES_ONLY_NAMES;
krh@281
   123
		i++;
krh@281
   124
	}
krh@281
   125
krh@317
   126
	set = razor_root_open_read_only(install_root);
krh@317
   127
	if (set == NULL)
krh@317
   128
		return 1;
krh@317
   129
krh@281
   130
	pi = create_iterator_from_argv(set, argc - i, argv + i);
krh@281
   131
	list_packages(pi, flags);
rhughes@241
   132
	razor_package_iterator_destroy(pi);
rhughes@241
   133
	razor_set_destroy(set);
rhughes@241
   134
rhughes@241
   135
	return 0;
rhughes@241
   136
}
rhughes@241
   137
krh@306
   138
static void
krh@306
   139
list_package_properties(struct razor_set *set,
krh@306
   140
			struct razor_package *package, uint32_t type)
rhughes@241
   141
{
krh@306
   142
	struct razor_property_iterator *pi;
rhughes@241
   143
	struct razor_property *property;
rhughes@241
   144
	const char *name, *version;
krh@247
   145
	uint32_t flags;
rhughes@241
   146
rhughes@241
   147
	pi = razor_property_iterator_create(set, package);
rhughes@241
   148
	while (razor_property_iterator_next(pi, &property,
krh@247
   149
					    &name, &flags, &version)) {
krh@247
   150
		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
rhughes@241
   151
			continue;
krh@247
   152
		printf("%s", name);
krh@247
   153
		if (version[0] != '\0')
krh@247
   154
			printf(" %s %s",
krh@247
   155
			       razor_property_relation_to_string(property),
krh@247
   156
			       version);
krh@247
   157
krh@247
   158
		if (flags & ~(RAZOR_PROPERTY_RELATION_MASK | RAZOR_PROPERTY_TYPE_MASK)) {
krh@247
   159
			printf(" [");
krh@247
   160
			if (flags & RAZOR_PROPERTY_PRE)
krh@247
   161
				printf(" pre");
krh@247
   162
			if (flags & RAZOR_PROPERTY_POST)
krh@247
   163
				printf(" post");
krh@247
   164
			if (flags & RAZOR_PROPERTY_PREUN)
krh@247
   165
				printf(" preun");
krh@247
   166
			if (flags & RAZOR_PROPERTY_POSTUN)
krh@247
   167
				printf(" postun");
krh@247
   168
			printf(" ]");
krh@247
   169
		}
krh@247
   170
		printf("\n");
rhughes@241
   171
	}
rhughes@241
   172
	razor_property_iterator_destroy(pi);
krh@306
   173
}
rhughes@241
   174
krh@306
   175
static int
krh@306
   176
list_properties(int argc, const char *argv[], uint32_t type)
krh@306
   177
{
krh@306
   178
	struct razor_set *set;
krh@306
   179
	struct razor_package *package;
krh@306
   180
	struct razor_package_iterator *pi;
krh@306
   181
	const char *name, *version, *arch;
krh@306
   182
krh@317
   183
	set = razor_root_open_read_only(install_root);
krh@317
   184
	if (set == NULL)
krh@317
   185
		return 1;
krh@317
   186
krh@306
   187
	pi = create_iterator_from_argv(set, argc, argv);
krh@306
   188
	while (razor_package_iterator_next(pi, &package,
richard@307
   189
					   RAZOR_DETAIL_NAME, &name,
richard@307
   190
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   191
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   192
					   RAZOR_DETAIL_LAST))
krh@306
   193
		list_package_properties(set, package, type);
krh@306
   194
	razor_package_iterator_destroy(pi);
rhughes@241
   195
	razor_set_destroy(set);
rhughes@241
   196
rhughes@241
   197
	return 0;
rhughes@241
   198
}
rhughes@241
   199
rhughes@241
   200
static int
rhughes@241
   201
command_list_requires(int argc, const char *argv[])
rhughes@241
   202
{
krh@306
   203
	return list_properties(argc, argv, RAZOR_PROPERTY_REQUIRES);
rhughes@241
   204
}
rhughes@241
   205
rhughes@241
   206
static int
rhughes@241
   207
command_list_provides(int argc, const char *argv[])
rhughes@241
   208
{
krh@306
   209
	return list_properties(argc, argv, RAZOR_PROPERTY_PROVIDES);
rhughes@241
   210
}
rhughes@241
   211
rhughes@241
   212
static int
rhughes@241
   213
command_list_obsoletes(int argc, const char *argv[])
rhughes@241
   214
{
krh@306
   215
	return list_properties(argc, argv, RAZOR_PROPERTY_OBSOLETES);
rhughes@241
   216
}
rhughes@241
   217
rhughes@241
   218
static int
rhughes@241
   219
command_list_conflicts(int argc, const char *argv[])
rhughes@241
   220
{
krh@306
   221
	return list_properties(argc, argv, RAZOR_PROPERTY_CONFLICTS);
rhughes@241
   222
}
rhughes@241
   223
rhughes@241
   224
static int
ali@369
   225
command_list_scripts(int argc, const char *argv[])
ali@369
   226
{
ali@369
   227
	struct razor_set *set;
ali@369
   228
	struct razor_package *package;
ali@369
   229
	struct razor_package_iterator *pi;
ali@369
   230
	const char *preunprog, *preun, *postunprog, *postun;
ali@369
   231
ali@369
   232
	set = razor_root_open_read_only(install_root);
ali@369
   233
	if (set == NULL)
ali@369
   234
		return 1;
ali@369
   235
ali@369
   236
	pi = create_iterator_from_argv(set, argc, argv);
ali@369
   237
	while (razor_package_iterator_next(pi, &package,
ali@369
   238
					   RAZOR_DETAIL_PREUNPROG, &preunprog,
ali@369
   239
					   RAZOR_DETAIL_PREUN, &preun,
ali@369
   240
					   RAZOR_DETAIL_POSTUNPROG, &postunprog,
ali@369
   241
					   RAZOR_DETAIL_POSTUN, &postun,
ali@369
   242
					   RAZOR_DETAIL_LAST)) {
ali@369
   243
		if (preun && *preun) {
ali@369
   244
			printf("preuninstall scriptlet");
ali@369
   245
			if (preunprog && *preunprog)
ali@369
   246
				printf(" (using %s)",preunprog);
ali@369
   247
			printf(":\n%s\n",preun);
ali@369
   248
		}
ali@369
   249
		if (postun && *postun) {
ali@369
   250
			printf("postuninstall scriptlet");
ali@369
   251
			if (postunprog && *postunprog)
ali@369
   252
				printf(" (using %s)",postunprog);
ali@369
   253
			printf(":\n%s\n",postun);
ali@369
   254
		}
ali@369
   255
	}
ali@369
   256
	razor_package_iterator_destroy(pi);
ali@369
   257
	razor_set_destroy(set);
ali@369
   258
ali@369
   259
	return 0;
ali@369
   260
}
ali@369
   261
ali@369
   262
static int
rhughes@241
   263
command_list_files(int argc, const char *argv[])
rhughes@241
   264
{
rhughes@241
   265
	struct razor_set *set;
rhughes@241
   266
krh@317
   267
	set = razor_root_open_read_only(install_root);
rhughes@241
   268
	if (set == NULL)
rhughes@241
   269
		return 1;
krh@317
   270
rhughes@241
   271
	razor_set_list_files(set, argv[0]);
rhughes@241
   272
	razor_set_destroy(set);
rhughes@241
   273
rhughes@241
   274
	return 0;
rhughes@241
   275
}
rhughes@241
   276
rhughes@241
   277
static int
rhughes@241
   278
command_list_file_packages(int argc, const char *argv[])
rhughes@241
   279
{
rhughes@241
   280
	struct razor_set *set;
rhughes@241
   281
	struct razor_package_iterator *pi;
rhughes@241
   282
krh@317
   283
	set = razor_root_open_read_only(install_root);
rhughes@241
   284
	if (set == NULL)
rhughes@241
   285
		return 1;
rhughes@241
   286
rhughes@241
   287
	pi = razor_package_iterator_create_for_file(set, argv[0]);
krh@281
   288
	list_packages(pi, 0);
rhughes@241
   289
	razor_package_iterator_destroy(pi);
rhughes@241
   290
rhughes@241
   291
	razor_set_destroy(set);
rhughes@241
   292
rhughes@241
   293
	return 0;
rhughes@241
   294
}
rhughes@241
   295
rhughes@241
   296
static int
rhughes@241
   297
command_list_package_files(int argc, const char *argv[])
rhughes@241
   298
{
rhughes@241
   299
	struct razor_set *set;
krh@306
   300
	struct razor_package_iterator *pi;
krh@306
   301
	struct razor_package *package;
krh@306
   302
	const char *name, *version, *arch;
rhughes@241
   303
krh@317
   304
	set = razor_root_open_read_only(install_root);
rhughes@241
   305
	if (set == NULL)
rhughes@241
   306
		return 1;
jbowes@288
   307
krh@306
   308
	pi = create_iterator_from_argv(set, argc, argv);
krh@306
   309
	while (razor_package_iterator_next(pi, &package,
richard@307
   310
					   RAZOR_DETAIL_NAME, &name,
richard@307
   311
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   312
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   313
					   RAZOR_DETAIL_LAST))
krh@306
   314
		razor_set_list_package_files(set, package);
krh@306
   315
	razor_package_iterator_destroy(pi);
krh@306
   316
rhughes@241
   317
	razor_set_destroy(set);
rhughes@241
   318
rhughes@241
   319
	return 0;
rhughes@241
   320
}
rhughes@241
   321
rhughes@241
   322
static int
rhughes@241
   323
list_property_packages(const char *ref_name,
rhughes@241
   324
		       const char *ref_version,
krh@247
   325
		       uint32_t type)
rhughes@241
   326
{
rhughes@241
   327
	struct razor_set *set;
rhughes@241
   328
	struct razor_property *property;
krh@281
   329
	struct razor_property_iterator *prop_iter;
krh@281
   330
	struct razor_package_iterator *pkg_iter;
rhughes@241
   331
	const char *name, *version;
krh@247
   332
	uint32_t flags;
rhughes@241
   333
rhughes@241
   334
	if (ref_name == NULL)
rhughes@241
   335
		return 0;
rhughes@241
   336
krh@317
   337
	set = razor_root_open_read_only(install_root);
rhughes@241
   338
	if (set == NULL)
rhughes@241
   339
		return 1;
rhughes@241
   340
krh@281
   341
	prop_iter = razor_property_iterator_create(set, NULL);
krh@281
   342
	while (razor_property_iterator_next(prop_iter, &property,
krh@247
   343
					    &name, &flags, &version)) {
rhughes@241
   344
		if (strcmp(ref_name, name) != 0)
rhughes@241
   345
			continue;
krh@247
   346
		if (ref_version &&
krh@247
   347
		    (flags & RAZOR_PROPERTY_RELATION_MASK) == RAZOR_PROPERTY_EQUAL &&
rhughes@241
   348
		    strcmp(ref_version, version) != 0)
rhughes@241
   349
			continue;
krh@247
   350
		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
rhughes@241
   351
			continue;
rhughes@241
   352
krh@281
   353
		pkg_iter =
krh@281
   354
			razor_package_iterator_create_for_property(set,
krh@281
   355
								   property);
krh@281
   356
		list_packages(pkg_iter, 0);
krh@281
   357
		razor_package_iterator_destroy(pkg_iter);
rhughes@241
   358
	}
krh@281
   359
	razor_property_iterator_destroy(prop_iter);
rhughes@241
   360
krh@317
   361
	razor_set_destroy(set);
krh@317
   362
rhughes@241
   363
	return 0;
rhughes@241
   364
}
rhughes@241
   365
rhughes@241
   366
static int
rhughes@241
   367
command_what_requires(int argc, const char *argv[])
rhughes@241
   368
{
rhughes@241
   369
	return list_property_packages(argv[0], argv[1],
rhughes@241
   370
				      RAZOR_PROPERTY_REQUIRES);
rhughes@241
   371
}
rhughes@241
   372
rhughes@241
   373
static int
rhughes@241
   374
command_what_provides(int argc, const char *argv[])
rhughes@241
   375
{
rhughes@241
   376
	return list_property_packages(argv[0], argv[1],
rhughes@241
   377
				      RAZOR_PROPERTY_PROVIDES);
rhughes@241
   378
}
rhughes@241
   379
rhughes@241
   380
static int
rhughes@241
   381
show_progress(void *clientp,
rhughes@241
   382
	      double dltotal, double dlnow, double ultotal, double ulnow)
rhughes@241
   383
{
rhughes@241
   384
	const char *file = clientp;
rhughes@241
   385
rhughes@241
   386
	if (!dlnow < dltotal)
rhughes@241
   387
		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
rhughes@241
   388
			file, (int) dlnow / 1024, (int) dltotal / 1024);
rhughes@241
   389
rhughes@241
   390
	return 0;
rhughes@241
   391
}
rhughes@241
   392
rhughes@241
   393
static int
rhughes@241
   394
download_if_missing(const char *url, const char *file)
rhughes@241
   395
{
ali@321
   396
#ifndef HAVE_CURL
ali@321
   397
	return 1;
ali@321
   398
#else
rhughes@241
   399
	CURL *curl;
rhughes@241
   400
	struct stat buf;
rhughes@241
   401
	char error[256];
rhughes@241
   402
	FILE *fp;
rhughes@241
   403
	CURLcode res;
rhughes@241
   404
	long response;
rhughes@241
   405
rhughes@241
   406
	curl = curl_easy_init();
rhughes@241
   407
	if (curl == NULL)
rhughes@241
   408
		return 1;
rhughes@241
   409
rhughes@241
   410
	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
rhughes@241
   411
	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
rhughes@241
   412
	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
rhughes@241
   413
	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
rhughes@241
   414
rhughes@241
   415
	if (stat(file, &buf) < 0) {
ali@345
   416
		fp = fopen(file, "wb");
rhughes@241
   417
		if (fp == NULL) {
rhughes@241
   418
			fprintf(stderr,
rhughes@241
   419
				"failed to open %s for writing\n", file);
rhughes@241
   420
			return -1;
rhughes@241
   421
		}
rhughes@241
   422
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
rhughes@241
   423
		curl_easy_setopt(curl, CURLOPT_URL, url);
rhughes@241
   424
		res = curl_easy_perform(curl);
rhughes@241
   425
		fclose(fp);
rhughes@241
   426
		if (res != CURLE_OK) {
rhughes@241
   427
			fprintf(stderr, "curl error: %s\n", error);
rhughes@241
   428
			unlink(file);
rhughes@241
   429
			return -1;
rhughes@241
   430
		}
rhughes@241
   431
		res = curl_easy_getinfo(curl,
rhughes@241
   432
					CURLINFO_RESPONSE_CODE, &response);
rhughes@241
   433
		if (res != CURLE_OK) {
rhughes@241
   434
			fprintf(stderr, "curl error: %s\n", error);
rhughes@241
   435
                        unlink(file);
rhughes@241
   436
                        return -1;
rhughes@241
   437
		}
rhughes@241
   438
		if (response != 200) {
rhughes@241
   439
			fprintf(stderr, " - failed %ld\n", response);
rhughes@241
   440
                        unlink(file);
rhughes@241
   441
                        return -1;
rhughes@241
   442
		}
rhughes@241
   443
		fprintf(stderr, "\n");
rhughes@241
   444
	}
rhughes@241
   445
rhughes@241
   446
	curl_easy_cleanup(curl);
rhughes@241
   447
rhughes@241
   448
	return 0;
ali@321
   449
#endif	/* HAVE_CURL */
rhughes@241
   450
}
rhughes@241
   451
rhughes@241
   452
#define YUM_URL "http://download.fedora.redhat.com" \
rhughes@241
   453
	"/pub/fedora/linux/development/i386/os"
rhughes@241
   454
rhughes@241
   455
static int
rhughes@241
   456
command_import_yum(int argc, const char *argv[])
rhughes@241
   457
{
rhughes@241
   458
	struct razor_set *set;
rhughes@241
   459
	char buffer[512];
rhughes@241
   460
rhughes@241
   461
	printf("downloading from %s.\n", yum_url);
rhughes@241
   462
	snprintf(buffer, sizeof buffer,
rhughes@241
   463
		 "%s/repodata/primary.xml.gz", yum_url);
rhughes@241
   464
	if (download_if_missing(buffer, "primary.xml.gz") < 0)
rhughes@241
   465
		return -1;
rhughes@241
   466
	snprintf(buffer, sizeof buffer,
rhughes@241
   467
		 "%s/repodata/filelists.xml.gz", yum_url);
rhughes@241
   468
	if (download_if_missing(buffer, "filelists.xml.gz") < 0)
rhughes@241
   469
		return -1;
rhughes@241
   470
rhughes@241
   471
	set = razor_set_create_from_yum();
rhughes@241
   472
	if (set == NULL)
rhughes@241
   473
		return 1;
ali@342
   474
	if (razor_set_write(set, rawhide_repo_filename, RAZOR_REPO_FILE_MAIN)) {
ali@342
   475
		perror(rawhide_repo_filename);
ali@342
   476
		return -1;
ali@342
   477
	}
ali@342
   478
	if (razor_set_write(set, "rawhide-details.rzdb",
ali@342
   479
	    RAZOR_REPO_FILE_DETAILS)) {
ali@342
   480
		perror("rawhide-details.rzdb");
ali@342
   481
		return -1;
ali@342
   482
	}
ali@342
   483
	if (razor_set_write(set, "rawhide-files.rzdb", RAZOR_REPO_FILE_FILES)) {
ali@342
   484
		perror("rawhide-files.rzdb");
ali@342
   485
		return -1;
ali@342
   486
	}
rhughes@241
   487
	razor_set_destroy(set);
rhughes@241
   488
	printf("wrote %s\n", rawhide_repo_filename);
rhughes@241
   489
rhughes@241
   490
	return 0;
rhughes@241
   491
}
rhughes@241
   492
ali@320
   493
#if HAVE_RPMLIB
rhughes@241
   494
static int
rhughes@241
   495
command_import_rpmdb(int argc, const char *argv[])
rhughes@241
   496
{
rhughes@241
   497
	struct razor_set *set;
krh@317
   498
	struct razor_root *root;
krh@317
   499
krh@317
   500
	root = razor_root_open(install_root);
krh@317
   501
	if (root == NULL)
krh@317
   502
		return 1;
rhughes@241
   503
rhughes@241
   504
	set = razor_set_create_from_rpmdb();
rhughes@241
   505
	if (set == NULL)
rhughes@241
   506
		return 1;
rhughes@241
   507
krh@317
   508
	razor_root_update(root, set);
krh@317
   509
krh@317
   510
	return razor_root_commit(root);
rhughes@241
   511
}
ali@320
   512
#endif
rhughes@241
   513
rhughes@241
   514
static int
rhughes@241
   515
mark_packages_for_update(struct razor_transaction *trans,
rhughes@241
   516
			 struct razor_set *set, const char *pattern)
rhughes@241
   517
{
rhughes@241
   518
	struct razor_package_iterator *pi;
rhughes@241
   519
	struct razor_package *package;
richard@302
   520
	const char *name;
rhughes@241
   521
	int matches = 0;
rhughes@241
   522
rhughes@241
   523
	pi = razor_package_iterator_create(set);
rhughes@241
   524
	while (razor_package_iterator_next(pi, &package,
richard@307
   525
					   RAZOR_DETAIL_NAME, &name,
richard@307
   526
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   527
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   528
			razor_transaction_update_package(trans, package);
rhughes@241
   529
			matches++;
rhughes@241
   530
		}
rhughes@241
   531
	}
rhughes@241
   532
	razor_package_iterator_destroy(pi);
rhughes@241
   533
rhughes@241
   534
	return matches;
rhughes@241
   535
}
rhughes@241
   536
rhughes@241
   537
static int
rhughes@241
   538
mark_packages_for_removal(struct razor_transaction *trans,
rhughes@241
   539
			  struct razor_set *set, const char *pattern)
rhughes@241
   540
{
rhughes@241
   541
	struct razor_package_iterator *pi;
rhughes@241
   542
	struct razor_package *package;
richard@302
   543
	const char *name;
rhughes@241
   544
	int matches = 0;
rhughes@241
   545
rhughes@241
   546
	pi = razor_package_iterator_create(set);
richard@307
   547
	while (razor_package_iterator_next(pi, &package,
richard@307
   548
					   RAZOR_DETAIL_NAME, &name,
richard@307
   549
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   550
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   551
			razor_transaction_remove_package(trans, package);
rhughes@241
   552
			matches++;
rhughes@241
   553
		}
rhughes@241
   554
	}
rhughes@241
   555
	razor_package_iterator_destroy(pi);
rhughes@241
   556
rhughes@241
   557
	return matches;
rhughes@241
   558
}
rhughes@241
   559
rhughes@241
   560
static int
rhughes@241
   561
command_update(int argc, const char *argv[])
rhughes@241
   562
{
rhughes@241
   563
	struct razor_set *set, *upstream;
rhughes@241
   564
	struct razor_transaction *trans;
rhughes@241
   565
	int i, errors;
rhughes@241
   566
krh@317
   567
	set = razor_root_open_read_only(install_root);
krh@317
   568
	if (set == NULL)
krh@315
   569
		return 1;
krh@315
   570
rhughes@241
   571
	upstream = razor_set_open(rawhide_repo_filename);
krh@315
   572
	if (upstream == NULL ||
krh@315
   573
	    razor_set_open_details(upstream, "rawhide-details.rzdb") ||
krh@315
   574
	    razor_set_open_files(upstream, "rawhide-files.rzdb"))
rhughes@241
   575
		return 1;
rhughes@241
   576
rhughes@241
   577
	trans = razor_transaction_create(set, upstream);
rhughes@241
   578
	if (argc == 0)
rhughes@241
   579
		razor_transaction_update_all(trans);
rhughes@241
   580
	for (i = 0; i < argc; i++) {
rhughes@241
   581
		if (mark_packages_for_update(trans, set, argv[i]) == 0) {
rhughes@241
   582
			fprintf(stderr, "no match for %s\n", argv[i]);
ali@369
   583
			razor_transaction_destroy(trans);
rhughes@241
   584
			return 1;
rhughes@241
   585
		}
rhughes@241
   586
	}
rhughes@241
   587
krh@252
   588
	razor_transaction_resolve(trans);
krh@252
   589
	errors = razor_transaction_describe(trans);
krh@252
   590
	if (errors) {
krh@252
   591
		fprintf(stderr, "unresolved dependencies\n");
ali@369
   592
		razor_transaction_destroy(trans);
rhughes@241
   593
		return 1;
krh@252
   594
	}
rhughes@241
   595
ali@369
   596
	set = razor_transaction_commit(trans);
jbowes@258
   597
	razor_set_write(set, updated_repo_filename, RAZOR_REPO_FILE_MAIN);
ali@369
   598
	razor_transaction_destroy(trans);
rhughes@241
   599
	razor_set_destroy(set);
rhughes@241
   600
	razor_set_destroy(upstream);
richard@310
   601
	printf("wrote system-updated.rzdb\n");
rhughes@241
   602
rhughes@241
   603
	return 0;
rhughes@241
   604
}
rhughes@241
   605
rhughes@241
   606
static int
rhughes@241
   607
command_remove(int argc, const char *argv[])
rhughes@241
   608
{
ali@363
   609
	struct razor_root *root;
ali@363
   610
	struct razor_set *system, *upstream, *next;
rhughes@241
   611
	struct razor_transaction *trans;
rhughes@241
   612
	int i, errors;
rhughes@241
   613
ali@363
   614
	root = razor_root_open(install_root);
ali@363
   615
	system = razor_root_get_system_set(root);
ali@363
   616
	if (system == NULL) {
ali@363
   617
		razor_root_close(root);
rhughes@241
   618
		return 1;
ali@363
   619
	}
rhughes@241
   620
ali@363
   621
	upstream = razor_set_create_without_root();
ali@363
   622
	trans = razor_transaction_create(system, upstream);
rhughes@241
   623
	for (i = 0; i < argc; i++) {
ali@363
   624
		if (mark_packages_for_removal(trans, system, argv[i]) == 0) {
rhughes@241
   625
			fprintf(stderr, "no match for %s\n", argv[i]);
ali@363
   626
			razor_transaction_destroy(trans);
ali@363
   627
			razor_set_destroy(upstream);
ali@363
   628
			razor_root_close(root);
rhughes@241
   629
			return 1;
rhughes@241
   630
		}
rhughes@241
   631
	}
rhughes@241
   632
ali@350
   633
	razor_transaction_resolve(trans);
ali@350
   634
	errors = razor_transaction_describe(trans);
ali@363
   635
	if (errors) {
ali@363
   636
		razor_transaction_destroy(trans);
ali@363
   637
		razor_set_destroy(upstream);
ali@363
   638
		razor_root_close(root);
rhughes@241
   639
		return 1;
ali@363
   640
	}
rhughes@241
   641
ali@369
   642
	next = razor_transaction_commit(trans);
ali@369
   643
	update_packages(trans, system, next, NULL);
ali@363
   644
	razor_root_update(root, next);
ali@363
   645
ali@369
   646
	razor_transaction_destroy(trans);
ali@363
   647
	razor_set_destroy(next);
rhughes@241
   648
	razor_set_destroy(upstream);
rhughes@241
   649
ali@363
   650
	return razor_root_commit(root);
rhughes@241
   651
}
rhughes@241
   652
rhughes@241
   653
static void
krh@253
   654
print_diff(enum razor_diff_action action,
krh@253
   655
	   struct razor_package *package,
krh@253
   656
	   const char *name,
krh@253
   657
	   const char *version,
krh@253
   658
	   const char *arch,
rhughes@241
   659
	   void *data)
rhughes@241
   660
{
krh@253
   661
	if (action == RAZOR_DIFF_ACTION_ADD)
krh@253
   662
		printf("install %s-%s.%s\n", name, version, arch);
krh@253
   663
	if (action == RAZOR_DIFF_ACTION_REMOVE)
krh@253
   664
		printf("remove %s-%s.%s\n", name, version, arch);
rhughes@241
   665
}
rhughes@241
   666
rhughes@241
   667
static int
rhughes@241
   668
command_diff(int argc, const char *argv[])
rhughes@241
   669
{
rhughes@241
   670
	struct razor_set *set, *updated;
rhughes@241
   671
krh@317
   672
	set = razor_root_open_read_only(install_root);
rhughes@241
   673
	updated = razor_set_open(updated_repo_filename);
rhughes@241
   674
	if (set == NULL || updated == NULL)
rhughes@241
   675
		return 1;
rhughes@241
   676
rhughes@241
   677
	razor_set_diff(set, updated, print_diff, NULL);
rhughes@241
   678
rhughes@241
   679
	razor_set_destroy(set);
rhughes@241
   680
	razor_set_destroy(updated);
rhughes@241
   681
rhughes@241
   682
	return 0;
rhughes@241
   683
}
rhughes@241
   684
rhughes@241
   685
static int
rhughes@241
   686
command_import_rpms(int argc, const char *argv[])
rhughes@241
   687
{
rhughes@241
   688
	DIR *dir;
rhughes@241
   689
	struct dirent *de;
rhughes@241
   690
	struct razor_importer *importer;
rhughes@241
   691
	struct razor_set *set;
rhughes@241
   692
	struct razor_rpm *rpm;
jbowes@263
   693
	int len, imported_count = 0;
rhughes@241
   694
	char filename[256];
rhughes@241
   695
	const char *dirname = argv[0];
rhughes@241
   696
rhughes@241
   697
	if (dirname == NULL) {
rhughes@241
   698
		fprintf(stderr, "usage: razor import-rpms DIR\n");
rhughes@241
   699
		return -1;
rhughes@241
   700
	}
rhughes@241
   701
rhughes@241
   702
	dir = opendir(dirname);
rhughes@241
   703
	if (dir == NULL) {
rhughes@241
   704
		fprintf(stderr, "couldn't read dir %s\n", dirname);
rhughes@241
   705
		return -1;
rhughes@241
   706
	}
rhughes@241
   707
krh@249
   708
	importer = razor_importer_create();
rhughes@241
   709
rhughes@241
   710
	while (de = readdir(dir), de != NULL) {
rhughes@241
   711
		len = strlen(de->d_name);
rhughes@241
   712
		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
rhughes@241
   713
		    continue;
rhughes@241
   714
		snprintf(filename, sizeof filename,
rhughes@241
   715
			 "%s/%s", dirname, de->d_name);
rhughes@241
   716
		rpm = razor_rpm_open(filename);
rhughes@241
   717
		if (rpm == NULL) {
rhughes@241
   718
			fprintf(stderr,
rhughes@241
   719
				"failed to open rpm \"%s\"\n", filename);
rhughes@241
   720
			continue;
rhughes@241
   721
		}
rhughes@241
   722
		if (razor_importer_add_rpm(importer, rpm)) {
rhughes@241
   723
			fprintf(stderr, "couldn't import %s\n", filename);
rhughes@241
   724
			break;
rhughes@241
   725
		}
rhughes@241
   726
		razor_rpm_close(rpm);
jbowes@263
   727
jbowes@263
   728
		printf("\rimporting %d", ++imported_count);
jbowes@263
   729
		fflush(stdout);
rhughes@241
   730
	}
rhughes@241
   731
rhughes@241
   732
	if (de != NULL) {
rhughes@241
   733
		razor_importer_destroy(importer);
rhughes@241
   734
		return -1;
rhughes@241
   735
	}
rhughes@241
   736
jbowes@263
   737
	printf("\nsaving\n");
rhughes@241
   738
	set = razor_importer_finish(importer);
rhughes@241
   739
jbowes@258
   740
	razor_set_write(set, repo_filename, RAZOR_REPO_FILE_MAIN);
richard@310
   741
	razor_set_write(set, "system-details.rzdb", RAZOR_REPO_FILE_DETAILS);
richard@310
   742
	razor_set_write(set, "system-files.rzdb", RAZOR_REPO_FILE_FILES);
rhughes@241
   743
	razor_set_destroy(set);
rhughes@241
   744
	printf("wrote %s\n", repo_filename);
rhughes@241
   745
rhughes@241
   746
	return 0;
rhughes@241
   747
}
rhughes@241
   748
krh@254
   749
static const char *
krh@254
   750
rpm_filename(const char *name, const char *version, const char *arch)
rhughes@241
   751
{
krh@254
   752
	static char file[PATH_MAX];
krh@254
   753
 	const char *v;
krh@254
   754
 
krh@254
   755
 	/* Skip epoch */
krh@253
   756
	v = strchr(version, ':');
krh@254
   757
 	if (v != NULL)
krh@254
   758
 		v = v + 1;
krh@254
   759
 	else
krh@253
   760
		v = version;
rhughes@241
   761
krh@254
   762
	snprintf(file, sizeof file, "%s-%s.%s.rpm", name, v, arch);
rhughes@241
   763
krh@254
   764
	return file;
rhughes@241
   765
}
rhughes@241
   766
krh@254
   767
static int
krh@254
   768
download_packages(struct razor_set *system, struct razor_set *next)
rhughes@241
   769
{
krh@316
   770
	struct razor_install_iterator *ii;
krh@254
   771
	struct razor_package *package;
krh@316
   772
	struct razor_set *set;
krh@316
   773
	enum razor_install_action action;
krh@254
   774
	const char *name, *version, *arch;
krh@254
   775
	char file[PATH_MAX], url[256];
krh@316
   776
	int errors = 0, count;
krh@316
   777
krh@316
   778
	ii = razor_set_create_install_iterator(system, next);
krh@316
   779
	while (razor_install_iterator_next(ii, &set, &package,
krh@316
   780
					   &action, &count)) {
krh@316
   781
		if (action == RAZOR_INSTALL_ACTION_REMOVE)
krh@316
   782
			continue;
krh@316
   783
krh@316
   784
		razor_package_get_details(set, package,
krh@316
   785
					  RAZOR_DETAIL_NAME, &name,
krh@316
   786
					  RAZOR_DETAIL_VERSION, &version,
krh@316
   787
					  RAZOR_DETAIL_ARCH, &arch,
krh@316
   788
					  RAZOR_DETAIL_LAST);
krh@316
   789
		
krh@254
   790
		snprintf(url, sizeof url,
krh@254
   791
			 "%s/Packages/%s",
krh@254
   792
			 yum_url, rpm_filename(name, version, arch));
krh@254
   793
		snprintf(file, sizeof file,
krh@254
   794
			 "rpms/%s", rpm_filename(name, version, arch));
krh@254
   795
		if (download_if_missing(url, file) < 0)
krh@254
   796
			errors++;
krh@254
   797
	}
krh@316
   798
	razor_install_iterator_destroy(ii);
krh@254
   799
krh@254
   800
	if (errors > 0) {
krh@254
   801
		fprintf(stderr, "failed to download %d packages\n", errors);
krh@254
   802
                return -1;
krh@254
   803
        }
krh@254
   804
krh@254
   805
	return 0;
krh@254
   806
}
krh@254
   807
ali@351
   808
static struct razor_set *
ali@351
   809
relocate_packages(struct razor_set *set, struct razor_relocations *relocations)
ali@351
   810
{
ali@351
   811
	struct razor_importer *importer;
ali@351
   812
	struct razor_property_iterator *prop_iter;
ali@351
   813
	struct razor_package_iterator *pkg_iter;
ali@351
   814
 	struct razor_file_iterator *file_iter;
ali@351
   815
 	struct razor_package *package;
ali@351
   816
	struct razor_property *property;
ali@351
   817
	struct razor_rpm *rpm;
ali@351
   818
	const char *name, *version, *arch, *summary, *desc, *url, *license;
ali@369
   819
	const char *preunprog, *preun, *postunprog, *postun;
ali@351
   820
	char file[PATH_MAX];
ali@351
   821
	uint32_t flags;
ali@351
   822
ali@351
   823
	importer = razor_importer_create();
ali@351
   824
	pkg_iter = razor_package_iterator_create(set);
ali@351
   825
ali@351
   826
	while (razor_package_iterator_next(pkg_iter, &package,
ali@351
   827
					   RAZOR_DETAIL_NAME, &name,
ali@351
   828
					   RAZOR_DETAIL_VERSION, &version,
ali@351
   829
					   RAZOR_DETAIL_ARCH, &arch,
ali@351
   830
					   RAZOR_DETAIL_SUMMARY, &summary,
ali@351
   831
					   RAZOR_DETAIL_DESCRIPTION, &desc,
ali@351
   832
					   RAZOR_DETAIL_URL, &url,
ali@351
   833
					   RAZOR_DETAIL_LICENSE, &license,
ali@369
   834
					   RAZOR_DETAIL_PREUNPROG, &preunprog,
ali@369
   835
					   RAZOR_DETAIL_PREUN, &preun,
ali@369
   836
					   RAZOR_DETAIL_POSTUNPROG, &postunprog,
ali@369
   837
					   RAZOR_DETAIL_POSTUN, &postun,
ali@351
   838
					   RAZOR_DETAIL_LAST)) {
ali@351
   839
		snprintf(file, sizeof file,
ali@351
   840
			 "rpms/%s", rpm_filename(name, version, arch));
ali@351
   841
		rpm = razor_rpm_open(file);
ali@351
   842
		if (rpm == NULL) {
ali@351
   843
			fprintf(stderr, "failed to open rpm %s\n", file);
ali@351
   844
			razor_package_iterator_destroy(pkg_iter);
ali@351
   845
			razor_importer_destroy(importer);
ali@351
   846
			return NULL;
ali@351
   847
		}
ali@351
   848
ali@351
   849
		razor_relocations_set_rpm(relocations, rpm);
ali@351
   850
		razor_rpm_close(rpm);
ali@351
   851
ali@351
   852
		razor_importer_begin_package(importer, name, version, arch);
ali@351
   853
		razor_importer_add_details(importer,
ali@351
   854
					   summary, desc, url, license);
ali@351
   855
ali@351
   856
		prop_iter = razor_property_iterator_create(set, package);
ali@351
   857
		while (razor_property_iterator_next(prop_iter, &property,
ali@351
   858
						    &name, &flags, &version))
ali@351
   859
			razor_importer_add_property(importer,
ali@351
   860
						    name, flags, version);
ali@351
   861
		razor_property_iterator_destroy(prop_iter);
ali@351
   862
ali@351
   863
		file_iter = razor_file_iterator_create(set, package);
ali@351
   864
		while (razor_file_iterator_next(file_iter, &name)) {
ali@351
   865
			name = razor_relocations_apply(relocations, name);
ali@351
   866
			razor_importer_add_file(importer, name);
ali@351
   867
		}
ali@351
   868
		razor_file_iterator_destroy(file_iter);
ali@351
   869
ali@369
   870
		razor_importer_add_script(importer, RAZOR_PROPERTY_PREUN,
ali@369
   871
					  preunprog, preun);
ali@369
   872
		razor_importer_add_script(importer, RAZOR_PROPERTY_POSTUN,
ali@369
   873
					  postunprog, postun);
ali@369
   874
ali@351
   875
		razor_importer_finish_package(importer);
ali@351
   876
	}
ali@351
   877
ali@351
   878
	razor_package_iterator_destroy(pkg_iter);
ali@351
   879
	return razor_importer_finish(importer);
ali@351
   880
}
ali@351
   881
krh@254
   882
static int
ali@369
   883
install_package(struct razor_transaction *trans, struct razor_set *set,
ali@369
   884
		struct razor_package *package,
ali@363
   885
		struct razor_relocations *relocations)
ali@363
   886
{
ali@363
   887
	int retval;
ali@363
   888
	const char *name, *version, *arch;
ali@363
   889
	char file[PATH_MAX];
ali@363
   890
	struct razor_rpm *rpm;
ali@363
   891
ali@363
   892
	razor_package_get_details(set, package,
ali@363
   893
				  RAZOR_DETAIL_NAME, &name,
ali@363
   894
				  RAZOR_DETAIL_VERSION, &version,
ali@363
   895
				  RAZOR_DETAIL_ARCH, &arch,
ali@363
   896
				  RAZOR_DETAIL_LAST);
ali@363
   897
ali@363
   898
	printf("install %s-%s\n", name, version);
ali@363
   899
ali@363
   900
	snprintf(file, sizeof file,
ali@363
   901
		 "rpms/%s", rpm_filename(name, version, arch));
ali@363
   902
	rpm = razor_rpm_open(file);
ali@363
   903
	if (rpm == NULL) {
ali@363
   904
		fprintf(stderr, "failed to open rpm %s\n", file);
ali@363
   905
		return -1;
ali@363
   906
	}
ali@363
   907
	if (relocations)
ali@363
   908
		razor_rpm_set_relocations(rpm, relocations);
ali@369
   909
	razor_transaction_fixup_package(trans, package, rpm);
ali@363
   910
	retval = razor_rpm_install(rpm, install_root);
ali@363
   911
	if (retval < 0)
ali@363
   912
		fprintf(stderr, "failed to install rpm %s\n", file);
ali@363
   913
	razor_rpm_close(rpm);
ali@363
   914
	return retval;
ali@363
   915
}
ali@363
   916
ali@363
   917
static int
ali@369
   918
update_packages(struct razor_transaction *trans, struct razor_set *system,
ali@369
   919
		struct razor_set *next, struct razor_relocations *relocations)
krh@254
   920
{
krh@316
   921
	struct razor_install_iterator *ii;
krh@254
   922
	struct razor_package *package;
krh@316
   923
	struct razor_set *set;
krh@316
   924
	enum razor_install_action action;
ali@363
   925
	int retval = 0, count;
rhughes@241
   926
krh@316
   927
	ii = razor_set_create_install_iterator(system, next);
ali@363
   928
	while (!retval && razor_install_iterator_next(ii, &set, &package,
ali@363
   929
						      &action, &count)) {
ali@363
   930
		if (action == RAZOR_INSTALL_ACTION_ADD)
ali@369
   931
			retval = install_package(trans, set, package,
ali@369
   932
						 relocations);
ali@363
   933
		else if (action == RAZOR_INSTALL_ACTION_REMOVE)
ali@369
   934
			retval = razor_package_remove(set, package,
ali@369
   935
						      install_root);
rhughes@241
   936
	}
krh@316
   937
	razor_install_iterator_destroy(ii);
rhughes@241
   938
ali@363
   939
	return retval;
rhughes@241
   940
}
rhughes@241
   941
rhughes@241
   942
static int
rhughes@241
   943
command_install(int argc, const char *argv[])
rhughes@241
   944
{
rhughes@241
   945
	struct razor_root *root;
ali@351
   946
	struct razor_relocations *relocations=NULL;
ali@351
   947
	struct razor_set *system, *upstream, *next, *set;
rhughes@241
   948
	struct razor_transaction *trans;
ali@351
   949
	int i, len, dependencies = 1;
ali@351
   950
	char *oldpath;
rhughes@241
   951
krh@250
   952
	root = razor_root_open(install_root);
krh@255
   953
	if (root == NULL)
krh@255
   954
		return 1;
krh@255
   955
ali@351
   956
	for (i = 0; i < argc; i++) {
ali@351
   957
		if (strcmp(argv[i], "--no-dependencies") == 0)
ali@351
   958
			dependencies = 0;
ali@351
   959
		else if (strcmp(argv[i], "--relocate") == 0) {
ali@351
   960
			i++;
ali@351
   961
			if (i >= argc || strchr(argv[i], '=') == NULL) {
ali@351
   962
				fprintf(stderr,
ali@351
   963
				    "Usage: razor install [OPTION...] RPM\n");
ali@351
   964
				fprintf(stderr, "Options:\n");
ali@351
   965
				fprintf(stderr, "    [--no-dependencies]\n");
ali@351
   966
				fprintf(stderr,
ali@351
   967
				    "    [--relocate OLDPATH=NEWPATH] RPM\n");
ali@351
   968
				return -1;
ali@351
   969
			}
ali@351
   970
			len = strchr(argv[i], '=') - argv[i];
ali@351
   971
			oldpath = malloc(len + 1);
ali@351
   972
			strncpy(oldpath, argv[i], len);
ali@351
   973
			oldpath[len] = '\0';
ali@351
   974
			if (!relocations)
ali@351
   975
			       relocations = razor_relocations_create();
ali@351
   976
			razor_relocations_add(relocations, oldpath,
ali@351
   977
					      argv[i] + len + 1);
ali@351
   978
			free(oldpath);
ali@351
   979
		} else
ali@351
   980
			break;
ali@351
   981
	}
ali@351
   982
krh@250
   983
	system = razor_root_get_system_set(root);
rhughes@241
   984
	upstream = razor_set_open(rawhide_repo_filename);
krh@315
   985
	if (upstream == NULL ||
krh@315
   986
	    razor_set_open_details(upstream, "rawhide-details.rzdb") ||
krh@315
   987
	    razor_set_open_files(upstream, "rawhide-files.rzdb")) {
krh@315
   988
			fprintf(stderr, "couldn't open rawhide repo\n");
krh@315
   989
			razor_root_close(root);
krh@315
   990
			return 1;
krh@315
   991
	}		
krh@315
   992
ali@351
   993
	if (relocations) {
ali@351
   994
		set = relocate_packages(upstream, relocations);
ali@351
   995
		razor_set_destroy(upstream);
ali@351
   996
		upstream = set;
ali@351
   997
	}
ali@351
   998
krh@250
   999
	trans = razor_transaction_create(system, upstream);
rhughes@241
  1000
rhughes@241
  1001
	for (; i < argc; i++) {
rhughes@241
  1002
		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
rhughes@241
  1003
			fprintf(stderr, "no package matched %s\n", argv[i]);
ali@369
  1004
			razor_transaction_destroy(trans);
rhughes@241
  1005
			razor_root_close(root);
rhughes@241
  1006
			return 1;
rhughes@241
  1007
		}
rhughes@241
  1008
	}
rhughes@241
  1009
rhughes@241
  1010
	if (dependencies) {
krh@245
  1011
		razor_transaction_resolve(trans);
krh@245
  1012
		if (razor_transaction_describe(trans) > 0) {
ali@369
  1013
			razor_transaction_destroy(trans);
rhughes@241
  1014
			razor_root_close(root);
rhughes@241
  1015
			return 1;
rhughes@241
  1016
		}
rhughes@241
  1017
	}
rhughes@241
  1018
ali@369
  1019
	next = razor_transaction_commit(trans);
rhughes@241
  1020
rhughes@241
  1021
	if (mkdir("rpms", 0777) && errno != EEXIST) {
rhughes@241
  1022
		fprintf(stderr, "failed to create rpms directory.\n");
ali@369
  1023
		razor_transaction_destroy(trans);
rhughes@241
  1024
		razor_root_close(root);
rhughes@241
  1025
		return 1;
rhughes@241
  1026
	}
rhughes@241
  1027
krh@254
  1028
	if (download_packages(system, next) < 0) {
ali@369
  1029
		razor_transaction_destroy(trans);
rhughes@241
  1030
		razor_root_close(root);
rhughes@241
  1031
                return 1;
rhughes@241
  1032
        }
rhughes@241
  1033
ali@369
  1034
	update_packages(trans, system, next, relocations);
rhughes@241
  1035
ali@369
  1036
	razor_root_update(root, next);
ali@369
  1037
ali@369
  1038
	razor_transaction_destroy(trans);
ali@351
  1039
	if (relocations)
ali@351
  1040
		razor_relocations_destroy(relocations);
rhughes@241
  1041
	razor_set_destroy(next);
rhughes@241
  1042
	razor_set_destroy(upstream);
rhughes@241
  1043
rhughes@241
  1044
	return razor_root_commit(root);
rhughes@241
  1045
}
rhughes@241
  1046
rhughes@241
  1047
static int
rhughes@241
  1048
command_init(int argc, const char *argv[])
rhughes@241
  1049
{
rhughes@241
  1050
	return razor_root_create(install_root);
rhughes@241
  1051
}
rhughes@241
  1052
rhughes@241
  1053
static int
rhughes@241
  1054
command_download(int argc, const char *argv[])
rhughes@241
  1055
{
rhughes@241
  1056
	struct razor_set *set;
rhughes@241
  1057
	struct razor_package_iterator *pi;
rhughes@241
  1058
	struct razor_package *package;
rhughes@241
  1059
	const char *pattern = argv[0], *name, *version, *arch;
rhughes@241
  1060
	char url[256], file[256];
rhughes@241
  1061
	int matches = 0;
rhughes@241
  1062
rhughes@241
  1063
	if (mkdir("rpms", 0777) && errno != EEXIST) {
rhughes@241
  1064
		fprintf(stderr, "failed to create rpms directory.\n");
rhughes@241
  1065
		return 1;
rhughes@241
  1066
	}
rhughes@241
  1067
rhughes@241
  1068
	set = razor_set_open(rawhide_repo_filename);
rhughes@241
  1069
	pi = razor_package_iterator_create(set);
rhughes@241
  1070
	while (razor_package_iterator_next(pi, &package,
richard@302
  1071
					   RAZOR_DETAIL_NAME, &name,
richard@302
  1072
					   RAZOR_DETAIL_VERSION, &version,
richard@307
  1073
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
  1074
					   RAZOR_DETAIL_LAST)) {
rhughes@241
  1075
		if (pattern && fnmatch(pattern, name, 0) != 0)
rhughes@241
  1076
			continue;
rhughes@241
  1077
rhughes@241
  1078
		matches++;
rhughes@241
  1079
		snprintf(url, sizeof url,
rhughes@241
  1080
			 "%s/Packages/%s-%s.%s.rpm",
rhughes@241
  1081
			 yum_url, name, version, arch);
rhughes@241
  1082
		snprintf(file, sizeof file,
rhughes@241
  1083
			 "rpms/%s-%s.%s.rpm", name, version, arch);
rhughes@241
  1084
		download_if_missing(url, file);
rhughes@241
  1085
	}
rhughes@241
  1086
	razor_package_iterator_destroy(pi);
rhughes@241
  1087
	razor_set_destroy(set);
rhughes@241
  1088
rhughes@241
  1089
	if (matches == 0)
rhughes@241
  1090
		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
rhughes@241
  1091
	else if (matches == 1)
rhughes@241
  1092
		fprintf(stderr, "downloaded 1 package\n");
rhughes@241
  1093
	else
rhughes@241
  1094
		fprintf(stderr, "downloaded %d packages\n", matches);
rhughes@241
  1095
rhughes@241
  1096
	return 0;
rhughes@241
  1097
}
rhughes@241
  1098
jbowes@258
  1099
static int
jbowes@258
  1100
command_info(int argc, const char *argv[])
jbowes@258
  1101
{
jbowes@258
  1102
	struct razor_set *set;
jbowes@258
  1103
	struct razor_package_iterator *pi;
jbowes@258
  1104
	struct razor_package *package;
jbowes@258
  1105
	const char *pattern = argv[0], *name, *version, *arch;
jbowes@258
  1106
	const char *summary, *description, *url, *license;
jbowes@258
  1107
krh@317
  1108
	set = razor_root_open_read_only(install_root);
jbowes@288
  1109
	if (set == NULL)
jbowes@288
  1110
		return 1;
krh@317
  1111
jbowes@258
  1112
	pi = razor_package_iterator_create(set);
jbowes@258
  1113
	while (razor_package_iterator_next(pi, &package,
richard@302
  1114
					   RAZOR_DETAIL_NAME, &name,
richard@302
  1115
					   RAZOR_DETAIL_VERSION, &version,
richard@307
  1116
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
  1117
					   RAZOR_DETAIL_LAST)) {
jbowes@258
  1118
		if (pattern && fnmatch(pattern, name, 0) != 0)
jbowes@258
  1119
			continue;
jbowes@258
  1120
richard@302
  1121
		razor_package_get_details (set, package,
richard@302
  1122
					   RAZOR_DETAIL_SUMMARY, &summary,
richard@302
  1123
					   RAZOR_DETAIL_DESCRIPTION, &description,
richard@302
  1124
					   RAZOR_DETAIL_URL, &url,
richard@302
  1125
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
  1126
					   RAZOR_DETAIL_LAST);
jbowes@258
  1127
jbowes@258
  1128
		printf ("Name:        %s\n", name);
jbowes@258
  1129
		printf ("Arch:        %s\n", arch);
jbowes@258
  1130
		printf ("Version:     %s\n", version);
jbowes@258
  1131
		printf ("URL:         %s\n", url);
jbowes@258
  1132
		printf ("License:     %s\n", license);
jbowes@258
  1133
		printf ("Summary:     %s\n", summary);
jbowes@258
  1134
		printf ("Description:\n");
jbowes@258
  1135
		printf ("%s\n", description);
jbowes@258
  1136
		printf ("\n");
jbowes@258
  1137
	}
jbowes@258
  1138
	razor_package_iterator_destroy(pi);
jbowes@258
  1139
	razor_set_destroy(set);
jbowes@258
  1140
jbowes@258
  1141
	return 0;
jbowes@258
  1142
}
jbowes@258
  1143
jbowes@292
  1144
#define SEARCH_MAX 256
jbowes@292
  1145
jbowes@292
  1146
static int
jbowes@292
  1147
command_search(int argc, const char *argv[])
jbowes@292
  1148
{
jbowes@292
  1149
	struct razor_set *set;
jbowes@292
  1150
	struct razor_package_iterator *pi;
jbowes@292
  1151
	struct razor_package *package;
jbowes@292
  1152
	char pattern[SEARCH_MAX];
jbowes@292
  1153
	const char *name, *version, *arch;
jbowes@292
  1154
	const char *summary, *description, *url, *license;
jbowes@292
  1155
jbowes@292
  1156
	if (!argv[0]) {
jbowes@292
  1157
		fprintf(stderr, "must specify a search term\n");
jbowes@292
  1158
		return 1;
jbowes@292
  1159
	}
jbowes@292
  1160
krh@294
  1161
	snprintf(pattern, sizeof pattern, "*%s*", argv[0]);
jbowes@292
  1162
krh@305
  1163
	set = razor_set_open(rawhide_repo_filename);
jbowes@292
  1164
	if (set == NULL)
jbowes@292
  1165
		return 1;
richard@310
  1166
richard@310
  1167
	if (razor_set_open_details(set, "rawhide-details.rzdb"))
jbowes@292
  1168
		return 1;
jbowes@292
  1169
jbowes@292
  1170
	pi = razor_package_iterator_create(set);
jbowes@292
  1171
	while (razor_package_iterator_next(pi, &package,
richard@304
  1172
					   RAZOR_DETAIL_NAME, &name,
richard@304
  1173
					   RAZOR_DETAIL_VERSION, &version,
krh@305
  1174
					   RAZOR_DETAIL_ARCH, &arch,
krh@305
  1175
					   RAZOR_DETAIL_SUMMARY, &summary,
krh@305
  1176
					   RAZOR_DETAIL_DESCRIPTION, &description,
krh@305
  1177
					   RAZOR_DETAIL_URL, &url,
krh@305
  1178
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
  1179
					   RAZOR_DETAIL_LAST)) {
krh@305
  1180
		if (!fnmatch(pattern, name, FNM_CASEFOLD) ||
krh@305
  1181
		    !fnmatch(pattern, url, FNM_CASEFOLD) ||
krh@305
  1182
		    !fnmatch(pattern, summary, FNM_CASEFOLD) ||
krh@305
  1183
		    !fnmatch(pattern, description, FNM_CASEFOLD))
krh@305
  1184
			printf("%s-%s.%s: %s\n", name, version, arch, summary);
jbowes@292
  1185
	}
jbowes@292
  1186
	razor_package_iterator_destroy(pi);
jbowes@292
  1187
	razor_set_destroy(set);
jbowes@292
  1188
jbowes@292
  1189
	return 0;
jbowes@292
  1190
}
jbowes@292
  1191
rhughes@241
  1192
static struct {
rhughes@241
  1193
	const char *name;
rhughes@241
  1194
	const char *description;
rhughes@241
  1195
	int (*func)(int argc, const char *argv[]);
rhughes@241
  1196
} razor_commands[] = {
rhughes@241
  1197
	{ "list", "list all packages", command_list },
rhughes@241
  1198
	{ "list-requires", "list all requires for the given package", command_list_requires },
rhughes@241
  1199
	{ "list-provides", "list all provides for the given package", command_list_provides },
rhughes@241
  1200
	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
rhughes@241
  1201
	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
ali@369
  1202
	{ "list-scripts", "list all scripts for the given package", command_list_scripts },
rhughes@241
  1203
	{ "list-files", "list files for package set", command_list_files },
rhughes@241
  1204
	{ "list-file-packages", "list packages owning file", command_list_file_packages },
rhughes@241
  1205
	{ "list-package-files", "list files in package", command_list_package_files },
rhughes@241
  1206
	{ "what-requires", "list the packages that have the given requires", command_what_requires },
rhughes@241
  1207
	{ "what-provides", "list the packages that have the given provides", command_what_provides },
rhughes@241
  1208
	{ "import-yum", "import yum metadata files", command_import_yum },
ali@320
  1209
#if HAVE_RPMLIB
rhughes@241
  1210
	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
ali@320
  1211
#endif
rhughes@241
  1212
	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
rhughes@241
  1213
	{ "update", "update all or specified packages", command_update },
rhughes@241
  1214
	{ "remove", "remove specified packages", command_remove },
rhughes@241
  1215
	{ "diff", "show diff between two package sets", command_diff },
rhughes@241
  1216
	{ "install", "install rpm", command_install },
rhughes@241
  1217
	{ "init", "init razor root", command_init },
jbowes@258
  1218
	{ "download", "download packages", command_download },
jbowes@292
  1219
	{ "info", "display package details", command_info },
jbowes@292
  1220
	{ "search", "search package details", command_search }
rhughes@241
  1221
};
rhughes@241
  1222
rhughes@241
  1223
static int
rhughes@241
  1224
usage(void)
rhughes@241
  1225
{
rhughes@241
  1226
	int i;
rhughes@241
  1227
rhughes@241
  1228
	printf("usage:\n");
rhughes@241
  1229
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1230
		printf("  %-20s%s\n",
rhughes@241
  1231
		       razor_commands[i].name, razor_commands[i].description);
rhughes@241
  1232
rhughes@241
  1233
	return 1;
rhughes@241
  1234
}
rhughes@241
  1235
rhughes@241
  1236
int
rhughes@241
  1237
main(int argc, const char *argv[])
rhughes@241
  1238
{
krh@317
  1239
	char *repo, *root;
rhughes@241
  1240
	int i;
rhughes@241
  1241
rhughes@241
  1242
	repo = getenv("RAZOR_REPO");
rhughes@241
  1243
	if (repo != NULL)
rhughes@241
  1244
		repo_filename = repo;
rhughes@241
  1245
krh@317
  1246
	root = getenv("RAZOR_ROOT");
krh@317
  1247
	if (root != NULL)
krh@317
  1248
		install_root = root;
krh@317
  1249
rhughes@241
  1250
	yum_url = getenv("YUM_URL");
rhughes@241
  1251
	if (yum_url == NULL)
rhughes@241
  1252
		yum_url = YUM_URL;
rhughes@241
  1253
ali@359
  1254
	if (getenv("RAZOR_NO_ROOT_NAME_CHECKS"))
ali@359
  1255
		razor_disable_root_name_checks(1);
ali@359
  1256
rhughes@241
  1257
	if (argc < 2)
rhughes@241
  1258
		return usage();
rhughes@241
  1259
rhughes@241
  1260
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1261
		if (strcmp(razor_commands[i].name, argv[1]) == 0)
rhughes@241
  1262
			return razor_commands[i].func(argc - 2, argv + 2);
rhughes@241
  1263
rhughes@241
  1264
	return usage();
rhughes@241
  1265
}