src/main.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu May 14 05:55:19 2009 +0100 (2009-05-14)
changeset 367 e45f50e940b6
parent 359 c9c90315ea24
child 369 f8c27fe9fe63
permissions -rw-r--r--
Order the installation and removal of packages correctly so scripts can run
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@363
    53
update_packages(struct razor_set *system, struct razor_set *next,
ali@363
    54
		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
rhughes@241
   225
command_list_files(int argc, const char *argv[])
rhughes@241
   226
{
rhughes@241
   227
	struct razor_set *set;
rhughes@241
   228
krh@317
   229
	set = razor_root_open_read_only(install_root);
rhughes@241
   230
	if (set == NULL)
rhughes@241
   231
		return 1;
krh@317
   232
rhughes@241
   233
	razor_set_list_files(set, argv[0]);
rhughes@241
   234
	razor_set_destroy(set);
rhughes@241
   235
rhughes@241
   236
	return 0;
rhughes@241
   237
}
rhughes@241
   238
rhughes@241
   239
static int
rhughes@241
   240
command_list_file_packages(int argc, const char *argv[])
rhughes@241
   241
{
rhughes@241
   242
	struct razor_set *set;
rhughes@241
   243
	struct razor_package_iterator *pi;
rhughes@241
   244
krh@317
   245
	set = razor_root_open_read_only(install_root);
rhughes@241
   246
	if (set == NULL)
rhughes@241
   247
		return 1;
rhughes@241
   248
rhughes@241
   249
	pi = razor_package_iterator_create_for_file(set, argv[0]);
krh@281
   250
	list_packages(pi, 0);
rhughes@241
   251
	razor_package_iterator_destroy(pi);
rhughes@241
   252
rhughes@241
   253
	razor_set_destroy(set);
rhughes@241
   254
rhughes@241
   255
	return 0;
rhughes@241
   256
}
rhughes@241
   257
rhughes@241
   258
static int
rhughes@241
   259
command_list_package_files(int argc, const char *argv[])
rhughes@241
   260
{
rhughes@241
   261
	struct razor_set *set;
krh@306
   262
	struct razor_package_iterator *pi;
krh@306
   263
	struct razor_package *package;
krh@306
   264
	const char *name, *version, *arch;
rhughes@241
   265
krh@317
   266
	set = razor_root_open_read_only(install_root);
rhughes@241
   267
	if (set == NULL)
rhughes@241
   268
		return 1;
jbowes@288
   269
krh@306
   270
	pi = create_iterator_from_argv(set, argc, argv);
krh@306
   271
	while (razor_package_iterator_next(pi, &package,
richard@307
   272
					   RAZOR_DETAIL_NAME, &name,
richard@307
   273
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   274
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   275
					   RAZOR_DETAIL_LAST))
krh@306
   276
		razor_set_list_package_files(set, package);
krh@306
   277
	razor_package_iterator_destroy(pi);
krh@306
   278
rhughes@241
   279
	razor_set_destroy(set);
rhughes@241
   280
rhughes@241
   281
	return 0;
rhughes@241
   282
}
rhughes@241
   283
rhughes@241
   284
static int
rhughes@241
   285
list_property_packages(const char *ref_name,
rhughes@241
   286
		       const char *ref_version,
krh@247
   287
		       uint32_t type)
rhughes@241
   288
{
rhughes@241
   289
	struct razor_set *set;
rhughes@241
   290
	struct razor_property *property;
krh@281
   291
	struct razor_property_iterator *prop_iter;
krh@281
   292
	struct razor_package_iterator *pkg_iter;
rhughes@241
   293
	const char *name, *version;
krh@247
   294
	uint32_t flags;
rhughes@241
   295
rhughes@241
   296
	if (ref_name == NULL)
rhughes@241
   297
		return 0;
rhughes@241
   298
krh@317
   299
	set = razor_root_open_read_only(install_root);
rhughes@241
   300
	if (set == NULL)
rhughes@241
   301
		return 1;
rhughes@241
   302
krh@281
   303
	prop_iter = razor_property_iterator_create(set, NULL);
krh@281
   304
	while (razor_property_iterator_next(prop_iter, &property,
krh@247
   305
					    &name, &flags, &version)) {
rhughes@241
   306
		if (strcmp(ref_name, name) != 0)
rhughes@241
   307
			continue;
krh@247
   308
		if (ref_version &&
krh@247
   309
		    (flags & RAZOR_PROPERTY_RELATION_MASK) == RAZOR_PROPERTY_EQUAL &&
rhughes@241
   310
		    strcmp(ref_version, version) != 0)
rhughes@241
   311
			continue;
krh@247
   312
		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
rhughes@241
   313
			continue;
rhughes@241
   314
krh@281
   315
		pkg_iter =
krh@281
   316
			razor_package_iterator_create_for_property(set,
krh@281
   317
								   property);
krh@281
   318
		list_packages(pkg_iter, 0);
krh@281
   319
		razor_package_iterator_destroy(pkg_iter);
rhughes@241
   320
	}
krh@281
   321
	razor_property_iterator_destroy(prop_iter);
rhughes@241
   322
krh@317
   323
	razor_set_destroy(set);
krh@317
   324
rhughes@241
   325
	return 0;
rhughes@241
   326
}
rhughes@241
   327
rhughes@241
   328
static int
rhughes@241
   329
command_what_requires(int argc, const char *argv[])
rhughes@241
   330
{
rhughes@241
   331
	return list_property_packages(argv[0], argv[1],
rhughes@241
   332
				      RAZOR_PROPERTY_REQUIRES);
rhughes@241
   333
}
rhughes@241
   334
rhughes@241
   335
static int
rhughes@241
   336
command_what_provides(int argc, const char *argv[])
rhughes@241
   337
{
rhughes@241
   338
	return list_property_packages(argv[0], argv[1],
rhughes@241
   339
				      RAZOR_PROPERTY_PROVIDES);
rhughes@241
   340
}
rhughes@241
   341
rhughes@241
   342
static int
rhughes@241
   343
show_progress(void *clientp,
rhughes@241
   344
	      double dltotal, double dlnow, double ultotal, double ulnow)
rhughes@241
   345
{
rhughes@241
   346
	const char *file = clientp;
rhughes@241
   347
rhughes@241
   348
	if (!dlnow < dltotal)
rhughes@241
   349
		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
rhughes@241
   350
			file, (int) dlnow / 1024, (int) dltotal / 1024);
rhughes@241
   351
rhughes@241
   352
	return 0;
rhughes@241
   353
}
rhughes@241
   354
