Compare commits
1 Commits
0134deeb56
...
d4dc71376e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d4dc71376e |
@@ -1,12 +1,79 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <czmq.h>
|
#include <czmq.h>
|
||||||
#include <zactor.h>
|
#include <zactor.h>
|
||||||
#include <guestfs.h>
|
#include <guestfs.h>
|
||||||
|
|
||||||
#define STREQ(a, b) (strcmp((a), (b)) == 0)
|
static int compare_key_len(const void *p1, const void *p2) {
|
||||||
|
const char *key1 = *(char* const*) p1;
|
||||||
|
const char *key2 = *(char* const*) p2;
|
||||||
|
return strlen(key1) - strlen(key2);
|
||||||
|
}
|
||||||
|
|
||||||
void *worker_task;
|
static int count_mountpoints(char *const *argv) {
|
||||||
|
size_t c;
|
||||||
|
for (c = 0; argv[c]; c++) {}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void worker_task(char *disk_path, char *file_path) {
|
||||||
|
guestfs_h *g;
|
||||||
|
char **roots, **mountpoints;
|
||||||
|
char *root;
|
||||||
|
size_t i, j;
|
||||||
|
|
||||||
|
char *file_content;
|
||||||
|
size_t file_size;
|
||||||
|
|
||||||
|
g = guestfs_create();
|
||||||
|
if (g == NULL) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (guestfs_add_drive_opts(g, disk_path,
|
||||||
|
GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
|
||||||
|
-1) == -1) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (guestfs_launch(g) == -1) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
roots = guestfs_inspect_os(g);
|
||||||
|
|
||||||
|
if (roots == NULL) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (j = 0; roots[j] != NULL; j++) {
|
||||||
|
root = roots[j];
|
||||||
|
mountpoints = guestfs_inspect_get_mountpoints(g, root);
|
||||||
|
if (mountpoints == NULL) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
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]);
|
||||||
|
free(mountpoints[i]);
|
||||||
|
free(mountpoints[i+1]);
|
||||||
|
}
|
||||||
|
free(mountpoints);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
char *endpoint(void);
|
char *endpoint(void);
|
||||||
|
|
||||||
@@ -19,37 +86,14 @@ int main(int argc, char **argv) {
|
|||||||
const int worker_count = argc - 1;
|
const int worker_count = argc - 1;
|
||||||
|
|
||||||
// One worker per disk image.
|
// One worker per disk image.
|
||||||
struct {
|
zactor_t *workers[worker_count];
|
||||||
char *name;
|
|
||||||
zactor_t *worker;
|
|
||||||
} worker_map[worker_count];
|
|
||||||
|
|
||||||
for (int i = 0; i < worker_count; i++) {
|
for (int i = 0; i < worker_count; i++) {
|
||||||
char *path = strtok(argv[i+1], ":");
|
char *path = strtok(argv[i+1], ":");
|
||||||
worker_map[i].name = strtok(NULL, ":");
|
char *name = strtok(NULL, ":");
|
||||||
zactor_new(worker_map[i].worker, path);
|
workers[i] = zactor_new(worker_task, (void *) &((struct { char* path; char *name; }) {.path = path, .name = name}));
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ep = endpoint();
|
|
||||||
zsock_t *frontend = zsock_new_router(ep);
|
|
||||||
free(ep);
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
zmsg_t *msg = zmsg_recv(frontend);
|
|
||||||
|
|
||||||
if (!msg) break;
|
|
||||||
|
|
||||||
// Find the worker with the given name.
|
|
||||||
zactor_t *worker = NULL;
|
|
||||||
for (int i = 0; i < worker_count; i++) {
|
|
||||||
|
|
||||||
}
|
|
||||||
if (worker) {
|
|
||||||
zmsg_send(&msg, zactor_sock(worker));
|
|
||||||
} else {
|
|
||||||
// The name specified does not exist.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user