librazor/razor.h
author J. Ali Harlow <ali@juiblex.co.uk>
Mon Sep 01 12:19:32 2014 +0100 (2014-09-01)
changeset 444 a2908416b7cc
parent 439 f28bb31024b4
child 445 aada48958b92
permissions -rw-r--r--
testsuite portability fixes
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  * Copyright (C) 2009, 2011, 2012  J. Ali Harlow <ali@juiblex.co.uk>
     5  *
     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.
    10  *
    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.
    15  *
    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.
    19  */
    20 
    21 #ifndef _RAZOR_H_
    22 #define _RAZOR_H_
    23 
    24 #include <stdint.h>
    25 #include <sys/types.h>
    26 
    27 /* GCC extensions */
    28 #if defined(__GNUC__)
    29 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
    30 #define RAZOR_MALLOC __attribute__((__malloc__))
    31 #else
    32 #define RAZOR_MALLOC
    33 #endif
    34 #if __GNUC__ >= 4
    35 #define RAZOR_NULL_TERMINATED __attribute__ ((__sentinel__))
    36 #else
    37 #define RAZOR_NULL_TERMINATED
    38 #endif
    39 #endif	/* __GNUC__ */
    40 
    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
    46 };
    47 
    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,
    53 	RAZOR_STAGE_ALL = 0x7
    54 };
    55 
    56 enum razor_detail_type {
    57 	RAZOR_DETAIL_LAST = 0,	/* the sentinel */
    58 	RAZOR_DETAIL_NAME,
    59 	RAZOR_DETAIL_VERSION,
    60 	RAZOR_DETAIL_ARCH,
    61 	RAZOR_DETAIL_SUMMARY,
    62 	RAZOR_DETAIL_DESCRIPTION,
    63 	RAZOR_DETAIL_URL,
    64 	RAZOR_DETAIL_LICENSE,
    65 	RAZOR_DETAIL_PREUNPROG,
    66 	RAZOR_DETAIL_PREUN,
    67 	RAZOR_DETAIL_POSTUNPROG,
    68 	RAZOR_DETAIL_POSTUN,
    69 	RAZOR_DETAIL_PREFIXES
    70 };
    71 
    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	=
    77 		RAZOR_PROPERTY_LESS |
    78 		RAZOR_PROPERTY_GREATER |
    79 		RAZOR_PROPERTY_EQUAL,
    80 
    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,
    86 		
    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	=
    92 		RAZOR_PROPERTY_PRE |
    93 		RAZOR_PROPERTY_POST |
    94 		RAZOR_PROPERTY_PREUN |
    95 		RAZOR_PROPERTY_POSTUN
    96 };
    97 
    98 enum razor_set_flags {
    99 	/*
   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.
   104 	 */
   105 	RAZOR_SET_PRIVATE		= 1 << 0,
   106 };
   107 
   108 /**
   109  * SECTION:error
   110  * @title: Error reporting
   111  * @short_description: A system for reporting errors.
   112  *
   113  * This object represents an error condition.
   114  */
   115 struct razor_error;
   116 
   117 struct razor_error *razor_error_new_str(const char *path, const char *str);
   118 struct razor_error *razor_error_dup(struct razor_error *src,
   119 				    const char *summary);
   120 
   121 #define razor_set_error(error, path, str) \
   122 	if (error) \
   123 		*(error) = razor_error_new_str(path, str); \
   124 	else
   125 
   126 #define razor_propagate_error(dest, src, summary) \
   127 	if (dest) \
   128 		*(dest) = razor_error_dup(src, summary); \
   129 	else
   130 
   131 const char *razor_error_get_primary_text(struct razor_error *error);
   132 const char *razor_error_get_secondary_text(struct razor_error *error);
   133 const char *razor_error_get_msg(struct razor_error *error);
   134 void razor_error_free(struct razor_error *error);
   135 
   136 /**
   137  * SECTION:atomic
   138  * @title: Atomic transactions
   139  * @short_description: File-based transactions that shouldn't half-succeed
   140  *
   141  * This is a helper object for issuing a sequence of file-based actions
   142  * that should either all succeed or all fail.
   143  *
   144  * Note that currently only Windows 7 has a native implementation and that
   145  * while the emulated fallback implementation attempts to rollback the
   146  * transaction if it fails, this is not guaranteed to succeed and that
   147  * the transaction is held in core (and thus vulnernable to program crashes,
   148  * power loss, etc.). This could (and should) be improved.
   149  *
   150  * Atomic transactions can be disabled via configure, in which case all
   151  * bets are off.
   152  **/
   153 struct razor_atomic;
   154 
   155 struct razor_atomic *razor_atomic_open(const char *description);
   156 int razor_atomic_commit(struct razor_atomic *atomic);
   157 struct razor_error *razor_atomic_get_error(struct razor_atomic *atomic);
   158 const char *razor_atomic_get_error_msg(struct razor_atomic *atomic);
   159 void razor_atomic_destroy(struct razor_atomic *atomic);
   160 
   161 /**
   162  * razor_atomic_make_dirs
   163  * 
   164  * Create all sub-directories leading up to path. We know root exists
   165  * and is a dir, root does not end in a '/', and path either has a
   166  * leading '/' or (on MS-Windows only) root is the empty string
   167  * and path starts with drive (eg., "c:/windows").
   168  * Note: path itself is not created, only the directory in which it
   169  * (would) exist.
   170  *
   171  * Returns: non-zero on error.
   172  **/
   173 int razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
   174   const char *path);
   175 int razor_atomic_remove(struct razor_atomic *atomic, const char *path);
   176 int razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath,
   177 			     const char *newpath);
   178 
   179 /**
   180  * razor_atomic_create_dir
   181  * 
   182  * Create a directory, replacing any existing object at this path except
   183  * an existing directory (which is not counted as a error).
   184  *
   185  * Returns: non-zero on error.
   186  */
   187 int razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname,
   188   mode_t mode);
   189 
   190 /**
   191  * razor_atomic_create_symlink
   192  * 
   193  * Create a symbolic link, replacing any existing object at this path except
   194  * a non-empty directory.
   195  *
   196  * Note: This function will always fail on platforms that don't support
   197  * symbolic links.
   198  *
   199  * Returns: non-zero on error.
   200  */
   201 int razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
   202   const char *path);
   203 /**
   204  * razor_atomic_create_file
   205  * 
   206  * Create a file, replacing any existing object at this path except
   207  * a non-empty directory.
   208  *
   209  * Returns: A handle to be passed to razor_atomic_write() and
   210  * razor_atomic_close() or a negative value on error.
   211  */
   212 int razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
   213   mode_t mode);
   214 int razor_atomic_write(struct razor_atomic *atomic, int handle,
   215   const void *data, size_t size);
   216 int razor_atomic_close(struct razor_atomic *atomic, int handle);
   217 int razor_atomic_sync(struct razor_atomic *atomic, int handle);
   218 void razor_atomic_abort(struct razor_atomic *atomic, const char *error_msg);
   219 int razor_atomic_in_error_state(struct razor_atomic *atomic);
   220 
   221 /**
   222  * SECTION:set
   223  * @title: Package Set
   224  * @short_description: Represents a set of packages and their metadata.
   225  *
   226  * This object represents a set of packages, their dependency
   227  * information, the file lists and a number of other details.
   228  **/
   229 
   230 struct razor_set;
   231 struct razor_package;
   232 struct razor_property;
   233 
   234 #define RAZOR_HEADER_VERSION		2	/* Current version */
   235 #define RAZOR_HEADER_VERSION_MIN	1	/* Minimum version we support */
   236 
   237 /**
   238  * razor_set_create:
   239  * 
   240  * Create a new #razor_set object.
   241  * 
   242  * Returns: the new #razor_set object.
   243  **/
   244 struct razor_set *razor_set_create_without_root(void);
   245 struct razor_set *razor_set_create(void);
   246 struct razor_set *
   247 razor_set_open(const char *filename, enum razor_set_flags flags,
   248 	       struct razor_error **error);
   249 uint32_t razor_set_get_header_version(struct razor_set *set);
   250 int razor_set_set_header_version(struct razor_set *set,
   251 				 uint32_t header_version);
   252 void razor_set_unref(struct razor_set *set);
   253 struct razor_set *razor_set_ref(struct razor_set *set);
   254 void razor_set_write_to_handle(struct razor_set *set,
   255 			       struct razor_atomic *atomic, int handle,
   256 			       uint32_t section_mask);
   257 int razor_set_write(struct razor_set *set, struct razor_atomic *atomic,
   258 		    const char *filename, uint32_t setions);
   259 int
   260 razor_set_bind_sections(struct razor_set *set, const char *filename,
   261 			enum razor_set_flags flags, struct razor_error **error);
   262 
   263 struct razor_package *
   264 razor_set_get_package(struct razor_set *set, const char *package);
   265 
   266 void
   267 razor_package_get_details(struct razor_set *set,
   268 			  struct razor_package *package, ...);
   269 int
   270 razor_package_remove(struct razor_set *prev, struct razor_set *next,
   271 		     struct razor_atomic *atomic, struct razor_package *package,
   272 		     const char *root, int install_count,
   273 		     enum razor_stage_type stage);
   274 
   275 /**
   276  * SECTION:iterator
   277  * @title: Iterators
   278  * @short_description: Objects for traversing packages or properties.
   279  *
   280  * The razor iterator objects provides a way to iterate through a set
   281  * of packages or properties.
   282  **/
   283 
   284 struct razor_package_iterator;
   285 
   286 /**
   287  * razor_package_iterator_create:
   288  * 
   289  * Create a new #razor_package_iterator object.
   290  * 
   291  * Returns: the new #razor_package_iterator object.
   292  **/
   293 
   294 struct razor_package_iterator *
   295 razor_package_iterator_create(struct razor_set *set);
   296 
   297 /**
   298  * razor_package_iterator_create_for_property:
   299  * 
   300  * Create a new #razor_package_iterator object for the packages that
   301  * own the given property.
   302  * 
   303  * Returns: the new #razor_package_iterator object.
   304  **/
   305 struct razor_package_iterator *
   306 razor_package_iterator_create_for_property(struct razor_set *set,
   307 					   struct razor_property *property);
   308 
   309 /**
   310  * razor_package_iterator_create_for_file:
   311  *
   312  * Create a new #razor_package_iterator object for the packages that
   313  * contain the given file name.
   314  *
   315  * Returns: the new #razor_package_iterator object.
   316  **/
   317 struct razor_package_iterator *
   318 razor_package_iterator_create_for_file(struct razor_set *set,
   319 				       const char *filename);
   320 
   321 int razor_package_iterator_next(struct razor_package_iterator *pi,
   322 				struct razor_package **package, ...);
   323 void razor_package_iterator_destroy(struct razor_package_iterator *pi);
   324 
   325 struct razor_package_query *
   326 razor_package_query_create(struct razor_set *set);
   327 void
   328 razor_package_query_add_package(struct razor_package_query *pq,
   329 				struct razor_package *p);
   330 void
   331 razor_package_query_add_iterator(struct razor_package_query *pq,
   332 				 struct razor_package_iterator *pi);
   333 struct razor_package_iterator *
   334 razor_package_query_finish(struct razor_package_query *pq);
   335 
   336 struct razor_property_iterator;
   337 struct razor_property_iterator *
   338 razor_property_iterator_create(struct razor_set *set,
   339 			       struct razor_package *package);
   340 int razor_property_iterator_next(struct razor_property_iterator *pi,
   341 				 struct razor_property **property,
   342 				 const char **name,
   343 				 uint32_t *flags,
   344 				 const char **version);
   345 void
   346 razor_property_iterator_destroy(struct razor_property_iterator *pi);
   347 
   348 struct razor_file_iterator;
   349 struct razor_file_iterator *
   350 razor_file_iterator_create(struct razor_set *set,
   351 			   struct razor_package *package, int post_order);
   352 int razor_file_iterator_next(struct razor_file_iterator *fi,
   353 			     const char **name);
   354 void razor_file_iterator_destroy(struct razor_file_iterator *fi);
   355 
   356 void razor_set_list_files(struct razor_set *set, const char *prefix);
   357 void razor_set_list_package_files(struct razor_set *set,
   358 				  struct razor_package *package);
   359 
   360 enum razor_diff_action {
   361 	RAZOR_DIFF_ACTION_ADD,
   362 	RAZOR_DIFF_ACTION_REMOVE,
   363 };
   364 
   365 typedef void (*razor_diff_callback_t)(enum razor_diff_action action,
   366 				      struct razor_package *package,
   367 				      const char *name,
   368 				      const char *version,
   369 				      const char *arch,
   370 				      void *data);
   371 
   372 void
   373 razor_set_diff(struct razor_set *set, struct razor_set *upstream,
   374 	       razor_diff_callback_t callback, void *data);
   375 
   376 struct razor_install_iterator;
   377 
   378 enum razor_install_action {
   379 	RAZOR_INSTALL_ACTION_ADD,
   380 	RAZOR_INSTALL_ACTION_REMOVE,
   381 	RAZOR_INSTALL_ACTION_COMMIT
   382 };
   383 
   384 struct razor_install_iterator *
   385 razor_set_create_install_iterator(struct razor_set *set,
   386 				  struct razor_set *next);
   387 
   388 int razor_install_iterator_next(struct razor_install_iterator *ii,
   389 				struct razor_package **package,
   390 				enum razor_install_action *action,
   391 				int *count);
   392 
   393 /**
   394  * razor_install_iterator_commit_set
   395  *
   396  * Immediately after razor_install_iterator_next() returns
   397  * RAZOR_INSTALL_ACTION_COMMIT, this function will return the razor_set
   398  * which should be committed.
   399  *
   400  * Returns: a new #razor_set object.
   401  **/
   402 struct razor_set *
   403 razor_install_iterator_commit_set(struct razor_install_iterator *ii);
   404 
   405 void razor_install_iterator_rewind(struct razor_install_iterator *ii);
   406 size_t razor_install_iterator_tell(struct razor_install_iterator *ii);
   407 size_t razor_install_iterator_seek(struct razor_install_iterator *ii,
   408 				   size_t pos);
   409 void razor_install_iterator_destroy(struct razor_install_iterator *ii);
   410 
   411 /**
   412  * SECTION:transaction
   413  * @title: Transaction
   414  * @short_description: Create a new package set by merging two or more sets.
   415  *
   416  * The razor transaction object provides a way to create a new package set
   417  * from packages from one or more other package sets.
   418  **/
   419 
   420 struct razor_rpm;
   421 
   422 struct razor_transaction *
   423 razor_transaction_create(struct razor_set *system, struct razor_set *upstream);
   424 void razor_transaction_install_package(struct razor_transaction *transaction,
   425 				       struct razor_package *package);
   426 void razor_transaction_remove_package(struct razor_transaction *transaction,
   427 				      struct razor_package *package);
   428 void razor_transaction_update_package(struct razor_transaction *trans,
   429 				      struct razor_package *package);
   430 void razor_transaction_fixup_package(struct razor_transaction *trans,
   431 				     struct razor_package *package,
   432 				     struct razor_rpm *rpm);
   433 void razor_transaction_update_all(struct razor_transaction *transaction);
   434 int razor_transaction_resolve(struct razor_transaction *trans);
   435 int razor_transaction_describe(struct razor_transaction *trans);
   436 struct razor_set *razor_transaction_commit(struct razor_transaction *trans);
   437 void razor_transaction_destroy(struct razor_transaction *trans);
   438 
   439 /* Temporary helper for test suite. */
   440 int razor_transaction_unsatisfied_property(struct razor_transaction *trans,
   441 					   const char *name,
   442 					   uint32_t flags,
   443 					   const char *version);
   444 
   445 /**
   446  * SECTION:rpm
   447  * @title: Razor RPM
   448  * @short_description: Operating on RPM files.
   449  *
   450  * Functions for open RPM files and extracting information and
   451  * installing or removing RPM files.
   452  **/
   453 
   454 struct razor_relocations;
   455 
   456 struct razor_relocations *razor_relocations_create(void);
   457 void razor_relocations_add(struct razor_relocations *relocations,
   458 			   const char *oldpath, const char *newpath);
   459 void razor_relocations_set_rpm(struct razor_relocations *relocations,
   460 			       struct razor_rpm *rpm);
   461 const char *razor_relocations_apply(struct razor_relocations *relocations,
   462 				    const char *path);
   463 void razor_relocations_destroy(struct razor_relocations *relocations);
   464 
   465 struct razor_rpm *razor_rpm_open(const char *filename,
   466 				 struct razor_error **error);
   467 void razor_rpm_get_details(struct razor_rpm *rpm, ...);
   468 void razor_rpm_set_relocations(struct razor_rpm *rpm,
   469 			       struct razor_relocations *relocations);
   470 int razor_rpm_install(struct razor_rpm *rpm, struct razor_atomic *atomic,
   471 		      const char *root, int install_count,
   472 		      enum razor_stage_type stage);
   473 int razor_rpm_close(struct razor_rpm *rpm);
   474 
   475 /**
   476  * SECTION:importer
   477  * @title: Importer
   478  * @short_description: A mechanism for building #razor_set objects
   479  *
   480  * The %razor_importer is a helper object for building a razor set
   481  * from external sources, like yum metadata, the RPM database or RPM
   482  * files.
   483  *
   484  * The importer is a stateful object; it has the notion of a current
   485  * package, and the caller can provide meta data such as properties,
   486  * files and similiar for the package as it becomes available.  Once a
   487  * package is fully described, the next pacakge can begin.  When all
   488  * packages have been described to the importer, the importer will
   489  * create a new %razor_set with the specified packages.
   490  *
   491  * A typical use
   492  * of the importer will follow this template:
   493  * |[
   494  * importer = razor_importer_create();
   495  *
   496  * while ( /<!-- -->* more packages to import *<!-- -->/; ) {
   497  *   /<!-- -->* get name, version and arch of next package *<!-- -->/
   498  *   razor_importer_begin_package(importer, name, version, arch);
   499  *   razor_importer_add_details(importer, summary, description, url, license);
   500  *
   501  *   while ( /<!-- -->* more properties to add *<!-- -->/ )
   502  *     razor_importer_add_property(importer, name, flags, version);
   503  *
   504  *   while ( /<!-- -->* [more files to add *<!-- -->/ )
   505  *     razor_importer_add_file(importer, name);
   506  *
   507  *   razor_importer_finish_package(importer);
   508  * }
   509  *
   510  * return razor_importer_finish(importer);
   511  * ]|
   512  **/
   513 struct razor_importer;
   514 
   515 struct razor_importer *razor_importer_create(void);
   516 void razor_importer_destroy(struct razor_importer *importer);
   517 void razor_importer_begin_package(struct razor_importer *importer,
   518 				  const char *name,
   519 				  const char *version,
   520 				  const char *arch);
   521 void razor_importer_add_details(struct razor_importer *importer,
   522 				const char *summary,
   523 				const char *description,
   524 				const char *url,
   525 				const char *license);
   526 void razor_importer_add_script(struct razor_importer *importer,
   527 			       enum razor_property_flags script,
   528 			       const char *program,
   529 			       const char *body);
   530 void razor_importer_add_install_prefix(struct razor_importer *importer,
   531 				       const char *install_prefix);
   532 void razor_importer_add_property(struct razor_importer *importer,
   533 				 const char *name,
   534 				 uint32_t flags,
   535 				 const char *version);
   536 void razor_importer_add_file(struct razor_importer *importer,
   537 			     const char *name);
   538 void razor_importer_finish_package(struct razor_importer *importer);
   539 
   540 int razor_importer_add_rpm(struct razor_importer *importer,
   541 			   struct razor_rpm *rpm);
   542 
   543 struct razor_set *razor_importer_finish(struct razor_importer *importer);
   544 
   545 struct razor_set *razor_set_create_from_yum(void);
   546 struct razor_set *razor_set_create_from_rpmdb(void);
   547 
   548 /**
   549  * SECTION:root
   550  * @title: Root
   551  * @short_description: Functions for accessing an install root.
   552  *
   553  * The #razor_root object encapsulate access to and locking of a razor
   554  * install root.
   555  **/
   556 struct razor_root;
   557 
   558 int razor_root_create(const char *root, struct razor_error **error);
   559 struct razor_root *
   560 razor_root_open(const char *root, struct razor_error **error);
   561 struct razor_set *
   562 razor_root_open_read_only(const char *root, struct razor_error **error);
   563 struct razor_set *razor_root_get_system_set(struct razor_root *root);
   564 int razor_root_close(struct razor_root *root);
   565 int
   566 razor_root_update(struct razor_root *root, struct razor_set *next,
   567 		  struct razor_atomic *atomic);
   568 
   569 /**
   570  * SECTION:misc
   571  * @title: Miscellaneous Functions
   572  * @short_description: Various helper functions
   573  *
   574  * Functions that doesn't fit anywhere else.
   575  **/
   576 
   577 const char *
   578 razor_property_relation_to_string(struct razor_property *p);
   579 const char *
   580 razor_property_type_to_string(struct razor_property *p);
   581 
   582 void razor_build_evr(char *evr_buf, int size, const char *epoch,
   583 		     const char *version, const char *release);
   584 int razor_versioncmp(const char *s1, const char *s2);
   585 
   586 void razor_disable_root_name_checks(int disable);
   587 
   588 void razor_set_lua_loader(const char *modname, void (*loader)());
   589 void (*razor_get_lua_loader(const char *modname))();
   590 
   591 char *razor_concat(const char *s, ...) RAZOR_MALLOC RAZOR_NULL_TERMINATED;
   592 
   593 char *razor_path_add_root(const char *path, const char *root) RAZOR_MALLOC;
   594 
   595 const char *razor_system_arch(void);
   596 
   597 #endif /* _RAZOR_H_ */