Commit d6514dae authored by Pavel Emelyanov's avatar Pavel Emelyanov

pb: Introduce show_plain routine

This one walks the 'plain' image file, i.e. the one with straignt <magi>[<size><entry>]*
format, and prints each message. Saves lots of lines of code.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 9de8295b
......@@ -67,21 +67,11 @@
#n2, s.n2, \
#n3, s.n3)
static char local_buf[PAGE_SIZE];
static LIST_HEAD(pstree_list);
void show_files(int fd_files, struct cr_options *o)
{
pr_img_head(CR_FD_FDINFO);
while (1) {
FdinfoEntry *e;
int ret = pb_read_eof(fd_files, &e, fdinfo_entry);
if (ret <= 0)
break;
pb_show_msg(e, &fdinfo_entry__descriptor);
fdinfo_entry__free_unpacked(e, NULL);
}
pr_img_tail(CR_FD_FDINFO);
pb_show_plain(fd_files, fdinfo_entry);
}
void show_fown_cont(fown_t *fown)
......@@ -100,20 +90,7 @@ void pb_show_fown_cont(void *p)
void show_reg_files(int fd_reg_files, struct cr_options *o)
{
local_buf[0] = 0;
pr_img_head(CR_FD_REG_FILES);
while (1) {
RegFileEntry *rfe;
int ret;
ret = pb_read_eof(fd_reg_files, &rfe, reg_file_entry);
if (ret <= 0)
break;
pb_show_msg(rfe, &reg_file_entry__descriptor);
reg_file_entry__free_unpacked(rfe, NULL);
}
pr_img_tail(CR_FD_REG_FILES);
pb_show_plain(fd_reg_files, reg_file_entry);
}
static inline char *remap_id_type(u32 id)
......@@ -126,19 +103,7 @@ static inline char *remap_id_type(u32 id)
void show_remap_files(int fd, struct cr_options *o)
{
RemapFilePathEntry *rfe;
pr_img_head(CR_FD_REMAP_FPATH);
while (1) {
int ret;
ret = pb_read_eof(fd, &rfe, remap_file_path_entry);
if (ret <= 0)
break;
pb_show_msg(rfe, &remap_file_path_entry__descriptor);
remap_file_path_entry__free_unpacked(rfe, NULL);
}
pr_img_tail(CR_FD_REMAP_FPATH);
pb_show_plain(fd, remap_file_path_entry);
}
void show_ghost_file(int fd, struct cr_options *o)
......@@ -175,18 +140,7 @@ void show_pipes_data(int fd_pipes, struct cr_options *o)
void show_pipes(int fd_pipes, struct cr_options *o)
{
PipeEntry *e;
int ret;
pr_img_head(CR_FD_PIPES);
while (1) {
ret = pb_read_eof(fd_pipes, &e, pipe_entry);
if (ret <= 0)
break;
pb_show_msg(e, &pipe_entry__descriptor);
pipe_entry__free_unpacked(e, NULL);
}
pr_img_tail(CR_FD_PIPES);
pb_show_plain(fd_pipes, pipe_entry);
}
void show_fifo_data(int fd, struct cr_options *o)
......@@ -198,16 +152,7 @@ void show_fifo_data(int fd, struct cr_options *o)
void show_fifo(int fd, struct cr_options *o)
{
FifoEntry *e;
pr_img_head(CR_FD_FIFO);
while (1) {
if (pb_read_eof(fd, &e, fifo_entry) <= 0)
break;
pb_show_msg(e, &fifo_entry__descriptor);
fifo_entry__free_unpacked(e, NULL);
}
pr_img_tail(CR_FD_FIFO);
pb_show_plain(fd, fifo_entry);
}
void show_fs(int fd_fs, struct cr_options *o)
......
......@@ -43,22 +43,7 @@ static void pr_info_eventfd(char *action, EventfdFileEntry *efe)
void show_eventfds(int fd, struct cr_options *o)
{
EventfdFileEntry *efe;
pr_img_head(CR_FD_EVENTFD);
while (1) {
int ret;
ret = pb_read_eof(fd, &efe, eventfd_file_entry);
if (ret <= 0)
goto out;
pb_show_msg(efe, &eventfd_file_entry__descriptor);
eventfd_file_entry__free_unpacked(efe, NULL);
}
out:
pr_img_tail(CR_FD_EVENTFD);
pb_show_plain(fd, eventfd_file_entry);
}
struct eventfd_dump_arg {
......
......@@ -55,41 +55,12 @@ static void pr_info_eventpoll(char *action, EventpollFileEntry *e)
void show_eventpoll_tfd(int fd, struct cr_options *o)
{
EventpollTfdEntry *e;
pr_img_head(CR_FD_EVENTPOLL_TFD);
while (1) {
int ret;
ret = pb_read_eof(fd, &e, eventpoll_tfd_entry);
if (ret <= 0)
goto out;
pb_show_msg(e, &eventpoll_tfd_entry__descriptor);
eventpoll_tfd_entry__free_unpacked(e, NULL);
}
out:
pr_img_tail(CR_FD_EVENTPOLL_TFD);
pb_show_plain(fd, eventpoll_tfd_entry);
}
void show_eventpoll(int fd, struct cr_options *o)
{
EventpollFileEntry *e;
pr_img_head(CR_FD_EVENTPOLL);
while (1) {
int ret;
ret = pb_read_eof(fd, &e, eventpoll_file_entry);
if (ret <= 0)
break;
pb_show_msg(e, &eventpoll_file_entry__descriptor);
eventpoll_file_entry__free_unpacked(e, NULL);
}
pr_img_tail(CR_FD_EVENTPOLL);
pb_show_plain(fd, eventpoll_file_entry);
}
static int dump_eventpoll_entry(union fdinfo_entries *e, void *arg)
......
......@@ -22,6 +22,7 @@
typedef size_t (*pb_getpksize_t)(void *obj);
typedef size_t (*pb_pack_t)(void *obj, void *where);
typedef void *(*pb_unpack_t)(void *allocator, size_t size, void *from);
typedef void (*pb_free_t)(void *obj, void *allocator);
extern int pb_read_object_with_header(int fd, void **pobj,
pb_unpack_t unpack,
......@@ -30,6 +31,7 @@ extern int pb_read_object_with_header(int fd, void **pobj,
#define PB_UNPACK_TYPECHECK(__op, __fn) ({ if (0) *__op = __fn##__unpack(NULL, 0, NULL); (pb_unpack_t)&__fn##__unpack; })
#define PB_PACK_TYPECHECK(__o, __fn) ({ if (0) __fn##__pack(__o, NULL); (pb_pack_t)&__fn##__pack; })
#define PB_GPS_TYPECHECK(__o, __fn) ({ if (0) __fn##__get_packed_size(__o); (pb_getpksize_t)&__fn##__get_packed_size; })
#define PB_FREE_TYPECHECK(__o, __fn) ({ if (0) __fn##__free_unpacked(__o, NULL); (pb_free_t)&__fn##__free_unpacked; })
#define pb_read(__fd, __obj_pptr, __proto_message_name) \
pb_read_object_with_header(__fd, (void **)__obj_pptr, \
......@@ -54,6 +56,16 @@ extern int pb_write_object_with_header(int fd, void *obj,
#define pb_repeated_size(__obj, __member) \
(sizeof(*(__obj)->__member) * (__obj)->n_ ##__member)
#include <google/protobuf-c/protobuf-c.h>
extern void pb_show_msg(const void *msg, const void *msg_desc);
extern void do_pb_show_plain(int fd, const ProtobufCMessageDescriptor *d,
pb_unpack_t unpack, pb_free_t free);
/* Don't have objects at hands to also do typechecking here */
#define pb_show_plain(__fd, __proto_message_name) \
do_pb_show_plain(__fd, &__proto_message_name##__descriptor, \
(pb_unpack_t)__proto_message_name##__unpack, \
(pb_free_t)__proto_message_name##__free_unpacked)
#endif /* PROTOBUF_H__ */
......@@ -59,43 +59,12 @@ int is_inotify_link(int lfd)
void show_inotify_wd(int fd_inotify_wd, struct cr_options *o)
{
InotifyWdEntry *e;
pr_img_head(CR_FD_INOTIFY_WD);
while (1) {
int ret;
ret = pb_read_eof(fd_inotify_wd, &e, inotify_wd_entry);
if (ret <= 0)
goto out;
if (e->f_handle->n_handle < 2) {
pr_err("Corrupted image n_handle = %d while %d expected\n",
(int)e->f_handle->n_handle, FH_ENTRY_SIZES__min_entries);
goto out;
}
pb_show_msg(e, &inotify_wd_entry__descriptor);
inotify_wd_entry__free_unpacked(e, NULL);
}
out:
pr_img_tail(CR_FD_INOTIFY_WD);
pb_show_plain(fd_inotify_wd, inotify_wd_entry);
}
void show_inotify(int fd_inotify, struct cr_options *o)
{
InotifyFileEntry *e;
pr_img_head(CR_FD_INOTIFY);
while (1) {
int ret;
ret = pb_read_eof(fd_inotify, &e, inotify_file_entry);
if (ret <= 0)
break;
pb_show_msg(e, &inotify_file_entry__descriptor);
inotify_file_entry__free_unpacked(e, NULL);
}
pr_img_tail(CR_FD_INOTIFY);
pb_show_plain(fd_inotify, inotify_file_entry);
}
static int dump_inotify_entry(union fdinfo_entries *e, void *arg)
......
......@@ -497,18 +497,5 @@ int prepare_mnt_ns(int ns_pid)
void show_mountpoints(int fd, struct cr_options *o)
{
pr_img_head(CR_FD_MOUNTPOINTS);
while (1) {
MntEntry *me;
int ret;
ret = pb_read_eof(fd, &me, mnt_entry);
if (ret <= 0)
break;
pb_show_msg(me, &mnt_entry__descriptor);
mnt_entry__free_unpacked(me, NULL);
}
pr_img_tail(CR_FD_MOUNTPOINTS);
pb_show_plain(fd, mnt_entry);
}
......@@ -143,6 +143,20 @@ void pb_show_msg(const void *msg, const void *msg_desc)
pr_msg("\n");
}
void do_pb_show_plain(int fd, const ProtobufCMessageDescriptor *md,
pb_unpack_t unpack, pb_free_t free)
{
while (1) {
void *obj;
if (pb_read_object_with_header(fd, &obj, unpack, true) <= 0)
break;
pb_show_msg(obj, md);
free(obj, NULL);
}
}
/*
* Reads PB record (header + packed object) from file @fd and unpack
* it with @unpack procedure to the pointer @pobj
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment