src/main.c
author Kristian H?gsberg <krh@redhat.com>
Wed Jun 25 11:24:55 2008 -0400 (2008-06-25)
changeset 281 d8d248598628
parent 280 25407a621c95
child 288 75da311eda45
permissions -rw-r--r--
Factor out package selection and listing in main.c.
rhughes@241
     1
/*
rhughes@241
     2
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
rhughes@241
     3
 * Copyright (C) 2008  Red Hat, Inc
rhughes@241
     4
 *
rhughes@241
     5
 * This program is free software; you can redistribute it and/or modify
rhughes@241
     6
 * it under the terms of the GNU General Public License as published by
rhughes@241
     7
 * the Free Software Foundation; either version 2 of the License, or
rhughes@241
     8
 * (at your option) any later version.
rhughes@241
     9
 *
rhughes@241
    10
 * This program is distributed in the hope that it will be useful,
rhughes@241
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rhughes@241
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
rhughes@241
    13
 * GNU General Public License for more details.
rhughes@241
    14
 *
rhughes@241
    15
 * You should have received a copy of the GNU General Public License along
rhughes@241
    16
 * with this program; if not, write to the Free Software Foundation, Inc.,
rhughes@241
    17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
rhughes@241
    18
 */
rhughes@241
    19
rhughes@241
    20
#include <stdlib.h>
rhughes@241
    21
#include <stddef.h>
rhughes@241
    22
#include <stdio.h>
rhughes@241
    23
#include <stdint.h>
rhughes@241
    24
#include <string.h>
rhughes@241
    25
#include <sys/stat.h>
rhughes@241
    26
#include <unistd.h>
rhughes@241
    27
#include <fcntl.h>
rhughes@241
    28
#include <dirent.h>
rhughes@241
    29
#include <curl/curl.h>
rhughes@241
    30
#include <fnmatch.h>
rhughes@241
    31
#include <errno.h>
rhughes@241
    32
#include "razor.h"
rhughes@241
    33
rhughes@241
    34
static const char system_repo_filename[] = "system.repo";
rhughes@241
    35
static const char next_repo_filename[] = "system-next.repo";
rhughes@241
    36
static const char rawhide_repo_filename[] = "rawhide.repo";
rhughes@241
    37
static const char updated_repo_filename[] = "system-updated.repo";
rhughes@241
    38
static const char install_root[] = "install";
rhughes@241
    39
static const char *repo_filename = system_repo_filename;
rhughes@241
    40
static const char *yum_url;
rhughes@241
    41
krh@271
    42
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
krh@271
    43
krh@281
    44
static struct razor_package_iterator *
krh@281
    45
create_iterator_from_argv(struct razor_set *set, int argc, const char *argv[])
rhughes@241
    46
{
krh@281
    47
	struct razor_package_query *query;
krh@281
    48
	struct razor_package_iterator *iter;
rhughes@241
    49
	struct razor_package *package;
krh@281
    50
	const char *name, *version, *arch, *pattern;
krh@281
    51
	int i, count;
rhughes@241
    52
krh@281
    53
	if (argc == 0)
krh@281
    54
		return razor_package_iterator_create(set);
krh@281
    55
krh@281
    56
	query = razor_package_query_create(set);
krh@281
    57
krh@281
    58
	for (i = 0; i < argc; i++) {
krh@281
    59
		iter = razor_package_iterator_create(set);
krh@281
    60
		pattern = argv[i];
krh@281
    61
		count = 0;
krh@281
    62
		while (razor_package_iterator_next(iter, &package,
krh@281
    63
						   &name, &version, &arch)) {
krh@281
    64
			if (fnmatch(pattern, name, 0) != 0)
krh@281
    65
				continue;
krh@281
    66
krh@281
    67
			razor_package_query_add_package(query, package);
krh@281
    68
			count++;
krh@281
    69
		}
krh@281
    70
		razor_package_iterator_destroy(iter);
krh@281
    71
krh@281
    72
		if (count == 0)
krh@281
    73
			fprintf(stderr,
krh@281
    74
				"no package matches \"%s\"\n", pattern);
rhughes@241
    75
	}
rhughes@241
    76
krh@281
    77
	return razor_package_query_finish(query);
krh@281
    78
}
krh@281
    79
krh@281
    80
#define LIST_PACKAGES_ONLY_NAMES 0x01
krh@281
    81
krh@281
    82
static void
krh@281
    83
list_packages(struct razor_package_iterator *iter, uint32_t flags)
krh@281
    84
{
krh@281
    85
	struct razor_package *package;
krh@281
    86
	const char *name, *version, *arch;
krh@281
    87
krh@281
    88
	while (razor_package_iterator_next(iter, &package,
rhughes@241
    89
					   &name, &version, &arch)) {
krh@281
    90
		if (flags & LIST_PACKAGES_ONLY_NAMES)
rhughes@241
    91
			printf("%s\n", name);
rhughes@241
    92
		else
rhughes@241
    93
			printf("%s-%s.%s\n", name, version, arch);
rhughes@241
    94
	}
krh@281
    95
}
krh@281
    96
krh@281
    97
static int
krh@281
    98
command_list(int argc, const char *argv[])
krh@281
    99
{
krh@281
   100
	struct razor_package_iterator *pi;
krh@281
   101
	struct razor_set *set;
krh@281
   102
	uint32_t flags = 0;
krh@281
   103
	int i = 0;
krh@281
   104
krh@281
   105
	if (i < argc && strcmp(argv[i], "--only-names") == 0) {
krh@281
   106
		flags |= LIST_PACKAGES_ONLY_NAMES;
krh@281
   107
		i++;
krh@281
   108
	}
krh@281
   109
krh@281
   110
	set = razor_set_open(repo_filename);
krh@281
   111
	pi = create_iterator_from_argv(set, argc - i, argv + i);
krh@281
   112
	list_packages(pi, flags);
rhughes@241
   113
	razor_package_iterator_destroy(pi);
rhughes@241
   114
	razor_set_destroy(set);
rhughes@241
   115
rhughes@241
   116
	return 0;
rhughes@241
   117
}
rhughes@241
   118
