1 #include "git-compat-util.h"
3 #include "simple-ipc.h"
4 #include "fsmonitor-ipc.h"
5 #include "repository.h"
6 #include "run-command.h"
10 #ifndef HAVE_FSMONITOR_DAEMON_BACKEND
13 * A trivial implementation of the fsmonitor_ipc__ API for unsupported
17 int fsmonitor_ipc__is_supported(void)
22 const char *fsmonitor_ipc__get_path(struct repository
*r UNUSED
)
27 enum ipc_active_state
fsmonitor_ipc__get_state(void)
29 return IPC_STATE__OTHER_ERROR
;
32 int fsmonitor_ipc__send_query(const char *since_token UNUSED
,
33 struct strbuf
*answer UNUSED
)
38 int fsmonitor_ipc__send_command(const char *command UNUSED
,
39 struct strbuf
*answer UNUSED
)
46 int fsmonitor_ipc__is_supported(void)
51 enum ipc_active_state
fsmonitor_ipc__get_state(void)
53 return ipc_get_active_state(fsmonitor_ipc__get_path(the_repository
));
56 static int spawn_daemon(void)
58 struct child_process cmd
= CHILD_PROCESS_INIT
;
62 cmd
.trace2_child_class
= "fsmonitor";
63 strvec_pushl(&cmd
.args
, "fsmonitor--daemon", "start", NULL
);
65 return run_command(&cmd
);
68 int fsmonitor_ipc__send_query(const char *since_token
,
69 struct strbuf
*answer
)
72 int tried_to_spawn
= 0;
73 enum ipc_active_state state
= IPC_STATE__OTHER_ERROR
;
74 struct ipc_client_connection
*connection
= NULL
;
75 struct ipc_client_connect_options options
76 = IPC_CLIENT_CONNECT_OPTIONS_INIT
;
77 const char *tok
= since_token
? since_token
: "";
78 size_t tok_len
= since_token
? strlen(since_token
) : 0;
80 options
.wait_if_busy
= 1;
81 options
.wait_if_not_found
= 0;
83 trace2_region_enter("fsm_client", "query", NULL
);
84 trace2_data_string("fsm_client", NULL
, "query/command", tok
);
87 state
= ipc_client_try_connect(fsmonitor_ipc__get_path(the_repository
),
88 &options
, &connection
);
91 case IPC_STATE__LISTENING
:
92 ret
= ipc_client_send_command_to_connection(
93 connection
, tok
, tok_len
, answer
);
94 ipc_client_close_connection(connection
);
96 trace2_data_intmax("fsm_client", NULL
,
97 "query/response-length", answer
->len
);
100 case IPC_STATE__NOT_LISTENING
:
101 case IPC_STATE__PATH_NOT_FOUND
:
110 * Try again, but this time give the daemon a chance to
111 * actually create the pipe/socket.
113 * Granted, the daemon just started so it can't possibly have
114 * any FS cached yet, so we'll always get a trivial answer.
115 * BUT the answer should include a new token that can serve
116 * as the basis for subsequent requests.
118 options
.wait_if_not_found
= 1;
121 case IPC_STATE__INVALID_PATH
:
122 ret
= error(_("fsmonitor_ipc__send_query: invalid path '%s'"),
123 fsmonitor_ipc__get_path(the_repository
));
126 case IPC_STATE__OTHER_ERROR
:
128 ret
= error(_("fsmonitor_ipc__send_query: unspecified error on '%s'"),
129 fsmonitor_ipc__get_path(the_repository
));
134 trace2_region_leave("fsm_client", "query", NULL
);
139 int fsmonitor_ipc__send_command(const char *command
,
140 struct strbuf
*answer
)
142 struct ipc_client_connection
*connection
= NULL
;
143 struct ipc_client_connect_options options
144 = IPC_CLIENT_CONNECT_OPTIONS_INIT
;
146 enum ipc_active_state state
;
147 const char *c
= command
? command
: "";
148 size_t c_len
= command
? strlen(command
) : 0;
150 strbuf_reset(answer
);
152 options
.wait_if_busy
= 1;
153 options
.wait_if_not_found
= 0;
155 state
= ipc_client_try_connect(fsmonitor_ipc__get_path(the_repository
),
156 &options
, &connection
);
157 if (state
!= IPC_STATE__LISTENING
) {
158 die(_("fsmonitor--daemon is not running"));
162 ret
= ipc_client_send_command_to_connection(connection
, c
, c_len
,
164 ipc_client_close_connection(connection
);
167 die(_("could not send '%s' command to fsmonitor--daemon"), c
);