rpm-razor.c
author Kristian H?gsberg <krh@redhat.com>
Mon Jun 09 16:38:58 2008 -0400 (2008-06-09)
changeset 235 060d83d8eca9
parent 230 c1e2aed8dd07
child 236 e4900c8df461
permissions -rw-r--r--
Add --initdb support for rpm wrapper.
krh@215
     1
/*
krh@215
     2
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
krh@215
     3
 * Copyright (C) 2008  Red Hat, Inc
krh@215
     4
 *
krh@215
     5
 * This program is free software; you can redistribute it and/or modify
krh@215
     6
 * it under the terms of the GNU General Public License as published by
krh@215
     7
 * the Free Software Foundation; either version 2 of the License, or
krh@215
     8
 * (at your option) any later version.
krh@215
     9
 *
krh@215
    10
 * This program is distributed in the hope that it will be useful,
krh@215
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
krh@215
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
krh@215
    13
 * GNU General Public License for more details.
krh@215
    14
 *
krh@215
    15
 * You should have received a copy of the GNU General Public License along
krh@215
    16
 * with this program; if not, write to the Free Software Foundation, Inc.,
krh@215
    17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
krh@215
    18
 */
krh@215
    19
krh@215
    20
#include <stdlib.h>
krh@215
    21
#include <string.h>
krh@215
    22
#include <stdio.h>
krh@215
    23
#include "razor.h"
krh@215
    24
krh@215
    25
enum option_type {
krh@215
    26
	OPTION_LAST,
krh@215
    27
	OPTION_GROUP,
krh@215
    28
	OPTION_BOOL,
krh@215
    29
	OPTION_STRING
krh@215
    30
};
krh@215
    31
krh@215
    32
struct option {
krh@215
    33
	enum option_type type;
krh@215
    34
	const char *name;
krh@215
    35
	char short_name;
krh@215
    36
	const char *arg_name;
krh@215
    37
	const char *description;
krh@215
    38
	void *data;
krh@215
    39
};
krh@215
    40
krh@218
    41
/* A note about all these options: rpm allows options to mean
krh@218
    42
 * different things depending on what other options are present on the
krh@218
    43
 * command line.  For example, if -q or --query is present, -i no
krh@218
    44
 * longer means install, but info.  The way we handle this is by
krh@218
    45
 * setting all the options that may match (ie if -i is given we set
krh@218
    46
 * install and info), and then look at the relevent one depending on
krh@218
    47
 * what else in on the command line. */
krh@218
    48
krh@216
    49
static int option_all, option_whatrequires, option_whatprovides;
krh@217
    50
static int option_package;
krh@215
    51
krh@215
    52
static const struct option query_options[] = {
krh@215
    53
	{ OPTION_BOOL, "configfiles", 'c', NULL, "list all configuration files", NULL },
krh@215
    54
	{ OPTION_BOOL, "docfiles", 'd', NULL, "list all documentation files", NULL },
krh@215
    55
	{ OPTION_BOOL, "dump", 0, NULL, "dump basic file information", NULL },
krh@215
    56
	{ OPTION_BOOL, "list", 0, NULL, "list files in package", NULL },
krh@215
    57
	{ OPTION_STRING, "queryformat", 0, "QUERYFORMAT", "use the following query format", NULL },
krh@215
    58
	{ OPTION_BOOL, "state", 's', NULL, "display the states of the listed files", NULL },
krh@215
    59
	{ OPTION_BOOL, "all", 'a', NULL, "query/verify all packages", &option_all },
krh@215
    60
	{ OPTION_BOOL, "file", 'f', NULL, "query/verify package(s) owning file", NULL },
krh@215
    61
	{ OPTION_BOOL, "group", 'g', NULL, "query/verify package(s) in group", NULL },
krh@217
    62
	{ OPTION_BOOL, "package", 'p', NULL, "query/verify a package file", &option_package },
krh@215
    63
	{ OPTION_BOOL, "ftswalk", 'W', NULL, "query/verify package(s) from TOP file tree walk", NULL },
krh@215
    64
	{ OPTION_BOOL, "pkgid", 0, NULL, "query/verify package(s) with package identifier", NULL },
krh@215
    65
	{ OPTION_BOOL, "hdrid", 0, NULL, "query/verify package(s) with header identifier", NULL },
krh@215
    66
	{ OPTION_BOOL, "fileid", 0, NULL, "query/verify package(s) with file identifier", NULL },
krh@215
    67
	{ OPTION_BOOL, "specfile", 0, NULL, "query a spec file", NULL, },
krh@215
    68
	{ OPTION_BOOL, "triggeredby", 0, NULL, "query the package(s) triggered by the package", NULL },
krh@216
    69
	{ OPTION_BOOL, "whatrequires", 0, NULL, "query/verify the package(s) which require a dependency", &option_whatrequires },
krh@216
    70
	{ OPTION_BOOL, "whatprovides", 0, NULL, "query/verify the package(s) which provide a dependency", &option_whatprovides },
krh@215
    71
	{ OPTION_BOOL, "nomanifest", 0, NULL, "do not process non-package files as manifests", NULL },
krh@215
    72
	{ }
krh@215
    73
};
krh@215
    74
krh@215
    75
