librazor/root.c
changeset 394 afe520f454bd
parent 373 fda83d91e600
child 403 e63951c1d0f8
     1.1 --- a/librazor/root.c	Fri Jul 03 19:06:29 2009 +0100
     1.2 +++ b/librazor/root.c	Fri Apr 23 21:07:15 2010 +0100
     1.3 @@ -42,7 +42,14 @@
     1.4  #endif
     1.5  
     1.6  static const char system_repo_filename[] = "system.rzdb";
     1.7 -static const char next_repo_filename[] = "system-next.rzdb";
     1.8 +/*
     1.9 + * system_lock_filename is chosen to be the same as the pre v0.3
    1.10 + * next_repo_filename. This means that once a system has been
    1.11 + * updated by a v0.3+ copy of razor all pre v0.3 versions of razor
    1.12 + * will see the system as permenantly locked.
    1.13 + */
    1.14 +static const char system_lock_filename[] = "system-next.rzdb";
    1.15 +static const char system_tmp_filename[] = "system.tmp";
    1.16  #ifdef MSWIN_API
    1.17  #define RAZOR_ROOT_PATH	NULL
    1.18  #else
    1.19 @@ -129,6 +136,7 @@
    1.20  razor_root_open(const char *root)
    1.21  {
    1.22  	struct razor_root *image;
    1.23 +	char lock_path[PATH_MAX];
    1.24  
    1.25  	assert (root != NULL);
    1.26  
    1.27 @@ -137,21 +145,28 @@
    1.28  	if (image == NULL)
    1.29  		return NULL;
    1.30  
    1.31 -	/* Create the new next repo file up front to ensure exclusive
    1.32 -	 * access. */
    1.33 +	image->system = razor_set_create_without_root();
    1.34 +	if (image->system == NULL) {
    1.35 +		free(image);
    1.36 +		return NULL;
    1.37 +	}
    1.38 +
    1.39 +	snprintf(lock_path, sizeof lock_path,
    1.40 +		 "%s%s/%s", root, razor_root_path, system_lock_filename);
    1.41 +
    1.42 +	if (razor_set_aquire_lock(image->system, lock_path, 1) < 0) {
    1.43 +		razor_set_destroy(image->system);
    1.44 +		free(image);
    1.45 +		return NULL;
    1.46 +	}
    1.47 +
    1.48  	snprintf(image->new_path, sizeof image->new_path,
    1.49 -		 "%s%s/%s", root, razor_root_path, next_repo_filename);
    1.50 +		 "%s%s/%s", root, razor_root_path, system_tmp_filename);
    1.51  	image->fd = open(image->new_path,
    1.52 -			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL | O_BINARY,
    1.53 +			 O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
    1.54  			 0666);
    1.55  	if (image->fd < 0) {
    1.56 -		fprintf(stderr, "failed to get lock file, "
    1.57 -			"maybe previous operation crashed?\n");
    1.58 -
    1.59 -		/* FIXME: Use fcntl advisory locking on the system
    1.60 -		 * package set file to figure out whether previous
    1.61 -		 * operation crashed or is still in progress. */
    1.62 -
    1.63 +		razor_set_destroy(image->system);
    1.64  		free(image);
    1.65  		return NULL;
    1.66  	}
    1.67 @@ -159,10 +174,10 @@
    1.68  	snprintf(image->path, sizeof image->path,
    1.69  		 "%s%s/%s", root, razor_root_path, system_repo_filename);
    1.70  
    1.71 -	image->system = razor_set_open(image->path);
    1.72 -	if (image->system == NULL) {
    1.73 +	if (razor_set_bind_sections(image->system, image->path)) {
    1.74 +		close(image->fd);
    1.75  		unlink(image->new_path);
    1.76 -		close(image->fd);
    1.77 +		razor_set_destroy(image->system);
    1.78  		free(image);
    1.79  		return NULL;
    1.80  	}
    1.81 @@ -174,14 +189,31 @@
    1.82  razor_root_open_read_only(const char *root)
    1.83  {
    1.84  	char path[PATH_MAX];
    1.85 +	struct razor_set *set;
    1.86  
    1.87  	assert (root != NULL);
    1.88  
    1.89  	razor_root_init();
    1.90 +	set = razor_set_create_without_root();
    1.91 +	if (set == NULL)
    1.92 +		return NULL;
    1.93 +
    1.94 +	snprintf(path, sizeof path,
    1.95 +		 "%s%s/%s", root, razor_root_path, system_lock_filename);
    1.96 +	if (razor_set_aquire_lock(set, path, 0) < 0) {
    1.97 +		razor_set_destroy(set);
    1.98 +		return NULL;
    1.99 +	}
   1.100 +
   1.101  	snprintf(path, sizeof path, "%s%s/%s",
   1.102  		 root, razor_root_path, system_repo_filename);
   1.103  
   1.104 -	return razor_set_open(path);
   1.105 +	if (razor_set_bind_sections(set, path)) {
   1.106 +		razor_set_destroy(set);
   1.107 +		return NULL;
   1.108 +	}
   1.109 +
   1.110 +	return set;
   1.111  }
   1.112  
   1.113  RAZOR_EXPORT struct razor_set *