krh@234: #include krh@234: #include krh@234: #include krh@234: #include krh@234: #include krh@234: #include krh@234: #include krh@234: #include "razor.h" krh@234: #include "razor-internal.h" krh@234: krh@234: static const char system_repo_filename[] = "system.repo"; krh@234: static const char next_repo_filename[] = "system-next.repo"; krh@234: static const char razor_root_path[] = "/var/lib/razor"; krh@234: krh@234: struct razor_root { krh@234: struct razor_set *system; krh@234: struct razor_set *next; krh@234: int fd; krh@234: char path[PATH_MAX]; krh@234: char new_path[PATH_MAX]; krh@234: }; krh@234: krh@234: int krh@234: razor_root_create(const char *root) krh@234: { krh@234: struct stat buf; krh@234: struct razor_set *set; krh@234: char path[PATH_MAX]; krh@234: krh@234: if (stat(root, &buf) < 0) { krh@234: if (mkdir(root, 0777) < 0) { krh@234: fprintf(stderr, krh@234: "could not create install root \"%s\"\n", krh@234: root); krh@234: return -1; krh@234: } krh@234: fprintf(stderr, "created install root \"%s\"\n", root); krh@234: } else if (!S_ISDIR(buf.st_mode)) { krh@234: fprintf(stderr, krh@234: "install root \"%s\" exists, but is not a directory\n", krh@234: root); krh@234: return -1; krh@234: } krh@234: krh@234: snprintf(path, sizeof path, "%s/%s", krh@234: razor_root_path, system_repo_filename); krh@234: if (razor_create_dir(root, path) < 0) { krh@234: fprintf(stderr, "could not create %s%s\n", krh@234: root, razor_root_path); krh@234: return -1; krh@234: } krh@234: krh@234: set = razor_set_create(); krh@234: snprintf(path, sizeof path, "%s%s/%s", krh@234: root, razor_root_path, system_repo_filename); krh@234: if (stat(root, &buf) == 0) { krh@234: fprintf(stderr, krh@234: "a razor install root is already initialized\n"); krh@234: return -1; krh@234: } krh@234: if (razor_set_write(set, path) < 0) { krh@234: fprintf(stderr, "could not write initial package set\n"); krh@234: return -1; krh@234: } krh@234: razor_set_destroy(set); krh@234: krh@234: return 0; krh@234: } krh@234: krh@234: struct razor_root * krh@234: razor_root_open(const char *root, int flags) krh@234: { krh@234: struct razor_root *image; krh@234: krh@234: image = malloc(sizeof *image); krh@234: if (image == NULL) krh@234: return NULL; krh@234: krh@234: /* Create the new next repo file up front to ensure exclusive krh@234: * access. */ krh@234: snprintf(image->new_path, sizeof image->new_path, krh@234: "%s%s/%s", root, root, next_repo_filename); krh@234: image->fd = open(image->new_path, krh@234: O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666); krh@234: if (image->fd < 0) { krh@234: fprintf(stderr, "failed to get lock file, " krh@234: "maybe previous operation crashed?\n"); krh@234: krh@234: /* FIXME: Use fcntl advisory locking on the system krh@234: * package set file to figure out whether previous krh@234: * operation crashed or is still in progress. */ krh@234: krh@234: free(image); krh@234: return NULL; krh@234: } krh@234: krh@234: snprintf(image->path, sizeof image->path, krh@234: "%s%s/%s", root, razor_root_path, system_repo_filename); krh@234: image->system = razor_set_open(image->path); krh@234: if (image->system == NULL) { krh@234: unlink(image->new_path); krh@234: close(image->fd); krh@234: free(image); krh@234: return NULL; krh@234: } krh@234: krh@234: return image; krh@234: } krh@234: krh@234: struct razor_transaction * krh@234: razor_root_create_transaction(struct razor_root *image, krh@234: struct razor_set *upstream) krh@234: { krh@234: /* FIXME: This should take a number of upstream repos. */ krh@234: return razor_transaction_create(image->system, upstream); krh@234: } krh@234: krh@234: int krh@234: razor_root_close(struct razor_root *image) krh@234: { krh@234: unlink(image->new_path); krh@234: close(image->fd); krh@234: free(image); krh@234: krh@234: return 0; krh@234: } krh@234: krh@234: void krh@234: razor_root_update(struct razor_root *root, struct razor_set *next) krh@234: { krh@234: razor_set_write_to_fd(next, root->fd); krh@234: root->next = next; krh@234: krh@234: /* Sync the new repo file so the new package set is on disk krh@234: * before we start upgrading. */ krh@234: fsync(root->fd); krh@234: printf("wrote %s\n", root->new_path); krh@234: } krh@234: krh@234: int krh@234: razor_root_commit(struct razor_root *image) krh@234: { krh@234: /* Make it so. */ krh@234: rename(image->new_path, image->path); krh@234: printf("renamed %s to %s\n", image->new_path, image->path); krh@234: close(image->fd); krh@234: free(image); krh@234: krh@234: return 0; krh@234: } krh@234: krh@234: void krh@234: razor_root_diff(struct razor_root *root, krh@234: razor_package_callback_t callback, void *data) krh@234: { krh@234: return razor_set_diff(root->system, root->next, callback, data); krh@234: }