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