rhughes@241
   119
static int
krh@247
   120
list_properties(const char *package_name, uint32_t type)
rhughes@241
   121
{
rhughes@241
   122
	struct razor_set *set;
rhughes@241
   123
	struct razor_property *property;
rhughes@241
   124
	struct razor_package *package;
rhughes@241
   125
	struct razor_property_iterator *pi;
rhughes@241
   126
	const char *name, *version;
krh@247
   127
	uint32_t flags;
rhughes@241
   128
rhughes@241
   129
	set = razor_set_open(repo_filename);
richard@280
   130
	if (package_name) {
rhughes@241
   131
		package = razor_set_get_package(set, package_name);
richard@280
   132
		if (!package) {
richard@280
   133
			fprintf(stderr, "no package named \"%s\"\n", package_name);
richard@280
   134
			return 1;
richard@280
   135
		}
richard@280
   136
	} else
rhughes@241
   137
		package = NULL;
rhughes@241
   138
rhughes@241
   139
	pi = razor_property_iterator_create(set, package);
rhughes@241
   140
	while (razor_property_iterator_next(pi, &property,
krh@247
   141
					    &name, &flags, &version)) {
krh@247
   142
		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
rhughes@241
   143
			continue;
krh@247
   144
		printf("%s", name);
krh@247
   145
		if (version[0] != '\0')
krh@247
   146
			printf(" %s %s",
krh@247
   147
			       razor_property_relation_to_string(property),
krh@247
   148
			       version);
krh@247
   149
krh@247
   150
		if (flags & ~(RAZOR_PROPERTY_RELATION_MASK | RAZOR_PROPERTY_TYPE_MASK)) {
krh@247
   151
			printf(" [");
krh@247
   152
			if (flags & RAZOR_PROPERTY_PRE)
krh@247
   153
				printf(" pre");
krh@247
   154
			if (flags & RAZOR_PROPERTY_POST)
krh@247
   155
				printf(" post");
krh@247
   156
			if (flags & RAZOR_PROPERTY_PREUN)
krh@247
   157
				printf(" preun");
krh@247
   158
			if (flags & RAZOR_PROPERTY_POSTUN)
krh@247
   159
				printf(" postun");
krh@247
   160
			printf(" ]");
krh@247
   161
		}
krh@247
   162
		printf("\n");
rhughes@241
   163
	}
rhughes@241
   164
	razor_property_iterator_destroy(pi);
rhughes@241
   165
rhughes@241
   166
	razor_set_destroy(set);
rhughes@241
   167
rhughes@241
   168
	return 0;
rhughes@241
   169
}
rhughes@241
   170
rhughes@241
   171
static int
rhughes@241
   172
command_list_requires(int argc, const char *argv[])
rhughes@241
   173
{
rhughes@241
   174
	return list_properties(argv[0], RAZOR_PROPERTY_REQUIRES);
rhughes@241
   175
}
rhughes@241
   176
rhughes@241
   177
static int
rhughes@241
   178
command_list_provides(int argc, const char *argv[])
rhughes@241
   179
{
rhughes@241
   180
	return list_properties(argv[0], RAZOR_PROPERTY_PROVIDES);
rhughes@241
   181
}
rhughes@241
   182
rhughes@241
   183
static int
rhughes@241
   184
command_list_obsoletes(int argc, const char *argv[])
rhughes@241
   185
{
rhughes@241
   186
	return list_properties(argv[0], RAZOR_PROPERTY_OBSOLETES);
rhughes@241
   187
}
rhughes@241
   188
rhughes@241
   189
static int
rhughes@241
   190
command_list_conflicts(int argc, const char *argv[])
rhughes@241
   191
{
rhughes@241
   192
	return list_properties(argv[0], RAZOR_PROPERTY_CONFLICTS);
rhughes@241
   193
}
rhughes@241
   194
rhughes@241
   195
static int
rhughes@241
   196
command_list_files(int argc, const char *argv[])
rhughes@241
   197
{
rhughes@241
   198
	struct razor_set *set;
rhughes@241
   199
rhughes@241
   200
	set = razor_set_open(repo_filename);
jbowes@258
   201
	razor_set_open_files(set, "system-files.repo");
rhughes@241
   202
	if (set == NULL)
rhughes@241
   203
		return 1;
rhughes@241
   204
	razor_set_list_files(set, argv[0]);
rhughes@241
   205
	razor_set_destroy(set);
rhughes@241
   206
rhughes@241
   207
	return 0;
rhughes@241
   208
}
rhughes@241
   209
rhughes@241
   210
static int
rhughes@241
   211
command_list_file_packages(int argc, const char *argv[])
rhughes@241
   212
{
rhughes@241
   213
	struct razor_set *set;
rhughes@241
   214
	struct razor_package_iterator *pi;
rhughes@241
   215
rhughes@241
   216
	set = razor_set_open(repo_filename);
jbowes@258
   217
	razor_set_open_files(set, "system-files.repo");
rhughes@241
   218
	if (set == NULL)
rhughes@241
   219
		return 1;
rhughes@241
   220
rhughes@241
   221
	pi = razor_package_iterator_create_for_file(set, argv[0]);
krh@281
   222
	list_packages(pi, 0);
rhughes@241
   223
	razor_package_iterator_destroy(pi);
rhughes@241
   224
rhughes@241
   225
	razor_set_destroy(set);
rhughes@241
   226
rhughes@241
   227
	return 0;
rhughes@241
   228
}
rhughes@241
   229
rhughes@241
   230
