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