librazor/root.c
author Kristian H?gsberg <krh@redhat.com>
Sat Jun 28 18:33:15 2008 -0400 (2008-06-28)
changeset 294 d7ecddf38c3d
parent 269 03fc85294bc9
child 300 455eaa569767
permissions -rw-r--r--
Revert "Get api docs building"

Pass --enable-gtk-doc to ./configure to build API docs. Add a
status print-out at the end of configure.ac to show whether gtk-doc
was enabled.

This reverts commit 436386fb11ee56e50c2c76c496b1f6c39530e170.
     1 #include <stdlib.h>
     2 #include <stdint.h>
     3 #include <stdio.h>
     4 #include <sys/stat.h>
     5 #include <dirent.h>
     6 #include <unistd.h>
     7 #include <fcntl.h>
     8 #include "razor.h"
     9 #include "razor-internal.h"
    10 
    11 static const char system_repo_filename[] = "system.repo";
    12 static const char system_repo_details_filename[] = "system-details.repo";
    13 static const char system_repo_files_filename[] = "system-files.repo";
    14 
    15 static const char next_repo_filename[] = "system-next.repo";
    16 static const char razor_root_path[] = "/var/lib/razor";
    17 
    18 struct razor_root {
    19 	struct razor_set *system;
    20 	struct razor_set *next;
    21 	int fd;
    22 	char path[PATH_MAX];
    23 	char new_path[PATH_MAX];
    24 };
    25 
    26 RAZOR_EXPORT int
    27 razor_root_create(const char *root)
    28 {
    29 	struct stat buf;
    30 	struct razor_set *set;
    31 	char path[PATH_MAX], details_path[PATH_MAX], files_path[PATH_MAX];
    32 
    33 	if (stat(root, &buf) < 0) {
    34 		if (mkdir(root, 0777) < 0) {
    35 			fprintf(stderr,
    36 				"could not create install root \"%s\"\n",
    37 				root);
    38 			return -1;
    39 		}
    40 		fprintf(stderr, "created install root \"%s\"\n", root);
    41 	} else if (!S_ISDIR(buf.st_mode)) {
    42 		fprintf(stderr,
    43 			"install root \"%s\" exists, but is not a directory\n",
    44 			root);
    45 		return -1;
    46 	}
    47 
    48 	snprintf(path, sizeof path, "%s/%s",
    49 		 razor_root_path, system_repo_filename);
    50 	if (razor_create_dir(root, path) < 0) {
    51 		fprintf(stderr, "could not create %s%s\n",
    52 			root, razor_root_path);
    53 		return -1;
    54 	}
    55 
    56 	set = razor_set_create();
    57 	snprintf(path, sizeof path, "%s%s/%s",
    58 		 root, razor_root_path, system_repo_filename);
    59 	snprintf(details_path, sizeof details_path, "%s%s/%s",
    60 		 root, razor_root_path, system_repo_details_filename);
    61 	snprintf(files_path, sizeof files_path, "%s%s/%s",
    62 		 root, razor_root_path, system_repo_files_filename);
    63 	if (stat(path, &buf) == 0) {
    64 		fprintf(stderr,
    65 			"a razor install root is already initialized\n");
    66 		return -1;
    67 	}
    68 	if (razor_set_write(set, path, RAZOR_REPO_FILE_MAIN) < 0 ||
    69 	    razor_set_write(set, details_path, RAZOR_REPO_FILE_DETAILS) < 0 ||
    70 	    razor_set_write(set, files_path, RAZOR_REPO_FILE_FILES) < 0 ) {
    71 		fprintf(stderr, "could not write initial package set\n");
    72 		return -1;
    73 	}
    74 	razor_set_destroy(set);
    75 
    76 	return 0;
    77 }
    78 
    79 RAZOR_EXPORT struct razor_root *
    80 razor_root_open(const char *root)
    81 {
    82 	struct razor_root *image;
    83 
    84 	image = malloc(sizeof *image);
    85 	if (image == NULL)
    86 		return NULL;
    87 
    88 	/* Create the new next repo file up front to ensure exclusive
    89 	 * access. */
    90 	snprintf(image->new_path, sizeof image->new_path,
    91 		 "%s%s/%s", root, razor_root_path, next_repo_filename);
    92 	image->fd = open(image->new_path,
    93 			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
    94 	if (image->fd < 0) {
    95 		fprintf(stderr, "failed to get lock file, "
    96 			"maybe previous operation crashed?\n");
    97 
    98 		/* FIXME: Use fcntl advisory locking on the system
    99 		 * package set file to figure out whether previous
   100 		 * operation crashed or is still in progress. */
   101 
   102 		free(image);
   103 		return NULL;
   104 	}
   105 
   106 	snprintf(image->path, sizeof image->path,
   107 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   108 	image->system = razor_set_open(image->path);
   109 	if (image->system == NULL) {
   110 		unlink(image->new_path);
   111 		close(image->fd);
   112 		free(image);
   113 		return NULL;
   114 	}
   115 
   116 	return image;
   117 }
   118 
   119 RAZOR_EXPORT struct razor_set *
   120 razor_root_open_read_only(const char *root)
   121 {
   122 	char path[PATH_MAX];
   123 
   124 	snprintf(path, sizeof path, "%s%s/%s",
   125 		 root, razor_root_path, system_repo_filename);
   126 
   127 	return razor_set_open(path);
   128 }
   129 
   130 RAZOR_EXPORT struct razor_set *
   131 razor_root_get_system_set(struct razor_root *root)
   132 {
   133 	return root->system;
   134 }
   135 
   136 RAZOR_EXPORT int
   137 razor_root_close(struct razor_root *root)
   138 {
   139 	razor_set_destroy(root->system);
   140 	unlink(root->new_path);
   141 	close(root->fd);
   142 	free(root);
   143 
   144 	return 0;
   145 }
   146 
   147 RAZOR_EXPORT void
   148 razor_root_update(struct razor_root *root, struct razor_set *next)
   149 {
   150 	razor_set_write_to_fd(next, root->fd, RAZOR_REPO_FILE_MAIN);
   151 	root->next = next;
   152 
   153 	/* Sync the new repo file so the new package set is on disk
   154 	 * before we start upgrading. */
   155 	fsync(root->fd);
   156 	printf("wrote %s\n", root->new_path);
   157 }
   158 
   159 RAZOR_EXPORT int
   160 razor_root_commit(struct razor_root *root)
   161 {
   162 	/* Make it so. */
   163 	rename(root->new_path, root->path);
   164 	printf("renamed %s to %s\n", root->new_path, root->path);
   165 	razor_set_destroy(root->system);
   166 	close(root->fd);
   167 	free(root);
   168 
   169 	return 0;
   170 }