static int
rhughes@241
   231
command_list_package_files(int argc, const char *argv[])
rhughes@241
   232
{
rhughes@241
   233
	struct razor_set *set;
rhughes@241
   234
rhughes@241
   235
	set = razor_set_open(repo_filename);
jbowes@258
   236
	razor_set_open_files(set, "system-files.repo");
rhughes@241
   237
	if (set == NULL)
rhughes@241
   238
		return 1;
rhughes@241
   239
	razor_set_list_package_files(set, argv[0]);
rhughes@241
   240
	razor_set_destroy(set);
rhughes@241
   241
rhughes@241
   242
	return 0;
rhughes@241
   243
}
rhughes@241
   244
rhughes@241
   245
static int
rhughes@241
   246
list_property_packages(const char *ref_name,
rhughes@241
   247
		       const char *ref_version,
krh@247
   248
		       uint32_t type)
rhughes@241
   249
{
rhughes@241
   250
	struct razor_set *set;
rhughes@241
   251
	struct razor_property *property;
krh@281
   252
	struct razor_property_iterator *prop_iter;
krh@281
   253
	struct razor_package_iterator *pkg_iter;
rhughes@241
   254
	const char *name, *version;
krh@247
   255
	uint32_t flags;
rhughes@241
   256
rhughes@241
   257
	if (ref_name == NULL)
rhughes@241
   258
		return 0;
rhughes@241
   259
rhughes@241
   260
	set = razor_set_open(repo_filename);
rhughes@241
   261
	if (set == NULL)
rhughes@241
   262
		return 1;
rhughes@241
   263
krh@281
   264
	prop_iter = razor_property_iterator_create(set, NULL);
krh@281
   265
	while (razor_property_iterator_next(prop_iter, &property,
krh@247
   266
					    &name, &flags, &version)) {
rhughes@241
   267
		if (strcmp(ref_name, name) != 0)
rhughes@241
   268
			continue;
krh@247
   269
		if (ref_version &&
krh@247
   270
		    (flags & RAZOR_PROPERTY_RELATION_MASK) == RAZOR_PROPERTY_EQUAL &&
rhughes@241
   271
		    strcmp(ref_version, version) != 0)
rhughes@241
   272
			continue;
krh@247
   273
		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
rhughes@241
   274
			continue;
rhughes@241
   275
krh@281
   276
		pkg_iter =
krh@281
   277
			razor_package_iterator_create_for_property(set,
krh@281
   278
								   property);
krh@281
   279
		list_packages(pkg_iter, 0);
krh@281
   280
		razor_package_iterator_destroy(pkg_iter);
rhughes@241
   281
	}
krh@281
   282
	razor_property_iterator_destroy(prop_iter);
rhughes@241
   283
rhughes@241
   284
	return 0;
rhughes@241
   285
}
rhughes@241
   286
rhughes@241
   287
static int
rhughes@241
   288
command_what_requires(int argc, const char *argv[])
rhughes@241
   289
{
rhughes@241
   290
	return list_property_packages(argv[0], argv[1],
rhughes@241
   291
				      RAZOR_PROPERTY_REQUIRES);
rhughes@241
   292
}
rhughes@241
   293
rhughes@241
   294
static int
rhughes@241
   295
command_what_provides(int argc, const char *argv[])
rhughes@241
   296
{
rhughes@241
   297
	return list_property_packages(argv[0], argv[1],
rhughes@241
   298
				      RAZOR_PROPERTY_PROVIDES);
rhughes@241
   299
}
rhughes@241
   300
rhughes@241
   301
static int
rhughes@241
   302
show_progress(void *clientp,
rhughes@241
   303
	      double dltotal, double dlnow, double ultotal, double ulnow)
rhughes@241
   304
{
rhughes@241
   305
	const char *file = clientp;
rhughes@241
   306
rhughes@241
   307
	if (!dlnow < dltotal)
rhughes@241
   308
		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
rhughes@241
   309
			file, (int) dlnow / 1024, (int) dltotal / 1024);
rhughes@241
   310
rhughes@241
   311
	return 0;
rhughes@241
   312
}
rhughes@241
   313
rhughes@241
   314
static int
rhughes@241
   315
download_if_missing(const char *url, const char *file)
rhughes@241
   316
{
rhughes@241
   317
	CURL *curl;
rhughes@241
   318
	struct stat buf;
rhughes@241
   319
	char error[256];
rhughes@241
   320
	FILE *fp;
rhughes@241
   321
	CURLcode res;
rhughes@241
   322
	long response;
rhughes@241
   323
rhughes@241
   324
	curl = curl_easy_init();
rhughes@241
   325
	if (curl == NULL)
rhughes@241
   326
		return 1;
rhughes@241
   327
rhughes@241
   328
	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
rhughes@241
   329
	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
rhughes@241
   330
	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
rhughes@241
   331
	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
rhughes@241
   332
rhughes@241
   333
	if (stat(file, &buf) < 0) {
rhughes@241
   334
		fp = fopen(file, "w");
rhughes@241
   335
		if (fp == NULL) {
rhughes@241
   336
			fprintf(stderr,
rhughes@241
   337
				"failed to open %s for writing\n", file);
rhughes@241
   338
			return -1;
rhughes@241
   339
		}
rhughes@241
   340
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
rhughes@241
   341
		curl_easy_setopt(curl, CURLOPT_URL, url);
rhughes@241
   342
		res = curl_easy_perform(curl);
rhughes@241
   343
		fclose(fp);
rhughes@241
   344
		if (res != CURLE_OK) {
rhughes@241
   345
			fprintf(stderr, "curl error: %s\n", error);
rhughes@241
   346
			unlink(file);
rhughes@241
   347
			return -1;
rhughes@241
   348
		}
rhughes@241
   349
		res = curl_easy_getinfo(curl,
rhughes@241
   350
					CURLINFO_RESPONSE_CODE, &response);
rhughes@241
   351
		if (res != CURLE_OK) {
rhughes@241
   352
			fprintf(stderr, "curl error: %s\n", error);
rhughes@241
   353
                        unlink(file);
rhughes@241
   354
                        return -1;
rhughes@241
   355
		}
rhughes@241
   356
		if (response != 200) {
rhughes@241
   357
			fprintf(stderr, " - failed %ld\n", response);
rhughes@241
   358
                        unlink(file);
rhughes@241
   359
                        return -1;
rhughes@241
   360
		}
rhughes@241
   361
		fprintf(stderr, "\n");
rhughes@241
   362
	}
rhughes@241
   363
rhughes@241
   364
	curl_easy_cleanup(curl);
rhughes@241
   365
rhughes@241
   366
	return 0;
rhughes@241
   367
}
rhughes@241
   368
