2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009, 2011, 2012, 2014 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.
25 #include <sys/types.h>
29 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
30 #define RAZOR_MALLOC __attribute__((__malloc__))
35 #define RAZOR_NULL_TERMINATED __attribute__ ((__sentinel__))
37 #define RAZOR_NULL_TERMINATED
41 enum razor_section_type {
42 RAZOR_SECTION_MAIN = 0x01,
43 RAZOR_SECTION_DETAILS = 0x02,
44 RAZOR_SECTION_FILES = 0x04,
45 RAZOR_SECTION_ALL = 0x07
48 enum razor_stage_type {
49 RAZOR_STAGE_SCRIPTS_PRE = 0x1,
50 RAZOR_STAGE_FILES = 0x2,
51 RAZOR_STAGE_SCRIPTS_POST = 0x4,
52 RAZOR_STAGE_SCRIPTS = 0x5,
56 enum razor_detail_type {
57 RAZOR_DETAIL_LAST = 0, /* the sentinel */
62 RAZOR_DETAIL_DESCRIPTION,
65 RAZOR_DETAIL_PREUNPROG,
67 RAZOR_DETAIL_POSTUNPROG,
72 enum razor_property_flags {
73 RAZOR_PROPERTY_LESS = 1 << 0,
74 RAZOR_PROPERTY_GREATER = 1 << 1,
75 RAZOR_PROPERTY_EQUAL = 1 << 2,
76 RAZOR_PROPERTY_RELATION_MASK =
78 RAZOR_PROPERTY_GREATER |
81 RAZOR_PROPERTY_REQUIRES = 0 << 3,
82 RAZOR_PROPERTY_PROVIDES = 1 << 3,
83 RAZOR_PROPERTY_CONFLICTS = 2 << 3,
84 RAZOR_PROPERTY_OBSOLETES = 3 << 3,
85 RAZOR_PROPERTY_TYPE_MASK = 3 << 3,
87 RAZOR_PROPERTY_PRE = 1 << 5,
88 RAZOR_PROPERTY_POST = 1 << 6,
89 RAZOR_PROPERTY_PREUN = 1 << 7,
90 RAZOR_PROPERTY_POSTUN = 1 << 8,
91 RAZOR_PROPERTY_SCRIPT_MASK =
94 RAZOR_PROPERTY_PREUN |
98 enum razor_set_flags {
100 * Create a private copy of the razor set such that changes made
101 * to the underlying file are not visible in the razor set.
102 * If this flag is not set then the caller must ensure that the
103 * underlying file (if any) is not changed.
105 RAZOR_SET_PRIVATE = 1 << 0,
110 * @title: Error reporting
111 * @short_description: A system for reporting errors.
113 * This object represents an error condition.
117 struct razor_error *razor_error_new_str(int domain, int code,
118 const char *object, const char *str);
119 struct razor_error *razor_error_dup(struct razor_error *src,
120 const char *summary);
122 #define razor_set_error(error, domain, code, object, str) \
124 *(error) = razor_error_new_str(domain, code, object, str); \
127 #define razor_propagate_error(dest, src, summary) \
129 *(dest) = razor_error_dup(src, summary); \
132 #define RAZOR_ERROR_DOMAIN(i1,i2,i3,c) \
133 (((i1)&0xff)<<24|((i2)&0xff)<<16|((i3)&0xff)<<8|(c)&0xff)
135 #define RAZOR_GENERAL_ERROR RAZOR_ERROR_DOMAIN('R','z','r',0)
136 #define RAZOR_POSIX_ERROR RAZOR_ERROR_DOMAIN('R','z','r',1)
137 #define RAZOR_MSWIN_ERROR RAZOR_ERROR_DOMAIN('R','z','r',2)
138 #define RAZOR_ZLIB_ERROR RAZOR_ERROR_DOMAIN('R','z','r',3)
140 enum razor_general_error {
141 RAZOR_GENERAL_ERROR_FAILED,
142 RAZOR_GENERAL_ERROR_DATABASE_CORRUPTED,
143 RAZOR_GENERAL_ERROR_DATABASE_INCOMPATIBLE,
144 RAZOR_GENERAL_ERROR_DATABASE_EXISTS,
145 RAZOR_GENERAL_ERROR_DATABASE_LOCKED,
146 RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
149 int razor_error_get_domain(struct razor_error *error);
150 int razor_error_get_code(struct razor_error *error);
151 const char *razor_error_get_object(struct razor_error *error);
152 const char *razor_error_get_primary_text(struct razor_error *error);
153 const char *razor_error_get_secondary_text(struct razor_error *error);
154 const char *razor_error_get_msg(struct razor_error *error);
155 void razor_error_free(struct razor_error *error);
159 * @title: Atomic transactions
160 * @short_description: File-based transactions that shouldn't half-succeed
162 * This is a helper object for issuing a sequence of file-based actions
163 * that should either all succeed or all fail.
165 * Note that currently only Windows 7 has a native implementation and that
166 * while the emulated fallback implementation attempts to rollback the
167 * transaction if it fails, this is not guaranteed to succeed and that
168 * the transaction is held in core (and thus vulnernable to program crashes,
169 * power loss, etc.). This could (and should) be improved.
171 * Atomic transactions can be disabled via configure, in which case all
176 struct razor_atomic *razor_atomic_open(const char *description);
177 int razor_atomic_commit(struct razor_atomic *atomic);
178 struct razor_error *razor_atomic_get_error(struct razor_atomic *atomic);
179 const char *razor_atomic_get_error_msg(struct razor_atomic *atomic);
180 void razor_atomic_destroy(struct razor_atomic *atomic);
183 * razor_atomic_make_dirs
185 * Create all sub-directories leading up to path. We know root exists
186 * and is a dir, root does not end in a '/', and path either has a
187 * leading '/' or (on MS-Windows only) root is the empty string
188 * and path starts with drive (eg., "c:/windows").
189 * Note: path itself is not created, only the directory in which it
192 * Returns: non-zero on error.
194 int razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
196 int razor_atomic_remove(struct razor_atomic *atomic, const char *path);
197 int razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
198 const char *newpath);
201 * razor_atomic_create_dir
203 * Create a directory, replacing any existing object at this path except
204 * an existing directory (which is not counted as a error).
206 * Returns: non-zero on error.
208 int razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
212 * razor_atomic_create_symlink
214 * Create a symbolic link, replacing any existing object at this path except
215 * a non-empty directory.
217 * Note: This function will always fail on platforms that don't support
220 * Returns: non-zero on error.
222 int razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
225 * razor_atomic_create_file
227 * Create a file, replacing any existing object at this path except
228 * a non-empty directory.
230 * Returns: A handle to be passed to razor_atomic_write() and
231 * razor_atomic_close() or a negative value on error.
233 int razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
235 int razor_atomic_write(struct razor_atomic *atomic, int handle,
236 const void *data, size_t size);
237 int razor_atomic_close(struct razor_atomic *atomic, int handle);
238 int razor_atomic_sync(struct razor_atomic *atomic, int handle);
239 void razor_atomic_abort(struct razor_atomic *atomic, int domain, int code,
240 const char *error_msg);
241 void razor_atomic_propagate_error(struct razor_atomic *atomic,
242 struct razor_error *error, const char *summary);
243 int razor_atomic_in_error_state(struct razor_atomic *atomic);
247 * @title: Package Set
248 * @short_description: Represents a set of packages and their metadata.
250 * This object represents a set of packages, their dependency
251 * information, the file lists and a number of other details.
255 struct razor_package;
256 struct razor_property;
258 #define RAZOR_HEADER_VERSION 2 /* Current version */
259 #define RAZOR_HEADER_VERSION_MIN 1 /* Minimum version we support */
264 * Create a new #razor_set object.
266 * Returns: the new #razor_set object.
268 struct razor_set *razor_set_create_without_root(void);
269 struct razor_set *razor_set_create(void);
271 razor_set_open(const char *filename, enum razor_set_flags flags,
272 struct razor_error **error);
273 uint32_t razor_set_get_header_version(struct razor_set *set);
274 int razor_set_set_header_version(struct razor_set *set,
275 uint32_t header_version);
276 void razor_set_unref(struct razor_set *set);
277 struct razor_set *razor_set_ref(struct razor_set *set);
278 void razor_set_write_to_handle(struct razor_set *set,
279 struct razor_atomic *atomic, int handle,
280 uint32_t section_mask);
281 int razor_set_write(struct razor_set *set, struct razor_atomic *atomic,
282 const char *filename, uint32_t setions);
284 razor_set_bind_sections(struct razor_set *set, const char *filename,
285 enum razor_set_flags flags, struct razor_error **error);
288 razor_package_get_details(struct razor_set *set,
289 struct razor_package *package, ...);
291 razor_package_remove(struct razor_set *prev, struct razor_set *next,
292 struct razor_atomic *atomic, struct razor_package *package,
293 const char *root, int install_count,
294 enum razor_stage_type stage);
299 * @short_description: Objects for traversing packages or properties.
301 * The razor iterator objects provides a way to iterate through a set
302 * of packages or properties.
305 struct razor_package_iterator;
308 * razor_package_iterator_create:
310 * Create a new #razor_package_iterator object.
312 * Returns: the new #razor_package_iterator object.
315 struct razor_package_iterator *
316 razor_package_iterator_create(struct razor_set *set);
319 * razor_package_iterator_create_for_property:
321 * Create a new #razor_package_iterator object for the packages that
322 * own the given property.
324 * Returns: the new #razor_package_iterator object.
326 struct razor_package_iterator *
327 razor_package_iterator_create_for_property(struct razor_set *set,
328 struct razor_property *property);
331 * razor_package_iterator_create_for_file:
333 * Create a new #razor_package_iterator object for the packages that
334 * contain the given file name.
336 * Returns: the new #razor_package_iterator object.
338 struct razor_package_iterator *
339 razor_package_iterator_create_for_file(struct razor_set *set,
340 const char *filename);
342 int razor_package_iterator_next(struct razor_package_iterator *pi,
343 struct razor_package **package, ...);
344 void razor_package_iterator_destroy(struct razor_package_iterator *pi);
346 struct razor_package_query *
347 razor_package_query_create(struct razor_set *set);
349 razor_package_query_add_package(struct razor_package_query *pq,
350 struct razor_package *p);
352 razor_package_query_add_iterator(struct razor_package_query *pq,
353 struct razor_package_iterator *pi);
354 struct razor_package_iterator *
355 razor_package_query_finish(struct razor_package_query *pq);
357 struct razor_property_iterator;
358 struct razor_property_iterator *
359 razor_property_iterator_create(struct razor_set *set,
360 struct razor_package *package);
361 int razor_property_iterator_next(struct razor_property_iterator *pi,
362 struct razor_property **property,
365 const char **version);
367 razor_property_iterator_destroy(struct razor_property_iterator *pi);
369 struct razor_file_iterator;
370 struct razor_file_iterator *
371 razor_file_iterator_create(struct razor_set *set,
372 struct razor_package *package, int post_order);
373 int razor_file_iterator_next(struct razor_file_iterator *fi,
375 void razor_file_iterator_destroy(struct razor_file_iterator *fi);
377 void razor_set_list_files(struct razor_set *set, const char *prefix);
378 void razor_set_list_package_files(struct razor_set *set,
379 struct razor_package *package);
381 enum razor_diff_action {
382 RAZOR_DIFF_ACTION_ADD,
383 RAZOR_DIFF_ACTION_REMOVE,
386 typedef void (*razor_diff_callback_t)(enum razor_diff_action action,
387 struct razor_package *package,
394 razor_set_diff(struct razor_set *set, struct razor_set *upstream,
395 razor_diff_callback_t callback, void *data);
397 struct razor_install_iterator;
399 enum razor_install_action {
400 RAZOR_INSTALL_ACTION_ADD,
401 RAZOR_INSTALL_ACTION_REMOVE,
402 RAZOR_INSTALL_ACTION_COMMIT
405 struct razor_install_iterator *
406 razor_set_create_install_iterator(struct razor_set *set,
407 struct razor_set *next);
409 int razor_install_iterator_next(struct razor_install_iterator *ii,
410 struct razor_package **package,
411 enum razor_install_action *action,
415 * razor_install_iterator_commit_set
417 * Immediately after razor_install_iterator_next() returns
418 * RAZOR_INSTALL_ACTION_COMMIT, this function will return the razor_set
419 * which should be committed.
421 * Returns: a new #razor_set object.
424 razor_install_iterator_commit_set(struct razor_install_iterator *ii);
426 void razor_install_iterator_rewind(struct razor_install_iterator *ii);
427 size_t razor_install_iterator_tell(struct razor_install_iterator *ii);
428 size_t razor_install_iterator_seek(struct razor_install_iterator *ii,
430 void razor_install_iterator_destroy(struct razor_install_iterator *ii);
433 * SECTION:transaction
434 * @title: Transaction
435 * @short_description: Create a new package set by merging two or more sets.
437 * The razor transaction object provides a way to create a new package set
438 * from packages from one or more other package sets.
443 struct razor_transaction *
444 razor_transaction_create(struct razor_set *system, struct razor_set *upstream);
445 void razor_transaction_install_package(struct razor_transaction *transaction,
446 struct razor_package *package);
447 void razor_transaction_remove_package(struct razor_transaction *transaction,
448 struct razor_package *package);
449 void razor_transaction_update_package(struct razor_transaction *trans,
450 struct razor_package *package);
451 void razor_transaction_fixup_package(struct razor_transaction *trans,
452 struct razor_package *package,
453 struct razor_rpm *rpm);
454 void razor_transaction_update_all(struct razor_transaction *transaction);
455 int razor_transaction_resolve(struct razor_transaction *trans);
457 typedef void (*razor_unsatisfied_callback_t)(const char *requirement,
458 struct razor_package *package,
464 int razor_transaction_unsatisfied(struct razor_transaction *trans,
465 razor_unsatisfied_callback_t callback,
467 int razor_transaction_describe(struct razor_transaction *trans);
468 struct razor_set *razor_transaction_commit(struct razor_transaction *trans);
469 void razor_transaction_destroy(struct razor_transaction *trans);
471 /* Temporary helper for test suite. */
472 int razor_transaction_unsatisfied_property(struct razor_transaction *trans,
475 const char *version);
480 * @short_description: Operating on RPM files.
482 * Functions for open RPM files and extracting information and
483 * installing or removing RPM files.
486 struct razor_relocations;
488 struct razor_relocations *razor_relocations_create(void);
489 void razor_relocations_add(struct razor_relocations *relocations,
490 const char *oldpath, const char *newpath);
491 void razor_relocations_set_rpm(struct razor_relocations *relocations,
492 struct razor_rpm *rpm);
493 const char *razor_relocations_apply(struct razor_relocations *relocations,
495 void razor_relocations_destroy(struct razor_relocations *relocations);
497 struct razor_rpm *razor_rpm_open(const char *filename,
498 struct razor_error **error);
499 void razor_rpm_get_details(struct razor_rpm *rpm, ...);
500 void razor_rpm_set_relocations(struct razor_rpm *rpm,
501 struct razor_relocations *relocations);
502 int razor_rpm_install(struct razor_rpm *rpm, struct razor_atomic *atomic,
503 const char *root, int install_count,
504 enum razor_stage_type stage);
505 int razor_rpm_close(struct razor_rpm *rpm);
510 * @short_description: A mechanism for building #razor_set objects
512 * The %razor_importer is a helper object for building a razor set
513 * from external sources, like yum metadata, the RPM database or RPM
516 * The importer is a stateful object; it has the notion of a current
517 * package, and the caller can provide meta data such as properties,
518 * files and similiar for the package as it becomes available. Once a
519 * package is fully described, the next pacakge can begin. When all
520 * packages have been described to the importer, the importer will
521 * create a new %razor_set with the specified packages.
524 * of the importer will follow this template:
526 * importer = razor_importer_create();
528 * while ( /<!-- -->* more packages to import *<!-- -->/; ) {
529 * /<!-- -->* get name, version and arch of next package *<!-- -->/
530 * razor_importer_begin_package(importer, name, version, arch);
531 * razor_importer_add_details(importer, summary, description, url, license);
533 * while ( /<!-- -->* more properties to add *<!-- -->/ )
534 * razor_importer_add_property(importer, name, flags, version);
536 * while ( /<!-- -->* [more files to add *<!-- -->/ )
537 * razor_importer_add_file(importer, name);
539 * razor_importer_finish_package(importer);
542 * return razor_importer_finish(importer);
545 struct razor_importer;
547 struct razor_importer *razor_importer_create(void);
548 void razor_importer_destroy(struct razor_importer *importer);
549 void razor_importer_begin_package(struct razor_importer *importer,
553 void razor_importer_add_details(struct razor_importer *importer,
555 const char *description,
557 const char *license);
558 void razor_importer_add_script(struct razor_importer *importer,
559 enum razor_property_flags script,
562 void razor_importer_add_install_prefix(struct razor_importer *importer,
563 const char *install_prefix);
564 void razor_importer_add_property(struct razor_importer *importer,
567 const char *version);
568 void razor_importer_add_file(struct razor_importer *importer,
570 void razor_importer_finish_package(struct razor_importer *importer);
572 int razor_importer_add_rpm(struct razor_importer *importer,
573 struct razor_rpm *rpm);
575 struct razor_set *razor_importer_finish(struct razor_importer *importer);
577 struct razor_set *razor_set_create_from_yum(void);
578 struct razor_set *razor_set_create_from_rpmdb(void);
583 * @short_description: Functions for accessing an install root.
585 * The #razor_root object encapsulate access to and locking of a razor
590 const char *razor_get_database_path();
591 void razor_set_database_path(const char *database_path);
592 int razor_root_create(const char *root, struct razor_error **error);
594 razor_root_open(const char *root, struct razor_error **error);
596 razor_root_open_read_only(const char *root, struct razor_error **error);
597 struct razor_set *razor_root_get_system_set(struct razor_root *root);
598 int razor_root_close(struct razor_root *root);
600 razor_root_update(struct razor_root *root, struct razor_set *next,
601 struct razor_atomic *atomic);
605 * @title: Miscellaneous Functions
606 * @short_description: Various helper functions
608 * Functions that doesn't fit anywhere else.
612 razor_property_relation_to_string(struct razor_property *p);
614 razor_property_type_to_string(struct razor_property *p);
616 void razor_build_evr(char *evr_buf, int size, const char *epoch,
617 const char *version, const char *release);
618 int razor_versioncmp(const char *s1, const char *s2);
620 void razor_disable_root_name_checks(int disable);
622 void razor_set_lua_loader(const char *modname, void (*loader)());
623 void (*razor_get_lua_loader(const char *modname))();
625 char *razor_concat(const char *s, ...) RAZOR_MALLOC RAZOR_NULL_TERMINATED;
627 char *razor_path_add_root(const char *path, const char *root) RAZOR_MALLOC;
629 const char *razor_system_arch(void);
631 #endif /* _RAZOR_H_ */