Mercurial > hg-stable
annotate hgext/inotify/server.py @ 8786:55af9be4efac
inotify: remove unused variables
author | Benoit Boissinot <benoit.boissinot@ens-lyon.org> |
---|---|
date | Sat, 13 Jun 2009 17:23:08 +0200 |
parents | 8ef1f63e554c |
children | 9aca76502280 |
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 |
8786
55af9be4efac
inotify: remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8610
diff
changeset
|
91 def _explain_watch_limit(ui, repo): |
6239 | 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 | |
8610
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
116 class pollable(object): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
117 """ |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
118 Interface to support polling. |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
119 The file descriptor returned by fileno() is registered to a polling |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
120 object. |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
121 Usage: |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
122 Every tick, check if an event has happened since the last tick: |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
123 * If yes, call handle_events |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
124 * If no, call handle_timeout |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
125 """ |
6239 | 126 poll_events = select.POLLIN |
8610
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
127 def fileno(self): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
128 raise NotImplementedError |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
129 |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
130 def handle_events(self, events): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
131 raise NotImplementedError |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
132 |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
133 def handle_timeout(self): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
134 raise NotImplementedError |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
135 |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
136 def shutdown(self): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
137 raise NotImplementedError |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
138 |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
139 class repowatcher(pollable): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
140 """ |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
141 Watches inotify events |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
142 """ |
6239 | 143 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
|
144 mask = ( |
dcfdcb51ac5c
inotify: make mask a class variable since it's instance-independant
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8382
diff
changeset
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 0) |
6239 | 156 |
157 def __init__(self, ui, repo, master): | |
158 self.ui = ui | |
159 self.repo = repo | |
160 self.wprefix = self.repo.wjoin('') | |
161 self.timeout = None | |
162 self.master = master | |
163 try: | |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
164 self.watcher = watcher.watcher() |
6239 | 165 except OSError, err: |
166 raise util.Abort(_('inotify service not available: %s') % | |
167 err.strerror) | |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
168 self.threshold = watcher.threshold(self.watcher) |
6239 | 169 self.registered = True |
170 self.fileno = self.watcher.fileno | |
171 | |
172 self.tree = {} | |
173 self.statcache = {} | |
174 self.statustrees = dict([(s, {}) for s in self.statuskeys]) | |
175 | |
176 self.watches = 0 | |
177 self.last_event = None | |
178 | |
8605
ed2d9bdbfad2
inotify: do not defer inotify events processing
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8604
diff
changeset
|
179 self.lastevent = {} |
6239 | 180 |
181 self.ds_info = self.dirstate_info() | |
8604
578f2a0049cd
inotify: do not recurse in handle_timeout(): call it explicitely, not in scan()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8600
diff
changeset
|
182 self.handle_timeout() |
6239 | 183 self.scan() |
184 | |
185 def event_time(self): | |
186 last = self.last_event | |
187 now = time.time() | |
188 self.last_event = now | |
189 | |
190 if last is None: | |
191 return 'start' | |
192 delta = now - last | |
193 if delta < 5: | |
194 return '+%.3f' % delta | |
195 if delta < 50: | |
196 return '+%.2f' % delta | |
197 return '+%.1f' % delta | |
198 | |
199 def dirstate_info(self): | |
200 try: | |
201 st = os.lstat(self.repo.join('dirstate')) | |
202 return st.st_mtime, st.st_ino | |
203 except OSError, err: | |
204 if err.errno != errno.ENOENT: | |
205 raise | |
206 return 0, 0 | |
207 | |
208 def add_watch(self, path, mask): | |
209 if not path: | |
210 return | |
211 if self.watcher.path(path) is None: | |
212 if self.ui.debugflag: | |
213 self.ui.note(_('watching %r\n') % path[len(self.wprefix):]) | |
214 try: | |
215 self.watcher.add(path, mask) | |
216 self.watches += 1 | |
217 except OSError, err: | |
218 if err.errno in (errno.ENOENT, errno.ENOTDIR): | |
219 return | |
220 if err.errno != errno.ENOSPC: | |
221 raise | |
8786
55af9be4efac
inotify: remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8610
diff
changeset
|
222 _explain_watch_limit(self.ui, self.repo) |
6239 | 223 |
224 def setup(self): | |
225 self.ui.note(_('watching directories under %r\n') % self.repo.root) | |
226 self.add_watch(self.repo.path, inotify.IN_DELETE) | |
227 self.check_dirstate() | |
228 | |
229 def wpath(self, evt): | |
230 path = evt.fullpath | |
231 if path == self.repo.root: | |
232 return '' | |
233 if path.startswith(self.wprefix): | |
234 return path[len(self.wprefix):] | |
235 raise 'wtf? ' + path | |
236 | |
237 def dir(self, tree, path): | |
238 if path: | |
239 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
|
240 tree = tree.setdefault(name, {}) |
6239 | 241 return tree |
242 | |
243 def lookup(self, path, tree): | |
244 if path: | |
245 try: | |
246 for name in path.split('/'): | |
247 tree = tree[name] | |
248 except KeyError: | |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
249 return 'x' |
6239 | 250 except TypeError: |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
251 return 'd' |
6239 | 252 return tree |
253 | |
254 def split(self, path): | |
255 c = path.rfind('/') | |
256 if c == -1: | |
257 return '', path | |
258 return path[:c], path[c+1:] | |
259 | |
260 def filestatus(self, fn, st): | |
261 try: | |
262 type_, mode, size, time = self.repo.dirstate._map[fn][:4] | |
263 except KeyError: | |
264 type_ = '?' | |
265 if type_ == 'n': | |
266 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
|
267 if size == -1: |
be81b4788115
inotify: fix confusion on files in lookup state
Matt Mackall <mpm@selenic.com>
parents:
6998
diff
changeset
|
268 return 'l' |
6239 | 269 if size and (size != st_size or (mode ^ st_mode) & 0100): |
270 return 'm' | |
271 if time != int(st_mtime): | |
272 return 'l' | |
273 return 'n' | |
274 if type_ == '?' and self.repo.dirstate._ignore(fn): | |
275 return 'i' | |
276 return type_ | |
277 | |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
278 def updatefile(self, wfn, osstat): |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
279 ''' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
280 update the file entry of an existing file. |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
281 |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
282 osstat: (mode, size, time) tuple, as returned by os.lstat(wfn) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
283 ''' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
284 |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
285 self._updatestatus(wfn, self.filestatus(wfn, osstat)) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
286 |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
287 def deletefile(self, wfn, oldstatus): |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
288 ''' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
289 update the entry of a file which has been deleted. |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
290 |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
291 oldstatus: char in statuskeys, status of the file before deletion |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
292 ''' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
293 if oldstatus == 'r': |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
294 newstatus = 'r' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
295 elif oldstatus in 'almn': |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
296 newstatus = '!' |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
297 else: |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
298 newstatus = None |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
299 |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
300 self.statcache.pop(wfn, None) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
301 self._updatestatus(wfn, newstatus) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
302 |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
303 def _updatestatus(self, wfn, newstatus): |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
304 ''' |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
305 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
|
306 |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
307 newstatus: - char in (statuskeys + 'ni'), new status to apply. |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
308 - or None, to stop tracking wfn |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
309 ''' |
6239 | 310 root, fn = self.split(wfn) |
311 d = self.dir(self.tree, root) | |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
312 |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
313 oldstatus = d.get(fn) |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
314 # oldstatus can be either: |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
315 # - None : fn is new |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
316 # - a char in statuskeys: fn is a (tracked) file |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
317 # - a dict: fn is a directory |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
318 isdir = isinstance(oldstatus, dict) |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
319 |
8382
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
320 if self.ui.debugflag and oldstatus != newstatus: |
6239 | 321 if isdir: |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
322 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
|
323 (wfn, len(oldstatus), newstatus)) |
6239 | 324 else: |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
325 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
|
326 (wfn, oldstatus, newstatus)) |
6239 | 327 if not isdir: |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
328 if oldstatus and oldstatus in self.statuskeys \ |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
329 and oldstatus != newstatus: |
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
330 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
|
331 if newstatus and newstatus != 'i': |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
332 d[fn] = newstatus |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
333 if newstatus in self.statuskeys: |
6f44b1adc948
inotify: RepoWatcher.updatestatus: document & use meaningful parameter names
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8381
diff
changeset
|
334 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
|
335 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
|
336 dd[fn] = newstatus |
6239 | 337 else: |
338 d.pop(fn, None) | |
7892
67e59a9886d5
Fixing issue1542, adding a relevant test
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
7451
diff
changeset
|
339 |
6239 | 340 |
341 def check_deleted(self, key): | |
342 # Files that had been deleted but were present in the dirstate | |
343 # may have vanished from the dirstate; we must clean them up. | |
344 nuke = [] | |
345 for wfn, ignore in self.walk(key, self.statustrees[key]): | |
346 if wfn not in self.repo.dirstate: | |
347 nuke.append(wfn) | |
348 for wfn in nuke: | |
349 root, fn = self.split(wfn) | |
350 del self.dir(self.statustrees[key], root)[fn] | |
351 del self.dir(self.tree, root)[fn] | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
352 |
6239 | 353 def scan(self, topdir=''): |
354 ds = self.repo.dirstate._map.copy() | |
355 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
|
356 for root, dirs, files in walk(self.repo, topdir): |
6239 | 357 for d in dirs: |
358 self.add_watch(join(root, d), self.mask) | |
359 wroot = root[len(self.wprefix):] | |
8334
0695288e8c37
inotify: inotify.server.walk() filetype is never used, do not yield it
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8325
diff
changeset
|
360 for fn in files: |
6239 | 361 wfn = join(wroot, fn) |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
362 self.updatefile(wfn, self.getstat(wfn)) |
6239 | 363 ds.pop(wfn, None) |
364 wtopdir = topdir | |
365 if wtopdir and wtopdir[-1] != '/': | |
366 wtopdir += '/' | |
367 for wfn, state in ds.iteritems(): | |
368 if not wfn.startswith(wtopdir): | |
369 continue | |
7302
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
370 try: |
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
371 st = self.stat(wfn) |
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
372 except OSError: |
972737252d05
inotify: server raising an error when removing a file (issue1371)
Gerard Korsten <soonkia77@gmail.com>
parents:
7280
diff
changeset
|
373 status = state[0] |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
374 self.deletefile(wfn, status) |
6239 | 375 else: |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
376 self.updatefile(wfn, st) |
6239 | 377 self.check_deleted('!') |
378 self.check_deleted('r') | |
379 | |
380 def check_dirstate(self): | |
381 ds_info = self.dirstate_info() | |
382 if ds_info == self.ds_info: | |
383 return | |
384 self.ds_info = ds_info | |
385 if not self.ui.debugflag: | |
386 self.last_event = None | |
387 self.ui.note(_('%s dirstate reload\n') % self.event_time()) | |
388 self.repo.dirstate.invalidate() | |
8604
578f2a0049cd
inotify: do not recurse in handle_timeout(): call it explicitely, not in scan()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8600
diff
changeset
|
389 self.handle_timeout() |
6239 | 390 self.scan() |
391 self.ui.note(_('%s end dirstate reload\n') % self.event_time()) | |
392 | |
393 def walk(self, states, tree, prefix=''): | |
394 # This is the "inner loop" when talking to the client. | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
395 |
6239 | 396 for name, val in tree.iteritems(): |
397 path = join(prefix, name) | |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
398 try: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
399 if val in states: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
400 yield path, val |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
401 except TypeError: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
402 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
|
403 yield p |
6239 | 404 |
405 def update_hgignore(self): | |
406 # An update of the ignore file can potentially change the | |
407 # states of all unknown and ignored files. | |
408 | |
409 # XXX If the user has other ignore files outside the repo, or | |
410 # changes their list of ignore files at run time, we'll | |
411 # potentially never see changes to them. We could get the | |
412 # client to report to us what ignore data they're using. | |
413 # But it's easier to do nothing than to open that can of | |
414 # worms. | |
415 | |
7085
1fcc282e2c43
inotify: fixup rebuilding ignore
Matt Mackall <mpm@selenic.com>
parents:
7082
diff
changeset
|
416 if '_ignore' in self.repo.dirstate.__dict__: |
1fcc282e2c43
inotify: fixup rebuilding ignore
Matt Mackall <mpm@selenic.com>
parents:
7082
diff
changeset
|
417 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
|
418 self.ui.note(_('rescanning due to .hgignore change\n')) |
8604
578f2a0049cd
inotify: do not recurse in handle_timeout(): call it explicitely, not in scan()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8600
diff
changeset
|
419 self.handle_timeout() |
6239 | 420 self.scan() |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
421 |
6239 | 422 def getstat(self, wpath): |
423 try: | |
424 return self.statcache[wpath] | |
425 except KeyError: | |
426 try: | |
427 return self.stat(wpath) | |
428 except OSError, err: | |
429 if err.errno != errno.ENOENT: | |
430 raise | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
431 |
6239 | 432 def stat(self, wpath): |
433 try: | |
434 st = os.lstat(join(self.wprefix, wpath)) | |
435 ret = st.st_mode, st.st_size, st.st_mtime | |
436 self.statcache[wpath] = ret | |
437 return ret | |
7280
810ca383da9c
remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7220
diff
changeset
|
438 except OSError: |
6239 | 439 self.statcache.pop(wpath, None) |
440 raise | |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
441 |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
442 def eventaction(code): |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
443 def decorator(f): |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
444 def wrapper(self, wpath): |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
445 if code == 'm' and wpath in self.lastevent and \ |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
446 self.lastevent[wpath] in 'cm': |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
447 return |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
448 self.lastevent[wpath] = code |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
449 self.timeout = 250 |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
450 |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
451 f(self, wpath) |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
452 |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
453 wrapper.func_name = f.func_name |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
454 return wrapper |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
455 return decorator |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
456 |
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
457 @eventaction('c') |
6239 | 458 def created(self, wpath): |
459 if wpath == '.hgignore': | |
460 self.update_hgignore() | |
461 try: | |
462 st = self.stat(wpath) | |
463 if stat.S_ISREG(st[0]): | |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
464 self.updatefile(wpath, st) |
7280
810ca383da9c
remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7220
diff
changeset
|
465 except OSError: |
6239 | 466 pass |
467 | |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
468 @eventaction('m') |
6239 | 469 def modified(self, wpath): |
470 if wpath == '.hgignore': | |
471 self.update_hgignore() | |
472 try: | |
473 st = self.stat(wpath) | |
474 if stat.S_ISREG(st[0]): | |
475 if self.repo.dirstate[wpath] in 'lmn': | |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
476 self.updatefile(wpath, st) |
6239 | 477 except OSError: |
478 pass | |
479 | |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
480 @eventaction('d') |
6239 | 481 def deleted(self, wpath): |
482 if wpath == '.hgignore': | |
483 self.update_hgignore() | |
484 elif wpath.startswith('.hg/'): | |
485 if wpath == '.hg/wlock': | |
486 self.check_dirstate() | |
487 return | |
488 | |
8599
1f706b1b62f3
inotify: server: refactor updatestatus()
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8557
diff
changeset
|
489 self.deletefile(wpath, self.repo.dirstate[wpath]) |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
490 |
6239 | 491 def process_create(self, wpath, evt): |
492 if self.ui.debugflag: | |
493 self.ui.note(_('%s event: created %s\n') % | |
494 (self.event_time(), wpath)) | |
495 | |
496 if evt.mask & inotify.IN_ISDIR: | |
497 self.scan(wpath) | |
498 else: | |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
499 self.created(wpath) |
6239 | 500 |
501 def process_delete(self, wpath, evt): | |
502 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
503 self.ui.note(_('%s event: deleted %s\n') % |
6239 | 504 (self.event_time(), wpath)) |
505 | |
506 if evt.mask & inotify.IN_ISDIR: | |
8600
d46cdfcecaf1
inotify: proper fix for issue1542 (partially reverting 67e59a9886d5)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8599
diff
changeset
|
507 tree = self.dir(self.tree, wpath).copy() |
d46cdfcecaf1
inotify: proper fix for issue1542 (partially reverting 67e59a9886d5)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8599
diff
changeset
|
508 for wfn, ignore in self.walk('?', tree): |
d46cdfcecaf1
inotify: proper fix for issue1542 (partially reverting 67e59a9886d5)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8599
diff
changeset
|
509 self.deletefile(join(wpath, wfn), '?') |
6239 | 510 self.scan(wpath) |
8600
d46cdfcecaf1
inotify: proper fix for issue1542 (partially reverting 67e59a9886d5)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8599
diff
changeset
|
511 else: |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
512 self.deleted(wpath) |
6239 | 513 |
514 def process_modify(self, wpath, evt): | |
515 if self.ui.debugflag: | |
516 self.ui.note(_('%s event: modified %s\n') % | |
517 (self.event_time(), wpath)) | |
518 | |
519 if not (evt.mask & inotify.IN_ISDIR): | |
8606
1c5752dabf76
inotify: use a decorator instead of dispatching calls
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8605
diff
changeset
|
520 self.modified(wpath) |
6239 | 521 |
522 def process_unmount(self, evt): | |
523 self.ui.warn(_('filesystem containing %s was unmounted\n') % | |
524 evt.fullpath) | |
525 sys.exit(0) | |
526 | |
8609
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
527 def handle_pollevents(self, events): |
6239 | 528 if self.ui.debugflag: |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
529 self.ui.note(_('%s readable: %d bytes\n') % |
6239 | 530 (self.event_time(), self.threshold.readable())) |
531 if not self.threshold(): | |
532 if self.registered: | |
533 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
534 self.ui.note(_('%s below threshold - unhooking\n') % |
6239 | 535 (self.event_time())) |
8607
e17eba98c789
inotify: handle_event: do not use event and fd parameters.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8606
diff
changeset
|
536 self.master.poll.unregister(self.fileno()) |
6239 | 537 self.registered = False |
538 self.timeout = 250 | |
539 else: | |
540 self.read_events() | |
541 | |
542 def read_events(self, bufsize=None): | |
543 events = self.watcher.read(bufsize) | |
544 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
545 self.ui.note(_('%s reading %d events\n') % |
6239 | 546 (self.event_time(), len(events))) |
547 for evt in events: | |
548 wpath = self.wpath(evt) | |
549 if evt.mask & inotify.IN_UNMOUNT: | |
550 self.process_unmount(wpath, evt) | |
551 elif evt.mask & (inotify.IN_MODIFY | inotify.IN_ATTRIB): | |
552 self.process_modify(wpath, evt) | |
553 elif evt.mask & (inotify.IN_DELETE | inotify.IN_DELETE_SELF | | |
554 inotify.IN_MOVED_FROM): | |
555 self.process_delete(wpath, evt) | |
556 elif evt.mask & (inotify.IN_CREATE | inotify.IN_MOVED_TO): | |
557 self.process_create(wpath, evt) | |
558 | |
8605
ed2d9bdbfad2
inotify: do not defer inotify events processing
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8604
diff
changeset
|
559 self.lastevent.clear() |
ed2d9bdbfad2
inotify: do not defer inotify events processing
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8604
diff
changeset
|
560 |
6239 | 561 def handle_timeout(self): |
562 if not self.registered: | |
563 if self.ui.debugflag: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
564 self.ui.note(_('%s hooking back up with %d bytes readable\n') % |
6239 | 565 (self.event_time(), self.threshold.readable())) |
566 self.read_events(0) | |
8610
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
567 self.master.poll.register(self, self.poll_events) |
6239 | 568 self.registered = True |
569 | |
570 self.timeout = None | |
571 | |
572 def shutdown(self): | |
573 self.watcher.close() | |
574 | |
8555
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
575 def debug(self): |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
576 """ |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
577 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
|
578 for debugging purposes. |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
579 """ |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
580 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
|
581 |
8610
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
582 class server(pollable): |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
583 """ |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
584 Listens for client queries on unix socket inotify.sock |
8ef1f63e554c
inotify: server: use a common 'pollable' interface for server & repowatcher
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8609
diff
changeset
|
585 """ |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
586 def __init__(self, ui, repo, repowatcher, timeout): |
6239 | 587 self.ui = ui |
588 self.repo = repo | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
589 self.repowatcher = repowatcher |
6239 | 590 self.timeout = timeout |
591 self.sock = socket.socket(socket.AF_UNIX) | |
592 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
|
593 self.realsockpath = None |
6239 | 594 try: |
595 self.sock.bind(self.sockpath) | |
596 except socket.error, err: | |
597 if err[0] == errno.EADDRINUSE: | |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
598 raise AlreadyStartedException(_('could not start server: %s') |
6239 | 599 % err[1]) |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
600 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
|
601 tempdir = tempfile.mkdtemp(prefix="hg-inotify-") |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
602 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
|
603 try: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
604 self.sock.bind(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
605 os.symlink(self.realsockpath, self.sockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
606 except (OSError, socket.error), inst: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
607 try: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
608 os.unlink(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
609 except: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
610 pass |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
611 os.rmdir(tempdir) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
612 if inst.errno == errno.EEXIST: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
613 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
|
614 % inst.strerror) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
615 raise |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
616 else: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
617 raise |
6239 | 618 self.sock.listen(5) |
619 self.fileno = self.sock.fileno | |
620 | |
621 def handle_timeout(self): | |
622 pass | |
623 | |
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
624 def answer_stat_query(self, cs): |
6239 | 625 names = cs.read().split('\0') |
6287
c86207d41512
Spacing cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6239
diff
changeset
|
626 |
6239 | 627 states = names.pop() |
628 | |
629 self.ui.note(_('answering query for %r\n') % states) | |
630 | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
631 if self.repowatcher.timeout: |
6239 | 632 # We got a query while a rescan is pending. Make sure we |
633 # rescan before responding, or we could give back a wrong | |
634 # answer. | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
635 self.repowatcher.handle_timeout() |
6239 | 636 |
637 if not names: | |
638 def genresult(states, tree): | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
639 for fn, state in self.repowatcher.walk(states, tree): |
6239 | 640 yield fn |
641 else: | |
642 def genresult(states, tree): | |
643 for fn in names: | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
644 l = self.repowatcher.lookup(fn, tree) |
7351
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
645 try: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
646 if l in states: |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
647 yield fn |
5ab0abf27dd9
Backed out changeset c5dbe86b0fee (issue1375)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7350
diff
changeset
|
648 except TypeError: |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
649 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
|
650 yield f |
6239 | 651 |
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 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
|
653 genresult('l', self.repowatcher.statustrees['l']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
654 genresult('m', self.repowatcher.statustrees['m']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
655 genresult('a', self.repowatcher.statustrees['a']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
656 genresult('r', self.repowatcher.statustrees['r']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
657 genresult('!', self.repowatcher.statustrees['!']), |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
658 '?' in states |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
659 and genresult('?', self.repowatcher.statustrees['?']) |
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
660 or [], |
6239 | 661 [], |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
662 'c' in states and genresult('n', self.repowatcher.tree) or [], |
6239 | 663 ]] |
664 | |
8555
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
665 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
|
666 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
|
667 |
8609
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
668 def handle_pollevents(self, events): |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
669 for e in events: |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
670 self.handle_pollevent() |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
671 |
8608
228db070bfc4
inotify: rename handle_event to handle_pollevent to avoid confusion
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8607
diff
changeset
|
672 def handle_pollevent(self): |
8554
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
673 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
|
674 |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
675 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
|
676 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
|
677 |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
678 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
|
679 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
|
680 '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
|
681 return |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
682 |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
683 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
|
684 |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
685 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
|
686 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
|
687 elif type == 'DBUG': |
3e09bc5fee12
inotify: introduce debuginotify, which lists which paths are under watch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8554
diff
changeset
|
688 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
|
689 else: |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
690 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
|
691 return |
47d7347484f5
inotify: put STAT-specific query answer generation part in its own method
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8553
diff
changeset
|
692 |
6239 | 693 try: |
694 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
|
695 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
|
696 |
8553
e387ecd7a6ed
inotify: change protocol so that different query types can be supported.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8386
diff
changeset
|
697 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
|
698 *map(len, results))) |
6239 | 699 sock.sendall(''.join(results)) |
700 finally: | |
701 sock.shutdown(socket.SHUT_WR) | |
702 except socket.error, err: | |
703 if err[0] != errno.EPIPE: | |
704 raise | |
705 | |
706 def shutdown(self): | |
707 self.sock.close() | |
708 try: | |
709 os.unlink(self.sockpath) | |
6997
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
710 if self.realsockpath: |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
711 os.unlink(self.realsockpath) |
9c4e488f105e
inotify: workaround ENAMETOOLONG by using symlinks
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6994
diff
changeset
|
712 os.rmdir(os.path.dirname(self.realsockpath)) |
6239 | 713 except OSError, err: |
714 if err.errno != errno.ENOENT: | |
715 raise | |
716 | |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
717 class master(object): |
6239 | 718 def __init__(self, ui, repo, timeout=None): |
719 self.ui = ui | |
720 self.repo = repo | |
721 self.poll = select.poll() | |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
722 self.repowatcher = repowatcher(ui, repo, self) |
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
723 self.server = server(ui, repo, self.repowatcher, timeout) |
6239 | 724 self.table = {} |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
725 for obj in (self.repowatcher, self.server): |
6239 | 726 fd = obj.fileno() |
727 self.table[fd] = obj | |
728 self.poll.register(fd, obj.poll_events) | |
729 | |
730 def register(self, fd, mask): | |
731 self.poll.register(fd, mask) | |
732 | |
733 def shutdown(self): | |
734 for obj in self.table.itervalues(): | |
735 obj.shutdown() | |
736 | |
737 def run(self): | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
738 self.repowatcher.setup() |
6239 | 739 self.ui.note(_('finished setup\n')) |
740 if os.getenv('TIME_STARTUP'): | |
741 sys.exit(0) | |
742 while True: | |
743 timeout = None | |
744 timeobj = None | |
745 for obj in self.table.itervalues(): | |
746 if obj.timeout is not None and (timeout is None or obj.timeout < timeout): | |
747 timeout, timeobj = obj.timeout, obj | |
748 try: | |
749 if self.ui.debugflag: | |
750 if timeout is None: | |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
751 self.ui.note(_('polling: no timeout\n')) |
6239 | 752 else: |
6961
12163fb21fce
i18n: mark strings for translation in inotify extension
Martin Geisler <mg@daimi.au.dk>
parents:
6909
diff
changeset
|
753 self.ui.note(_('polling: %sms timeout\n') % timeout) |
6239 | 754 events = self.poll.poll(timeout) |
755 except select.error, err: | |
756 if err[0] == errno.EINTR: | |
757 continue | |
758 raise | |
759 if events: | |
8609
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
760 by_fd = {} |
6239 | 761 for fd, event in events: |
8609
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
762 by_fd.setdefault(fd, []).append(event) |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
763 |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
764 for fd, events in by_fd.iteritems(): |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
765 self.table[fd].handle_pollevents(events) |
aeaa0bd9dc24
inotify: process all inotify events in one batch
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8608
diff
changeset
|
766 |
6239 | 767 elif timeobj: |
768 timeobj.handle_timeout() | |
769 | |
770 def start(ui, repo): | |
7451
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
771 def closefds(ignore): |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
772 # (from python bug #1177468) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
773 # close all inherited file descriptors |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
774 # 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
|
775 # 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
|
776 # 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
|
777 try: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
778 os.urandom(4) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
779 urandom_fd = getattr(os, '_urandomfd', None) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
780 except AttributeError: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
781 urandom_fd = None |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
782 ignore.append(urandom_fd) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
783 for fd in range(3, 256): |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
784 if fd in ignore: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
785 continue |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
786 try: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
787 os.close(fd) |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
788 except OSError: |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
789 pass |
fca9947652ce
inotify: close most file descriptors when autostarting
Brendan Cully <brendan@kublai.com>
parents:
7420
diff
changeset
|
790 |
8385
1536501ade62
inotify: Coding Style: name classes in lowercase.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8384
diff
changeset
|
791 m = master(ui, repo) |
6239 | 792 sys.stdout.flush() |
793 sys.stderr.flush() | |
794 | |
795 pid = os.fork() | |
796 if pid: | |
797 return pid | |
798 | |
8335
713ec3f9c9de
inotify: Clarify the use of "watcher" name.
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
8334
diff
changeset
|
799 closefds([m.server.fileno(), m.repowatcher.fileno()]) |
6239 | 800 os.setsid() |
801 | |
802 fd = os.open('/dev/null', os.O_RDONLY) | |
803 os.dup2(fd, 0) | |
804 if fd > 0: | |
805 os.close(fd) | |
806 | |
807 fd = os.open(ui.config('inotify', 'log', '/dev/null'), | |
808 os.O_RDWR | os.O_CREAT | os.O_TRUNC) | |
809 os.dup2(fd, 1) | |
810 os.dup2(fd, 2) | |
811 if fd > 2: | |
812 os.close(fd) | |
813 | |
814 try: | |
815 m.run() | |
816 finally: | |
817 m.shutdown() | |
818 os._exit(0) |