rhughes@241
   369
#define YUM_URL "http://download.fedora.redhat.com" \
rhughes@241
   370
	"/pub/fedora/linux/development/i386/os"
rhughes@241
   371
rhughes@241
   372
static int
rhughes@241
   373
command_import_yum(int argc, const char *argv[])
rhughes@241
   374
{
rhughes@241
   375
	struct razor_set *set;
rhughes@241
   376
	char buffer[512];
rhughes@241
   377
rhughes@241
   378
	printf("downloading from %s.\n", yum_url);
rhughes@241
   379
	snprintf(buffer, sizeof buffer,
rhughes@241
   380
		 "%s/repodata/primary.xml.gz", yum_url);
rhughes@241
   381
	if (download_if_missing(buffer, "primary.xml.gz") < 0)
rhughes@241
   382
		return -1;
rhughes@241
   383
	snprintf(buffer, sizeof buffer,
rhughes@241
   384
		 "%s/repodata/filelists.xml.gz", yum_url);
rhughes@241
   385
	if (download_if_missing(buffer, "filelists.xml.gz") < 0)
rhughes@241
   386
		return -1;
rhughes@241
   387
rhughes@241
   388
	set = razor_set_create_from_yum();
rhughes@241
   389
	if (set == NULL)
rhughes@241
   390
		return 1;
jbowes@258
   391
	razor_set_write(set, rawhide_repo_filename, RAZOR_REPO_FILE_MAIN);
jbowes@258
   392
	razor_set_write(set, "rawhide-details.repo", RAZOR_REPO_FILE_DETAILS);
jbowes@258
   393
	razor_set_write(set, "rawhide-files.repo", RAZOR_REPO_FILE_FILES);
rhughes@241
   394
	razor_set_destroy(set);
rhughes@241
   395
	printf("wrote %s\n", rawhide_repo_filename);
rhughes@241
   396
rhughes@241
   397
	return 0;
rhughes@241
   398
}
rhughes@241
   399
rhughes@241
   400
static int
rhughes@241
   401
command_import_rpmdb(int argc, const char *argv[])
rhughes@241
   402
{
rhughes@241
   403
	struct razor_set *set;
rhughes@241
   404
rhughes@241
   405
	set = razor_set_create_from_rpmdb();
rhughes@241
   406
	if (set == NULL)
rhughes@241
   407
		return 1;
jbowes@258
   408
	razor_set_write(set, repo_filename, RAZOR_REPO_FILE_MAIN);
jbowes@258
   409
	razor_set_write(set, "system-details.repo", RAZOR_REPO_FILE_DETAILS);
jbowes@258
   410
	razor_set_write(set, "system-files.repo", RAZOR_REPO_FILE_FILES);
rhughes@241
   411
	razor_set_destroy(set);
rhughes@241
   412
	printf("wrote %s\n", repo_filename);
rhughes@241
   413
rhughes@241
   414
	return 0;
rhughes@241
   415
}
rhughes@241
   416
rhughes@241
   417
static int
rhughes@241
   418
mark_packages_for_update(struct razor_transaction *trans,
rhughes@241
   419
			 struct razor_set *set, const char *pattern)
rhughes@241
   420
{
rhughes@241
   421
	struct razor_package_iterator *pi;
rhughes@241
   422
	struct razor_package *package;
rhughes@241
   423
	const char *name, *version, *arch;
rhughes@241
   424
	int matches = 0;
rhughes@241
   425
rhughes@241
   426
	pi = razor_package_iterator_create(set);
rhughes@241
   427
	while (razor_package_iterator_next(pi, &package,
rhughes@241
   428
					   &name, &version, &arch)) {
rhughes@241
   429
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   430
			razor_transaction_update_package(trans, package);
rhughes@241
   431
			matches++;
rhughes@241
   432
		}
rhughes@241
   433
	}
rhughes@241
   434
	razor_package_iterator_destroy(pi);
rhughes@241
   435
rhughes@241
   436
	return matches;
rhughes@241
   437
}
rhughes@241
   438
rhughes@241
   439
static int
rhughes@241
   440
mark_packages_for_removal(struct razor_transaction *trans,
rhughes@241
   441
			  struct razor_set *set, const char *pattern)
rhughes@241
   442
{
rhughes@241
   443
	struct razor_package_iterator *pi;
rhughes@241
   444
	struct razor_package *package;
rhughes@241
   445
	const char *name, *version, *arch;
rhughes@241
   446
	int matches = 0;
rhughes@241
   447
rhughes@241
   448
	pi = razor_package_iterator_create(set);
rhughes@241
   449
	while (razor_package_iterator_next(pi, &package,
rhughes@241
   450
					   &name, &version, &arch)) {
rhughes@241
   451
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   452
			razor_transaction_remove_package(trans, package);
rhughes@241
   453
			matches++;
rhughes@241
   454
		}
rhughes@241
   455
	}
rhughes@241
   456
	razor_package_iterator_destroy(pi);
