Move razor root code to its own file.
authorKristian H?gsberg <krh@redhat.com>
Mon Jun 09 16:14:05 2008 -0400 (2008-06-09)
changeset 2347f5d32472bef
parent 233 2555ce3e2567
child 235 060d83d8eca9
Move razor root code to its own file.
Makefile
main.c
razor-root.c
razor.h
     1.1 --- a/Makefile	Mon Jun 09 16:01:34 2008 -0400
     1.2 +++ b/Makefile	Mon Jun 09 16:14:05 2008 -0400
     1.3 @@ -3,7 +3,7 @@
     1.4  
     1.5  all : razor test-driver rpm-razor
     1.6  
     1.7 -librazor_objs = razor.o yum.o rpm.o types.o util.o
     1.8 +librazor_objs = razor.o yum.o rpm.o types.o util.o razor-root.o
     1.9  librazor.a : $(librazor_objs)
    1.10  	ar cr $@ $(librazor_objs)
    1.11  
     2.1 --- a/main.c	Mon Jun 09 16:01:34 2008 -0400
     2.2 +++ b/main.c	Mon Jun 09 16:14:05 2008 -0400
     2.3 @@ -30,14 +30,12 @@
     2.4  #include <fnmatch.h>
     2.5  #include <errno.h>
     2.6  #include "razor.h"
     2.7 -#include "razor-internal.h"
     2.8  
     2.9  static const char system_repo_filename[] = "system.repo";
    2.10  static const char next_repo_filename[] = "system-next.repo";
    2.11  static const char rawhide_repo_filename[] = "rawhide.repo";
    2.12  static const char updated_repo_filename[] = "system-updated.repo";
    2.13 -static const char razor_root_path[] = "/var/lib/razor";
    2.14 -static const char root[] = "install";
    2.15 +static const char install_root[] = "install";
    2.16  static const char *repo_filename = system_repo_filename;
    2.17  static const char *yum_url;
    2.18  
    2.19 @@ -563,159 +561,6 @@
    2.20  	return 0;
    2.21  }
    2.22  
    2.23 -/* The image data struct encapsulates filesystem conventions and the
    2.24 - * locking protocol.  FIXME: Should this be razor_root?*/
    2.25 -struct razor_root {
    2.26 -	struct razor_set *system;
    2.27 -	int fd;
    2.28 -	char path[PATH_MAX];
    2.29 -	char new_path[PATH_MAX];
    2.30 -};
    2.31 -
    2.32 -#define RAZOR_ROOT_OPEN_WRITE 0x01
    2.33 -
    2.34 -int
    2.35 -razor_root_create(const char *root);
    2.36 -struct razor_root *
    2.37 -razor_root_open(const char *root, int flags);
    2.38 -struct razor_transaction *
    2.39 -razor_root_create_transaction(struct razor_root *image,
    2.40 -			      struct razor_set *upstream);
    2.41 -int
    2.42 -razor_root_close(struct razor_root *image);
    2.43 -void
    2.44 -razor_root_update(struct razor_root *image, struct razor_set *next);
    2.45 -int
    2.46 -razor_root_commit(struct razor_root *image);
    2.47 -
    2.48 -int
    2.49 -razor_root_create(const char *root)
    2.50 -{	
    2.51 -	struct stat buf;
    2.52 -	struct razor_set *set;
    2.53 -	char path[PATH_MAX];
    2.54 -
    2.55 -	if (stat(root, &buf) < 0) {
    2.56 -		if (mkdir(root, 0777) < 0) {
    2.57 -			fprintf(stderr,
    2.58 -				"could not create install root \"%s\"\n",
    2.59 -				root);
    2.60 -			return -1;
    2.61 -		}
    2.62 -		fprintf(stderr, "created install root \"%s\"\n", root);
    2.63 -	} else if (!S_ISDIR(buf.st_mode)) {
    2.64 -		fprintf(stderr,
    2.65 -			"install root \"%s\" exists, but is not a directory\n",
    2.66 -			root);
    2.67 -		return -1;
    2.68 -	}
    2.69 -
    2.70 -	snprintf(path, sizeof path, "%s/%s",
    2.71 -		 razor_root_path, system_repo_filename);
    2.72 -	if (razor_create_dir(root, path) < 0) {
    2.73 -		fprintf(stderr, "could not create %s%s\n",
    2.74 -			root, razor_root_path);
    2.75 -		return -1;
    2.76 -	}
    2.77 -
    2.78 -	set = razor_set_create();
    2.79 -	snprintf(path, sizeof path, "%s%s/%s",
    2.80 -		 root, razor_root_path, system_repo_filename);
    2.81 -	if (stat(root, &buf) == 0) {
    2.82 -		fprintf(stderr,
    2.83 -			"a razor install root is already initialized\n");
    2.84 -		return -1;
    2.85 -	}
    2.86 -	if (razor_set_write(set, path) < 0) {
    2.87 -		fprintf(stderr, "could not write initial package set\n");
    2.88 -		return -1;
    2.89 -	}
    2.90 -	razor_set_destroy(set);
    2.91 -
    2.92 -	return 0;
    2.93 -}
    2.94 -
    2.95 -struct razor_root *
    2.96 -razor_root_open(const char *root, int flags)
    2.97 -{
    2.98 -	struct razor_root *image;
    2.99 -
   2.100 -	image = malloc(sizeof *image);
   2.101 -	if (image == NULL)
   2.102 -		return NULL;
   2.103 -
   2.104 -	/* Create the new next repo file up front to ensure exclusive
   2.105 -	 * access. */
   2.106 -	snprintf(image->new_path, sizeof image->new_path,
   2.107 -		 "%s%s/%s", root, root, next_repo_filename);
   2.108 -	image->fd = open(image->new_path,
   2.109 -			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
   2.110 -	if (image->fd < 0) {
   2.111 -		fprintf(stderr, "failed to get lock file, "
   2.112 -			"maybe previous operation crashed?\n");
   2.113 -
   2.114 -		/* FIXME: Use fcntl advisory locking on the system
   2.115 -		 * package set file to figure out whether previous
   2.116 -		 * operation crashed or is still in progress. */
   2.117 -
   2.118 -		free(image);
   2.119 -		return NULL;
   2.120 -	}
   2.121 -
   2.122 -	snprintf(image->path, sizeof image->path,
   2.123 -		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   2.124 -	image->system = razor_set_open(image->path);
   2.125 -	if (image->system == NULL) {
   2.126 -		unlink(image->new_path);
   2.127 -		close(image->fd);
   2.128 -		free(image);
   2.129 -		return NULL;
   2.130 -	}
   2.131 -
   2.132 -	return image;
   2.133 -}
   2.134 -
   2.135 -struct razor_transaction *
   2.136 -razor_root_create_transaction(struct razor_root *image,
   2.137 -			      struct razor_set *upstream)
   2.138 -{
   2.139 -	/* FIXME: This should take a number of upstream repos. */
   2.140 -	return razor_transaction_create(image->system, upstream);
   2.141 -}
   2.142 -
   2.143 -int
   2.144 -razor_root_close(struct razor_root *image)
   2.145 -{
   2.146 -	unlink(image->new_path);
   2.147 -	close(image->fd);
   2.148 -	free(image);
   2.149 -
   2.150 -	return 0;
   2.151 -}
   2.152 -
   2.153 -void
   2.154 -razor_root_update(struct razor_root *image, struct razor_set *next)
   2.155 -{
   2.156 -	razor_set_write_to_fd(next, image->fd);
   2.157 -
   2.158 -	/* Sync the new repo file so the new package set is on disk
   2.159 -	 * before we start upgrading. */
   2.160 -	fsync(image->fd);
   2.161 -	printf("wrote %s\n", image->new_path);
   2.162 -}
   2.163 -
   2.164 -int
   2.165 -razor_root_commit(struct razor_root *image)
   2.166 -{
   2.167 -	/* Make it so. */
   2.168 -	rename(image->new_path, image->path);
   2.169 -	printf("renamed %s to %s\n", image->new_path, image->path);
   2.170 -	close(image->fd);
   2.171 -	free(image);
   2.172 -
   2.173 -	return 0;
   2.174 -}
   2.175 -
   2.176  static void
   2.177  download_package(const char *name,
   2.178  		 const char *old_version,
   2.179 @@ -797,7 +642,7 @@
   2.180  		i++;
   2.181  	}
   2.182  
   2.183 -	root = razor_root_open(razor_root_path, RAZOR_ROOT_OPEN_WRITE);
   2.184 +	root = razor_root_open(install_root, RAZOR_ROOT_OPEN_WRITE);
   2.185  	upstream = razor_set_open(rawhide_repo_filename);
   2.186  	trans = razor_root_create_transaction(root, upstream);
   2.187  
   2.188 @@ -827,7 +672,7 @@
   2.189  		return 1;
   2.190  	}
   2.191  
   2.192 -	razor_set_diff(root->system, next, download_package, &errors);
   2.193 +	razor_root_diff(root, download_package, &errors);
   2.194  	if (errors > 0) {
   2.195  		fprintf(stderr, "failed to download %d packages\n", errors);
   2.196  		razor_root_close(root);
   2.197 @@ -836,7 +681,7 @@
   2.198  
   2.199  	/* FIXME: We need to figure out the right install order here,
   2.200  	 * so the post and pre scripts can run. */
   2.201 -	razor_set_diff(root->system, next, install_package, (void *) root);
   2.202 +	razor_root_diff(root, install_package, (void *) root);
   2.203  
   2.204  	razor_set_destroy(next);
   2.205  	razor_set_destroy(upstream);
   2.206 @@ -847,7 +692,7 @@
   2.207  static int
   2.208  command_init(int argc, const char *argv[])
   2.209  {
   2.210 -	return razor_root_create(root);
   2.211 +	return razor_root_create(install_root);
   2.212  }
   2.213  
   2.214  static int
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/razor-root.c	Mon Jun 09 16:14:05 2008 -0400
     3.3 @@ -0,0 +1,157 @@
     3.4 +#include <stdlib.h>
     3.5 +#include <stdint.h>
     3.6 +#include <stdio.h>
     3.7 +#include <sys/stat.h>
     3.8 +#include <dirent.h>
     3.9 +#include <unistd.h>
    3.10 +#include <fcntl.h>
    3.11 +#include "razor.h"
    3.12 +#include "razor-internal.h"
    3.13 +
    3.14 +static const char system_repo_filename[] = "system.repo";
    3.15 +static const char next_repo_filename[] = "system-next.repo";
    3.16 +static const char razor_root_path[] = "/var/lib/razor";
    3.17 +
    3.18 +struct razor_root {
    3.19 +	struct razor_set *system;
    3.20 +	struct razor_set *next;
    3.21 +	int fd;
    3.22 +	char path[PATH_MAX];
    3.23 +	char new_path[PATH_MAX];
    3.24 +};
    3.25 +
    3.26 +int
    3.27 +razor_root_create(const char *root)
    3.28 +{	
    3.29 +	struct stat buf;
    3.30 +	struct razor_set *set;
    3.31 +	char path[PATH_MAX];
    3.32 +
    3.33 +	if (stat(root, &buf) < 0) {
    3.34 +		if (mkdir(root, 0777) < 0) {
    3.35 +			fprintf(stderr,
    3.36 +				"could not create install root \"%s\"\n",
    3.37 +				root);
    3.38 +			return -1;
    3.39 +		}
    3.40 +		fprintf(stderr, "created install root \"%s\"\n", root);
    3.41 +	} else if (!S_ISDIR(buf.st_mode)) {
    3.42 +		fprintf(stderr,
    3.43 +			"install root \"%s\" exists, but is not a directory\n",
    3.44 +			root);
    3.45 +		return -1;
    3.46 +	}
    3.47 +
    3.48 +	snprintf(path, sizeof path, "%s/%s",
    3.49 +		 razor_root_path, system_repo_filename);
    3.50 +	if (razor_create_dir(root, path) < 0) {
    3.51 +		fprintf(stderr, "could not create %s%s\n",
    3.52 +			root, razor_root_path);
    3.53 +		return -1;
    3.54 +	}
    3.55 +
    3.56 +	set = razor_set_create();
    3.57 +	snprintf(path, sizeof path, "%s%s/%s",
    3.58 +		 root, razor_root_path, system_repo_filename);
    3.59 +	if (stat(root, &buf) == 0) {
    3.60 +		fprintf(stderr,
    3.61 +			"a razor install root is already initialized\n");
    3.62 +		return -1;
    3.63 +	}
    3.64 +	if (razor_set_write(set, path) < 0) {
    3.65 +		fprintf(stderr, "could not write initial package set\n");
    3.66 +		return -1;
    3.67 +	}
    3.68 +	razor_set_destroy(set);
    3.69 +
    3.70 +	return 0;
    3.71 +}
    3.72 +
    3.73 +struct razor_root *
    3.74 +razor_root_open(const char *root, int flags)
    3.75 +{
    3.76 +	struct razor_root *image;
    3.77 +
    3.78 +	image = malloc(sizeof *image);
    3.79 +	if (image == NULL)
    3.80 +		return NULL;
    3.81 +
    3.82 +	/* Create the new next repo file up front to ensure exclusive
    3.83 +	 * access. */
    3.84 +	snprintf(image->new_path, sizeof image->new_path,
    3.85 +		 "%s%s/%s", root, root, next_repo_filename);
    3.86 +	image->fd = open(image->new_path,
    3.87 +			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
    3.88 +	if (image->fd < 0) {
    3.89 +		fprintf(stderr, "failed to get lock file, "
    3.90 +			"maybe previous operation crashed?\n");
    3.91 +
    3.92 +		/* FIXME: Use fcntl advisory locking on the system
    3.93 +		 * package set file to figure out whether previous
    3.94 +		 * operation crashed or is still in progress. */
    3.95 +
    3.96 +		free(image);
    3.97 +		return NULL;
    3.98 +	}
    3.99 +
   3.100 +	snprintf(image->path, sizeof image->path,
   3.101 +		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   3.102 +	image->system = razor_set_open(image->path);
   3.103 +	if (image->system == NULL) {
   3.104 +		unlink(image->new_path);
   3.105 +		close(image->fd);
   3.106 +		free(image);
   3.107 +		return NULL;
   3.108 +	}
   3.109 +
   3.110 +	return image;
   3.111 +}
   3.112 +
   3.113 +struct razor_transaction *
   3.114 +razor_root_create_transaction(struct razor_root *image,
   3.115 +			      struct razor_set *upstream)
   3.116 +{
   3.117 +	/* FIXME: This should take a number of upstream repos. */
   3.118 +	return razor_transaction_create(image->system, upstream);
   3.119 +}
   3.120 +
   3.121 +int
   3.122 +razor_root_close(struct razor_root *image)
   3.123 +{
   3.124 +	unlink(image->new_path);
   3.125 +	close(image->fd);
   3.126 +	free(image);
   3.127 +
   3.128 +	return 0;
   3.129 +}
   3.130 +
   3.131 +void
   3.132 +razor_root_update(struct razor_root *root, struct razor_set *next)
   3.133 +{
   3.134 +	razor_set_write_to_fd(next, root->fd);
   3.135 +	root->next = next;
   3.136 +
   3.137 +	/* Sync the new repo file so the new package set is on disk
   3.138 +	 * before we start upgrading. */
   3.139 +	fsync(root->fd);
   3.140 +	printf("wrote %s\n", root->new_path);
   3.141 +}
   3.142 +
   3.143 +int
   3.144 +razor_root_commit(struct razor_root *image)
   3.145 +{
   3.146 +	/* Make it so. */
   3.147 +	rename(image->new_path, image->path);
   3.148 +	printf("renamed %s to %s\n", image->new_path, image->path);
   3.149 +	close(image->fd);
   3.150 +	free(image);
   3.151 +
   3.152 +	return 0;
   3.153 +}
   3.154 +
   3.155 +void
   3.156 +razor_root_diff(struct razor_root *root,
   3.157 +		razor_package_callback_t callback, void *data)
   3.158 +{
   3.159 +	return razor_set_diff(root->system, root->next, callback, data);
   3.160 +}
     4.1 --- a/razor.h	Mon Jun 09 16:01:34 2008 -0400
     4.2 +++ b/razor.h	Mon Jun 09 16:14:05 2008 -0400
     4.3 @@ -166,4 +166,22 @@
     4.4  int razor_rpm_install(struct razor_rpm *rpm, const char *root);
     4.5  int razor_rpm_close(struct razor_rpm *rpm);
     4.6  
     4.7 +
     4.8 +/* Razor root functions. The root data struct encapsulates filesystem
     4.9 + * conventions and the locking protocol. */
    4.10 +
    4.11 +struct razor_root;
    4.12 +#define RAZOR_ROOT_OPEN_WRITE 0x01
    4.13 +
    4.14 +int razor_root_create(const char *root);
    4.15 +struct razor_root *razor_root_open(const char *root, int flags);
    4.16 +struct razor_transaction *
    4.17 +razor_root_create_transaction(struct razor_root *image,
    4.18 +			      struct razor_set *upstream);
    4.19 +int razor_root_close(struct razor_root *image);
    4.20 +void razor_root_update(struct razor_root *image, struct razor_set *next);
    4.21 +int razor_root_commit(struct razor_root *image);
    4.22 +void razor_root_diff(struct razor_root *root,
    4.23 +		     razor_package_callback_t callback, void *data);
    4.24 +
    4.25  #endif /* _RAZOR_H_ */