parse-options: disallow negating OPTION_SET_INT 0
[git/gitster.git] / fsmonitor-ipc.c
blob88575aa54cad07312b2aa3dab3a87cfe0ebb3693
1 #include "git-compat-util.h"
2 #include "fsmonitor-ll.h"
3 #include "gettext.h"
4 #include "simple-ipc.h"
5 #include "fsmonitor-ipc.h"
6 #include "repository.h"
7 #include "run-command.h"
8 #include "strbuf.h"
9 #include "trace2.h"
11 #ifndef HAVE_FSMONITOR_DAEMON_BACKEND
14 * A trivial implementation of the fsmonitor_ipc__ API for unsupported
15 * platforms.
18 int fsmonitor_ipc__is_supported(void)
20 return 0;
23 const char *fsmonitor_ipc__get_path(struct repository *r)
25 return NULL;
28 enum ipc_active_state fsmonitor_ipc__get_state(void)
30 return IPC_STATE__OTHER_ERROR;
33 int fsmonitor_ipc__send_query(const char *since_token,
34 struct strbuf *answer)
36 return -1;
39 int fsmonitor_ipc__send_command(const char *command,
40 struct strbuf *answer)
42 return -1;
45 #else
47 int fsmonitor_ipc__is_supported(void)
49 return 1;
52 enum ipc_active_state fsmonitor_ipc__get_state(void)
54 return ipc_get_active_state(fsmonitor_ipc__get_path(the_repository));
57 static int spawn_daemon(void)
59 struct child_process cmd = CHILD_PROCESS_INIT;
61 cmd.git_cmd = 1;
62 cmd.no_stdin = 1;
63 cmd.trace2_child_class = "fsmonitor";
64 strvec_pushl(&cmd.args, "fsmonitor--daemon", "start", NULL);
66 return run_command(&cmd);
69 int fsmonitor_ipc__send_query(const char *since_token,
70 struct strbuf *answer)
72 int ret = -1;
73 int tried_to_spawn = 0;
74 enum ipc_active_state state = IPC_STATE__OTHER_ERROR;
75 struct ipc_client_connection *connection = NULL;
76 struct ipc_client_connect_options options
77 = IPC_CLIENT_CONNECT_OPTIONS_INIT;
78 const char *tok = since_token ? since_token : "";
79 size_t tok_len = since_token ? strlen(since_token) : 0;
81 options.wait_if_busy = 1;
82 options.wait_if_not_found = 0;
84 trace2_region_enter("fsm_client", "query", NULL);
85 trace2_data_string("fsm_client", NULL, "query/command", tok);
87 try_again:
88 state = ipc_client_try_connect(fsmonitor_ipc__get_path(the_repository),
89 &options, &connection);
91 switch (state) {
92 case IPC_STATE__LISTENING:
93 ret = ipc_client_send_command_to_connection(
94 connection, tok, tok_len, answer);
95 ipc_client_close_connection(connection);
97 trace2_data_intmax("fsm_client", NULL,
98 "query/response-length", answer->len);
99 goto done;
101 case IPC_STATE__NOT_LISTENING:
102 case IPC_STATE__PATH_NOT_FOUND:
103 if (tried_to_spawn)
104 goto done;
106 tried_to_spawn++;
107 if (spawn_daemon())
108 goto done;
111 * Try again, but this time give the daemon a chance to
112 * actually create the pipe/socket.
114 * Granted, the daemon just started so it can't possibly have
115 * any FS cached yet, so we'll always get a trivial answer.
116 * BUT the answer should include a new token that can serve
117 * as the basis for subsequent requests.
119 options.wait_if_not_found = 1;
120 goto try_again;
122 case IPC_STATE__INVALID_PATH:
123 ret = error(_("fsmonitor_ipc__send_query: invalid path '%s'"),
124 fsmonitor_ipc__get_path(the_repository));
125 goto done;
127 case IPC_STATE__OTHER_ERROR:
128 default:
129 ret = error(_("fsmonitor_ipc__send_query: unspecified error on '%s'"),
130 fsmonitor_ipc__get_path(the_repository));
131 goto done;
134 done:
135 trace2_region_leave("fsm_client", "query", NULL);
137 return ret;
140 int fsmonitor_ipc__send_command(const char *command,
141 struct strbuf *answer)
143 struct ipc_client_connection *connection = NULL;
144 struct ipc_client_connect_options options
145 = IPC_CLIENT_CONNECT_OPTIONS_INIT;
146 int ret;
147 enum ipc_active_state state;
148 const char *c = command ? command : "";
149 size_t c_len = command ? strlen(command) : 0;
151 strbuf_reset(answer);
153 options.wait_if_busy = 1;
154 options.wait_if_not_found = 0;
156 state = ipc_client_try_connect(fsmonitor_ipc__get_path(the_repository),
157 &options, &connection);
158 if (state != IPC_STATE__LISTENING) {
159 die(_("fsmonitor--daemon is not running"));
160 return -1;
163 ret = ipc_client_send_command_to_connection(connection, c, c_len,
164 answer);
165 ipc_client_close_connection(connection);
167 if (ret == -1) {
168 die(_("could not send '%s' command to fsmonitor--daemon"), c);
169 return -1;
172 return 0;
175 #endif