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