static const struct option verify_options[] = {
krh@215
    76
	{ OPTION_BOOL, "nomd5", 0, NULL, "don't verify MD5 digest of files", NULL },
krh@215
    77
	{ OPTION_BOOL, "nofiles", 0, NULL, "don't verify files in package", NULL },
krh@215
    78
	{ OPTION_BOOL, "nodeps", 0, NULL, "don't verify package dependencies", NULL },
krh@215
    79
	{ OPTION_BOOL, "noscript", 0, NULL, "don't execute verify script(s)", NULL, },
krh@222
    80
	{ OPTION_BOOL, "all", 'a', NULL, "query/verify all packages", &option_all },
krh@215
    81
	{ OPTION_BOOL, "file", 'f', NULL, "query/verify package(s) owning file", NULL },
krh@215
    82
	{ OPTION_BOOL, "group", 'g', NULL, "query/verify package(s) in group", NULL },
krh@222
    83
	{ OPTION_BOOL, "package", 'p', NULL, "query/verify a package file", &option_package },
krh@215
    84
	{ OPTION_BOOL, "ftswalk", 'W', NULL, "query/verify package(s) from TOP file tree walk", NULL },
krh@215
    85
	{ OPTION_BOOL, "pkgid", 0, NULL, "query/verify package(s) with package identifier", NULL },
krh@215
    86
	{ OPTION_BOOL, "hdrid", 0, NULL, "query/verify package(s) with header identifier", NULL },
krh@215
    87
	{ OPTION_BOOL, "fileid", 0, NULL, "query/verify package(s) with file identifier", NULL },
krh@215
    88
	{ OPTION_BOOL, "specfile", 0, NULL, "query a spec file", NULL },
krh@215
    89
	{ OPTION_BOOL, "triggeredby", 0, NULL, "query the package(s) triggered by the package", NULL },
krh@216
    90
	{ OPTION_BOOL, "whatrequires", 0, NULL, "query/verify the package(s) which require a dependency", &option_whatrequires },
krh@216
    91
	{ OPTION_BOOL, "whatprovides", 0, NULL, "query/verify the package(s) which provide a dependency", &option_whatprovides },
krh@215
    92
	{ OPTION_BOOL, "nomanifest", 0, NULL, "do not process non-package files as manifests", NULL },
krh@215
    93
	{ }
krh@215
    94
};
krh@215
    95
krh@215
    96
static const struct option ftw_options[] = {
krh@215
    97
	{ OPTION_BOOL, "comfollow", 0, NULL, "FTS_COMFOLLOW: follow command line symlinks", NULL },
krh@215
    98
	{ OPTION_BOOL, "logical", 0, NULL, "FTS_LOGICAL: logical walk", NULL },
krh@215
    99
	{ OPTION_BOOL, "nochdir", 0, NULL, "FTS_NOCHDIR: don't change directories", NULL },
krh@215
   100
	{ OPTION_BOOL, "nostat", 0, NULL, "FTS_NOSTAT: don't get stat info", NULL },
krh@215
   101
	{ OPTION_BOOL, "physical", 0, NULL, "FTS_PHYSICAL: physical walk", NULL },
krh@215
   102
	{ OPTION_BOOL, "seedot", 0, NULL, "FTS_SEEDOT: return dot and dot-dot", NULL },
krh@215
   103
	{ OPTION_BOOL, "xdev", 0, NULL, "FTS_XDEV: don't cross devices", NULL },
krh@215
   104
	{ OPTION_BOOL, "whiteout", 0, NULL, "FTS_WHITEOUT: return whiteout information", NULL },
krh@215
   105
	{ }
krh@215
   106
};
krh@215
   107
krh@215
   108
static const struct option signature_options[] = {
krh@215
   109
	{ OPTION_BOOL, "addsign", 0, NULL, "sign package(s) (identical to --resign)", NULL, },
krh@215
   110
	{ OPTION_BOOL, "checksig", 'K', NULL, "verify package signature(s)", NULL, },
krh@215
   111
	{ OPTION_BOOL, "delsign", 0, NULL, "delete package signatures", NULL, },
krh@215
   112
	{ OPTION_BOOL, "import", 0, NULL, "import an armored public key", NULL, },
krh@215
   113
	{ OPTION_BOOL, "resign", 0, NULL, "sign package(s) (identical to --addsign)", NULL, },
krh@215
   114
	{ OPTION_BOOL, "nodigest", 0, NULL, "don't verify package digest(s)", NULL, },
krh@215
   115
	{ OPTION_BOOL, "nosignature", 0, NULL, "don't verify package signature(s)", NULL },
krh@215
   116
	{ }
krh@215
   117
};
krh@215
   118
krh@235
   119
static int option_initdb;
krh@235
   120
krh@215
   121
static const struct option database_options[] = {
krh@235
   122
	{ OPTION_BOOL, "initdb", 0, NULL, "initialize database", &option_initdb },
krh@215
   123
	{ OPTION_BOOL, "rebuilddb", 0, NULL, "rebuild database inverted lists from installed package headers", NULL },
krh@215
   124
	{ }
krh@215
   125
};
krh@215
   126
krh@215
   127
static int option_erase, option_install, option_upgrade;
krh@215
   128
krh@215
   129
