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