src/main.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Aug 13 07:08:45 2009 +0100 (2009-08-13)
changeset 381 d35581ea0c67
parent 376 d15a16347c77
child 382 4e261a14a6bd
permissions -rw-r--r--
Fix a bug causing libtool versioning to be ignored.

This bug meant that razor 0.1 was recorded as interface 0 rather
than the intended interface 1. razor 0.2 will use interface 1.
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;
krh@373
   474
	if (razor_set_write(set, rawhide_repo_filename, RAZOR_SECTION_ALL)) {
ali@342
   475
		perror(rawhide_repo_filename);
ali@342
   476
		return -1;
ali@342
   477
	}
rhughes@241
   478
	razor_set_destroy(set);
rhughes@241
   479
	printf("wrote %s\n", rawhide_repo_filename);
rhughes@241
   480
rhughes@241
   481
	return 0;
rhughes@241
   482
}
rhughes@241
   483
ali@320
   484
#if HAVE_RPMLIB
rhughes@241
   485
static int
rhughes@241
   486
command_import_rpmdb(int argc, const char *argv[])
rhughes@241
   487
{
rhughes@241
   488
	struct razor_set *set;
krh@317
   489
	struct razor_root *root;
krh@317
   490
krh@317
   491
	root = razor_root_open(install_root);
krh@317
   492
	if (root == NULL)
krh@317
   493
		return 1;
rhughes@241
   494
rhughes@241
   495
	set = razor_set_create_from_rpmdb();
rhughes@241
   496
	if (set == NULL)
rhughes@241
   497
		return 1;
rhughes@241
   498
krh@317
   499
	razor_root_update(root, set);
krh@317
   500
krh@317
   501
	return razor_root_commit(root);
rhughes@241
   502
}
ali@320
   503
#endif
rhughes@241
   504
rhughes@241
   505
static int
rhughes@241
   506
mark_packages_for_update(struct razor_transaction *trans,
rhughes@241
   507
			 struct razor_set *set, const char *pattern)
rhughes@241
   508
{
rhughes@241
   509
	struct razor_package_iterator *pi;
rhughes@241
   510
	struct razor_package *package;
richard@302
   511
	const char *name;
rhughes@241
   512
	int matches = 0;
rhughes@241
   513
rhughes@241
   514
	pi = razor_package_iterator_create(set);
rhughes@241
   515
	while (razor_package_iterator_next(pi, &package,
richard@307
   516
					   RAZOR_DETAIL_NAME, &name,
richard@307
   517
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   518
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   519
			razor_transaction_update_package(trans, package);
rhughes@241
   520
			matches++;
rhughes@241
   521
		}
rhughes@241
   522
	}
rhughes@241
   523
	razor_package_iterator_destroy(pi);
rhughes@241
   524
rhughes@241
   525
	return matches;
rhughes@241
   526
}
rhughes@241
   527
rhughes@241
   528
static int
rhughes@241
   529
mark_packages_for_removal(struct razor_transaction *trans,
rhughes@241
   530
			  struct razor_set *set, const char *pattern)
rhughes@241
   531
{
rhughes@241
   532
	struct razor_package_iterator *pi;
rhughes@241
   533
	struct razor_package *package;
richard@302
   534
	const char *name;
rhughes@241
   535
	int matches = 0;
rhughes@241
   536
rhughes@241
   537
	pi = razor_package_iterator_create(set);
richard@307
   538
	while (razor_package_iterator_next(pi, &package,
richard@307
   539
					   RAZOR_DETAIL_NAME, &name,
richard@307
   540
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   541
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   542
			razor_transaction_remove_package(trans, package);
rhughes@241
   543
			matches++;
rhughes@241
   544
		}
rhughes@241
   545
	}
rhughes@241
   546
	razor_package_iterator_destroy(pi);
rhughes@241
   547
rhughes@241
   548
	return matches;
rhughes@241
   549
}
rhughes@241
   550
rhughes@241
   551
static int
rhughes@241
   552
