librazor/root.c
changeset 424 8cbc438cc298
parent 406 5ab137def3d1
child 425 0c8bdd8dc942
     1.1 --- a/librazor/root.c	Fri Jan 27 08:12:19 2012 +0000
     1.2 +++ b/librazor/root.c	Thu Feb 16 17:33:47 2012 +0000
     1.3 @@ -1,7 +1,7 @@
     1.4  /*
     1.5   * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     1.6   * Copyright (C) 2008  Red Hat, Inc
     1.7 - * Copyright (C) 2009, 2011  J. Ali Harlow <ali@juiblex.co.uk>
     1.8 + * Copyright (C) 2009, 2011, 2012  J. Ali Harlow <ali@juiblex.co.uk>
     1.9   *
    1.10   * This program is free software; you can redistribute it and/or modify
    1.11   * it under the terms of the GNU General Public License as published by
    1.12 @@ -49,7 +49,6 @@
    1.13   * will see the system as permenantly locked.
    1.14   */
    1.15  static const char system_lock_filename[] = "system-next.rzdb";
    1.16 -static const char system_tmp_filename[] = "system.tmp";
    1.17  #ifdef MSWIN_API
    1.18  #define RAZOR_ROOT_PATH	NULL
    1.19  #else
    1.20 @@ -59,10 +58,7 @@
    1.21  
    1.22  struct razor_root {
    1.23  	struct razor_set *system;
    1.24 -	struct razor_set *next;
    1.25 -	struct razor_atomic *atomic;
    1.26 -	int handle;
    1.27 -	char *path, *new_path;
    1.28 +	char *path;
    1.29  };
    1.30  
    1.31  static void
    1.32 @@ -136,7 +132,7 @@
    1.33  }
    1.34  
    1.35  RAZOR_EXPORT struct razor_root *
    1.36 -razor_root_open(const char *root, struct razor_atomic *atomic)
    1.37 +razor_root_open(const char *root, struct razor_error **error)
    1.38  {
    1.39  	struct razor_root *image;
    1.40  	char *lock_path;
    1.41 @@ -147,16 +143,14 @@
    1.42  	razor_root_init();
    1.43  	image = malloc(sizeof *image);
    1.44  	if (image == NULL) {
    1.45 -		razor_atomic_abort(atomic, "Not enough memory");
    1.46 +		razor_set_error(error, NULL, "Not enough memory");
    1.47  		return NULL;
    1.48  	}
    1.49  
    1.50 -	image->atomic = atomic;
    1.51 -
    1.52  	image->system = razor_set_create_without_root();
    1.53  	if (image->system == NULL) {
    1.54  		free(image);
    1.55 -		razor_atomic_abort(atomic, "Not enough memory");
    1.56 +		razor_set_error(error, NULL, "Not enough memory");
    1.57  		return NULL;
    1.58  	}
    1.59  
    1.60 @@ -168,19 +162,8 @@
    1.61  	free(lock_path);
    1.62  
    1.63  	if (r < 0) {
    1.64 -		razor_atomic_abort(atomic,
    1.65 -				   "Failed to aquire exclusive system lock");
    1.66 -		razor_set_unref(image->system);
    1.67 -		free(image);
    1.68 -		return NULL;
    1.69 -	}
    1.70 -
    1.71 -	image->new_path = razor_concat(root, razor_root_path, "/",
    1.72 -				       system_tmp_filename, NULL);
    1.73 -	image->handle = razor_atomic_create_file(atomic, image->new_path,
    1.74 -						 S_IRWXU | S_IRWXG | S_IRWXO);
    1.75 -	if (image->handle < 0) {
    1.76 -		free(image->new_path);
    1.77 +		razor_set_error(error, NULL,
    1.78 +				"Failed to aquire exclusive system lock");
    1.79  		razor_set_unref(image->system);
    1.80  		free(image);
    1.81  		return NULL;
    1.82 @@ -189,9 +172,8 @@
    1.83  	image->path = razor_concat(root, razor_root_path, "/",
    1.84  				   system_repo_filename, NULL);
    1.85  
    1.86 -	if (razor_set_bind_sections(image->system, atomic, image->path)) {
    1.87 -		unlink(image->new_path);
    1.88 -		free(image->new_path);
    1.89 +	if (razor_set_bind_sections(image->system, image->path,
    1.90 +				    RAZOR_SET_PRIVATE, error)) {
    1.91  		free(image->path);
    1.92  		razor_set_unref(image->system);
    1.93  		free(image);
    1.94 @@ -202,7 +184,7 @@
    1.95  }
    1.96  
    1.97  RAZOR_EXPORT struct razor_set *
    1.98 -razor_root_open_read_only(const char *root, struct razor_atomic *atomic)
    1.99 +razor_root_open_read_only(const char *root, struct razor_error **error)
   1.100  {
   1.101  	char *path;
   1.102  	struct razor_set *set;
   1.103 @@ -212,15 +194,15 @@
   1.104  	razor_root_init();
   1.105  	set = razor_set_create_without_root();
   1.106  	if (set == NULL) {
   1.107 -		razor_atomic_abort(atomic, "Not enough memory");
   1.108 +		razor_set_error(error, NULL, "Not enough memory");
   1.109  		return NULL;
   1.110  	}
   1.111  
   1.112  	path = razor_concat(root, razor_root_path, "/", system_lock_filename,
   1.113  			    NULL);
   1.114  	if (razor_set_aquire_lock(set, path, 0) < 0) {
   1.115 -		razor_atomic_abort(atomic, "Failed to aquire non-exclusive "
   1.116 -					   "system lock");
   1.117 +		razor_set_error(error, NULL,
   1.118 +				"Failed to aquire non-exclusive system lock");
   1.119  		free(path);
   1.120  		razor_set_unref(set);
   1.121  		return NULL;
   1.122 @@ -230,7 +212,7 @@
   1.123  	path = razor_concat(root, razor_root_path, "/", system_repo_filename,
   1.124  			    NULL);
   1.125  
   1.126 -	if (razor_set_bind_sections(set, atomic, path)) {
   1.127 +	if (razor_set_bind_sections(set, path, 0, error)) {
   1.128  		razor_set_unref(set);
   1.129  		set = NULL;
   1.130  	}
   1.131 @@ -254,44 +236,34 @@
   1.132  	assert (root != NULL);
   1.133  
   1.134  	razor_set_unref(root->system);
   1.135 -	razor_atomic_close(root->atomic, root->handle);
   1.136 -	razor_atomic_remove(root->atomic, root->new_path);
   1.137  	free(root->path);
   1.138 -	free(root->new_path);
   1.139  	free(root);
   1.140  
   1.141  	return 0;
   1.142  }
   1.143  
   1.144 -RAZOR_EXPORT void
   1.145 -razor_root_update(struct razor_root *root, struct razor_set *next)
   1.146 +RAZOR_EXPORT int
   1.147 +razor_root_update(struct razor_root *root, struct razor_set *next,
   1.148 +		  struct razor_atomic *atomic)
   1.149  {
   1.150 +	int handle, retval;
   1.151 +
   1.152  	assert (root != NULL);
   1.153  	assert (next != NULL);
   1.154  
   1.155 -	razor_root_init();
   1.156 -	razor_set_write_to_handle(next, root->atomic, root->handle,
   1.157 -				  RAZOR_SECTION_ALL);
   1.158 -	root->next = next;
   1.159 +	handle = razor_atomic_create_file(atomic, root->path,
   1.160 +					  S_IRWXU | S_IRWXG | S_IRWXO);
   1.161 +	if (handle < 0)
   1.162 +		return handle;
   1.163  
   1.164 -	/* Sync the new repo file so the new package set is on disk
   1.165 -	 * before we start upgrading. */
   1.166 -	razor_atomic_sync(root->atomic, root->handle);
   1.167 -}
   1.168 +	razor_set_write_to_handle(next, atomic, handle, RAZOR_SECTION_ALL);
   1.169  
   1.170 -RAZOR_EXPORT int
   1.171 -razor_root_commit(struct razor_root *root)
   1.172 -{
   1.173 -	int retval;
   1.174 -	assert (root != NULL);
   1.175 +	retval = razor_atomic_close(atomic, handle);
   1.176  
   1.177 -	razor_atomic_close(root->atomic, root->handle);
   1.178 -	retval = razor_atomic_rename_file(root->atomic, root->new_path,
   1.179 -					  root->path);
   1.180 -	razor_set_unref(root->system);
   1.181 -	free(root->path);
   1.182 -	free(root->new_path);
   1.183 -	free(root);
   1.184 +	if (!retval) {
   1.185 +		razor_set_unref(root->system);
   1.186 +		root->system = razor_set_ref(next);
   1.187 +	}
   1.188  
   1.189  	return retval;
   1.190  }