Skip to content

Commit e43cdc6

Browse files
committed
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. 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]>
1 parent 6e79621 commit e43cdc6

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

builtin/fsmonitor--daemon.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,87 @@ static int handle_client(void *data,
121121
return result;
122122
}
123123

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

fsmonitor--daemon.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,66 @@ 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 client should only care about paths within the working
42+
* directory proper (inside the working directory and not ".git" nor
43+
* inside of ".git/"). That is, the client has read the index and is
44+
* asking for a list of any paths in the working directory that have
45+
* been modified since the last token. The client does not care about
46+
* file system changes within the .git directory (such as new loose
47+
* objects or packfiles). So the client will only receive paths that
48+
* are classified as IS_WORKDIR_PATH.
49+
*
50+
* The daemon uses the IS_DOT_GIT and IS_GITDIR internally to mean the
51+
* exact ".git" directory or GITDIR. If the daemon receives a delete
52+
* event for either of these directories, it will automatically
53+
* shutdown, for example.
54+
*
55+
* Note that the daemon DOES NOT explicitly watch nor special case the
56+
* ".git/index" file. The daemon does not read the index and does not
57+
* have any internal index-relative state. The daemon only collects
58+
* the set of modified paths within the working directory.
59+
*/
60+
enum fsmonitor_path_type {
61+
IS_WORKDIR_PATH = 0,
62+
63+
IS_DOT_GIT,
64+
IS_INSIDE_DOT_GIT,
65+
IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX,
66+
67+
IS_GITDIR,
68+
IS_INSIDE_GITDIR,
69+
IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX,
70+
71+
IS_OUTSIDE_CONE,
72+
};
73+
74+
/*
75+
* Classify a pathname relative to the root of the working directory.
76+
*/
77+
enum fsmonitor_path_type fsmonitor_classify_path_workdir_relative(
78+
const char *relative_path);
79+
80+
/*
81+
* Classify a pathname relative to a <gitdir> that is external to the
82+
* worktree directory.
83+
*/
84+
enum fsmonitor_path_type fsmonitor_classify_path_gitdir_relative(
85+
const char *relative_path);
86+
87+
/*
88+
* Classify an absolute pathname received from a filesystem event.
89+
*/
90+
enum fsmonitor_path_type fsmonitor_classify_path_absolute(
91+
struct fsmonitor_daemon_state *state,
92+
const char *path);
93+
3394
#endif /* HAVE_FSMONITOR_DAEMON_BACKEND */
3495
#endif /* FSMONITOR_DAEMON_H */

0 commit comments

Comments
 (0)