command_update(int argc, const char *argv[])
rhughes@241
   553
{
rhughes@241
   554
	struct razor_set *set, *upstream;
rhughes@241
   555
	struct razor_transaction *trans;
rhughes@241
   556
	int i, errors;
rhughes@241
   557
krh@317
   558
	set = razor_root_open_read_only(install_root);
krh@317
   559
	if (set == NULL)
krh@315
   560
		return 1;
krh@315
   561
rhughes@241
   562
	upstream = razor_set_open(rawhide_repo_filename);
krh@373
   563
	if (upstream == NULL)
rhughes@241
   564
		return 1;
rhughes@241
   565
rhughes@241
   566
	trans = razor_transaction_create(set, upstream);
rhughes@241
   567
	if (argc == 0)
rhughes@241
   568
		razor_transaction_update_all(trans);
rhughes@241
   569
	for (i = 0; i < argc; i++) {
rhughes@241
   570
		if (mark_packages_for_update(trans, set, argv[i]) == 0) {
rhughes@241
   571
			fprintf(stderr, "no match for %s\n", argv[i]);
ali@369
   572
			razor_transaction_destroy(trans);
rhughes@241
   573
			return 1;
rhughes@241
   574
		}
rhughes@241
   575
	}
rhughes@241
   576
krh@252
   577
	razor_transaction_resolve(trans);
krh@252
   578
	errors = razor_transaction_describe(trans);
krh@252
   579
	if (errors) {
krh@252
   580
		fprintf(stderr, "unresolved dependencies\n");
ali@369
   581
		razor_transaction_destroy(trans);
rhughes@241
   582
		return 1;
krh@252
   583
	}
rhughes@241
   584
ali@369
   585
	set = razor_transaction_commit(trans);
krh@373
   586
	razor_set_write(set, updated_repo_filename, RAZOR_SECTION_ALL);
ali@369
   587
	razor_transaction_destroy(trans);
rhughes@241
   588
	razor_set_destroy(set);
rhughes@241
   589
	razor_set_destroy(upstream);
richard@310
   590
	printf("wrote system-updated.rzdb\n");
rhughes@241
   591
rhughes@241
   592
	return 0;
rhughes@241
   593
}
rhughes@241
   594
rhughes@241
   595
static int
rhughes@241
   596
command_remove(int argc, const char *argv[])
rhughes@241
   597
{
ali@363
   598
	struct razor_root *root;
ali@363
   599
	struct razor_set *system, *upstream, *next;
rhughes@241
   600
	struct razor_transaction *trans;
rhughes@241
   601
	int i, errors;
rhughes@241
   602
ali@363
   603
	root = razor_root_open(install_root);
ali@363
   604
	system = razor_root_get_system_set(root);
ali@363
   605
	if (system == NULL) {
ali@363
   606
		razor_root_close(root);
rhughes@241
   607
		return 1;
ali@363
   608
	}
rhughes@241
   609
ali@363
   610
	upstream = razor_set_create_without_root();
ali@363
   611
	trans = razor_transaction_create(system, upstream);
rhughes@241
   612
	for (i = 0; i < argc; i++) {
ali@363
   613
		if (mark_packages_for_removal(trans, system, argv[i]) == 0) {
rhughes@241
   614
			fprintf(stderr, "no match for %s\n", argv[i]);
ali@363
   615
			razor_transaction_destroy(trans);
ali@363
   616
			razor_set_destroy(upstream);
ali@363
   617
			razor_root_close(root);
rhughes@241
   618
			return 1;
rhughes@241
   619
		}
rhughes@241
   620
	}
rhughes@241
   621
ali@350
   622
	razor_transaction_resolve(trans);
ali@350
   623
	errors = razor_transaction_describe(trans);
ali@363
   624
	if (errors) {
ali@363
   625
		razor_transaction_destroy(trans);
ali@363
   626
		razor_set_destroy(upstream);
ali@363
   627
		razor_root_close(root);
rhughes@241
   628
		return 1;
ali@363
   629
	}
rhughes@241
   630
ali@369
   631
	next = razor_transaction_commit(trans);
ali@369
   632
	update_packages(trans, system, next, NULL);
ali@363
   633
	razor_root_update(root, next);
ali@363
   634
ali@369
   635
	razor_transaction_destroy(trans);
ali@363
   636
	razor_set_destroy(next);
rhughes@241
   637
	razor_set_destroy(upstream);
rhughes@241
   638
ali@363
   639
	return razor_root_commit(root);
rhughes@241
   640
}
rhughes@241
   641
rhughes@241
   642
static void
krh@253
   643
print_diff(enum razor_diff_action action,
krh@253
   644
	   struct razor_package *package,
krh@253
   645
	   const char *name,
krh@253
   646
	   const char *version,
krh@253
   647
	   const char *arch,
rhughes@241
   648
	   void *data)