rhughes@241
   457
rhughes@241
   458
	return matches;
rhughes@241
   459
}
rhughes@241
   460
rhughes@241
   461
static int
rhughes@241
   462
command_update(int argc, const char *argv[])
rhughes@241
   463
{
rhughes@241
   464
	struct razor_set *set, *upstream;
rhughes@241
   465
	struct razor_transaction *trans;
rhughes@241
   466
	int i, errors;
rhughes@241
   467
rhughes@241
   468
	set = razor_set_open(repo_filename);
rhughes@241
   469
	upstream = razor_set_open(rawhide_repo_filename);
rhughes@241
   470
	if (set == NULL || upstream == NULL)
rhughes@241
   471
		return 1;
rhughes@241
   472
rhughes@241
   473
	trans = razor_transaction_create(set, upstream);
rhughes@241
   474
	if (argc == 0)
rhughes@241
   475
		razor_transaction_update_all(trans);
rhughes@241
   476
	for (i = 0; i < argc; i++) {
rhughes@241
   477
		if (mark_packages_for_update(trans, set, argv[i]) == 0) {
rhughes@241
   478
			fprintf(stderr, "no match for %s\n", argv[i]);
rhughes@241
   479
			return 1;
rhughes@241
   480
		}
rhughes@241
   481
	}
rhughes@241
   482
krh@252
   483
	razor_transaction_resolve(trans);
krh@252
   484
	errors = razor_transaction_describe(trans);
krh@252
   485
	if (errors) {
krh@252
   486
		fprintf(stderr, "unresolved dependencies\n");
rhughes@241
   487
		return 1;
krh@252
   488
	}
rhughes@241
   489
rhughes@241
   490
	set = razor_transaction_finish(trans);
jbowes@258
   491
	razor_set_write(set, updated_repo_filename, RAZOR_REPO_FILE_MAIN);
rhughes@241
   492
	razor_set_destroy(set);
rhughes@241
   493
	razor_set_destroy(upstream);
rhughes@241
   494
	printf("wrote system-updated.repo\n");
rhughes@241
   495
rhughes@241
   496
	return 0;
rhughes@241
   497
}
rhughes@241
   498
rhughes@241
   499
static int
rhughes@241
   500
command_remove(int argc, const char *argv[])
rhughes@241
   501
{
rhughes@241
   502
	struct razor_set *set, *upstream;
rhughes@241
   503
	struct razor_transaction *trans;
rhughes@241
   504
	int i, errors;
rhughes@241
   505
rhughes@241
   506
	set = razor_set_open(repo_filename);
rhughes@241
   507
	if (set == NULL)
rhughes@241
   508
		return 1;
rhughes@241
   509
rhughes@241
   510
	upstream = razor_set_create();
rhughes@241
   511
	trans = razor_transaction_create(set, upstream);
rhughes@241
   512
	for (i = 0; i < argc; i++) {
rhughes@241
   513
		if (mark_packages_for_removal(trans, set, argv[i]) == 0) {
rhughes@241
   514
			fprintf(stderr, "no match for %s\n", argv[i]);
rhughes@241
   515
			return 1;
rhughes@241
   516
		}
rhughes@241
   517
	}
rhughes@241
   518
rhughes@241
   519
	errors = razor_transaction_resolve(trans);
rhughes@241
   520
	if (errors)
rhughes@241
   521
		return 1;
rhughes@241
   522
rhughes@241
   523
	set = razor_transaction_finish(trans);
jbowes@258
   524
	razor_set_write(set, updated_repo_filename, RAZOR_REPO_FILE_MAIN);
rhughes@241
   525
	razor_set_destroy(set);
rhughes@241
   526
	razor_set_destroy(upstream);
rhughes@241
   527
	printf("wrote system-updated.repo\n");
rhughes@241
   528
rhughes@241
   529
	return 0;
rhughes@241
   530
}
rhughes@241
   531
rhughes@241
   532
static void
krh@253
   533
print_diff(enum razor_diff_action action,
krh@253
   534
	   struct razor_package *package,
krh@253
   535
	   const char *name,
krh@253
   536
	   const char *version,
krh@253
   537
	   const char *arch,
rhughes@241
   538
	   void *data)
rhughes@241
   539
{
krh@253
   540
	if (action == RAZOR_DIFF_ACTION_ADD)
krh@253
   541
		printf("install %s-%s.%s\n", name, version, arch);
krh@253
   542
	if (action == RAZOR_DIFF_ACTION_REMOVE)
krh@253
   543
		printf("remove %s-%s.%s\n", name, version, arch);
rhughes@241
   544
}
rhughes@241
   545
rhughes@241
   546
static int
rhughes@241
   547
command_diff(int argc, const char *argv[])
rhughes@241
   548
{
rhughes@241
   549
	struct razor_set *set, *updated;
rhughes@241
   550
rhughes@241
   551
	set = razor_set_open(repo_filename);
rhughes@241
   552
	updated = razor_set_open(updated_repo_filename);
rhughes@241
   553
	if (set == NULL || updated == NULL)
rhughes@241
   554
		return 1;
rhughes@241
   555
rhughes@241
   556
	razor_set_diff(set, updated, print_diff, NULL);
rhughes@241
   557
rhughes@241
   558
	razor_set_destroy(set);
rhughes@241
   559
	razor_set_destroy(updated);
rhughes@241
   560
rhughes@241
   561
	return 0;
rhughes@241
   562
}
rhughes@241
   563
rhughes@241
   564
static int
rhughes@241
   565
