|
| 1 | +/* |
| 2 | + * test-fsmonitor-client.c: client code to send commands/requests to |
| 3 | + * a `git fsmonitor--daemon` daemon. |
| 4 | + */ |
| 5 | + |
| 6 | +#include "test-tool.h" |
| 7 | +#include "cache.h" |
| 8 | +#include "parse-options.h" |
| 9 | +#include "fsmonitor-ipc.h" |
| 10 | + |
| 11 | +#ifndef HAVE_FSMONITOR_DAEMON_BACKEND |
| 12 | +int cmd__fsmonitor_client(int argc, const char **argv) |
| 13 | +{ |
| 14 | + die("fsmonitor--daemon not available on this platform"); |
| 15 | +} |
| 16 | +#else |
| 17 | + |
| 18 | +/* |
| 19 | + * Read the `.git/index` to get the last token written to the |
| 20 | + * FSMonitor Index Extension. |
| 21 | + */ |
| 22 | +static const char *get_token_from_index(void) |
| 23 | +{ |
| 24 | + struct index_state *istate = the_repository->index; |
| 25 | + |
| 26 | + if (do_read_index(istate, the_repository->index_file, 0) < 0) |
| 27 | + die("unable to read index file"); |
| 28 | + if (!istate->fsmonitor_last_update) |
| 29 | + die("index file does not have fsmonitor extension"); |
| 30 | + |
| 31 | + return istate->fsmonitor_last_update; |
| 32 | +} |
| 33 | + |
| 34 | +/* |
| 35 | + * Send an IPC query to a `git-fsmonitor--daemon` daemon and |
| 36 | + * ask for the changes since the given token or from the last |
| 37 | + * token in the index extension. |
| 38 | + * |
| 39 | + * This will implicitly start a daemon process if necessary. The |
| 40 | + * daemon process will persist after we exit. |
| 41 | + */ |
| 42 | +static int do_send_query(const char *token) |
| 43 | +{ |
| 44 | + struct strbuf answer = STRBUF_INIT; |
| 45 | + int ret; |
| 46 | + |
| 47 | + if (!token || !*token) |
| 48 | + token = get_token_from_index(); |
| 49 | + |
| 50 | + ret = fsmonitor_ipc__send_query(token, &answer); |
| 51 | + if (ret < 0) |
| 52 | + die("could not query fsmonitor--daemon"); |
| 53 | + |
| 54 | + write_in_full(1, answer.buf, answer.len); |
| 55 | + strbuf_release(&answer); |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +/* |
| 61 | + * Send a "flush" command to the `git-fsmonitor--daemon` (if running) |
| 62 | + * and tell it to flush its cache. |
| 63 | + * |
| 64 | + * This feature is primarily used by the test suite to simulate a loss of |
| 65 | + * sync with the filesystem where we miss kernel events. |
| 66 | + */ |
| 67 | +static int do_send_flush(void) |
| 68 | +{ |
| 69 | + struct strbuf answer = STRBUF_INIT; |
| 70 | + int ret; |
| 71 | + |
| 72 | + ret = fsmonitor_ipc__send_command("flush", &answer); |
| 73 | + if (ret) |
| 74 | + return ret; |
| 75 | + |
| 76 | + write_in_full(1, answer.buf, answer.len); |
| 77 | + strbuf_release(&answer); |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +int cmd__fsmonitor_client(int argc, const char **argv) |
| 83 | +{ |
| 84 | + const char *subcmd; |
| 85 | + const char *token = NULL; |
| 86 | + |
| 87 | + const char * const fsmonitor_client_usage[] = { |
| 88 | + "test-tool fsmonitor-client query [<token>]", |
| 89 | + "test-tool fsmonitor-client flush", |
| 90 | + NULL, |
| 91 | + }; |
| 92 | + |
| 93 | + struct option options[] = { |
| 94 | + OPT_STRING(0, "token", &token, "token", |
| 95 | + "command token to send to the server"), |
| 96 | + OPT_END() |
| 97 | + }; |
| 98 | + |
| 99 | + argc = parse_options(argc, argv, NULL, options, fsmonitor_client_usage, 0); |
| 100 | + |
| 101 | + if (argc != 1) |
| 102 | + usage_with_options(fsmonitor_client_usage, options); |
| 103 | + |
| 104 | + subcmd = argv[0]; |
| 105 | + |
| 106 | + setup_git_directory(); |
| 107 | + |
| 108 | + if (!strcmp(subcmd, "query")) |
| 109 | + return !!do_send_query(token); |
| 110 | + |
| 111 | + if (!strcmp(subcmd, "flush")) |
| 112 | + return !!do_send_flush(); |
| 113 | + |
| 114 | + die("Unhandled subcommand: '%s'", subcmd); |
| 115 | +} |
| 116 | +#endif |
0 commit comments