Jeff Hostetler | 9dcba0b | 2022-03-25 18:02:53 +0000 | [diff] [blame] | 1 | #ifndef FSMONITOR_DAEMON_H |
| 2 | #define FSMONITOR_DAEMON_H |
| 3 | |
| 4 | #ifdef HAVE_FSMONITOR_DAEMON_BACKEND |
| 5 | |
Elijah Newren | 31d20fa | 2023-12-23 17:14:53 +0000 | [diff] [blame] | 6 | #include "hashmap.h" |
Jeff Hostetler | 9dcba0b | 2022-03-25 18:02:53 +0000 | [diff] [blame] | 7 | #include "thread-utils.h" |
Eric DeCosta | 12fd27d | 2022-10-04 17:32:29 +0000 | [diff] [blame] | 8 | #include "fsmonitor-path-utils.h" |
Jeff Hostetler | 9dcba0b | 2022-03-25 18:02:53 +0000 | [diff] [blame] | 9 | |
| 10 | struct fsmonitor_batch; |
| 11 | struct fsmonitor_token_data; |
| 12 | |
Jeff Hostetler | bec486b | 2022-03-25 18:02:57 +0000 | [diff] [blame] | 13 | /* |
| 14 | * Create a new batch of path(s). The returned batch is considered |
| 15 | * private and not linked into the fsmonitor daemon state. The caller |
| 16 | * should fill this batch with one or more paths and then publish it. |
| 17 | */ |
| 18 | struct fsmonitor_batch *fsmonitor_batch__new(void); |
| 19 | |
| 20 | /* |
| 21 | * Free the list of batches starting with this one. |
| 22 | */ |
| 23 | void fsmonitor_batch__free_list(struct fsmonitor_batch *batch); |
| 24 | |
| 25 | /* |
| 26 | * Add this path to this batch of modified files. |
| 27 | * |
| 28 | * The batch should be private and NOT (yet) linked into the fsmonitor |
| 29 | * daemon state and therefore not yet visible to worker threads and so |
| 30 | * no locking is required. |
| 31 | */ |
| 32 | void fsmonitor_batch__add_path(struct fsmonitor_batch *batch, const char *path); |
| 33 | |
Jeff Hostetler | 207534e | 2022-05-26 21:47:09 +0000 | [diff] [blame] | 34 | struct fsm_listen_data; /* opaque platform-specific data for listener thread */ |
Jeff Hostetler | d060555 | 2022-05-26 21:47:10 +0000 | [diff] [blame] | 35 | struct fsm_health_data; /* opaque platform-specific data for health thread */ |
Jeff Hostetler | 9dcba0b | 2022-03-25 18:02:53 +0000 | [diff] [blame] | 36 | |
| 37 | struct fsmonitor_daemon_state { |
| 38 | pthread_t listener_thread; |
Jeff Hostetler | d060555 | 2022-05-26 21:47:10 +0000 | [diff] [blame] | 39 | pthread_t health_thread; |
Jeff Hostetler | 9dcba0b | 2022-03-25 18:02:53 +0000 | [diff] [blame] | 40 | pthread_mutex_t main_lock; |
| 41 | |
| 42 | struct strbuf path_worktree_watch; |
| 43 | struct strbuf path_gitdir_watch; |
Eric DeCosta | 12fd27d | 2022-10-04 17:32:29 +0000 | [diff] [blame] | 44 | struct alias_info alias; |
Jeff Hostetler | 9dcba0b | 2022-03-25 18:02:53 +0000 | [diff] [blame] | 45 | int nr_paths_watching; |
| 46 | |
| 47 | struct fsmonitor_token_data *current_token_data; |
| 48 | |
Jeff Hostetler | b05880d | 2022-03-25 18:03:11 +0000 | [diff] [blame] | 49 | struct strbuf path_cookie_prefix; |
| 50 | pthread_cond_t cookies_cond; |
| 51 | int cookie_seq; |
| 52 | struct hashmap cookies; |
| 53 | |
Jeff Hostetler | 207534e | 2022-05-26 21:47:09 +0000 | [diff] [blame] | 54 | int listen_error_code; |
Jeff Hostetler | d060555 | 2022-05-26 21:47:10 +0000 | [diff] [blame] | 55 | int health_error_code; |
Jeff Hostetler | 207534e | 2022-05-26 21:47:09 +0000 | [diff] [blame] | 56 | struct fsm_listen_data *listen_data; |
Jeff Hostetler | d060555 | 2022-05-26 21:47:10 +0000 | [diff] [blame] | 57 | struct fsm_health_data *health_data; |
Jeff Hostetler | 9dcba0b | 2022-03-25 18:02:53 +0000 | [diff] [blame] | 58 | |
| 59 | struct ipc_server_data *ipc_server_data; |
Jeff Hostetler | 39664e9 | 2022-05-26 21:47:07 +0000 | [diff] [blame] | 60 | struct strbuf path_ipc; |
Eric DeCosta | 12fd27d | 2022-10-04 17:32:29 +0000 | [diff] [blame] | 61 | |
Jeff Hostetler | 9dcba0b | 2022-03-25 18:02:53 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Jeff Hostetler | 0ae7a1d | 2022-03-25 18:02:55 +0000 | [diff] [blame] | 64 | /* |
| 65 | * Pathname classifications. |
| 66 | * |
| 67 | * The daemon classifies the pathnames that it receives from file |
| 68 | * system notification events into the following categories and uses |
| 69 | * that to decide whether clients are told about them. (And to watch |
| 70 | * for file system synchronization events.) |
| 71 | * |
| 72 | * The daemon only collects and reports on the set of modified paths |
| 73 | * within the working directory (proper). |
| 74 | * |
| 75 | * The client should only care about paths within the working |
| 76 | * directory proper (inside the working directory and not ".git" nor |
| 77 | * inside of ".git/"). That is, the client has read the index and is |
| 78 | * asking for a list of any paths in the working directory that have |
| 79 | * been modified since the last token. The client does not care about |
| 80 | * file system changes within the ".git/" directory (such as new loose |
| 81 | * objects or packfiles). So the client will only receive paths that |
| 82 | * are classified as IS_WORKDIR_PATH. |
| 83 | * |
| 84 | * Note that ".git" is usually a directory and is therefore inside |
| 85 | * the cone of the FS watch that we have on the working directory root, |
| 86 | * so we will also get FS events for disk activity on and within ".git/" |
| 87 | * that we need to respond to or filter from the client. |
| 88 | * |
| 89 | * But Git also allows ".git" to be a *file* that points to a GITDIR |
| 90 | * outside of the working directory. When this happens, we need to |
| 91 | * create FS watches on both the working directory root *and* on the |
| 92 | * (external) GITDIR root. (The latter is required because we put |
| 93 | * cookie files inside it and use them to sync with the FS event |
| 94 | * stream.) |
| 95 | * |
| 96 | * Note that in the context of this discussion, I'm using "GITDIR" |
| 97 | * to only mean an external GITDIR referenced by a ".git" file. |
| 98 | * |
| 99 | * The platform FS event backends will receive watch-specific |
Štěpán Němec | 97509a3 | 2023-10-05 11:00:51 +0200 | [diff] [blame] | 100 | * relative paths (except for those OSes that always emit absolute |
Jeff Hostetler | 0ae7a1d | 2022-03-25 18:02:55 +0000 | [diff] [blame] | 101 | * paths). We use the following enum and routines to classify each |
| 102 | * path so that we know how to handle it. There is a slight asymmetry |
| 103 | * here because ".git/" is inside the working directory and the |
| 104 | * (external) GITDIR is not, and therefore how we handle events may |
| 105 | * vary slightly, so I have different enums for "IS...DOT_GIT..." and |
| 106 | * "IS...GITDIR...". |
| 107 | * |
| 108 | * The daemon uses the IS_DOT_GIT and IS_GITDIR internally to mean the |
| 109 | * exact ".git" file/directory or GITDIR directory. If the daemon |
| 110 | * receives a delete event for either of these paths, it will |
| 111 | * automatically shutdown, for example. |
| 112 | * |
| 113 | * Note that the daemon DOES NOT explicitly watch nor special case the |
| 114 | * index. The daemon does not read the index nor have any internal |
| 115 | * index-relative state, so there are no "IS...INDEX..." enum values. |
| 116 | */ |
| 117 | enum fsmonitor_path_type { |
| 118 | IS_WORKDIR_PATH = 0, |
| 119 | |
| 120 | IS_DOT_GIT, |
| 121 | IS_INSIDE_DOT_GIT, |
| 122 | IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX, |
| 123 | |
| 124 | IS_GITDIR, |
| 125 | IS_INSIDE_GITDIR, |
| 126 | IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX, |
| 127 | |
| 128 | IS_OUTSIDE_CONE, |
| 129 | }; |
| 130 | |
| 131 | /* |
| 132 | * Classify a pathname relative to the root of the working directory. |
| 133 | */ |
| 134 | enum fsmonitor_path_type fsmonitor_classify_path_workdir_relative( |
| 135 | const char *relative_path); |
| 136 | |
| 137 | /* |
| 138 | * Classify a pathname relative to a <gitdir> that is external to the |
| 139 | * worktree directory. |
| 140 | */ |
| 141 | enum fsmonitor_path_type fsmonitor_classify_path_gitdir_relative( |
| 142 | const char *relative_path); |
| 143 | |
| 144 | /* |
| 145 | * Classify an absolute pathname received from a filesystem event. |
| 146 | */ |
| 147 | enum fsmonitor_path_type fsmonitor_classify_path_absolute( |
| 148 | struct fsmonitor_daemon_state *state, |
| 149 | const char *path); |
| 150 | |
Jeff Hostetler | bec486b | 2022-03-25 18:02:57 +0000 | [diff] [blame] | 151 | /* |
| 152 | * Prepend the this batch of path(s) onto the list of batches associated |
| 153 | * with the current token. This makes the batch visible to worker threads. |
| 154 | * |
| 155 | * The caller no longer owns the batch and must not free it. |
| 156 | * |
| 157 | * Wake up the client threads waiting on these cookies. |
| 158 | */ |
| 159 | void fsmonitor_publish(struct fsmonitor_daemon_state *state, |
| 160 | struct fsmonitor_batch *batch, |
| 161 | const struct string_list *cookie_names); |
| 162 | |
| 163 | /* |
| 164 | * If the platform-specific layer loses sync with the filesystem, |
| 165 | * it should call this to invalidate cached data and abort waiting |
| 166 | * threads. |
| 167 | */ |
| 168 | void fsmonitor_force_resync(struct fsmonitor_daemon_state *state); |
| 169 | |
Jeff Hostetler | 9dcba0b | 2022-03-25 18:02:53 +0000 | [diff] [blame] | 170 | #endif /* HAVE_FSMONITOR_DAEMON_BACKEND */ |
| 171 | #endif /* FSMONITOR_DAEMON_H */ |