[CRIU] [PATCH v5 1/5] image: Add stat_image_at() function

Kirill Tkhai ktkhai at virtuozzo.com
Thu Aug 4 06:48:46 PDT 2016


Current implementation of open_image_at() does not
allow to differ errors during opening image. It returns
NULL in case of any problem.

Add function stat_image_at() to be able to determine
if image file does not exists or there are other problems.

v5: New

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

diff --git a/criu/image.c b/criu/image.c
index a3bb285..b6c5df6 100644
--- a/criu/image.c
+++ b/criu/image.c
@@ -1,6 +1,7 @@
 #include <unistd.h>
 #include <stdarg.h>
 #include <fcntl.h>
+#include <sys/types.h>
 #include <sys/stat.h>
 #include "crtools.h"
 #include "cr_options.h"
@@ -262,6 +263,24 @@ struct cr_img *open_image_at(int dfd, int type, unsigned long flags, ...)
 	return img;
 }
 
+int stat_image_at(int dfd, int type, struct stat *st, ...)
+{
+	char path[PATH_MAX];
+	va_list args;
+
+	if (dfd == -1)
+		dfd = get_service_fd(IMG_FD_OFF);
+
+	va_start(args, st);
+	vsnprintf(path, PATH_MAX, imgset_template[type].fmt, args);
+	va_end(args);
+
+	if (fstatat(dfd, path, st, 0) < 0)
+		return -errno;
+
+	return 0;
+}
+
 static inline u32 head_magic(int oflags)
 {
 	return oflags & O_SERVICE ? IMG_SERVICE_MAGIC : IMG_COMMON_MAGIC;
diff --git a/criu/include/image.h b/criu/include/image.h
index 65b7b0a..9a3ca15 100644
--- a/criu/include/image.h
+++ b/criu/include/image.h
@@ -148,6 +148,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 int stat_image_at(int dfd, int type, struct stat *st, ...);
+#define stat_image(type, st, ...) stat_image_at(-1, type, st, ##__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