diff -r 4e261a14a6bd -r e63951c1d0f8 librazor/razor.h --- a/librazor/razor.h Thu Aug 13 07:14:51 2009 +0100 +++ b/librazor/razor.h Thu Nov 10 10:35:21 2011 +0000 @@ -1,7 +1,7 @@ /* * Copyright (C) 2008 Kristian Høgsberg * Copyright (C) 2008 Red Hat, Inc - * Copyright (C) 2009 J. Ali Harlow + * Copyright (C) 2009, 2011 J. Ali Harlow * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,6 +22,21 @@ #define _RAZOR_H_ #include +#include + +/* GCC extensions */ +#if defined(__GNUC__) +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) +#define RAZOR_MALLOC __attribute__((__malloc__)) +#else +#define RAZOR_MALLOC +#endif +#if __GNUC__ >= 4 +#define RAZOR_NULL_TERMINATED __attribute__ ((__sentinel__)) +#else +#define RAZOR_NULL_TERMINATED +#endif +#endif /* __GNUC__ */ enum razor_section_type { RAZOR_SECTION_MAIN = 0x01, @@ -30,6 +45,14 @@ RAZOR_SECTION_ALL = 0x07 }; +enum razor_stage_type { + RAZOR_STAGE_SCRIPTS_PRE = 0x1, + RAZOR_STAGE_FILES = 0x2, + RAZOR_STAGE_SCRIPTS_POST = 0x4, + RAZOR_STAGE_SCRIPTS = 0x5, + RAZOR_STAGE_ALL = 0x7 +}; + enum razor_detail_type { RAZOR_DETAIL_LAST = 0, /* the sentinel */ RAZOR_DETAIL_NAME, @@ -73,6 +96,85 @@ }; /** + * SECTION:atomic + * @title: Atomic transactions + * @short_description: File-based transactions that shouldn't half-succeed + * + * This is a helper object for issuing a sequence of file-based actions + * that should either all succeed or all fail. + * + * Note that currently only Windows 7 has a native implementation and that + * the fallback implementation will not rollback even if an error does occur. + * This could (and should) be improved. + **/ +struct razor_atomic; + +struct razor_atomic *razor_atomic_open(const char *description); +int razor_atomic_commit(struct razor_atomic *atomic); +const char *razor_atomic_get_error_msg(struct razor_atomic *atomic); +void razor_atomic_destroy(struct razor_atomic *atomic); + +/** + * razor_atomic_make_dirs + * + * Create all sub-directories leading up to path. We know root exists + * and is a dir, root does not end in a '/', and path either has a + * leading '/' or (on MS-Windows only) root is the empty string + * and path starts with drive (eg., "c:/windows"). + * Note: path itself is not created, only the directory in which it + * (would) exist. + * + * Returns: non-zero on error. + **/ +int razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root, + const char *path); +int razor_atomic_remove(struct razor_atomic *atomic, const char *path); +int razor_atomic_rename_file(struct razor_atomic *atomic, const char *oldpath, + const char *newpath); + +/** + * razor_atomic_create_dir + * + * Create a directory, replacing any existing object at this path except + * an existing directory (which is not counted as a error). + * + * Returns: non-zero on error. + */ +int razor_atomic_create_dir(struct razor_atomic *atomic, const char *dirname, + mode_t mode); + +/** + * razor_atomic_create_symlink + * + * Create a symbolic link, replacing any existing object at this path except + * a non-empty directory. + * + * Note: This function will always fail on platforms that don't support + * symbolic links. + * + * Returns: non-zero on error. + */ +int razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target, + const char *path); +/** + * razor_atomic_create_file + * + * Create a file, replacing any existing object at this path except + * a non-empty directory. + * + * Returns: A handle to be passed to razor_atomic_write() and + * razor_atomic_close() or a negative value on error. + */ +int razor_atomic_create_file(struct razor_atomic *atomic, const char *filename, + mode_t mode); +int razor_atomic_write(struct razor_atomic *atomic, int handle, + const void *data, size_t size); +int razor_atomic_close(struct razor_atomic *atomic, int handle); +int razor_atomic_sync(struct razor_atomic *atomic, int handle); +void razor_atomic_abort(struct razor_atomic *atomic, const char *error_msg); +int razor_atomic_in_error_state(struct razor_atomic *atomic); + +/** * SECTION:set * @title: Package Set * @short_description: Represents a set of packages and their metadata. @@ -85,6 +187,9 @@ struct razor_package; struct razor_property; +#define RAZOR_HEADER_VERSION 2 /* Current version */ +#define RAZOR_HEADER_VERSION_MIN 1 /* Minimum version we support */ + /** * razor_set_create: * @@ -94,13 +199,20 @@ **/ struct razor_set *razor_set_create_without_root(void); struct razor_set *razor_set_create(void); -struct razor_set *razor_set_open(const char *filename); -void razor_set_destroy(struct razor_set *set); -int razor_set_write_to_fd(struct razor_set *set, - int fd, uint32_t section_mask); -int razor_set_write(struct razor_set *set, +struct razor_set *razor_set_open(const char *filename, + struct razor_atomic *atomic); +uint32_t razor_set_get_header_version(struct razor_set *set); +int razor_set_set_header_version(struct razor_set *set, + uint32_t header_version); +void razor_set_unref(struct razor_set *set); +struct razor_set *razor_set_ref(struct razor_set *set); +void razor_set_write_to_handle(struct razor_set *set, + struct razor_atomic *atomic, int handle, + uint32_t section_mask); +int razor_set_write(struct razor_set *set, struct razor_atomic *atomic, const char *filename, uint32_t setions); -int razor_set_bind_sections(struct razor_set *set, const char *filename); +int razor_set_bind_sections(struct razor_set *set, struct razor_atomic *atomic, + const char *filename); struct razor_package * razor_set_get_package(struct razor_set *set, const char *package); @@ -110,8 +222,9 @@ struct razor_package *package, ...); int razor_package_remove(struct razor_set *prev, struct razor_set *next, - struct razor_package *package, const char *root, - int install_count); + struct razor_atomic *atomic, struct razor_package *package, + const char *root, int install_count, + enum razor_stage_type stage); /** * SECTION:iterator @@ -230,6 +343,7 @@ enum razor_install_action *action, int *count); +void razor_install_iterator_rewind(struct razor_install_iterator *ii); void razor_install_iterator_destroy(struct razor_install_iterator *ii); /** @@ -286,12 +400,14 @@ const char *path); void razor_relocations_destroy(struct razor_relocations *relocations); -struct razor_rpm *razor_rpm_open(const char *filename); +struct razor_rpm *razor_rpm_open(const char *filename, + struct razor_atomic *atomic); void razor_rpm_get_details(struct razor_rpm *rpm, ...); void razor_rpm_set_relocations(struct razor_rpm *rpm, struct razor_relocations *relocations); -int razor_rpm_install(struct razor_rpm *rpm, const char *root, - int install_count); +int razor_rpm_install(struct razor_rpm *rpm, struct razor_atomic *atomic, + const char *root, int install_count, + enum razor_stage_type stage); int razor_rpm_close(struct razor_rpm *rpm); /** @@ -378,14 +494,15 @@ struct razor_root; int razor_root_create(const char *root); -struct razor_root *razor_root_open(const char *root); -struct razor_set *razor_root_open_read_only(const char *root); +struct razor_root * +razor_root_open(const char *root, struct razor_atomic *atomic); +struct razor_set *razor_root_open_read_only(const char *root, + struct razor_atomic *atomic); struct razor_set *razor_root_get_system_set(struct razor_root *root); int razor_root_close(struct razor_root *root); void razor_root_update(struct razor_root *root, struct razor_set *next); int razor_root_commit(struct razor_root *root); - /** * SECTION:misc * @title: Miscellaneous Functions @@ -408,5 +525,8 @@ void razor_set_lua_loader(const char *modname, void (*loader)()); void (*razor_get_lua_loader(const char *modname))(); +char *razor_concat(const char *s, ...) RAZOR_MALLOC RAZOR_NULL_TERMINATED; + +const char *razor_system_arch(void); #endif /* _RAZOR_H_ */