rhughes@241
   355
static int
rhughes@241
   356
download_if_missing(const char *url, const char *file)
rhughes@241
   357
{
ali@321
   358
#ifndef HAVE_CURL
ali@321
   359
	return 1;
ali@321
   360
#else
rhughes@241
   361
	CURL *curl;
rhughes@241
   362
	struct stat buf;
rhughes@241
   363
	char error[256];
rhughes@241
   364
	FILE *fp;
rhughes@241
   365
	CURLcode res;
rhughes@241
   366
	long response;
rhughes@241
   367
rhughes@241
   368
	curl = curl_easy_init();
rhughes@241
   369
	if (curl == NULL)
rhughes@241
   370
		return 1;
rhughes@241
   371
rhughes@241
   372
	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
rhughes@241
   373
	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
rhughes@241
   374
	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
rhughes@241
   375
	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
rhughes@241
   376
rhughes@241
   377
	if (stat(file, &buf) < 0) {
ali@345
   378
		fp = fopen(file, "wb");
rhughes@241
   379
		if (fp == NULL) {
rhughes@241
   380
			fprintf(stderr,
rhughes@241
   381
				"failed to open %s for writing\n", file);
rhughes@241
   382
			return -1;
rhughes@241
   383
		}
rhughes@241
   384
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
rhughes@241
   385
		curl_easy_setopt(curl, CURLOPT_URL, url);
rhughes@241
   386
		res = curl_easy_perform(curl);
rhughes@241
   387
		fclose(fp);
rhughes@241
   388
		if (res != CURLE_OK) {
rhughes@241
   389
			fprintf(stderr, "curl error: %s\n", error);
rhughes@241
   390
			unlink(file);
rhughes@241
   391
			return -1;
rhughes@241
   392
		}
rhughes@241
   393
		res = curl_easy_getinfo(curl,
rhughes@241
   394
					CURLINFO_RESPONSE_CODE, &response);
rhughes@241
   395
		if (res != CURLE_OK) {
rhughes@241
   396
			fprintf(stderr, "curl error: %s\n", error);
rhughes@241
   397
                        unlink(file);
rhughes@241
   398
                        return -1;
rhughes@241
   399
		}
rhughes@241
   400
		if (response != 200) {
rhughes@241
   401
			fprintf(stderr, " - failed %ld\n", response);
rhughes@241
   402
                        unlink(file);
rhughes@241
   403
                        return -1;
rhughes@241
   404
		}
rhughes@241
   405
		fprintf(stderr, "\n");
rhughes@241
   406
	}
rhughes@241
   407
rhughes@241
   408
	curl_easy_cleanup(curl);
rhughes@241
   409
rhughes@241
   410
	return 0;
ali@321
   411
#endif	/* HAVE_CURL */
rhughes@241
   412
}
rhughes@241
   413
rhughes@241
   414
#define YUM_URL "http://download.fedora.redhat.com" \
rhughes@241
   415
	"/pub/fedora/linux/development/i386/os"
rhughes@241
   416
rhughes@241
   417
static int
rhughes@241
   418
command_import_yum(int argc, const char *argv[])
rhughes@241
   419
{
rhughes@241
   420
	struct razor_set *set;
rhughes@241
   421
	char buffer[512];
rhughes@241
   422
rhughes@241
   423
	printf("downloading from %s.\n", yum_url);
rhughes@241
   424
	snprintf(buffer, sizeof buffer,
rhughes@241
   425
		 "%s/repodata/primary.xml.gz", yum_url);
rhughes@241
   426
	if (download_if_missing(buffer, "primary.xml.gz") < 0)
rhughes@241
   427
		return -1;
rhughes@241
   428
	snprintf(buffer, sizeof buffer,
rhughes@241
   429
		 "%s/repodata/filelists.xml.gz", yum_url);
rhughes@241
   430
	if (download_if_missing(buffer, "filelists.xml.gz") < 0)
rhughes@241
   431
		return -1;
rhughes@241
   432
rhughes@241
   433
	set = razor_set_create_from_yum();
rhughes@241
   434
	if (set == NULL)
rhughes@241
   435
		return 1;
ali@342
   436
	if (razor_set_write(set, rawhide_repo_filename, RAZOR_REPO_FILE_MAIN)) {
ali@342
   437
		perror(rawhide_repo_filename);
ali@342
   438
		return -1;
ali@342
   439
	}
ali@342
   440
	if (razor_set_write(set, "rawhide-details.rzdb",
ali@342
   441
	    RAZOR_REPO_FILE_DETAILS)) {
ali@342
   442
		perror("rawhide-details.rzdb");
ali@342
   443
		return -1;
ali@342
   444
	}
ali@342
   445
	if (razor_set_write(set, "rawhide-files.rzdb", RAZOR_REPO_FILE_FILES)) {
ali@342
   446
		perror("rawhide-files.rzdb");
ali@342
   447
		return -1;
ali@342
   448
	}
rhughes@241
   449
	razor_set_destroy(set);
rhughes@241
   450
	printf("wrote %s\n", rawhide_repo_filename);
rhughes@241
   451
rhughes@241
   452
	return 0;
rhughes@241
   453
}
rhughes@241
   454
ali@320
   455
#if HAVE_RPMLIB
rhughes@241
   456
static int
rhughes@241
   457
command_import_rpmdb(int argc, const char *argv[])
rhughes@241
   458
{
rhughes@241
   459
	struct razor_set *set;
krh@317
   460
	struct razor_root *root;
krh@317
   461
krh@317
   462
	root = razor_root_open(install_root);
krh@317
   463
	if (root == NULL)
krh@317
   464
		return 1;
rhughes@241
   465
rhughes@241
   466
	set = razor_set_create_from_rpmdb();
rhughes@241
   467
	if (set == NULL)
rhughes@241
   468
		return 1;
rhughes@241
   469
krh@317
   470
	razor_root_update(root, set);
krh@317
   471
krh@317
   472
	return razor_root_commit(root);
rhughes@241
   473
}
ali@320
   474
#endif
rhughes@241
   475
rhughes@241
   476
static int
rhughes@241
   477
mark_packages_for_update(struct razor_transaction *trans,
rhughes@241
   478
			 struct razor_set *set, const char *pattern)
