main.c
author Kristian H?gsberg <krh@redhat.com>
Wed Mar 12 15:38:56 2008 -0400 (2008-03-12)
changeset 171 cca0174a756e
parent 155 068f443e00cc
child 175 d453ed5bfcfe
permissions -rw-r--r--
Write out the new repo on install.

We write the repo to a lock file repo, and once the RPMs are installed
we rename() the lock repo back in place.
krh@43
     1
#include <stdlib.h>
krh@43
     2
#include <stddef.h>
krh@43
     3
#include <stdio.h>
krh@43
     4
#include <string.h>
krh@72
     5
#include <sys/types.h>
krh@72
     6
#include <sys/stat.h>
krh@43
     7
#include <unistd.h>
krh@75
     8
#include <dirent.h>
krh@71
     9
#include <curl/curl.h>
krh@94
    10
#include <fnmatch.h>
krh@43
    11
#include "razor.h"
krh@151
    12
#include "razor-internal.h"
krh@43
    13
krh@152
    14
static const char system_repo_filename[] = "system.repo";
krh@171
    15
static const char next_repo_filename[] = "system-next.repo";
krh@152
    16
static const char rawhide_repo_filename[] = "rawhide.repo";
krh@152
    17
static const char updated_repo_filename[] = "system-updated.repo";
krh@152
    18
static const char razor_root_path[] = "/var/lib/razor";
krh@152
    19
static const char root[] = "install";
krh@152
    20
static const char *repo_filename = system_repo_filename;
krh@43
    21
krh@43
    22
static int
krh@43
    23
command_list(int argc, const char *argv[])
krh@43
    24
{
krh@43
    25
	struct razor_set *set;
krh@92
    26
	struct razor_package_iterator *pi;
krh@92
    27
	struct razor_package *package;
krh@92
    28
	const char *pattern = argv[0], *name, *version;
krh@43
    29
krh@43
    30
	set = razor_set_open(repo_filename);
krh@92
    31
	pi = razor_package_iterator_create(set);
krh@92
    32
	while (razor_package_iterator_next(pi, &package, &name, &version)) {
krh@92
    33
		if (pattern && fnmatch(pattern, name, 0) != 0)
krh@92
    34
			continue;
krh@92
    35
krh@92
    36
		printf("%s-%s\n", name, version);
krh@92
    37
	}
krh@92
    38
	razor_package_iterator_destroy(pi);
krh@92
    39
	razor_set_destroy(set);
krh@92
    40
krh@92
    41
	return 0;
krh@92
    42
}
krh@92
    43
krh@92
    44
static int
krh@92
    45
list_properties(const char *package_name,
krh@92
    46
		enum razor_property_type required_type)
krh@92
    47
{
krh@92
    48
	struct razor_set *set;
krh@92
    49
	struct razor_property *property;
krh@92
    50
	struct razor_package *package;
krh@92
    51
	struct razor_property_iterator *pi;
krh@92
    52
	const char *name, *version;
krh@92
    53
	enum razor_property_type type;
danw@109
    54
	enum razor_version_relation relation;
krh@92
    55
krh@92
    56
	set = razor_set_open(repo_filename);
krh@92
    57
	if (package_name)
krh@92
    58
		package = razor_set_get_package(set, package_name);
krh@92
    59
	else
krh@92
    60
		package = NULL;
krh@92
    61
krh@92
    62
	pi = razor_property_iterator_create(set, package);
krh@92
    63
	while (razor_property_iterator_next(pi, &property,
danw@109
    64
					    &name, &relation, &version,
danw@109
    65
					    &type)) {
krh@92
    66
		if (type != required_type)
krh@92
    67
			continue;
krh@92
    68
		if (version[0] == '\0')
krh@92
    69
			printf("%s\n", name);
krh@92
    70
		else
danw@137
    71
			printf("%s %s %s\n", name,
danw@137
    72
			       razor_version_relations[relation], version);
krh@92
    73
	}
krh@92
    74
	razor_property_iterator_destroy(pi);
krh@92
    75
krh@43
    76
	razor_set_destroy(set);
krh@43
    77
krh@43
    78
	return 0;
krh@43
    79
}
krh@43
    80