command_import_rpms(int argc, const char *argv[])
rhughes@241
   566
{
rhughes@241
   567
	DIR *dir;
rhughes@241
   568
	struct dirent *de;
rhughes@241
   569
	struct razor_importer *importer;
rhughes@241
   570
	struct razor_set *set;
rhughes@241
   571
	struct razor_rpm *rpm;
jbowes@263
   572
	int len, imported_count = 0;
rhughes@241
   573
	char filename[256];
rhughes@241
   574
	const char *dirname = argv[0];
rhughes@241
   575
rhughes@241
   576
	if (dirname == NULL) {
rhughes@241
   577
		fprintf(stderr, "usage: razor import-rpms DIR\n");
rhughes@241
   578
		return -1;
rhughes@241
   579
	}
rhughes@241
   580
rhughes@241
   581
	dir = opendir(dirname);
rhughes@241
   582
	if (dir == NULL) {
rhughes@241
   583
		fprintf(stderr, "couldn't read dir %s\n", dirname);
rhughes@241
   584
		return -1;
rhughes@241
   585
	}
rhughes@241
   586
krh@249
   587
	importer = razor_importer_create();
rhughes@241
   588
rhughes@241
   589
	while (de = readdir(dir), de != NULL) {
rhughes@241
   590
		len = strlen(de->d_name);
rhughes@241
   591
		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
rhughes@241
   592
		    continue;
rhughes@241
   593
		snprintf(filename, sizeof filename,
rhughes@241
   594
			 "%s/%s", dirname, de->d_name);
rhughes@241
   595
		rpm = razor_rpm_open(filename);
rhughes@241
   596
		if (rpm == NULL) {
rhughes@241
   597
			fprintf(stderr,
rhughes@241
   598
				"failed to open rpm \"%s\"\n", filename);
rhughes@241
   599
			continue;
rhughes@241
   600
		}
rhughes@241
   601
		if (razor_importer_add_rpm(importer, rpm)) {
rhughes@241
   602
			fprintf(stderr, "couldn't import %s\n", filename);
rhughes@241
   603
			break;
rhughes@241
   604
		}
rhughes@241
   605
		razor_rpm_close(rpm);
jbowes@263
   606
jbowes@263
   607
		printf("\rimporting %d", ++imported_count);
jbowes@263
   608
		fflush(stdout);
rhughes@241
   609
	}
rhughes@241
   610
rhughes@241
   611
	if (de != NULL) {
rhughes@241
   612
		razor_importer_destroy(importer);
rhughes@241
   613
		return -1;
rhughes@241
   614
	}
rhughes@241
   615
jbowes@263
   616
	printf("\nsaving\n");
rhughes@241
   617
	set = razor_importer_finish(importer);
rhughes@241
   618
jbowes@258
   619
	razor_set_write(set, repo_filename, RAZOR_REPO_FILE_MAIN);
rhughes@241
   620
	razor_set_destroy(set);
rhughes@241
   621
	printf("wrote %s\n", repo_filename);
rhughes@241
   622
rhughes@241
   623
	return 0;
rhughes@241
   624
}
rhughes@241
   625
krh@254
   626
static const char *
krh@254
   627
rpm_filename(const char *name, const char *version, const char *arch)
rhughes@241
   628
{
krh@254
   629
	static char file[PATH_MAX];
krh@254
   630
 	const char *v;
krh@254
   631
 
krh@254
   632
 	/* Skip epoch */
krh@253
   633
	v = strchr(version, ':');
krh@254
   634
 	if (v != NULL)
krh@254
   635
 		v = v + 1;
krh@254
   636
 	else
krh@253
   637
		v = version;
rhughes@241
   638
krh@254
   639
	snprintf(file, sizeof file, "%s-%s.%s.rpm", name, v, arch);
rhughes@241
   640
krh@254
   641
	return file;
rhughes@241
   642
}
rhughes@241
   643
krh@254
   644
static int
krh@254
   645
download_packages(struct razor_set *system, struct razor_set *next)
rhughes@241
   646
{
krh@254
   647
	struct razor_package_iterator *pi;
krh@254
   648
	struct razor_package *package;
krh@254
   649
	const char *name, *version, *arch;
krh@254
   650
	char file[PATH_MAX], url[256];
krh@254
   651
	int errors;
krh@254
   652
 
krh@254
   653
	pi = razor_set_create_install_iterator(system, next);
krh@254
   654
	errors = 0;
krh@254
   655
	while (razor_package_iterator_next(pi, &package,
krh@254
   656
					   &name, &version, &arch)) {
krh@254
   657
		snprintf(url, sizeof url,
krh@254
   658
			 "%s/Packages/%s",
krh@254
   659
			 yum_url, rpm_filename(name, version, arch));
krh@254
   660
		snprintf(file, sizeof file,
krh@254
   661
			 "rpms/%s", rpm_filename(name, version, arch));
krh@254
   662
		if (download_if_missing(url, file) < 0)
krh@254
   663
			errors++;
krh@254
   664
	}
krh@254
   665
	razor_package_iterator_destroy(pi);
krh@254
   666
krh@254
   667
	if (errors > 0) {
krh@254
   668
		fprintf(stderr, "failed to download %d packages\n", errors);
krh@254
   669
                return -1;
krh@254
   670
        }
krh@254
   671
krh@254
   672
	return 0;
krh@254
   673
}
krh@254
   674
krh@254
   675
static int
krh@254
   676
