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