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