static const struct option install_options[] = {
krh@215
   130
	{ OPTION_BOOL, "aid", 0, NULL, "add suggested packages to transaction", NULL, },
krh@215
   131
	{ OPTION_BOOL, "allfiles", 0, NULL, "install all files, even configurations which might otherwise be skipped", NULL, },
krh@215
   132
	{ OPTION_BOOL, "allmatches", 0, NULL, "remove all packages which match <package> (normally an error is generated if <package> specified multiple packages)", NULL, },
krh@215
   133
	{ OPTION_BOOL, "badreloc", 0, NULL, "relocate files in non-relocatable package", NULL },
krh@215
   134
	{ OPTION_BOOL, "erase", 'e', "<package>", "erase (uninstall) package", &option_erase },
krh@215
   135
	{ OPTION_BOOL, "excludedocs", 0, NULL, "do not install documentation", NULL, },
krh@215
   136
	{ OPTION_BOOL, "excludepath", 0, "<path>", "skip files with leading component <path> ", NULL, },
krh@215
   137
	{ OPTION_BOOL, "fileconflicts", 0, NULL, "detect file conflicts between packages", NULL, },
krh@215
   138
	{ OPTION_BOOL, "force", 0, NULL, "short hand for --replacepkgs --replacefiles", NULL },
krh@215
   139
	{ OPTION_BOOL, "freshen", 'F', "<packagefile>+", "upgrade package(s) if already installed", NULL },
krh@215
   140
	{ OPTION_BOOL, "hash", 'h', NULL, "print hash marks as package installs (good with -v)", NULL },
krh@215
   141
	{ OPTION_BOOL, "ignorearch", 0, NULL, "don't verify package architecture", NULL, },
krh@215
   142
	{ OPTION_BOOL, "ignoreos", 0, NULL, "don't verify package operating system", NULL, },
krh@215
   143
	{ OPTION_BOOL, "ignoresize", 0, NULL, "don't check disk space before installing", NULL },
krh@215
   144
	{ OPTION_BOOL, "install", 'i', NULL, "install package(s)", &option_install },
krh@215
   145
	{ OPTION_BOOL, "justdb", 0, NULL, "update the database, but do not modify the filesystem", NULL, },
krh@215
   146
	{ OPTION_BOOL, "nodeps", 0, NULL, "do not verify package dependencies", NULL, },
krh@215
   147
	{ OPTION_BOOL, "nomd5", 0, NULL, "don't verify MD5 digest of files", NULL, },
krh@215
   148
	{ OPTION_BOOL, "nocontexts", 0, NULL, "don't install file security contexts", NULL, },
krh@215
   149
	{ OPTION_BOOL, "noorder", 0, NULL, "do not reorder package installation to satisfy dependencies", NULL, },
krh@215
   150
	{ OPTION_BOOL, "nosuggest", 0, NULL, "do not suggest missing dependency resolution(s)", NULL, },
krh@215
   151
	{ OPTION_BOOL, "noscripts", 0, NULL, "do not execute package scriptlet(s)", NULL, },
krh@215
   152
	{ OPTION_BOOL, "notriggers", 0, NULL, "do not execute any scriptlet(s) triggered by this package", NULL, },
krh@215
   153
	{ OPTION_BOOL, "oldpackage", 0, NULL, "upgrade to an old version of the package (--force on upgrades does this automatically)", NULL },
krh@215
   154
	{ OPTION_BOOL, "percent", 0, NULL, "print percentages as package installs", NULL, },
krh@215
   155
	{ OPTION_STRING, "prefix", 0, "<dir>", "relocate the package to <dir>, if relocatable", NULL, },
krh@215
   156
	{ OPTION_STRING, "relocate", 0, "<old>=<new>", "relocate files from path <old> to <new>", NULL, },
krh@215
   157
	{ OPTION_BOOL, "repackage", 0, NULL, "save erased package files by repackaging", NULL, },
krh@215
   158
	{ OPTION_BOOL, "replacefiles", 0, NULL, "ignore file conflicts between packages", NULL, },
krh@215
   159
	{ OPTION_BOOL, "replacepkgs", 0, NULL, "reinstall if the package is already present", NULL, },
krh@215
   160
	{ OPTION_BOOL, "test", 0, NULL, "don't install, but tell if it would work or not", NULL },
krh@215
   161
	{ OPTION_BOOL, "upgrade", 'U', "<packagefile>+", "upgrade package(s)", &option_upgrade },
krh@215
   162
	{ }
krh@215
   163
};
krh@215
   164
krh@215
   165
static int option_version;
krh@215
   166
krh@215
   167
static const struct option common_options[] = {
krh@215
   168
	{ OPTION_STRING, "define", 'D', "MACRO EXPR", "define MACRO with value EXPR", NULL, },
krh@215
   169
	{ OPTION_STRING, "eval", 'E', "EXPR", "print macro expansion of EXPR", NULL },
krh@215
   170
	{ OPTION_STRING, "macros", 0, "<FILE:...>", "read <FILE:...> instead of default file(s)", NULL },
krh@215
   171
	{ OPTION_BOOL, "nodigest", 0, NULL, "don't verify package digest(s)", NULL, },
krh@215
   172
	{ OPTION_BOOL, "nosignature", 0, NULL, "don't verify package signature(s)", NULL, },
krh@215
   173
	{ OPTION_STRING, "rcfile", 0, "<FILE:...>", "read <FILE:...> instead of default file(s)", NULL },
krh@215
   174
	{ OPTION_STRING, "root", 'r', "ROOT", "use ROOT as top level directory (default: \"/\")", NULL },
krh@215
   175
	{ OPTION_BOOL, "querytags", 0, NULL, "display known query tags", NULL, },
krh@215
   176
	{ OPTION_BOOL, "showrc", 0, NULL, "display final rpmrc and macro configuration", NULL, },
krh@215
   177
	{ OPTION_BOOL, "quiet", 0, NULL, "provide less detailed output", NULL },
krh@215
   178
	{ OPTION_BOOL, "verbose", 'v', NULL, "provide more detailed output", NULL },
krh@215
   179
	{ OPTION_BOOL, "version", 0, NULL, "print the version of rpm being used", &option_version },
krh@215
   180
	{ }
krh@215
   181
};
krh@215
   182
krh@218
   183
static int option_conflicts, option_obsoletes, option_requires;
krh@218
   184
static int option_provides, option_info, option_changelog;
krh@216
   185
krh@215
   186
