Add a test for relocations that are valid paths but would be invalid URIs if mis-interpreted
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009, 2011, 2012, 2014, 2016 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.
30 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
31 #define RAZOR_MALLOC __attribute__((__malloc__))
36 #define RAZOR_NULL_TERMINATED __attribute__ ((__sentinel__))
38 #define RAZOR_NULL_TERMINATED
42 enum razor_section_type {
43 RAZOR_SECTION_MAIN = 0x01,
44 RAZOR_SECTION_DETAILS = 0x02,
45 RAZOR_SECTION_FILES = 0x04,
46 RAZOR_SECTION_ALL = 0x07
49 enum razor_stage_type {
50 RAZOR_STAGE_SCRIPTS_PRE = 0x1,
51 RAZOR_STAGE_FILES = 0x2,
52 RAZOR_STAGE_SCRIPTS_POST = 0x4,
53 RAZOR_STAGE_SCRIPTS = 0x5,
57 enum razor_detail_type {
58 RAZOR_DETAIL_LAST = 0, /* the sentinel */
63 RAZOR_DETAIL_DESCRIPTION,
66 RAZOR_DETAIL_PREUNPROG,
68 RAZOR_DETAIL_POSTUNPROG,
73 enum razor_property_flags {
74 RAZOR_PROPERTY_LESS = 1 << 0,
75 RAZOR_PROPERTY_GREATER = 1 << 1,
76 RAZOR_PROPERTY_EQUAL = 1 << 2,
77 RAZOR_PROPERTY_RELATION_MASK =
79 RAZOR_PROPERTY_GREATER |
82 RAZOR_PROPERTY_REQUIRES = 0 << 3,
83 RAZOR_PROPERTY_PROVIDES = 1 << 3,
84 RAZOR_PROPERTY_CONFLICTS = 2 << 3,
85 RAZOR_PROPERTY_OBSOLETES = 3 << 3,
86 RAZOR_PROPERTY_TYPE_MASK = 3 << 3,
88 RAZOR_PROPERTY_PRE = 1 << 5,
89 RAZOR_PROPERTY_POST = 1 << 6,
90 RAZOR_PROPERTY_PREUN = 1 << 7,
91 RAZOR_PROPERTY_POSTUN = 1 << 8,
92 RAZOR_PROPERTY_SCRIPT_MASK =
95 RAZOR_PROPERTY_PREUN |
99 enum razor_set_flags {
101 * Create a private copy of the razor set such that changes made
102 * to the underlying file are not visible in the razor set.
103 * If this flag is not set then the caller must ensure that the
104 * underlying file (if any) is not changed.
106 RAZOR_SET_PRIVATE = 1 << 0,
111 * @title: Error reporting
112 * @short_description: A system for reporting errors.
114 * This object represents an error condition.
118 struct razor_error *razor_error_new_str(int domain, int code,
119 const char *object, const char *str);
120 struct razor_error *razor_error_dup(struct razor_error *src,
121 const char *summary);
123 #define razor_set_error(error, domain, code, object, str) \
125 *(error) = razor_error_new_str(domain, code, object, str); \
128 #define razor_propagate_error(dest, src, summary) \
130 *(dest) = razor_error_dup(src, summary); \
133 #define razor_error_matches(error, domain, code) \
134 ((error) && razor_error_get_domain(error) == (domain) && \
135 razor_error_get_code(error) == (code))
137 #define RAZOR_ERROR_DOMAIN(i1,i2,i3,c) \
138 (((i1)&0xff)<<24|((i2)&0xff)<<16|((i3)&0xff)<<8|(c)&0xff)
140 #define RAZOR_GENERAL_ERROR RAZOR_ERROR_DOMAIN('R','z','r',0)
141 #define RAZOR_POSIX_ERROR RAZOR_ERROR_DOMAIN('R','z','r',1)
142 #define RAZOR_MSWIN_ERROR RAZOR_ERROR_DOMAIN('R','z','r',2)
143 #define RAZOR_ZLIB_ERROR RAZOR_ERROR_DOMAIN('R','z','r',3)
145 enum razor_general_error {
146 RAZOR_GENERAL_ERROR_FAILED,
147 RAZOR_GENERAL_ERROR_DATABASE_CORRUPTED,
148 RAZOR_GENERAL_ERROR_DATABASE_INCOMPATIBLE,
149 RAZOR_GENERAL_ERROR_DATABASE_EXISTS,
150 RAZOR_GENERAL_ERROR_DATABASE_LOCKED,
151 RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
152 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
153 RAZOR_GENERAL_ERROR_BAD_URI,
154 RAZOR_GENERAL_ERROR_UNSUPPORTED_ARCHIVE,
157 int razor_error_get_domain(struct razor_error *error);
158 int razor_error_get_code(struct razor_error *error);
159 const char *razor_error_get_object(struct razor_error *error);
160 const char *razor_error_get_primary_text(struct razor_error *error);
161 const char *razor_error_get_secondary_text(struct razor_error *error);
162 const char *razor_error_get_msg(struct razor_error *error);
163 void razor_error_free(struct razor_error *error);
167 * @title: Atomic transactions
168 * @short_description: File-based transactions that shouldn't half-succeed
170 * This is a helper object for issuing a sequence of file-based actions
171 * that should either all succeed or all fail.
173 * Note that currently only Windows 7 has a native implementation and that
174 * while the emulated fallback implementation attempts to rollback the
175 * transaction if it fails, this is not guaranteed to succeed and that
176 * the transaction is held in core (and thus vulnernable to program crashes,
177 * power loss, etc.). This could (and should) be improved.
179 * Atomic transactions can be disabled via configure, in which case all
183 @RAZOR_HAVE_ATOMIC_ROLLBACK@
187 struct razor_atomic *razor_atomic_open(const char *description);
188 int razor_atomic_commit(struct razor_atomic *atomic);
189 struct razor_error *razor_atomic_get_error(struct razor_atomic *atomic);
190 const char *razor_atomic_get_error_msg(struct razor_atomic *atomic);
191 void razor_atomic_destroy(struct razor_atomic *atomic);
194 * razor_atomic_make_dirs
196 * Create all sub-directories leading up to uri. We know root_uri exists
197 * and is a dir, root_uri does not end in a '/', and uri either has a
198 * leading '/' or (on MS-Windows only) root_uri is the empty string
199 * and uri starts with drive (eg., "c:/windows").
200 * Note: uri itself is not created, only the directory in which it
203 * Returns: non-zero on error.
205 int razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root_uri,
207 int razor_atomic_remove(struct razor_atomic *atomic, const char *path);
208 int razor_atomic_rename_file(struct razor_atomic *atomic, const char *old_uri,
209 const char *new_uri);
212 * razor_atomic_create_dir
214 * Create a directory, replacing any existing object at this path except
215 * an existing directory (which is not counted as a error).
217 * Returns: non-zero on error.
219 int razor_atomic_create_dir(struct razor_atomic *atomic, const char *uri,
223 * razor_atomic_create_symlink
225 * Create a symbolic link, replacing any existing object at this path except
226 * a non-empty directory.
228 * Note: This function will always fail on platforms that don't support
231 * Returns: non-zero on error.
233 int razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
236 * razor_atomic_create_file
238 * Create a file, replacing any existing object at this path except
239 * a non-empty directory.
241 * Returns: A handle to be passed to razor_atomic_write() and
242 * razor_atomic_close() or a negative value on error.
244 int razor_atomic_create_file(struct razor_atomic *atomic, const char *uri,
246 int razor_atomic_write(struct razor_atomic *atomic, int handle,
247 const void *data, size_t size);
248 int razor_atomic_close(struct razor_atomic *atomic, int handle);
249 int razor_atomic_sync(struct razor_atomic *atomic, int handle);
250 void razor_atomic_abort(struct razor_atomic *atomic, int domain, int code,
251 const char *error_msg);
253 razor_atomic_propagate_error(struct razor_atomic *atomic,
254 struct razor_error *error, const char *summary);
255 int razor_atomic_in_error_state(struct razor_atomic *atomic);
259 * @title: Package Set
260 * @short_description: Represents a set of packages and their metadata.
262 * This object represents a set of packages, their dependency
263 * information, the file lists and a number of other details.
267 struct razor_package;
268 struct razor_property;
270 #define RAZOR_HEADER_VERSION 2 /* Current version */
271 #define RAZOR_HEADER_VERSION_MIN 1 /* Minimum version we support */
276 * Create a new #razor_set object.
278 * Returns: the new #razor_set object.
280 struct razor_set *razor_set_create_without_root(void);
281 struct razor_set *razor_set_create(void);
283 razor_set_open(const char *uri, enum razor_set_flags flags,
284 struct razor_error **error);
285 uint32_t razor_set_get_header_version(struct razor_set *set);
286 int razor_set_set_header_version(struct razor_set *set,
287 uint32_t header_version);
288 void razor_set_unref(struct razor_set *set);
289 struct razor_set *razor_set_ref(struct razor_set *set);
290 void razor_set_write_to_handle(struct razor_set *set,
291 struct razor_atomic *atomic, int handle,
292 uint32_t section_mask);
293 int razor_set_write(struct razor_set *set, struct razor_atomic *atomic,
294 const char *uri, uint32_t setions);
296 razor_set_bind_sections(struct razor_set *set, const char *uri,
297 enum razor_set_flags flags, struct razor_error **error);
300 razor_package_get_details(struct razor_set *set,
301 struct razor_package *package, ...);
303 razor_package_remove(struct razor_set *prev, struct razor_set *next,
304 struct razor_atomic *atomic, struct razor_package *package,
305 const char *root_uri, int install_count,
306 enum razor_stage_type stage);
311 * @short_description: Objects for traversing packages or properties.
313 * The razor iterator objects provides a way to iterate through a set
314 * of packages or properties.
317 struct razor_package_iterator;
320 * razor_package_iterator_create:
322 * Create a new #razor_package_iterator object.
324 * Returns: the new #razor_package_iterator object.
327 struct razor_package_iterator *
328 razor_package_iterator_create(struct razor_set *set);
331 * razor_package_iterator_create_for_property:
333 * Create a new #razor_package_iterator object for the packages that
334 * own the given property.
336 * Returns: the new #razor_package_iterator object.
338 struct razor_package_iterator *
339 razor_package_iterator_create_for_property(struct razor_set *set,
340 struct razor_property *property);
343 * razor_package_iterator_create_for_file:
345 * Create a new #razor_package_iterator object for the packages that
346 * contain the given file name.
348 * Returns: the new #razor_package_iterator object.
350 struct razor_package_iterator *
351 razor_package_iterator_create_for_file(struct razor_set *set,
352 const char *filename);
354 int razor_package_iterator_next(struct razor_package_iterator *pi,
355 struct razor_package **package, ...);
356 void razor_package_iterator_destroy(struct razor_package_iterator *pi);
358 struct razor_package_query *
359 razor_package_query_create(struct razor_set *set);
361 razor_package_query_add_package(struct razor_package_query *pq,
362 struct razor_package *p);
364 razor_package_query_add_iterator(struct razor_package_query *pq,
365 struct razor_package_iterator *pi);
366 struct razor_package_iterator *
367 razor_package_query_finish(struct razor_package_query *pq);
369 struct razor_property_iterator;
370 struct razor_property_iterator *
371 razor_property_iterator_create(struct razor_set *set,
372 struct razor_package *package);
373 int razor_property_iterator_next(struct razor_property_iterator *pi,
374 struct razor_property **property,
377 const char **version);
379 razor_property_iterator_destroy(struct razor_property_iterator *pi);
381 struct razor_file_iterator;
382 struct razor_file_iterator *
383 razor_file_iterator_create(struct razor_set *set,
384 struct razor_package *package, int post_order);
385 int razor_file_iterator_next(struct razor_file_iterator *fi,
387 void razor_file_iterator_destroy(struct razor_file_iterator *fi);
389 void razor_set_list_files(struct razor_set *set, const char *prefix);
390 void razor_set_list_package_files(struct razor_set *set,
391 struct razor_package *package);
393 struct razor_string_iterator;
394 struct razor_string_iterator *
395 razor_install_prefix_iterator_create(struct razor_set *set,
396 struct razor_package *package);
397 int razor_string_iterator_next(struct razor_string_iterator *si,
398 const char **string);
399 void razor_string_iterator_destroy(struct razor_string_iterator *si);
401 enum razor_diff_action {
402 RAZOR_DIFF_ACTION_ADD,
403 RAZOR_DIFF_ACTION_REMOVE,
406 typedef void (*razor_diff_callback_t)(enum razor_diff_action action,
407 struct razor_package *package,
414 razor_set_diff(struct razor_set *set, struct razor_set *upstream,
415 razor_diff_callback_t callback, void *data);
417 struct razor_install_iterator;
419 enum razor_install_action {
420 RAZOR_INSTALL_ACTION_ADD,
421 RAZOR_INSTALL_ACTION_REMOVE,
422 RAZOR_INSTALL_ACTION_COMMIT
425 struct razor_install_iterator *
426 razor_set_create_install_iterator(struct razor_set *set,
427 struct razor_set *next);
429 int razor_install_iterator_next(struct razor_install_iterator *ii,
430 struct razor_package **package,
431 enum razor_install_action *action,
435 * razor_install_iterator_commit_set
437 * Immediately after razor_install_iterator_next() returns
438 * RAZOR_INSTALL_ACTION_COMMIT, this function will return the razor_set
439 * which should be committed.
441 * Returns: a new #razor_set object.
444 razor_install_iterator_commit_set(struct razor_install_iterator *ii);
446 void razor_install_iterator_rewind(struct razor_install_iterator *ii);
447 size_t razor_install_iterator_tell(struct razor_install_iterator *ii);
448 size_t razor_install_iterator_seek(struct razor_install_iterator *ii,
450 void razor_install_iterator_destroy(struct razor_install_iterator *ii);
453 * SECTION:transaction
454 * @title: Transaction
455 * @short_description: Create a new package set by merging two or more sets.
457 * The razor transaction object provides a way to create a new package set
458 * from packages from one or more other package sets.
463 struct razor_transaction *
464 razor_transaction_create(struct razor_set *system, struct razor_set *upstream);
465 void razor_transaction_install_package(struct razor_transaction *transaction,
466 struct razor_package *package);
467 void razor_transaction_remove_package(struct razor_transaction *transaction,
468 struct razor_package *package);
469 void razor_transaction_update_package(struct razor_transaction *trans,
470 struct razor_package *package);
471 void razor_transaction_fixup_package(struct razor_transaction *trans,
472 struct razor_package *package,
473 struct razor_rpm *rpm);
474 void razor_transaction_update_all(struct razor_transaction *transaction);
475 int razor_transaction_resolve(struct razor_transaction *trans);
477 typedef void (*razor_unsatisfied_callback_t)(const char *requirement,
478 struct razor_package *package,
484 int razor_transaction_unsatisfied(struct razor_transaction *trans,
485 razor_unsatisfied_callback_t callback,
487 int razor_transaction_describe(struct razor_transaction *trans);
488 struct razor_set *razor_transaction_commit(struct razor_transaction *trans);
489 void razor_transaction_destroy(struct razor_transaction *trans);
491 /* Temporary helper for test suite. */
492 int razor_transaction_unsatisfied_property(struct razor_transaction *trans,
495 const char *version);
500 * @short_description: Operating on RPM files.
502 * Functions for open RPM files and extracting information and
503 * installing or removing RPM files.
506 struct razor_relocations;
508 struct razor_relocations *razor_relocations_create(void);
509 void razor_relocations_add(struct razor_relocations *relocations,
510 const char *oldpath, const char *newpath);
511 void razor_relocations_set_rpm(struct razor_relocations *relocations,
512 struct razor_rpm *rpm);
513 const char *razor_relocations_apply(struct razor_relocations *relocations,
515 void razor_relocations_destroy(struct razor_relocations *relocations);
517 struct razor_rpm *razor_rpm_open(const char *uri, struct razor_error **error);
518 void razor_rpm_get_details(struct razor_rpm *rpm, ...);
519 void razor_rpm_set_relocations(struct razor_rpm *rpm,
520 struct razor_relocations *relocations);
521 int razor_rpm_install(struct razor_rpm *rpm, struct razor_atomic *atomic,
522 const char *root_uri, int install_count,
523 enum razor_stage_type stage);
524 int razor_rpm_close(struct razor_rpm *rpm);
529 * @short_description: A mechanism for building #razor_set objects
531 * The %razor_importer is a helper object for building a razor set
532 * from external sources, like yum metadata, the RPM database or RPM
535 * The importer is a stateful object; it has the notion of a current
536 * package, and the caller can provide meta data such as properties,
537 * files and similiar for the package as it becomes available. Once a
538 * package is fully described, the next pacakge can begin. When all
539 * packages have been described to the importer, the importer will
540 * create a new %razor_set with the specified packages.
543 * of the importer will follow this template:
545 * importer = razor_importer_create();
547 * while ( /<!-- -->* more packages to import *<!-- -->/; ) {
548 * /<!-- -->* get name, version and arch of next package *<!-- -->/
549 * razor_importer_begin_package(importer, name, version, arch);
550 * razor_importer_add_details(importer, summary, description, url, license);
552 * while ( /<!-- -->* more properties to add *<!-- -->/ )
553 * razor_importer_add_property(importer, name, flags, version);
555 * while ( /<!-- -->* [more files to add *<!-- -->/ )
556 * razor_importer_add_file(importer, name);
558 * razor_importer_finish_package(importer);
561 * return razor_importer_finish(importer);
564 struct razor_importer;
566 struct razor_importer *razor_importer_create(void);
567 void razor_importer_destroy(struct razor_importer *importer);
568 void razor_importer_begin_package(struct razor_importer *importer,
572 void razor_importer_add_details(struct razor_importer *importer,
574 const char *description,
576 const char *license);
577 void razor_importer_add_script(struct razor_importer *importer,
578 enum razor_property_flags script,
581 void razor_importer_add_install_prefix(struct razor_importer *importer,
582 const char *install_prefix);
583 void razor_importer_add_property(struct razor_importer *importer,
586 const char *version);
587 void razor_importer_add_file(struct razor_importer *importer,
589 void razor_importer_finish_package(struct razor_importer *importer);
591 int razor_importer_add_rpm(struct razor_importer *importer,
592 struct razor_rpm *rpm);
594 struct razor_set *razor_importer_finish(struct razor_importer *importer);
599 * @short_description: Functions for accessing an install root.
601 * The #razor_root object encapsulate access to and locking of a razor
606 const char *razor_get_database_uri(void);
607 void razor_set_database_uri(const char *database_uri);
608 int razor_root_create(const char *root_uri, struct razor_error **error);
610 razor_root_open(const char *root_uri, struct razor_error **error);
612 razor_root_open_read_only(const char *root_uri, struct razor_error **error);
613 struct razor_set *razor_root_get_system_set(struct razor_root *root);
614 int razor_root_close(struct razor_root *root);
616 razor_root_update(struct razor_root *root, struct razor_set *next,
617 struct razor_atomic *atomic);
621 * @title: URI Input/Output
622 * @short_description: Functions to support non-file URI handlers
624 * Libraries that want to support URIs other than file URIs (which razor
625 * handles internally), can install their own URI handlers. Unsupported
626 * virtual functions should be set to %NULL. A fallback URI handler (which
627 * will be used if no scheme-specific handler is found) can be installed
628 * by passing scheme as %NULL. Finally, URI handlers can be removed by
629 * passing vtable as %NULL.
632 struct razor_uri_vtable {
633 unsigned structure_size;
634 int (*mkdir)(const char *path, mode_t mode, struct razor_error **error);
635 int (*unlink)(const char *path, struct razor_error **error);
636 int (*open)(const char *path, int flags, mode_t mode,
637 struct razor_error **error);
638 int (*move)(const char *path, const char *dest,
639 struct razor_error **error);
640 void *(*get_contents)(const char *path, size_t *length, int _private,
641 struct razor_error **error);
642 int (*free_contents)(void *addr, size_t length);
643 int (*is_directory)(const char *path, struct razor_error **error);
644 char *(*mkdtemp_near)(const char *path, const char *_template,
645 struct razor_error **error);
646 void *(*opendir)(const char *path, struct razor_error **error);
647 char *(*readdir)(void *dir, struct razor_error **error);
648 int (*closedir)(void *dir, struct razor_error **error);
651 int razor_uri_mkdir(const char *uri, mode_t mode, struct razor_error **error);
652 int razor_uri_unlink(const char *uri, struct razor_error **error);
653 int razor_uri_open(const char *uri, int flags, mode_t mode,
654 struct razor_error **error);
655 int razor_uri_move(const char *uri, const char *dest,
656 struct razor_error **error);
657 int razor_uri_is_directory(const char *uri, struct razor_error **error);
658 char *razor_uri_mkdtemp_near(const char *uri, const char *_template,
659 struct razor_error **error) RAZOR_MALLOC;
660 void *razor_uri_opendir(const char *uri, struct razor_error **error);
661 char *razor_uri_readdir(void *dir, struct razor_error **error) RAZOR_MALLOC;
662 int razor_uri_closedir(void *dir, struct razor_error **error);
663 void *razor_uri_get_contents(const char *uri, size_t *length, int _private,
664 struct razor_error **error);
665 int razor_uri_free_contents(void *addr, size_t length);
667 int razor_uri_set_vtable(const char *scheme, struct razor_uri_vtable *vtable,
668 struct razor_error **error);
672 * @title: Miscellaneous Functions
673 * @short_description: Various helper functions
675 * Functions that doesn't fit anywhere else.
679 razor_property_relation_to_string(struct razor_property *p);
681 razor_property_type_to_string(struct razor_property *p);
683 void razor_build_evr(char *evr_buf, int size, const char *epoch,
684 const char *version, const char *release);
685 int razor_versioncmp(const char *s1, const char *s2);
687 void razor_disable_root_name_checks(int disable);
689 void razor_set_lua_loader(const char *modname, void (*loader)());
690 void (*razor_get_lua_loader(const char *modname))();
692 char *razor_concat(const char *s, ...) RAZOR_MALLOC RAZOR_NULL_TERMINATED;
694 char *razor_path_from_uri(const char *uri, struct razor_error **error)
696 char *razor_path_to_uri(const char *path) RAZOR_MALLOC;
697 char *razor_path_relative_to_uri(const char *uri, const char *path,
698 struct razor_error **error) RAZOR_MALLOC;
700 /* According to RFC 3986 § 5.2 */
701 char *razor_resolve_relative_uri(const char *base_uri, const char *relative_uri,
702 struct razor_error **error);
704 const char *razor_system_arch(void);
706 int razor_dump_database(FILE *fp, const char *root_uri, const char *filename,
707 struct razor_error **error);
709 #endif /* _RAZOR_H_ */