rhughes@241
   479
{
rhughes@241
   480
	struct razor_package_iterator *pi;
rhughes@241
   481
	struct razor_package *package;
richard@302
   482
	const char *name;
rhughes@241
   483
	int matches = 0;
rhughes@241
   484
rhughes@241
   485
	pi = razor_package_iterator_create(set);
rhughes@241
   486
	while (razor_package_iterator_next(pi, &package,
richard@307
   487
					   RAZOR_DETAIL_NAME, &name,
richard@307
   488
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   489
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   490
			razor_transaction_update_package(trans, package);
rhughes@241
   491
			matches++;
rhughes@241
   492
		}
rhughes@241
   493
	}
rhughes@241
   494
	razor_package_iterator_destroy(pi);
rhughes@241
   495
rhughes@241
   496
	return matches;
rhughes@241
   497
}
rhughes@241
   498
rhughes@241
   499
static int
rhughes@241
   500
mark_packages_for_removal(struct razor_transaction *trans,
rhughes@241
   501
			  struct razor_set *set, const char *pattern)
rhughes@241
   502
{
rhughes@241
   503
	struct razor_package_iterator *pi;
rhughes@241
   504
	struct razor_package *package;
richard@302
   505
	const char *name;
rhughes@241
   506
	int matches = 0;
rhughes@241
   507
rhughes@241
   508
	pi = razor_package_iterator_create(set);
richard@307
   509
	while (razor_package_iterator_next(pi, &package,
richard@307
   510
					   RAZOR_DETAIL_NAME, &name,
richard@307
   511
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   512
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   513
			razor_transaction_remove_package(trans, package);
rhughes@241
   514
			matches++;
rhughes@241
   515
		}
rhughes@241
   516
	}
rhughes@241
   517
	razor_package_iterator_destroy(pi);
rhughes@241
   518
rhughes@241
   519
	return matches;
rhughes@241
   520
}
rhughes@241
   521
rhughes@241
   522
static int
rhughes@241
   523
command_update(int argc, const char *argv[])
rhughes@241
   524
{
rhughes@241
   525
	struct razor_set *set, *upstream;
rhughes@241
   526
	struct razor_transaction *trans;
rhughes@241
   527
	int i, errors;
rhughes@241
   528
krh@317
   529
	set = razor_root_open_read_only(install_root);
krh@317
   530
	if (set == NULL)
krh@315
   531
		return 1;
krh@315
   532
rhughes@241
   533
	upstream = razor_set_open(rawhide_repo_filename);
krh@315
   534
	if (upstream == NULL ||
krh@315
   535
	    razor_set_open_details(upstream, "rawhide-details.rzdb") ||
krh@315
   536
	    razor_set_open_files(upstream, "rawhide-files.rzdb"))
rhughes@241
   537
		return 1;
rhughes@241
   538
rhughes@241
   539
	trans = razor_transaction_create(set, upstream);
rhughes@241
   540
	if (argc == 0)
rhughes@241
   541
		razor_transaction_update_all(trans);
rhughes@241
   542
	for (i = 0; i < argc; i++) {
rhughes@241
   543
		if (mark_packages_for_update(trans, set, argv[i]) == 0) {
rhughes@241
   544
			fprintf(stderr, "no match for %s\n", argv[i]);
rhughes@241
   545
			return 1;
rhughes@241
   546
		}
rhughes@241
   547
	}
rhughes@241
   548
krh@252
   549
	razor_transaction_resolve(trans);
krh@252
   550
	errors = razor_transaction_describe(trans);
krh@252
   551
	if (errors) {
krh@252
   552
		fprintf(stderr, "unresolved dependencies\n");
rhughes@241
   553
		return 1;
krh@252
   554
	}
rhughes@241
   555
rhughes@241
   556
	set = razor_transaction_finish(trans);
jbowes@258
   557
	razor_set_write(set, updated_repo_filename, RAZOR_REPO_FILE_MAIN);
rhughes@241
   558
	razor_set_destroy(set);
rhughes@241
   559
	razor_set_destroy(upstream);
richard@310
   560
	printf("wrote system-updated.rzdb\n");
rhughes@241
   561
rhughes@241
   562
	return 0;
rhughes@241
   563
}
rhughes@241
   564
rhughes@241
   565
static int
rhughes@241
   566
command_remove(int argc, const char *argv[])
rhughes@241
   567
{
ali@363
   568
	struct razor_root *root;
ali@363
   569
	struct razor_set *system, *upstream, *next;
rhughes@241
   570
	struct razor_transaction *trans;
rhughes@241
   571
	int i, errors;
rhughes@241
   572
ali@363
   573
	root = razor_root_open(install_root);
ali@363
   574
	system = razor_root_get_system_set(root);
ali@363
   575
	if (system == NULL) {
ali@363
   576
		razor_root_close(root);
rhughes@241
   577
		return 1;
ali@363
   578
	}
rhughes@241
   579
ali@363
   580
	upstream = razor_set_create_without_root();
ali@363
   581
	trans = razor_transaction_create(system, upstream);
rhughes@241
   582
	for (i = 0; i < argc; i++) {
ali@363
   583
		if (mark_packages_for_removal(trans, system, argv[i]) == 0) {
rhughes@241
   584
			fprintf(stderr, "no match for %s\n", argv[i]);
ali@363
   585
			razor_transaction_destroy(trans);
ali@363
   586
			razor_set_destroy(upstream);
ali@363
   587
			razor_root_close(root);
rhughes@241
   588
			return 1;
rhughes@241
   589
		}
rhughes@241
   590
	}
rhughes@241
   591
ali@350
   592
	razor_transaction_resolve(trans);
ali@350
   593
	errors = razor_transaction_describe(trans);
ali@363
   594
	if (errors) {
ali@363
   595
		razor_transaction_destroy(trans);
ali@363
   596
		razor_set_destroy(upstream);
ali@363
   597
		razor_root_close(root);
rhughes@241
   598
		return 1;
ali@363
   599
	}
rhughes@241
   600
ali@363
   601
	next = razor_transaction_finish(trans);
ali@363
   602
	update_packages(system, next, NULL);
ali@363
   603
	razor_root_update(root, next);
ali@363
   604
ali@363
   605
	razor_set_destroy(next);
rhughes@241
   606
	razor_set_destroy(upstream);
rhughes@241
   607
ali@363
   608
	return razor_root_commit(root);
rhughes@241
   609
}
rhughes@241
   610
rhughes@241
   611
static void
krh@253
   612
print_diff(enum razor_diff_action action,
krh@253
   613
	   struct razor_package *package,
krh@253
   614
	   const char *name,
krh@253
   615
	   const char *version,
krh@253
   616
	   const char *arch,
rhughes@241
   617
	   void *data)
