1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
5 #include "simple-ipc.h"
6 #include "fsmonitor-ipc.h"
7 #include "repository.h"
8 #include "run-command.h"
12 #ifndef HAVE_FSMONITOR_DAEMON_BACKEND
15 * A trivial implementation of the fsmonitor_ipc__ API for unsupported
19 int fsmonitor_ipc__is_supported(void)
24 const char *fsmonitor_ipc__get_path(struct repository
*r UNUSED
)
29 enum ipc_active_state
fsmonitor_ipc__get_state(void)
31 return IPC_STATE__OTHER_ERROR
;
34 int fsmonitor_ipc__send_query(const char *since_token UNUSED
,
35 struct strbuf
*answer UNUSED
)
40 int fsmonitor_ipc__send_command(const char *command UNUSED
,
41 struct strbuf
*answer UNUSED
)
48 int fsmonitor_ipc__is_supported(void)
53 enum ipc_active_state
fsmonitor_ipc__get_state(void)
55 return ipc_get_active_state(fsmonitor_ipc__get_path(the_repository
));
58 static int spawn_daemon(void)
60 struct child_process cmd
= CHILD_PROCESS_INIT
;
64 cmd
.trace2_child_class
= "fsmonitor";
65 strvec_pushl(&cmd
.args
, "fsmonitor--daemon", "start", NULL
);
67 return run_command(&cmd
);
70 int fsmonitor_ipc__send_query(const char *since_token
,
71 struct strbuf
*answer
)
74 int tried_to_spawn
= 0;
75 enum ipc_active_state state
= IPC_STATE__OTHER_ERROR
;
76 struct ipc_client_connection
*connection
= NULL
;
77 struct ipc_client_connect_options options
78 = IPC_CLIENT_CONNECT_OPTIONS_INIT
;
79 const char *tok
= since_token
? since_token
: "";
80 size_t tok_len
= since_token
? strlen(since_token
) : 0;
82 options
.wait_if_busy
= 1;
83 options
.wait_if_not_found
= 0;
85 trace2_region_enter("fsm_client", "query", NULL
);
86 trace2_data_string("fsm_client", NULL
, "query/command", tok
);
89 state
= ipc_client_try_connect(fsmonitor_ipc__get_path(the_repository
),
90 &options
, &connection
);
93 case IPC_STATE__LISTENING
:
94 ret
= ipc_client_send_command_to_connection(
95 connection
, tok
, tok_len
, answer
);
96 ipc_client_close_connection(connection
);
98 trace2_data_intmax("fsm_client", NULL
,
99 "query/response-length", answer
->len
);
102 case IPC_STATE__NOT_LISTENING
:
103 case IPC_STATE__PATH_NOT_FOUND
:
112 * Try again, but this time give the daemon a chance to
113 * actually create the pipe/socket.
115 * Granted, the daemon just started so it can't possibly have
116 * any FS cached yet, so we'll always get a trivial answer.
117 * BUT the answer should include a new token that can serve
118 * as the basis for subsequent requests.
120 options
.wait_if_not_found
= 1;
123 case IPC_STATE__INVALID_PATH
:
124 ret
= error(_("fsmonitor_ipc__send_query: invalid path '%s'"),
125 fsmonitor_ipc__get_path(the_repository
));
128 case IPC_STATE__OTHER_ERROR
:
130 ret
= error(_("fsmonitor_ipc__send_query: unspecified error on '%s'"),
131 fsmonitor_ipc__get_path(the_repository
));
136 trace2_region_leave("fsm_client", "query", NULL
);
141 int fsmonitor_ipc__send_command(const char *command
,
142 struct strbuf
*answer
)
144 struct ipc_client_connection
*connection
= NULL
;
145 struct ipc_client_connect_options options
146 = IPC_CLIENT_CONNECT_OPTIONS_INIT
;
148 enum ipc_active_state state
;
149 const char *c
= command
? command
: "";
150 size_t c_len
= command
? strlen(command
) : 0;
152 strbuf_reset(answer
);
154 options
.wait_if_busy
= 1;
155 options
.wait_if_not_found
= 0;
157 state
= ipc_client_try_connect(fsmonitor_ipc__get_path(the_repository
),
158 &options
, &connection
);
159 if (state
!= IPC_STATE__LISTENING
) {
160 die(_("fsmonitor--daemon is not running"));
164 ret
= ipc_client_send_command_to_connection(connection
, c
, c_len
,
166 ipc_client_close_connection(connection
);
169 die(_("could not send '%s' command to fsmonitor--daemon"), c
);