|
krh@43
|
1 |
#include <stdlib.h>
|
|
krh@43
|
2 |
#include <stddef.h>
|
|
krh@43
|
3 |
#include <stdio.h>
|
|
krh@43
|
4 |
#include <string.h>
|
|
krh@72
|
5 |
#include <sys/types.h>
|
|
krh@72
|
6 |
#include <sys/stat.h>
|
|
krh@43
|
7 |
#include <unistd.h>
|
|
krh@75
|
8 |
#include <dirent.h>
|
|
krh@71
|
9 |
#include <curl/curl.h>
|
|
krh@43
|
10 |
#include "razor.h"
|
|
krh@43
|
11 |
|
|
krh@43
|
12 |
static const char *repo_filename = "system.repo";
|
|
krh@44
|
13 |
static const char *rawhide_repo_filename = "rawhide.repo";
|
|
krh@44
|
14 |
static const char *updated_repo_filename = "system-updated.repo";
|
|
krh@43
|
15 |
|
|
krh@43
|
16 |
static int
|
|
krh@43
|
17 |
command_list(int argc, const char *argv[])
|
|
krh@43
|
18 |
{
|
|
krh@43
|
19 |
struct razor_set *set;
|
|
krh@43
|
20 |
|
|
krh@43
|
21 |
set = razor_set_open(repo_filename);
|
|
krh@54
|
22 |
razor_set_list(set, argv[0]);
|
|
krh@43
|
23 |
razor_set_destroy(set);
|
|
krh@43
|
24 |
|
|
krh@43
|
25 |
return 0;
|
|
krh@43
|
26 |
}
|
|
krh@43
|
27 |
|
|
krh@43
|
28 |
static int
|
|
krh@43
|
29 |
command_list_requires(int argc, const char *argv[])
|
|
krh@43
|
30 |
{
|
|
krh@43
|
31 |
struct razor_set *set;
|
|
krh@43
|
32 |
|
|
krh@43
|
33 |
set = razor_set_open(repo_filename);
|
|
krh@66
|
34 |
razor_set_list_properties(set, argv[0], RAZOR_PROPERTY_REQUIRES);
|
|
krh@43
|
35 |
razor_set_destroy(set);
|
|
krh@43
|
36 |
|
|
krh@43
|
37 |
return 0;
|
|
krh@43
|
38 |
}
|
|
krh@43
|
39 |
|
|
krh@43
|
40 |
static int
|
|
krh@43
|
41 |
command_list_provides(int argc, const char *argv[])
|
|
krh@43
|
42 |
{
|
|
krh@43
|
43 |
struct razor_set *set;
|
|
krh@43
|
44 |
|
|
krh@43
|
45 |
set = razor_set_open(repo_filename);
|
|
krh@66
|
46 |
razor_set_list_properties(set, argv[0], RAZOR_PROPERTY_PROVIDES);
|
|
krh@43
|
47 |
razor_set_destroy(set);
|
|
krh@43
|
48 |
|
|
krh@43
|
49 |
return 0;
|
|
krh@43
|
50 |
}
|
|
krh@43
|
51 |
|
|
krh@43
|
52 |
static int
|
|
krh@67
|
53 |
command_list_obsoletes(int argc, const char *argv[])
|
|
krh@67
|
54 |
{
|
|
krh@67
|
55 |
struct razor_set *set;
|
|
krh@67
|
56 |
|
|
krh@67
|
57 |
set = razor_set_open(repo_filename);
|
|
krh@67
|
58 |
razor_set_list_properties(set, argv[0], RAZOR_PROPERTY_OBSOLETES);
|
|
krh@67
|
59 |
razor_set_destroy(set);
|
|
krh@67
|
60 |
|
|
krh@67
|
61 |
return 0;
|
|
krh@67
|
62 |
}
|
|
krh@67
|
63 |
|
|
krh@67
|
64 |
static int
|
|
krh@67
|
65 |
command_list_conflicts(int argc, const char *argv[])
|
|
krh@67
|
66 |
{
|
|
krh@67
|
67 |
struct razor_set *set;
|
|
krh@67
|
68 |
|
|
krh@67
|
69 |
set = razor_set_open(repo_filename);
|
|
krh@67
|
70 |
razor_set_list_properties(set, argv[0], RAZOR_PROPERTY_CONFLICTS);
|
|
krh@67
|
71 |
razor_set_destroy(set);
|
|
krh@67
|
72 |
|
|
krh@67
|
73 |
return 0;
|
|
krh@67
|
74 |
}
|
|
krh@67
|
75 |
|
|
krh@67
|
76 |
static int
|
|
krh@48
|
77 |
command_list_files(int argc, const char *argv[])
|
|
krh@48
|
78 |
{
|
|
krh@48
|
79 |
struct razor_set *set;
|
|
krh@48
|
80 |
|
|
krh@48
|
81 |
set = razor_set_open(repo_filename);
|
|
krh@48
|
82 |
if (set == NULL)
|
|
krh@48
|
83 |
return 1;
|
|
krh@49
|
84 |
razor_set_list_files(set, argv[0]);
|
|
krh@48
|
85 |
razor_set_destroy(set);
|
|
krh@48
|
86 |
|
|
krh@48
|
87 |
return 0;
|
|
krh@48
|
88 |
}
|
|
krh@48
|
89 |
|
|
krh@48
|
90 |
static int
|
|
krh@52
|
91 |
command_list_file_packages(int argc, const char *argv[])
|
|
krh@52
|
92 |
{
|
|
krh@52
|
93 |
struct razor_set *set;
|
|
krh@52
|
94 |
|
|
krh@52
|
95 |
set = razor_set_open(repo_filename);
|
|
krh@52
|
96 |
if (set == NULL)
|
|
krh@52
|
97 |
return 1;
|
|
krh@52
|
98 |
razor_set_list_file_packages(set, argv[0]);
|
|
krh@52
|
99 |
razor_set_destroy(set);
|
|
krh@52
|
100 |
|
|
krh@52
|
101 |
return 0;
|
|
krh@52
|
102 |
}
|
|
krh@52
|
103 |
|
|
krh@56
|
104 |
static int
|
|
krh@56
|
105 |
command_list_package_files(int argc, const char *argv[])
|
|
krh@56
|
106 |
{
|
|
krh@56
|
107 |
struct razor_set *set;
|
|
krh@56
|
108 |
|
|
krh@56
|
109 |
set = razor_set_open(repo_filename);
|
|
krh@56
|
110 |
if (set == NULL)
|
|
krh@56
|
111 |
return 1;
|
|
krh@56
|
112 |
razor_set_list_package_files(set, argv[0]);
|
|
krh@56
|
113 |
razor_set_destroy(set);
|
|
krh@56
|
114 |
|
|
krh@56
|
115 |
return 0;
|
|
krh@56
|
116 |
}
|
|
krh@52
|
117 |
|
|
krh@52
|
118 |
static int
|
|
krh@43
|
119 |
command_what_requires(int argc, const char *argv[])
|
|
krh@43
|
120 |
{
|
|
krh@43
|
121 |
struct razor_set *set;
|
|
krh@43
|
122 |
|
|
krh@43
|
123 |
set = razor_set_open(repo_filename);
|
|
krh@66
|
124 |
razor_set_list_property_packages(set, argv[0], argv[1],
|
|
krh@66
|
125 |
RAZOR_PROPERTY_REQUIRES);
|
|
krh@43
|
126 |
razor_set_destroy(set);
|
|
krh@43
|
127 |
|
|
krh@43
|
128 |
return 0;
|
|
krh@43
|
129 |
}
|
|
krh@43
|
130 |
|
|
krh@43
|
131 |
static int
|
|
krh@43
|
132 |
command_what_provides(int argc, const char *argv[])
|
|
krh@43
|
133 |
{
|
|
krh@43
|
134 |
struct razor_set *set;
|
|
krh@43
|
135 |
|
|
krh@43
|
136 |
set = razor_set_open(repo_filename);
|
|
krh@66
|
137 |
razor_set_list_property_packages(set, argv[0], argv[1],
|
|
krh@66
|
138 |
RAZOR_PROPERTY_PROVIDES);
|
|
krh@43
|
139 |
razor_set_destroy(set);
|
|
krh@43
|
140 |
|
|
krh@43
|
141 |
return 0;
|
|
krh@43
|
142 |
}
|
|
krh@43
|
143 |
|
|
krh@73
|
144 |
static int
|
|
krh@73
|
145 |
show_progress(void *clientp,
|
|
krh@73
|
146 |
double dltotal, double dlnow, double ultotal, double ulnow)
|
|
krh@73
|
147 |
{
|
|
krh@73
|
148 |
const char *file = clientp;
|
|
krh@73
|
149 |
|
|
krh@73
|
150 |
if (!dlnow < dltotal)
|
|
krh@73
|
151 |
fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
|
|
krh@73
|
152 |
file, (int) dlnow / 1024, (int) dltotal / 1024);
|
|
krh@73
|
153 |
else
|
|
krh@73
|
154 |
fprintf(stderr, "\n");
|
|
krh@73
|
155 |
|
|
krh@73
|
156 |
return 0;
|
|
krh@73
|
157 |
}
|
|
krh@73
|
158 |
|
|
krh@73
|
159 |
static int
|
|
krh@73
|
160 |
download_if_missing(CURL *curl, const char *url, const char *file)
|
|
krh@73
|
161 |
{
|
|
krh@73
|
162 |
struct stat buf;
|
|
krh@73
|
163 |
char error[256];
|
|
krh@73
|
164 |
FILE *fp;
|
|
krh@73
|
165 |
CURLcode res;
|
|
krh@73
|
166 |
char buffer[256];
|
|
krh@73
|
167 |
|
|
krh@73
|
168 |
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
|
|
krh@73
|
169 |
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
|
|
krh@73
|
170 |
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
|
|
krh@73
|
171 |
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
|
|
krh@73
|
172 |
|
|
krh@73
|
173 |
if (stat(file, &buf) < 0) {
|
|
krh@73
|
174 |
fp = fopen(file, "w");
|
|
krh@73
|
175 |
snprintf(buffer, sizeof buffer, "%s/%s", url, file);
|
|
krh@73
|
176 |
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
|
|
krh@73
|
177 |
curl_easy_setopt(curl, CURLOPT_URL, buffer);
|
|
krh@73
|
178 |
res = curl_easy_perform(curl);
|
|
krh@73
|
179 |
fclose(fp);
|
|
krh@73
|
180 |
if (res != CURLE_OK) {
|
|
krh@73
|
181 |
fprintf(stderr, "curl error: %s\n", error);
|
|
krh@73
|
182 |
unlink(file);
|
|
krh@73
|
183 |
return -1;
|
|
krh@73
|
184 |
}
|
|
krh@73
|
185 |
}
|
|
krh@73
|
186 |
|
|
krh@73
|
187 |
return 0;
|
|
krh@73
|
188 |
}
|
|
krh@73
|
189 |
|
|
krh@71
|
190 |
#define REPO_URL "http://download.fedora.redhat.com" \
|
|
krh@71
|
191 |
"/pub/fedora/linux/development/i386/os/repodata"
|
|
krh@71
|
192 |
|
|
krh@43
|
193 |
static int
|
|
krh@43
|
194 |
command_import_yum(int argc, const char *argv[])
|
|
krh@43
|
195 |
{
|
|
krh@43
|
196 |
struct razor_set *set;
|
|
krh@71
|
197 |
CURL *curl;
|
|
krh@71
|
198 |
|
|
krh@71
|
199 |
curl = curl_easy_init();
|
|
krh@71
|
200 |
if (curl == NULL)
|
|
krh@71
|
201 |
return 1;
|
|
krh@71
|
202 |
|
|
krh@73
|
203 |
if (download_if_missing(curl, REPO_URL, "primary.xml.gz") < 0)
|
|
krh@73
|
204 |
return -1;
|
|
krh@73
|
205 |
if (download_if_missing(curl, REPO_URL, "filelists.xml.gz") < 0)
|
|
krh@73
|
206 |
return -1;
|
|
krh@71
|
207 |
curl_easy_cleanup(curl);
|
|
krh@43
|
208 |
|
|
krh@70
|
209 |
set = razor_set_create_from_yum();
|
|
krh@43
|
210 |
if (set == NULL)
|
|
krh@43
|
211 |
return 1;
|
|
krh@43
|
212 |
razor_set_write(set, rawhide_repo_filename);
|
|
krh@43
|
213 |
razor_set_destroy(set);
|
|
krh@43
|
214 |
printf("wrote %s\n", rawhide_repo_filename);
|
|
krh@43
|
215 |
|
|
krh@43
|
216 |
return 0;
|
|
krh@43
|
217 |
}
|
|
krh@43
|
218 |
|
|
krh@43
|
219 |
static int
|
|
krh@43
|
220 |
command_import_rpmdb(int argc, const char *argv[])
|
|
krh@43
|
221 |
{
|
|
krh@43
|
222 |
struct razor_set *set;
|
|
krh@43
|
223 |
|
|
krh@43
|
224 |
set = razor_set_create_from_rpmdb();
|
|
krh@43
|
225 |
if (set == NULL)
|
|
krh@43
|
226 |
return 1;
|
|
krh@43
|
227 |
razor_set_write(set, repo_filename);
|
|
krh@43
|
228 |
razor_set_destroy(set);
|
|
krh@43
|
229 |
printf("wrote %s\n", repo_filename);
|
|
krh@43
|
230 |
|
|
krh@43
|
231 |
return 0;
|
|
krh@43
|
232 |
}
|
|
krh@43
|
233 |
|
|
krh@43
|
234 |
static int
|
|
krh@43
|
235 |
command_validate(int argc, const char *argv[])
|
|
krh@43
|
236 |
{
|
|
krh@43
|
237 |
struct razor_set *set;
|
|
krh@43
|
238 |
|
|
krh@43
|
239 |
set = razor_set_open(repo_filename);
|
|
krh@43
|
240 |
if (set == NULL)
|
|
krh@43
|
241 |
return 1;
|
|
krh@43
|
242 |
razor_set_list_unsatisfied(set);
|
|
krh@43
|
243 |
razor_set_destroy(set);
|
|
krh@43
|
244 |
|
|
krh@43
|
245 |
return 0;
|
|
krh@43
|
246 |
}
|
|
krh@43
|
247 |
|
|
krh@43
|
248 |
static int
|
|
krh@43
|
249 |
command_update(int argc, const char *argv[])
|
|
krh@43
|
250 |
{
|
|
krh@43
|
251 |
struct razor_set *set, *upstream;
|
|
krh@43
|
252 |
|
|
krh@43
|
253 |
set = razor_set_open(repo_filename);
|
|
krh@43
|
254 |
upstream = razor_set_open(rawhide_repo_filename);
|
|
krh@43
|
255 |
if (set == NULL || upstream == NULL)
|
|
krh@43
|
256 |
return 1;
|
|
krh@45
|
257 |
set = razor_set_update(set, upstream, argc, argv);
|
|
krh@44
|
258 |
razor_set_write(set, updated_repo_filename);
|
|
krh@43
|
259 |
razor_set_destroy(set);
|
|
krh@43
|
260 |
razor_set_destroy(upstream);
|
|
krh@43
|
261 |
printf("wrote system-updated.repo\n");
|
|
krh@43
|
262 |
|
|
krh@43
|
263 |
return 0;
|
|
krh@43
|
264 |
}
|
|
krh@43
|
265 |
|
|
krh@44
|
266 |
static void
|
|
krh@44
|
267 |
print_diff(const char *name,
|
|
krh@44
|
268 |
const char *old_version, const char *new_version, void *data)
|
|
krh@44
|
269 |
{
|
|
krh@44
|
270 |
if (old_version)
|
|
krh@44
|
271 |
printf("removing %s %s\n", name, old_version);
|
|
krh@44
|
272 |
else
|
|
krh@44
|
273 |
printf("install %s %s\n", name, new_version);
|
|
krh@44
|
274 |
}
|
|
krh@44
|
275 |
|
|
krh@44
|
276 |
static int
|
|
krh@44
|
277 |
command_diff(int argc, const char *argv[])
|
|
krh@44
|
278 |
{
|
|
krh@44
|
279 |
struct razor_set *set, *updated;
|
|
krh@44
|
280 |
|
|
krh@44
|
281 |
set = razor_set_open(repo_filename);
|
|
krh@44
|
282 |
updated = razor_set_open(updated_repo_filename);
|
|
krh@44
|
283 |
if (set == NULL || updated == NULL)
|
|
krh@44
|
284 |
return 1;
|
|
krh@44
|
285 |
|
|
krh@44
|
286 |
razor_set_diff(set, updated, print_diff, NULL);
|
|
krh@44
|
287 |
|
|
krh@44
|
288 |
razor_set_destroy(set);
|
|
krh@44
|
289 |
razor_set_destroy(updated);
|
|
krh@44
|
290 |
|
|
krh@44
|
291 |
return 0;
|
|
krh@44
|
292 |
}
|
|
krh@44
|
293 |
|
|
krh@74
|
294 |
static int
|
|
krh@75
|
295 |
command_import_rpms(int argc, const char *argv[])
|
|
krh@74
|
296 |
{
|
|
krh@75
|
297 |
DIR *dir;
|
|
krh@75
|
298 |
struct dirent *de;
|
|
krh@75
|
299 |
struct razor_importer *importer;
|
|
krh@75
|
300 |
struct razor_set *set;
|
|
krh@75
|
301 |
int len;
|
|
krh@75
|
302 |
char filename[256];
|
|
krh@75
|
303 |
const char *dirname = argv[0];
|
|
krh@75
|
304 |
|
|
krh@75
|
305 |
if (dirname == NULL) {
|
|
krh@75
|
306 |
fprintf(stderr, "usage: razor import-rpms DIR\n");
|
|
krh@75
|
307 |
return -1;
|
|
krh@75
|
308 |
}
|
|
krh@75
|
309 |
|
|
krh@75
|
310 |
dir = opendir(dirname);
|
|
krh@75
|
311 |
if (dir == NULL) {
|
|
krh@75
|
312 |
fprintf(stderr, "couldn't read dir %s\n", dirname);
|
|
krh@75
|
313 |
return -1;
|
|
krh@75
|
314 |
}
|
|
krh@75
|
315 |
|
|
krh@75
|
316 |
importer = razor_importer_new();
|
|
krh@75
|
317 |
|
|
krh@75
|
318 |
while (de = readdir(dir), de != NULL) {
|
|
krh@75
|
319 |
len = strlen(de->d_name);
|
|
krh@75
|
320 |
if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
|
|
krh@75
|
321 |
continue;
|
|
krh@75
|
322 |
snprintf(filename, sizeof filename,
|
|
krh@75
|
323 |
"%s/%s", dirname, de->d_name);
|
|
krh@75
|
324 |
if (razor_importer_add_rpm(importer, filename)) {
|
|
krh@75
|
325 |
fprintf(stderr, "couldn't import %s\n", filename);
|
|
krh@75
|
326 |
break;
|
|
krh@75
|
327 |
}
|
|
krh@75
|
328 |
}
|
|
krh@75
|
329 |
|
|
krh@75
|
330 |
if (de != NULL) {
|
|
krh@75
|
331 |
razor_importer_destroy(importer);
|
|
krh@75
|
332 |
return -1;
|
|
krh@75
|
333 |
}
|
|
krh@75
|
334 |
|
|
krh@75
|
335 |
set = razor_importer_finish(importer);
|
|
krh@75
|
336 |
|
|
krh@75
|
337 |
razor_set_write(set, repo_filename);
|
|
krh@75
|
338 |
razor_set_destroy(set);
|
|
krh@75
|
339 |
printf("wrote %s\n", repo_filename);
|
|
krh@74
|
340 |
|
|
krh@74
|
341 |
return 0;
|
|
krh@74
|
342 |
}
|
|
krh@74
|
343 |
|
|
krh@43
|
344 |
static struct {
|
|
krh@43
|
345 |
const char *name;
|
|
krh@43
|
346 |
const char *description;
|
|
krh@43
|
347 |
int (*func)(int argc, const char *argv[]);
|
|
krh@43
|
348 |
} razor_commands[] = {
|
|
krh@43
|
349 |
{ "list", "list all packages", command_list },
|
|
krh@67
|
350 |
{ "list-requires", "list all requires for the given package", command_list_requires },
|
|
krh@75
|
351 |
{ "list-provides", "list all provides for the given package", command_list_provides },
|
|
krh@75
|
352 |
{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
|
|
krh@75
|
353 |
{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
|
|
krh@48
|
354 |
{ "list-files", "list files for package set", command_list_files },
|
|
krh@52
|
355 |
{ "list-file-packages", "list packages owning file", command_list_file_packages },
|
|
krh@56
|
356 |
{ "list-package-files", "list files in package", command_list_package_files },
|
|
krh@43
|
357 |
{ "what-requires", "list the packages that have the given requires", command_what_requires },
|
|
krh@43
|
358 |
{ "what-provides", "list the packages that have the given provides", command_what_provides },
|
|
krh@75
|
359 |
{ "import-yum", "import yum metadata files", command_import_yum },
|
|
krh@43
|
360 |
{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
|
|
krh@75
|
361 |
{ "import-rpms", "import rpms from the given directory", command_import_rpms },
|
|
krh@43
|
362 |
{ "validate", "validate a package set", command_validate },
|
|
krh@44
|
363 |
{ "update", "update all or specified packages", command_update },
|
|
krh@75
|
364 |
{ "diff", "show diff between two package sets", command_diff }
|
|
krh@43
|
365 |
};
|
|
krh@43
|
366 |
|
|
krh@43
|
367 |
static int
|
|
krh@43
|
368 |
usage(void)
|
|
krh@43
|
369 |
{
|
|
krh@43
|
370 |
int i;
|
|
krh@43
|
371 |
|
|
krh@43
|
372 |
printf("usage:\n");
|
|
krh@43
|
373 |
for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
|
|
krh@43
|
374 |
printf(" %-20s%s\n",
|
|
krh@43
|
375 |
razor_commands[i].name, razor_commands[i].description);
|
|
krh@43
|
376 |
|
|
krh@43
|
377 |
return 1;
|
|
krh@43
|
378 |
}
|
|
krh@43
|
379 |
|
|
krh@43
|
380 |
int
|
|
krh@43
|
381 |
main(int argc, const char *argv[])
|
|
krh@43
|
382 |
{
|
|
krh@43
|
383 |
char *repo;
|
|
krh@43
|
384 |
int i;
|
|
krh@43
|
385 |
|
|
krh@43
|
386 |
repo = getenv("RAZOR_REPO");
|
|
krh@43
|
387 |
if (repo != NULL)
|
|
krh@43
|
388 |
repo_filename = repo;
|
|
krh@43
|
389 |
|
|
krh@43
|
390 |
if (argc < 2)
|
|
krh@43
|
391 |
return usage();
|
|
krh@43
|
392 |
|
|
krh@43
|
393 |
for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
|
|
krh@43
|
394 |
if (strcmp(razor_commands[i].name, argv[1]) == 0)
|
|
krh@43
|
395 |
return razor_commands[i].func(argc - 2, argv + 2);
|
|
krh@43
|
396 |
|
|
krh@43
|
397 |
return usage();
|
|
krh@43
|
398 |
}
|