rhughes@241
   618
{
krh@253
   619
	if (action == RAZOR_DIFF_ACTION_ADD)
krh@253
   620
		printf("install %s-%s.%s\n", name, version, arch);
krh@253
   621
	if (action == RAZOR_DIFF_ACTION_REMOVE)
krh@253
   622
		printf("remove %s-%s.%s\n", name, version, arch);
rhughes@241
   623
}
rhughes@241
   624
rhughes@241
   625
static int
rhughes@241
   626
command_diff(int argc, const char *argv[])
rhughes@241
   627
{
rhughes@241
   628
	struct razor_set *set, *updated;
rhughes@241
   629
krh@317
   630
	set = razor_root_open_read_only(install_root);
rhughes@241
   631
	updated = razor_set_open(updated_repo_filename);
rhughes@241
   632
	if (set == NULL || updated == NULL)
rhughes@241
   633
		return 1;
rhughes@241
   634
rhughes@241
   635
	razor_set_diff(set, updated, print_diff, NULL);
rhughes@241
   636
rhughes@241
   637
	razor_set_destroy(set);
rhughes@241
   638
	razor_set_destroy(updated);
rhughes@241
   639
rhughes@241
   640
	return 0;
rhughes@241
   641
}
rhughes@241
   642
rhughes@241
   643
static int
rhughes@241
   644
command_import_rpms(int argc, const char *argv[])
rhughes@241
   645
{
rhughes@241
   646
	DIR *dir;
rhughes@241
   647
	struct dirent *de;
rhughes@241
   648
	struct razor_importer *importer;
rhughes@241
   649
	struct razor_set *set;
rhughes@241
   650
	struct razor_rpm *rpm;
jbowes@263
   651
	int len, imported_count = 0;
rhughes@241
   652
	char filename[256];
rhughes@241
   653
	const char *dirname = argv[0];
rhughes@241
   654
rhughes@241
   655
	if (dirname == NULL) {
rhughes@241
   656
		fprintf(stderr, "usage: razor import-rpms DIR\n");
rhughes@241
   657
		return -1;
rhughes@241
   658
	}
rhughes@241
   659
rhughes@241
   660
	dir = opendir(dirname);
rhughes@241
   661
	if (dir == NULL) {
rhughes@241
   662
		fprintf(stderr, "couldn't read dir %s\n", dirname);
rhughes@241
   663
		return -1;
rhughes@241
   664
	}
rhughes@241
   665
krh@249
   666
	importer = razor_importer_create();
rhughes@241
   667
rhughes@241
   668
	while (de = readdir(dir), de != NULL) {
rhughes@241
   669
		len = strlen(de->d_name);
rhughes@241
   670
		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
rhughes@241
   671
		    continue;
rhughes@241
   672
		snprintf(filename, sizeof filename,
rhughes@241
   673
			 "%s/%s", dirname, de->d_name);
rhughes@241
   674
		rpm = razor_rpm_open(filename);
rhughes@241
   675
		if (rpm == NULL) {
rhughes@241
   676
			fprintf(stderr,
rhughes@241
   677
				"failed to open rpm \"%s\"\n", filename);
rhughes@241
   678
			continue;
rhughes@241
   679
		}
rhughes@241
   680
		if (razor_importer_add_rpm(importer, rpm)) {
rhughes@241
   681
			fprintf(stderr, "couldn't import %s\n", filename);
rhughes@241
   682
			break;
rhughes@241
   683
		}
rhughes@241
   684
		razor_rpm_close(rpm);
jbowes@263
   685
jbowes@263
   686
		printf("\rimporting %d", ++imported_count);
jbowes@263
   687
		fflush(stdout);
rhughes@241
   688
	}
rhughes@241
   689
rhughes@241
   690
	if (de != NULL) {
rhughes@241
   691
		razor_importer_destroy(importer);
rhughes@241
   692
		return -1;
rhughes@241
   693
	}
rhughes@241
   694
jbowes@263
   695
	printf("\nsaving\n");
rhughes@241
   696
	set = razor_importer_finish(importer);
rhughes@241
   697
jbowes@258
   698
	razor_set_write(set, repo_filename, RAZOR_REPO_FILE_MAIN);
richard@310
   699
	razor_set_write(set, "system-details.rzdb", RAZOR_REPO_FILE_DETAILS);
richard@310
   700
	razor_set_write(set, "system-files.rzdb", RAZOR_REPO_FILE_FILES);
rhughes@241
   701
	razor_set_destroy(set);
rhughes@241
   702
	printf("wrote %s\n", repo_filename);
rhughes@241
   703
rhughes@241
   704
	return 0;
rhughes@241
   705
}
rhughes@241
   706
krh@254
   707
static const char *
krh@254
   708
rpm_filename(const char *name, const char *version, const char *arch)
rhughes@241
   709
{
krh@254
   710
	static char file[PATH_MAX];
krh@254
   711
 	const char *v;
krh@254
   712
 
krh@254
   713
 	/* Skip epoch */
krh@253
   714
	v = strchr(version, ':');
krh@254
   715
 	if (v != NULL)
krh@254
   716
 		v = v + 1;
krh@254
   717
 	else
krh@253
   718
		v = version;
rhughes@241
   719
krh@254
   720
	snprintf(file, sizeof file, "%s-%s.%s.rpm", name, v, arch);
rhughes@241
   721
krh@254
   722
	return file;
rhughes@241
   723
}
rhughes@241
   724
krh@254
   725
static int
krh@254
   726
download_packages(struct razor_set *system, struct razor_set *next)
rhughes@241
   727
{
krh@316
   728
	struct razor_install_iterator *ii;
krh@254
   729
	struct razor_package *package;
krh@316
   730
	struct razor_set *set;
krh@316
   731
	enum razor_install_action action;
krh@254
   732
	const char *name, *version, *arch;
krh@254
   733
	char file[PATH_MAX], url[256];
krh@316
   734
	int errors = 0, count;
krh@316
   735
krh@316
   736
	ii = razor_set_create_install_iterator(system, next);
krh@316
   737
	while (razor_install_iterator_next(ii, &set, &package,
krh@316
   738
					   &action, &count)) {
krh@316
   739
		if (action == RAZOR_INSTALL_ACTION_REMOVE)
krh@316
   740
			continue;
krh@316
   741
krh@316
   742
		razor_package_get_details(set, package,
krh@316
   743
					  RAZOR_DETAIL_NAME, &name,
krh@316
   744
					  RAZOR_DETAIL_VERSION, &version,
krh@316
   745
					  RAZOR_DETAIL_ARCH, &arch,
krh@316
   746
					  RAZOR_DETAIL_LAST);
krh@316
   747
		
krh@254
   748
		snprintf(url, sizeof url,
krh@254
   749
			 "%s/Packages/%s",
krh@254
   750
			 yum_url, rpm_filename(name, version, arch));
krh@254
   751
		snprintf(file, sizeof file,
krh@254
   752
			 "rpms/%s", rpm_filename(name, version, arch));
krh@254
   753
		if (download_if_missing(url, file) < 0)
krh@254
   754
			errors++;
krh@254
   755
	}
krh@316
   756
	razor_install_iterator_destroy(ii);
krh@254
   757
krh@254
   758
	if (errors > 0) {
krh@254
   759
		fprintf(stderr, "failed to download %d packages\n", errors);
krh@254
   760
                return -1;
krh@254
   761
        }
krh@254
   762
krh@254
   763
	return 0;
krh@254
   764
}
krh@254
   765
