Was putting new data in the front of the message instead of the end. This mismatched with how I was deserializing the messages
75 lines
1.9 KiB
C
75 lines
1.9 KiB
C
#include <czmq.h>
|
|
|
|
enum guestfs_inspect_command_const {
|
|
GUESTFS_COMMAND_LS,
|
|
/* GUESTFS_COMMAND_TOUCH, */
|
|
/* GUESTFS_COMMAND_MKDIR, */
|
|
GUESTFS_COMMAND_CAT
|
|
};
|
|
|
|
struct guestfs_ls_args {
|
|
size_t paths_length;
|
|
char **paths;
|
|
};
|
|
struct guestfs_cat_args {};
|
|
|
|
struct guestfs_inpsect_command {
|
|
char *name;
|
|
enum guestfs_inspect_command_const command;
|
|
union {
|
|
struct guestfs_ls_args ls;
|
|
struct guestfs_cat_args cat;
|
|
} args;
|
|
};
|
|
|
|
static char *endpoint() {
|
|
// TODO: This should be based on GUESTFS_INSPECT_ENDPOINT, or XDG_RUNTIME_DIR if the former is not set, and default to a sensible path if neither are set.
|
|
const char* ep = "ipc:///tmp/guestfs-inspect.sock";
|
|
char *res = malloc(strlen(ep) + 1);
|
|
strcpy(res, ep);
|
|
return res;
|
|
}
|
|
|
|
static zmsg_t *command_to_zmsg(struct guestfs_inpsect_command *command) {
|
|
zmsg_t *res = zmsg_new();
|
|
|
|
zmsg_addstr(res, command->name);
|
|
zmsg_addmem(res, &(command->command), sizeof(command->command));
|
|
|
|
switch (command->command) {
|
|
case GUESTFS_COMMAND_LS:
|
|
zmsg_addmem(res, &(command->args.ls.paths_length), sizeof(command->args.ls.paths_length));
|
|
|
|
for (size_t i = 0; i < (command->args.ls.paths_length); i++) {
|
|
zmsg_addstr(res, command->args.ls.paths[i]);
|
|
}
|
|
break;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
static struct guestfs_inpsect_command *zmsg_to_command(zmsg_t *msg) {
|
|
struct guestfs_inpsect_command *res = malloc(sizeof(struct guestfs_inpsect_command));
|
|
|
|
res->name = zmsg_popstr(msg);
|
|
zframe_t *next = zmsg_pop(msg);
|
|
res->command = *(zframe_data(next));
|
|
free(next);
|
|
|
|
switch (res->command) {
|
|
case GUESTFS_COMMAND_LS:
|
|
next = zmsg_pop(msg);
|
|
res->args.ls.paths_length = *(zframe_data(next));
|
|
free(next);
|
|
|
|
res->args.ls.paths = calloc(res->args.ls.paths_length, sizeof(char *));
|
|
|
|
for (size_t i = 0; i < res->args.ls.paths_length; i++) {
|
|
res->args.ls.paths[i] = zmsg_popstr(msg);
|
|
}
|
|
break;
|
|
}
|
|
return res;
|
|
}
|