rhughes@241
   649
{
krh@253
   650
	if (action == RAZOR_DIFF_ACTION_ADD)
krh@253
   651
		printf("install %s-%s.%s\n", name, version, arch);
krh@253
   652
	if (action == RAZOR_DIFF_ACTION_REMOVE)
krh@253
   653
		printf("remove %s-%s.%s\n", name, version, arch);
rhughes@241
   654
}
rhughes@241
   655
rhughes@241
   656
static int
rhughes@241
   657
command_diff(int argc, const char *argv[])
rhughes@241
   658
{
rhughes@241
   659
	struct razor_set *set, *updated;
rhughes@241
   660
krh@317
   661
	set = razor_root_open_read_only(install_root);
rhughes@241
   662
	updated = razor_set_open(updated_repo_filename);
rhughes@241
   663
	if (set == NULL || updated == NULL)
rhughes@241
   664
		return 1;
rhughes@241
   665
rhughes@241
   666
	razor_set_diff(set, updated, print_diff, NULL);
rhughes@241
   667
rhughes@241
   668
	razor_set_destroy(set);
rhughes@241
   669
	razor_set_destroy(updated);
rhughes@241
   670
rhughes@241
   671
	return 0;
rhughes@241
   672
}
rhughes@241
   673
rhughes@241
   674
static int
rhughes@241
   675
command_import_rpms(int argc, const char *argv[])
rhughes@241
   676
{
rhughes@241
   677
	DIR *dir;
rhughes@241
   678
	struct dirent *de;
rhughes@241
   679
	struct razor_importer *importer;
rhughes@241
   680
	struct razor_set *set;
rhughes@241
   681
	struct razor_rpm *rpm;
jbowes@263
   682
	int len, imported_count = 0;
rhughes@241
   683
	char filename[256];
rhughes@241
   684
	const char *dirname = argv[0];
rhughes@241
   685
rhughes@241
   686
	if (dirname == NULL) {
rhughes@241
   687
		fprintf(stderr, "usage: razor import-rpms DIR\n");
rhughes@241
   688
		return -1;
rhughes@241
   689
	}
rhughes@241
   690
rhughes@241
   691
	dir = opendir(dirname);
rhughes@241
   692
	if (dir == NULL) {
rhughes@241
   693
		fprintf(stderr, "couldn't read dir %s\n", dirname);
rhughes@241
   694
		return -1;
rhughes@241
   695
	}
rhughes@241
   696
krh@249
   697
	importer = razor_importer_create();
rhughes@241
   698
rhughes@241
   699
	while (de = readdir(dir), de != NULL) {
rhughes@241
   700
		len = strlen(de->d_name);
rhughes@241
   701
		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
rhughes@241
   702
		    continue;
rhughes@241
   703
		snprintf(filename, sizeof filename,
rhughes@241
   704
			 "%s/%s", dirname, de->d_name);
rhughes@241
   705
		rpm = razor_rpm_open(filename);
rhughes@241
   706
		if (rpm == NULL) {
rhughes@241
   707
			fprintf(stderr,
rhughes@241
   708
				"failed to open rpm \"%s\"\n", filename);
rhughes@241
   709
			continue;
rhughes@241
   710
		}
rhughes@241
   711
		if (razor_importer_add_rpm(importer, rpm)) {
rhughes@241
   712
			fprintf(stderr, "couldn't import %s\n", filename);
rhughes@241
   713
			break;
rhughes@241
   714
		}
rhughes@241
   715
		razor_rpm_close(rpm);
jbowes@263
   716
jbowes@263
   717
		printf("\rimporting %d", ++imported_count);
jbowes@263
   718
		fflush(stdout);
rhughes@241
   719
	}
rhughes@241
   720
rhughes@241
   721
	if (de != NULL) {
rhughes@241
   722
		razor_importer_destroy(importer);
rhughes@241
   723
		return -1;
rhughes@241
   724
	}
rhughes@241
   725
jbowes@263
   726
	printf("\nsaving\n");
rhughes@241
   727
	set = razor_importer_finish(importer);
rhughes@241
   728
krh@373
   729
	razor_set_write(set, repo_filename, RAZOR_SECTION_ALL);
rhughes@241
   730
	razor_set_destroy(set);
rhughes@241
   731
	printf("wrote %s\n", repo_filename);
rhughes@241
   732
rhughes@241
   733
	return 0;
rhughes@241
   734
}
rhughes@241
   735