static const struct option alias_options[] = {
krh@215
   187
	{ OPTION_BOOL, "scripts", 0, NULL, "list install/erase scriptlets from package(s)", NULL, },
krh@215
   188
	{ OPTION_BOOL, "setperms", 0, NULL, "set permissions of files in a package", NULL, },
krh@215
   189
	{ OPTION_BOOL, "setugids", 0, NULL, "set user/group ownership of files in a package", NULL, },
krh@216
   190
	{ OPTION_BOOL, "conflicts", 0, NULL, "list capabilities this package conflicts with", &option_conflicts, },
krh@216
   191
	{ OPTION_BOOL, "obsoletes", 0, NULL, "list other packages removed by installing this package", &option_obsoletes, },
krh@216
   192
	{ OPTION_BOOL, "provides", 0, NULL, "list capabilities that this package provides", &option_provides, },
krh@216
   193
	{ OPTION_BOOL, "requires", 0, NULL, "list capabilities required by package(s)", &option_requires, },
krh@218
   194
	{ OPTION_BOOL, "info", 'i', NULL, "list descriptive information from package(s)", &option_info, },
krh@218
   195
	{ OPTION_BOOL, "changelog", 0, NULL, "list change logs for this package", &option_changelog, },
krh@215
   196
	{ OPTION_BOOL, "xml", 0, NULL, "list metadata in xml", NULL, },
krh@215
   197
	{ OPTION_BOOL, "triggers", 0, NULL, "list trigger scriptlets from package(s)", NULL, },
krh@215
   198
	{ OPTION_BOOL, "last", 0, NULL, "list package(s) by install time, most recent first", NULL, },
krh@215
   199
	{ OPTION_BOOL, "dupes", 0, NULL, "list duplicated packages", NULL, },
krh@215
   200
	{ OPTION_BOOL, "filesbypkg", 0, NULL, "list all files from each package", NULL, },
krh@215
   201
	{ OPTION_BOOL, "fileclass", 0, NULL, "list file names with classes", NULL, },
krh@215
   202
	{ OPTION_BOOL, "filecolor", 0, NULL, "list file names with colors", NULL, },
krh@215
   203
	{ OPTION_BOOL, "filecontext", 0, NULL, "list file names with security context from header", NULL, },
krh@215
   204
	{ OPTION_BOOL, "fscontext", 0, NULL, "list file names with security context from file system", NULL, },
krh@215
   205
	{ OPTION_BOOL, "recontext", 0, NULL, "list file names with security context from policy RE", NULL, },
krh@215
   206
	{ OPTION_BOOL, "fileprovide", 0, NULL, "list file names with provides", NULL, },
krh@215
   207
	{ OPTION_BOOL, "filerequire", 0, NULL, "list file names with requires", NULL, },
krh@215
   208
	{ OPTION_BOOL, "redhatprovides", 0, NULL, "find package name that contains a provided capability (needs rpmdb-redhat package installed)", NULL, },
krh@215
   209
	{ OPTION_BOOL, "redhatrequires", 0, NULL, "find package name that contains a required capability (needs rpmdb-redhat package installed)", NULL, },
krh@215
   210
	{ OPTION_STRING, "buildpolicy", 0, "<policy>", "set buildroot <policy> (e.g. compress man pages)", NULL, },
krh@215
   211
	{ OPTION_BOOL, "with", 0, "<option>", "enable configure <option> for build", NULL, },
krh@215
   212
	{ OPTION_BOOL, "without", 0, "<option>", "disable configure <option> for build", NULL },
krh@215
   213
	{ }
krh@215
   214
};
krh@215
   215
krh@215
   216
static int option_help, option_usage;
krh@215
   217
krh@215
   218
static const struct option help_options[] = {
krh@215
   219
	{ OPTION_BOOL, "help", '?', NULL, "Show this help message", &option_help },
krh@215
   220
	{ OPTION_BOOL, "usage", 0, NULL, "Display brief usage message", &option_usage},
krh@215
   221
	{ }
krh@215
   222
};
krh@215
   223
krh@215
   224
static int option_query, option_verify;
krh@215
   225
krh@215
   226
static const struct option rpm_options[] = {
krh@215
   227
	{ OPTION_BOOL, "query", 'q', NULL, "Query rpm database", &option_query },
krh@215
   228
	{ OPTION_BOOL, "verify", 'V', NULL, "Verify rpm database", &option_verify },
krh@215
   229
	{ OPTION_GROUP, NULL, 0, NULL, "Query options (with -q or --query):", &query_options },
krh@215
   230
	{ OPTION_GROUP, NULL, 0, NULL, "Verify options (with -V or --verify):", &verify_options },
krh@215
   231
	{ OPTION_GROUP, NULL, 0, NULL, "File tree walk options (with --ftswalk):", &ftw_options },
krh@215
   232
	{ OPTION_GROUP, NULL, 0, NULL, "Signature options:", &signature_options },
krh@215
   233
	{ OPTION_GROUP, NULL, 0, NULL, "Database options:", &database_options },
krh@215
   234
	{ OPTION_GROUP, NULL, 0, NULL, "Install/Upgrade/Erase options:", &install_options },
krh@215
   235
	{ OPTION_GROUP, NULL, 0, NULL, "Common options for all rpm modes and executables:", &common_options },
krh@215
   236
	{ OPTION_GROUP, NULL, 0, NULL, "Options implemented via popt alias/exec:", &alias_options },
krh@215
   237
	{ OPTION_GROUP, NULL, 0, NULL, "Help options", &help_options },
krh@215
   238
	{ }
krh@215
   239
};
krh@215
   240
krh@216
   241
static const char system_repo_filename[] = "system.repo";
krh@216
   242
static const char *repo_filename = system_repo_filename;
krh@235
   243
static const char install_root[] = "install";
krh@235
   244
krh@235
   245
static void
krh@235
   246
command_initdb(int argc, const char *argv[])
krh@235
   247
{
krh@235
   248
	razor_root_create(install_root);
krh@235
   249
}
krh@216
   250
krh@216
   251
static struct razor_property *
krh@216
   252
add_property_packages(struct razor_set *set, 
krh@216
   253
		      struct razor_package_query *query,
krh@216
   254
		      const char *ref_name,
krh@216
   255
		      const char *ref_version,
krh@216
   256
		      enum razor_property_type ref_type)
