src/main.c
author Richard Hughes <richard@hughsie.com>
Wed Jun 25 11:00:25 2008 -0400 (2008-06-25)
changeset 280 25407a621c95
parent 271 3980d1d9148e
child 281 d8d248598628
permissions -rw-r--r--
Error out if package isn't found instead of listing all properties.

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