krh@43
    81
static int
krh@43
    82
command_list_requires(int argc, const char *argv[])
krh@43
    83
{
krh@92
    84
	return list_properties(argv[0], RAZOR_PROPERTY_REQUIRES);
krh@43
    85
}
krh@43
    86
krh@43
    87
static int
krh@43
    88
command_list_provides(int argc, const char *argv[])
krh@43
    89
{
krh@92
    90
	return list_properties(argv[0], RAZOR_PROPERTY_PROVIDES);
krh@43
    91
}
krh@43
    92
krh@43
    93
static int
krh@67
    94
command_list_obsoletes(int argc, const char *argv[])
krh@67
    95
{
krh@92
    96
	return list_properties(argv[0], RAZOR_PROPERTY_OBSOLETES);
krh@67
    97
}
krh@67
    98
krh@67
    99
static int
krh@67
   100
command_list_conflicts(int argc, const char *argv[])
krh@67
   101
{
krh@92
   102
	return list_properties(argv[0], RAZOR_PROPERTY_CONFLICTS);
krh@67
   103
}
krh@67
   104
krh@67
   105
static int
krh@48
   106
command_list_files(int argc, const char *argv[])
krh@48
   107
{
krh@48
   108
	struct razor_set *set;
krh@48
   109
krh@48
   110
	set = razor_set_open(repo_filename);
krh@48
   111
	if (set == NULL)
krh@48
   112
		return 1;
krh@49
   113
	razor_set_list_files(set, argv[0]);
krh@48
   114
	razor_set_destroy(set);
krh@48
   115
krh@48
   116
	return 0;
krh@48
   117
}
krh@48
   118
krh@48
   119
static int
krh@52
   120
command_list_file_packages(int argc, const char *argv[])
krh@52
   121
{
krh@52
   122
	struct razor_set *set;
krh@102
   123
	struct razor_package_iterator *pi;
krh@102
   124
	struct razor_package *package;
krh@102
   125
	const char *name, *version;
krh@52
   126
krh@52
   127
	set = razor_set_open(repo_filename);
krh@52
   128
	if (set == NULL)
krh@52
   129
		return 1;
krh@102
   130
krh@102
   131
	pi = razor_package_iterator_create_for_file(set, argv[0]);
krh@102
   132
	while (razor_package_iterator_next(pi, &package, &name, &version))
krh@102
   133
		printf("%s-%s\n", name, version);
krh@102
   134
	razor_package_iterator_destroy(pi);
krh@102
   135
krh@52
   136
	razor_set_destroy(set);
krh@52
   137
krh@52
   138
	return 0;
krh@52
   139
}
krh@52
   140
krh@56
   141
static int
krh@56
   142
command_list_package_files(int argc, const char *argv[])
krh@56
   143
{
krh@56
   144
	struct razor_set *set;
krh@56
   145
krh@56
   146
	set = razor_set_open(repo_filename);
krh@56
   147
	if (set == NULL)
krh@56
   148
		return 1;
krh@56
   149
	razor_set_list_package_files(set, argv[0]);
krh@56
   150
	razor_set_destroy(set);
krh@56
   151
krh@56
   152
	return 0;
krh@56
   153
}
krh@52
   154
krh@101
   155
static void
krh@101
   156
list_packages_for_property(struct razor_set *set,
krh@101
   157
			   struct razor_property *property)
krh@101
   158
{
krh@101
   159
	struct razor_package_iterator *pi;
krh@101
   160
	struct razor_package *package;
krh@101
   161
	const char *name, *version;
krh@101
   162
krh@101
   163
	pi = razor_package_iterator_create_for_property(set, property);
krh@101
   164
	while (razor_package_iterator_next(pi, &package, &name, &version))
krh@101
   165
		printf("%s-%s\n", name, version);
krh@101
   166
	razor_package_iterator_destroy(pi);
krh@101
   167
}
krh@101
   168
