2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009 J. Ali Harlow <ali@juiblex.co.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/types.h>
44 #include "razor-internal.h"
62 struct razor_set_section_index {
68 #define MAIN(type, field) \
69 { type, offsetof(struct razor_set, field), RAZOR_SECTION_MAIN }
70 #define FILES(type, field) \
71 { type, offsetof(struct razor_set, field), RAZOR_SECTION_FILES }
72 #define DETAILS(type, field) \
73 { type, offsetof(struct razor_set, field), RAZOR_SECTION_DETAILS }
75 struct razor_set_section_index razor_sections[] = {
76 MAIN(RAZOR_STRING_POOL, string_pool),
77 MAIN(RAZOR_PACKAGES, packages),
78 MAIN(RAZOR_PROPERTIES, properties),
79 MAIN(RAZOR_PACKAGE_POOL, package_pool),
80 MAIN(RAZOR_PROPERTY_POOL, property_pool),
81 MAIN(RAZOR_PREFIX_POOL, prefix_pool),
82 FILES(RAZOR_FILES, files),
83 FILES(RAZOR_FILE_POOL, file_pool),
84 FILES(RAZOR_FILE_STRING_POOL, file_string_pool),
85 DETAILS(RAZOR_DETAILS_STRING_POOL, details_string_pool)
88 RAZOR_EXPORT struct razor_set *
89 razor_set_create_without_root(void)
91 struct razor_set *set;
94 set = zalloc(sizeof *set);
96 empty = array_add(&set->string_pool, 1);
104 RAZOR_EXPORT struct razor_set *
105 razor_set_create(void)
107 struct razor_set *set;
108 struct razor_entry *e;
110 set = razor_set_create_without_root();
112 e = array_add(&set->files, sizeof *e);
114 e->flags = RAZOR_ENTRY_LAST;
116 list_set_empty(&e->packages);
121 struct razor_mapped_file {
122 struct razor_set_header *header;
124 struct razor_mapped_file *next;
128 razor_set_bind_sections(struct razor_set *set, const char *filename)
130 struct razor_set_section *s, *sections;
131 struct razor_mapped_file *file;
136 file = zalloc(sizeof *file);
140 file->header = razor_file_get_contents(filename, &file->size);
146 if (set->mapped_files == NULL) {
147 for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
148 array = (void *) set + razor_sections[i].offset;
149 array_release(array);
153 file->next = set->mapped_files;
154 set->mapped_files = file;
156 sections = (void *) file->header + sizeof *file->header;
157 pool = (void *) sections +
158 file->header->num_sections * sizeof *sections;
160 for (i = 0; i < file->header->num_sections; i++) {
162 for (j = 0; j < ARRAY_SIZE(razor_sections); j++)
163 if (!strcmp(razor_sections[j].name, &pool[s->name]))
165 if (j == ARRAY_SIZE(razor_sections))
167 array = (void *) set + razor_sections[j].offset;
168 array->data = (void *) file->header + s->offset;
169 array->size = s->size;
170 array->alloc = s->size;
176 RAZOR_EXPORT struct razor_set *
177 razor_set_open(const char *filename)
179 struct razor_set *set;
181 set = zalloc(sizeof *set);
183 if (razor_set_bind_sections(set, filename)){
191 razor_set_aquire_lock(struct razor_set *set, const char *path, int exclusive)
197 fd = open(path, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, 0666);
205 DWORD flags = LOCKFILE_FAIL_IMMEDIATELY;
206 OVERLAPPED lock = {0};
209 flags |= LOCKFILE_EXCLUSIVE_LOCK;
210 if (fd >= 0 && !LockFileEx(_get_osfhandle(fd), flags, 0, 1, 0, &lock)) {
214 if (set->lock_fd >= 0)
215 (void)UnlockFile(_get_osfhandle(set->lock_fd), 0, 0, 1, 0);
217 struct flock lock = {0};
219 lock.l_type = exclusive ? F_WRLCK : F_RDLCK;
220 lock.l_whence = SEEK_SET;
223 if (fd >= 0 && fcntl(fd, F_SETLK, &lock) < 0) {
227 if (set->lock_fd >= 0) {
228 lock.l_type = F_UNLCK;
229 (void)fcntl(set->lock_fd, F_SETLK, &lock);
233 if (set->lock_fd >= 0)
241 razor_set_destroy(struct razor_set *set)
243 struct razor_mapped_file *file, *next;
247 assert (set != NULL);
249 if (set->mapped_files == NULL) {
250 for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
251 array = (void *) set + razor_sections[i].offset;
252 array_release(array);
255 for (file = set->mapped_files; file != NULL; file = next) {
257 razor_file_free_contents(file->header, file->size);
262 razor_set_aquire_lock(set, NULL, 0);
267 razor_set_write_to_fd(struct razor_set *set, int fd, uint32_t section_mask)
269 struct razor_set_header header;
270 struct razor_set_section sections[ARRAY_SIZE(razor_sections)];
271 struct hashtable table;
272 struct array pool, *arrays[ARRAY_SIZE(razor_sections)];
275 static const char padding[4];
278 hashtable_init(&table, &pool);
281 for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
282 if ((razor_sections[i].flags & section_mask) == 0)
285 arrays[j] = (void *) set + razor_sections[i].offset;
287 hashtable_tokenize(&table, razor_sections[i].name);
292 header.magic = RAZOR_MAGIC;
293 header.version = RAZOR_VERSION;
294 header.num_sections = count;
295 offset = sizeof header + count * sizeof *sections + ALIGN(pool.size, 4);
297 for (i = 0; i < count; i++) {
298 sections[i].offset = offset;
299 sections[i].size = arrays[i]->size;
300 offset += ALIGN(arrays[i]->size, 4);
303 razor_write(fd, &header, sizeof header);
304 razor_write(fd, sections, count * sizeof *sections);
305 razor_write(fd, pool.data, pool.size);
306 razor_write(fd, padding, PADDING(pool.size, 4));
308 for (i = 0; i < count; i++) {
309 razor_write(fd, arrays[i]->data, arrays[i]->size);
310 razor_write(fd, padding, PADDING(arrays[i]->size, 4));
313 array_release(&pool);
314 hashtable_release(&table);
320 razor_set_write(struct razor_set *set, const char *filename, uint32_t sections)
324 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0666);
328 status = razor_set_write_to_fd(set, fd, sections);
338 razor_build_evr(char *evr_buf, int size, const char *epoch,
339 const char *version, const char *release)
343 if (!version || !*version) {
348 if (epoch && *epoch && strcmp(epoch, "0") != 0) {
349 len = snprintf(evr_buf, size, "%s:", epoch);
353 len = snprintf(evr_buf, size, "%s", version);
356 if (release && *release)
357 snprintf(evr_buf, size, "-%s", release);
361 razor_versioncmp(const char *s1, const char *s2)
370 n1 = strtol(s1, (char **) &p1, 10);
371 n2 = strtol(s2, (char **) &p2, 10);
373 /* Epoch; if one but not the other has an epoch set, default
374 * the epoch-less version to 0. */
375 res = (*p1 == ':') - (*p2 == ':');
380 } else if (res > 0) {
393 if (isdigit(*p1) && isdigit(*p2))
394 return razor_versioncmp(p1, p2);
401 razor_package_get_details_string(struct razor_set *set,
402 struct razor_package *package,
403 enum razor_detail_type type)
408 case RAZOR_DETAIL_NAME:
409 pool = set->string_pool.data;
410 return &pool[package->name];
412 case RAZOR_DETAIL_VERSION:
413 pool = set->string_pool.data;
414 return &pool[package->version];
416 case RAZOR_DETAIL_ARCH:
417 pool = set->string_pool.data;
418 return &pool[package->arch];
420 case RAZOR_DETAIL_SUMMARY:
421 pool = set->details_string_pool.data;
422 return &pool[package->summary];
424 case RAZOR_DETAIL_DESCRIPTION:
425 pool = set->details_string_pool.data;
426 return &pool[package->description];
428 case RAZOR_DETAIL_URL:
429 pool = set->details_string_pool.data;
430 return &pool[package->url];
432 case RAZOR_DETAIL_LICENSE:
433 pool = set->details_string_pool.data;
434 return &pool[package->license];
436 case RAZOR_DETAIL_PREUNPROG:
437 pool = set->string_pool.data;
438 return &pool[package->preun.program];
440 case RAZOR_DETAIL_PREUN:
441 pool = set->string_pool.data;
442 return &pool[package->preun.body];
444 case RAZOR_DETAIL_POSTUNPROG:
445 pool = set->string_pool.data;
446 return &pool[package->postun.program];
448 case RAZOR_DETAIL_POSTUN:
449 pool = set->string_pool.data;
450 return &pool[package->postun.body];
453 fprintf(stderr, "type %u not found\n", type);
458 static const char *const *
459 razor_package_get_details_array(struct razor_set *set,
460 struct razor_package *package,
461 enum razor_detail_type type)
464 case RAZOR_DETAIL_PREFIXES:
465 /* We don't track prefixes in packages. Install prefixes
466 * are tracked, but we don't provide an API to get them.
471 fprintf(stderr, "type %u not found\n", type);
477 * razor_package_get_details_varg:
479 * @package: a %razor_package
480 * @args: a va_list of arguments to set
483 razor_package_get_details_varg(struct razor_set *set,
484 struct razor_package *package,
488 enum razor_detail_type type;
490 const char *const **array;
492 for (i = 0;; i += 2) {
493 type = va_arg(args, enum razor_detail_type);
494 if (type == RAZOR_DETAIL_LAST)
496 if (type == RAZOR_DETAIL_PREFIXES) {
497 array = va_arg(args, const char *const **);
498 *array = razor_package_get_details_array(set, package,
501 string = va_arg(args, const char **);
502 *string = razor_package_get_details_string(set, package,
510 * razor_package_get_details:
512 * @package: a %razor_package
514 * Gets details about a package using a varg interface
515 * The vararg must be terminated with %RAZOR_DETAIL_LAST.
517 * Example: razor_package_get_details (set, package,
518 * RAZOR_DETAIL_URL, &url,
519 * RAZOR_DETAIL_LAST);
522 razor_package_get_details(struct razor_set *set, struct razor_package *package, ...)
526 assert (set != NULL);
527 assert (package != NULL);
529 va_start(args, NULL);
530 razor_package_get_details_varg (set, package, args);
535 * razor_package_remove:
536 * @prev: The %razor_set before the current transaction
537 * @next: The %razor_set after the current transaction is applied
538 * @package: a %razor_package
539 * @root: the root into which the package is currently installed
540 * @install_count: the value to pass to uninstall scripts
542 * Removes an installed package.
545 razor_package_remove(struct razor_set *prev, struct razor_set *next,
546 struct razor_package *package, const char *root,
549 struct razor_file_iterator *fi;
550 struct razor_package_iterator *pi;
551 struct razor_package *p;
552 char buffer[PATH_MAX];
553 const char *name, *program, *script;
554 int retval = 0, i, count;
555 struct environment env;
559 environment_init(&env);
560 link = list_first(&package->install_prefixes, &prev->prefix_pool);
561 for (i = 0; link; i++) {
562 prefix = (const char *)prev->string_pool.data + link->data;
563 sprintf(buffer, "RPM_INSTALL_PREFIX%d", i);
564 environment_add_variable(&env, buffer, prefix);
565 link = list_next(link);
567 environment_set(&env);
569 razor_package_get_details(prev, package,
570 RAZOR_DETAIL_PREUNPROG, &program,
571 RAZOR_DETAIL_PREUN, &script,
574 retval = razor_run_script(root, RAZOR_PROPERTY_PREUN, program, script,
577 fi = razor_file_iterator_create(prev, package, 1);
579 while (razor_file_iterator_next(fi, &name)) {
580 pi = razor_package_iterator_create_for_file(next, name);
582 while (razor_package_iterator_next(pi, &p, RAZOR_DETAIL_LAST))
584 razor_package_iterator_destroy(pi);
586 snprintf(buffer, sizeof buffer, "%s%s", root, name);
587 if (razor_remove(buffer) && errno != ENOENT) {
594 razor_file_iterator_destroy(fi);
596 razor_package_get_details(prev, package,
597 RAZOR_DETAIL_POSTUNPROG, &program,
598 RAZOR_DETAIL_POSTUN, &script,
601 if (razor_run_script(root, RAZOR_PROPERTY_POSTUN, program, script,
605 environment_unset(&env);
606 environment_release(&env);
611 RAZOR_EXPORT const char *
612 razor_property_relation_to_string(struct razor_property *p)
616 switch (p->flags & RAZOR_PROPERTY_RELATION_MASK) {
617 case RAZOR_PROPERTY_LESS:
620 case RAZOR_PROPERTY_LESS | RAZOR_PROPERTY_EQUAL:
623 case RAZOR_PROPERTY_EQUAL:
626 case RAZOR_PROPERTY_GREATER | RAZOR_PROPERTY_EQUAL:
629 case RAZOR_PROPERTY_GREATER:
637 RAZOR_EXPORT const char *
638 razor_property_type_to_string(struct razor_property *p)
642 switch (p->flags & RAZOR_PROPERTY_TYPE_MASK) {
643 case RAZOR_PROPERTY_REQUIRES:
645 case RAZOR_PROPERTY_PROVIDES:
647 case RAZOR_PROPERTY_CONFLICTS:
649 case RAZOR_PROPERTY_OBSOLETES:
656 RAZOR_EXPORT struct razor_entry *
657 razor_set_find_entry(struct razor_set *set,
658 struct razor_entry *dir, const char *pattern)
660 struct razor_entry *e, *subdir;
661 const char *n, *pool = set->file_string_pool.data;
664 assert (set != NULL);
665 assert (pattern != NULL);
673 if (strcmp(pattern, n) == 0)
676 if (e->start != 0 && strncmp(pattern, n, len) == 0 &&
677 pattern[len] == '/') {
678 subdir = (struct razor_entry *) set->files.data +
680 return razor_set_find_entry(set, subdir,
683 } while (!((e++)->flags & RAZOR_ENTRY_LAST));
689 list_dir(struct razor_set *set, struct razor_entry *dir,
690 char *prefix, const char *pattern)
692 struct razor_entry *e, *subdir;
693 const char *n, *pool = set->file_string_pool.data;
698 if (pattern && pattern[0] && fnmatch(pattern, n, 0) != 0)
700 printf("%s/%s\n", prefix, n);
702 char *sub = prefix + strlen (prefix);
705 subdir = (struct razor_entry *) set->files.data +
707 list_dir(set, subdir, prefix, pattern);
710 } while (!((e++)->flags & RAZOR_ENTRY_LAST));
714 razor_set_list_files(struct razor_set *set, const char *pattern)
716 struct razor_entry *root, *e;
717 char buffer[512], *p, *base;
719 assert (set != NULL);
721 root = (struct razor_entry *) set->files.data;
723 if (pattern == NULL) {
724 p = set->file_string_pool.data;
728 strcpy(buffer, p + e->name);
729 list_dir(set, root + e->start, buffer, NULL);
731 } while (!((e++)->flags & RAZOR_ENTRY_LAST));
735 strcpy(buffer, pattern);
736 e = razor_set_find_entry(set, root, buffer);
740 p = strrchr(buffer, '/');
748 e = razor_set_find_entry(set, root, buffer);
750 list_dir(set, root + e->start, buffer, base);
754 razor_set_list_package_files(struct razor_set *set,
755 struct razor_package *package)
757 struct razor_file_iterator *fi;
760 assert (set != NULL);
761 assert (package != NULL);
763 fi = razor_file_iterator_create(set, package, 0);
765 while (razor_file_iterator_next(fi, &name))
766 printf("%s\n", name);
768 razor_file_iterator_destroy(fi);
772 * Package data can potentially come from two places. The so-called
773 * metadata (eg., from comps.xml) and from an RPM file. We consider
774 * a package which has additional data from an RPM file as "fixed".
775 * If a package needs fixing, then razor_transaction_fixup_package()
776 * will do so. When considering what packages to add and to remove
777 * we need to take this into account since we always want to add
778 * unfixed packages (otherwise we have a potential conflict between
779 * the existing package data and that present in the RPM).
782 razor_package_is_fixed(struct razor_set *set, struct razor_package *p)
784 const char *preunprog, *preun, *postunprog, *postun;
788 razor_package_get_details(set, p,
789 RAZOR_DETAIL_PREUNPROG, &preunprog,
790 RAZOR_DETAIL_PREUN, &preun,
791 RAZOR_DETAIL_POSTUNPROG, &postunprog,
792 RAZOR_DETAIL_POSTUN, &postun,
794 return *preunprog || *preun || *postunprog || *postun;
798 razor_set_diff(struct razor_set *set, struct razor_set *upstream,
799 razor_diff_callback_t callback, void *data)
801 struct razor_package_iterator *pi1, *pi2;
802 struct razor_package *p1, *p2;
803 const char *name1, *name2, *version1, *version2, *arch1, *arch2;
804 int res, is_fixed1, is_fixed2;
806 assert (set != NULL);
807 assert (upstream != NULL);
809 pi1 = razor_package_iterator_create(set);
810 pi2 = razor_package_iterator_create(upstream);
812 razor_package_iterator_next(pi1, &p1,
813 RAZOR_DETAIL_NAME, &name1,
814 RAZOR_DETAIL_VERSION, &version1,
815 RAZOR_DETAIL_ARCH, &arch1,
817 is_fixed1 = razor_package_is_fixed(set, p1);
818 razor_package_iterator_next(pi2, &p2,
819 RAZOR_DETAIL_NAME, &name2,
820 RAZOR_DETAIL_VERSION, &version2,
821 RAZOR_DETAIL_ARCH, &arch2,
823 is_fixed2 = razor_package_is_fixed(upstream, p2);
827 res = strcmp(name1, name2);
829 res = razor_versioncmp(version1, version2);
831 res = is_fixed1 - is_fixed2;
836 if (p2 == NULL || res < 0)
837 callback(RAZOR_DIFF_ACTION_REMOVE,
838 p1, name1, version1, arch1, data);
839 else if (p1 == NULL || res > 0)
840 callback(RAZOR_DIFF_ACTION_ADD,
841 p2, name2, version2, arch2, data);
843 if (p1 != NULL && res <= 0) {
844 razor_package_iterator_next(pi1, &p1,
845 RAZOR_DETAIL_NAME, &name1,
846 RAZOR_DETAIL_VERSION, &version1,
847 RAZOR_DETAIL_ARCH, &arch1,
849 is_fixed1 = razor_package_is_fixed(set, p1);
851 if (p2 != NULL && res >= 0) {
852 razor_package_iterator_next(pi2, &p2,
853 RAZOR_DETAIL_NAME, &name2,
854 RAZOR_DETAIL_VERSION, &version2,
855 RAZOR_DETAIL_ARCH, &arch2,
857 is_fixed2 = razor_package_is_fixed(upstream, p2);
861 razor_package_iterator_destroy(pi1);
862 razor_package_iterator_destroy(pi2);
865 struct install_action {
866 enum razor_install_action action;
867 struct razor_package *package;
870 struct razor_install_iterator {
871 struct razor_set *set;
872 struct razor_set *next;
873 struct array actions;
878 add_action(enum razor_diff_action action,
879 struct razor_package *package,
885 struct razor_install_iterator *ii = data;
886 struct install_action *a;
888 a = array_add(&ii->actions, sizeof *a);
889 a->package = package;
892 case RAZOR_DIFF_ACTION_ADD:
893 a->action = RAZOR_INSTALL_ACTION_ADD;
895 case RAZOR_DIFF_ACTION_REMOVE:
896 a->action = RAZOR_INSTALL_ACTION_REMOVE;
901 RAZOR_EXPORT struct razor_install_iterator *
902 razor_set_create_install_iterator(struct razor_set *set,
903 struct razor_set *next)
905 struct razor_install_iterator *ii;
906 struct razor_property *prop;
907 /* A graph of the actions to be perfomed where
908 * A->B means action A should follow action B.
910 struct graph follows;
911 struct install_action *actions, *ai, *aj;
912 int i, j, count, vertex_added;
914 struct razor_set *rs;
916 assert (set != NULL);
917 assert (next != NULL);
919 ii = zalloc(sizeof *ii);
923 razor_set_diff(set, next, add_action, ii);
925 actions = ii->actions.data;
926 count = ii->actions.size / sizeof (struct install_action);
928 graph_init(&follows);
930 for(i = 0; i < count; i++) {
932 rs = ai->action == RAZOR_INSTALL_ACTION_ADD ? next : set;
934 link = list_first(&ai->package->properties, &rs->property_pool);
935 for(; link; link = list_next(link)) {
936 prop = rs->properties.data;
938 switch(prop->flags & RAZOR_PROPERTY_TYPE_MASK) {
939 case RAZOR_PROPERTY_REQUIRES:
940 case RAZOR_PROPERTY_CONFLICTS:
941 for(j = 0; j < count; j++) {
945 if (aj->package->name == prop->name) {
947 RAZOR_INSTALL_ACTION_ADD)
948 graph_add_edge(&follows,
951 graph_add_edge(&follows,
959 if (ai->action == RAZOR_INSTALL_ACTION_ADD) {
960 for(j = 0; j < count; j++) {
964 if (aj->package == ai->package &&
965 aj->action == RAZOR_INSTALL_ACTION_REMOVE) {
966 graph_add_edge(&follows, i, j);
972 graph_add_edge(&follows, i, i);
975 ii->order = graph_sort(&follows);
976 graph_release(&follows);
982 razor_install_iterator_next(struct razor_install_iterator *ii,
983 struct razor_package **package,
984 enum razor_install_action *action,
987 struct install_action *a;
988 struct razor_package_iterator *pi;
989 struct razor_package *pkg;
990 const char *removing, *name;
992 if (deque_empty(ii->order))
995 a = (struct install_action *)ii->actions.data + deque_pop(ii->order);
996 *package = a->package;
1000 if (a->action == RAZOR_INSTALL_ACTION_REMOVE) {
1001 razor_package_get_details(ii->set, a->package,
1002 RAZOR_DETAIL_NAME, &removing,
1005 pi = razor_package_iterator_create(ii->next);
1006 while (razor_package_iterator_next(pi, &pkg,
1007 RAZOR_DETAIL_NAME, &name,
1008 RAZOR_DETAIL_LAST)) {
1009 if (!strcmp(name, removing))
1012 razor_package_iterator_destroy(pi);
1019 razor_install_iterator_destroy(struct razor_install_iterator *ii)
1021 array_release(&ii->actions);
1022 deque_free(ii->order);