Skip to content

Commit 0ae7a1d

Browse files
jeffhostetlergitster
authored andcommitted
fsmonitor--daemon: add pathname classification
Teach fsmonitor--daemon to classify relative and absolute pathnames and decide how they should be handled. This will be used by the platform-specific backend to respond to each filesystem event. When we register for filesystem notifications on a directory, we get events for everything (recursively) in the directory. We want to report to clients changes to tracked and untracked paths within the working directory proper. We do not want to report changes within the .git directory, for example. This classification will be used in a later commit by the different backends to classify paths as events are received. Signed-off-by: Jeff Hostetler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c284e27 commit 0ae7a1d

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed

builtin/fsmonitor--daemon.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,87 @@ static int handle_client(void *data,
134134
return result;
135135
}
136136

137+
#define FSMONITOR_COOKIE_PREFIX ".fsmonitor-daemon-"
138+
139+
enum fsmonitor_path_type fsmonitor_classify_path_workdir_relative(
140+
const char *rel)
141+
{
142+
if (fspathncmp(rel, ".git", 4))
143+
return IS_WORKDIR_PATH;
144+
rel += 4;
145+
146+
if (!*rel)
147+
return IS_DOT_GIT;
148+
if (*rel != '/')
149+
return IS_WORKDIR_PATH; /* e.g. .gitignore */
150+
rel++;
151+
152+
if (!fspathncmp(rel, FSMONITOR_COOKIE_PREFIX,
153+
strlen(FSMONITOR_COOKIE_PREFIX)))
154+
return IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX;
155+
156+
return IS_INSIDE_DOT_GIT;
157+
}
158+
159+
enum fsmonitor_path_type fsmonitor_classify_path_gitdir_relative(
160+
const char *rel)
161+
{
162+
if (!fspathncmp(rel, FSMONITOR_COOKIE_PREFIX,
163+
strlen(FSMONITOR_COOKIE_PREFIX)))
164+
return IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX;
165+
166+
return IS_INSIDE_GITDIR;
167+
}
168+
169+
static enum fsmonitor_path_type try_classify_workdir_abs_path(
170+
struct fsmonitor_daemon_state *state,
171+
const char *path)
172+
{
173+
const char *rel;
174+
175+
if (fspathncmp(path, state->path_worktree_watch.buf,
176+
state->path_worktree_watch.len))
177+
return IS_OUTSIDE_CONE;
178+
179+
rel = path + state->path_worktree_watch.len;
180+
181+
if (!*rel)
182+
return IS_WORKDIR_PATH; /* it is the root dir exactly */
183+
if (*rel != '/')
184+
return IS_OUTSIDE_CONE;
185+
rel++;
186+
187+
return fsmonitor_classify_path_workdir_relative(rel);
188+
}
189+
190+
enum fsmonitor_path_type fsmonitor_classify_path_absolute(
191+
struct fsmonitor_daemon_state *state,
192+
const char *path)
193+
{
194+
const char *rel;
195+
enum fsmonitor_path_type t;
196+
197+
t = try_classify_workdir_abs_path(state, path);
198+
if (state->nr_paths_watching == 1)
199+
return t;
200+
if (t != IS_OUTSIDE_CONE)
201+
return t;
202+
203+
if (fspathncmp(path, state->path_gitdir_watch.buf,
204+
state->path_gitdir_watch.len))
205+
return IS_OUTSIDE_CONE;
206+
207+
rel = path + state->path_gitdir_watch.len;
208+
209+
if (!*rel)
210+
return IS_GITDIR; /* it is the <gitdir> exactly */
211+
if (*rel != '/')
212+
return IS_OUTSIDE_CONE;
213+
rel++;
214+
215+
return fsmonitor_classify_path_gitdir_relative(rel);
216+
}
217+
137218
static void *fsm_listen__thread_proc(void *_state)
138219
{
139220
struct fsmonitor_daemon_state *state = _state;

fsmonitor--daemon.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,92 @@ struct fsmonitor_daemon_state {
3030
struct ipc_server_data *ipc_server_data;
3131
};
3232

33+
/*
34+
* Pathname classifications.
35+
*
36+
* The daemon classifies the pathnames that it receives from file
37+
* system notification events into the following categories and uses
38+
* that to decide whether clients are told about them. (And to watch
39+
* for file system synchronization events.)
40+
*
41+
* The daemon only collects and reports on the set of modified paths
42+
* within the working directory (proper).
43+
*
44+
* The client should only care about paths within the working
45+
* directory proper (inside the working directory and not ".git" nor
46+
* inside of ".git/"). That is, the client has read the index and is
47+
* asking for a list of any paths in the working directory that have
48+
* been modified since the last token. The client does not care about
49+
* file system changes within the ".git/" directory (such as new loose
50+
* objects or packfiles). So the client will only receive paths that
51+
* are classified as IS_WORKDIR_PATH.
52+
*
53+
* Note that ".git" is usually a directory and is therefore inside
54+
* the cone of the FS watch that we have on the working directory root,
55+
* so we will also get FS events for disk activity on and within ".git/"
56+
* that we need to respond to or filter from the client.
57+
*
58+
* But Git also allows ".git" to be a *file* that points to a GITDIR
59+
* outside of the working directory. When this happens, we need to
60+
* create FS watches on both the working directory root *and* on the
61+
* (external) GITDIR root. (The latter is required because we put
62+
* cookie files inside it and use them to sync with the FS event
63+
* stream.)
64+
*
65+
* Note that in the context of this discussion, I'm using "GITDIR"
66+
* to only mean an external GITDIR referenced by a ".git" file.
67+
*
68+
* The platform FS event backends will receive watch-specific
69+
* relative paths (except for those OS's that always emit absolute
70+
* paths). We use the following enum and routines to classify each
71+
* path so that we know how to handle it. There is a slight asymmetry
72+
* here because ".git/" is inside the working directory and the
73+
* (external) GITDIR is not, and therefore how we handle events may
74+
* vary slightly, so I have different enums for "IS...DOT_GIT..." and
75+
* "IS...GITDIR...".
76+
*
77+
* The daemon uses the IS_DOT_GIT and IS_GITDIR internally to mean the
78+
* exact ".git" file/directory or GITDIR directory. If the daemon
79+
* receives a delete event for either of these paths, it will
80+
* automatically shutdown, for example.
81+
*
82+
* Note that the daemon DOES NOT explicitly watch nor special case the
83+
* index. The daemon does not read the index nor have any internal
84+
* index-relative state, so there are no "IS...INDEX..." enum values.
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+
33120
#endif /* HAVE_FSMONITOR_DAEMON_BACKEND */
34121
#endif /* FSMONITOR_DAEMON_H */

0 commit comments

Comments
 (0)