krh@52
   169
static int
krh@100
   170
list_property_packages(const char *ref_name,
krh@100
   171
		       const char *ref_version,
krh@100
   172
		       enum razor_property_type ref_type)
krh@43
   173
{
krh@43
   174
	struct razor_set *set;
krh@100
   175
	struct razor_property *property;
krh@100
   176
	struct razor_property_iterator *pi;
krh@100
   177
	const char *name, *version;
krh@100
   178
	enum razor_property_type type;
danw@109
   179
	enum razor_version_relation relation;
krh@100
   180
krh@100
   181
	if (ref_name == NULL)
krh@100
   182
		return 0;
krh@43
   183
krh@43
   184
	set = razor_set_open(repo_filename);
krh@100
   185
	if (set == NULL)
krh@100
   186
		return 1;
krh@100
   187
krh@100
   188
	pi = razor_property_iterator_create(set, NULL);
krh@100
   189
	while (razor_property_iterator_next(pi, &property,
danw@109
   190
					    &name, &relation, &version,
danw@109
   191
					    &type)) {
krh@100
   192
		if (strcmp(ref_name, name) != 0)
krh@100
   193
			continue;
danw@109
   194
		if (ref_version && relation == RAZOR_VERSION_EQUAL &&
danw@109
   195
		    strcmp(ref_version, version) != 0)
krh@100
   196
			continue;
krh@100
   197
		if (ref_type != type)
krh@100
   198
			continue;
krh@100
   199
krh@101
   200
		list_packages_for_property(set, property);
krh@100
   201
	}
krh@100
   202
	razor_property_iterator_destroy(pi);
krh@43
   203
krh@43
   204
	return 0;
krh@43
   205
}
krh@43
   206
krh@43
   207
static int
krh@100
   208
command_what_requires(int argc, const char *argv[])
krh@100
   209
{
krh@100
   210
	return list_property_packages(argv[0], argv[1],
krh@100
   211
				      RAZOR_PROPERTY_REQUIRES);
krh@100
   212
}
krh@100
   213
krh@100
   214
static int
krh@43
   215
command_what_provides(int argc, const char *argv[])
krh@43
   216
{
krh@100
   217
	return list_property_packages(argv[0], argv[1],
krh@100
   218
				      RAZOR_PROPERTY_PROVIDES);
krh@43
   219
}
krh@43
   220
krh@73
   221
static int
krh@73
   222
show_progress(void *clientp,
krh@73
   223
	      double dltotal, double dlnow, double ultotal, double ulnow)
krh@73
   224
{
krh@73
   225
	const char *file = clientp;
krh@73
   226
krh@73
   227
	if (!dlnow < dltotal)
krh@73
   228
		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
krh@73
   229
			file, (int) dlnow / 1024, (int) dltotal / 1024);
krh@73
   230
	else
krh@73
   231
		fprintf(stderr, "\n");
krh@73
   232
krh@73
   233
	return 0;
krh@73
   234
}
krh@73
   235
krh@73
   236
static int
krh@73
   237
download_if_missing(CURL *curl, const char *url, const char *file)
krh@73
   238
{
krh@73
   239
	struct stat buf;
krh@73
   240
	char error[256];
krh@73
   241
	FILE *fp;
krh@73
   242
	CURLcode res;
krh@73
   243
	char buffer[256];
krh@73
   244
krh@73
   245
	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
krh@73
   246
	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
krh@73
   247
	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
krh@73
   248
	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
krh@73
   249
krh@73
   250
	if (stat(file, &buf) < 0) {
krh@73
   251
		fp = fopen(file, "w");
krh@73
   252
		snprintf(buffer, sizeof buffer, "%s/%s", url, file);
krh@73
   253
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
krh@73
   254
		curl_easy_setopt(curl, CURLOPT_URL, buffer);
krh@73
   255
		res = curl_easy_perform(curl);
krh@73
   256
		fclose(fp);
krh@73
   257
		if (res != CURLE_OK) {
krh@73
   258
			fprintf(stderr, "curl error: %s\n", error);
krh@73
   259
			unlink(file);
krh@73
   260
			return -1;
krh@73
   261
		}
krh@73
   262
	}
krh@73
   263
krh@73
   264
	return 0;
krh@73
   265
}
krh@73
   266