ali@351
   766
static struct razor_set *
ali@351
   767
relocate_packages(struct razor_set *set, struct razor_relocations *relocations)
ali@351
   768
{
ali@351
   769
	struct razor_importer *importer;
ali@351
   770
	struct razor_property_iterator *prop_iter;
ali@351
   771
	struct razor_package_iterator *pkg_iter;
ali@351
   772
 	struct razor_file_iterator *file_iter;
ali@351
   773
 	struct razor_package *package;
ali@351
   774
	struct razor_property *property;
ali@351
   775
	struct razor_rpm *rpm;
ali@351
   776
	const char *name, *version, *arch, *summary, *desc, *url, *license;
ali@351
   777
	char file[PATH_MAX];
ali@351
   778
	uint32_t flags;
ali@351
   779
ali@351
   780
	importer = razor_importer_create();
ali@351
   781
	pkg_iter = razor_package_iterator_create(set);
ali@351
   782
ali@351
   783
	while (razor_package_iterator_next(pkg_iter, &package,
ali@351
   784
					   RAZOR_DETAIL_NAME, &name,
ali@351
   785
					   RAZOR_DETAIL_VERSION, &version,
ali@351
   786
					   RAZOR_DETAIL_ARCH, &arch,
ali@351
   787
					   RAZOR_DETAIL_SUMMARY, &summary,
ali@351
   788
					   RAZOR_DETAIL_DESCRIPTION, &desc,
ali@351
   789
					   RAZOR_DETAIL_URL, &url,
ali@351
   790
					   RAZOR_DETAIL_LICENSE, &license,
ali@351
   791
					   RAZOR_DETAIL_LAST)) {
ali@351
   792
		snprintf(file, sizeof file,
ali@351
   793
			 "rpms/%s", rpm_filename(name, version, arch));
ali@351
   794
		rpm = razor_rpm_open(file);
ali@351
   795
		if (rpm == NULL) {
ali@351
   796
			fprintf(stderr, "failed to open rpm %s\n", file);
ali@351
   797
			razor_package_iterator_destroy(pkg_iter);
ali@351
   798
			razor_importer_destroy(importer);
ali@351
   799
			return NULL;
ali@351
   800
		}
ali@351
   801
ali@351
   802
		razor_relocations_set_rpm(relocations, rpm);
ali@351
   803
		razor_rpm_close(rpm);
ali@351
   804
ali@351
   805
		razor_importer_begin_package(importer, name, version, arch);
ali@351
   806
		razor_importer_add_details(importer,
ali@351
   807
					   summary, desc, url, license);
ali@351
   808
ali@351
   809
		prop_iter = razor_property_iterator_create(set, package);
ali@351
   810
		while (razor_property_iterator_next(prop_iter, &property,
ali@351
   811
						    &name, &flags, &version))
ali@351
   812
			razor_importer_add_property(importer,
ali@351
   813
						    name, flags, version);
ali@351
   814
		razor_property_iterator_destroy(prop_iter);
ali@351
   815
ali@351
   816
		file_iter = razor_file_iterator_create(set, package);
ali@351
   817
		while (razor_file_iterator_next(file_iter, &name)) {
ali@351
   818
			name = razor_relocations_apply(relocations, name);
ali@351
   819
			razor_importer_add_file(importer, name);
ali@351
   820
		}
ali@351
   821
		razor_file_iterator_destroy(file_iter);
ali@351
   822
ali@351
   823
		razor_importer_finish_package(importer);
ali@351
   824
	}
ali@351
   825
ali@351
   826
	razor_package_iterator_destroy(pkg_iter);
ali@351
   827
	return razor_importer_finish(importer);
ali@351
   828
}
ali@351
   829
krh@254
   830
static int
ali@363
   831
install_package(struct razor_set *set, struct razor_package *package,
ali@363
   832
		struct razor_relocations *relocations)
ali@363
   833
{
ali@363
   834
	int retval;
ali@363
   835
	const char *name, *version, *arch;
ali@363
   836
	char file[PATH_MAX];
ali@363
   837
	struct razor_rpm *rpm;
ali@363
   838
ali@363
   839
	razor_package_get_details(set, package,
ali@363
   840
				  RAZOR_DETAIL_NAME, &name,
ali@363
   841
				  RAZOR_DETAIL_VERSION, &version,
ali@363
   842
				  RAZOR_DETAIL_ARCH, &arch,
ali@363
   843
				  RAZOR_DETAIL_LAST);
ali@363
   844
ali@363
   845
	printf("install %s-%s\n", name, version);
ali@363
   846
ali@363
   847
	snprintf(file, sizeof file,
ali@363
   848
		 "rpms/%s", rpm_filename(name, version, arch));
ali@363
   849
	rpm = razor_rpm_open(file);
ali@363
   850
	if (rpm == NULL) {
ali@363
   851
		fprintf(stderr, "failed to open rpm %s\n", file);
ali@363
   852
		return -1;
ali@363
   853
	}
ali@363
   854
	if (relocations)
ali@363
   855
		razor_rpm_set_relocations(rpm, relocations);
ali@363
   856
	retval = razor_rpm_install(rpm, install_root);
ali@363
   857
	if (retval < 0)
ali@363
   858
		fprintf(stderr, "failed to install rpm %s\n", file);
ali@363
   859
	razor_rpm_close(rpm);
ali@363
   860
	return retval;
ali@363
   861
}
ali@363
   862
ali@363
   863
static int
ali@363
   864
