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