2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-30 05:48:05 +00:00

img: Move images IO helpers into .c file

This is to simplify the change from int fd to more
generic image class data-type.

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:47:21 +04:00
parent 9d9ac53cd5
commit 1cb690ddc9
3 changed files with 96 additions and 93 deletions

88
image.c
View File

@ -315,3 +315,91 @@ int open_pages_image(unsigned long flags, int pm_fd)
{
return open_pages_image_at(get_service_fd(IMG_FD_OFF), flags, pm_fd);
}
/*
* Write buffer @ptr of @size bytes into @fd file
* Returns
* 0 on success
* -1 on error (error message is printed)
*/
int write_img_buf(int fd, const void *ptr, int size)
{
int ret;
ret = write(fd, ptr, size);
if (ret == size)
return 0;
if (ret < 0)
pr_perror("Can't write img file");
else
pr_err("Img trimmed %d/%d\n", ret, size);
return -1;
}
/*
* Read buffer @ptr of @size bytes from @fd file
* Returns
* 1 on success
* 0 on EOF (silently)
* -1 on error (error message is printed)
*/
int read_img_buf_eof(int fd, void *ptr, int size)
{
int ret;
ret = read(fd, ptr, size);
if (ret == size)
return 1;
if (ret == 0)
return 0;
if (ret < 0)
pr_perror("Can't read img file");
else
pr_err("Img trimmed %d/%d\n", ret, size);
return -1;
}
/*
* Read buffer @ptr of @size bytes from @fd file
* Returns
* 1 on success
* -1 on error or EOF (error message is printed)
*/
int read_img_buf(int fd, void *ptr, int size)
{
int ret;
ret = read_img_buf_eof(fd, ptr, size);
if (ret == 0) {
pr_err("Unexpected EOF\n");
ret = -1;
}
return ret;
}
/*
* read_img_str -- same as read_img_buf, but allocates memory for
* the buffer and puts the '\0' at the end
*/
int read_img_str(int fd, char **pstr, int size)
{
int ret;
char *str;
str = xmalloc(size + 1);
if (!str)
return -1;
ret = read_img_buf(fd, str, size);
if (ret < 0) {
xfree(str);
return -1;
}
str[size] = '\0';
*pstr = str;
return 0;
}

View File

@ -83,4 +83,12 @@ extern int open_pages_image(unsigned long flags, int pm_fd);
extern int open_pages_image_at(int dfd, unsigned long flags, int pm_fd);
extern void up_page_ids_base(void);
extern int write_img_buf(int fd, const void *ptr, int size);
#define write_img(fd, ptr) write_img_buf((fd), (ptr), sizeof(*(ptr)))
extern int read_img_buf_eof(int fd, void *ptr, int size);
#define read_img_eof(fd, ptr) read_img_buf_eof((fd), (ptr), sizeof(*(ptr)))
extern int read_img_buf(int fd, void *ptr, int size);
#define read_img(fd, ptr) read_img_buf((fd), (ptr), sizeof(*(ptr)))
extern int read_img_str(int fd, char **pstr, int size);
#endif /* __CR_IMAGE_H__ */

View File

@ -34,74 +34,6 @@
#define MEGA(size) PREF_SHIFT_OP(K, <<, size)
#define GIGA(size) PREF_SHIFT_OP(K, <<, size)
/*
* Write buffer @ptr of @size bytes into @fd file
* Returns
* 0 on success
* -1 on error (error message is printed)
*/
static inline int write_img_buf(int fd, const void *ptr, int size)
{
int ret;
ret = write(fd, ptr, size);
if (ret == size)
return 0;
if (ret < 0)
pr_perror("Can't write img file");
else
pr_err("Img trimmed %d/%d\n", ret, size);
return -1;
}
#define write_img(fd, ptr) write_img_buf((fd), (ptr), sizeof(*(ptr)))
/*
* Read buffer @ptr of @size bytes from @fd file
* Returns
* 1 on success
* 0 on EOF (silently)
* -1 on error (error message is printed)
*/
static inline int read_img_buf_eof(int fd, void *ptr, int size)
{
int ret;
ret = read(fd, ptr, size);
if (ret == size)
return 1;
if (ret == 0)
return 0;
if (ret < 0)
pr_perror("Can't read img file");
else
pr_err("Img trimmed %d/%d\n", ret, size);
return -1;
}
#define read_img_eof(fd, ptr) read_img_buf_eof((fd), (ptr), sizeof(*(ptr)))
/*
* Read buffer @ptr of @size bytes from @fd file
* Returns
* 1 on success
* -1 on error or EOF (error message is printed)
*/
static inline int read_img_buf(int fd, void *ptr, int size)
{
int ret;
ret = read_img_buf_eof(fd, ptr, size);
if (ret == 0) {
pr_err("Unexpected EOF\n");
ret = -1;
}
return ret;
}
#define read_img(fd, ptr) read_img_buf((fd), (ptr), sizeof(*(ptr)))
struct vma_area;
struct list_head;
@ -234,31 +166,6 @@ extern int is_anon_link_type(char *link, char *type);
((c) >= 'a' && (c) <= 'f') || \
((c) >= 'A' && (c) <= 'F'))
/*
* read_img_str -- same as read_img_buf, but allocates memory for
* the buffer and puts the '\0' at the end
*/
static inline int read_img_str(int fd, char **pstr, int size)
{
int ret;
char *str;
str = xmalloc(size + 1);
if (!str)
return -1;
ret = read_img_buf(fd, str, size);
if (ret < 0) {
xfree(str);
return -1;
}
str[size] = '\0';
*pstr = str;
return 0;
}
extern void *shmalloc(size_t bytes);
extern void shfree_last(void *ptr);