remove_package(struct razor_set *set, struct razor_package *package)
ali@363
   865
{
ali@363
   866
	struct razor_file_iterator *fi;
ali@363
   867
	struct razor_package_iterator *pi;
ali@363
   868
	struct razor_package *p;
ali@363
   869
	char buffer[PATH_MAX];
ali@363
   870
	const char *name;
ali@363
   871
	int retval = 0, count;
ali@363
   872
ali@363
   873
	fi = razor_file_iterator_create(set, package);
ali@363
   874
ali@363
   875
	while (!retval && razor_file_iterator_next(fi, &name)) {
ali@363
   876
		pi = razor_package_iterator_create_for_file(set, name);
ali@363
   877
		count = 0;
ali@363
   878
		while (razor_package_iterator_next(pi, &p, RAZOR_DETAIL_LAST))
ali@363
   879
			count++;
ali@363
   880
		razor_package_iterator_destroy(pi);
ali@363
   881
		if (count <= 1) {
ali@363
   882
			snprintf(buffer, sizeof buffer, "%s%s", install_root,
ali@363
   883
				 name);
ali@363
   884
			retval = remove(buffer);
ali@363
   885
		}
ali@363
   886
	}
ali@363
   887
ali@363
   888
	razor_file_iterator_destroy(fi);
ali@363
   889
	return retval;
ali@363
   890
}
ali@363
   891
ali@363
   892
static int
ali@363
   893
update_packages(struct razor_set *system, struct razor_set *next,
ali@363
   894
		struct razor_relocations *relocations)
krh@254
   895
{
krh@316
   896
	struct razor_install_iterator *ii;
krh@254
   897
	struct razor_package *package;
krh@316
   898
	struct razor_set *set;
krh@316
   899
	enum razor_install_action action;
ali@363
   900
	int retval = 0, count;
rhughes@241
   901
krh@316
   902
	ii = razor_set_create_install_iterator(system, next);
ali@363
   903
	while (!retval && razor_install_iterator_next(ii, &set, &package,
ali@363
   904
						      &action, &count)) {
ali@363
   905
		if (action == RAZOR_INSTALL_ACTION_ADD)
ali@363
   906
			retval = install_package(set, package, relocations);
ali@363
   907
		else if (action == RAZOR_INSTALL_ACTION_REMOVE)
ali@363
   908
			retval = remove_package(set, package);
rhughes@241
   909
	}
krh@316
   910
	razor_install_iterator_destroy(ii);
rhughes@241
   911
ali@363
   912
	return retval;
rhughes@241
   913
}
rhughes@241
   914
rhughes@241
   915
static int
rhughes@241
   916
command_install(int argc, const char *argv[])
rhughes@241
   917
{
rhughes@241
   918
	struct razor_root *root;
ali@351
   919
	struct razor_relocations *relocations=NULL;
ali@351
   920
	struct razor_set *system, *upstream, *next, *set;
rhughes@241
   921
	struct razor_transaction *trans;
ali@351
   922
	int i, len, dependencies = 1;
ali@351
   923
	char *oldpath;
rhughes@241
   924
krh@250
   925
	root = razor_root_open(install_root);
krh@255
   926
	if (root == NULL)
krh@255
   927
		return 1;
krh@255
   928
ali@351
   929
	for (i = 0; i < argc; i++) {
ali@351
   930
		if (strcmp(argv[i], "--no-dependencies") == 0)
ali@351
   931
			dependencies = 0;
ali@351
   932
		else if (strcmp(argv[i], "--relocate") == 0) {
ali@351
   933
			i++;
ali@351
   934
			if (i >= argc || strchr(argv[i], '=') == NULL) {
ali@351
   935
				fprintf(stderr,
ali@351
   936
				    "Usage: razor install [OPTION...] RPM\n");
ali@351
   937
				fprintf(stderr, "Options:\n");
ali@351
   938
				fprintf(stderr, "    [--no-dependencies]\n");
ali@351
   939
				fprintf(stderr,
ali@351
   940
				    "    [--relocate OLDPATH=NEWPATH] RPM\n");
ali@351
   941
				return -1;
ali@351
   942
			}
ali@351
   943
			len = strchr(argv[i], '=') - argv[i];
ali@351
   944
			oldpath = malloc(len + 1);
ali@351
   945
			strncpy(oldpath, argv[i], len);
ali@351
   946
			oldpath[len] = '\0';
ali@351
   947
			if (!relocations)
ali@351
   948
			       relocations = razor_relocations_create();
ali@351
   949
			razor_relocations_add(relocations, oldpath,
ali@351
   950
					      argv[i] + len + 1);
ali@351
   951
			free(oldpath);
ali@351
   952
		} else
ali@351
   953
			break;
ali@351
   954
	}
ali@351
   955
krh@250
   956
	system = razor_root_get_system_set(root);
rhughes@241
   957
	upstream = razor_set_open(rawhide_repo_filename);
krh@315
   958
	if (upstream == NULL ||
krh@315
   959
	    razor_set_open_details(upstream, "rawhide-details.rzdb") ||
krh@315
   960
	    razor_set_open_files(upstream, "rawhide-files.rzdb")) {
krh@315
   961
			fprintf(stderr, "couldn't open rawhide repo\n");
krh@315
   962
			razor_root_close(root);
krh@315
   963
			return 1;
krh@315
   964
	}		
krh@315
   965
ali@351
   966
	if (relocations) {
ali@351
   967
		set = relocate_packages(upstream, relocations);
ali@351
   968
		razor_set_destroy(upstream);
ali@351
   969
		upstream = set;
ali@351
   970
	}
ali@351
   971
krh@250
   972
	trans = razor_transaction_create(system, upstream);
rhughes@241
   973
rhughes@241
   974
	for (; i < argc; i++) {
rhughes@241
   975
		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
rhughes@241
   976
			fprintf(stderr, "no package matched %s\n", argv[i]);
rhughes@241
   977
			razor_root_close(root);
rhughes@241
   978
			return 1;
rhughes@241
   979
		}
rhughes@241
   980
	}
rhughes@241
   981
rhughes@241
   982
	if (dependencies) {
krh@245
   983
		razor_transaction_resolve(trans);
krh@245
   984
		if (razor_transaction_describe(trans) > 0) {
rhughes@241
   985
			razor_root_close(root);
rhughes@241
   986
			return 1;
rhughes@241
   987
		}
rhughes@241
   988
	}
rhughes@241
   989
rhughes@241
   990
	next = razor_transaction_finish(trans);
rhughes@241
   991
rhughes@241
   992
	razor_root_update(root, next);
rhughes@241
   993
rhughes@241
   994
	if (mkdir("rpms", 0777) && errno != EEXIST) {
rhughes@241
   995
		fprintf(stderr, "failed to create rpms directory.\n");
rhughes@241
   996
		razor_root_close(root);
rhughes@241
   997
		return 1;
rhughes@241
   998
	}
