main.c
changeset 235 060d83d8eca9
parent 232 2389f44500bb
child 239 7a1b0282ae3c
     1.1 --- a/main.c	Mon Jun 09 15:39:23 2008 -0400
     1.2 +++ b/main.c	Mon Jun 09 16:38:58 2008 -0400
     1.3 @@ -30,14 +30,12 @@
     1.4  #include <fnmatch.h>
     1.5  #include <errno.h>
     1.6  #include "razor.h"
     1.7 -#include "razor-internal.h"
     1.8  
     1.9  static const char system_repo_filename[] = "system.repo";
    1.10  static const char next_repo_filename[] = "system-next.repo";
    1.11  static const char rawhide_repo_filename[] = "rawhide.repo";
    1.12  static const char updated_repo_filename[] = "system-updated.repo";
    1.13 -static const char razor_root_path[] = "/var/lib/razor";
    1.14 -static const char root[] = "install";
    1.15 +static const char install_root[] = "install";
    1.16  static const char *repo_filename = system_repo_filename;
    1.17  static const char *yum_url;
    1.18  
    1.19 @@ -563,159 +561,6 @@
    1.20  	return 0;
    1.21  }
    1.22  
    1.23 -/* The image data struct encapsulates filesystem conventions and the
    1.24 - * locking protocol.  FIXME: Should this be razor_root?*/
    1.25 -struct razor_root {
    1.26 -	struct razor_set *system;
    1.27 -	int fd;
    1.28 -	char path[PATH_MAX];
    1.29 -	char new_path[PATH_MAX];
    1.30 -};
    1.31 -
    1.32 -#define RAZOR_ROOT_OPEN_WRITE 0x01
    1.33 -
    1.34 -int
    1.35 -razor_root_create(const char *root);
    1.36 -struct razor_root *
    1.37 -razor_root_open(const char *root, int flags);
    1.38 -struct razor_transaction *
    1.39 -razor_root_create_transaction(struct razor_root *image,
    1.40 -			      struct razor_set *upstream);
    1.41 -int
    1.42 -razor_root_close(struct razor_root *image);
    1.43 -void
    1.44 -razor_root_update(struct razor_root *image, struct razor_set *next);
    1.45 -int
    1.46 -razor_root_commit(struct razor_root *image);
    1.47 -
    1.48 -int
    1.49 -razor_root_create(const char *root)
    1.50 -{	
    1.51 -	struct stat buf;
    1.52 -	struct razor_set *set;
    1.53 -	char path[PATH_MAX];
    1.54 -
    1.55 -	if (stat(root, &buf) < 0) {
    1.56 -		if (mkdir(root, 0777) < 0) {
    1.57 -			fprintf(stderr,
    1.58 -				"could not create install root \"%s\"\n",
    1.59 -				root);
    1.60 -			return -1;
    1.61 -		}
    1.62 -		fprintf(stderr, "created install root \"%s\"\n", root);
    1.63 -	} else if (!S_ISDIR(buf.st_mode)) {
    1.64 -		fprintf(stderr,
    1.65 -			"install root \"%s\" exists, but is not a directory\n",
    1.66 -			root);
    1.67 -		return -1;
    1.68 -	}
    1.69 -
    1.70 -	snprintf(path, sizeof path, "%s/%s",
    1.71 -		 razor_root_path, system_repo_filename);
    1.72 -	if (razor_create_dir(root, path) < 0) {
    1.73 -		fprintf(stderr, "could not create %s%s\n",
    1.74 -			root, razor_root_path);
    1.75 -		return -1;
    1.76 -	}
    1.77 -
    1.78 -	set = razor_set_create();
    1.79 -	snprintf(path, sizeof path, "%s%s/%s",
    1.80 -		 root, razor_root_path, system_repo_filename);
    1.81 -	if (stat(root, &buf) == 0) {
    1.82 -		fprintf(stderr,
    1.83 -			"a razor install root is already initialized\n");
    1.84 -		return -1;
    1.85 -	}
    1.86 -	if (razor_set_write(set, path) < 0) {
    1.87 -		fprintf(stderr, "could not write initial package set\n");
    1.88 -		return -1;
    1.89 -	}
    1.90 -	razor_set_destroy(set);
    1.91 -
    1.92 -	return 0;
    1.93 -}
    1.94 -
    1.95 -struct razor_root *
    1.96 -razor_root_open(const char *root, int flags)
    1.97 -{
    1.98 -	struct razor_root *image;
    1.99 -
   1.100 -	image = malloc(sizeof *image);
   1.101 -	if (image == NULL)
   1.102 -		return NULL;
   1.103 -
   1.104 -	/* Create the new next repo file up front to ensure exclusive
   1.105 -	 * access. */
   1.106 -	snprintf(image->new_path, sizeof image->new_path,
   1.107 -		 "%s%s/%s", root, root, next_repo_filename);
   1.108 -	image->fd = open(image->new_path,
   1.109 -			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
   1.110 -	if (image->fd < 0) {
   1.111 -		fprintf(stderr, "failed to get lock file, "
   1.112 -			"maybe previous operation crashed?\n");
   1.113 -
   1.114 -		/* FIXME: Use fcntl advisory locking on the system
   1.115 -		 * package set file to figure out whether previous
   1.116 -		 * operation crashed or is still in progress. */
   1.117 -
   1.118 -		free(image);
   1.119 -		return NULL;
   1.120 -	}
   1.121 -
   1.122 -	snprintf(image->path, sizeof image->path,
   1.123 -		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   1.124 -	image->system = razor_set_open(image->path);
   1.125 -	if (image->system == NULL) {
   1.126 -		unlink(image->new_path);
   1.127 -		close(image->fd);
   1.128 -		free(image);
   1.129 -		return NULL;
   1.130 -	}
   1.131 -
   1.132 -	return image;
   1.133 -}
   1.134 -
   1.135 -struct razor_transaction *
   1.136 -razor_root_create_transaction(struct razor_root *image,
   1.137 -			      struct razor_set *upstream)
   1.138 -{
   1.139 -	/* FIXME: This should take a number of upstream repos. */
   1.140 -	return razor_transaction_create(image->system, upstream);
   1.141 -}
   1.142 -
   1.143 -int
   1.144 -razor_root_close(struct razor_root *image)
   1.145 -{
   1.146 -	unlink(image->new_path);
   1.147 -	close(image->fd);
   1.148 -	free(image);
   1.149 -
   1.150 -	return 0;
   1.151 -}
   1.152 -
   1.153 -void
   1.154 -razor_root_update(struct razor_root *image, struct razor_set *next)
   1.155 -{
   1.156 -	razor_set_write_to_fd(next, image->fd);
   1.157 -
   1.158 -	/* Sync the new repo file so the new package set is on disk
   1.159 -	 * before we start upgrading. */
   1.160 -	fsync(image->fd);
   1.161 -	printf("wrote %s\n", image->new_path);
   1.162 -}
   1.163 -
   1.164 -int
   1.165 -razor_root_commit(struct razor_root *image)
   1.166 -{
   1.167 -	/* Make it so. */
   1.168 -	rename(image->new_path, image->path);
   1.169 -	printf("renamed %s to %s\n", image->new_path, image->path);
   1.170 -	close(image->fd);
   1.171 -	free(image);
   1.172 -
   1.173 -	return 0;
   1.174 -}
   1.175 -
   1.176  static void
   1.177  download_package(const char *name,
   1.178  		 const char *old_version,
   1.179 @@ -797,7 +642,7 @@
   1.180  		i++;
   1.181  	}
   1.182  
   1.183 -	root = razor_root_open(razor_root_path, RAZOR_ROOT_OPEN_WRITE);
   1.184 +	root = razor_root_open(install_root, RAZOR_ROOT_OPEN_WRITE);
   1.185  	upstream = razor_set_open(rawhide_repo_filename);
   1.186  	trans = razor_root_create_transaction(root, upstream);
   1.187  
   1.188 @@ -827,7 +672,7 @@
   1.189  		return 1;
   1.190  	}
   1.191  
   1.192 -	razor_set_diff(root->system, next, download_package, &errors);
   1.193 +	razor_root_diff(root, download_package, &errors);
   1.194  	if (errors > 0) {
   1.195  		fprintf(stderr, "failed to download %d packages\n", errors);
   1.196  		razor_root_close(root);
   1.197 @@ -836,7 +681,7 @@
   1.198  
   1.199  	/* FIXME: We need to figure out the right install order here,
   1.200  	 * so the post and pre scripts can run. */
   1.201 -	razor_set_diff(root->system, next, install_package, (void *) root);
   1.202 +	razor_root_diff(root, install_package, (void *) root);
   1.203  
   1.204  	razor_set_destroy(next);
   1.205  	razor_set_destroy(upstream);
   1.206 @@ -847,7 +692,7 @@
   1.207  static int
   1.208  command_init(int argc, const char *argv[])
   1.209  {
   1.210 -	return razor_root_create(root);
   1.211 +	return razor_root_create(install_root);
   1.212  }
   1.213  
   1.214  static int