Merge branch 'ab/config-based-hooks-base' into seen
[git] / fsmonitor--daemon.h
1 #ifndef FSMONITOR_DAEMON_H
2 #define FSMONITOR_DAEMON_H
3
4 #ifdef HAVE_FSMONITOR_DAEMON_BACKEND
5
6 #include "cache.h"
7 #include "dir.h"
8 #include "run-command.h"
9 #include "simple-ipc.h"
10 #include "thread-utils.h"
11
12 struct fsmonitor_batch;
13 struct fsmonitor_token_data;
14
15 /*
16  * Create a new batch of path(s).  The returned batch is considered
17  * private and not linked into the fsmonitor daemon state.  The caller
18  * should fill this batch with one or more paths and then publish it.
19  */
20 struct fsmonitor_batch *fsmonitor_batch__new(void);
21
22 /*
23  * Free this batch and return the value of the batch->next field.
24  */
25 struct fsmonitor_batch *fsmonitor_batch__pop(struct fsmonitor_batch *batch);
26
27 /*
28  * Add this path to this batch of modified files.
29  *
30  * The batch should be private and NOT (yet) linked into the fsmonitor
31  * daemon state and therefore not yet visible to worker threads and so
32  * no locking is required.
33  */
34 void fsmonitor_batch__add_path(struct fsmonitor_batch *batch, const char *path);
35
36 struct fsmonitor_daemon_backend_data; /* opaque platform-specific data */
37
38 struct fsmonitor_daemon_state {
39         pthread_t listener_thread;
40         pthread_mutex_t main_lock;
41
42         struct strbuf path_worktree_watch;
43         struct strbuf path_gitdir_watch;
44         int nr_paths_watching;
45
46         struct fsmonitor_token_data *current_token_data;
47
48         struct strbuf path_cookie_prefix;
49         pthread_cond_t cookies_cond;
50         int cookie_seq;
51         struct hashmap cookies;
52
53         int error_code;
54         struct fsmonitor_daemon_backend_data *backend_data;
55
56         struct ipc_server_data *ipc_server_data;
57 };
58
59 /*
60  * Pathname classifications.
61  *
62  * The daemon classifies the pathnames that it receives from file
63  * system notification events into the following categories and uses
64  * that to decide whether clients are told about them.  (And to watch
65  * for file system synchronization events.)
66  *
67  * The client should only care about paths within the working
68  * directory proper (inside the working directory and not ".git" nor
69  * inside of ".git/").  That is, the client has read the index and is
70  * asking for a list of any paths in the working directory that have
71  * been modified since the last token.  The client does not care about
72  * file system changes within the .git directory (such as new loose
73  * objects or packfiles).  So the client will only receive paths that
74  * are classified as IS_WORKDIR_PATH.
75  *
76  * The daemon uses the IS_DOT_GIT and IS_GITDIR internally to mean the
77  * exact ".git" directory or GITDIR.  If the daemon receives a delete
78  * event for either of these directories, it will automatically
79  * shutdown, for example.
80  *
81  * Note that the daemon DOES NOT explicitly watch nor special case the
82  * ".git/index" file.  The daemon does not read the index and does not
83  * have any internal index-relative state.  The daemon only collects
84  * the set of modified paths within the working directory.
85  */
86 enum fsmonitor_path_type {
87         IS_WORKDIR_PATH = 0,
88
89         IS_DOT_GIT,
90         IS_INSIDE_DOT_GIT,
91         IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX,
92
93         IS_GITDIR,
94         IS_INSIDE_GITDIR,
95         IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX,
96
97         IS_OUTSIDE_CONE,
98 };
99
100 /*
101  * Classify a pathname relative to the root of the working directory.
102  */
103 enum fsmonitor_path_type fsmonitor_classify_path_workdir_relative(
104         const char *relative_path);
105
106 /*
107  * Classify a pathname relative to a <gitdir> that is external to the
108  * worktree directory.
109  */
110 enum fsmonitor_path_type fsmonitor_classify_path_gitdir_relative(
111         const char *relative_path);
112
113 /*
114  * Classify an absolute pathname received from a filesystem event.
115  */
116 enum fsmonitor_path_type fsmonitor_classify_path_absolute(
117         struct fsmonitor_daemon_state *state,
118         const char *path);
119
120 /*
121  * Prepend the this batch of path(s) onto the list of batches associated
122  * with the current token.  This makes the batch visible to worker threads.
123  *
124  * The caller no longer owns the batch and must not free it.
125  *
126  * Wake up the client threads waiting on these cookies.
127  */
128 void fsmonitor_publish(struct fsmonitor_daemon_state *state,
129                        struct fsmonitor_batch *batch,
130                        const struct string_list *cookie_names);
131
132 /*
133  * If the platform-specific layer loses sync with the filesystem,
134  * it should call this to invalidate cached data and abort waiting
135  * threads.
136  */
137 void fsmonitor_force_resync(struct fsmonitor_daemon_state *state);
138
139 #endif /* HAVE_FSMONITOR_DAEMON_BACKEND */
140 #endif /* FSMONITOR_DAEMON_H */