[CRIU] [PATCH 03/15] img: Move images IO helpers into .c file

Pavel Emelyanov xemul at parallels.com
Mon Sep 29 01:47:21 PDT 2014


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

Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 image.c         | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/image.h |  8 +++++
 include/util.h  | 93 ---------------------------------------------------------
 3 files changed, 96 insertions(+), 93 deletions(-)

diff --git a/image.c b/image.c
index bc2d69d..66f1513 100644
--- a/image.c
+++ b/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;
+}
+
diff --git a/include/image.h b/include/image.h
index 477f339..e6bdebd 100644
--- a/include/image.h
+++ b/include/image.h
@@ -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__ */
diff --git a/include/util.h b/include/util.h
index eb5e35f..717aeee 100644
--- a/include/util.h
+++ b/include/util.h
@@ -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);
 
-- 
1.8.4.2




More information about the CRIU mailing list