krh@71
   267
#define REPO_URL "http://download.fedora.redhat.com" \
krh@71
   268
	"/pub/fedora/linux/development/i386/os/repodata"
krh@71
   269
krh@43
   270
static int
krh@43
   271
command_import_yum(int argc, const char *argv[])
krh@43
   272
{
krh@43
   273
	struct razor_set *set;
krh@71
   274
	CURL *curl;
krh@71
   275
krh@71
   276
	curl = curl_easy_init();
krh@71
   277
	if (curl == NULL)
krh@71
   278
		return 1;
krh@71
   279
krh@73
   280
	if (download_if_missing(curl, REPO_URL, "primary.xml.gz") < 0)
krh@73
   281
		return -1;
krh@73
   282
	if (download_if_missing(curl, REPO_URL, "filelists.xml.gz") < 0)
krh@73
   283
		return -1;
krh@71
   284
	curl_easy_cleanup(curl);
krh@43
   285
krh@70
   286
	set = razor_set_create_from_yum();
krh@43
   287
	if (set == NULL)
krh@43
   288
		return 1;
krh@43
   289
	razor_set_write(set, rawhide_repo_filename);
krh@43
   290
	razor_set_destroy(set);
krh@43
   291
	printf("wrote %s\n", rawhide_repo_filename);
krh@43
   292
krh@43
   293
	return 0;
krh@43
   294
}
krh@43
   295
krh@43
   296
static int
krh@43
   297
command_import_rpmdb(int argc, const char *argv[])
krh@43
   298
{
krh@43
   299
	struct razor_set *set;
krh@43
   300
krh@43
   301
	set = razor_set_create_from_rpmdb();
krh@43
   302
	if (set == NULL)
krh@43
   303
		return 1;
krh@43
   304
	razor_set_write(set, repo_filename);
krh@43
   305
	razor_set_destroy(set);
krh@43
   306
	printf("wrote %s\n", repo_filename);
krh@43
   307
krh@43
   308
	return 0;
krh@43
   309
}
krh@43
   310
krh@43
   311
static int
krh@43
   312
command_validate(int argc, const char *argv[])
krh@43
   313
{
krh@43
   314
	struct razor_set *set;
krh@43
   315
krh@43
   316
	set = razor_set_open(repo_filename);
krh@43
   317
	if (set == NULL)
krh@43
   318
		return 1;
krh@43
   319
	razor_set_list_unsatisfied(set);
krh@43
   320
	razor_set_destroy(set);
krh@43
   321
krh@43
   322
	return 0;
krh@43
   323
}
krh@43
   324
krh@43
   325
static int
krh@43
   326
command_update(int argc, const char *argv[])
krh@43
   327
{
krh@43
   328
	struct razor_set *set, *upstream;
danw@137
   329
	struct razor_transaction *trans;
krh@43
   330
krh@43
   331
	set = razor_set_open(repo_filename);
krh@43
   332
	upstream = razor_set_open(rawhide_repo_filename);
krh@43
   333
	if (set == NULL || upstream == NULL)
krh@43
   334
		return 1;
danw@137
   335
	trans = razor_transaction_create(set, upstream, argc, argv, 0, NULL);
danw@137
   336
	razor_transaction_describe(trans);
danw@137
   337
	if (trans->errors)
danw@137
   338
		return 1;
danw@137
   339
danw@137
   340
	set = razor_transaction_run(trans);
danw@137
   341
	razor_transaction_destroy(trans);
krh@44
   342
	razor_set_write(set, updated_repo_filename);
krh@43
   343
	razor_set_destroy(set);
krh@43
   344
	razor_set_destroy(upstream);
krh@43
   345
	printf("wrote system-updated.repo\n");
krh@43
   346
krh@43
   347
	return 0;
krh@43
   348
}
krh@43
   349
