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