Show More
@@ -1,715 +1,715 b'' | |||||
1 | # server.py - inotify status server |
|
1 | # server.py - inotify status server | |
2 | # |
|
2 | # | |
3 | # Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com> |
|
3 | # Copyright 2006, 2007, 2008 Bryan O'Sullivan <bos@serpentine.com> | |
4 | # Copyright 2007, 2008 Brendan Cully <brendan@kublai.com> |
|
4 | # Copyright 2007, 2008 Brendan Cully <brendan@kublai.com> | |
5 | # |
|
5 | # | |
6 | # This software may be used and distributed according to the terms |
|
6 | # This software may be used and distributed according to the terms | |
7 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | # of the GNU General Public License, incorporated herein by reference. | |
8 |
|
8 | |||
9 | from mercurial.i18n import gettext as _ |
|
9 | from mercurial.i18n import gettext as _ | |
10 | from mercurial import osutil, ui, util |
|
10 | from mercurial import osutil, ui, util | |
11 | import common |
|
11 | import common | |
12 | import errno, os, select, socket, stat, struct, sys, time |
|
12 | import errno, os, select, socket, stat, struct, sys, time | |
13 |
|
13 | |||
14 | try: |
|
14 | try: | |
15 |
import |
|
15 | import linux as inotify | |
16 |
from |
|
16 | from linux import watcher | |
17 | except ImportError: |
|
17 | except ImportError: | |
18 | print >> sys.stderr, '*** native support is required for this extension' |
|
18 | print >> sys.stderr, '*** native support is required for this extension' | |
19 | raise |
|
19 | raise | |
20 |
|
20 | |||
21 | class AlreadyStartedException(Exception): pass |
|
21 | class AlreadyStartedException(Exception): pass | |
22 |
|
22 | |||
23 | def join(a, b): |
|
23 | def join(a, b): | |
24 | if a: |
|
24 | if a: | |
25 | if a[-1] == '/': |
|
25 | if a[-1] == '/': | |
26 | return a + b |
|
26 | return a + b | |
27 | return a + '/' + b |
|
27 | return a + '/' + b | |
28 | return b |
|
28 | return b | |
29 |
|
29 | |||
30 | walk_ignored_errors = (errno.ENOENT, errno.ENAMETOOLONG) |
|
30 | walk_ignored_errors = (errno.ENOENT, errno.ENAMETOOLONG) | |
31 |
|
31 | |||
32 | def walkrepodirs(repo): |
|
32 | def walkrepodirs(repo): | |
33 | '''Iterate over all subdirectories of this repo. |
|
33 | '''Iterate over all subdirectories of this repo. | |
34 | Exclude the .hg directory, any nested repos, and ignored dirs.''' |
|
34 | Exclude the .hg directory, any nested repos, and ignored dirs.''' | |
35 | rootslash = repo.root + os.sep |
|
35 | rootslash = repo.root + os.sep | |
36 | def walkit(dirname, top): |
|
36 | def walkit(dirname, top): | |
37 | hginside = False |
|
37 | hginside = False | |
38 | try: |
|
38 | try: | |
39 | for name, kind in osutil.listdir(rootslash + dirname): |
|
39 | for name, kind in osutil.listdir(rootslash + dirname): | |
40 | if kind == stat.S_IFDIR: |
|
40 | if kind == stat.S_IFDIR: | |
41 | if name == '.hg': |
|
41 | if name == '.hg': | |
42 | hginside = True |
|
42 | hginside = True | |
43 | if not top: break |
|
43 | if not top: break | |
44 | else: |
|
44 | else: | |
45 | d = join(dirname, name) |
|
45 | d = join(dirname, name) | |
46 | if repo.dirstate._ignore(d): |
|
46 | if repo.dirstate._ignore(d): | |
47 | continue |
|
47 | continue | |
48 | for subdir, hginsub in walkit(d, False): |
|
48 | for subdir, hginsub in walkit(d, False): | |
49 | if not hginsub: |
|
49 | if not hginsub: | |
50 | yield subdir, False |
|
50 | yield subdir, False | |
51 | except OSError, err: |
|
51 | except OSError, err: | |
52 | if err.errno not in walk_ignored_errors: |
|
52 | if err.errno not in walk_ignored_errors: | |
53 | raise |
|
53 | raise | |
54 | yield rootslash + dirname, hginside |
|
54 | yield rootslash + dirname, hginside | |
55 | for dirname, hginside in walkit('', True): |
|
55 | for dirname, hginside in walkit('', True): | |
56 | yield dirname |
|
56 | yield dirname | |
57 |
|
57 | |||
58 | def walk(repo, root): |
|
58 | def walk(repo, root): | |
59 | '''Like os.walk, but only yields regular files.''' |
|
59 | '''Like os.walk, but only yields regular files.''' | |
60 |
|
60 | |||
61 | # This function is critical to performance during startup. |
|
61 | # This function is critical to performance during startup. | |
62 |
|
62 | |||
63 | reporoot = root == '' |
|
63 | reporoot = root == '' | |
64 | rootslash = repo.root + os.sep |
|
64 | rootslash = repo.root + os.sep | |
65 |
|
65 | |||
66 | def walkit(root, reporoot): |
|
66 | def walkit(root, reporoot): | |
67 | files, dirs = [], [] |
|
67 | files, dirs = [], [] | |
68 | hginside = False |
|
68 | hginside = False | |
69 |
|
69 | |||
70 | try: |
|
70 | try: | |
71 | fullpath = rootslash + root |
|
71 | fullpath = rootslash + root | |
72 | for name, kind in osutil.listdir(fullpath): |
|
72 | for name, kind in osutil.listdir(fullpath): | |
73 | if kind == stat.S_IFDIR: |
|
73 | if kind == stat.S_IFDIR: | |
74 | if name == '.hg': |
|
74 | if name == '.hg': | |
75 | hginside = True |
|
75 | hginside = True | |
76 | if reporoot: |
|
76 | if reporoot: | |
77 | continue |
|
77 | continue | |
78 | else: |
|
78 | else: | |
79 | break |
|
79 | break | |
80 | dirs.append(name) |
|
80 | dirs.append(name) | |
81 | elif kind in (stat.S_IFREG, stat.S_IFLNK): |
|
81 | elif kind in (stat.S_IFREG, stat.S_IFLNK): | |
82 | path = join(root, name) |
|
82 | path = join(root, name) | |
83 | files.append((name, kind)) |
|
83 | files.append((name, kind)) | |
84 |
|
84 | |||
85 | yield hginside, fullpath, dirs, files |
|
85 | yield hginside, fullpath, dirs, files | |
86 |
|
86 | |||
87 | for subdir in dirs: |
|
87 | for subdir in dirs: | |
88 | path = join(root, subdir) |
|
88 | path = join(root, subdir) | |
89 | if repo.dirstate._ignore(path): |
|
89 | if repo.dirstate._ignore(path): | |
90 | continue |
|
90 | continue | |
91 | for result in walkit(path, False): |
|
91 | for result in walkit(path, False): | |
92 | if not result[0]: |
|
92 | if not result[0]: | |
93 | yield result |
|
93 | yield result | |
94 | except OSError, err: |
|
94 | except OSError, err: | |
95 | if err.errno not in walk_ignored_errors: |
|
95 | if err.errno not in walk_ignored_errors: | |
96 | raise |
|
96 | raise | |
97 | for result in walkit(root, reporoot): |
|
97 | for result in walkit(root, reporoot): | |
98 | yield result[1:] |
|
98 | yield result[1:] | |
99 |
|
99 | |||
100 | def _explain_watch_limit(ui, repo, count): |
|
100 | def _explain_watch_limit(ui, repo, count): | |
101 | path = '/proc/sys/fs/inotify/max_user_watches' |
|
101 | path = '/proc/sys/fs/inotify/max_user_watches' | |
102 | try: |
|
102 | try: | |
103 | limit = int(file(path).read()) |
|
103 | limit = int(file(path).read()) | |
104 | except IOError, err: |
|
104 | except IOError, err: | |
105 | if err.errno != errno.ENOENT: |
|
105 | if err.errno != errno.ENOENT: | |
106 | raise |
|
106 | raise | |
107 | raise util.Abort(_('this system does not seem to ' |
|
107 | raise util.Abort(_('this system does not seem to ' | |
108 | 'support inotify')) |
|
108 | 'support inotify')) | |
109 | ui.warn(_('*** the current per-user limit on the number ' |
|
109 | ui.warn(_('*** the current per-user limit on the number ' | |
110 | 'of inotify watches is %s\n') % limit) |
|
110 | 'of inotify watches is %s\n') % limit) | |
111 | ui.warn(_('*** this limit is too low to watch every ' |
|
111 | ui.warn(_('*** this limit is too low to watch every ' | |
112 | 'directory in this repository\n')) |
|
112 | 'directory in this repository\n')) | |
113 | ui.warn(_('*** counting directories: ')) |
|
113 | ui.warn(_('*** counting directories: ')) | |
114 | ndirs = len(list(walkrepodirs(repo))) |
|
114 | ndirs = len(list(walkrepodirs(repo))) | |
115 | ui.warn(_('found %d\n') % ndirs) |
|
115 | ui.warn(_('found %d\n') % ndirs) | |
116 | newlimit = min(limit, 1024) |
|
116 | newlimit = min(limit, 1024) | |
117 | while newlimit < ((limit + ndirs) * 1.1): |
|
117 | while newlimit < ((limit + ndirs) * 1.1): | |
118 | newlimit *= 2 |
|
118 | newlimit *= 2 | |
119 | ui.warn(_('*** to raise the limit from %d to %d (run as root):\n') % |
|
119 | ui.warn(_('*** to raise the limit from %d to %d (run as root):\n') % | |
120 | (limit, newlimit)) |
|
120 | (limit, newlimit)) | |
121 | ui.warn(_('*** echo %d > %s\n') % (newlimit, path)) |
|
121 | ui.warn(_('*** echo %d > %s\n') % (newlimit, path)) | |
122 | raise util.Abort(_('cannot watch %s until inotify watch limit is raised') |
|
122 | raise util.Abort(_('cannot watch %s until inotify watch limit is raised') | |
123 | % repo.root) |
|
123 | % repo.root) | |
124 |
|
124 | |||
125 | class Watcher(object): |
|
125 | class Watcher(object): | |
126 | poll_events = select.POLLIN |
|
126 | poll_events = select.POLLIN | |
127 | statuskeys = 'almr!?' |
|
127 | statuskeys = 'almr!?' | |
128 |
|
128 | |||
129 | def __init__(self, ui, repo, master): |
|
129 | def __init__(self, ui, repo, master): | |
130 | self.ui = ui |
|
130 | self.ui = ui | |
131 | self.repo = repo |
|
131 | self.repo = repo | |
132 | self.wprefix = self.repo.wjoin('') |
|
132 | self.wprefix = self.repo.wjoin('') | |
133 | self.timeout = None |
|
133 | self.timeout = None | |
134 | self.master = master |
|
134 | self.master = master | |
135 | self.mask = ( |
|
135 | self.mask = ( | |
136 | inotify.IN_ATTRIB | |
|
136 | inotify.IN_ATTRIB | | |
137 | inotify.IN_CREATE | |
|
137 | inotify.IN_CREATE | | |
138 | inotify.IN_DELETE | |
|
138 | inotify.IN_DELETE | | |
139 | inotify.IN_DELETE_SELF | |
|
139 | inotify.IN_DELETE_SELF | | |
140 | inotify.IN_MODIFY | |
|
140 | inotify.IN_MODIFY | | |
141 | inotify.IN_MOVED_FROM | |
|
141 | inotify.IN_MOVED_FROM | | |
142 | inotify.IN_MOVED_TO | |
|
142 | inotify.IN_MOVED_TO | | |
143 | inotify.IN_MOVE_SELF | |
|
143 | inotify.IN_MOVE_SELF | | |
144 | inotify.IN_ONLYDIR | |
|
144 | inotify.IN_ONLYDIR | | |
145 | inotify.IN_UNMOUNT | |
|
145 | inotify.IN_UNMOUNT | | |
146 | 0) |
|
146 | 0) | |
147 | try: |
|
147 | try: | |
148 | self.watcher = watcher.Watcher() |
|
148 | self.watcher = watcher.Watcher() | |
149 | except OSError, err: |
|
149 | except OSError, err: | |
150 | raise util.Abort(_('inotify service not available: %s') % |
|
150 | raise util.Abort(_('inotify service not available: %s') % | |
151 | err.strerror) |
|
151 | err.strerror) | |
152 | self.threshold = watcher.Threshold(self.watcher) |
|
152 | self.threshold = watcher.Threshold(self.watcher) | |
153 | self.registered = True |
|
153 | self.registered = True | |
154 | self.fileno = self.watcher.fileno |
|
154 | self.fileno = self.watcher.fileno | |
155 |
|
155 | |||
156 | self.repo.dirstate.__class__.inotifyserver = True |
|
156 | self.repo.dirstate.__class__.inotifyserver = True | |
157 |
|
157 | |||
158 | self.tree = {} |
|
158 | self.tree = {} | |
159 | self.statcache = {} |
|
159 | self.statcache = {} | |
160 | self.statustrees = dict([(s, {}) for s in self.statuskeys]) |
|
160 | self.statustrees = dict([(s, {}) for s in self.statuskeys]) | |
161 |
|
161 | |||
162 | self.watches = 0 |
|
162 | self.watches = 0 | |
163 | self.last_event = None |
|
163 | self.last_event = None | |
164 |
|
164 | |||
165 | self.eventq = {} |
|
165 | self.eventq = {} | |
166 | self.deferred = 0 |
|
166 | self.deferred = 0 | |
167 |
|
167 | |||
168 | self.ds_info = self.dirstate_info() |
|
168 | self.ds_info = self.dirstate_info() | |
169 | self.scan() |
|
169 | self.scan() | |
170 |
|
170 | |||
171 | def event_time(self): |
|
171 | def event_time(self): | |
172 | last = self.last_event |
|
172 | last = self.last_event | |
173 | now = time.time() |
|
173 | now = time.time() | |
174 | self.last_event = now |
|
174 | self.last_event = now | |
175 |
|
175 | |||
176 | if last is None: |
|
176 | if last is None: | |
177 | return 'start' |
|
177 | return 'start' | |
178 | delta = now - last |
|
178 | delta = now - last | |
179 | if delta < 5: |
|
179 | if delta < 5: | |
180 | return '+%.3f' % delta |
|
180 | return '+%.3f' % delta | |
181 | if delta < 50: |
|
181 | if delta < 50: | |
182 | return '+%.2f' % delta |
|
182 | return '+%.2f' % delta | |
183 | return '+%.1f' % delta |
|
183 | return '+%.1f' % delta | |
184 |
|
184 | |||
185 | def dirstate_info(self): |
|
185 | def dirstate_info(self): | |
186 | try: |
|
186 | try: | |
187 | st = os.lstat(self.repo.join('dirstate')) |
|
187 | st = os.lstat(self.repo.join('dirstate')) | |
188 | return st.st_mtime, st.st_ino |
|
188 | return st.st_mtime, st.st_ino | |
189 | except OSError, err: |
|
189 | except OSError, err: | |
190 | if err.errno != errno.ENOENT: |
|
190 | if err.errno != errno.ENOENT: | |
191 | raise |
|
191 | raise | |
192 | return 0, 0 |
|
192 | return 0, 0 | |
193 |
|
193 | |||
194 | def add_watch(self, path, mask): |
|
194 | def add_watch(self, path, mask): | |
195 | if not path: |
|
195 | if not path: | |
196 | return |
|
196 | return | |
197 | if self.watcher.path(path) is None: |
|
197 | if self.watcher.path(path) is None: | |
198 | if self.ui.debugflag: |
|
198 | if self.ui.debugflag: | |
199 | self.ui.note(_('watching %r\n') % path[len(self.wprefix):]) |
|
199 | self.ui.note(_('watching %r\n') % path[len(self.wprefix):]) | |
200 | try: |
|
200 | try: | |
201 | self.watcher.add(path, mask) |
|
201 | self.watcher.add(path, mask) | |
202 | self.watches += 1 |
|
202 | self.watches += 1 | |
203 | except OSError, err: |
|
203 | except OSError, err: | |
204 | if err.errno in (errno.ENOENT, errno.ENOTDIR): |
|
204 | if err.errno in (errno.ENOENT, errno.ENOTDIR): | |
205 | return |
|
205 | return | |
206 | if err.errno != errno.ENOSPC: |
|
206 | if err.errno != errno.ENOSPC: | |
207 | raise |
|
207 | raise | |
208 | _explain_watch_limit(self.ui, self.repo, self.watches) |
|
208 | _explain_watch_limit(self.ui, self.repo, self.watches) | |
209 |
|
209 | |||
210 | def setup(self): |
|
210 | def setup(self): | |
211 | self.ui.note(_('watching directories under %r\n') % self.repo.root) |
|
211 | self.ui.note(_('watching directories under %r\n') % self.repo.root) | |
212 | self.add_watch(self.repo.path, inotify.IN_DELETE) |
|
212 | self.add_watch(self.repo.path, inotify.IN_DELETE) | |
213 | self.check_dirstate() |
|
213 | self.check_dirstate() | |
214 |
|
214 | |||
215 | def wpath(self, evt): |
|
215 | def wpath(self, evt): | |
216 | path = evt.fullpath |
|
216 | path = evt.fullpath | |
217 | if path == self.repo.root: |
|
217 | if path == self.repo.root: | |
218 | return '' |
|
218 | return '' | |
219 | if path.startswith(self.wprefix): |
|
219 | if path.startswith(self.wprefix): | |
220 | return path[len(self.wprefix):] |
|
220 | return path[len(self.wprefix):] | |
221 | raise 'wtf? ' + path |
|
221 | raise 'wtf? ' + path | |
222 |
|
222 | |||
223 | def dir(self, tree, path): |
|
223 | def dir(self, tree, path): | |
224 | if path: |
|
224 | if path: | |
225 | for name in path.split('/'): |
|
225 | for name in path.split('/'): | |
226 | tree.setdefault(name, {}) |
|
226 | tree.setdefault(name, {}) | |
227 | tree = tree[name] |
|
227 | tree = tree[name] | |
228 | return tree |
|
228 | return tree | |
229 |
|
229 | |||
230 | def lookup(self, path, tree): |
|
230 | def lookup(self, path, tree): | |
231 | if path: |
|
231 | if path: | |
232 | try: |
|
232 | try: | |
233 | for name in path.split('/'): |
|
233 | for name in path.split('/'): | |
234 | tree = tree[name] |
|
234 | tree = tree[name] | |
235 | except KeyError: |
|
235 | except KeyError: | |
236 | return 'x' |
|
236 | return 'x' | |
237 | except TypeError: |
|
237 | except TypeError: | |
238 | return 'd' |
|
238 | return 'd' | |
239 | return tree |
|
239 | return tree | |
240 |
|
240 | |||
241 | def split(self, path): |
|
241 | def split(self, path): | |
242 | c = path.rfind('/') |
|
242 | c = path.rfind('/') | |
243 | if c == -1: |
|
243 | if c == -1: | |
244 | return '', path |
|
244 | return '', path | |
245 | return path[:c], path[c+1:] |
|
245 | return path[:c], path[c+1:] | |
246 |
|
246 | |||
247 | def filestatus(self, fn, st): |
|
247 | def filestatus(self, fn, st): | |
248 | try: |
|
248 | try: | |
249 | type_, mode, size, time = self.repo.dirstate._map[fn][:4] |
|
249 | type_, mode, size, time = self.repo.dirstate._map[fn][:4] | |
250 | except KeyError: |
|
250 | except KeyError: | |
251 | type_ = '?' |
|
251 | type_ = '?' | |
252 | if type_ == 'n': |
|
252 | if type_ == 'n': | |
253 | if not st: |
|
253 | if not st: | |
254 | return '!' |
|
254 | return '!' | |
255 | st_mode, st_size, st_mtime = st |
|
255 | st_mode, st_size, st_mtime = st | |
256 | if size and (size != st_size or (mode ^ st_mode) & 0100): |
|
256 | if size and (size != st_size or (mode ^ st_mode) & 0100): | |
257 | return 'm' |
|
257 | return 'm' | |
258 | if time != int(st_mtime): |
|
258 | if time != int(st_mtime): | |
259 | return 'l' |
|
259 | return 'l' | |
260 | return 'n' |
|
260 | return 'n' | |
261 | if type_ in 'ma' and not st: |
|
261 | if type_ in 'ma' and not st: | |
262 | return '!' |
|
262 | return '!' | |
263 | if type_ == '?' and self.repo.dirstate._ignore(fn): |
|
263 | if type_ == '?' and self.repo.dirstate._ignore(fn): | |
264 | return 'i' |
|
264 | return 'i' | |
265 | return type_ |
|
265 | return type_ | |
266 |
|
266 | |||
267 | def updatestatus(self, wfn, st=None, status=None, oldstatus=None): |
|
267 | def updatestatus(self, wfn, st=None, status=None, oldstatus=None): | |
268 | if st: |
|
268 | if st: | |
269 | status = self.filestatus(wfn, st) |
|
269 | status = self.filestatus(wfn, st) | |
270 | else: |
|
270 | else: | |
271 | self.statcache.pop(wfn, None) |
|
271 | self.statcache.pop(wfn, None) | |
272 | root, fn = self.split(wfn) |
|
272 | root, fn = self.split(wfn) | |
273 | d = self.dir(self.tree, root) |
|
273 | d = self.dir(self.tree, root) | |
274 | if oldstatus is None: |
|
274 | if oldstatus is None: | |
275 | oldstatus = d.get(fn) |
|
275 | oldstatus = d.get(fn) | |
276 | isdir = False |
|
276 | isdir = False | |
277 | if oldstatus: |
|
277 | if oldstatus: | |
278 | try: |
|
278 | try: | |
279 | if not status: |
|
279 | if not status: | |
280 | if oldstatus in 'almn': |
|
280 | if oldstatus in 'almn': | |
281 | status = '!' |
|
281 | status = '!' | |
282 | elif oldstatus == 'r': |
|
282 | elif oldstatus == 'r': | |
283 | status = 'r' |
|
283 | status = 'r' | |
284 | except TypeError: |
|
284 | except TypeError: | |
285 | # oldstatus may be a dict left behind by a deleted |
|
285 | # oldstatus may be a dict left behind by a deleted | |
286 | # directory |
|
286 | # directory | |
287 | isdir = True |
|
287 | isdir = True | |
288 | else: |
|
288 | else: | |
289 | if oldstatus in self.statuskeys and oldstatus != status: |
|
289 | if oldstatus in self.statuskeys and oldstatus != status: | |
290 | del self.dir(self.statustrees[oldstatus], root)[fn] |
|
290 | del self.dir(self.statustrees[oldstatus], root)[fn] | |
291 | if self.ui.debugflag and oldstatus != status: |
|
291 | if self.ui.debugflag and oldstatus != status: | |
292 | if isdir: |
|
292 | if isdir: | |
293 | self.ui.note('status: %r dir(%d) -> %s\n' % |
|
293 | self.ui.note('status: %r dir(%d) -> %s\n' % | |
294 | (wfn, len(oldstatus), status)) |
|
294 | (wfn, len(oldstatus), status)) | |
295 | else: |
|
295 | else: | |
296 | self.ui.note('status: %r %s -> %s\n' % |
|
296 | self.ui.note('status: %r %s -> %s\n' % | |
297 | (wfn, oldstatus, status)) |
|
297 | (wfn, oldstatus, status)) | |
298 | if not isdir: |
|
298 | if not isdir: | |
299 | if status and status != 'i': |
|
299 | if status and status != 'i': | |
300 | d[fn] = status |
|
300 | d[fn] = status | |
301 | if status in self.statuskeys: |
|
301 | if status in self.statuskeys: | |
302 | dd = self.dir(self.statustrees[status], root) |
|
302 | dd = self.dir(self.statustrees[status], root) | |
303 | if oldstatus != status or fn not in dd: |
|
303 | if oldstatus != status or fn not in dd: | |
304 | dd[fn] = status |
|
304 | dd[fn] = status | |
305 | else: |
|
305 | else: | |
306 | d.pop(fn, None) |
|
306 | d.pop(fn, None) | |
307 |
|
307 | |||
308 | def check_deleted(self, key): |
|
308 | def check_deleted(self, key): | |
309 | # Files that had been deleted but were present in the dirstate |
|
309 | # Files that had been deleted but were present in the dirstate | |
310 | # may have vanished from the dirstate; we must clean them up. |
|
310 | # may have vanished from the dirstate; we must clean them up. | |
311 | nuke = [] |
|
311 | nuke = [] | |
312 | for wfn, ignore in self.walk(key, self.statustrees[key]): |
|
312 | for wfn, ignore in self.walk(key, self.statustrees[key]): | |
313 | if wfn not in self.repo.dirstate: |
|
313 | if wfn not in self.repo.dirstate: | |
314 | nuke.append(wfn) |
|
314 | nuke.append(wfn) | |
315 | for wfn in nuke: |
|
315 | for wfn in nuke: | |
316 | root, fn = self.split(wfn) |
|
316 | root, fn = self.split(wfn) | |
317 | del self.dir(self.statustrees[key], root)[fn] |
|
317 | del self.dir(self.statustrees[key], root)[fn] | |
318 | del self.dir(self.tree, root)[fn] |
|
318 | del self.dir(self.tree, root)[fn] | |
319 |
|
319 | |||
320 | def scan(self, topdir=''): |
|
320 | def scan(self, topdir=''): | |
321 | self.handle_timeout() |
|
321 | self.handle_timeout() | |
322 | ds = self.repo.dirstate._map.copy() |
|
322 | ds = self.repo.dirstate._map.copy() | |
323 | self.add_watch(join(self.repo.root, topdir), self.mask) |
|
323 | self.add_watch(join(self.repo.root, topdir), self.mask) | |
324 | for root, dirs, entries in walk(self.repo, topdir): |
|
324 | for root, dirs, entries in walk(self.repo, topdir): | |
325 | for d in dirs: |
|
325 | for d in dirs: | |
326 | self.add_watch(join(root, d), self.mask) |
|
326 | self.add_watch(join(root, d), self.mask) | |
327 | wroot = root[len(self.wprefix):] |
|
327 | wroot = root[len(self.wprefix):] | |
328 | d = self.dir(self.tree, wroot) |
|
328 | d = self.dir(self.tree, wroot) | |
329 | for fn, kind in entries: |
|
329 | for fn, kind in entries: | |
330 | wfn = join(wroot, fn) |
|
330 | wfn = join(wroot, fn) | |
331 | self.updatestatus(wfn, self.getstat(wfn)) |
|
331 | self.updatestatus(wfn, self.getstat(wfn)) | |
332 | ds.pop(wfn, None) |
|
332 | ds.pop(wfn, None) | |
333 | wtopdir = topdir |
|
333 | wtopdir = topdir | |
334 | if wtopdir and wtopdir[-1] != '/': |
|
334 | if wtopdir and wtopdir[-1] != '/': | |
335 | wtopdir += '/' |
|
335 | wtopdir += '/' | |
336 | for wfn, state in ds.iteritems(): |
|
336 | for wfn, state in ds.iteritems(): | |
337 | if not wfn.startswith(wtopdir): |
|
337 | if not wfn.startswith(wtopdir): | |
338 | continue |
|
338 | continue | |
339 | status = state[0] |
|
339 | status = state[0] | |
340 | st = self.getstat(wfn) |
|
340 | st = self.getstat(wfn) | |
341 | if status == 'r' and not st: |
|
341 | if status == 'r' and not st: | |
342 | self.updatestatus(wfn, st, status=status) |
|
342 | self.updatestatus(wfn, st, status=status) | |
343 | else: |
|
343 | else: | |
344 | self.updatestatus(wfn, st, oldstatus=status) |
|
344 | self.updatestatus(wfn, st, oldstatus=status) | |
345 | self.check_deleted('!') |
|
345 | self.check_deleted('!') | |
346 | self.check_deleted('r') |
|
346 | self.check_deleted('r') | |
347 |
|
347 | |||
348 | def check_dirstate(self): |
|
348 | def check_dirstate(self): | |
349 | ds_info = self.dirstate_info() |
|
349 | ds_info = self.dirstate_info() | |
350 | if ds_info == self.ds_info: |
|
350 | if ds_info == self.ds_info: | |
351 | return |
|
351 | return | |
352 | self.ds_info = ds_info |
|
352 | self.ds_info = ds_info | |
353 | if not self.ui.debugflag: |
|
353 | if not self.ui.debugflag: | |
354 | self.last_event = None |
|
354 | self.last_event = None | |
355 | self.ui.note(_('%s dirstate reload\n') % self.event_time()) |
|
355 | self.ui.note(_('%s dirstate reload\n') % self.event_time()) | |
356 | self.repo.dirstate.invalidate() |
|
356 | self.repo.dirstate.invalidate() | |
357 | self.scan() |
|
357 | self.scan() | |
358 | self.ui.note(_('%s end dirstate reload\n') % self.event_time()) |
|
358 | self.ui.note(_('%s end dirstate reload\n') % self.event_time()) | |
359 |
|
359 | |||
360 | def walk(self, states, tree, prefix=''): |
|
360 | def walk(self, states, tree, prefix=''): | |
361 | # This is the "inner loop" when talking to the client. |
|
361 | # This is the "inner loop" when talking to the client. | |
362 |
|
362 | |||
363 | for name, val in tree.iteritems(): |
|
363 | for name, val in tree.iteritems(): | |
364 | path = join(prefix, name) |
|
364 | path = join(prefix, name) | |
365 | try: |
|
365 | try: | |
366 | if val in states: |
|
366 | if val in states: | |
367 | yield path, val |
|
367 | yield path, val | |
368 | except TypeError: |
|
368 | except TypeError: | |
369 | for p in self.walk(states, val, path): |
|
369 | for p in self.walk(states, val, path): | |
370 | yield p |
|
370 | yield p | |
371 |
|
371 | |||
372 | def update_hgignore(self): |
|
372 | def update_hgignore(self): | |
373 | # An update of the ignore file can potentially change the |
|
373 | # An update of the ignore file can potentially change the | |
374 | # states of all unknown and ignored files. |
|
374 | # states of all unknown and ignored files. | |
375 |
|
375 | |||
376 | # XXX If the user has other ignore files outside the repo, or |
|
376 | # XXX If the user has other ignore files outside the repo, or | |
377 | # changes their list of ignore files at run time, we'll |
|
377 | # changes their list of ignore files at run time, we'll | |
378 | # potentially never see changes to them. We could get the |
|
378 | # potentially never see changes to them. We could get the | |
379 | # client to report to us what ignore data they're using. |
|
379 | # client to report to us what ignore data they're using. | |
380 | # But it's easier to do nothing than to open that can of |
|
380 | # But it's easier to do nothing than to open that can of | |
381 | # worms. |
|
381 | # worms. | |
382 |
|
382 | |||
383 | if self.repo.dirstate.ignorefunc is not None: |
|
383 | if self.repo.dirstate.ignorefunc is not None: | |
384 | self.repo.dirstate.ignorefunc = None |
|
384 | self.repo.dirstate.ignorefunc = None | |
385 | self.ui.note('rescanning due to .hgignore change\n') |
|
385 | self.ui.note('rescanning due to .hgignore change\n') | |
386 | self.scan() |
|
386 | self.scan() | |
387 |
|
387 | |||
388 | def getstat(self, wpath): |
|
388 | def getstat(self, wpath): | |
389 | try: |
|
389 | try: | |
390 | return self.statcache[wpath] |
|
390 | return self.statcache[wpath] | |
391 | except KeyError: |
|
391 | except KeyError: | |
392 | try: |
|
392 | try: | |
393 | return self.stat(wpath) |
|
393 | return self.stat(wpath) | |
394 | except OSError, err: |
|
394 | except OSError, err: | |
395 | if err.errno != errno.ENOENT: |
|
395 | if err.errno != errno.ENOENT: | |
396 | raise |
|
396 | raise | |
397 |
|
397 | |||
398 | def stat(self, wpath): |
|
398 | def stat(self, wpath): | |
399 | try: |
|
399 | try: | |
400 | st = os.lstat(join(self.wprefix, wpath)) |
|
400 | st = os.lstat(join(self.wprefix, wpath)) | |
401 | ret = st.st_mode, st.st_size, st.st_mtime |
|
401 | ret = st.st_mode, st.st_size, st.st_mtime | |
402 | self.statcache[wpath] = ret |
|
402 | self.statcache[wpath] = ret | |
403 | return ret |
|
403 | return ret | |
404 | except OSError, err: |
|
404 | except OSError, err: | |
405 | self.statcache.pop(wpath, None) |
|
405 | self.statcache.pop(wpath, None) | |
406 | raise |
|
406 | raise | |
407 |
|
407 | |||
408 | def created(self, wpath): |
|
408 | def created(self, wpath): | |
409 | if wpath == '.hgignore': |
|
409 | if wpath == '.hgignore': | |
410 | self.update_hgignore() |
|
410 | self.update_hgignore() | |
411 | try: |
|
411 | try: | |
412 | st = self.stat(wpath) |
|
412 | st = self.stat(wpath) | |
413 | if stat.S_ISREG(st[0]): |
|
413 | if stat.S_ISREG(st[0]): | |
414 | self.updatestatus(wpath, st) |
|
414 | self.updatestatus(wpath, st) | |
415 | except OSError, err: |
|
415 | except OSError, err: | |
416 | pass |
|
416 | pass | |
417 |
|
417 | |||
418 | def modified(self, wpath): |
|
418 | def modified(self, wpath): | |
419 | if wpath == '.hgignore': |
|
419 | if wpath == '.hgignore': | |
420 | self.update_hgignore() |
|
420 | self.update_hgignore() | |
421 | try: |
|
421 | try: | |
422 | st = self.stat(wpath) |
|
422 | st = self.stat(wpath) | |
423 | if stat.S_ISREG(st[0]): |
|
423 | if stat.S_ISREG(st[0]): | |
424 | if self.repo.dirstate[wpath] in 'lmn': |
|
424 | if self.repo.dirstate[wpath] in 'lmn': | |
425 | self.updatestatus(wpath, st) |
|
425 | self.updatestatus(wpath, st) | |
426 | except OSError: |
|
426 | except OSError: | |
427 | pass |
|
427 | pass | |
428 |
|
428 | |||
429 | def deleted(self, wpath): |
|
429 | def deleted(self, wpath): | |
430 | if wpath == '.hgignore': |
|
430 | if wpath == '.hgignore': | |
431 | self.update_hgignore() |
|
431 | self.update_hgignore() | |
432 | elif wpath.startswith('.hg/'): |
|
432 | elif wpath.startswith('.hg/'): | |
433 | if wpath == '.hg/wlock': |
|
433 | if wpath == '.hg/wlock': | |
434 | self.check_dirstate() |
|
434 | self.check_dirstate() | |
435 | return |
|
435 | return | |
436 |
|
436 | |||
437 | self.updatestatus(wpath, None) |
|
437 | self.updatestatus(wpath, None) | |
438 |
|
438 | |||
439 | def schedule_work(self, wpath, evt): |
|
439 | def schedule_work(self, wpath, evt): | |
440 | self.eventq.setdefault(wpath, []) |
|
440 | self.eventq.setdefault(wpath, []) | |
441 | prev = self.eventq[wpath] |
|
441 | prev = self.eventq[wpath] | |
442 | try: |
|
442 | try: | |
443 | if prev and evt == 'm' and prev[-1] in 'cm': |
|
443 | if prev and evt == 'm' and prev[-1] in 'cm': | |
444 | return |
|
444 | return | |
445 | self.eventq[wpath].append(evt) |
|
445 | self.eventq[wpath].append(evt) | |
446 | finally: |
|
446 | finally: | |
447 | self.deferred += 1 |
|
447 | self.deferred += 1 | |
448 | self.timeout = 250 |
|
448 | self.timeout = 250 | |
449 |
|
449 | |||
450 | def deferred_event(self, wpath, evt): |
|
450 | def deferred_event(self, wpath, evt): | |
451 | if evt == 'c': |
|
451 | if evt == 'c': | |
452 | self.created(wpath) |
|
452 | self.created(wpath) | |
453 | elif evt == 'm': |
|
453 | elif evt == 'm': | |
454 | self.modified(wpath) |
|
454 | self.modified(wpath) | |
455 | elif evt == 'd': |
|
455 | elif evt == 'd': | |
456 | self.deleted(wpath) |
|
456 | self.deleted(wpath) | |
457 |
|
457 | |||
458 | def process_create(self, wpath, evt): |
|
458 | def process_create(self, wpath, evt): | |
459 | if self.ui.debugflag: |
|
459 | if self.ui.debugflag: | |
460 | self.ui.note(_('%s event: created %s\n') % |
|
460 | self.ui.note(_('%s event: created %s\n') % | |
461 | (self.event_time(), wpath)) |
|
461 | (self.event_time(), wpath)) | |
462 |
|
462 | |||
463 | if evt.mask & inotify.IN_ISDIR: |
|
463 | if evt.mask & inotify.IN_ISDIR: | |
464 | self.scan(wpath) |
|
464 | self.scan(wpath) | |
465 | else: |
|
465 | else: | |
466 | self.schedule_work(wpath, 'c') |
|
466 | self.schedule_work(wpath, 'c') | |
467 |
|
467 | |||
468 | def process_delete(self, wpath, evt): |
|
468 | def process_delete(self, wpath, evt): | |
469 | if self.ui.debugflag: |
|
469 | if self.ui.debugflag: | |
470 | self.ui.note(('%s event: deleted %s\n') % |
|
470 | self.ui.note(('%s event: deleted %s\n') % | |
471 | (self.event_time(), wpath)) |
|
471 | (self.event_time(), wpath)) | |
472 |
|
472 | |||
473 | if evt.mask & inotify.IN_ISDIR: |
|
473 | if evt.mask & inotify.IN_ISDIR: | |
474 | self.scan(wpath) |
|
474 | self.scan(wpath) | |
475 | else: |
|
475 | else: | |
476 | self.schedule_work(wpath, 'd') |
|
476 | self.schedule_work(wpath, 'd') | |
477 |
|
477 | |||
478 | def process_modify(self, wpath, evt): |
|
478 | def process_modify(self, wpath, evt): | |
479 | if self.ui.debugflag: |
|
479 | if self.ui.debugflag: | |
480 | self.ui.note(_('%s event: modified %s\n') % |
|
480 | self.ui.note(_('%s event: modified %s\n') % | |
481 | (self.event_time(), wpath)) |
|
481 | (self.event_time(), wpath)) | |
482 |
|
482 | |||
483 | if not (evt.mask & inotify.IN_ISDIR): |
|
483 | if not (evt.mask & inotify.IN_ISDIR): | |
484 | self.schedule_work(wpath, 'm') |
|
484 | self.schedule_work(wpath, 'm') | |
485 |
|
485 | |||
486 | def process_unmount(self, evt): |
|
486 | def process_unmount(self, evt): | |
487 | self.ui.warn(_('filesystem containing %s was unmounted\n') % |
|
487 | self.ui.warn(_('filesystem containing %s was unmounted\n') % | |
488 | evt.fullpath) |
|
488 | evt.fullpath) | |
489 | sys.exit(0) |
|
489 | sys.exit(0) | |
490 |
|
490 | |||
491 | def handle_event(self, fd, event): |
|
491 | def handle_event(self, fd, event): | |
492 | if self.ui.debugflag: |
|
492 | if self.ui.debugflag: | |
493 | self.ui.note('%s readable: %d bytes\n' % |
|
493 | self.ui.note('%s readable: %d bytes\n' % | |
494 | (self.event_time(), self.threshold.readable())) |
|
494 | (self.event_time(), self.threshold.readable())) | |
495 | if not self.threshold(): |
|
495 | if not self.threshold(): | |
496 | if self.registered: |
|
496 | if self.registered: | |
497 | if self.ui.debugflag: |
|
497 | if self.ui.debugflag: | |
498 | self.ui.note('%s below threshold - unhooking\n' % |
|
498 | self.ui.note('%s below threshold - unhooking\n' % | |
499 | (self.event_time())) |
|
499 | (self.event_time())) | |
500 | self.master.poll.unregister(fd) |
|
500 | self.master.poll.unregister(fd) | |
501 | self.registered = False |
|
501 | self.registered = False | |
502 | self.timeout = 250 |
|
502 | self.timeout = 250 | |
503 | else: |
|
503 | else: | |
504 | self.read_events() |
|
504 | self.read_events() | |
505 |
|
505 | |||
506 | def read_events(self, bufsize=None): |
|
506 | def read_events(self, bufsize=None): | |
507 | events = self.watcher.read(bufsize) |
|
507 | events = self.watcher.read(bufsize) | |
508 | if self.ui.debugflag: |
|
508 | if self.ui.debugflag: | |
509 | self.ui.note('%s reading %d events\n' % |
|
509 | self.ui.note('%s reading %d events\n' % | |
510 | (self.event_time(), len(events))) |
|
510 | (self.event_time(), len(events))) | |
511 | for evt in events: |
|
511 | for evt in events: | |
512 | wpath = self.wpath(evt) |
|
512 | wpath = self.wpath(evt) | |
513 | if evt.mask & inotify.IN_UNMOUNT: |
|
513 | if evt.mask & inotify.IN_UNMOUNT: | |
514 | self.process_unmount(wpath, evt) |
|
514 | self.process_unmount(wpath, evt) | |
515 | elif evt.mask & (inotify.IN_MODIFY | inotify.IN_ATTRIB): |
|
515 | elif evt.mask & (inotify.IN_MODIFY | inotify.IN_ATTRIB): | |
516 | self.process_modify(wpath, evt) |
|
516 | self.process_modify(wpath, evt) | |
517 | elif evt.mask & (inotify.IN_DELETE | inotify.IN_DELETE_SELF | |
|
517 | elif evt.mask & (inotify.IN_DELETE | inotify.IN_DELETE_SELF | | |
518 | inotify.IN_MOVED_FROM): |
|
518 | inotify.IN_MOVED_FROM): | |
519 | self.process_delete(wpath, evt) |
|
519 | self.process_delete(wpath, evt) | |
520 | elif evt.mask & (inotify.IN_CREATE | inotify.IN_MOVED_TO): |
|
520 | elif evt.mask & (inotify.IN_CREATE | inotify.IN_MOVED_TO): | |
521 | self.process_create(wpath, evt) |
|
521 | self.process_create(wpath, evt) | |
522 |
|
522 | |||
523 | def handle_timeout(self): |
|
523 | def handle_timeout(self): | |
524 | if not self.registered: |
|
524 | if not self.registered: | |
525 | if self.ui.debugflag: |
|
525 | if self.ui.debugflag: | |
526 | self.ui.note('%s hooking back up with %d bytes readable\n' % |
|
526 | self.ui.note('%s hooking back up with %d bytes readable\n' % | |
527 | (self.event_time(), self.threshold.readable())) |
|
527 | (self.event_time(), self.threshold.readable())) | |
528 | self.read_events(0) |
|
528 | self.read_events(0) | |
529 | self.master.poll.register(self, select.POLLIN) |
|
529 | self.master.poll.register(self, select.POLLIN) | |
530 | self.registered = True |
|
530 | self.registered = True | |
531 |
|
531 | |||
532 | if self.eventq: |
|
532 | if self.eventq: | |
533 | if self.ui.debugflag: |
|
533 | if self.ui.debugflag: | |
534 | self.ui.note('%s processing %d deferred events as %d\n' % |
|
534 | self.ui.note('%s processing %d deferred events as %d\n' % | |
535 | (self.event_time(), self.deferred, |
|
535 | (self.event_time(), self.deferred, | |
536 | len(self.eventq))) |
|
536 | len(self.eventq))) | |
537 | for wpath, evts in util.sort(self.eventq.items()): |
|
537 | for wpath, evts in util.sort(self.eventq.items()): | |
538 | for evt in evts: |
|
538 | for evt in evts: | |
539 | self.deferred_event(wpath, evt) |
|
539 | self.deferred_event(wpath, evt) | |
540 | self.eventq.clear() |
|
540 | self.eventq.clear() | |
541 | self.deferred = 0 |
|
541 | self.deferred = 0 | |
542 | self.timeout = None |
|
542 | self.timeout = None | |
543 |
|
543 | |||
544 | def shutdown(self): |
|
544 | def shutdown(self): | |
545 | self.watcher.close() |
|
545 | self.watcher.close() | |
546 |
|
546 | |||
547 | class Server(object): |
|
547 | class Server(object): | |
548 | poll_events = select.POLLIN |
|
548 | poll_events = select.POLLIN | |
549 |
|
549 | |||
550 | def __init__(self, ui, repo, watcher, timeout): |
|
550 | def __init__(self, ui, repo, watcher, timeout): | |
551 | self.ui = ui |
|
551 | self.ui = ui | |
552 | self.repo = repo |
|
552 | self.repo = repo | |
553 | self.watcher = watcher |
|
553 | self.watcher = watcher | |
554 | self.timeout = timeout |
|
554 | self.timeout = timeout | |
555 | self.sock = socket.socket(socket.AF_UNIX) |
|
555 | self.sock = socket.socket(socket.AF_UNIX) | |
556 | self.sockpath = self.repo.join('inotify.sock') |
|
556 | self.sockpath = self.repo.join('inotify.sock') | |
557 | try: |
|
557 | try: | |
558 | self.sock.bind(self.sockpath) |
|
558 | self.sock.bind(self.sockpath) | |
559 | except socket.error, err: |
|
559 | except socket.error, err: | |
560 | if err[0] == errno.EADDRINUSE: |
|
560 | if err[0] == errno.EADDRINUSE: | |
561 | raise AlreadyStartedException(_('could not start server: %s') \ |
|
561 | raise AlreadyStartedException(_('could not start server: %s') \ | |
562 | % err[1]) |
|
562 | % err[1]) | |
563 | raise |
|
563 | raise | |
564 | self.sock.listen(5) |
|
564 | self.sock.listen(5) | |
565 | self.fileno = self.sock.fileno |
|
565 | self.fileno = self.sock.fileno | |
566 |
|
566 | |||
567 | def handle_timeout(self): |
|
567 | def handle_timeout(self): | |
568 | pass |
|
568 | pass | |
569 |
|
569 | |||
570 | def handle_event(self, fd, event): |
|
570 | def handle_event(self, fd, event): | |
571 | sock, addr = self.sock.accept() |
|
571 | sock, addr = self.sock.accept() | |
572 |
|
572 | |||
573 | cs = common.recvcs(sock) |
|
573 | cs = common.recvcs(sock) | |
574 | version = ord(cs.read(1)) |
|
574 | version = ord(cs.read(1)) | |
575 |
|
575 | |||
576 | sock.sendall(chr(common.version)) |
|
576 | sock.sendall(chr(common.version)) | |
577 |
|
577 | |||
578 | if version != common.version: |
|
578 | if version != common.version: | |
579 | self.ui.warn(_('received query from incompatible client ' |
|
579 | self.ui.warn(_('received query from incompatible client ' | |
580 | 'version %d\n') % version) |
|
580 | 'version %d\n') % version) | |
581 | return |
|
581 | return | |
582 |
|
582 | |||
583 | names = cs.read().split('\0') |
|
583 | names = cs.read().split('\0') | |
584 |
|
584 | |||
585 | states = names.pop() |
|
585 | states = names.pop() | |
586 |
|
586 | |||
587 | self.ui.note(_('answering query for %r\n') % states) |
|
587 | self.ui.note(_('answering query for %r\n') % states) | |
588 |
|
588 | |||
589 | if self.watcher.timeout: |
|
589 | if self.watcher.timeout: | |
590 | # We got a query while a rescan is pending. Make sure we |
|
590 | # We got a query while a rescan is pending. Make sure we | |
591 | # rescan before responding, or we could give back a wrong |
|
591 | # rescan before responding, or we could give back a wrong | |
592 | # answer. |
|
592 | # answer. | |
593 | self.watcher.handle_timeout() |
|
593 | self.watcher.handle_timeout() | |
594 |
|
594 | |||
595 | if not names: |
|
595 | if not names: | |
596 | def genresult(states, tree): |
|
596 | def genresult(states, tree): | |
597 | for fn, state in self.watcher.walk(states, tree): |
|
597 | for fn, state in self.watcher.walk(states, tree): | |
598 | yield fn |
|
598 | yield fn | |
599 | else: |
|
599 | else: | |
600 | def genresult(states, tree): |
|
600 | def genresult(states, tree): | |
601 | for fn in names: |
|
601 | for fn in names: | |
602 | l = self.watcher.lookup(fn, tree) |
|
602 | l = self.watcher.lookup(fn, tree) | |
603 | try: |
|
603 | try: | |
604 | if l in states: |
|
604 | if l in states: | |
605 | yield fn |
|
605 | yield fn | |
606 | except TypeError: |
|
606 | except TypeError: | |
607 | for f, s in self.watcher.walk(states, l, fn): |
|
607 | for f, s in self.watcher.walk(states, l, fn): | |
608 | yield f |
|
608 | yield f | |
609 |
|
609 | |||
610 | results = ['\0'.join(r) for r in [ |
|
610 | results = ['\0'.join(r) for r in [ | |
611 | genresult('l', self.watcher.statustrees['l']), |
|
611 | genresult('l', self.watcher.statustrees['l']), | |
612 | genresult('m', self.watcher.statustrees['m']), |
|
612 | genresult('m', self.watcher.statustrees['m']), | |
613 | genresult('a', self.watcher.statustrees['a']), |
|
613 | genresult('a', self.watcher.statustrees['a']), | |
614 | genresult('r', self.watcher.statustrees['r']), |
|
614 | genresult('r', self.watcher.statustrees['r']), | |
615 | genresult('!', self.watcher.statustrees['!']), |
|
615 | genresult('!', self.watcher.statustrees['!']), | |
616 | '?' in states and genresult('?', self.watcher.statustrees['?']) or [], |
|
616 | '?' in states and genresult('?', self.watcher.statustrees['?']) or [], | |
617 | [], |
|
617 | [], | |
618 | 'c' in states and genresult('n', self.watcher.tree) or [], |
|
618 | 'c' in states and genresult('n', self.watcher.tree) or [], | |
619 | ]] |
|
619 | ]] | |
620 |
|
620 | |||
621 | try: |
|
621 | try: | |
622 | try: |
|
622 | try: | |
623 | sock.sendall(struct.pack(common.resphdrfmt, |
|
623 | sock.sendall(struct.pack(common.resphdrfmt, | |
624 | *map(len, results))) |
|
624 | *map(len, results))) | |
625 | sock.sendall(''.join(results)) |
|
625 | sock.sendall(''.join(results)) | |
626 | finally: |
|
626 | finally: | |
627 | sock.shutdown(socket.SHUT_WR) |
|
627 | sock.shutdown(socket.SHUT_WR) | |
628 | except socket.error, err: |
|
628 | except socket.error, err: | |
629 | if err[0] != errno.EPIPE: |
|
629 | if err[0] != errno.EPIPE: | |
630 | raise |
|
630 | raise | |
631 |
|
631 | |||
632 | def shutdown(self): |
|
632 | def shutdown(self): | |
633 | self.sock.close() |
|
633 | self.sock.close() | |
634 | try: |
|
634 | try: | |
635 | os.unlink(self.sockpath) |
|
635 | os.unlink(self.sockpath) | |
636 | except OSError, err: |
|
636 | except OSError, err: | |
637 | if err.errno != errno.ENOENT: |
|
637 | if err.errno != errno.ENOENT: | |
638 | raise |
|
638 | raise | |
639 |
|
639 | |||
640 | class Master(object): |
|
640 | class Master(object): | |
641 | def __init__(self, ui, repo, timeout=None): |
|
641 | def __init__(self, ui, repo, timeout=None): | |
642 | self.ui = ui |
|
642 | self.ui = ui | |
643 | self.repo = repo |
|
643 | self.repo = repo | |
644 | self.poll = select.poll() |
|
644 | self.poll = select.poll() | |
645 | self.watcher = Watcher(ui, repo, self) |
|
645 | self.watcher = Watcher(ui, repo, self) | |
646 | self.server = Server(ui, repo, self.watcher, timeout) |
|
646 | self.server = Server(ui, repo, self.watcher, timeout) | |
647 | self.table = {} |
|
647 | self.table = {} | |
648 | for obj in (self.watcher, self.server): |
|
648 | for obj in (self.watcher, self.server): | |
649 | fd = obj.fileno() |
|
649 | fd = obj.fileno() | |
650 | self.table[fd] = obj |
|
650 | self.table[fd] = obj | |
651 | self.poll.register(fd, obj.poll_events) |
|
651 | self.poll.register(fd, obj.poll_events) | |
652 |
|
652 | |||
653 | def register(self, fd, mask): |
|
653 | def register(self, fd, mask): | |
654 | self.poll.register(fd, mask) |
|
654 | self.poll.register(fd, mask) | |
655 |
|
655 | |||
656 | def shutdown(self): |
|
656 | def shutdown(self): | |
657 | for obj in self.table.itervalues(): |
|
657 | for obj in self.table.itervalues(): | |
658 | obj.shutdown() |
|
658 | obj.shutdown() | |
659 |
|
659 | |||
660 | def run(self): |
|
660 | def run(self): | |
661 | self.watcher.setup() |
|
661 | self.watcher.setup() | |
662 | self.ui.note(_('finished setup\n')) |
|
662 | self.ui.note(_('finished setup\n')) | |
663 | if os.getenv('TIME_STARTUP'): |
|
663 | if os.getenv('TIME_STARTUP'): | |
664 | sys.exit(0) |
|
664 | sys.exit(0) | |
665 | while True: |
|
665 | while True: | |
666 | timeout = None |
|
666 | timeout = None | |
667 | timeobj = None |
|
667 | timeobj = None | |
668 | for obj in self.table.itervalues(): |
|
668 | for obj in self.table.itervalues(): | |
669 | if obj.timeout is not None and (timeout is None or obj.timeout < timeout): |
|
669 | if obj.timeout is not None and (timeout is None or obj.timeout < timeout): | |
670 | timeout, timeobj = obj.timeout, obj |
|
670 | timeout, timeobj = obj.timeout, obj | |
671 | try: |
|
671 | try: | |
672 | if self.ui.debugflag: |
|
672 | if self.ui.debugflag: | |
673 | if timeout is None: |
|
673 | if timeout is None: | |
674 | self.ui.note('polling: no timeout\n') |
|
674 | self.ui.note('polling: no timeout\n') | |
675 | else: |
|
675 | else: | |
676 | self.ui.note('polling: %sms timeout\n' % timeout) |
|
676 | self.ui.note('polling: %sms timeout\n' % timeout) | |
677 | events = self.poll.poll(timeout) |
|
677 | events = self.poll.poll(timeout) | |
678 | except select.error, err: |
|
678 | except select.error, err: | |
679 | if err[0] == errno.EINTR: |
|
679 | if err[0] == errno.EINTR: | |
680 | continue |
|
680 | continue | |
681 | raise |
|
681 | raise | |
682 | if events: |
|
682 | if events: | |
683 | for fd, event in events: |
|
683 | for fd, event in events: | |
684 | self.table[fd].handle_event(fd, event) |
|
684 | self.table[fd].handle_event(fd, event) | |
685 | elif timeobj: |
|
685 | elif timeobj: | |
686 | timeobj.handle_timeout() |
|
686 | timeobj.handle_timeout() | |
687 |
|
687 | |||
688 | def start(ui, repo): |
|
688 | def start(ui, repo): | |
689 | m = Master(ui, repo) |
|
689 | m = Master(ui, repo) | |
690 | sys.stdout.flush() |
|
690 | sys.stdout.flush() | |
691 | sys.stderr.flush() |
|
691 | sys.stderr.flush() | |
692 |
|
692 | |||
693 | pid = os.fork() |
|
693 | pid = os.fork() | |
694 | if pid: |
|
694 | if pid: | |
695 | return pid |
|
695 | return pid | |
696 |
|
696 | |||
697 | os.setsid() |
|
697 | os.setsid() | |
698 |
|
698 | |||
699 | fd = os.open('/dev/null', os.O_RDONLY) |
|
699 | fd = os.open('/dev/null', os.O_RDONLY) | |
700 | os.dup2(fd, 0) |
|
700 | os.dup2(fd, 0) | |
701 | if fd > 0: |
|
701 | if fd > 0: | |
702 | os.close(fd) |
|
702 | os.close(fd) | |
703 |
|
703 | |||
704 | fd = os.open(ui.config('inotify', 'log', '/dev/null'), |
|
704 | fd = os.open(ui.config('inotify', 'log', '/dev/null'), | |
705 | os.O_RDWR | os.O_CREAT | os.O_TRUNC) |
|
705 | os.O_RDWR | os.O_CREAT | os.O_TRUNC) | |
706 | os.dup2(fd, 1) |
|
706 | os.dup2(fd, 1) | |
707 | os.dup2(fd, 2) |
|
707 | os.dup2(fd, 2) | |
708 | if fd > 2: |
|
708 | if fd > 2: | |
709 | os.close(fd) |
|
709 | os.close(fd) | |
710 |
|
710 | |||
711 | try: |
|
711 | try: | |
712 | m.run() |
|
712 | m.run() | |
713 | finally: |
|
713 | finally: | |
714 | m.shutdown() |
|
714 | m.shutdown() | |
715 | os._exit(0) |
|
715 | os._exit(0) |
General Comments 0
You need to be logged in to leave comments.
Login now