Mercurial > hg-stable
annotate hgext/inotify/server.py @ 8557:67f76a4463ef
inotify: Removing the unnecessary "inotifyserver" class variable.
author | Nicolas Dumazet <nicdumz.commits@gmail.com> |
---|---|
date | Fri, 08 May 2009 12:19:57 +0900 |
parents | 3e09bc5fee12 |
children | 1f706b1b62f3 |
rev | line source |
---|---|
6239 | 1 # server.py - inotify status server |
2 # | |
3 # Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com> | |
4 # Copyright 2007, 2008 Brendan Cully <brendan@kublai.com> | |
5 # | |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8209
diff
changeset
|
6 # This software may be used and distributed according to the terms of the |
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8209
diff
changeset
|
7 # GNU General Public License version 2, incorporated herein by reference. |
6239 | 8 |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
9 from mercurial.i18n import _ |
7420
b4ac1e2cd38c
inotify: remove unused imports (thanks pyflakes)
Brendan Cully <brendan@kublai.com>
parents:
7351
diff
changeset
|
10 from mercurial import osutil, util |
6239 | 11 import common |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
12 import errno, os, select, socket, stat, struct, sys, tempfile, time |
6239 | 13 |
14 try: | |
6994
bf727bab38b9
Use relative imports in inotify.server.
Brendan Cully <brendan@kublai.com>
parents:
6287
diff
changeset
|
15 import linux as inotify |
bf727bab38b9
Use relative imports in inotify.server.
Brendan Cully <brendan@kublai.com>
parents:
6287
diff
changeset
|
16 from linux import watcher |
6239 | 17 except ImportError: |
18 raise | |
19 | |
20 class AlreadyStartedException(Exception): pass | |
21 | |
22 def join(a, b): | |
23 if a: | |
24 if a[-1] == '/': | |
25 return a + b | |
26 return a + '/' + b | |
27 return b | |
28 | |
29 walk_ignored_errors = (errno.ENOENT, errno.ENAMETOOLONG) | |
30 | |
31 def walkrepodirs(repo): | |
32 '''Iterate over all subdirectories of this repo. | |
33 Exclude the .hg directory, any nested repos, and ignored dirs.''' | |
34 rootslash = repo.root + os.sep | |
8322
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
35 |
6239 | 36 def walkit(dirname, top): |
8321
ec985dcfd7da
inotify: inotify.server.walkrepodirs() simplify
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8320
diff
changeset
|
37 fullpath = rootslash + dirname |
6239 | 38 try: |
8321
ec985dcfd7da
inotify: inotify.server.walkrepodirs() simplify
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8320
diff
changeset
|
39 for name, kind in osutil.listdir(fullpath): |
6239 | 40 if kind == stat.S_IFDIR: |
41 if name == '.hg': | |
8323
589a82fb02a2
inotify: inotify.server.walk*() cleanup
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8322
diff
changeset
|
42 if not top: |
589a82fb02a2
inotify: inotify.server.walk*() cleanup
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8322
diff
changeset
|
43 return |
6239 | 44 else: |
45 d = join(dirname, name) | |
46 if repo.dirstate._ignore(d): | |
47 continue | |
8322
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
48 for subdir in walkit(d, False): |
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
49 yield subdir |
6239 | 50 except OSError, err: |
51 if err.errno not in walk_ignored_errors: | |
52 raise | |
8324
b923d599c309
inotify: inotify.server.walk*() remove unnecessary var
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8323
diff
changeset
|
53 yield fullpath |
8322
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
54 |
3c6c21eb3416
inotify: inotify.server.walkrepodirs() simplify walking
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8321
diff
changeset
|
55 return walkit('', True) |
6239 | 56 |
57 def walk(repo, root): | |
58 '''Like os.walk, but only yields regular files.''' | |
59 | |
60 # This function is critical to performance during startup. | |
61 | |
62 rootslash = repo.root + os.sep | |
63 | |
64 def walkit(root, reporoot): | |
65 files, dirs = [], [] | |
66 | |
67 try: | |
68 fullpath = rootslash + root | |
69 for name, kind in osutil.listdir(fullpath): | |
70 if kind == stat.S_IFDIR: | |
71 if name == '.hg': | |
8325
f2559645643a
inotify: inotify.server.walk() simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8324
diff
changeset
|
72 if not reporoot: |
8323
589a82fb02a2
inotify: inotify.server.walk*() cleanup
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8322
diff
changeset
|
73 return |
8325
f2559645643a
inotify: inotify.server.walk() simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8324
diff
changeset
|
74 else: |
f2559645643a
inotify: inotify.server.walk() simplify control flow
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8324
diff
changeset
|
75 dirs.append(name) |
8381
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
76 path = join(root, name) |
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
77 if repo.dirstate._ignore(path): |
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
78 continue |
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
79 for result in walkit(path, False): |
f52fcc864df4
inotify: server.walk(): use yield instead of for
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8336
diff
changeset
|
80 yield result |
6239 | 81 elif kind in (stat.S_IFREG, stat.S_IFLNK): |
8334
0695288e8c37
inotify: inotify.server.walk() filetype is never used, do not yield it
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8325
diff
changeset
|
82 files.append(name) |
8324
b923d599c309
inotify: inotify.server.walk*() remove unnecessary var
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8323
diff
changeset
|
83 yield fullpath, dirs, files |
6239 | 84 |
85 except OSError, err: | |
86 if err.errno not in walk_ignored_errors: | |
87 raise | |
8320
a1305c1c8d8e
inotify: inotify.server.walk() simplify algorithm
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8319
diff
changeset
|
88 |
a1305c1c8d8e
inotify: inotify.server.walk() simplify algorithm
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8319
diff
changeset
|
89 return walkit(root, root == '') |
6239 | 90 |
91 def _explain_watch_limit(ui, repo, count): | |
92 path = '/proc/sys/fs/inotify/max_user_watches' | |
93 try: | |
94 limit = int(file(path).read()) | |
95 except IOError, err: | |
96 if err.errno != errno.ENOENT: | |
97 raise | |
98 raise util.Abort(_('this system does not seem to ' | |
99 'support inotify')) | |
100 ui.warn(_('*** the current per-user limit on the number ' | |
101 'of inotify watches is %s\n') % limit) | |
102 ui.warn(_('*** this limit is too low to watch every ' | |
103 'directory in this repository\n')) | |
104 ui.warn(_('*** counting directories: ')) | |
105 ndirs = len(list(walkrepodirs(repo))) | |
106 ui.warn(_('found %d\n') % ndirs) | |
107 newlimit = min(limit, 1024) | |
108 while newlimit < ((limit + ndirs) * 1.1): | |
109 newlimit *= 2 | |
110 ui.warn(_('*** to raise the limit from %d to %d (run as root):\n') % | |
111 (limit, newlimit)) | |
112 ui.warn(_('*** echo %d > %s\n') % (newlimit, path)) | |
113 raise util.Abort(_('cannot watch %s until inotify watch limit is raised') | |
114 % repo.root) | |
115 | |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
116 class repowatcher(object): |
6239 | 117 poll_events = select.POLLIN |
118 statuskeys = 'almr!?' | |
8383
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
119 mask = ( |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
120 inotify.IN_ATTRIB | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
121 inotify.IN_CREATE | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
122 inotify.IN_DELETE | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
123 inotify.IN_DELETE_SELF | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
124 inotify.IN_MODIFY | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
125 inotify.IN_MOVED_FROM | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
126 inotify.IN_MOVED_TO | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
127 inotify.IN_MOVE_SELF | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
128 inotify.IN_ONLYDIR | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
129 inotify.IN_UNMOUNT | |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
130 0) |
6239 | 131 |
132 def __init__(self, ui, repo, master): | |
133 self.ui = ui | |
134 self.repo = repo | |
135 self.wprefix = self.repo.wjoin('') | |
136 self.timeout = None | |
137 self.master = master | |
138 try: | |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
139 self.watcher = watcher.watcher() |
6239 | 140 except OSError, err: |
141 raise util.Abort(_('inotify service not available: %s') % | |
142 err.strerror) | |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
143 self.threshold = watcher.threshold(self.watcher) |
6239 | 144 self.registered = True |
145 self.fileno = self.watcher.fileno | |
146 | |
147 self.tree = {} | |
148 self.statcache = {} | |
149 self.statustrees = dict([(s, {}) for s in self.statuskeys]) | |
150 | |
151 self.watches = 0 | |
152 self.last_event = None | |
153 | |
154 self.eventq = {} | |
155 self.deferred = 0 | |
156 | |
157 self.ds_info = self.dirstate_info() | |
158 self.scan() | |
159 | |
160 def event_time(self): | |
161 last = self.last_event | |
162 now = time.time() | |
163 self.last_event = now | |
164 | |
165 if last is None: | |
166 return 'start' | |
167 delta = now - last | |
168 if delta < 5: | |
169 return '+%.3f' % delta | |
170 if delta < 50: | |
171 return '+%.2f' % delta | |
172 return '+%.1f' % delta | |
173 | |
174 def dirstate_info(self): | |
175 try: | |
176 st = os.lstat(self.repo.join('dirstate')) | |
177 return st.st_mtime, st.st_ino | |
178 except OSError, err: | |
179 if err.errno != errno.ENOENT: | |
180 raise | |
181 return 0, 0 | |
182 | |
183 def add_watch(self, path, mask): | |
184 if not path: | |
185 return | |
186 if self.watcher.path(path) is None: | |
187 if self.ui.debugflag: | |
188 self.ui.note(_('watching %r\n') % path[len(self.wprefix):]) | |
189 try: | |
190 self.watcher.add(path, mask) | |
191 self.watches += 1 | |
192 except OSError, err: | |
193 if err.errno in (errno.ENOENT, errno.ENOTDIR): | |
194 return | |
195 if err.errno != errno.ENOSPC: | |
196 raise | |
197 _explain_watch_limit(self.ui, self.repo, self.watches) | |
198 | |
199 def setup(self): | |
200 self.ui.note(_('watching directories under %r\n') % self.repo.root) | |
201 self.add_watch(self.repo.path, inotify.IN_DELETE) | |
202 self.check_dirstate() | |
203 | |
204 def wpath(self, evt): | |
205 path = evt.fullpath | |
206 if path == self.repo.root: | |
207 return '' | |
208 if path.startswith(self.wprefix): | |
209 return path[len(self.wprefix):] | |
210 raise 'wtf? ' + path | |
211 | |
212 def dir(self, tree, path): | |
213 if path: | |
214 for name in path.split('/'): | |
8384
483d9b2103da
inotify: dic.setdefault(k, d) ; v = dic[k] --> v = dic.setdefault(k, d)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8383
diff
changeset
|
215 tree = tree.setdefault(name, {}) |
6239 | 216 return tree |
217 | |
218 def lookup(self, path, tree): | |
219 if path: | |
220 try: | |
221 for name in path.split('/'): | |
222 tree = tree[name] | |
223 except KeyError: | |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
224 return 'x' |
6239 | 225 except TypeError: |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
226 return 'd' |
6239 | 227 return tree |
228 | |
229 def split(self, path): | |
230 c = path.rfind('/') | |
231 if c == -1: | |
232 return '', path | |
233 return path[:c], path[c+1:] | |
234 | |
235 def filestatus(self, fn, st): | |
236 try: | |
237 type_, mode, size, time = self.repo.dirstate._map[fn][:4] | |
238 except KeyError: | |
239 type_ = '?' | |
240 if type_ == 'n': | |
241 st_mode, st_size, st_mtime = st | |
7082
be81b4788115
inotify: fix confusion on files in lookup state
Matt Mackall <mpm@selenic.com>
parents:
6998
diff
changeset
|
242 if size == -1: |
be81b4788115
inotify: fix confusion on files in lookup state
Matt Mackall <mpm@selenic.com>
parents:
6998
diff
changeset
|
243 return 'l' |
6239 | 244 if size and (size != st_size or (mode ^ st_mode) & 0100): |
245 return 'm' | |
246 if time != int(st_mtime): | |
247 return 'l' | |
248 return 'n' | |
249 if type_ == '?' and self.repo.dirstate._ignore(fn): | |
250 return 'i' | |
251 return type_ | |
252 | |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
253 def updatestatus(self, wfn, osstat=None, newstatus=None): |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
254 ''' |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
255 Update the stored status of a file or directory. |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
256 |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
257 osstat: (mode, size, time) tuple, as returned by os.lstat(wfn) |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
258 |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
259 newstatus: char in statuskeys, new status to apply. |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
260 ''' |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
261 if osstat: |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
262 newstatus = self.filestatus(wfn, osstat) |
6239 | 263 else: |
264 self.statcache.pop(wfn, None) | |
265 root, fn = self.split(wfn) | |
266 d = self.dir(self.tree, root) | |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
267 oldstatus = d.get(fn) |
6239 | 268 isdir = False |
269 if oldstatus: | |
270 try: | |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
271 if not newstatus: |
6239 | 272 if oldstatus in 'almn': |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
273 newstatus = '!' |
6239 | 274 elif oldstatus == 'r': |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
275 newstatus = 'r' |
6239 | 276 except TypeError: |
277 # oldstatus may be a dict left behind by a deleted | |
278 # directory | |
279 isdir = True | |
280 else: | |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
281 if oldstatus in self.statuskeys and oldstatus != newstatus: |
6239 | 282 del self.dir(self.statustrees[oldstatus], root)[fn] |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
283 if self.ui.debugflag and oldstatus != newstatus: |
6239 | 284 if isdir: |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
285 self.ui.note(_('status: %r dir(%d) -> %s\n') % |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
286 (wfn, len(oldstatus), newstatus)) |
6239 | 287 else: |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
288 self.ui.note(_('status: %r %s -> %s\n') % |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
289 (wfn, oldstatus, newstatus)) |
6239 | 290 if not isdir: |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
291 if newstatus and newstatus != 'i': |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
292 d[fn] = newstatus |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
293 if newstatus in self.statuskeys: |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
294 dd = self.dir(self.statustrees[newstatus], root) |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
295 if oldstatus != newstatus or fn not in dd: |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
296 dd[fn] = newstatus |
6239 | 297 else: |
298 d.pop(fn, None) | |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
299 elif not newstatus: |
7892
67e59a9886d5
Fixing issue1542, adding a relevant test
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
7451
diff
changeset
|
300 # a directory is being removed, check its contents |
67e59a9886d5
Fixing issue1542, adding a relevant test
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
7451
diff
changeset
|
301 for subfile, b in oldstatus.copy().iteritems(): |
67e59a9886d5
Fixing issue1542, adding a relevant test
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
7451
diff
changeset
|
302 self.updatestatus(wfn + '/' + subfile, None) |
67e59a9886d5
Fixing issue1542, adding a relevant test
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
7451
diff
changeset
|
303 |
6239 | 304 |
305 def check_deleted(self, key): | |
306 # Files that had been deleted but were present in the dirstate | |
307 # may have vanished from the dirstate; we must clean them up. | |
308 nuke = [] | |
309 for wfn, ignore in self.walk(key, self.statustrees[key]): | |
310 if wfn not in self.repo.dirstate: | |
311 nuke.append(wfn) | |
312 for wfn in nuke: | |
313 root, fn = self.split(wfn) | |
314 del self.dir(self.statustrees[key], root)[fn] | |
315 del self.dir(self.tree, root)[fn] | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
316 |
6239 | 317 def scan(self, topdir=''): |
318 self.handle_timeout() | |
319 ds = self.repo.dirstate._map.copy() | |
320 self.add_watch(join(self.repo.root, topdir), self.mask) | |
8334
0695288e8c37
inotify: inotify.server.walk() filetype is never used, do not yield it
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8325
diff
changeset
|
321 for root, dirs, files in walk(self.repo, topdir): |
6239 | 322 for d in dirs: |
323 self.add_watch(join(root, d), self.mask) | |
324 wroot = root[len(self.wprefix):] | |
325 d = self.dir(self.tree, wroot) | |
8334
0695288e8c37
inotify: inotify.server.walk() filetype is never used, do not yield it
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8325
diff
changeset
|
326 for fn in files: |
6239 | 327 wfn = join(wroot, fn) |
328 self.updatestatus(wfn, self.getstat(wfn)) | |
329 ds.pop(wfn, None) | |
330 wtopdir = topdir | |
331 if wtopdir and wtopdir[-1] != '/': | |
332 wtopdir += '/' | |
333 for wfn, state in ds.iteritems(): | |
334 if not wfn.startswith(wtopdir): | |
335 continue | |
7302
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
336 try: |
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
337 st = self.stat(wfn) |
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
338 except OSError: |
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
339 status = state[0] |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
340 self.updatestatus(wfn, None, newstatus=status) |
6239 | 341 else: |
7086
4033195d455b
inotify: avoid status getting out of sync
Matt Mackall <mpm@selenic.com>
parents:
7085
diff
changeset
|
342 self.updatestatus(wfn, st) |
6239 | 343 self.check_deleted('!') |
344 self.check_deleted('r') | |
345 | |
346 def check_dirstate(self): | |
347 ds_info = self.dirstate_info() | |
348 if ds_info == self.ds_info: | |
349 return | |
350 self.ds_info = ds_info | |
351 if not self.ui.debugflag: | |
352 self.last_event = None | |
353 self.ui.note(_('%s dirstate reload\n') % self.event_time()) | |
354 self.repo.dirstate.invalidate() | |
355 self.scan() | |
356 self.ui.note(_('%s end dirstate reload\n') % self.event_time()) | |
357 | |
358 def walk(self, states, tree, prefix=''): | |
359 # This is the "inner loop" when talking to the client. | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
360 |
6239 | 361 for name, val in tree.iteritems(): |
362 path = join(prefix, name) | |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
363 try: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
364 if val in states: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
365 yield path, val |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
366 except TypeError: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
367 for p in self.walk(states, val, path): |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
368 yield p |
6239 | 369 |
370 def update_hgignore(self): | |
371 # An update of the ignore file can potentially change the | |
372 # states of all unknown and ignored files. | |
373 | |
374 # XXX If the user has other ignore files outside the repo, or | |
375 # changes their list of ignore files at run time, we'll | |
376 # potentially never see changes to them. We could get the | |
377 # client to report to us what ignore data they're using. | |
378 # But it's easier to do nothing than to open that can of | |
379 # worms. | |
380 | |
7085
1fcc282e2c43
inotify: fixup rebuilding ignore
Matt Mackall <mpm@selenic.com>
parents:
7082
diff
changeset
|
381 if '_ignore' in self.repo.dirstate.__dict__: |
1fcc282e2c43
inotify: fixup rebuilding ignore
Matt Mackall <mpm@selenic.com>
parents:
7082
diff
changeset
|
382 delattr(self.repo.dirstate, '_ignore') |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
383 self.ui.note(_('rescanning due to .hgignore change\n')) |
6239 | 384 self.scan() |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
385 |
6239 | 386 def getstat(self, wpath): |
387 try: | |
388 return self.statcache[wpath] | |
389 except KeyError: | |
390 try: | |
391 return self.stat(wpath) | |
392 except OSError, err: | |
393 if err.errno != errno.ENOENT: | |
394 raise | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
395 |
6239 | 396 def stat(self, wpath): |
397 try: | |
398 st = os.lstat(join(self.wprefix, wpath)) | |
399 ret = st.st_mode, st.st_size, st.st_mtime | |
400 self.statcache[wpath] = ret | |
401 return ret | |
7280
810ca383da9c
remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7220
diff
changeset
|
402 except OSError: |
6239 | 403 self.statcache.pop(wpath, None) |
404 raise | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
405 |
6239 | 406 def created(self, wpath): |
407 if wpath == '.hgignore': | |
408 self.update_hgignore() | |
409 try: | |
410 st = self.stat(wpath) | |
411 if stat.S_ISREG(st[0]): | |
412 self.updatestatus(wpath, st) | |
7280
810ca383da9c
remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7220
diff
changeset
|
413 except OSError: |
6239 | 414 pass |
415 | |
416 def modified(self, wpath): | |
417 if wpath == '.hgignore': | |
418 self.update_hgignore() | |
419 try: | |
420 st = self.stat(wpath) | |
421 if stat.S_ISREG(st[0]): | |
422 if self.repo.dirstate[wpath] in 'lmn': | |
423 self.updatestatus(wpath, st) | |
424 except OSError: | |
425 pass | |
426 | |
427 def deleted(self, wpath): | |
428 if wpath == '.hgignore': | |
429 self.update_hgignore() | |
430 elif wpath.startswith('.hg/'): | |
431 if wpath == '.hg/wlock': | |
432 self.check_dirstate() | |
433 return | |
434 | |
435 self.updatestatus(wpath, None) | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
436 |
6239 | 437 def schedule_work(self, wpath, evt): |
8384
483d9b2103da
inotify: dic.setdefault(k, d) ; v = dic[k] --> v = dic.setdefault(k, d)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8383
diff
changeset
|
438 prev = self.eventq.setdefault(wpath, []) |
6239 | 439 try: |
440 if prev and evt == 'm' and prev[-1] in 'cm': | |
441 return | |
442 self.eventq[wpath].append(evt) | |
443 finally: | |
444 self.deferred += 1 | |
445 self.timeout = 250 | |
446 | |
447 def deferred_event(self, wpath, evt): | |
448 if evt == 'c': | |
449 self.created(wpath) | |
450 elif evt == 'm': | |
451 self.modified(wpath) | |
452 elif evt == 'd': | |
453 self.deleted(wpath) | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
454 |
6239 | 455 def process_create(self, wpath, evt): |
456 if self.ui.debugflag: | |
457 self.ui.note(_('%s event: created %s\n') % | |
458 (self.event_time(), wpath)) | |
459 | |
460 if evt.mask & inotify.IN_ISDIR: | |
461 self.scan(wpath) | |
462 else: | |
463 self.schedule_work(wpath, 'c') | |
464 | |
465 def process_delete(self, wpath, evt): | |
466 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
467 self.ui.note(_('%s event: deleted %s\n') % |
6239 | 468 (self.event_time(), wpath)) |
469 | |
470 if evt.mask & inotify.IN_ISDIR: | |
471 self.scan(wpath) | |
7892
67e59a9886d5
Fixing issue1542, adding a relevant test
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
7451
diff
changeset
|
472 self.schedule_work(wpath, 'd') |
6239 | 473 |
474 def process_modify(self, wpath, evt): | |
475 if self.ui.debugflag: | |
476 self.ui.note(_('%s event: modified %s\n') % | |
477 (self.event_time(), wpath)) | |
478 | |
479 if not (evt.mask & inotify.IN_ISDIR): | |
480 self.schedule_work(wpath, 'm') | |
481 | |
482 def process_unmount(self, evt): | |
483 self.ui.warn(_('filesystem containing %s was unmounted\n') % | |
484 evt.fullpath) | |
485 sys.exit(0) | |
486 | |
487 def handle_event(self, fd, event): | |
488 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
489 self.ui.note(_('%s readable: %d bytes\n') % |
6239 | 490 (self.event_time(), self.threshold.readable())) |
491 if not self.threshold(): | |
492 if self.registered: | |
493 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
494 self.ui.note(_('%s below threshold - unhooking\n') % |
6239 | 495 (self.event_time())) |
496 self.master.poll.unregister(fd) | |
497 self.registered = False | |
498 self.timeout = 250 | |
499 else: | |
500 self.read_events() | |
501 | |
502 def read_events(self, bufsize=None): | |
503 events = self.watcher.read(bufsize) | |
504 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
505 self.ui.note(_('%s reading %d events\n') % |
6239 | 506 (self.event_time(), len(events))) |
507 for evt in events: | |
508 wpath = self.wpath(evt) | |
509 if evt.mask & inotify.IN_UNMOUNT: | |
510 self.process_unmount(wpath, evt) | |
511 elif evt.mask & (inotify.IN_MODIFY | inotify.IN_ATTRIB): | |
512 self.process_modify(wpath, evt) | |
513 elif evt.mask & (inotify.IN_DELETE | inotify.IN_DELETE_SELF | | |
514 inotify.IN_MOVED_FROM): | |
515 self.process_delete(wpath, evt) | |
516 elif evt.mask & (inotify.IN_CREATE | inotify.IN_MOVED_TO): | |
517 self.process_create(wpath, evt) | |
518 | |
519 def handle_timeout(self): | |
520 if not self.registered: | |
521 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
522 self.ui.note(_('%s hooking back up with %d bytes readable\n') % |
6239 | 523 (self.event_time(), self.threshold.readable())) |
524 self.read_events(0) | |
525 self.master.poll.register(self, select.POLLIN) | |
526 self.registered = True | |
527 | |
528 if self.eventq: | |
529 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
530 self.ui.note(_('%s processing %d deferred events as %d\n') % |
6239 | 531 (self.event_time(), self.deferred, |
532 len(self.eventq))) | |
8209
a1a5a57efe90
replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents:
7892
diff
changeset
|
533 for wpath, evts in sorted(self.eventq.iteritems()): |
6239 | 534 for evt in evts: |
535 self.deferred_event(wpath, evt) | |
536 self.eventq.clear() | |
537 self.deferred = 0 | |
538 self.timeout = None | |
539 | |
540 def shutdown(self): | |
541 self.watcher.close() | |
542 | |
8555
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
543 def debug(self): |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
544 """ |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
545 Returns a sorted list of relatives paths currently watched, |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
546 for debugging purposes. |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
547 """ |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
548 return sorted(tuple[0][len(self.wprefix):] for tuple in self.watcher) |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
549 |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
550 class server(object): |
6239 | 551 poll_events = select.POLLIN |
552 | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
553 def __init__(self, ui, repo, repowatcher, timeout): |
6239 | 554 self.ui = ui |
555 self.repo = repo | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
556 self.repowatcher = repowatcher |
6239 | 557 self.timeout = timeout |
558 self.sock = socket.socket(socket.AF_UNIX) | |
559 self.sockpath = self.repo.join('inotify.sock') | |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
560 self.realsockpath = None |
6239 | 561 try: |
562 self.sock.bind(self.sockpath) | |
563 except socket.error, err: | |
564 if err[0] == errno.EADDRINUSE: | |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
565 raise AlreadyStartedException(_('could not start server: %s') |
6239 | 566 % err[1]) |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
567 if err[0] == "AF_UNIX path too long": |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
568 tempdir = tempfile.mkdtemp(prefix="hg-inotify-") |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
569 self.realsockpath = os.path.join(tempdir, "inotify.sock") |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
570 try: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
571 self.sock.bind(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
572 os.symlink(self.realsockpath, self.sockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
573 except (OSError, socket.error), inst: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
574 try: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
575 os.unlink(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
576 except: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
577 pass |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
578 os.rmdir(tempdir) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
579 if inst.errno == errno.EEXIST: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
580 raise AlreadyStartedException(_('could not start server: %s') |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
581 % inst.strerror) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
582 raise |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
583 else: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
584 raise |
6239 | 585 self.sock.listen(5) |
586 self.fileno = self.sock.fileno | |
587 | |
588 def handle_timeout(self): | |
589 pass | |
590 | |
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
591 def answer_stat_query(self, cs): |
6239 | 592 names = cs.read().split('\0') |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
593 |
6239 | 594 states = names.pop() |
595 | |
596 self.ui.note(_('answering query for %r\n') % states) | |
597 | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
598 if self.repowatcher.timeout: |
6239 | 599 # We got a query while a rescan is pending. Make sure we |
600 # rescan before responding, or we could give back a wrong | |
601 # answer. | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
602 self.repowatcher.handle_timeout() |
6239 | 603 |
604 if not names: | |
605 def genresult(states, tree): | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
606 for fn, state in self.repowatcher.walk(states, tree): |
6239 | 607 yield fn |
608 else: | |
609 def genresult(states, tree): | |
610 for fn in names: | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
611 l = self.repowatcher.lookup(fn, tree) |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
612 try: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
613 if l in states: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
614 yield fn |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
615 except TypeError: |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
616 for f, s in self.repowatcher.walk(states, l, fn): |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
617 yield f |
6239 | 618 |
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
619 return ['\0'.join(r) for r in [ |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
620 genresult('l', self.repowatcher.statustrees['l']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
621 genresult('m', self.repowatcher.statustrees['m']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
622 genresult('a', self.repowatcher.statustrees['a']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
623 genresult('r', self.repowatcher.statustrees['r']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
624 genresult('!', self.repowatcher.statustrees['!']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
625 '?' in states |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
626 and genresult('?', self.repowatcher.statustrees['?']) |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
627 or [], |
6239 | 628 [], |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
629 'c' in states and genresult('n', self.repowatcher.tree) or [], |
6239 | 630 ]] |
631 | |
8555
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
632 def answer_dbug_query(self): |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
633 return ['\0'.join(self.repowatcher.debug())] |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
634 |
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
635 def handle_event(self, fd, event): |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
636 sock, addr = self.sock.accept() |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
637 |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
638 cs = common.recvcs(sock) |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
639 version = ord(cs.read(1)) |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
640 |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
641 if version != common.version: |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
642 self.ui.warn(_('received query from incompatible client ' |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
643 'version %d\n') % version) |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
644 return |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
645 |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
646 type = cs.read(4) |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
647 |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
648 if type == 'STAT': |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
649 results = self.answer_stat_query(cs) |
8555
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
650 elif type == 'DBUG': |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
651 results = self.answer_dbug_query() |
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
652 else: |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
653 self.ui.warn(_('unrecognized query type: %s\n') % type) |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
654 return |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
655 |
6239 | 656 try: |
657 try: | |
8386
4aad982111b6
inotify: Abstract the layer format and sizes to a inotify.common dictionary
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8385
diff
changeset
|
658 v = chr(common.version) |
4aad982111b6
inotify: Abstract the layer format and sizes to a inotify.common dictionary
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8385
diff
changeset
|
659 |
8553
e387ecd7a6ed
inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8386
diff
changeset
|
660 sock.sendall(v + type + struct.pack(common.resphdrfmts[type], |
e387ecd7a6ed
inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8386
diff
changeset
|
661 *map(len, results))) |
6239 | 662 sock.sendall(''.join(results)) |
663 finally: | |
664 sock.shutdown(socket.SHUT_WR) | |
665 except socket.error, err: | |
666 if err[0] != errno.EPIPE: | |
667 raise | |
668 | |
669 def shutdown(self): | |
670 self.sock.close() | |
671 try: | |
672 os.unlink(self.sockpath) | |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
673 if self.realsockpath: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
674 os.unlink(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
675 os.rmdir(os.path.dirname(self.realsockpath)) |
6239 | 676 except OSError, err: |
677 if err.errno != errno.ENOENT: | |
678 raise | |
679 | |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
680 class master(object): |
6239 | 681 def __init__(self, ui, repo, timeout=None): |
682 self.ui = ui | |
683 self.repo = repo | |
684 self.poll = select.poll() | |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
685 self.repowatcher = repowatcher(ui, repo, self) |
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
686 self.server = server(ui, repo, self.repowatcher, timeout) |
6239 | 687 self.table = {} |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
688 for obj in (self.repowatcher, self.server): |
6239 | 689 fd = obj.fileno() |
690 self.table[fd] = obj | |
691 self.poll.register(fd, obj.poll_events) | |
692 | |
693 def register(self, fd, mask): | |
694 self.poll.register(fd, mask) | |
695 | |
696 def shutdown(self): | |
697 for obj in self.table.itervalues(): | |
698 obj.shutdown() | |
699 | |
700 def run(self): | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
701 self.repowatcher.setup() |
6239 | 702 self.ui.note(_('finished setup\n')) |
703 if os.getenv('TIME_STARTUP'): | |
704 sys.exit(0) | |
705 while True: | |
706 timeout = None | |
707 timeobj = None | |
708 for obj in self.table.itervalues(): | |
709 if obj.timeout is not None and (timeout is None or obj.timeout < timeout): | |
710 timeout, timeobj = obj.timeout, obj | |
711 try: | |
712 if self.ui.debugflag: | |
713 if timeout is None: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
714 self.ui.note(_('polling: no timeout\n')) |
6239 | 715 else: |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
716 self.ui.note(_('polling: %sms timeout\n') % timeout) |
6239 | 717 events = self.poll.poll(timeout) |
718 except select.error, err: | |
719 if err[0] == errno.EINTR: | |
720 continue | |
721 raise | |
722 if events: | |
723 for fd, event in events: | |
724 self.table[fd].handle_event(fd, event) | |
725 elif timeobj: | |
726 timeobj.handle_timeout() | |
727 | |
728 def start(ui, repo): | |
7451
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
729 def closefds(ignore): |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
730 # (from python bug #1177468) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
731 # close all inherited file descriptors |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
732 # Python 2.4.1 and later use /dev/urandom to seed the random module's RNG |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
733 # a file descriptor is kept internally as os._urandomfd (created on demand |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
734 # the first time os.urandom() is called), and should not be closed |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
735 try: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
736 os.urandom(4) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
737 urandom_fd = getattr(os, '_urandomfd', None) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
738 except AttributeError: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
739 urandom_fd = None |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
740 ignore.append(urandom_fd) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
741 for fd in range(3, 256): |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
742 if fd in ignore: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
743 continue |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
744 try: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
745 os.close(fd) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
746 except OSError: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
747 pass |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
748 |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
749 m = master(ui, repo) |
6239 | 750 sys.stdout.flush() |
751 sys.stderr.flush() | |
752 | |
753 pid = os.fork() | |
754 if pid: | |
755 return pid | |
756 | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
757 closefds([m.server.fileno(), m.repowatcher.fileno()]) |
6239 | 758 os.setsid() |
759 | |
760 fd = os.open('/dev/null', os.O_RDONLY) | |
761 os.dup2(fd, 0) | |
762 if fd > 0: | |
763 os.close(fd) | |
764 | |
765 fd = os.open(ui.config('inotify', 'log', '/dev/null'), | |
766 os.O_RDWR | os.O_CREAT | os.O_TRUNC) | |
767 os.dup2(fd, 1) | |
768 os.dup2(fd, 2) | |
769 if fd > 2: | |
770 os.close(fd) | |
771 | |
772 try: | |
773 m.run() | |
774 finally: | |
775 m.shutdown() | |
776 os._exit(0) |