danw@129
   350
static int
danw@129
   351
command_remove(int argc, const char *argv[])
danw@129
   352
{
danw@129
   353
	struct razor_set *set;
danw@137
   354
	struct razor_transaction *trans;
danw@129
   355
danw@129
   356
	set = razor_set_open(repo_filename);
danw@129
   357
	if (set == NULL)
danw@129
   358
		return 1;
danw@137
   359
	trans = razor_transaction_create(set, NULL, 0, NULL, argc, argv);
danw@137
   360
	razor_transaction_describe(trans);
danw@137
   361
	if (trans->errors)
danw@137
   362
		return 1;
danw@137
   363
danw@137
   364
	set = razor_transaction_run(trans);
danw@137
   365
	razor_transaction_destroy(trans);
danw@129
   366
	razor_set_write(set, updated_repo_filename);
danw@129
   367
	razor_set_destroy(set);
danw@129
   368
	printf("wrote system-updated.repo\n");
danw@129
   369
danw@129
   370
	return 0;
danw@129
   371
}
danw@129
   372
krh@44
   373
static void
krh@44
   374
print_diff(const char *name,
krh@44
   375
	   const char *old_version, const char *new_version, void *data)
krh@44
   376
{
krh@44
   377
	if (old_version)
krh@44
   378
		printf("removing %s %s\n", name, old_version);
krh@44
   379
	else
krh@44
   380
		printf("install %s %s\n", name, new_version);
krh@44
   381
}
krh@44
   382
krh@44
   383
static int
krh@44
   384
command_diff(int argc, const char *argv[])
krh@44
   385
{
krh@44
   386
	struct razor_set *set, *updated;
krh@44
   387
krh@44
   388
	set = razor_set_open(repo_filename);
krh@44
   389
	updated = razor_set_open(updated_repo_filename);
krh@44
   390
	if (set == NULL || updated == NULL)
krh@44
   391
		return 1;
krh@44
   392
krh@44
   393
	razor_set_diff(set, updated, print_diff, NULL);	
krh@44
   394
krh@44
   395
	razor_set_destroy(set);
krh@44
   396
	razor_set_destroy(updated);
krh@44
   397
krh@44
   398
	return 0;
krh@44
   399
}
krh@44
   400
krh@74
   401
static int
krh@75
   402
