main.c
author Kristian H?gsberg <krh@redhat.com>
Wed Jun 04 20:04:57 2008 -0400 (2008-06-04)
changeset 218 9aed412ed2b8
parent 211 cf0ca962262b
child 224 5803b6151d02
child 230 c1e2aed8dd07
permissions -rw-r--r--
Process all option structs that match a given arg.

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