krh@216
   257
{
krh@216
   258
	struct razor_property *property;
krh@216
   259
	struct razor_property_iterator *pi;
krh@216
   260
	struct razor_package_iterator *pkgi;
krh@216
   261
	const char *name, *version;
krh@216
   262
	enum razor_property_type type;
krh@216
   263
	enum razor_version_relation relation;
krh@216
   264
krh@216
   265
	pi = razor_property_iterator_create(set, NULL);
krh@216
   266
	while (razor_property_iterator_next(pi, &property, &name,
krh@216
   267
					    &relation, &version, &type)) {
krh@216
   268
		if (strcmp(ref_name, name) != 0)
krh@216
   269
			continue;
krh@216
   270
		if (ref_version && relation == RAZOR_VERSION_EQUAL &&
krh@216
   271
		    strcmp(ref_version, version) != 0)
krh@216
   272
			continue;
krh@216
   273
		if (ref_type != type)
krh@216
   274
			continue;
krh@216
   275
krh@216
   276
		pkgi = razor_package_iterator_create_for_property(set,
krh@216
   277
								  property);
krh@216
   278
		razor_package_query_add_iterator(query, pkgi);
krh@216
   279
		razor_package_iterator_destroy(pkgi);
krh@216
   280
	}
krh@216
   281
	razor_property_iterator_destroy(pi);
krh@216
   282
krh@216
   283
	return property;
krh@216
   284
}
krh@216
   285
krh@216
   286
static int
krh@216
   287
strcmpp(const void *p1, const void *p2)
krh@216
   288
{
krh@216
   289
	return strcmp(*(char * const *) p1, *(char * const *) p2);
krh@216
   290
}
krh@216
   291
krh@216
   292
static void
krh@216
   293
add_command_line_packages(struct razor_set *set,
krh@216
   294
			  struct razor_package_query *query,
krh@216
   295
			  int argc, const char **argv)
krh@216
   296
{
krh@216
   297
	struct razor_package *package;
krh@216
   298
	struct razor_package_iterator *pi;
krh@216
   299
	const char *name, *version, *arch;
krh@216
   300
	int i, cmp;
krh@216
   301
krh@216
   302
	qsort(argv, argc, sizeof(*argv), strcmpp);
krh@216
   303
	i = 0;
krh@216
   304
krh@216
   305
	pi = razor_package_iterator_create(set);
krh@216
   306
krh@216
   307
	while (razor_package_iterator_next(pi, &package,
krh@216
   308
					   &name, &version, &arch)) {
krh@216
   309
		while (cmp = strcmp(argv[i], name), cmp < 0 && i < argc) {
krh@216
   310
			printf("package %s is not installed\n", argv[i]);
krh@216
   311
			i++;
krh@216
   312
		}
krh@216
   313
krh@216
   314
		if (cmp == 0) {
krh@216
   315
			razor_package_query_add_package(query, package);
krh@216
   316
			i++;
krh@216
   317
		}
krh@216
   318
	}
krh@216
   319
krh@216
   320
	razor_package_iterator_destroy(pi);
krh@216
   321
}
krh@216
   322
krh@222
   323
static struct razor_package_iterator *
krh@222
   324
get_query_packages(struct razor_set *set, int argc, const char *argv[])
krh@222
   325
{
krh@222
   326
	struct razor_package_query *query;
krh@222
   327
	struct razor_package_iterator *pi;
krh@222
   328
	int i;
krh@222
   329
krh@222
   330
	if (option_all + option_whatprovides + option_whatrequires > 1) {
krh@222
   331
		printf("only one type of query/verify "
krh@222
   332
		       "may be performed at a time\n");
krh@222
   333
		exit(1);
krh@222
   334
	}
krh@222
   335
krh@222
   336
	query = razor_package_query_create(set);
krh@222
   337
krh@222
   338
	if (option_all) {
krh@222
   339
		pi = razor_package_iterator_create(set);
krh@222
   340
		razor_package_query_add_iterator(query, pi);
krh@222
   341
		razor_package_iterator_destroy(pi);
krh@222
   342
	} else if (option_whatrequires) {
krh@222
   343
		for (i = 0; i < argc; i++)
krh@222
   344
			add_property_packages(set, query,
krh@222
   345
					      argv[i], NULL,
krh@222
   346
					      RAZOR_PROPERTY_REQUIRES);
krh@222
   347
	} else if (option_whatprovides) {
krh@222
   348
		for (i = 0; i < argc; i++)
krh@222
   349
			add_property_packages(set, query,
krh@222
   350
					      argv[i], NULL,
krh@222
   351
					      RAZOR_PROPERTY_PROVIDES);
krh@222
   352
	} else if (argc > 0) {
krh@222
   353
		add_command_line_packages(set, query, argc, argv);
krh@222
   354
	} else {
krh@222
   355
		printf("no arguments given for query/verify\n");
krh@222
   356
		exit(1);
krh@222
   357
	}
krh@222
   358
krh@222
   359
	return razor_package_query_finish(query);
krh@222
   360
}
krh@222
   361
krh@230
   362
static const char *relation_string[] = { "<", "<=", "=", ">=", ">" };
krh@230
   363
krh@216
   364
static void
krh@216
   365
print_package_properties(struct razor_set *set,
krh@216
   366
			 struct razor_package *package,
krh@216
   367
			 enum razor_property_type ref_type)
krh@216
   368
{
krh@216
   369
	struct razor_property *property;
krh@216
   370
	struct razor_property_iterator *pi;
krh@216
   371
	const char *name, *version;
krh@216
   372
	enum razor_property_type type;
krh@216
   373
	enum razor_version_relation relation;
krh@216
   374
krh@216
   375
	pi = razor_property_iterator_create(set, package);
krh@216
   376
	while (razor_property_iterator_next(pi, &property,
krh@216
   377
					    &name, &relation, &version,
krh@216
   378
					    &type)) {
krh@216
   379
		if (type != ref_type)
krh@216
   380
			continue;
krh@216
   381
		if (version[0] == '\0')
krh@216
   382
			printf("%s\n", name);
krh@216
   383
		else
krh@216
   384
			printf("%s %s %s\n", name,
krh@230
   385
			       relation_string[relation], version);
krh@216
   386
	}
krh@216
   387
	razor_property_iterator_destroy(pi);
krh@216
   388
}
krh@216
   389
krh@218
   390
static void
krh@218
   391
