Convert main.c to use razor_root for most cases.
authorKristian H?gsberg <krh@redhat.com>
Tue Jul 08 22:57:34 2008 -0400 (2008-07-08)
changeset 317019a53b65271
parent 316 5ebed314390c
child 318 829d6711b316
Convert main.c to use razor_root for most cases.

With this we change the default repo location to /var/lib/razor, but let the env
variable RAZOR_ROOT override it.
librazor/root.c
src/main.c
     1.1 --- a/librazor/root.c	Tue Jul 08 22:02:58 2008 -0400
     1.2 +++ b/librazor/root.c	Tue Jul 08 22:57:34 2008 -0400
     1.3 @@ -20,6 +20,7 @@
     1.4  #include <stdlib.h>
     1.5  #include <stdint.h>
     1.6  #include <stdio.h>
     1.7 +#include <string.h>
     1.8  #include <sys/stat.h>
     1.9  #include <dirent.h>
    1.10  #include <unistd.h>
    1.11 @@ -40,6 +41,7 @@
    1.12  	struct razor_set *system;
    1.13  	struct razor_set *next;
    1.14  	int fd;
    1.15 +	char root[PATH_MAX];
    1.16  	char path[PATH_MAX];
    1.17  	char new_path[PATH_MAX];
    1.18  };
    1.19 @@ -53,7 +55,9 @@
    1.20  
    1.21  	assert (root != NULL);
    1.22  
    1.23 -	if (stat(root, &buf) < 0) {
    1.24 +	if (root[0] == '\0') {
    1.25 +		/* root is file system root */
    1.26 +	} else if (stat(root, &buf) < 0) {
    1.27  		if (mkdir(root, 0777) < 0) {
    1.28  			fprintf(stderr,
    1.29  				"could not create install root \"%s\"\n",
    1.30 @@ -136,6 +140,10 @@
    1.31  	snprintf(files_path, sizeof files_path,
    1.32  		 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
    1.33  
    1.34 +	/* FIXME: We store the root path to make the hack in
    1.35 +	 * razor_root_update() work.  Need to get rid of this. */
    1.36 +	strcpy(image->root, root);
    1.37 +
    1.38  	image->system = razor_set_open(image->path);
    1.39  	if (image->system == NULL ||
    1.40  	    razor_set_open_details(image->system, details_path) ||
    1.41 @@ -152,14 +160,30 @@
    1.42  RAZOR_EXPORT struct razor_set *
    1.43  razor_root_open_read_only(const char *root)
    1.44  {
    1.45 -	char path[PATH_MAX];
    1.46 +	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
    1.47 +	struct razor_set *set;
    1.48  
    1.49  	assert (root != NULL);
    1.50  
    1.51  	snprintf(path, sizeof path, "%s%s/%s",
    1.52  		 root, razor_root_path, system_repo_filename);
    1.53 +	snprintf(details_path, sizeof details_path,
    1.54 +		 "%s%s/%s", root, razor_root_path, system_repo_details_filename);
    1.55 +	snprintf(files_path, sizeof files_path,
    1.56 +		 "%s%s/%s", root, razor_root_path, system_repo_files_filename);
    1.57  
    1.58 -	return razor_set_open(path);
    1.59 +
    1.60 +	set = razor_set_open(path);
    1.61 +	if (set == NULL)
    1.62 +		return NULL;
    1.63 +
    1.64 +	if (razor_set_open_details(set, details_path) ||
    1.65 +	    razor_set_open_files(set, files_path)) {
    1.66 +		razor_set_destroy(set);
    1.67 +		return NULL;
    1.68 +	}
    1.69 +
    1.70 +	return set;
    1.71  }
    1.72  
    1.73  RAZOR_EXPORT struct razor_set *
    1.74 @@ -186,12 +210,26 @@
    1.75  RAZOR_EXPORT void
    1.76  razor_root_update(struct razor_root *root, struct razor_set *next)
    1.77  {
    1.78 +	char path[PATH_MAX];
    1.79 +
    1.80  	assert (root != NULL);
    1.81  	assert (next != NULL);
    1.82  
    1.83  	razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
    1.84  	root->next = next;
    1.85  
    1.86 +	/* FIXME: This is a pretty bad hack that just overwrites the
    1.87 +	 * system details and files rzdb files before the transaction
    1.88 +	 * succeeds.  We need to fix this by merging the separate
    1.89 +	 * details and files rzdb files back into the main rzdb
    1.90 +	 * file. */
    1.91 +	snprintf(path, sizeof path,
    1.92 +		 "%s%s/%s", root->root, razor_root_path, system_repo_details_filename);
    1.93 +	razor_set_write(next, path, RAZOR_REPO_FILE_DETAILS);
    1.94 +	snprintf(path, sizeof path,
    1.95 +		 "%s%s/%s", root->root, razor_root_path, system_repo_files_filename);
    1.96 +	razor_set_write(next, path, RAZOR_REPO_FILE_FILES);
    1.97 +
    1.98  	/* Sync the new repo file so the new package set is on disk
    1.99  	 * before we start upgrading. */
   1.100  	fsync(root->fd);
     2.1 --- a/src/main.c	Tue Jul 08 22:02:58 2008 -0400
     2.2 +++ b/src/main.c	Tue Jul 08 22:57:34 2008 -0400
     2.3 @@ -37,7 +37,7 @@
     2.4  static const char next_repo_filename[] = "system-next.rzdb";
     2.5  static const char rawhide_repo_filename[] = "rawhide.rzdb";
     2.6  static const char updated_repo_filename[] = "system-updated.rzdb";
     2.7 -static const char install_root[] = "install";
     2.8 +static const char *install_root = "";
     2.9  static const char *repo_filename = system_repo_filename;
    2.10  static const char *yum_url;
    2.11  
    2.12 @@ -113,7 +113,10 @@
    2.13  		i++;
    2.14  	}
    2.15  
    2.16 -	set = razor_set_open(repo_filename);
    2.17 +	set = razor_root_open_read_only(install_root);
    2.18 +	if (set == NULL)
    2.19 +		return 1;
    2.20 +
    2.21  	pi = create_iterator_from_argv(set, argc - i, argv + i);
    2.22  	list_packages(pi, flags);
    2.23  	razor_package_iterator_destroy(pi);
    2.24 @@ -167,7 +170,10 @@
    2.25  	struct razor_package_iterator *pi;
    2.26  	const char *name, *version, *arch;
    2.27  
    2.28 -	set = razor_set_open(repo_filename);
    2.29 +	set = razor_root_open_read_only(install_root);
    2.30 +	if (set == NULL)
    2.31 +		return 1;
    2.32 +
    2.33  	pi = create_iterator_from_argv(set, argc, argv);
    2.34  	while (razor_package_iterator_next(pi, &package,
    2.35  					   RAZOR_DETAIL_NAME, &name,
    2.36 @@ -210,9 +216,10 @@
    2.37  {
    2.38  	struct razor_set *set;
    2.39  
    2.40 -	set = razor_set_open(repo_filename);
    2.41 +	set = razor_root_open_read_only(install_root);
    2.42  	if (set == NULL)
    2.43  		return 1;
    2.44 +
    2.45  	if (razor_set_open_files(set, "system-files.rzdb"))
    2.46  		return 1;
    2.47  
    2.48 @@ -228,11 +235,9 @@
    2.49  	struct razor_set *set;
    2.50  	struct razor_package_iterator *pi;
    2.51  
    2.52 -	set = razor_set_open(repo_filename);
    2.53 +	set = razor_root_open_read_only(install_root);
    2.54  	if (set == NULL)
    2.55  		return 1;
    2.56 -	if (razor_set_open_files(set, "system-files.rzdb"))
    2.57 -		return 1;
    2.58  
    2.59  	pi = razor_package_iterator_create_for_file(set, argv[0]);
    2.60  	list_packages(pi, 0);
    2.61 @@ -251,11 +256,9 @@
    2.62  	struct razor_package *package;
    2.63  	const char *name, *version, *arch;
    2.64  
    2.65 -	set = razor_set_open(repo_filename);
    2.66 +	set = razor_root_open_read_only(install_root);
    2.67  	if (set == NULL)
    2.68  		return 1;
    2.69 -	if (razor_set_open_files(set, "system-files.rzdb"))
    2.70 -		return 1;
    2.71  
    2.72  	pi = create_iterator_from_argv(set, argc, argv);
    2.73  	while (razor_package_iterator_next(pi, &package,
    2.74 @@ -286,7 +289,7 @@
    2.75  	if (ref_name == NULL)
    2.76  		return 0;
    2.77  
    2.78 -	set = razor_set_open(repo_filename);
    2.79 +	set = razor_root_open_read_only(install_root);
    2.80  	if (set == NULL)
    2.81  		return 1;
    2.82  
    2.83 @@ -310,6 +313,8 @@
    2.84  	}
    2.85  	razor_property_iterator_destroy(prop_iter);
    2.86  
    2.87 +	razor_set_destroy(set);
    2.88 +
    2.89  	return 0;
    2.90  }
    2.91  
    2.92 @@ -430,17 +435,19 @@
    2.93  command_import_rpmdb(int argc, const char *argv[])
    2.94  {
    2.95  	struct razor_set *set;
    2.96 +	struct razor_root *root;
    2.97 +
    2.98 +	root = razor_root_open(install_root);
    2.99 +	if (root == NULL)
   2.100 +		return 1;
   2.101  
   2.102  	set = razor_set_create_from_rpmdb();
   2.103  	if (set == NULL)
   2.104  		return 1;
   2.105 -	razor_set_write(set, repo_filename, RAZOR_REPO_FILE_MAIN);
   2.106 -	razor_set_write(set, "system-details.rzdb", RAZOR_REPO_FILE_DETAILS);
   2.107 -	razor_set_write(set, "system-files.rzdb", RAZOR_REPO_FILE_FILES);
   2.108 -	razor_set_destroy(set);
   2.109 -	printf("wrote %s\n", repo_filename);
   2.110  
   2.111 -	return 0;
   2.112 +	razor_root_update(root, set);
   2.113 +
   2.114 +	return razor_root_commit(root);
   2.115  }
   2.116  
   2.117  static int
   2.118 @@ -496,10 +503,8 @@
   2.119  	struct razor_transaction *trans;
   2.120  	int i, errors;
   2.121  
   2.122 -	set = razor_set_open(repo_filename);
   2.123 -	if (set == NULL ||
   2.124 -	    razor_set_open_details(set, "system-details.rzdb") ||
   2.125 -	    razor_set_open_files(set, "system-files.rzdb"))
   2.126 +	set = razor_root_open_read_only(install_root);
   2.127 +	if (set == NULL)
   2.128  		return 1;
   2.129  
   2.130  	upstream = razor_set_open(rawhide_repo_filename);
   2.131 @@ -541,7 +546,7 @@
   2.132  	struct razor_transaction *trans;
   2.133  	int i, errors;
   2.134  
   2.135 -	set = razor_set_open(repo_filename);
   2.136 +	set = razor_root_open_read_only(install_root);
   2.137  	if (set == NULL)
   2.138  		return 1;
   2.139  
   2.140 @@ -586,7 +591,7 @@
   2.141  {
   2.142  	struct razor_set *set, *updated;
   2.143  
   2.144 -	set = razor_set_open(repo_filename);
   2.145 +	set = razor_root_open_read_only(install_root);
   2.146  	updated = razor_set_open(updated_repo_filename);
   2.147  	if (set == NULL || updated == NULL)
   2.148  		return 1;
   2.149 @@ -896,11 +901,10 @@
   2.150  	const char *pattern = argv[0], *name, *version, *arch;
   2.151  	const char *summary, *description, *url, *license;
   2.152  
   2.153 -	set = razor_set_open(repo_filename);
   2.154 +	set = razor_root_open_read_only(install_root);
   2.155  	if (set == NULL)
   2.156  		return 1;
   2.157 -	if (razor_set_open_details(set, "system-details.rzdb"))
   2.158 -		return 1;
   2.159 +
   2.160  	pi = razor_package_iterator_create(set);
   2.161  	while (razor_package_iterator_next(pi, &package,
   2.162  					   RAZOR_DETAIL_NAME, &name,
   2.163 @@ -1025,13 +1029,17 @@
   2.164  int
   2.165  main(int argc, const char *argv[])
   2.166  {
   2.167 -	char *repo;
   2.168 +	char *repo, *root;
   2.169  	int i;
   2.170  
   2.171  	repo = getenv("RAZOR_REPO");
   2.172  	if (repo != NULL)
   2.173  		repo_filename = repo;
   2.174  
   2.175 +	root = getenv("RAZOR_ROOT");
   2.176 +	if (root != NULL)
   2.177 +		install_root = root;
   2.178 +
   2.179  	yum_url = getenv("YUM_URL");
   2.180  	if (yum_url == NULL)
   2.181  		yum_url = YUM_URL;