rhughes@241
   999
krh@254
  1000
	if (download_packages(system, next) < 0) {
rhughes@241
  1001
		razor_root_close(root);
rhughes@241
  1002
                return 1;
rhughes@241
  1003
        }
rhughes@241
  1004
ali@363
  1005
	update_packages(system, next, relocations);
rhughes@241
  1006
ali@351
  1007
	if (relocations)
ali@351
  1008
		razor_relocations_destroy(relocations);
rhughes@241
  1009
	razor_set_destroy(next);
rhughes@241
  1010
	razor_set_destroy(upstream);
rhughes@241
  1011
rhughes@241
  1012
	return razor_root_commit(root);
rhughes@241
  1013
}
rhughes@241
  1014
rhughes@241
  1015
static int
rhughes@241
  1016
command_init(int argc, const char *argv[])
rhughes@241
  1017
{
rhughes@241
  1018
	return razor_root_create(install_root);
rhughes@241
  1019
}
rhughes@241
  1020
rhughes@241
  1021
static int
rhughes@241
  1022
command_download(int argc, const char *argv[])
rhughes@241
  1023
{
rhughes@241
  1024
	struct razor_set *set;
rhughes@241
  1025
	struct razor_package_iterator *pi;
rhughes@241
  1026
	struct razor_package *package;
rhughes@241
  1027
	const char *pattern = argv[0], *name, *version, *arch;
rhughes@241
  1028
	char url[256], file[256];
rhughes@241
  1029
	int matches = 0;
rhughes@241
  1030
rhughes@241
  1031
	if (mkdir("rpms", 0777) && errno != EEXIST) {
rhughes@241
  1032
		fprintf(stderr, "failed to create rpms directory.\n");
rhughes@241
  1033
		return 1;
rhughes@241
  1034
	}
rhughes@241
  1035
rhughes@241
  1036
	set = razor_set_open(rawhide_repo_filename);
rhughes@241
  1037
	pi = razor_package_iterator_create(set);
rhughes@241
  1038
	while (razor_package_iterator_next(pi, &package,
richard@302
  1039
					   RAZOR_DETAIL_NAME, &name,
richard@302
  1040
					   RAZOR_DETAIL_VERSION, &version,
richard@307
  1041
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
  1042
					   RAZOR_DETAIL_LAST)) {
rhughes@241
  1043
		if (pattern && fnmatch(pattern, name, 0) != 0)
rhughes@241
  1044
			continue;
rhughes@241
  1045
rhughes@241
  1046
		matches++;
rhughes@241
  1047
		snprintf(url, sizeof url,
rhughes@241
  1048
			 "%s/Packages/%s-%s.%s.rpm",
rhughes@241
  1049
			 yum_url, name, version, arch);
rhughes@241
  1050
		snprintf(file, sizeof file,
rhughes@241
  1051
			 "rpms/%s-%s.%s.rpm", name, version, arch);
rhughes@241
  1052
		download_if_missing(url, file);
rhughes@241
  1053
	}
rhughes@241
  1054
	razor_package_iterator_destroy(pi);
rhughes@241
  1055
	razor_set_destroy(set);
rhughes@241
  1056
rhughes@241
  1057
	if (matches == 0)
rhughes@241
  1058
		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
rhughes@241
  1059
	else if (matches == 1)
rhughes@241
  1060
		fprintf(stderr, "downloaded 1 package\n");
rhughes@241
  1061
	else
rhughes@241
  1062
		fprintf(stderr, "downloaded %d packages\n", matches);
rhughes@241
  1063
rhughes@241
  1064
	return 0;
rhughes@241
  1065
}
rhughes@241
  1066
jbowes@258
  1067
static int
jbowes@258
  1068
command_info(int argc, const char *argv[])
jbowes@258
  1069
{
jbowes@258
  1070
	struct razor_set *set;
jbowes@258
  1071
	struct razor_package_iterator *pi;
jbowes@258
  1072
	struct razor_package *package;
jbowes@258
  1073
	const char *pattern = argv[0], *name, *version, *arch;
jbowes@258
  1074
	const char *summary, *description, *url, *license;
jbowes@258
  1075
krh@317
  1076
	set = razor_root_open_read_only(install_root);
jbowes@288
  1077
	if (set == NULL)
jbowes@288
  1078
		return 1;
krh@317
  1079
jbowes@258
  1080
	pi = razor_package_iterator_create(set);
jbowes@258
  1081
	while (razor_package_iterator_next(pi, &package,
richard@302
  1082
					   RAZOR_DETAIL_NAME, &name,
richard@302
  1083
					   RAZOR_DETAIL_VERSION, &version,
richard@307
  1084
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
  1085
					   RAZOR_DETAIL_LAST)) {
jbowes@258
  1086
		if (pattern && fnmatch(pattern, name, 0) != 0)
jbowes@258
  1087
			continue;
jbowes@258
  1088
richard@302
  1089
		razor_package_get_details (set, package,
richard@302
  1090
					   RAZOR_DETAIL_SUMMARY, &summary,
richard@302
  1091
					   RAZOR_DETAIL_DESCRIPTION, &description,
richard@302
  1092
					   RAZOR_DETAIL_URL, &url,
richard@302
  1093
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
  1094
					   RAZOR_DETAIL_LAST);
jbowes@258
  1095
jbowes@258
  1096
		printf ("Name:        %s\n", name);
jbowes@258
  1097
		printf ("Arch:        %s\n", arch);
jbowes@258
  1098
		printf ("Version:     %s\n", version);
jbowes@258
  1099
		printf ("URL:         %s\n", url);
jbowes@258
  1100
		printf ("License:     %s\n", license);
jbowes@258
  1101
		printf ("Summary:     %s\n", summary);
jbowes@258
  1102
		printf ("Description:\n");
jbowes@258
  1103
		printf ("%s\n", description);
jbowes@258
  1104
		printf ("\n");
jbowes@258
  1105
	}
jbowes@258
  1106
	razor_package_iterator_destroy(pi);
jbowes@258
  1107
	razor_set_destroy(set);
jbowes@258
  1108
jbowes@258
  1109
	return 0;
jbowes@258
  1110
}
jbowes@258
  1111
jbowes@292
  1112
#define SEARCH_MAX 256
jbowes@292
  1113
jbowes@292
  1114
static int
jbowes@292
  1115