krh@254
   736
static const char *
krh@254
   737
rpm_filename(const char *name, const char *version, const char *arch)
rhughes@241
   738
{
krh@254
   739
	static char file[PATH_MAX];
krh@254
   740
 	const char *v;
krh@254
   741
 
krh@254
   742
 	/* Skip epoch */
krh@253
   743
	v = strchr(version, ':');
krh@254
   744
 	if (v != NULL)
krh@254
   745
 		v = v + 1;
krh@254
   746
 	else
krh@253
   747
		v = version;
rhughes@241
   748
krh@254
   749
	snprintf(file, sizeof file, "%s-%s.%s.rpm", name, v, arch);
rhughes@241
   750
krh@254
   751
	return file;
rhughes@241
   752
}
rhughes@241
   753
krh@254
   754
static int
krh@254
   755
download_packages(struct razor_set *system, struct razor_set *next)
rhughes@241
   756
{
krh@316
   757
	struct razor_install_iterator *ii;
krh@254
   758
	struct razor_package *package;
krh@316
   759
	struct razor_set *set;
krh@316
   760
	enum razor_install_action action;
krh@254
   761
	const char *name, *version, *arch;
krh@254
   762
	char file[PATH_MAX], url[256];
krh@316
   763
	int errors = 0, count;
krh@316
   764
krh@316
   765
	ii = razor_set_create_install_iterator(system, next);
krh@316
   766
	while (razor_install_iterator_next(ii, &set, &package,
krh@316
   767
					   &action, &count)) {
krh@316
   768
		if (action == RAZOR_INSTALL_ACTION_REMOVE)
krh@316
   769
			continue;
krh@316
   770
krh@316
   771
		razor_package_get_details(set, package,
krh@316
   772
					  RAZOR_DETAIL_NAME, &name,
krh@316
   773
					  RAZOR_DETAIL_VERSION, &version,
krh@316
   774
					  RAZOR_DETAIL_ARCH, &arch,
krh@316
   775
					  RAZOR_DETAIL_LAST);
krh@316
   776
		
krh@254
   777
		snprintf(url, sizeof url,
krh@254
   778
			 "%s/Packages/%s",
krh@254
   779
			 yum_url, rpm_filename(name, version, arch));
krh@254
   780
		snprintf(file, sizeof file,
krh@254
   781
			 "rpms/%s", rpm_filename(name, version, arch));
krh@254
   782
		if (download_if_missing(url, file) < 0)
krh@254
   783
			errors++;
krh@254
   784
	}
krh@316
   785
	razor_install_iterator_destroy(ii);
krh@254
   786
krh@254
   787
	if (errors > 0) {
krh@254
   788
		fprintf(stderr, "failed to download %d packages\n", errors);
krh@254
   789
                return -1;
krh@254
   790
        }
krh@254
   791
krh@254
   792
	return 0;
krh@254
   793
}
krh@254
   794
ali@351
   795
static struct razor_set *
ali@351
   796
