diff -r 5ab137def3d1 -r 8cbc438cc298 librazor/root.c --- a/librazor/root.c Fri Jan 27 08:12:19 2012 +0000 +++ b/librazor/root.c Thu Feb 16 17:33:47 2012 +0000 @@ -1,7 +1,7 @@ /* * Copyright (C) 2008 Kristian Høgsberg * Copyright (C) 2008 Red Hat, Inc - * Copyright (C) 2009, 2011 J. Ali Harlow + * Copyright (C) 2009, 2011, 2012 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 @@ -49,7 +49,6 @@ * will see the system as permenantly locked. */ static const char system_lock_filename[] = "system-next.rzdb"; -static const char system_tmp_filename[] = "system.tmp"; #ifdef MSWIN_API #define RAZOR_ROOT_PATH NULL #else @@ -59,10 +58,7 @@ struct razor_root { struct razor_set *system; - struct razor_set *next; - struct razor_atomic *atomic; - int handle; - char *path, *new_path; + char *path; }; static void @@ -136,7 +132,7 @@ } RAZOR_EXPORT struct razor_root * -razor_root_open(const char *root, struct razor_atomic *atomic) +razor_root_open(const char *root, struct razor_error **error) { struct razor_root *image; char *lock_path; @@ -147,16 +143,14 @@ razor_root_init(); image = malloc(sizeof *image); if (image == NULL) { - razor_atomic_abort(atomic, "Not enough memory"); + razor_set_error(error, NULL, "Not enough memory"); return NULL; } - image->atomic = atomic; - image->system = razor_set_create_without_root(); if (image->system == NULL) { free(image); - razor_atomic_abort(atomic, "Not enough memory"); + razor_set_error(error, NULL, "Not enough memory"); return NULL; } @@ -168,19 +162,8 @@ free(lock_path); if (r < 0) { - razor_atomic_abort(atomic, - "Failed to aquire exclusive system lock"); - razor_set_unref(image->system); - free(image); - return NULL; - } - - image->new_path = razor_concat(root, razor_root_path, "/", - system_tmp_filename, NULL); - image->handle = razor_atomic_create_file(atomic, image->new_path, - S_IRWXU | S_IRWXG | S_IRWXO); - if (image->handle < 0) { - free(image->new_path); + razor_set_error(error, NULL, + "Failed to aquire exclusive system lock"); razor_set_unref(image->system); free(image); return NULL; @@ -189,9 +172,8 @@ image->path = razor_concat(root, razor_root_path, "/", system_repo_filename, NULL); - if (razor_set_bind_sections(image->system, atomic, image->path)) { - unlink(image->new_path); - free(image->new_path); + if (razor_set_bind_sections(image->system, image->path, + RAZOR_SET_PRIVATE, error)) { free(image->path); razor_set_unref(image->system); free(image); @@ -202,7 +184,7 @@ } RAZOR_EXPORT struct razor_set * -razor_root_open_read_only(const char *root, struct razor_atomic *atomic) +razor_root_open_read_only(const char *root, struct razor_error **error) { char *path; struct razor_set *set; @@ -212,15 +194,15 @@ razor_root_init(); set = razor_set_create_without_root(); if (set == NULL) { - razor_atomic_abort(atomic, "Not enough memory"); + razor_set_error(error, NULL, "Not enough memory"); return NULL; } path = razor_concat(root, razor_root_path, "/", system_lock_filename, NULL); if (razor_set_aquire_lock(set, path, 0) < 0) { - razor_atomic_abort(atomic, "Failed to aquire non-exclusive " - "system lock"); + razor_set_error(error, NULL, + "Failed to aquire non-exclusive system lock"); free(path); razor_set_unref(set); return NULL; @@ -230,7 +212,7 @@ path = razor_concat(root, razor_root_path, "/", system_repo_filename, NULL); - if (razor_set_bind_sections(set, atomic, path)) { + if (razor_set_bind_sections(set, path, 0, error)) { razor_set_unref(set); set = NULL; } @@ -254,44 +236,34 @@ assert (root != NULL); razor_set_unref(root->system); - razor_atomic_close(root->atomic, root->handle); - razor_atomic_remove(root->atomic, root->new_path); free(root->path); - free(root->new_path); free(root); return 0; } -RAZOR_EXPORT void -razor_root_update(struct razor_root *root, struct razor_set *next) +RAZOR_EXPORT int +razor_root_update(struct razor_root *root, struct razor_set *next, + struct razor_atomic *atomic) { + int handle, retval; + assert (root != NULL); assert (next != NULL); - razor_root_init(); - razor_set_write_to_handle(next, root->atomic, root->handle, - RAZOR_SECTION_ALL); - root->next = next; + handle = razor_atomic_create_file(atomic, root->path, + S_IRWXU | S_IRWXG | S_IRWXO); + if (handle < 0) + return handle; - /* Sync the new repo file so the new package set is on disk - * before we start upgrading. */ - razor_atomic_sync(root->atomic, root->handle); -} + razor_set_write_to_handle(next, atomic, handle, RAZOR_SECTION_ALL); -RAZOR_EXPORT int -razor_root_commit(struct razor_root *root) -{ - int retval; - assert (root != NULL); + retval = razor_atomic_close(atomic, handle); - razor_atomic_close(root->atomic, root->handle); - retval = razor_atomic_rename_file(root->atomic, root->new_path, - root->path); - razor_set_unref(root->system); - free(root->path); - free(root->new_path); - free(root); + if (!retval) { + razor_set_unref(root->system); + root->system = razor_set_ref(next); + } return retval; }