command_import_rpms(int argc, const char *argv[])
krh@74
   403
{
krh@75
   404
	DIR *dir;
krh@75
   405
	struct dirent *de;
krh@75
   406
	struct razor_importer *importer;
krh@75
   407
	struct razor_set *set;
krh@77
   408
	struct razor_rpm *rpm;
krh@75
   409
	int len;
krh@75
   410
	char filename[256];
krh@75
   411
	const char *dirname = argv[0];
krh@75
   412
krh@75
   413
	if (dirname == NULL) {
krh@75
   414
		fprintf(stderr, "usage: razor import-rpms DIR\n");
krh@75
   415
		return -1;
krh@75
   416
	}
krh@75
   417
krh@75
   418
	dir = opendir(dirname);
krh@75
   419
	if (dir == NULL) {
krh@75
   420
		fprintf(stderr, "couldn't read dir %s\n", dirname);
krh@75
   421
		return -1;
krh@75
   422
	}
krh@75
   423
krh@75
   424
	importer = razor_importer_new();
krh@75
   425
krh@75
   426
	while (de = readdir(dir), de != NULL) {
krh@75
   427
		len = strlen(de->d_name);
krh@75
   428
		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
krh@75
   429
		    continue;
krh@75
   430
		snprintf(filename, sizeof filename,
krh@75
   431
			 "%s/%s", dirname, de->d_name);
krh@77
   432
		rpm = razor_rpm_open(filename);
krh@77
   433
		if (rpm == NULL) {
krh@77
   434
			fprintf(stderr,
krh@77
   435
				"failed to open rpm \"%s\"\n", filename);
krh@77
   436
			continue;
krh@77
   437
		}
krh@77
   438
		if (razor_importer_add_rpm(importer, rpm)) {
krh@75
   439
			fprintf(stderr, "couldn't import %s\n", filename);
krh@75
   440
			break;
krh@75
   441
		}
krh@77
   442
		razor_rpm_close(rpm);
krh@75
   443
	}
krh@75
   444
krh@75
   445
	if (de != NULL) {
krh@75
   446
		razor_importer_destroy(importer);
krh@75
   447
		return -1;
krh@75
   448
	}
krh@75
   449
krh@75
   450
	set = razor_importer_finish(importer);
krh@75
   451
krh@75
   452
	razor_set_write(set, repo_filename);
krh@75
   453
	razor_set_destroy(set);
krh@75
   454
	printf("wrote %s\n", repo_filename);
krh@74
   455
krh@74
   456
	return 0;
krh@74
   457
}
krh@74
   458
krh@152
   459
static struct razor_set *
krh@152
   460
create_set_from_rpms(int argc, const char *argv[])
krh@152
   461
{
krh@152
   462
	struct razor_importer *importer;
krh@152
   463
	struct razor_rpm *rpm;
krh@152
   464
	int i;
krh@152
   465
krh@152
   466
	importer = razor_importer_new();
krh@152
   467
	for (i = 0; i < argc; i++) {
krh@152
   468
		rpm = razor_rpm_open(argv[i]);
krh@152
   469
		if (rpm == NULL) {
krh@152
   470
			fprintf(stderr,
krh@152
   471
				"failed to open rpm \"%s\"\n", argv[i]);
krh@152
   472
			continue;
krh@152
   473
		}
krh@152
   474
		if (razor_importer_add_rpm(importer, rpm)) {
krh@152
   475
			fprintf(stderr, "couldn't import %s\n", argv[i]);
krh@152
   476
			break;
krh@152
   477
		}
krh@152
   478
		razor_rpm_close(rpm);
krh@152
   479
	}
krh@152
   480
krh@152
   481
	return razor_importer_finish(importer);
krh@152
   482
}
krh@152
   483
krh@152
   484
static char **
krh@152
   485
list_packages(int count, struct razor_set *set)
krh@152
   486
{
krh@152
   487
	struct razor_package_iterator *pi;
krh@152
   488
	struct razor_package *package;
krh@152
   489
	const char *name, *version;
krh@152
   490
	char **packages;
krh@152
   491
	int i;
krh@152
   492
krh@152
   493
	packages = malloc(count * sizeof *packages);
krh@152
   494
	pi = razor_package_iterator_create(set);
krh@152
   495
	i = 0;
krh@152
   496
	while (razor_package_iterator_next(pi, &package, &name, &version))
krh@152
   497
		packages[i] = strdup(name);
krh@152
   498
	razor_package_iterator_destroy(pi);
krh@152
   499
krh@152
   500
	return packages;
krh@152
   501
}
krh@151
   502
krh@77
   503
static int
krh@77
   504
