The functions are also made non-static. This is in preparation to moving the header function implementations to a .c file.
139 lines
3.7 KiB
C
139 lines
3.7 KiB
C
#ifndef LIBGUESTFS_INSPECT_H
|
|
#define LIBGUESTFS_INSPECT_H
|
|
|
|
#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 {
|
|
size_t paths_length;
|
|
char **paths;
|
|
};
|
|
|
|
struct guestfs_inpsect_command {
|
|
char *name;
|
|
enum guestfs_inspect_command_const command;
|
|
union {
|
|
struct guestfs_ls_args ls;
|
|
struct guestfs_cat_args cat;
|
|
} args;
|
|
};
|
|
|
|
char *guestfs_inspect_endpoint() {
|
|
const char *guestfs_inspect_endpoint = getenv("GUESTFS_INSPECT_ENDPOINT");
|
|
const char *socket_file_name = "/guestfs-inspect.sock";
|
|
char *res = NULL;
|
|
if (guestfs_inspect_endpoint) {
|
|
res = malloc(strlen(guestfs_inspect_endpoint) + 1);
|
|
strcpy(res, guestfs_inspect_endpoint);
|
|
} else {
|
|
const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
|
|
if (xdg_runtime_dir) {
|
|
res = malloc(strlen(xdg_runtime_dir) + strlen(socket_file_name) + strlen("ipc://") + 1);
|
|
strcpy(res, "ipc://");
|
|
strcat(res, xdg_runtime_dir);
|
|
strcat(res, socket_file_name);
|
|
} else {
|
|
const char *tmp_dir = "/tmp";
|
|
res = malloc(strlen(tmp_dir) + strlen(socket_file_name) + strlen("ipc://") + 1);
|
|
strcpy(res, "ipc://");
|
|
strcat(res, tmp_dir);
|
|
strcat(res, socket_file_name);
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
zmsg_t *guestfs_inspect_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;
|
|
case GUESTFS_COMMAND_CAT:
|
|
zmsg_addmem(res, &(command->args.cat.paths_length), sizeof(command->args.cat.paths_length));
|
|
|
|
for (size_t i = 0; i < (command->args.cat.paths_length); i++) {
|
|
zmsg_addstr(res, command->args.cat.paths[i]);
|
|
}
|
|
break;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
struct guestfs_inpsect_command *guestfs_inspect_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;
|
|
case GUESTFS_COMMAND_CAT:
|
|
next = zmsg_pop(msg);
|
|
res->args.cat.paths_length = *(zframe_data(next));
|
|
free(next);
|
|
|
|
res->args.cat.paths = calloc(res->args.cat.paths_length, sizeof(char *));
|
|
|
|
for (size_t i = 0; i < res->args.cat.paths_length; i++) {
|
|
res->args.cat.paths[i] = zmsg_popstr(msg);
|
|
}
|
|
break;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
void guestfs_inspect_command_destroy(struct guestfs_inpsect_command **c) {
|
|
free((*c)->name);
|
|
switch ((*c)->command) {
|
|
case GUESTFS_COMMAND_LS:
|
|
for (size_t i = 0; i < (*c)->args.ls.paths_length; i++) {
|
|
free((*c)->args.ls.paths[i]);
|
|
}
|
|
free((*c)->args.ls.paths);
|
|
break;
|
|
case GUESTFS_COMMAND_CAT:
|
|
for (size_t i = 0; i < (*c)->args.cat.paths_length; i++) {
|
|
free((*c)->args.cat.paths[i]);
|
|
}
|
|
free((*c)->args.cat.paths);
|
|
break;
|
|
}
|
|
|
|
free(*c);
|
|
*c = NULL;
|
|
}
|
|
|
|
#endif // LIBGUESTFS_INSPECT_H
|