print_package_info(struct razor_set *set, struct razor_package *package)
krh@218
   392
{
krh@218
   393
	printf("FIXME: Package info not tracked.\n");
krh@218
   394
}
krh@218
   395
krh@218
   396
static void
krh@218
   397
print_package_changelog(struct razor_set *set, struct razor_package *package)
krh@218
   398
{
krh@218
   399
	printf("FIXME: Package changelog not tracked.\n");
krh@218
   400
}
krh@218
   401
krh@217
   402
static struct razor_set *
krh@217
   403
create_set_from_command_line(int argc, const char *argv[])
krh@217
   404
{
krh@217
   405
	struct razor_importer *importer;
krh@217
   406
	struct razor_rpm *rpm;
krh@217
   407
	int i;
krh@217
   408
krh@217
   409
	importer = razor_importer_new();
krh@217
   410
krh@217
   411
	for (i = 0; i < argc; i++) {
krh@217
   412
		rpm = razor_rpm_open(argv[i]);
krh@217
   413
		if (rpm == NULL)
krh@217
   414
			continue;
krh@217
   415
		if (razor_importer_add_rpm(importer, rpm))
krh@217
   416
			printf("couldn't import %s\n", argv[i]);
krh@217
   417
krh@217
   418
		razor_rpm_close(rpm);
krh@217
   419
	}
krh@217
   420
krh@217
   421
	return razor_importer_finish(importer);
krh@217
   422
}
krh@217
   423
krh@215
   424
static void
krh@215
   425
command_query(int argc, const char *argv[])
krh@215
   426
{
krh@216
   427
	struct razor_set *set;
krh@216
   428
	struct razor_package_iterator *pi;
krh@216
   429
	struct razor_package *package;
krh@216
   430
	const char *name, *version, *arch;
krh@216
   431
krh@217
   432
	if (option_package) {
krh@217
   433
		set = create_set_from_command_line(argc, argv);
krh@217
   434
		argc = 0;
krh@217
   435
		option_all = 1;
krh@217
   436
	} else {
krh@217
   437
		set = razor_set_open(repo_filename);
krh@217
   438
	}
krh@216
   439
krh@222
   440
	pi = get_query_packages(set, argc, argv);
krh@216
   441
krh@216
   442
	while (razor_package_iterator_next(pi, &package,
krh@216
   443
					   &name, &version, &arch)) {
krh@216
   444
		if (option_conflicts)
krh@216
   445
			print_package_properties(set, package,
krh@216
   446
						 RAZOR_PROPERTY_CONFLICTS);
krh@216
   447
		if (option_obsoletes)
krh@216
   448
			print_package_properties(set, package,
krh@216
   449
						 RAZOR_PROPERTY_OBSOLETES);
krh@216
   450
		if (option_requires)
krh@216
   451
			print_package_properties(set, package,
krh@216
   452
						 RAZOR_PROPERTY_REQUIRES);
krh@216
   453
		if (option_provides)
krh@216
   454
			print_package_properties(set, package,
krh@216
   455
						 RAZOR_PROPERTY_PROVIDES);
krh@218
   456
		if (option_info)
krh@218
   457
			print_package_info(set, package);
krh@218
   458
		if (option_changelog)
krh@218
   459
			print_package_changelog(set, package);
krh@218
   460
krh@218
   461
		if (option_conflicts + option_obsoletes +
krh@218
   462
		    option_requires + option_provides +
krh@218
   463
		    option_info + option_changelog == 0)
krh@216
   464
			printf("%s-%s.%s\n", name, version, arch);
krh@216
   465
	}
krh@216
   466
krh@216
   467
	razor_package_iterator_destroy(pi);
krh@216
   468
krh@216
   469
	razor_set_destroy(set);
krh@216
   470
krh@216
   471
	return;
krh@215
   472
}
krh@215
   473
krh@215
   474
static void
krh@215
   475
command_verify(int argc, const char *argv[])
krh@215
   476
{
krh@222
   477
	struct razor_set *set;
krh@222
   478
	struct razor_package_iterator *pi;
krh@222
   479
	struct razor_package *package;
krh@222
   480
	const char *name, *version, *arch;
krh@222
   481
krh@222
   482
	if (option_package) {
krh@222
   483
		set = create_set_from_command_line(argc, argv);
krh@222
   484
		argc = 0;
krh@222
   485
		option_all = 1;
krh@222
   486
	} else {
krh@222
   487
		set = razor_set_open(repo_filename);
krh@215
   488
	}
krh@215
   489
krh@222
   490
	pi = get_query_packages(set, argc, argv);
krh@222
   491
krh@222
   492
	while (razor_package_iterator_next(pi, &package,
krh@222
   493
					   &name, &version, &arch)) {
krh@222
   494
		printf("verify %s-%s.%s - not implemented\n",
krh@222
   495
		       name, version, arch);
krh@222
   496
	}
krh@222
   497
krh@222
   498
	razor_package_iterator_destroy(pi);
krh@215
   499
}
krh@215
   500
krh@215
   501
static void
krh@215
   502
command_erase(int argc, const char *argv[])
krh@215
   503
{
krh@221
   504
	struct razor_set *set, *next;
krh@220
   505
	struct razor_transaction *trans;
krh@220
   506
	struct razor_package_query *query;
krh@220
   507
	struct razor_package_iterator *pi;
krh@220
   508
	struct razor_package *package;
krh@220
   509
	const char *name, *version, *arch;
krh@220
   510
krh@215
   511
	if (argc == 0) {
krh@215
   512
		printf("no packages given for erase\n");
krh@215
   513
		exit(1);
krh@215
   514
	}
krh@215
   515
krh@220
   516
	set = razor_set_open(repo_filename);
krh@220
   517
krh@220
   518
	trans = razor_transaction_create(set, NULL);
krh@220
   519
krh@220
   520
	query = razor_package_query_create(set);
krh@220
   521
	add_command_line_packages(set, query, argc, argv);
krh@221
   522
krh@220
   523
	pi = razor_package_query_finish(query);
krh@220
   524
	while (razor_package_iterator_next(pi, &package,
krh@220
   525
					   &name, &version, &arch))
krh@220
   526
		razor_transaction_remove_package(trans, package);
krh@220
   527
	razor_package_iterator_destroy(pi);
krh@220
   528
krh@221
   529
	next = razor_transaction_finish(trans);
krh@221
   530
	razor_set_destroy(set);
krh@220
   531
krh@221
   532
	razor_set_destroy(next);
krh@215
   533
}
krh@215
   534