command_install(int argc, const char *argv[])
krh@77
   505
{
krh@171
   506
	struct razor_set *system, *upstream, *next;
krh@152
   507
	struct razor_transaction *trans;
krh@77
   508
	struct razor_rpm *rpm;
krh@152
   509
	const char *filename;
krh@171
   510
	char path[PATH_MAX], new_path[PATH_MAX], **packages;
krh@152
   511
	int i;
krh@151
   512
krh@152
   513
	upstream = create_set_from_rpms(argc, argv);
krh@152
   514
	snprintf(path, sizeof path,
krh@152
   515
		 "%s%s/%s", root, razor_root_path, system_repo_filename);
krh@152
   516
	system = razor_set_open(path);
krh@152
   517
	if (system == NULL) {
krh@152
   518
		fprintf(stderr, "couldn't open system package database\n");
krh@151
   519
		return -1;
krh@151
   520
	}
krh@152
   521
krh@152
   522
	packages = list_packages(argc, upstream);
krh@152
   523
	trans = razor_transaction_create(system, upstream,
danw@155
   524
					 argc, (const char **)packages,
danw@155
   525
					 0, NULL);
danw@155
   526
	free(packages);
krh@152
   527
	razor_transaction_describe(trans);
krh@152
   528
	if (trans->errors)
krh@152
   529
		return 1;
krh@152
   530
krh@152
   531
	/* FIXME: Use _finish() convention here?  That is, a function
krh@152
   532
	 * that starts the computation and returns the result while
krh@152
   533
	 * destroying the transaction.  Nice for transient objects
krh@152
   534
	 * such as the merger and the importer.  Should we do that for
krh@152
   535
	 * transactions too, that is, razor_transaction_finish()? */
krh@171
   536
	next = razor_transaction_run(trans);
krh@152
   537
	razor_transaction_destroy(trans);
krh@152
   538
krh@171
   539
	/* FIXME: Need razor_set_write_to_fd() so we can open it excl
krh@171
   540
	 * up front here or fail if it already exists. */
krh@171
   541
	snprintf(new_path, sizeof new_path,
krh@171
   542
		 "%s%s/%s", root, razor_root_path, next_repo_filename);
krh@171
   543
	razor_set_write(next, path);
krh@171
   544
krh@171
   545
	razor_set_destroy(next);
krh@171
   546
	razor_set_destroy(system);
krh@171
   547
	razor_set_destroy(upstream);
krh@171
   548
krh@171
   549
	printf("wrote %s\n", new_path);
krh@171
   550
krh@152
   551
	for (i = 0; i < argc; i++) {
krh@152
   552
		filename = argv[i];
krh@152
   553
		rpm = razor_rpm_open(argv[i]);
krh@152
   554
		if (rpm == NULL) {
krh@152
   555
			fprintf(stderr, "failed to open rpm %s\n", filename);
krh@152
   556
			return -1;
krh@152
   557
		}
krh@152
   558
		if (razor_rpm_install(rpm, root) < 0) {
krh@152
   559
			fprintf(stderr,
krh@152
   560
				"failed to install rpm %s\n", filename);
krh@152
   561
			return -1;
krh@152
   562
		}
krh@152
   563
		razor_rpm_close(rpm);
krh@152
   564
	}	
krh@151
   565
krh@171
   566
	/* Make it so. */
krh@171
   567
	rename(new_path, path);
krh@171
   568
	printf("renamed %s to %s\n", new_path, path);
krh@171
   569
krh@151
   570
	return 0;
krh@151
   571
}
krh@151
   572
krh@151
   573
static int
krh@151
   574