relocate_packages(struct razor_set *set, struct razor_relocations *relocations)
ali@351
   797
{
ali@372
   798
	int i;
ali@351
   799
	struct razor_importer *importer;
ali@351
   800
	struct razor_property_iterator *prop_iter;
ali@351
   801
	struct razor_package_iterator *pkg_iter;
ali@351
   802
 	struct razor_file_iterator *file_iter;
ali@351
   803
 	struct razor_package *package;
ali@351
   804
	struct razor_property *property;
ali@351
   805
	struct razor_rpm *rpm;
ali@351
   806
	const char *name, *version, *arch, *summary, *desc, *url, *license;
ali@369
   807
	const char *preunprog, *preun, *postunprog, *postun;
ali@372
   808
	const char *install_prefix;
ali@372
   809
	const char *const *prefixes;
ali@351
   810
	char file[PATH_MAX];
ali@351
   811
	uint32_t flags;
ali@351
   812
ali@351
   813
	importer = razor_importer_create();
ali@351
   814
	pkg_iter = razor_package_iterator_create(set);
ali@351
   815
ali@351
   816
	while (razor_package_iterator_next(pkg_iter, &package,
ali@351
   817
					   RAZOR_DETAIL_NAME, &name,
ali@351
   818
					   RAZOR_DETAIL_VERSION, &version,
ali@351
   819
					   RAZOR_DETAIL_ARCH, &arch,
ali@351
   820
					   RAZOR_DETAIL_SUMMARY, &summary,
ali@351
   821
					   RAZOR_DETAIL_DESCRIPTION, &desc,
ali@351
   822
					   RAZOR_DETAIL_URL, &url,
ali@351
   823
					   RAZOR_DETAIL_LICENSE, &license,
ali@369
   824
					   RAZOR_DETAIL_PREUNPROG, &preunprog,
ali@369
   825
					   RAZOR_DETAIL_PREUN, &preun,
ali@369
   826
					   RAZOR_DETAIL_POSTUNPROG, &postunprog,
ali@369
   827
					   RAZOR_DETAIL_POSTUN, &postun,
ali@351
   828
					   RAZOR_DETAIL_LAST)) {
ali@351
   829
		snprintf(file, sizeof file,
ali@351
   830
			 "rpms/%s", rpm_filename(name, version, arch));
ali@351
   831
		rpm = razor_rpm_open(file);
ali@351
   832
		if (rpm == NULL) {
ali@351
   833
			fprintf(stderr, "failed to open rpm %s\n", file);
ali@351
   834
			razor_package_iterator_destroy(pkg_iter);
ali@351
   835
			razor_importer_destroy(importer);
ali@351
   836
			return NULL;
ali@351
   837
		}
ali@351
   838
ali@351
   839
		razor_relocations_set_rpm(relocations, rpm);
ali@351
   840
ali@351
   841
		razor_importer_begin_package(importer, name, version, arch);
ali@351
   842
		razor_importer_add_details(importer,
ali@351
   843
					   summary, desc, url, license);
ali@351
   844
ali@372
   845
		razor_rpm_get_details(rpm, RAZOR_DETAIL_PREFIXES, &prefixes,
ali@372
   846
				      RAZOR_DETAIL_LAST);
ali@372
   847
		for (i = 0; prefixes && prefixes[i]; i++) {
ali@372
   848
			install_prefix = razor_relocations_apply(relocations,
ali@372
   849
								 prefixes[i]);
ali@372
   850
			razor_importer_add_install_prefix(importer,
ali@372
   851
							  install_prefix);
ali@372
   852
		}
ali@372
   853
ali@372
   854
		razor_rpm_close(rpm);
ali@372
   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@377
   863
		file_iter = razor_file_iterator_create(set, package, 0);
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@376
   910
	retval = razor_rpm_install(rpm, install_root, 1);
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@376
   935
						      install_root, 0);
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@373
   985
	if (upstream == NULL) {
krh@373
   986
		fprintf(stderr, "couldn't open rawhide repo\n");
krh@373
   987
		razor_root_close(root);
krh@373
   988
		return 1;
krh@315
   989
	}		
krh@315
   990
ali@351
   991
	if (relocations) {
ali@351
   992
		set = relocate_packages(upstream, relocations);
ali@351
   993
		razor_set_destroy(upstream);
ali@351
   994
		upstream = set;
ali@351
   995
	}
ali@351
   996
krh@250
   997
	trans = razor_transaction_create(system, upstream);
rhughes@241
   998
rhughes@241
   999
	for (; i < argc; i++) {
rhughes@241
  1000
		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
rhughes@241
  1001
			fprintf(stderr, "no package matched %s\n", argv[i]);
ali@369
  1002
			razor_transaction_destroy(trans);
rhughes@241
  1003
			razor_root_close(root);
rhughes@241
  1004
			return 1;
rhughes@241
  1005
		}
rhughes@241
  1006
	}
rhughes@241
  1007
rhughes@241
  1008
	if (dependencies) {
krh@245
  1009
		razor_transaction_resolve(trans);
krh@245
  1010
		if (razor_transaction_describe(trans) > 0) {
ali@369
  1011
			razor_transaction_destroy(trans);
rhughes@241
  1012
			razor_root_close(root);
rhughes@241
  1013
			return 1;
rhughes@241
  1014
		}
rhughes@241
  1015
	}
rhughes@241
  1016
ali@369
  1017
	next = razor_transaction_commit(trans);
rhughes@241
  1018
rhughes@241
  1019
	if (mkdir("rpms", 0777) && errno != EEXIST) {
rhughes@241
  1020
		fprintf(stderr, "failed to create rpms directory.\n");
ali@369
  1021
		razor_transaction_destroy(trans);
rhughes@241
  1022
		razor_root_close(root);
rhughes@241
  1023
		return 1;
rhughes@241
  1024
	}
