2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-29 13:28:27 +00:00

img: Prepare to use bfd engine

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
Acked-by: Cyrill Gorcunov <gorcunov@openvz.org>
This commit is contained in:
Pavel Emelyanov 2014-09-29 12:50:13 +04:00
parent 67bbc7ea0b
commit b90ae65c4c
5 changed files with 56 additions and 18 deletions

19
bfd.c
View File

@ -4,6 +4,7 @@
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/uio.h>
#include "log.h"
#include "bfd.h"
@ -95,7 +96,8 @@ int bfdopen(struct bfd *f)
void bclose(struct bfd *f)
{
buf_put(&f->b);
if (bfd_buffered(f))
buf_put(&f->b);
close(f->fd);
}
@ -185,3 +187,18 @@ again:
goto again;
}
int bwrite(struct bfd *bfd, const void *buf, int size)
{
return write(bfd->fd, buf, size);
}
int bwritev(struct bfd *bfd, const struct iovec *iov, int cnt)
{
return writev(bfd->fd, iov, cnt);
}
int bread(struct bfd *bfd, void *buf, int size)
{
return read(bfd->fd, buf, size);
}

22
image.c
View File

@ -229,7 +229,12 @@ struct cr_img *open_image_at(int dfd, int type, unsigned long flags, ...)
goto err;
}
img->_fd = ret;
img->_x.fd = ret;
if (oflags & O_NOBUF)
bfd_setraw(&img->_x);
else if (bfdopen(&img->_x))
goto err;
if (imgset_template[type].magic == RAW_IMAGE_MAGIC)
goto skip_magic;
@ -258,7 +263,7 @@ errn:
void close_image(struct cr_img *img)
{
close(img->_fd);
bclose(&img->_x);
xfree(img);
}
@ -267,8 +272,11 @@ struct cr_img *img_from_fd(int fd)
struct cr_img *img;
img = xmalloc(sizeof(*img));
if (img)
img->_fd = fd;
if (img) {
img->_x.fd = fd;
bfd_setraw(&img->_x);
}
return img;
}
@ -355,10 +363,9 @@ struct cr_img *open_pages_image(unsigned long flags, struct cr_img *pmi)
*/
int write_img_buf(struct cr_img *img, const void *ptr, int size)
{
int fd = img->_fd;
int ret;
ret = write(fd, ptr, size);
ret = bwrite(&img->_x, ptr, size);
if (ret == size)
return 0;
@ -378,10 +385,9 @@ int write_img_buf(struct cr_img *img, const void *ptr, int size)
*/
int read_img_buf_eof(struct cr_img *img, void *ptr, int size)
{
int fd = img->_fd;
int ret;
ret = read(fd, ptr, size);
ret = bread(&img->_x, ptr, size);
if (ret == size)
return 1;
if (ret == 0)

View File

@ -13,8 +13,22 @@ struct bfd {
struct xbuf b;
};
static inline bool bfd_buffered(struct bfd *b)
{
return b->b.mem != NULL;
}
static inline void bfd_setraw(struct bfd *b)
{
b->b.mem = NULL;
}
#define BREADERR ((char *)-1)
int bfdopen(struct bfd *f);
void bclose(struct bfd *f);
char *breadline(struct bfd *f);
int bwrite(struct bfd *f, const void *buf, int sz);
struct iovec;
int bwritev(struct bfd *f, const struct iovec *iov, int cnt);
int bread(struct bfd *f, void *buf, int sz);
#endif

View File

@ -8,6 +8,8 @@
#include "image-desc.h"
#include "fcntl.h"
#include "magic.h"
#include "bfd.h"
#include "bug.h"
#define PAGE_IMAGE_SIZE 4096
#define PAGE_RSS 1
@ -77,12 +79,13 @@ extern bool ns_per_id;
#define O_RSTR (O_RDONLY)
struct cr_img {
int _fd;
struct bfd _x;
};
static inline int img_raw_fd(struct cr_img *img)
{
return img->_fd;
BUG_ON(bfd_buffered(&img->_x));
return img->_x.fd;
}
extern int open_image_dir(char *dir);

View File

@ -17,7 +17,7 @@
#include "string.h"
#include "sockets.h"
#include "cr_options.h"
#include "bfd.h"
#include "protobuf.h"
/*
@ -494,7 +494,7 @@ void do_pb_show_plain(struct cr_img *img, int type, int single_entry,
static char *image_name(struct cr_img *img)
{
int fd = img->_fd;
int fd = img->_x.fd;
static char image_path[PATH_MAX];
if (read_fd_link(fd, image_path, sizeof(image_path)) > 0)
@ -515,7 +515,6 @@ static char *image_name(struct cr_img *img)
int do_pb_read_one(struct cr_img *img, void **pobj, int type, bool eof)
{
int fd = img->_fd;
u8 local[PB_PKOBJ_LOCAL_SIZE];
void *buf = (void *)&local;
u32 size;
@ -529,7 +528,7 @@ int do_pb_read_one(struct cr_img *img, void **pobj, int type, bool eof)
*pobj = NULL;
ret = read(fd, &size, sizeof(size));
ret = bread(&img->_x, &size, sizeof(size));
if (ret == 0) {
if (eof) {
return 0;
@ -552,7 +551,7 @@ int do_pb_read_one(struct cr_img *img, void **pobj, int type, bool eof)
goto err;
}
ret = read(fd, buf, size);
ret = bread(&img->_x, buf, size);
if (ret < 0) {
pr_perror("Can't read %d bytes from file %s",
size, image_name(img));
@ -590,7 +589,6 @@ err:
*/
int pb_write_one(struct cr_img *img, void *obj, int type)
{
int fd = img->_fd;
u8 local[PB_PKOBJ_LOCAL_SIZE];
void *buf = (void *)&local;
u32 size, packed;
@ -620,7 +618,7 @@ int pb_write_one(struct cr_img *img, void *obj, int type)
iov[1].iov_base = buf;
iov[1].iov_len = size;
ret = writev(fd, iov, 2);
ret = bwritev(&img->_x, iov, 2);
if (ret != size + sizeof(size)) {
pr_perror("Can't write %d bytes", (int)(size + sizeof(size)));
goto err;