struct cr_img *img_from_fd(int fd) { struct cr_img *img; img = xmalloc(sizeof(*img)); if (img) { img->_x.fd = fd; bfd_setraw(&img->_x); } return img; }
static int do_open_image(struct cr_img *img, int dfd, int type, unsigned long oflags, char *path) { int ret, flags; flags = oflags & ~(O_NOBUF | O_SERVICE); ret = openat(dfd, path, flags, CR_FD_PERM); if (ret < 0) { if (!(flags & O_CREAT) && (errno == ENOENT)) { pr_info("No %s image\n", path); img->_x.fd = EMPTY_IMG_FD; goto skip_magic; } pr_perror("Unable to open %s", path); goto err; } img->_x.fd = ret; if (oflags & O_NOBUF) bfd_setraw(&img->_x); else { if (flags == O_RDONLY) ret = bfdopenr(&img->_x); else ret = bfdopenw(&img->_x); if (ret) goto err; } if (imgset_template[type].magic == RAW_IMAGE_MAGIC) goto skip_magic; if (flags == O_RDONLY) ret = img_check_magic(img, oflags, type, path); else ret = img_write_magic(img, oflags, type); if (ret) goto err; skip_magic: return 0; err: return -1; }
struct cr_img *open_image_at(int dfd, int type, unsigned long flags, ...) { struct cr_img *img; unsigned long oflags = flags; char path[PATH_MAX]; va_list args; int ret; img = xmalloc(sizeof(*img)); if (!img) goto errn; oflags |= imgset_template[type].oflags; flags &= ~(O_OPT | O_NOBUF); va_start(args, flags); vsnprintf(path, PATH_MAX, imgset_template[type].fmt, args); va_end(args); ret = openat(dfd, path, flags, CR_FD_PERM); if (ret < 0) { if ((oflags & O_OPT) && errno == ENOENT) { xfree(img); return NULL; } pr_perror("Unable to open %s", path); goto err; } img->_x.fd = ret; if (oflags & O_NOBUF) bfd_setraw(&img->_x); else if (bfdopen(&img->_x, flags)) goto err; if (imgset_template[type].magic == RAW_IMAGE_MAGIC) goto skip_magic; if (flags == O_RDONLY) { u32 magic; if (read_img(img, &magic) < 0) goto err; if (magic != imgset_template[type].magic) { pr_err("Magic doesn't match for %s\n", path); goto err; } } else { if (write_img(img, &imgset_template[type].magic)) goto err; } skip_magic: return img; err: xfree(img); errn: return NULL; }
static int do_open_image(struct cr_img *img, int dfd, int type, unsigned long oflags, char *path) { int ret, flags; flags = oflags & ~(O_NOBUF | O_SERVICE | O_FORCE_LOCAL); if (opts.remote && !(oflags & O_FORCE_LOCAL)) { char *snapshot_id = NULL; snapshot_id = get_snapshot_id_from_idx(dfd); if (snapshot_id == NULL) ret = -1; else if (flags == O_RDONLY) { pr_info("do_open_remote_image RDONLY path=%s snapshot_id=%s\n", path, snapshot_id); ret = read_remote_image_connection(snapshot_id, path); } else { pr_info("do_open_remote_image WDONLY path=%s snapshot_id=%s\n", path, snapshot_id); ret = write_remote_image_connection(snapshot_id, path, O_WRONLY); } } else ret = openat(dfd, path, flags, CR_FD_PERM); if (ret < 0) { if (!(flags & O_CREAT) && (errno == ENOENT)) { pr_info("No %s image\n", path); img->_x.fd = EMPTY_IMG_FD; goto skip_magic; } pr_perror("Unable to open %s", path); goto err; } img->_x.fd = ret; if (oflags & O_NOBUF) bfd_setraw(&img->_x); else { if (flags == O_RDONLY) ret = bfdopenr(&img->_x); else ret = bfdopenw(&img->_x); if (ret) goto err; } if (imgset_template[type].magic == RAW_IMAGE_MAGIC) goto skip_magic; if (flags == O_RDONLY) ret = img_check_magic(img, oflags, type, path); else ret = img_write_magic(img, oflags, type); if (ret) goto err; skip_magic: return 0; err: return -1; }
static int do_open_image(struct cr_img *img, int dfd, int type, unsigned long oflags, char *path) { int ret, flags; flags = oflags & ~(O_NOBUF | O_SERVICE | O_FORCE_LOCAL); /* * For pages images dedup we need to open images read-write on * restore, that may require proper capabilities, so we ask * usernsd to do it for us */ if (root_ns_mask & CLONE_NEWUSER && type == CR_FD_PAGES && oflags & O_RDWR) { struct openat_args pa = { .flags = flags, .err = 0, .mode = CR_FD_PERM, }; snprintf(pa.path, PATH_MAX, "%s", path); ret = userns_call(userns_openat, UNS_FDOUT, &pa, sizeof(struct openat_args), dfd); if (ret < 0) errno = pa.err; } else ret = openat(dfd, path, flags, CR_FD_PERM); if (ret < 0) { if (!(flags & O_CREAT) && (errno == ENOENT || ret == -ENOENT)) { pr_info("No %s image\n", path); img->_x.fd = EMPTY_IMG_FD; goto skip_magic; } pr_perror("Unable to open %s", path); goto err; } img->_x.fd = ret; if (oflags & O_NOBUF) bfd_setraw(&img->_x); else { if (flags == O_RDONLY) ret = bfdopenr(&img->_x); else ret = bfdopenw(&img->_x); if (ret) goto err; } if (imgset_template[type].magic == RAW_IMAGE_MAGIC) goto skip_magic; if (flags == O_RDONLY) ret = img_check_magic(img, oflags, type, path); else ret = img_write_magic(img, oflags, type); if (ret) goto err; skip_magic: return 0; err: return -1; }