command_search(int argc, const char *argv[])
jbowes@292
  1116
{
jbowes@292
  1117
	struct razor_set *set;
jbowes@292
  1118
	struct razor_package_iterator *pi;
jbowes@292
  1119
	struct razor_package *package;
jbowes@292
  1120
	char pattern[SEARCH_MAX];
jbowes@292
  1121
	const char *name, *version, *arch;
jbowes@292
  1122
	const char *summary, *description, *url, *license;
jbowes@292
  1123
jbowes@292
  1124
	if (!argv[0]) {
jbowes@292
  1125
		fprintf(stderr, "must specify a search term\n");
jbowes@292
  1126
		return 1;
jbowes@292
  1127
	}
jbowes@292
  1128
krh@294
  1129
	snprintf(pattern, sizeof pattern, "*%s*", argv[0]);
jbowes@292
  1130
krh@305
  1131
	set = razor_set_open(rawhide_repo_filename);
jbowes@292
  1132
	if (set == NULL)
jbowes@292
  1133
		return 1;
richard@310
  1134
richard@310
  1135
	if (razor_set_open_details(set, "rawhide-details.rzdb"))
jbowes@292
  1136
		return 1;
jbowes@292
  1137
jbowes@292
  1138
	pi = razor_package_iterator_create(set);
jbowes@292
  1139
	while (razor_package_iterator_next(pi, &package,
richard@304
  1140
					   RAZOR_DETAIL_NAME, &name,
richard@304
  1141
					   RAZOR_DETAIL_VERSION, &version,
krh@305
  1142
					   RAZOR_DETAIL_ARCH, &arch,
krh@305
  1143
					   RAZOR_DETAIL_SUMMARY, &summary,
krh@305
  1144
					   RAZOR_DETAIL_DESCRIPTION, &description,
krh@305
  1145
					   RAZOR_DETAIL_URL, &url,
krh@305
  1146
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
  1147
					   RAZOR_DETAIL_LAST)) {
krh@305
  1148
		if (!fnmatch(pattern, name, FNM_CASEFOLD) ||
krh@305
  1149
		    !fnmatch(pattern, url, FNM_CASEFOLD) ||
krh@305
  1150
		    !fnmatch(pattern, summary, FNM_CASEFOLD) ||
krh@305
  1151
		    !fnmatch(pattern, description, FNM_CASEFOLD))
krh@305
  1152
			printf("%s-%s.%s: %s\n", name, version, arch, summary);
jbowes@292
  1153
	}
jbowes@292
  1154
	razor_package_iterator_destroy(pi);
jbowes@292
  1155
	razor_set_destroy(set);
jbowes@292
  1156
jbowes@292
  1157
	return 0;
jbowes@292
  1158
}
jbowes@292
  1159
rhughes@241
  1160
static struct {
rhughes@241
  1161
	const char *name;
rhughes@241
  1162
	const char *description;
rhughes@241
  1163
	int (*func)(int argc, const char *argv[]);
rhughes@241
  1164
} razor_commands[] = {
rhughes@241
  1165
	{ "list", "list all packages", command_list },
rhughes@241
  1166
	{ "list-requires", "list all requires for the given package", command_list_requires },
rhughes@241
  1167
	{ "list-provides", "list all provides for the given package", command_list_provides },
rhughes@241
  1168
	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
rhughes@241
  1169
	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
rhughes@241
  1170
	{ "list-files", "list files for package set", command_list_files },
rhughes@241
  1171
	{ "list-file-packages", "list packages owning file", command_list_file_packages },
rhughes@241
  1172
	{ "list-package-files", "list files in package", command_list_package_files },
rhughes@241
  1173
	{ "what-requires", "list the packages that have the given requires", command_what_requires },
rhughes@241
  1174
	{ "what-provides", "list the packages that have the given provides", command_what_provides },
rhughes@241
  1175
	{ "import-yum", "import yum metadata files", command_import_yum },
ali@320
  1176
#if HAVE_RPMLIB
rhughes@241
  1177
	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
ali@320
  1178
#endif
rhughes@241
  1179
	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
rhughes@241
  1180
	{ "update", "update all or specified packages", command_update },
rhughes@241
  1181
	{ "remove", "remove specified packages", command_remove },
rhughes@241
  1182
	{ "diff", "show diff between two package sets", command_diff },
rhughes@241
  1183
	{ "install", "install rpm", command_install },
rhughes@241
  1184
	{ "init", "init razor root", command_init },
jbowes@258
  1185
	{ "download", "download packages", command_download },
jbowes@292
  1186
	{ "info", "display package details", command_info },
jbowes@292
  1187
	{ "search", "search package details", command_search }
rhughes@241
  1188
};
rhughes@241
  1189
rhughes@241
  1190
static int
rhughes@241
  1191
usage(void)
rhughes@241
  1192
{
rhughes@241
  1193
	int i;
rhughes@241
  1194
rhughes@241
  1195
	printf("usage:\n");
rhughes@241
  1196
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1197
		printf("  %-20s%s\n",
rhughes@241
  1198
		       razor_commands[i].name, razor_commands[i].description);
rhughes@241
  1199
rhughes@241
  1200
	return 1;
rhughes@241
  1201
}
rhughes@241
  1202
rhughes@241
  1203
int
rhughes@241
  1204
main(int argc, const char *argv[])
rhughes@241
  1205
{
krh@317
  1206
	char *repo, *root;
rhughes@241
  1207
	int i;
rhughes@241
  1208
rhughes@241
  1209
	repo = getenv("RAZOR_REPO");
rhughes@241
  1210
	if (repo != NULL)
rhughes@241
  1211
		repo_filename = repo;
rhughes@241
  1212
krh@317
  1213
	root = getenv("RAZOR_ROOT");
krh@317
  1214
	if (root != NULL)
krh@317
  1215
		install_root = root;
krh@317
  1216
rhughes@241
  1217
	yum_url = getenv("YUM_URL");
rhughes@241
  1218
	if (yum_url == NULL)
rhughes@241
  1219
		yum_url = YUM_URL;
rhughes@241
  1220
ali@359
  1221
	if (getenv("RAZOR_NO_ROOT_NAME_CHECKS"))
ali@359
  1222
		razor_disable_root_name_checks(1);
ali@359
  1223
rhughes@241
  1224
	if (argc < 2)
rhughes@241
  1225
		return usage();
rhughes@241
  1226
rhughes@241
  1227
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1228
		if (strcmp(razor_commands[i].name, argv[1]) == 0)
rhughes@241
  1229
			return razor_commands[i].func(argc - 2, argv + 2);
rhughes@241
  1230
rhughes@241
  1231
	return usage();
rhughes@241
  1232
}