rhughes@241
  1025
krh@254
  1026
	if (download_packages(system, next) < 0) {
ali@369
  1027
		razor_transaction_destroy(trans);
rhughes@241
  1028
		razor_root_close(root);
rhughes@241
  1029
                return 1;
rhughes@241
  1030
        }
rhughes@241
  1031
ali@369
  1032
	update_packages(trans, system, next, relocations);
rhughes@241
  1033
ali@369
  1034
	razor_root_update(root, next);
ali@369
  1035
ali@369
  1036
	razor_transaction_destroy(trans);
ali@351
  1037
	if (relocations)
ali@351
  1038
		razor_relocations_destroy(relocations);
rhughes@241
  1039
	razor_set_destroy(next);
rhughes@241
  1040
	razor_set_destroy(upstream);
rhughes@241
  1041
rhughes@241
  1042
	return razor_root_commit(root);
rhughes@241
  1043
}
rhughes@241
  1044
rhughes@241
  1045
static int
rhughes@241
  1046
command_init(int argc, const char *argv[])
rhughes@241
  1047
{
rhughes@241
  1048
	return razor_root_create(install_root);
rhughes@241
  1049
}
rhughes@241
  1050
rhughes@241
  1051
static int
rhughes@241
  1052
command_download(int argc, const char *argv[])
rhughes@241
  1053
{
rhughes@241
  1054
	struct razor_set *set;
rhughes@241
  1055
	struct razor_package_iterator *pi;
rhughes@241
  1056
	struct razor_package *package;
rhughes@241
  1057
	const char *pattern = argv[0], *name, *version, *arch;
rhughes@241
  1058
	char url[256], file[256];
rhughes@241
  1059
	int matches = 0;
rhughes@241
  1060
rhughes@241
  1061
	if (mkdir("rpms", 0777) && errno != EEXIST) {
rhughes@241
  1062
		fprintf(stderr, "failed to create rpms directory.\n");
rhughes@241
  1063
		return 1;
rhughes@241
  1064
	}
rhughes@241
  1065
rhughes@241
  1066
	set = razor_set_open(rawhide_repo_filename);
rhughes@241
  1067
	pi = razor_package_iterator_create(set);
rhughes@241
  1068
	while (razor_package_iterator_next(pi, &package,
richard@302
  1069
					   RAZOR_DETAIL_NAME, &name,
richard@302
  1070
					   RAZOR_DETAIL_VERSION, &version,
richard@307
  1071
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
  1072
					   RAZOR_DETAIL_LAST)) {
rhughes@241
  1073
		if (pattern && fnmatch(pattern, name, 0) != 0)
rhughes@241
  1074
			continue;
rhughes@241
  1075
rhughes@241
  1076
		matches++;
rhughes@241
  1077
		snprintf(url, sizeof url,
rhughes@241
  1078
			 "%s/Packages/%s-%s.%s.rpm",
rhughes@241
  1079
			 yum_url, name, version, arch);
rhughes@241
  1080
		snprintf(file, sizeof file,
rhughes@241
  1081
			 "rpms/%s-%s.%s.rpm", name, version, arch);
rhughes@241
  1082
		download_if_missing(url, file);
rhughes@241
  1083
	}
rhughes@241
  1084
	razor_package_iterator_destroy(pi);
rhughes@241
  1085
	razor_set_destroy(set);
rhughes@241
  1086
rhughes@241
  1087
	if (matches == 0)
rhughes@241
  1088
		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
rhughes@241
  1089
	else if (matches == 1)
rhughes@241
  1090
		fprintf(stderr, "downloaded 1 package\n");
rhughes@241
  1091
	else
rhughes@241
  1092
		fprintf(stderr, "downloaded %d packages\n", matches);
rhughes@241
  1093
rhughes@241
  1094
	return 0;
rhughes@241
  1095
}
rhughes@241
  1096
jbowes@258
  1097
static int
jbowes@258
  1098
