librazor/root.c
author J. Ali Harlow <ali@juiblex.co.uk>
Mon Sep 28 17:36:02 2009 +0100 (2009-09-28)
changeset 385 3cde352d3270
parent 346 765b72a613b2
child 388 6a6462ce8a08
permissions -rw-r--r--
Start 0.3
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
     5  *
     6  * This program is free software; you can redistribute it and/or modify
     7  * it under the terms of the GNU General Public License as published by
     8  * the Free Software Foundation; either version 2 of the License, or
     9  * (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along
    17  * with this program; if not, write to the Free Software Foundation, Inc.,
    18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    19  */
    20 
    21 #include "config.h"
    22 
    23 #include <stdlib.h>
    24 #include <stdint.h>
    25 #include <stdio.h>
    26 #include <string.h>
    27 #include <sys/stat.h>
    28 #include <dirent.h>
    29 #include <unistd.h>
    30 #include <fcntl.h>
    31 #include <limits.h>
    32 #include <assert.h>
    33 #ifdef MSWIN_API
    34 #include <shlobj.h>
    35 #endif
    36 
    37 #include "razor.h"
    38 #include "razor-internal.h"
    39 
    40 #ifndef O_BINARY
    41 #define O_BINARY	0
    42 #endif
    43 
    44 static const char system_repo_filename[] = "system.rzdb";
    45 static const char next_repo_filename[] = "system-next.rzdb";
    46 #ifdef MSWIN_API
    47 #define RAZOR_ROOT_PATH	NULL
    48 #else
    49 #define RAZOR_ROOT_PATH	"/var/lib/razor"
    50 #endif
    51 static const char *razor_root_path = RAZOR_ROOT_PATH;
    52 
    53 struct razor_root {
    54 	struct razor_set *system;
    55 	struct razor_set *next;
    56 	int fd;
    57 	char path[PATH_MAX];
    58 	char new_path[PATH_MAX];
    59 };
    60 
    61 static void
    62 razor_root_init(void)
    63 {
    64 #ifdef MSWIN_API
    65 	static char root_path[MAX_PATH];
    66 	if (!razor_root_path) {
    67 		SHGetFolderPath(NULL,
    68 			CSIDL_COMMON_APPDATA | CSIDL_FLAG_DONT_VERIFY, NULL, 0,
    69 			root_path);
    70 		strcat(root_path, "\\Razor");
    71 		razor_root_path = root_path;
    72 	}
    73 #endif
    74 }
    75 
    76 RAZOR_EXPORT int
    77 razor_root_create(const char *root)
    78 {
    79 	struct stat buf;
    80 	struct razor_set *set;
    81 	char path[PATH_MAX];
    82 
    83 	assert (root != NULL);
    84 
    85 	razor_root_init();
    86 	if (root[0] == '\0') {
    87 		/* root is file system root */
    88 	} else if (stat(root, &buf) < 0) {
    89 		if (mkdir(root, 0777) < 0) {
    90 			fprintf(stderr,
    91 				"could not create install root \"%s\"\n",
    92 				root);
    93 			return -1;
    94 		}
    95 		fprintf(stderr, "created install root \"%s\"\n", root);
    96 	} else if (!S_ISDIR(buf.st_mode)) {
    97 		fprintf(stderr,
    98 			"install root \"%s\" exists, but is not a directory\n",
    99 			root);
   100 		return -1;
   101 	}
   102 
   103 	snprintf(path, sizeof path, "%s/%s",
   104 		 razor_root_path, system_repo_filename);
   105 	if (razor_create_dir(root, path) < 0) {
   106 		fprintf(stderr, "could not create %s%s\n",
   107 			root, razor_root_path);
   108 		return -1;
   109 	}
   110 
   111 	set = razor_set_create();
   112 	snprintf(path, sizeof path, "%s%s/%s",
   113 		 root, razor_root_path, system_repo_filename);
   114 	if (stat(path, &buf) == 0) {
   115 		fprintf(stderr,
   116 			"a razor install root is already initialized\n");
   117 		return -1;
   118 	}
   119 	if (razor_set_write(set, path, RAZOR_SECTION_ALL) < 0) {
   120 		fprintf(stderr, "could not write initial package set\n");
   121 		return -1;
   122 	}
   123 	razor_set_destroy(set);
   124 
   125 	return 0;
   126 }
   127 
   128 RAZOR_EXPORT struct razor_root *
   129 razor_root_open(const char *root)
   130 {
   131 	struct razor_root *image;
   132 
   133 	assert (root != NULL);
   134 
   135 	razor_root_init();
   136 	image = malloc(sizeof *image);
   137 	if (image == NULL)
   138 		return NULL;
   139 
   140 	/* Create the new next repo file up front to ensure exclusive
   141 	 * access. */
   142 	snprintf(image->new_path, sizeof image->new_path,
   143 		 "%s%s/%s", root, razor_root_path, next_repo_filename);
   144 	image->fd = open(image->new_path,
   145 			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL | O_BINARY,
   146 			 0666);
   147 	if (image->fd < 0) {
   148 		fprintf(stderr, "failed to get lock file, "
   149 			"maybe previous operation crashed?\n");
   150 
   151 		/* FIXME: Use fcntl advisory locking on the system
   152 		 * package set file to figure out whether previous
   153 		 * operation crashed or is still in progress. */
   154 
   155 		free(image);
   156 		return NULL;
   157 	}
   158 
   159 	snprintf(image->path, sizeof image->path,
   160 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   161 
   162 	image->system = razor_set_open(image->path);
   163 	if (image->system == NULL) {
   164 		unlink(image->new_path);
   165 		close(image->fd);
   166 		free(image);
   167 		return NULL;
   168 	}
   169 
   170 	return image;
   171 }
   172 
   173 RAZOR_EXPORT struct razor_set *
   174 razor_root_open_read_only(const char *root)
   175 {
   176 	char path[PATH_MAX];
   177 
   178 	assert (root != NULL);
   179 
   180 	razor_root_init();
   181 	snprintf(path, sizeof path, "%s%s/%s",
   182 		 root, razor_root_path, system_repo_filename);
   183 
   184 	return razor_set_open(path);
   185 }
   186 
   187 RAZOR_EXPORT struct razor_set *
   188 razor_root_get_system_set(struct razor_root *root)
   189 {
   190 	assert (root != NULL);
   191 
   192 	return root->system;
   193 }
   194 
   195 RAZOR_EXPORT int
   196 razor_root_close(struct razor_root *root)
   197 {
   198 	assert (root != NULL);
   199 
   200 	razor_set_destroy(root->system);
   201 	close(root->fd);
   202 	unlink(root->new_path);
   203 	free(root);
   204 
   205 	return 0;
   206 }
   207 
   208 RAZOR_EXPORT void
   209 razor_root_update(struct razor_root *root, struct razor_set *next)
   210 {
   211 	assert (root != NULL);
   212 	assert (next != NULL);
   213 
   214 	razor_root_init();
   215 	razor_set_write_to_fd(next, root->fd, RAZOR_SECTION_ALL);
   216 	root->next = next;
   217 
   218 	/* Sync the new repo file so the new package set is on disk
   219 	 * before we start upgrading. */
   220 	fsync(root->fd);
   221 	printf("wrote %s\n", root->new_path);
   222 }
   223 
   224 RAZOR_EXPORT int
   225 razor_root_commit(struct razor_root *root)
   226 {
   227 	int retval;
   228 	assert (root != NULL);
   229 
   230 	/* Make it so. */
   231 	close(root->fd);
   232 #ifdef MSWIN_API
   233 	/* Rename is not atomic under MS-Windows */
   234 	remove(root->path);
   235 #endif
   236 	retval = rename(root->new_path, root->path);
   237 	if (retval)
   238 		perror(root->path);
   239 	else
   240 		printf("renamed %s to %s\n", root->new_path, root->path);
   241 	razor_set_destroy(root->system);
   242 	free(root);
   243 
   244 	return retval;
   245 }