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