command_info(int argc, const char *argv[])
jbowes@258
  1099
{
jbowes@258
  1100
	struct razor_set *set;
jbowes@258
  1101
	struct razor_package_iterator *pi;
jbowes@258
  1102
	struct razor_package *package;
jbowes@258
  1103
	const char *pattern = argv[0], *name, *version, *arch;
jbowes@258
  1104
	const char *summary, *description, *url, *license;
jbowes@258
  1105
krh@317
  1106
	set = razor_root_open_read_only(install_root);
jbowes@288
  1107
	if (set == NULL)
jbowes@288
  1108
		return 1;
krh@317
  1109
jbowes@258
  1110
	pi = razor_package_iterator_create(set);
jbowes@258
  1111
	while (razor_package_iterator_next(pi, &package,
richard@302
  1112
					   RAZOR_DETAIL_NAME, &name,
richard@302
  1113
					   RAZOR_DETAIL_VERSION, &version,
richard@307
  1114
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
  1115
					   RAZOR_DETAIL_LAST)) {
jbowes@258
  1116
		if (pattern && fnmatch(pattern, name, 0) != 0)
jbowes@258
  1117
			continue;
jbowes@258
  1118
richard@302
  1119
		razor_package_get_details (set, package,
richard@302
  1120
					   RAZOR_DETAIL_SUMMARY, &summary,
richard@302
  1121
					   RAZOR_DETAIL_DESCRIPTION, &description,
richard@302
  1122
					   RAZOR_DETAIL_URL, &url,
richard@302
  1123
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
  1124
					   RAZOR_DETAIL_LAST);
jbowes@258
  1125
jbowes@258
  1126
		printf ("Name:        %s\n", name);
jbowes@258
  1127
		printf ("Arch:        %s\n", arch);
jbowes@258
  1128
		printf ("Version:     %s\n", version);
jbowes@258
  1129
		printf ("URL:         %s\n", url);
jbowes@258
  1130
		printf ("License:     %s\n", license);
jbowes@258
  1131
		printf ("Summary:     %s\n", summary);
jbowes@258
  1132
		printf ("Description:\n");
jbowes@258
  1133
		printf ("%s\n", description);
jbowes@258
  1134
		printf ("\n");
jbowes@258
  1135
	}
jbowes@258
  1136
	razor_package_iterator_destroy(pi);
jbowes@258
  1137
	razor_set_destroy(set);
jbowes@258
  1138
jbowes@258
  1139
	return 0;
jbowes@258
  1140
}
jbowes@258
  1141
jbowes@292
  1142
#define SEARCH_MAX 256
jbowes@292
  1143
jbowes@292
  1144
static int
jbowes@292
  1145
command_search(int argc, const char *argv[])
jbowes@292
  1146
{
jbowes@292
  1147
	struct razor_set *set;
jbowes@292
  1148
	struct razor_package_iterator *pi;
jbowes@292
  1149
	struct razor_package *package;
jbowes@292
  1150
	char pattern[SEARCH_MAX];
jbowes@292
  1151
	const char *name, *version, *arch;
jbowes@292
  1152
	const char *summary, *description, *url, *license;
jbowes@292
  1153
jbowes@292
  1154
	if (!argv[0]) {
jbowes@292
  1155
		fprintf(stderr, "must specify a search term\n");
jbowes@292
  1156
		return 1;
jbowes@292
  1157
	}
jbowes@292
  1158
krh@294
  1159
	snprintf(pattern, sizeof pattern, "*%s*", argv[0]);
jbowes@292
  1160
krh@305
  1161
	set = razor_set_open(rawhide_repo_filename);
jbowes@292
  1162
	if (set == NULL)
jbowes@292
  1163
		return 1;
richard@310
  1164
jbowes@292
  1165
	pi = razor_package_iterator_create(set);
jbowes@292
  1166
	while (razor_package_iterator_next(pi, &package,
richard@304
  1167
					   RAZOR_DETAIL_NAME, &name,
richard@304
  1168
					   RAZOR_DETAIL_VERSION, &version,
krh@305
  1169
					   RAZOR_DETAIL_ARCH, &arch,
krh@305
  1170
					   RAZOR_DETAIL_SUMMARY, &summary,
krh@305
  1171
					   RAZOR_DETAIL_DESCRIPTION, &description,
krh@305
  1172
					   RAZOR_DETAIL_URL, &url,
krh@305
  1173
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
  1174
					   RAZOR_DETAIL_LAST)) {
krh@305
  1175
		if (!fnmatch(pattern, name, FNM_CASEFOLD) ||
krh@305
  1176
		    !fnmatch(pattern, url, FNM_CASEFOLD) ||
krh@305
  1177
		    !fnmatch(pattern, summary, FNM_CASEFOLD) ||
krh@305
  1178
		    !fnmatch(pattern, description, FNM_CASEFOLD))
krh@305
  1179
			printf("%s-%s.%s: %s\n", name, version, arch, summary);
jbowes@292
  1180
	}
jbowes@292
  1181
	razor_package_iterator_destroy(pi);
jbowes@292
  1182
	razor_set_destroy(set);
jbowes@292
  1183
jbowes@292
  1184
	return 0;
jbowes@292
  1185
}
jbowes@292
  1186
