Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 2 | #include "config.h" |
| 3 | #include "dir.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 4 | #include "environment.h" |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 5 | #include "ewah/ewok.h" |
| 6 | #include "fsmonitor.h" |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 7 | #include "fsmonitor-ipc.h" |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 8 | #include "run-command.h" |
| 9 | #include "strbuf.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 10 | #include "trace2.h" |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 11 | |
Kevin Willford | 56c6910 | 2020-01-07 19:04:28 +0000 | [diff] [blame] | 12 | #define INDEX_EXTENSION_VERSION1 (1) |
| 13 | #define INDEX_EXTENSION_VERSION2 (2) |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 14 | #define HOOK_INTERFACE_VERSION1 (1) |
| 15 | #define HOOK_INTERFACE_VERSION2 (2) |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 16 | |
| 17 | struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR); |
| 18 | |
Derrick Stolee | cae70ac | 2021-01-23 19:58:14 +0000 | [diff] [blame] | 19 | static void assert_index_minimum(struct index_state *istate, size_t pos) |
| 20 | { |
| 21 | if (pos > istate->cache_nr) |
| 22 | BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)", |
| 23 | (uintmax_t)pos, istate->cache_nr); |
| 24 | } |
| 25 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 26 | static void fsmonitor_ewah_callback(size_t pos, void *is) |
| 27 | { |
| 28 | struct index_state *istate = (struct index_state *)is; |
William Baker | 3444ec2 | 2019-10-11 13:11:23 -0700 | [diff] [blame] | 29 | struct cache_entry *ce; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 30 | |
Derrick Stolee | cae70ac | 2021-01-23 19:58:14 +0000 | [diff] [blame] | 31 | assert_index_minimum(istate, pos + 1); |
William Baker | 3444ec2 | 2019-10-11 13:11:23 -0700 | [diff] [blame] | 32 | |
| 33 | ce = istate->cache[pos]; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 34 | ce->ce_flags &= ~CE_FSMONITOR_VALID; |
| 35 | } |
| 36 | |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 37 | static int fsmonitor_hook_version(void) |
| 38 | { |
| 39 | int hook_version; |
| 40 | |
| 41 | if (git_config_get_int("core.fsmonitorhookversion", &hook_version)) |
| 42 | return -1; |
| 43 | |
| 44 | if (hook_version == HOOK_INTERFACE_VERSION1 || |
| 45 | hook_version == HOOK_INTERFACE_VERSION2) |
| 46 | return hook_version; |
| 47 | |
| 48 | warning("Invalid hook version '%i' in core.fsmonitorhookversion. " |
| 49 | "Must be 1 or 2.", hook_version); |
| 50 | return -1; |
| 51 | } |
| 52 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 53 | int read_fsmonitor_extension(struct index_state *istate, const void *data, |
| 54 | unsigned long sz) |
| 55 | { |
| 56 | const char *index = data; |
| 57 | uint32_t hdr_version; |
| 58 | uint32_t ewah_size; |
| 59 | struct ewah_bitmap *fsmonitor_dirty; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 60 | int ret; |
Kevin Willford | 56c6910 | 2020-01-07 19:04:28 +0000 | [diff] [blame] | 61 | uint64_t timestamp; |
| 62 | struct strbuf last_update = STRBUF_INIT; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 63 | |
Kevin Willford | 56c6910 | 2020-01-07 19:04:28 +0000 | [diff] [blame] | 64 | if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t)) |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 65 | return error("corrupt fsmonitor extension (too short)"); |
| 66 | |
| 67 | hdr_version = get_be32(index); |
| 68 | index += sizeof(uint32_t); |
Kevin Willford | 56c6910 | 2020-01-07 19:04:28 +0000 | [diff] [blame] | 69 | if (hdr_version == INDEX_EXTENSION_VERSION1) { |
| 70 | timestamp = get_be64(index); |
| 71 | strbuf_addf(&last_update, "%"PRIu64"", timestamp); |
| 72 | index += sizeof(uint64_t); |
| 73 | } else if (hdr_version == INDEX_EXTENSION_VERSION2) { |
| 74 | strbuf_addstr(&last_update, index); |
| 75 | index += last_update.len + 1; |
| 76 | } else { |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 77 | return error("bad fsmonitor version %d", hdr_version); |
Kevin Willford | 56c6910 | 2020-01-07 19:04:28 +0000 | [diff] [blame] | 78 | } |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 79 | |
Kevin Willford | 56c6910 | 2020-01-07 19:04:28 +0000 | [diff] [blame] | 80 | istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 81 | |
| 82 | ewah_size = get_be32(index); |
| 83 | index += sizeof(uint32_t); |
| 84 | |
| 85 | fsmonitor_dirty = ewah_new(); |
| 86 | ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size); |
| 87 | if (ret != ewah_size) { |
| 88 | ewah_free(fsmonitor_dirty); |
| 89 | return error("failed to parse ewah bitmap reading fsmonitor index extension"); |
| 90 | } |
Alex Vandiver | ba1b9ca | 2017-10-27 16:26:37 -0700 | [diff] [blame] | 91 | istate->fsmonitor_dirty = fsmonitor_dirty; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 92 | |
Derrick Stolee | cae70ac | 2021-01-23 19:58:14 +0000 | [diff] [blame] | 93 | if (!istate->split_index) |
| 94 | assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size); |
William Baker | 3444ec2 | 2019-10-11 13:11:23 -0700 | [diff] [blame] | 95 | |
Jeff Hostetler | 29fbbf4 | 2021-02-03 15:34:48 +0000 | [diff] [blame] | 96 | trace2_data_string("index", NULL, "extension/fsmn/read/token", |
| 97 | istate->fsmonitor_last_update); |
| 98 | trace_printf_key(&trace_fsmonitor, |
| 99 | "read fsmonitor extension successful '%s'", |
| 100 | istate->fsmonitor_last_update); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
Alex Vandiver | 3bd28eb | 2017-11-09 11:58:10 -0800 | [diff] [blame] | 104 | void fill_fsmonitor_bitmap(struct index_state *istate) |
| 105 | { |
William Baker | 3444ec2 | 2019-10-11 13:11:23 -0700 | [diff] [blame] | 106 | unsigned int i, skipped = 0; |
Alex Vandiver | 3bd28eb | 2017-11-09 11:58:10 -0800 | [diff] [blame] | 107 | istate->fsmonitor_dirty = ewah_new(); |
William Baker | 3444ec2 | 2019-10-11 13:11:23 -0700 | [diff] [blame] | 108 | for (i = 0; i < istate->cache_nr; i++) { |
| 109 | if (istate->cache[i]->ce_flags & CE_REMOVE) |
| 110 | skipped++; |
| 111 | else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID)) |
| 112 | ewah_set(istate->fsmonitor_dirty, i - skipped); |
| 113 | } |
Alex Vandiver | 3bd28eb | 2017-11-09 11:58:10 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 116 | void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate) |
| 117 | { |
| 118 | uint32_t hdr_version; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 119 | uint32_t ewah_start; |
| 120 | uint32_t ewah_size = 0; |
| 121 | int fixup = 0; |
| 122 | |
Derrick Stolee | cae70ac | 2021-01-23 19:58:14 +0000 | [diff] [blame] | 123 | if (!istate->split_index) |
| 124 | assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size); |
William Baker | 3444ec2 | 2019-10-11 13:11:23 -0700 | [diff] [blame] | 125 | |
Kevin Willford | 56c6910 | 2020-01-07 19:04:28 +0000 | [diff] [blame] | 126 | put_be32(&hdr_version, INDEX_EXTENSION_VERSION2); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 127 | strbuf_add(sb, &hdr_version, sizeof(uint32_t)); |
| 128 | |
Kevin Willford | 56c6910 | 2020-01-07 19:04:28 +0000 | [diff] [blame] | 129 | strbuf_addstr(sb, istate->fsmonitor_last_update); |
| 130 | strbuf_addch(sb, 0); /* Want to keep a NUL */ |
| 131 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 132 | fixup = sb->len; |
| 133 | strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */ |
| 134 | |
| 135 | ewah_start = sb->len; |
Alex Vandiver | 3bd28eb | 2017-11-09 11:58:10 -0800 | [diff] [blame] | 136 | ewah_serialize_strbuf(istate->fsmonitor_dirty, sb); |
| 137 | ewah_free(istate->fsmonitor_dirty); |
| 138 | istate->fsmonitor_dirty = NULL; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 139 | |
| 140 | /* fix up size field */ |
| 141 | put_be32(&ewah_size, sb->len - ewah_start); |
| 142 | memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t)); |
| 143 | |
Jeff Hostetler | 29fbbf4 | 2021-02-03 15:34:48 +0000 | [diff] [blame] | 144 | trace2_data_string("index", NULL, "extension/fsmn/write/token", |
| 145 | istate->fsmonitor_last_update); |
| 146 | trace_printf_key(&trace_fsmonitor, |
| 147 | "write fsmonitor extension successful '%s'", |
| 148 | istate->fsmonitor_last_update); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
Kevin Willford | 56c6910 | 2020-01-07 19:04:28 +0000 | [diff] [blame] | 152 | * Call the query-fsmonitor hook passing the last update token of the saved results. |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 153 | */ |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 154 | static int query_fsmonitor_hook(struct repository *r, |
| 155 | int version, |
| 156 | const char *last_update, |
| 157 | struct strbuf *query_result) |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 158 | { |
| 159 | struct child_process cp = CHILD_PROCESS_INIT; |
Jeff Hostetler | 940b94f | 2021-02-03 15:34:47 +0000 | [diff] [blame] | 160 | int result; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 161 | |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 162 | if (fsm_settings__get_mode(r) != FSMONITOR_MODE_HOOK) |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 163 | return -1; |
| 164 | |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 165 | strvec_push(&cp.args, fsm_settings__get_hook_path(r)); |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 166 | strvec_pushf(&cp.args, "%d", version); |
| 167 | strvec_pushf(&cp.args, "%s", last_update); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 168 | cp.use_shell = 1; |
Alex Vandiver | 11cf33b | 2017-10-27 16:26:34 -0700 | [diff] [blame] | 169 | cp.dir = get_git_work_tree(); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 170 | |
Jeff Hostetler | 940b94f | 2021-02-03 15:34:47 +0000 | [diff] [blame] | 171 | trace2_region_enter("fsm_hook", "query", NULL); |
| 172 | |
| 173 | result = capture_command(&cp, query_result, 1024); |
| 174 | |
| 175 | if (result) |
| 176 | trace2_data_intmax("fsm_hook", NULL, "query/failed", result); |
Jeff Hostetler | 974c1b3 | 2022-03-25 18:02:44 +0000 | [diff] [blame] | 177 | else |
Jeff Hostetler | 940b94f | 2021-02-03 15:34:47 +0000 | [diff] [blame] | 178 | trace2_data_intmax("fsm_hook", NULL, "query/response-length", |
| 179 | query_result->len); |
| 180 | |
Jeff Hostetler | 940b94f | 2021-02-03 15:34:47 +0000 | [diff] [blame] | 181 | trace2_region_leave("fsm_hook", "query", NULL); |
| 182 | |
| 183 | return result; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Kevin Willford | ff03836 | 2021-02-03 15:34:49 +0000 | [diff] [blame] | 186 | static void fsmonitor_refresh_callback(struct index_state *istate, char *name) |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 187 | { |
Kevin Willford | ff03836 | 2021-02-03 15:34:49 +0000 | [diff] [blame] | 188 | int i, len = strlen(name); |
Jeff Hostetler | 95a4e78 | 2022-05-26 21:47:14 +0000 | [diff] [blame] | 189 | int pos = index_name_pos(istate, name, len); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 190 | |
Jeff Hostetler | 95a4e78 | 2022-05-26 21:47:14 +0000 | [diff] [blame] | 191 | trace_printf_key(&trace_fsmonitor, |
| 192 | "fsmonitor_refresh_callback '%s' (pos %d)", |
| 193 | name, pos); |
| 194 | |
| 195 | if (name[len - 1] == '/') { |
Kevin Willford | ff03836 | 2021-02-03 15:34:49 +0000 | [diff] [blame] | 196 | /* |
Jeff Hostetler | 95a4e78 | 2022-05-26 21:47:14 +0000 | [diff] [blame] | 197 | * The daemon can decorate directory events, such as |
| 198 | * moves or renames, with a trailing slash if the OS |
| 199 | * FS Event contains sufficient information, such as |
| 200 | * MacOS. |
| 201 | * |
| 202 | * Use this to invalidate the entire cone under that |
| 203 | * directory. |
| 204 | * |
| 205 | * We do not expect an exact match because the index |
| 206 | * does not normally contain directory entries, so we |
| 207 | * start at the insertion point and scan. |
Kevin Willford | ff03836 | 2021-02-03 15:34:49 +0000 | [diff] [blame] | 208 | */ |
Jeff Hostetler | 95a4e78 | 2022-05-26 21:47:14 +0000 | [diff] [blame] | 209 | if (pos < 0) |
| 210 | pos = -pos - 1; |
Kevin Willford | ff03836 | 2021-02-03 15:34:49 +0000 | [diff] [blame] | 211 | |
| 212 | /* Mark all entries for the folder invalid */ |
Jeff Hostetler | 95a4e78 | 2022-05-26 21:47:14 +0000 | [diff] [blame] | 213 | for (i = pos; i < istate->cache_nr; i++) { |
| 214 | if (!starts_with(istate->cache[i]->name, name)) |
| 215 | break; |
| 216 | istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; |
Kevin Willford | ff03836 | 2021-02-03 15:34:49 +0000 | [diff] [blame] | 217 | } |
Kevin Willford | ff03836 | 2021-02-03 15:34:49 +0000 | [diff] [blame] | 218 | |
Jeff Hostetler | 95a4e78 | 2022-05-26 21:47:14 +0000 | [diff] [blame] | 219 | /* |
| 220 | * We need to remove the traling "/" from the path |
| 221 | * for the untracked cache. |
| 222 | */ |
| 223 | name[len - 1] = '\0'; |
| 224 | } else if (pos >= 0) { |
| 225 | /* |
| 226 | * We have an exact match for this path and can just |
| 227 | * invalidate it. |
| 228 | */ |
| 229 | istate->cache[pos]->ce_flags &= ~CE_FSMONITOR_VALID; |
| 230 | } else { |
| 231 | /* |
| 232 | * The path is not a tracked file -or- it is a |
| 233 | * directory event on a platform that cannot |
| 234 | * distinguish between file and directory events in |
| 235 | * the event handler, such as Windows. |
| 236 | * |
| 237 | * Scan as if it is a directory and invalidate the |
| 238 | * cone under it. (But remember to ignore items |
| 239 | * between "name" and "name/", such as "name-" and |
| 240 | * "name.". |
| 241 | */ |
| 242 | pos = -pos - 1; |
| 243 | |
| 244 | for (i = pos; i < istate->cache_nr; i++) { |
| 245 | if (!starts_with(istate->cache[i]->name, name)) |
| 246 | break; |
| 247 | if ((unsigned char)istate->cache[i]->name[len] > '/') |
| 248 | break; |
| 249 | if (istate->cache[i]->name[len] == '/') |
| 250 | istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; |
Kevin Willford | ff03836 | 2021-02-03 15:34:49 +0000 | [diff] [blame] | 251 | } |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Mark the untracked cache dirty even if it wasn't found in the index |
| 256 | * as it could be a new untracked file. |
| 257 | */ |
Nguyễn Thái Ngọc Duy | 0cacebf | 2018-02-07 16:21:40 +0700 | [diff] [blame] | 258 | untracked_cache_invalidate_path(istate, name, 0); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 259 | } |
| 260 | |
Jeff Hostetler | 26b9f34 | 2022-03-25 18:03:12 +0000 | [diff] [blame] | 261 | /* |
| 262 | * The number of pathnames that we need to receive from FSMonitor |
| 263 | * before we force the index to be updated. |
| 264 | * |
| 265 | * Note that any pathname within the set of received paths MAY cause |
| 266 | * cache-entry or istate flag bits to be updated and thus cause the |
| 267 | * index to be updated on disk. |
| 268 | * |
| 269 | * However, the response may contain many paths (such as ignored |
| 270 | * paths) that will not update any flag bits. And thus not force the |
| 271 | * index to be updated. (This is fine and normal.) It also means |
| 272 | * that the token will not be updated in the FSMonitor index |
| 273 | * extension. So the next Git command will find the same token in the |
| 274 | * index, make the same token-relative request, and receive the same |
| 275 | * response (plus any newly changed paths). If this response is large |
| 276 | * (and continues to grow), performance could be impacted. |
| 277 | * |
| 278 | * For example, if the user runs a build and it writes 100K object |
| 279 | * files but doesn't modify any source files, the index would not need |
| 280 | * to be updated. The FSMonitor response (after the build and |
| 281 | * relative to a pre-build token) might be 5MB. Each subsequent Git |
| 282 | * command will receive that same 100K/5MB response until something |
| 283 | * causes the index to be updated. And `refresh_fsmonitor()` will |
| 284 | * have to iterate over those 100K paths each time. |
| 285 | * |
| 286 | * Performance could be improved if we optionally force update the |
| 287 | * index after a very large response and get an updated token into |
| 288 | * the FSMonitor index extension. This should allow subsequent |
| 289 | * commands to get smaller and more current responses. |
| 290 | * |
| 291 | * The value chosen here does not need to be precise. The index |
| 292 | * will be updated automatically the first time the user touches |
| 293 | * a tracked file and causes a command like `git status` to |
| 294 | * update an mtime to be updated and/or set a flag bit. |
| 295 | */ |
| 296 | static int fsmonitor_force_update_threshold = 100; |
| 297 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 298 | void refresh_fsmonitor(struct index_state *istate) |
| 299 | { |
Eric DeCosta | 25c2cab | 2022-10-04 17:32:30 +0000 | [diff] [blame] | 300 | static int warn_once = 0; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 301 | struct strbuf query_result = STRBUF_INIT; |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 302 | int query_success = 0, hook_version = -1; |
| 303 | size_t bol = 0; /* beginning of line */ |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 304 | uint64_t last_update; |
Kevin Willford | 56c6910 | 2020-01-07 19:04:28 +0000 | [diff] [blame] | 305 | struct strbuf last_update_token = STRBUF_INIT; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 306 | char *buf; |
Carlo Marcelo Arenas Belón | 5d137fc | 2019-06-15 09:11:35 -0700 | [diff] [blame] | 307 | unsigned int i; |
Jeff Hostetler | 974c1b3 | 2022-03-25 18:02:44 +0000 | [diff] [blame] | 308 | int is_trivial = 0; |
Ævar Arnfjörð Bjarmason | 6269f8e | 2023-01-17 14:57:00 +0100 | [diff] [blame] | 309 | struct repository *r = istate->repo; |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 310 | enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r); |
Eric DeCosta | 25c2cab | 2022-10-04 17:32:30 +0000 | [diff] [blame] | 311 | enum fsmonitor_reason reason = fsm_settings__get_reason(r); |
| 312 | |
| 313 | if (!warn_once && reason > FSMONITOR_REASON_OK) { |
Jeff King | c4f9490 | 2022-10-10 20:42:38 -0400 | [diff] [blame] | 314 | char *msg = fsm_settings__get_incompatible_msg(r, reason); |
Eric DeCosta | 25c2cab | 2022-10-04 17:32:30 +0000 | [diff] [blame] | 315 | warn_once = 1; |
Jeff King | c4f9490 | 2022-10-10 20:42:38 -0400 | [diff] [blame] | 316 | warning("%s", msg); |
| 317 | free(msg); |
Eric DeCosta | 25c2cab | 2022-10-04 17:32:30 +0000 | [diff] [blame] | 318 | } |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 319 | |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 320 | if (fsm_mode <= FSMONITOR_MODE_DISABLED || |
| 321 | istate->fsmonitor_has_run_once) |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 322 | return; |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 323 | |
Johannes Schindelin | 398a3b0 | 2019-05-07 13:10:21 +0200 | [diff] [blame] | 324 | istate->fsmonitor_has_run_once = 1; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 325 | |
| 326 | trace_printf_key(&trace_fsmonitor, "refresh fsmonitor"); |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 327 | |
| 328 | if (fsm_mode == FSMONITOR_MODE_IPC) { |
Jeff Hostetler | 9c307e8 | 2022-03-25 18:02:47 +0000 | [diff] [blame] | 329 | query_success = !fsmonitor_ipc__send_query( |
| 330 | istate->fsmonitor_last_update ? |
| 331 | istate->fsmonitor_last_update : "builtin:fake", |
| 332 | &query_result); |
| 333 | if (query_success) { |
| 334 | /* |
| 335 | * The response contains a series of nul terminated |
| 336 | * strings. The first is the new token. |
| 337 | * |
| 338 | * Use `char *buf` as an interlude to trick the CI |
| 339 | * static analysis to let us use `strbuf_addstr()` |
| 340 | * here (and only copy the token) rather than |
| 341 | * `strbuf_addbuf()`. |
| 342 | */ |
| 343 | buf = query_result.buf; |
| 344 | strbuf_addstr(&last_update_token, buf); |
| 345 | bol = last_update_token.len + 1; |
| 346 | is_trivial = query_result.buf[bol] == '/'; |
| 347 | if (is_trivial) |
| 348 | trace2_data_intmax("fsm_client", NULL, |
| 349 | "query/trivial-response", 1); |
| 350 | } else { |
| 351 | /* |
| 352 | * The builtin daemon is not available on this |
| 353 | * platform -OR- we failed to get a response. |
| 354 | * |
| 355 | * Generate a fake token (rather than a V1 |
| 356 | * timestamp) for the index extension. (If |
| 357 | * they switch back to the hook API, we don't |
| 358 | * want ambiguous state.) |
| 359 | */ |
| 360 | strbuf_addstr(&last_update_token, "builtin:fake"); |
| 361 | } |
| 362 | |
| 363 | goto apply_results; |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | assert(fsm_mode == FSMONITOR_MODE_HOOK); |
| 367 | |
| 368 | hook_version = fsmonitor_hook_version(); |
| 369 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 370 | /* |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 371 | * This could be racy so save the date/time now and query_fsmonitor_hook |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 372 | * should be inclusive to ensure we don't miss potential changes. |
| 373 | */ |
| 374 | last_update = getnanotime(); |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 375 | if (hook_version == HOOK_INTERFACE_VERSION1) |
| 376 | strbuf_addf(&last_update_token, "%"PRIu64"", last_update); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 377 | |
| 378 | /* |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 379 | * If we have a last update token, call query_fsmonitor_hook for the set of |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 380 | * changes since that token, else assume everything is possibly dirty |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 381 | * and check it all. |
| 382 | */ |
| 383 | if (istate->fsmonitor_last_update) { |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 384 | if (hook_version == -1 || hook_version == HOOK_INTERFACE_VERSION2) { |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 385 | query_success = !query_fsmonitor_hook( |
| 386 | r, HOOK_INTERFACE_VERSION2, |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 387 | istate->fsmonitor_last_update, &query_result); |
| 388 | |
| 389 | if (query_success) { |
| 390 | if (hook_version < 0) |
| 391 | hook_version = HOOK_INTERFACE_VERSION2; |
| 392 | |
| 393 | /* |
| 394 | * First entry will be the last update token |
| 395 | * Need to use a char * variable because static |
| 396 | * analysis was suggesting to use strbuf_addbuf |
| 397 | * but we don't want to copy the entire strbuf |
Elijah Newren | 6d12b53 | 2020-07-28 20:45:38 +0000 | [diff] [blame] | 398 | * only the chars up to the first NUL |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 399 | */ |
| 400 | buf = query_result.buf; |
| 401 | strbuf_addstr(&last_update_token, buf); |
| 402 | if (!last_update_token.len) { |
| 403 | warning("Empty last update token."); |
| 404 | query_success = 0; |
| 405 | } else { |
| 406 | bol = last_update_token.len + 1; |
Jeff Hostetler | 974c1b3 | 2022-03-25 18:02:44 +0000 | [diff] [blame] | 407 | is_trivial = query_result.buf[bol] == '/'; |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 408 | } |
| 409 | } else if (hook_version < 0) { |
| 410 | hook_version = HOOK_INTERFACE_VERSION1; |
| 411 | if (!last_update_token.len) |
| 412 | strbuf_addf(&last_update_token, "%"PRIu64"", last_update); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | if (hook_version == HOOK_INTERFACE_VERSION1) { |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 417 | query_success = !query_fsmonitor_hook( |
| 418 | r, HOOK_INTERFACE_VERSION1, |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 419 | istate->fsmonitor_last_update, &query_result); |
Jeff Hostetler | 974c1b3 | 2022-03-25 18:02:44 +0000 | [diff] [blame] | 420 | if (query_success) |
| 421 | is_trivial = query_result.buf[0] == '/'; |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Jeff Hostetler | 974c1b3 | 2022-03-25 18:02:44 +0000 | [diff] [blame] | 424 | if (is_trivial) |
| 425 | trace2_data_intmax("fsm_hook", NULL, |
| 426 | "query/trivial-response", 1); |
| 427 | |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 428 | trace_performance_since(last_update, "fsmonitor process '%s'", |
| 429 | fsm_settings__get_hook_path(r)); |
| 430 | trace_printf_key(&trace_fsmonitor, |
| 431 | "fsmonitor process '%s' returned %s", |
| 432 | fsm_settings__get_hook_path(r), |
| 433 | query_success ? "success" : "failure"); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Jeff Hostetler | 9c307e8 | 2022-03-25 18:02:47 +0000 | [diff] [blame] | 436 | apply_results: |
Jeff Hostetler | 974c1b3 | 2022-03-25 18:02:44 +0000 | [diff] [blame] | 437 | /* |
| 438 | * The response from FSMonitor (excluding the header token) is |
| 439 | * either: |
| 440 | * |
| 441 | * [a] a (possibly empty) list of NUL delimited relative |
| 442 | * pathnames of changed paths. This list can contain |
| 443 | * files and directories. Directories have a trailing |
| 444 | * slash. |
| 445 | * |
| 446 | * [b] a single '/' to indicate the provider had no |
| 447 | * information and that we should consider everything |
| 448 | * invalid. We call this a trivial response. |
| 449 | */ |
Jeff Hostetler | 26b9f34 | 2022-03-25 18:03:12 +0000 | [diff] [blame] | 450 | trace2_region_enter("fsmonitor", "apply_results", istate->repo); |
| 451 | |
Jeff Hostetler | 974c1b3 | 2022-03-25 18:02:44 +0000 | [diff] [blame] | 452 | if (query_success && !is_trivial) { |
| 453 | /* |
| 454 | * Mark all pathnames returned by the monitor as dirty. |
| 455 | * |
| 456 | * This updates both the cache-entries and the untracked-cache. |
| 457 | */ |
Jeff Hostetler | 26b9f34 | 2022-03-25 18:03:12 +0000 | [diff] [blame] | 458 | int count = 0; |
| 459 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 460 | buf = query_result.buf; |
Kevin Willford | 8da2c57 | 2020-01-07 19:04:29 +0000 | [diff] [blame] | 461 | for (i = bol; i < query_result.len; i++) { |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 462 | if (buf[i] != '\0') |
| 463 | continue; |
| 464 | fsmonitor_refresh_callback(istate, buf + bol); |
| 465 | bol = i + 1; |
Jeff Hostetler | 26b9f34 | 2022-03-25 18:03:12 +0000 | [diff] [blame] | 466 | count++; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 467 | } |
Jeff Hostetler | 26b9f34 | 2022-03-25 18:03:12 +0000 | [diff] [blame] | 468 | if (bol < query_result.len) { |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 469 | fsmonitor_refresh_callback(istate, buf + bol); |
Jeff Hostetler | 26b9f34 | 2022-03-25 18:03:12 +0000 | [diff] [blame] | 470 | count++; |
| 471 | } |
Utsav Shah | 679f2f9 | 2019-11-20 08:32:17 +0000 | [diff] [blame] | 472 | |
| 473 | /* Now mark the untracked cache for fsmonitor usage */ |
| 474 | if (istate->untracked) |
| 475 | istate->untracked->use_fsmonitor = 1; |
Jeff Hostetler | 26b9f34 | 2022-03-25 18:03:12 +0000 | [diff] [blame] | 476 | |
| 477 | if (count > fsmonitor_force_update_threshold) |
| 478 | istate->cache_changed |= FSMONITOR_CHANGED; |
| 479 | |
| 480 | trace2_data_intmax("fsmonitor", istate->repo, "apply_count", |
| 481 | count); |
| 482 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 483 | } else { |
Jeff Hostetler | 974c1b3 | 2022-03-25 18:02:44 +0000 | [diff] [blame] | 484 | /* |
| 485 | * We failed to get a response or received a trivial response, |
| 486 | * so invalidate everything. |
| 487 | * |
| 488 | * We only want to run the post index changed hook if |
| 489 | * we've actually changed entries, so keep track if we |
| 490 | * actually changed entries or not. |
| 491 | */ |
Utsav Shah | 679f2f9 | 2019-11-20 08:32:17 +0000 | [diff] [blame] | 492 | int is_cache_changed = 0; |
Jeff Hostetler | 974c1b3 | 2022-03-25 18:02:44 +0000 | [diff] [blame] | 493 | |
Utsav Shah | 679f2f9 | 2019-11-20 08:32:17 +0000 | [diff] [blame] | 494 | for (i = 0; i < istate->cache_nr; i++) { |
| 495 | if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) { |
| 496 | is_cache_changed = 1; |
| 497 | istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; |
| 498 | } |
| 499 | } |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 500 | |
Jeff Hostetler | 974c1b3 | 2022-03-25 18:02:44 +0000 | [diff] [blame] | 501 | /* |
| 502 | * If we're going to check every file, ensure we save |
| 503 | * the results. |
| 504 | */ |
Utsav Shah | 679f2f9 | 2019-11-20 08:32:17 +0000 | [diff] [blame] | 505 | if (is_cache_changed) |
| 506 | istate->cache_changed |= FSMONITOR_CHANGED; |
Ben Peart | ca598d5 | 2018-04-10 14:14:31 -0400 | [diff] [blame] | 507 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 508 | if (istate->untracked) |
| 509 | istate->untracked->use_fsmonitor = 0; |
| 510 | } |
Jeff Hostetler | 26b9f34 | 2022-03-25 18:03:12 +0000 | [diff] [blame] | 511 | trace2_region_leave("fsmonitor", "apply_results", istate->repo); |
| 512 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 513 | strbuf_release(&query_result); |
| 514 | |
Kevin Willford | 56c6910 | 2020-01-07 19:04:28 +0000 | [diff] [blame] | 515 | /* Now that we've updated istate, save the last_update_token */ |
| 516 | FREE_AND_NULL(istate->fsmonitor_last_update); |
| 517 | istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Jeff Hostetler | fcd19b0 | 2021-02-03 15:34:50 +0000 | [diff] [blame] | 520 | /* |
| 521 | * The caller wants to turn on FSMonitor. And when the caller writes |
| 522 | * the index to disk, a FSMonitor extension should be included. This |
| 523 | * requires that `istate->fsmonitor_last_update` not be NULL. But we |
| 524 | * have not actually talked to a FSMonitor process yet, so we don't |
| 525 | * have an initial value for this field. |
| 526 | * |
| 527 | * For a protocol V1 FSMonitor process, this field is a formatted |
| 528 | * "nanoseconds since epoch" field. However, for a protocol V2 |
| 529 | * FSMonitor process, this field is an opaque token. |
| 530 | * |
| 531 | * Historically, `add_fsmonitor()` has initialized this field to the |
| 532 | * current time for protocol V1 processes. There are lots of race |
| 533 | * conditions here, but that code has shipped... |
| 534 | * |
| 535 | * The only true solution is to use a V2 FSMonitor and get a current |
| 536 | * or default token value (that it understands), but we cannot do that |
| 537 | * until we have actually talked to an instance of the FSMonitor process |
| 538 | * (but the protocol requires that we send a token first...). |
| 539 | * |
| 540 | * For simplicity, just initialize like we have a V1 process and require |
| 541 | * that V2 processes adapt. |
| 542 | */ |
| 543 | static void initialize_fsmonitor_last_update(struct index_state *istate) |
| 544 | { |
| 545 | struct strbuf last_update = STRBUF_INIT; |
| 546 | |
| 547 | strbuf_addf(&last_update, "%"PRIu64"", getnanotime()); |
| 548 | istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL); |
| 549 | } |
| 550 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 551 | void add_fsmonitor(struct index_state *istate) |
| 552 | { |
Carlo Marcelo Arenas Belón | 5d137fc | 2019-06-15 09:11:35 -0700 | [diff] [blame] | 553 | unsigned int i; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 554 | |
| 555 | if (!istate->fsmonitor_last_update) { |
| 556 | trace_printf_key(&trace_fsmonitor, "add fsmonitor"); |
| 557 | istate->cache_changed |= FSMONITOR_CHANGED; |
Jeff Hostetler | fcd19b0 | 2021-02-03 15:34:50 +0000 | [diff] [blame] | 558 | initialize_fsmonitor_last_update(istate); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 559 | |
| 560 | /* reset the fsmonitor state */ |
| 561 | for (i = 0; i < istate->cache_nr; i++) |
| 562 | istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; |
| 563 | |
| 564 | /* reset the untracked cache */ |
| 565 | if (istate->untracked) { |
| 566 | add_untracked_cache(istate); |
| 567 | istate->untracked->use_fsmonitor = 1; |
| 568 | } |
| 569 | |
| 570 | /* Update the fsmonitor state */ |
| 571 | refresh_fsmonitor(istate); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | void remove_fsmonitor(struct index_state *istate) |
| 576 | { |
| 577 | if (istate->fsmonitor_last_update) { |
| 578 | trace_printf_key(&trace_fsmonitor, "remove fsmonitor"); |
| 579 | istate->cache_changed |= FSMONITOR_CHANGED; |
Kevin Willford | 56c6910 | 2020-01-07 19:04:28 +0000 | [diff] [blame] | 580 | FREE_AND_NULL(istate->fsmonitor_last_update); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
| 584 | void tweak_fsmonitor(struct index_state *istate) |
| 585 | { |
Carlo Marcelo Arenas Belón | 5d137fc | 2019-06-15 09:11:35 -0700 | [diff] [blame] | 586 | unsigned int i; |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 587 | int fsmonitor_enabled = (fsm_settings__get_mode(istate->repo) |
| 588 | > FSMONITOR_MODE_DISABLED); |
Alex Vandiver | ba1b9ca | 2017-10-27 16:26:37 -0700 | [diff] [blame] | 589 | |
| 590 | if (istate->fsmonitor_dirty) { |
| 591 | if (fsmonitor_enabled) { |
| 592 | /* Mark all entries valid */ |
| 593 | for (i = 0; i < istate->cache_nr; i++) { |
Jeff Hostetler | f954c7b | 2022-05-26 21:47:17 +0000 | [diff] [blame] | 594 | if (S_ISGITLINK(istate->cache[i]->ce_mode)) |
| 595 | continue; |
Alex Vandiver | ba1b9ca | 2017-10-27 16:26:37 -0700 | [diff] [blame] | 596 | istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID; |
| 597 | } |
| 598 | |
| 599 | /* Mark all previously saved entries as dirty */ |
Derrick Stolee | cae70ac | 2021-01-23 19:58:14 +0000 | [diff] [blame] | 600 | assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size); |
Alex Vandiver | ba1b9ca | 2017-10-27 16:26:37 -0700 | [diff] [blame] | 601 | ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate); |
| 602 | |
Utsav Shah | 679f2f9 | 2019-11-20 08:32:17 +0000 | [diff] [blame] | 603 | refresh_fsmonitor(istate); |
Alex Vandiver | ba1b9ca | 2017-10-27 16:26:37 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | ewah_free(istate->fsmonitor_dirty); |
| 607 | istate->fsmonitor_dirty = NULL; |
| 608 | } |
| 609 | |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 610 | if (fsmonitor_enabled) |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 611 | add_fsmonitor(istate); |
Jeff Hostetler | 1e0ea5c | 2022-03-25 18:02:46 +0000 | [diff] [blame] | 612 | else |
| 613 | remove_fsmonitor(istate); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 614 | } |