Make yum url overridable by environment variable YUM_URL.
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <curl/curl.h>
33 #include "razor-internal.h"
35 static const char system_repo_filename[] = "system.repo";
36 static const char next_repo_filename[] = "system-next.repo";
37 static const char rawhide_repo_filename[] = "rawhide.repo";
38 static const char updated_repo_filename[] = "system-updated.repo";
39 static const char razor_root_path[] = "/var/lib/razor";
40 static const char root[] = "install";
41 static const char *repo_filename = system_repo_filename;
42 static const char *yum_url;
45 command_list(int argc, const char *argv[])
47 struct razor_set *set;
48 struct razor_package_iterator *pi;
49 struct razor_package *package;
50 const char *pattern, *name, *version, *arch;
51 int only_names = 0, i = 0;
53 if (i < argc && strcmp(argv[i], "--only-names") == 0) {
59 set = razor_set_open(repo_filename);
60 pi = razor_package_iterator_create(set);
61 while (razor_package_iterator_next(pi, &package,
62 &name, &version, &arch)) {
63 if (pattern && fnmatch(pattern, name, 0) != 0)
69 printf("%s-%s.%s\n", name, version, arch);
71 razor_package_iterator_destroy(pi);
72 razor_set_destroy(set);
78 list_properties(const char *package_name,
79 enum razor_property_type required_type)
81 static const char *relation_string[] = { "<", "<=", "=", ">=", ">" };
82 struct razor_set *set;
83 struct razor_property *property;
84 struct razor_package *package;
85 struct razor_property_iterator *pi;
86 const char *name, *version;
87 enum razor_property_type type;
88 enum razor_version_relation relation;
90 set = razor_set_open(repo_filename);
92 package = razor_set_get_package(set, package_name);
96 pi = razor_property_iterator_create(set, package);
97 while (razor_property_iterator_next(pi, &property,
98 &name, &relation, &version,
100 if (type != required_type)
102 if (version[0] == '\0')
103 printf("%s\n", name);
105 printf("%s %s %s\n", name,
106 relation_string[relation], version);
108 razor_property_iterator_destroy(pi);
110 razor_set_destroy(set);
116 command_list_requires(int argc, const char *argv[])
118 return list_properties(argv[0], RAZOR_PROPERTY_REQUIRES);
122 command_list_provides(int argc, const char *argv[])
124 return list_properties(argv[0], RAZOR_PROPERTY_PROVIDES);
128 command_list_obsoletes(int argc, const char *argv[])
130 return list_properties(argv[0], RAZOR_PROPERTY_OBSOLETES);
134 command_list_conflicts(int argc, const char *argv[])
136 return list_properties(argv[0], RAZOR_PROPERTY_CONFLICTS);
140 command_list_files(int argc, const char *argv[])
142 struct razor_set *set;
144 set = razor_set_open(repo_filename);
147 razor_set_list_files(set, argv[0]);
148 razor_set_destroy(set);
154 command_list_file_packages(int argc, const char *argv[])
156 struct razor_set *set;
157 struct razor_package_iterator *pi;
158 struct razor_package *package;
159 const char *name, *version, *arch;
161 set = razor_set_open(repo_filename);
165 pi = razor_package_iterator_create_for_file(set, argv[0]);
166 while (razor_package_iterator_next(pi, &package,
167 &name, &version, &arch))
168 printf("%s-%s\n", name, version);
169 razor_package_iterator_destroy(pi);
171 razor_set_destroy(set);
177 command_list_package_files(int argc, const char *argv[])
179 struct razor_set *set;
181 set = razor_set_open(repo_filename);
184 razor_set_list_package_files(set, argv[0]);
185 razor_set_destroy(set);
191 list_packages_for_property(struct razor_set *set,
192 struct razor_property *property)
194 struct razor_package_iterator *pi;
195 struct razor_package *package;
196 const char *name, *version, *arch;
198 pi = razor_package_iterator_create_for_property(set, property);
199 while (razor_package_iterator_next(pi, &package,
200 &name, &version, &arch))
201 printf("%s-%s.%s\n", name, version, arch);
202 razor_package_iterator_destroy(pi);
206 list_property_packages(const char *ref_name,
207 const char *ref_version,
208 enum razor_property_type ref_type)
210 struct razor_set *set;
211 struct razor_property *property;
212 struct razor_property_iterator *pi;
213 const char *name, *version;
214 enum razor_property_type type;
215 enum razor_version_relation relation;
217 if (ref_name == NULL)
220 set = razor_set_open(repo_filename);
224 pi = razor_property_iterator_create(set, NULL);
225 while (razor_property_iterator_next(pi, &property,
226 &name, &relation, &version,
228 if (strcmp(ref_name, name) != 0)
230 if (ref_version && relation == RAZOR_VERSION_EQUAL &&
231 strcmp(ref_version, version) != 0)
233 if (ref_type != type)
236 list_packages_for_property(set, property);
238 razor_property_iterator_destroy(pi);
244 command_what_requires(int argc, const char *argv[])
246 return list_property_packages(argv[0], argv[1],
247 RAZOR_PROPERTY_REQUIRES);
251 command_what_provides(int argc, const char *argv[])
253 return list_property_packages(argv[0], argv[1],
254 RAZOR_PROPERTY_PROVIDES);
258 show_progress(void *clientp,
259 double dltotal, double dlnow, double ultotal, double ulnow)
261 const char *file = clientp;
263 if (!dlnow < dltotal)
264 fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
265 file, (int) dlnow / 1024, (int) dltotal / 1024);
271 download_if_missing(const char *url, const char *file)
280 curl = curl_easy_init();
284 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
285 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
286 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
287 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
289 if (stat(file, &buf) < 0) {
290 fp = fopen(file, "w");
293 "failed to open %s for writing\n", file);
296 curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
297 curl_easy_setopt(curl, CURLOPT_URL, url);
298 res = curl_easy_perform(curl);
300 if (res != CURLE_OK) {
301 fprintf(stderr, "curl error: %s\n", error);
305 res = curl_easy_getinfo(curl,
306 CURLINFO_RESPONSE_CODE, &response);
307 if (res != CURLE_OK) {
308 fprintf(stderr, "curl error: %s\n", error);
312 if (response != 200) {
313 fprintf(stderr, " - failed %ld\n", response);
317 fprintf(stderr, "\n");
320 curl_easy_cleanup(curl);
325 #define YUM_URL "http://download.fedora.redhat.com" \
326 "/pub/fedora/linux/development/i386/os"
329 command_import_yum(int argc, const char *argv[])
331 struct razor_set *set;
334 printf("downloading from %s.\n", yum_url);
335 snprintf(buffer, sizeof buffer,
336 "%s/repodata/primary.xml.gz", yum_url);
337 if (download_if_missing(buffer, "primary.xml.gz") < 0)
339 snprintf(buffer, sizeof buffer,
340 "%s/repodata/filelists.xml.gz", yum_url);
341 if (download_if_missing(buffer, "filelists.xml.gz") < 0)
344 set = razor_set_create_from_yum();
347 razor_set_write(set, rawhide_repo_filename);
348 razor_set_destroy(set);
349 printf("wrote %s\n", rawhide_repo_filename);
355 command_import_rpmdb(int argc, const char *argv[])
357 struct razor_set *set;
359 set = razor_set_create_from_rpmdb();
362 razor_set_write(set, repo_filename);
363 razor_set_destroy(set);
364 printf("wrote %s\n", repo_filename);
370 mark_packages_for_update(struct razor_transaction *trans,
371 struct razor_set *set, const char *pattern)
373 struct razor_package_iterator *pi;
374 struct razor_package *package;
375 const char *name, *version, *arch;
378 pi = razor_package_iterator_create(set);
379 while (razor_package_iterator_next(pi, &package,
380 &name, &version, &arch)) {
381 if (pattern && fnmatch(pattern, name, 0) == 0) {
382 razor_transaction_update_package(trans, package);
386 razor_package_iterator_destroy(pi);
392 mark_packages_for_removal(struct razor_transaction *trans,
393 struct razor_set *set, const char *pattern)
395 struct razor_package_iterator *pi;
396 struct razor_package *package;
397 const char *name, *version, *arch;
400 pi = razor_package_iterator_create(set);
401 while (razor_package_iterator_next(pi, &package,
402 &name, &version, &arch)) {
403 if (pattern && fnmatch(pattern, name, 0) == 0) {
404 razor_transaction_remove_package(trans, package);
408 razor_package_iterator_destroy(pi);
414 command_update(int argc, const char *argv[])
416 struct razor_set *set, *upstream;
417 struct razor_transaction *trans;
420 set = razor_set_open(repo_filename);
421 upstream = razor_set_open(rawhide_repo_filename);
422 if (set == NULL || upstream == NULL)
425 trans = razor_transaction_create(set, upstream);
427 razor_transaction_update_all(trans);
428 for (i = 0; i < argc; i++) {
429 if (mark_packages_for_update(trans, set, argv[i]) == 0) {
430 fprintf(stderr, "no match for %s\n", argv[i]);
435 errors = razor_transaction_resolve(trans);
439 set = razor_transaction_finish(trans);
440 razor_set_write(set, updated_repo_filename);
441 razor_set_destroy(set);
442 razor_set_destroy(upstream);
443 printf("wrote system-updated.repo\n");
449 command_remove(int argc, const char *argv[])
451 struct razor_set *set;
452 struct razor_transaction *trans;
455 set = razor_set_open(repo_filename);
459 trans = razor_transaction_create(set, NULL);
460 for (i = 0; i < argc; i++) {
461 if (mark_packages_for_removal(trans, set, argv[i]) == 0) {
462 fprintf(stderr, "no match for %s\n", argv[i]);
467 errors = razor_transaction_resolve(trans);
471 set = razor_transaction_finish(trans);
472 razor_set_write(set, updated_repo_filename);
473 razor_set_destroy(set);
474 printf("wrote system-updated.repo\n");
480 print_diff(const char *name,
481 const char *old_version, const char *new_version, const char *arch,
485 printf("removing %s %s\n", name, old_version);
487 printf("install %s %s\n", name, new_version);
491 command_diff(int argc, const char *argv[])
493 struct razor_set *set, *updated;
495 set = razor_set_open(repo_filename);
496 updated = razor_set_open(updated_repo_filename);
497 if (set == NULL || updated == NULL)
500 razor_set_diff(set, updated, print_diff, NULL);
502 razor_set_destroy(set);
503 razor_set_destroy(updated);
509 command_import_rpms(int argc, const char *argv[])
513 struct razor_importer *importer;
514 struct razor_set *set;
515 struct razor_rpm *rpm;
518 const char *dirname = argv[0];
520 if (dirname == NULL) {
521 fprintf(stderr, "usage: razor import-rpms DIR\n");
525 dir = opendir(dirname);
527 fprintf(stderr, "couldn't read dir %s\n", dirname);
531 importer = razor_importer_new();
533 while (de = readdir(dir), de != NULL) {
534 len = strlen(de->d_name);
535 if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
537 snprintf(filename, sizeof filename,
538 "%s/%s", dirname, de->d_name);
539 rpm = razor_rpm_open(filename);
542 "failed to open rpm \"%s\"\n", filename);
545 if (razor_importer_add_rpm(importer, rpm)) {
546 fprintf(stderr, "couldn't import %s\n", filename);
549 razor_rpm_close(rpm);
553 razor_importer_destroy(importer);
557 set = razor_importer_finish(importer);
559 razor_set_write(set, repo_filename);
560 razor_set_destroy(set);
561 printf("wrote %s\n", repo_filename);
567 download_package(const char *name,
568 const char *old_version,
569 const char *new_version,
573 char file[PATH_MAX], url[256];
581 v = strchr(new_version, ':');
587 snprintf(url, sizeof url,
588 "%s/Packages/%s-%s.%s.rpm", yum_url, name, v, arch);
589 snprintf(file, sizeof file,
590 "rpms/%s-%s.%s.rpm", name, v, arch);
591 if (download_if_missing(url, file) < 0)
596 install_package(const char *name,
597 const char *old_version,
598 const char *new_version,
602 const char *v, *root = data;
604 struct razor_rpm *rpm;
607 printf("removing %s %s not handled\n", name, old_version);
612 v = strchr(new_version, ':');
618 printf("install %s %s\n", name, v);
619 snprintf(file, sizeof file, "rpms/%s-%s.%s.rpm", name, v, arch);
621 rpm = razor_rpm_open(file);
623 fprintf(stderr, "failed to open rpm %s\n", file);
626 if (razor_rpm_install(rpm, root) < 0) {
628 "failed to install rpm %s\n", file);
631 razor_rpm_close(rpm);
635 command_install(int argc, const char *argv[])
637 struct razor_set *system, *upstream, *next;
638 struct razor_transaction *trans;
639 char path[PATH_MAX], new_path[PATH_MAX];
640 int i = 0, errors, fd, dependencies = 1;
642 if (i < argc && strcmp(argv[i], "--no-dependencies") == 0) {
647 /* Create the new next repo file up front to ensure exclusive
649 snprintf(new_path, sizeof new_path,
650 "%s%s/%s", root, razor_root_path, next_repo_filename);
651 fd = open(new_path, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
653 fprintf(stderr, "failed to get lock file, "
654 "maybe previous operation crashed?\n");
656 /* FIXME: Use fcntl advisory locking to figure out
657 * whether previous operation crashed or is still in
663 upstream = razor_set_open(rawhide_repo_filename);
664 snprintf(path, sizeof path,
665 "%s%s/%s", root, razor_root_path, system_repo_filename);
666 system = razor_set_open(path);
667 if (system == NULL || upstream == NULL) {
671 trans = razor_transaction_create(system, upstream);
672 for (; i < argc; i++) {
673 if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
674 fprintf(stderr, "no package matched %s\n", argv[i]);
681 errors = razor_transaction_resolve(trans);
688 next = razor_transaction_finish(trans);
690 razor_set_write_to_fd(next, fd);
691 printf("wrote %s\n", new_path);
693 if (mkdir("rpms", 0777) && errno != EEXIST) {
694 fprintf(stderr, "failed to create rpms directory.\n");
698 razor_set_diff(system, next, download_package, &errors);
700 fprintf(stderr, "failed to download %d packages\n", errors);
705 /* FIXME: We need to figure out the right install order here,
706 * so the post and pre scripts can run. */
707 razor_set_diff(system, next, install_package, (void *) root);
709 razor_set_destroy(next);
710 razor_set_destroy(system);
711 razor_set_destroy(upstream);
714 rename(new_path, path);
715 printf("renamed %s to %s\n", new_path, path);
721 command_init(int argc, const char *argv[])
724 struct razor_set *set;
727 if (stat(root, &buf) < 0) {
728 if (mkdir(root, 0777) < 0) {
730 "could not create install root \"%s\"\n",
734 fprintf(stderr, "created install root \"%s\"\n", root);
735 } else if (!S_ISDIR(buf.st_mode)) {
737 "install root \"%s\" exists, but is not a directory\n",
742 snprintf(path, sizeof path, "%s/%s",
743 razor_root_path, system_repo_filename);
744 if (razor_create_dir(root, path) < 0) {
745 fprintf(stderr, "could not create %s%s\n",
746 root, razor_root_path);
750 set = razor_set_create();
751 snprintf(path, sizeof path, "%s%s/%s",
752 root, razor_root_path, system_repo_filename);
753 if (razor_set_write(set, path) < 0) {
754 fprintf(stderr, "could not write initial package set\n");
757 razor_set_destroy(set);
763 command_download(int argc, const char *argv[])
765 struct razor_set *set;
766 struct razor_package_iterator *pi;
767 struct razor_package *package;
768 const char *pattern = argv[0], *name, *version, *arch;
769 char url[256], file[256];
772 if (mkdir("rpms", 0777) && errno != EEXIST) {
773 fprintf(stderr, "failed to create rpms directory.\n");
777 set = razor_set_open(rawhide_repo_filename);
778 pi = razor_package_iterator_create(set);
779 while (razor_package_iterator_next(pi, &package,
780 &name, &version, &arch)) {
781 if (pattern && fnmatch(pattern, name, 0) != 0)
785 snprintf(url, sizeof url,
786 "%s/Packages/%s-%s.%s.rpm",
787 yum_url, name, version, arch);
788 snprintf(file, sizeof file,
789 "rpms/%s-%s.%s.rpm", name, version, arch);
790 download_if_missing(url, file);
792 razor_package_iterator_destroy(pi);
793 razor_set_destroy(set);
796 fprintf(stderr, "no packages matched \"%s\"\n", pattern);
797 else if (matches == 1)
798 fprintf(stderr, "downloaded 1 package\n");
800 fprintf(stderr, "downloaded %d packages\n", matches);
807 const char *description;
808 int (*func)(int argc, const char *argv[]);
809 } razor_commands[] = {
810 { "list", "list all packages", command_list },
811 { "list-requires", "list all requires for the given package", command_list_requires },
812 { "list-provides", "list all provides for the given package", command_list_provides },
813 { "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
814 { "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
815 { "list-files", "list files for package set", command_list_files },
816 { "list-file-packages", "list packages owning file", command_list_file_packages },
817 { "list-package-files", "list files in package", command_list_package_files },
818 { "what-requires", "list the packages that have the given requires", command_what_requires },
819 { "what-provides", "list the packages that have the given provides", command_what_provides },
820 { "import-yum", "import yum metadata files", command_import_yum },
821 { "import-rpmdb", "import the system rpm database", command_import_rpmdb },
822 { "import-rpms", "import rpms from the given directory", command_import_rpms },
823 { "update", "update all or specified packages", command_update },
824 { "remove", "remove specified packages", command_remove },
825 { "diff", "show diff between two package sets", command_diff },
826 { "install", "install rpm", command_install },
827 { "init", "init razor root", command_init },
828 { "download", "download packages", command_download }
837 for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
839 razor_commands[i].name, razor_commands[i].description);
845 main(int argc, const char *argv[])
850 repo = getenv("RAZOR_REPO");
852 repo_filename = repo;
854 yum_url = getenv("YUM_URL");
861 for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
862 if (strcmp(razor_commands[i].name, argv[1]) == 0)
863 return razor_commands[i].func(argc - 2, argv + 2);