From 2505662cfb5db91e88df9b1d111f3d986b95b16a Mon Sep 17 00:00:00 2001 From: Pin Date: Sun, 3 Mar 2024 01:12:59 -0500 Subject: [PATCH] breaking out init_guestfs and cat_file --- guestfs-inspectd.c | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/guestfs-inspectd.c b/guestfs-inspectd.c index b338268..cb3ba99 100644 --- a/guestfs-inspectd.c +++ b/guestfs-inspectd.c @@ -36,8 +36,17 @@ static int count_mountpoints(char *const *argv) { return c; } -static void *worker_task(zsock_t *pipe, char *disk_path) { - guestfs_h *g; +static void cat_file(guestfs_h *g, char *file_path, char **file_content, size_t *file_size) { + if (guestfs_is_file_opts(g, file_path, GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) { + (*file_content) = guestfs_read_file(g, file_path, file_size); + if ((*file_content) == NULL) { + exit(EXIT_FAILURE); + } + } + return; +} + +static void init_guestfs(guestfs_h **g, char *disk_path) { char **roots, **mountpoints; char *root; size_t i, j; @@ -46,23 +55,23 @@ static void *worker_task(zsock_t *pipe, char *disk_path) { //size_t file_size; // Create a connection handle - g = guestfs_create(); + (*g) = guestfs_create(); if (g == NULL) { exit(EXIT_FAILURE); } // Adding disk_path to connection handle - if (guestfs_add_drive_opts(g, disk_path, GUESTFS_ADD_DRIVE_OPTS_READONLY, 1, -1) == -1) { + if (guestfs_add_drive_opts((*g), disk_path, GUESTFS_ADD_DRIVE_OPTS_READONLY, 1, -1) == -1) { exit(EXIT_FAILURE); } // Launching connection handle - if (guestfs_launch(g) == -1) { + if (guestfs_launch((*g)) == -1) { exit(EXIT_FAILURE); } // Pulling rootfs information - roots = guestfs_inspect_os(g); + roots = guestfs_inspect_os((*g)); if (roots == NULL) { exit(EXIT_FAILURE); } @@ -70,21 +79,29 @@ static void *worker_task(zsock_t *pipe, char *disk_path) { // Looping through roots to mount mountpoints for (j = 0; roots[j] != NULL; j++) { root = roots[j]; - mountpoints = guestfs_inspect_get_mountpoints(g, root); + mountpoints = guestfs_inspect_get_mountpoints((*g), root); if (mountpoints == NULL) { exit(EXIT_FAILURE); } // Sorting mountpoints to be in {'${device_path}', '${mount_path}'} format qsort(mountpoints, count_mountpoints(mountpoints) / 2, 2 * sizeof (char*), compare_key_len); for (i = 0; mountpoints[i] != NULL; i += 2) { - guestfs_mount_ro(g, mountpoints[i+1], mountpoints[i]); + guestfs_mount_ro((*g), mountpoints[i+1], mountpoints[i]); free(mountpoints[i]); free(mountpoints[i+1]); } free(mountpoints); free(root); } + free(roots); +} +static void *worker_task(zsock_t *pipe, char *disk_path) { + guestfs_h *g = NULL; + + init_guestfs(&g, disk_path); + + /* // ZeroMQ Opens here zsock_signal(pipe, 0); while (true) { @@ -100,16 +117,8 @@ static void *worker_task(zsock_t *pipe, char *disk_path) { zmsg_send(&msg, pipe); zmsg_destory(&msg); } - /*if (guestfs_is_file_opts(g, file_path, GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1) > 0) { - printf("--- %s ---\n", file_path); - file_content = guestfs_read_file(g, file_path, &file_size); - if (file_content == NULL) { - exit(EXIT_FAILURE); - } - printf("%s\n", file_content); - free(file_content); - }*/ - free(roots); + */ + guestfs_close(g); exit(EXIT_SUCCESS); }