[CRIU] [PATCH v2 1/2] Add test_image_at()

Kirill Tkhai ktkhai at odin.com
Fri Oct 23 09:04:38 PDT 2015


Add a helper to check if a image file exists in a dump.
The helper is simple and may be overloaded in the future,
if it's necessary.

It will be used in next patch.

Signed-off-by: Kirill Tkhai <ktkhai at odin.com>
---
 image.c         |   21 +++++++++++++++++++++
 include/image.h |    2 ++
 2 files changed, 23 insertions(+)

diff --git a/image.c b/image.c
index dc9d6a1..faf9703 100644
--- a/image.c
+++ b/image.c
@@ -1,3 +1,4 @@
+#include <sys/stat.h>
 #include <unistd.h>
 #include <stdarg.h>
 #include <fcntl.h>
@@ -259,6 +260,26 @@ struct cr_img *open_image_at(int dfd, int type, unsigned long flags, ...)
 	return img;
 }
 
+/* Test if image file exists */
+bool test_image_at(int dfd, int type, ...)
+{
+	char path[PATH_MAX];
+	struct stat buf;
+	va_list args;
+
+	if (dfd == -1)
+		dfd = get_service_fd(IMG_FD_OFF);
+
+	va_start(args, type);
+	vsnprintf(path, PATH_MAX, imgset_template[type].fmt, args);
+	va_end(args);
+
+	if (fstatat(dfd, path, &buf, 0) < 0)
+		return false;
+
+	return true;
+}
+
 static inline u32 head_magic(int oflags)
 {
 	return oflags & O_SERVICE ? IMG_SERVICE_MAGIC : IMG_COMMON_MAGIC;
diff --git a/include/image.h b/include/image.h
index 305febf..fa97ff3 100644
--- a/include/image.h
+++ b/include/image.h
@@ -170,6 +170,8 @@ extern void close_image_dir(void);
 
 extern struct cr_img *open_image_at(int dfd, int type, unsigned long flags, ...);
 #define open_image(typ, flags, ...) open_image_at(-1, typ, flags, ##__VA_ARGS__)
+extern bool test_image_at(int dfd, int type, ...);
+#define test_image(typ, ...) test_image_at(-1, typ, ##__VA_ARGS__)
 extern int open_image_lazy(struct cr_img *img);
 extern struct cr_img *open_pages_image(unsigned long flags, struct cr_img *pmi);
 extern struct cr_img *open_pages_image_at(int dfd, unsigned long flags, struct cr_img *pmi);



More information about the CRIU mailing list