krh@215
   535
static void
krh@215
   536
command_install(int argc, const char *argv[])
krh@215
   537
{
krh@221
   538
	struct razor_set *set, *upstream, *next;
krh@221
   539
	struct razor_transaction *trans;
krh@221
   540
	struct razor_package_iterator *pi;
krh@221
   541
	struct razor_package *package;
krh@221
   542
	const char *name, *version, *arch;
krh@221
   543
krh@215
   544
	if (argc == 0) {
krh@215
   545
		printf("no packages given for install\n");
krh@215
   546
		exit(1);
krh@215
   547
	}
krh@215
   548
krh@221
   549
	set = razor_set_open(repo_filename);
krh@221
   550
	upstream = create_set_from_command_line(argc, argv);
krh@221
   551
krh@221
   552
	trans = razor_transaction_create(set, upstream);
krh@221
   553
krh@221
   554
	pi = razor_package_iterator_create(upstream);
krh@221
   555
	while (razor_package_iterator_next(pi, &package,
krh@221
   556
					   &name, &version, &arch))
krh@221
   557
		razor_transaction_install_package(trans, package);
krh@221
   558
	razor_package_iterator_destroy(pi);
krh@221
   559
krh@221
   560
	next = razor_transaction_finish(trans);
krh@221
   561
	razor_set_destroy(set);
krh@221
   562
	razor_set_destroy(upstream);
krh@221
   563
krh@221
   564
	razor_set_destroy(next);
krh@215
   565
}
krh@215
   566
krh@215
   567
static void
krh@215
   568
command_update(int argc, const char *argv[])
krh@215
   569
{
krh@215
   570
	if (argc == 0) {
krh@215
   571
		printf("no packages given for update\n");
krh@215
   572
		exit(1);
krh@215
   573
	}
krh@215
   574
krh@215
   575
	printf("command update - not implemented\n");
krh@215
   576
}
krh@215
   577
krh@218
   578
static int
krh@219
   579
for_each_option(const struct option *options,
krh@219
   580
		const char *name, char short_name,
krh@218
   581
		void (*fn)(const struct option *o,
krh@219
   582
			   const char *name, char short_name,
krh@219
   583
			   void *data), void *data)
krh@215
   584
{
krh@218
   585
	int i, count = 0;
krh@215
   586
krh@215
   587
	for (i = 0; options[i].type != OPTION_LAST; i++) {
krh@215
   588
		switch (options[i].type) {
krh@215
   589
		case OPTION_GROUP:
krh@219
   590
			count += for_each_option(options[i].data,
krh@219
   591
						 name, short_name, fn, data);
krh@215
   592
			break;
krh@215
   593
krh@215
   594
		case OPTION_BOOL:
krh@215
   595
		case OPTION_STRING:
krh@219
   596
			if (name && strcmp(options[i].name, name) == 0) {
krh@219
   597
				fn(&options[i], name, 0, data);
krh@218
   598
				count++;
krh@219
   599
				break;
krh@218
   600
			}
krh@219
   601
krh@219
   602
			if (short_name &&
krh@219
   603
			    short_name == options[i].short_name) {
krh@219
   604
				fn(&options[i], NULL, short_name, data);
krh@218
   605
				count++;
krh@219
   606
				break;
krh@218
   607
			}
krh@215
   608
			break;
krh@215
   609
krh@215
   610
		case OPTION_LAST:
krh@215
   611
			break;
krh@215
   612
		}
krh@215
   613
	}
krh@215
   614
krh@218
   615
	return count;
krh@218
   616
}
krh@218
   617
krh@218
   618
static void
krh@219
   619
handle_option(const struct option *o,
krh@219
   620
	      const char *name, char short_name, void *data)
krh@218
   621
{
krh@218
   622
	if (o->data == NULL) {
krh@219
   623
		if (name)
krh@219
   624
			printf("option --%s not supported\n", name);
krh@219
   625
		else
krh@219
   626
			printf("option -%c not supported\n", short_name);
krh@218
   627
		return;
krh@218
   628
	}
krh@218
   629
krh@218
   630
	switch (o->type) {
krh@218
   631
	case OPTION_BOOL:
krh@218
   632
		*(int *) o->data = 1;
krh@218
   633
		break;
krh@218
   634
krh@218
   635
	case OPTION_STRING:
krh@219
   636
		*(const char **) o->data = name + strlen(o->name) + 1;
krh@218
   637
		break;
krh@218
   638
krh@218
   639
	case OPTION_LAST:
krh@218
   640
	case OPTION_GROUP:
krh@218
   641
		/* Shouldn't happen. */
krh@218
   642
		break;
krh@218
   643
	}
krh@215
   644
}
krh@215
   645
krh@215
   646
static int
krh@215
   647