command_init(int argc, const char *argv[])
krh@151
   575
{
krh@77
   576
	struct stat buf;
krh@151
   577
	struct razor_set *set;
krh@151
   578
	char path[PATH_MAX];
krh@77
   579
krh@77
   580
	if (stat(root, &buf) < 0) {
krh@77
   581
		if (mkdir(root, 0777) < 0) {
krh@77
   582
			fprintf(stderr,
krh@77
   583
				"could not create install root \"%s\"\n",
krh@77
   584
				root);
krh@77
   585
			return -1;
krh@77
   586
		}
krh@77
   587
		fprintf(stderr, "created install root \"%s\"\n", root);
krh@77
   588
	} else if (!S_ISDIR(buf.st_mode)) {
krh@77
   589
		fprintf(stderr,
krh@77
   590
			"install root \"%s\" exists, but is not a directory\n",
krh@77
   591
			root);
krh@77
   592
		return -1;
krh@77
   593
	}
krh@77
   594
krh@151
   595
	if (razor_create_dir(root, razor_root_path) < 0) {
krh@151
   596
		fprintf(stderr, "could not create %s%s\n",
krh@151
   597
			root, razor_root_path);
krh@77
   598
		return -1;
krh@77
   599
	}
krh@151
   600
krh@151
   601
	set = razor_set_create();
krh@151
   602
	snprintf(path, sizeof path, "%s%s/%s",
krh@152
   603
		 root, razor_root_path, system_repo_filename);
krh@151
   604
	if (razor_set_write(set, path) < 0) {
krh@151
   605
		fprintf(stderr, "could not write initial package set\n");
krh@77
   606
		return -1;
krh@77
   607
	}
krh@151
   608
	razor_set_destroy(set);
krh@77
   609
krh@77
   610
	return 0;
krh@77
   611
}
krh@77
   612
krh@43
   613
static struct {
krh@43
   614
	const char *name;
krh@43
   615
	const char *description;
krh@43
   616
	int (*func)(int argc, const char *argv[]);
krh@43
   617
} razor_commands[] = {
krh@43
   618
	{ "list", "list all packages", command_list },
krh@67
   619
	{ "list-requires", "list all requires for the given package", command_list_requires },
krh@75
   620
	{ "list-provides", "list all provides for the given package", command_list_provides },
krh@75
   621
	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
krh@75
   622
	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
krh@48
   623
	{ "list-files", "list files for package set", command_list_files },
krh@52
   624
	{ "list-file-packages", "list packages owning file", command_list_file_packages },
krh@56
   625
	{ "list-package-files", "list files in package", command_list_package_files },
krh@43
   626
	{ "what-requires", "list the packages that have the given requires", command_what_requires },
krh@43
   627
	{ "what-provides", "list the packages that have the given provides", command_what_provides },
krh@75
   628
	{ "import-yum", "import yum metadata files", command_import_yum },
krh@43
   629
	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
krh@75
   630
	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
krh@43
   631
	{ "validate", "validate a package set", command_validate },
krh@44
   632
	{ "update", "update all or specified packages", command_update },
danw@129
   633
	{ "remove", "remove specified packages", command_remove },
krh@77
   634
	{ "diff", "show diff between two package sets", command_diff },
krh@151
   635
	{ "install", "install rpm", command_install },
krh@151
   636
	{ "init", "init razor root", command_init }
krh@43
   637
};
krh@43
   638
krh@43
   639
static int
krh@43
   640
usage(void)
krh@43
   641
{
krh@43
   642
	int i;
krh@43
   643
krh@43
   644
	printf("usage:\n");
krh@43
   645
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
krh@43
   646
		printf("  %-20s%s\n",
krh@43
   647
		       razor_commands[i].name, razor_commands[i].description);
krh@43
   648
krh@43
   649
	return 1;
krh@43
   650
}
krh@43
   651
krh@43
   652
int
krh@43
   653
main(int argc, const char *argv[])
krh@43
   654
{
krh@43
   655
	char *repo;
krh@43
   656
	int i;
krh@43
   657
krh@43
   658
	repo = getenv("RAZOR_REPO");
krh@43
   659
	if (repo != NULL)
krh@43
   660
		repo_filename = repo;
krh@43
   661
krh@43
   662
	if (argc < 2)
krh@43
   663
		return usage();
krh@43
   664
krh@43
   665
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
krh@43
   666
		if (strcmp(razor_commands[i].name, argv[1]) == 0)
krh@43
   667
			return razor_commands[i].func(argc - 2, argv + 2);
krh@43
   668
krh@43
   669
	return usage();
krh@43
   670
}