mirror of
https://github.com/checkpoint-restore/criu
synced 2025-08-31 22:35:33 +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:
88
image.c
88
image.c
@@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user