install_packages(struct razor_set *system, struct razor_set *next)
krh@254
   677
{
krh@254
   678
	struct razor_package_iterator *pi;
krh@254
   679
	struct razor_package *package;
krh@254
   680
	struct razor_rpm *rpm;
krh@254
   681
	const char *name, *version, *arch;
rhughes@241
   682
	char file[PATH_MAX];
rhughes@241
   683
krh@254
   684
	pi = razor_set_create_install_iterator(system, next);
krh@254
   685
	while (razor_package_iterator_next(pi, &package,
krh@254
   686
					   &name, &version, &arch)) {
krh@254
   687
		printf("install %s-%s\n", name, version);
krh@254
   688
krh@254
   689
		snprintf(file, sizeof file,
krh@254
   690
			 "rpms/%s", rpm_filename(name, version, arch));
krh@254
   691
		rpm = razor_rpm_open(file);
krh@254
   692
		if (rpm == NULL) {
krh@254
   693
			fprintf(stderr, "failed to open rpm %s\n", file);
krh@254
   694
			return -1;
krh@254
   695
		}
krh@254
   696
		if (razor_rpm_install(rpm, install_root) < 0) {
krh@254
   697
			fprintf(stderr,
krh@254
   698
				"failed to install rpm %s\n", file);
krh@254
   699
			return -1;
krh@254
   700
		}
krh@254
   701
		razor_rpm_close(rpm);
rhughes@241
   702
	}
krh@254
   703
	razor_package_iterator_destroy(pi);
rhughes@241
   704
krh@254
   705
	return 0;
rhughes@241
   706
}
rhughes@241
   707
rhughes@241
   708
static int
rhughes@241
   709
command_install(int argc, const char *argv[])
rhughes@241
   710
{
rhughes@241
   711
	struct razor_root *root;
krh@250
   712
	struct razor_set *system, *upstream, *next;
rhughes@241
   713
	struct razor_transaction *trans;
krh@281
   714
	int i = 0, dependencies = 1;
rhughes@241
   715
rhughes@241
   716
	if (i < argc && strcmp(argv[i], "--no-dependencies") == 0) {
rhughes@241
   717
		dependencies = 0;
rhughes@241
   718
		i++;
rhughes@241
   719
	}
rhughes@241
   720
krh@250
   721
	root = razor_root_open(install_root);
krh@255
   722
	if (root == NULL)
krh@255
   723
		return 1;
krh@255
   724
krh@250
   725
	system = razor_root_get_system_set(root);
rhughes@241
   726
	upstream = razor_set_open(rawhide_repo_filename);
krh@250
   727
	trans = razor_transaction_create(system, upstream);
rhughes@241
   728
rhughes@241
   729
	for (; i < argc; i++) {
rhughes@241
   730
		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
rhughes@241
   731
			fprintf(stderr, "no package matched %s\n", argv[i]);
rhughes@241
   732
			razor_root_close(root);
rhughes@241
   733
			return 1;
rhughes@241
   734
		}
rhughes@241
   735
	}
rhughes@241
   736
rhughes@241
   737
	if (dependencies) {
krh@245
   738
		razor_transaction_resolve(trans);
krh@245
   739
		if (razor_transaction_describe(trans) > 0) {
rhughes@241
   740
			razor_root_close(root);
rhughes@241
   741
			return 1;
rhughes@241
   742
		}
rhughes@241
   743
	}
rhughes@241
   744
rhughes@241
   745
	next = razor_transaction_finish(trans);
rhughes@241
   746
rhughes@241
   747
	razor_root_update(root, next);
rhughes@241
   748
rhughes@241
   749
	if (mkdir("rpms", 0777) && errno != EEXIST) {
rhughes@241
   750
		fprintf(stderr, "failed to create rpms directory.\n");
rhughes@241
   751
		razor_root_close(root);
rhughes@241
   752
		return 1;
rhughes@241
   753
	}
rhughes@241
   754
krh@254
   755
	if (download_packages(system, next) < 0) {
rhughes@241
   756
		razor_root_close(root);
rhughes@241
   757
                return 1;
rhughes@241
   758
        }
rhughes@241
   759
krh@254
   760
	install_packages(system, next);
rhughes@241
   761
rhughes@241
   762
	razor_set_destroy(next);
rhughes@241
   763
	razor_set_destroy(upstream);
rhughes@241
   764
rhughes@241
   765
	return razor_root_commit(root);
rhughes@241
   766
}
rhughes@241
   767
rhughes@241
   768
static int
rhughes@241
   769
command_init(int argc, const char *argv[])
rhughes@241
   770
{
rhughes@241
   771
	return razor_root_create(install_root);
rhughes@241
   772
}
rhughes@241
   773
rhughes@241
   774
static int
rhughes@241
   775
command_download(int argc, const char *argv[])
rhughes@241
   776
{
rhughes@241
   777
	struct razor_set *set;
rhughes@241
   778
	struct razor_package_iterator *pi;
rhughes@241
   779
	struct razor_package *package;
rhughes@241
   780
	const char *pattern = argv[0], *name, *version, *arch;
rhughes@241
   781
	char url[256], file[256];
rhughes@241
   782
	int matches = 0;
rhughes@241
   783
rhughes@241
   784
	if (mkdir("rpms", 0777) && errno != EEXIST) {
rhughes@241
   785
		fprintf(stderr, "failed to create rpms directory.\n");
rhughes@241
   786
		return 1;
rhughes@241
   787
	}
rhughes@241
   788
rhughes@241
   789
	set = razor_set_open(rawhide_repo_filename);
rhughes@241
   790
	pi = razor_package_iterator_create(set);
rhughes@241
   791
	while (razor_package_iterator_next(pi, &package,
rhughes@241
   792
					   &name, &version, &arch)) {
rhughes@241
   793
		if (pattern && fnmatch(pattern, name, 0) != 0)
rhughes@241
   794
			continue;
rhughes@241
   795
rhughes@241
   796
		matches++;
rhughes@241
   797
		snprintf(url, sizeof url,
rhughes@241
   798
			 "%s/Packages/%s-%s.%s.rpm",
rhughes@241
   799
			 yum_url, name, version, arch);
rhughes@241
   800
		snprintf(file, sizeof file,
rhughes@241
   801
			 "rpms/%s-%s.%s.rpm", name, version, arch);
rhughes@241
   802
		download_if_missing(url, file);
rhughes@241
   803
	}
rhughes@241
   804
	razor_package_iterator_destroy(pi);
rhughes@241
   805
	razor_set_destroy(set);
rhughes@241
   806
rhughes@241
   807
	if (matches == 0)
rhughes@241
   808
		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
rhughes@241
   809
	else if (matches == 1)
rhughes@241
   810
		fprintf(stderr, "downloaded 1 package\n");
rhughes@241
   811
	else
rhughes@241
   812
		fprintf(stderr, "downloaded %d packages\n", matches);
rhughes@241
   813
rhughes@241
   814
	return 0;
rhughes@241
   815
}
rhughes@241
   816