rhughes@241
  1187
static struct {
rhughes@241
  1188
	const char *name;
rhughes@241
  1189
	const char *description;
rhughes@241
  1190
	int (*func)(int argc, const char *argv[]);
rhughes@241
  1191
} razor_commands[] = {
rhughes@241
  1192
	{ "list", "list all packages", command_list },
rhughes@241
  1193
	{ "list-requires", "list all requires for the given package", command_list_requires },
rhughes@241
  1194
	{ "list-provides", "list all provides for the given package", command_list_provides },
rhughes@241
  1195
	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
rhughes@241
  1196
	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
ali@369
  1197
	{ "list-scripts", "list all scripts for the given package", command_list_scripts },
rhughes@241
  1198
	{ "list-files", "list files for package set", command_list_files },
rhughes@241
  1199
	{ "list-file-packages", "list packages owning file", command_list_file_packages },
rhughes@241
  1200
	{ "list-package-files", "list files in package", command_list_package_files },
rhughes@241
  1201
	{ "what-requires", "list the packages that have the given requires", command_what_requires },
rhughes@241
  1202
	{ "what-provides", "list the packages that have the given provides", command_what_provides },
rhughes@241
  1203
	{ "import-yum", "import yum metadata files", command_import_yum },
ali@320
  1204
#if HAVE_RPMLIB
rhughes@241
  1205
	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
ali@320
  1206
#endif
rhughes@241
  1207
	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
rhughes@241
  1208
	{ "update", "update all or specified packages", command_update },
rhughes@241
  1209
	{ "remove", "remove specified packages", command_remove },
rhughes@241
  1210
	{ "diff", "show diff between two package sets", command_diff },
rhughes@241
  1211
	{ "install", "install rpm", command_install },
rhughes@241
  1212
	{ "init", "init razor root", command_init },
jbowes@258
  1213
	{ "download", "download packages", command_download },
jbowes@292
  1214
	{ "info", "display package details", command_info },
jbowes@292
  1215
	{ "search", "search package details", command_search }
rhughes@241
  1216
};
rhughes@241
  1217
rhughes@241
  1218
static int
rhughes@241
  1219
usage(void)
rhughes@241
  1220
{
rhughes@241
  1221
	int i;
rhughes@241
  1222
rhughes@241
  1223
	printf("usage:\n");
rhughes@241
  1224
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1225
		printf("  %-20s%s\n",
rhughes@241
  1226
		       razor_commands[i].name, razor_commands[i].description);
rhughes@241
  1227
rhughes@241
  1228
	return 1;
rhughes@241
  1229
}
rhughes@241
  1230
rhughes@241
  1231
int
rhughes@241
  1232
main(int argc, const char *argv[])
rhughes@241
  1233
{
krh@317
  1234
	char *repo, *root;
rhughes@241
  1235
	int i;
rhughes@241
  1236
rhughes@241
  1237
	repo = getenv("RAZOR_REPO");
rhughes@241
  1238
	if (repo != NULL)
rhughes@241
  1239
		repo_filename = repo;
rhughes@241
  1240
krh@317
  1241
	root = getenv("RAZOR_ROOT");
krh@317
  1242
	if (root != NULL)
krh@317
  1243
		install_root = root;
krh@317
  1244
rhughes@241
  1245
	yum_url = getenv("YUM_URL");
rhughes@241
  1246
	if (yum_url == NULL)
rhughes@241
  1247
		yum_url = YUM_URL;
rhughes@241
  1248
ali@359
  1249
	if (getenv("RAZOR_NO_ROOT_NAME_CHECKS"))
ali@359
  1250
		razor_disable_root_name_checks(1);
ali@359
  1251
rhughes@241
  1252
	if (argc < 2)
rhughes@241
  1253
		return usage();
rhughes@241
  1254
rhughes@241
  1255
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1256
		if (strcmp(razor_commands[i].name, argv[1]) == 0)
rhughes@241
  1257
			return razor_commands[i].func(argc - 2, argv + 2);
rhughes@241
  1258
rhughes@241
  1259
	return usage();
rhughes@241
  1260
}