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