parse_options(const struct option *options, int argc, const char **argv)
krh@215
   648
{
krh@219
   649
	int i, j, k;
krh@215
   650
krh@215
   651
	for (i = 1, j = 0; i < argc; i++) {
krh@215
   652
		if (argv[i][0] != '-') {
krh@215
   653
			argv[j++] = argv[i];
krh@215
   654
			continue;
krh@215
   655
		}
krh@218
   656
krh@219
   657
		if (argv[i][1] == '-') {
krh@219
   658
			if (for_each_option(options, &argv[i][2], 0,
krh@219
   659
					    handle_option, NULL) == 0) {
krh@219
   660
				printf("unknown option: %s\n", argv[i]);
krh@219
   661
				exit(1);
krh@219
   662
			}
krh@219
   663
			continue;
krh@219
   664
		}
krh@219
   665
krh@219
   666
		for (k = 1; argv[i][k]; k++) {
krh@219
   667
			if (for_each_option(options, NULL, argv[i][k],
krh@219
   668
					    handle_option, NULL) == 0) {
krh@219
   669
				printf("unknown option: -%c\n", argv[i][k]);
krh@219
   670
				exit(1);
krh@219
   671
			}
krh@215
   672
		}
krh@215
   673
	}
krh@215
   674
krh@215
   675
	return j;
krh@215
   676
}
krh@215
   677
krh@215
   678
static void
krh@215
   679
print_options_help(const struct option *options)
krh@215
   680
{
krh@215
   681
	int i;
krh@215
   682
krh@215
   683
	for (i = 0; options[i].type != OPTION_LAST; i++) {
krh@215
   684
		switch (options[i].type) {
krh@215
   685
		case OPTION_GROUP:
krh@215
   686
			printf("%s\n", options[i].description);
krh@215
   687
			print_options_help(options[i].data);
krh@215
   688
			printf("\n");
krh@215
   689
			break;
krh@215
   690
krh@215
   691
		case OPTION_BOOL:
krh@215
   692
		case OPTION_STRING:
krh@215
   693
			printf("  ");
krh@215
   694
			if (options[i].short_name)
krh@215
   695
				printf("-%c", options[i].short_name);
krh@215
   696
			if (options[i].short_name && options[i].name)
krh@215
   697
				printf(", ");
krh@215
   698
			if (options[i].name)
krh@215
   699
				printf("--%s", options[i].name);
krh@215
   700
			if (options[i].arg_name)
krh@215
   701
				printf("=%s", options[i].arg_name);
krh@215
   702
			if (options[i].description)
krh@215
   703
				printf("\t\t%s", options[i].description);
krh@215
   704
			printf("\n");
krh@215
   705
			break;
krh@215
   706
krh@215
   707
		case OPTION_LAST:
krh@215
   708
			break;
krh@215
   709
		}
krh@215
   710
	}
krh@215
   711
}
krh@215
   712
krh@215
   713
static void
krh@215
   714
print_options_usage(const struct option *options)
krh@215
   715
{
krh@215
   716
	int i;
krh@215
   717
krh@215
   718
	for (i = 0; options[i].type != OPTION_LAST; i++) {
krh@215
   719
		switch (options[i].type) {
krh@215
   720
		case OPTION_GROUP:
krh@215
   721
			print_options_usage(options[i].data);
krh@215
   722
			break;
krh@215
   723
krh@215
   724
		case OPTION_BOOL:
krh@215
   725
			printf("[");
krh@215
   726
			if (options[i].short_name)
krh@215
   727
				printf("-%c", options[i].short_name);
krh@215
   728
			if (options[i].short_name && options[i].name)
krh@215
   729
				printf("|");
krh@215
   730
			if (options[i].name)
krh@215
   731
				printf("--%s", options[i].name);
krh@215
   732
			printf("] ");
krh@215
   733
			break;
krh@215
   734
krh@215
   735
		case OPTION_STRING:
krh@215
   736
			printf("[");
krh@215
   737
			if (options[i].short_name)
krh@215
   738
				printf("-%c", options[i].short_name);
krh@215
   739
			if (options[i].short_name && options[i].name)
krh@215
   740
				printf("|");
krh@215
   741
			if (options[i].name)
krh@215
   742
				printf("--%s", options[i].name);
krh@215
   743
			if (options[i].arg_name)
krh@215
   744
				printf("=%s", options[i].arg_name);
krh@215
   745
			printf("] ");
krh@215
   746
			break;
krh@215
   747
krh@215
   748
krh@215
   749
			break;
krh@215
   750
krh@215
   751
		case OPTION_LAST:
krh@215
   752
			break;
krh@215
   753
		}
krh@215
   754
	}
krh@215
   755
}
krh@215
   756
krh@215
   757
int
krh@215
   758
main(int argc, const char *argv[])
krh@215
   759
{
krh@215
   760
	argc = parse_options(rpm_options, argc, argv);
krh@215
   761
krh@215
   762
	if (option_version) {
krh@215
   763
		printf("razor rpm version hoopla.\n");
krh@215
   764
		exit(0);
krh@215
   765
	}
krh@215
   766
krh@215
   767
	if (option_help) {
krh@215
   768
		printf("Usage: rpm [OPTION...]\n");
krh@215
   769
		print_options_help(rpm_options);
krh@215
   770
		exit(0);
krh@215
   771
	}
krh@215
   772
krh@215
   773
	if (option_usage) {
krh@215
   774
		printf("Usage: rpm [OPTION...]\n");
krh@215
   775
		print_options_usage(rpm_options);
krh@215
   776
		printf("\n");
krh@215
   777
		exit(0);
krh@215
   778
	}
krh@215
   779
krh@235
   780
	if (option_initdb) {
krh@235
   781
		command_initdb(argc, argv);
krh@235
   782
	} else if (option_verify) {
krh@218
   783
		command_verify(argc, argv);
krh@218
   784
	} else if (option_query) {
krh@215
   785
		command_query(argc, argv);
krh@215
   786
	} else if (option_install) {
krh@215
   787
		command_install(argc, argv);
krh@215
   788
	} else if (option_upgrade) {
krh@215
   789
		command_update(argc, argv);
krh@218
   790
	} else if (option_erase) {
krh@218
   791
		command_erase(argc, argv);
krh@215
   792
	} else {
krh@215
   793
		print_options_usage(rpm_options);
krh@215
   794
		printf("\n");
krh@215
   795
		exit(0);
krh@215
   796
	}
krh@215
   797
krh@215
   798
	return 0;
krh@215
   799
}