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