jbowes@258
   817
static int
jbowes@258
   818
command_info(int argc, const char *argv[])
jbowes@258
   819
{
jbowes@258
   820
	struct razor_set *set;
jbowes@258
   821
	struct razor_package_iterator *pi;
jbowes@258
   822
	struct razor_package *package;
jbowes@258
   823
	const char *pattern = argv[0], *name, *version, *arch;
jbowes@258
   824
	const char *summary, *description, *url, *license;
jbowes@258
   825
jbowes@258
   826
	set = razor_set_open(repo_filename);
jbowes@258
   827
	razor_set_open_details(set, "system-details.repo");
jbowes@258
   828
	pi = razor_package_iterator_create(set);
jbowes@258
   829
	while (razor_package_iterator_next(pi, &package,
jbowes@258
   830
					   &name, &version, &arch)) {
jbowes@258
   831
		if (pattern && fnmatch(pattern, name, 0) != 0)
jbowes@258
   832
			continue;
jbowes@258
   833
jbowes@258
   834
		razor_package_get_details (set, package, &summary, &description,
jbowes@258
   835
					   &url, &license);
jbowes@258
   836
jbowes@258
   837
		printf ("Name:        %s\n", name);
jbowes@258
   838
		printf ("Arch:        %s\n", arch);
jbowes@258
   839
		printf ("Version:     %s\n", version);
jbowes@258
   840
		printf ("URL:         %s\n", url);
jbowes@258
   841
		printf ("License:     %s\n", license);
jbowes@258
   842
		printf ("Summary:     %s\n", summary);
jbowes@258
   843
		printf ("Description:\n");
jbowes@258
   844
		printf ("%s\n", description);
jbowes@258
   845
		printf ("\n");
jbowes@258
   846
	}
jbowes@258
   847
	razor_package_iterator_destroy(pi);
jbowes@258
   848
	razor_set_destroy(set);
jbowes@258
   849
jbowes@258
   850
	return 0;
jbowes@258
   851
}
jbowes@258
   852
rhughes@241
   853
static struct {
rhughes@241
   854
	const char *name;
rhughes@241
   855
	const char *description;
rhughes@241
   856
	int (*func)(int argc, const char *argv[]);
rhughes@241
   857
} razor_commands[] = {
rhughes@241
   858
	{ "list", "list all packages", command_list },
rhughes@241
   859
	{ "list-requires", "list all requires for the given package", command_list_requires },
rhughes@241
   860
	{ "list-provides", "list all provides for the given package", command_list_provides },
rhughes@241
   861
	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
rhughes@241
   862
	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
rhughes@241
   863
	{ "list-files", "list files for package set", command_list_files },
rhughes@241
   864
	{ "list-file-packages", "list packages owning file", command_list_file_packages },
rhughes@241
   865
	{ "list-package-files", "list files in package", command_list_package_files },
rhughes@241
   866
	{ "what-requires", "list the packages that have the given requires", command_what_requires },
rhughes@241
   867
	{ "what-provides", "list the packages that have the given provides", command_what_provides },
rhughes@241
   868
	{ "import-yum", "import yum metadata files", command_import_yum },
rhughes@241
   869
	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
rhughes@241
   870
	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
rhughes@241
   871
	{ "update", "update all or specified packages", command_update },
rhughes@241
   872
	{ "remove", "remove specified packages", command_remove },
rhughes@241
   873
	{ "diff", "show diff between two package sets", command_diff },
rhughes@241
   874
	{ "install", "install rpm", command_install },
rhughes@241
   875
	{ "init", "init razor root", command_init },
jbowes@258
   876
	{ "download", "download packages", command_download },
jbowes@258
   877
	{ "info", "display package details", command_info }
rhughes@241
   878
};
rhughes@241
   879
rhughes@241
   880
static int
rhughes@241
   881
usage(void)
rhughes@241
   882
{
rhughes@241
   883
	int i;
rhughes@241
   884
rhughes@241
   885
	printf("usage:\n");
rhughes@241
   886
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
   887
		printf("  %-20s%s\n",
rhughes@241
   888
		       razor_commands[i].name, razor_commands[i].description);
rhughes@241
   889
rhughes@241
   890
	return 1;
rhughes@241
   891
}
rhughes@241
   892
rhughes@241
   893
int
rhughes@241
   894
main(int argc, const char *argv[])
rhughes@241
   895
{
rhughes@241
   896
	char *repo;
rhughes@241
   897
	int i;
rhughes@241
   898
rhughes@241
   899
	repo = getenv("RAZOR_REPO");
rhughes@241
   900
	if (repo != NULL)
rhughes@241
   901
		repo_filename = repo;
rhughes@241
   902
rhughes@241
   903
	yum_url = getenv("YUM_URL");
rhughes@241
   904
	if (yum_url == NULL)
rhughes@241
   905
		yum_url = YUM_URL;
rhughes@241
   906
rhughes@241
   907
	if (argc < 2)
rhughes@241
   908
		return usage();
rhughes@241
   909
rhughes@241
   910
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
   911
		if (strcmp(razor_commands[i].name, argv[1]) == 0)
rhughes@241
   912
			return razor_commands[i].func(argc - 2, argv + 2);
rhughes@241
   913
rhughes@241
   914
	return usage();
rhughes@241
   915
}