server.py
492 lines
| 15.3 KiB
| text/x-python
|
PythonLexer
Nicolas Dumazet
|
r9933 | # server.py - common entry point for inotify status server | ||
Bryan O'Sullivan
|
r6239 | # | ||
Nicolas Dumazet
|
r9933 | # Copyright 2009 Nicolas Dumazet <nicdumz@gmail.com> | ||
Bryan O'Sullivan
|
r6239 | # | ||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Bryan O'Sullivan
|
r6239 | |||
Martin Geisler
|
r6961 | from mercurial.i18n import _ | ||
Nicolas Dumazet
|
r9514 | from mercurial import cmdutil, osutil, util | ||
Bryan O'Sullivan
|
r6239 | import common | ||
Nicolas Dumazet
|
r9933 | import errno | ||
import os | ||||
import socket | ||||
import stat | ||||
import struct | ||||
import sys | ||||
import tempfile | ||||
Bryan O'Sullivan
|
r6239 | |||
Matt Mackall
|
r10282 | class AlreadyStartedException(Exception): | ||
pass | ||||
Benoit Boissinot
|
r10494 | class TimeoutException(Exception): | ||
pass | ||||
Bryan O'Sullivan
|
r6239 | |||
def join(a, b): | ||||
if a: | ||||
if a[-1] == '/': | ||||
return a + b | ||||
return a + '/' + b | ||||
return b | ||||
Nicolas Dumazet
|
r8787 | def split(path): | ||
c = path.rfind('/') | ||||
if c == -1: | ||||
return '', path | ||||
Matt Mackall
|
r10282 | return path[:c], path[c + 1:] | ||
Nicolas Dumazet
|
r8787 | |||
Bryan O'Sullivan
|
r6239 | walk_ignored_errors = (errno.ENOENT, errno.ENAMETOOLONG) | ||
Nicolas Dumazet
|
r9350 | def walk(dirstate, absroot, root): | ||
Bryan O'Sullivan
|
r6239 | '''Like os.walk, but only yields regular files.''' | ||
# This function is critical to performance during startup. | ||||
def walkit(root, reporoot): | ||||
files, dirs = [], [] | ||||
try: | ||||
Nicolas Dumazet
|
r9350 | fullpath = join(absroot, root) | ||
Bryan O'Sullivan
|
r6239 | for name, kind in osutil.listdir(fullpath): | ||
if kind == stat.S_IFDIR: | ||||
if name == '.hg': | ||||
Nicolas Dumazet
|
r8325 | if not reporoot: | ||
Nicolas Dumazet
|
r8323 | return | ||
Nicolas Dumazet
|
r8325 | else: | ||
dirs.append(name) | ||||
Nicolas Dumazet
|
r8381 | path = join(root, name) | ||
Nicolas Dumazet
|
r9350 | if dirstate._ignore(path): | ||
Nicolas Dumazet
|
r8381 | continue | ||
for result in walkit(path, False): | ||||
yield result | ||||
Bryan O'Sullivan
|
r6239 | elif kind in (stat.S_IFREG, stat.S_IFLNK): | ||
Nicolas Dumazet
|
r8334 | files.append(name) | ||
Nicolas Dumazet
|
r8324 | yield fullpath, dirs, files | ||
Bryan O'Sullivan
|
r6239 | |||
except OSError, err: | ||||
Nicolas Dumazet
|
r9116 | if err.errno == errno.ENOTDIR: | ||
# fullpath was a directory, but has since been replaced | ||||
# by a file. | ||||
yield fullpath, dirs, files | ||||
elif err.errno not in walk_ignored_errors: | ||||
Bryan O'Sullivan
|
r6239 | raise | ||
Nicolas Dumazet
|
r8320 | |||
return walkit(root, root == '') | ||||
Bryan O'Sullivan
|
r6239 | |||
Nicolas Dumazet
|
r9115 | class directory(object): | ||
""" | ||||
Representing a directory | ||||
* path is the relative path from repo root to this directory | ||||
* files is a dict listing the files in this directory | ||||
- keys are file names | ||||
- values are file status | ||||
* dirs is a dict listing the subdirectories | ||||
- key are subdirectories names | ||||
- values are directory objects | ||||
""" | ||||
def __init__(self, relpath=''): | ||||
self.path = relpath | ||||
self.files = {} | ||||
self.dirs = {} | ||||
def dir(self, relpath): | ||||
""" | ||||
Returns the directory contained at the relative path relpath. | ||||
Creates the intermediate directories if necessary. | ||||
""" | ||||
if not relpath: | ||||
return self | ||||
l = relpath.split('/') | ||||
ret = self | ||||
while l: | ||||
next = l.pop(0) | ||||
try: | ||||
ret = ret.dirs[next] | ||||
except KeyError: | ||||
d = directory(join(ret.path, next)) | ||||
ret.dirs[next] = d | ||||
ret = d | ||||
return ret | ||||
Nicolas Dumazet
|
r9854 | def walk(self, states, visited=None): | ||
Nicolas Dumazet
|
r9115 | """ | ||
yield (filename, status) pairs for items in the trees | ||||
that have status in states. | ||||
filenames are relative to the repo root | ||||
""" | ||||
for file, st in self.files.iteritems(): | ||||
if st in states: | ||||
yield join(self.path, file), st | ||||
for dir in self.dirs.itervalues(): | ||||
Nicolas Dumazet
|
r9854 | if visited is not None: | ||
visited.add(dir.path) | ||||
Nicolas Dumazet
|
r9115 | for e in dir.walk(states): | ||
yield e | ||||
Nicolas Dumazet
|
r9854 | def lookup(self, states, path, visited): | ||
Nicolas Dumazet
|
r9115 | """ | ||
yield root-relative filenames that match path, and whose | ||||
status are in states: | ||||
* if path is a file, yield path | ||||
* if path is a directory, yield directory files | ||||
* if path is not tracked, yield nothing | ||||
""" | ||||
if path[-1] == '/': | ||||
path = path[:-1] | ||||
paths = path.split('/') | ||||
# we need to check separately for last node | ||||
last = paths.pop() | ||||
tree = self | ||||
try: | ||||
for dir in paths: | ||||
tree = tree.dirs[dir] | ||||
except KeyError: | ||||
# path is not tracked | ||||
Nicolas Dumazet
|
r9854 | visited.add(tree.path) | ||
Nicolas Dumazet
|
r9115 | return | ||
try: | ||||
# if path is a directory, walk it | ||||
Nicolas Dumazet
|
r9854 | target = tree.dirs[last] | ||
visited.add(target.path) | ||||
for file, st in target.walk(states, visited): | ||||
Nicolas Dumazet
|
r9115 | yield file | ||
except KeyError: | ||||
try: | ||||
if tree.files[last] in states: | ||||
# path is a file | ||||
Nicolas Dumazet
|
r9854 | visited.add(tree.path) | ||
Nicolas Dumazet
|
r9115 | yield path | ||
except KeyError: | ||||
# path is not tracked | ||||
pass | ||||
Nicolas Dumazet
|
r9933 | class repowatcher(object): | ||
Nicolas Dumazet
|
r8610 | """ | ||
Watches inotify events | ||||
""" | ||||
Bryan O'Sullivan
|
r6239 | statuskeys = 'almr!?' | ||
Nicolas Dumazet
|
r9350 | def __init__(self, ui, dirstate, root): | ||
Bryan O'Sullivan
|
r6239 | self.ui = ui | ||
Nicolas Dumazet
|
r9350 | self.dirstate = dirstate | ||
self.wprefix = join(root, '') | ||||
Nicolas Dumazet
|
r9349 | self.prefixlen = len(self.wprefix) | ||
Bryan O'Sullivan
|
r6239 | |||
Nicolas Dumazet
|
r9115 | self.tree = directory() | ||
Bryan O'Sullivan
|
r6239 | self.statcache = {} | ||
Nicolas Dumazet
|
r9115 | self.statustrees = dict([(s, directory()) for s in self.statuskeys]) | ||
Bryan O'Sullivan
|
r6239 | |||
Nicolas Dumazet
|
r9933 | self.ds_info = self.dirstate_info() | ||
Bryan O'Sullivan
|
r6239 | self.last_event = None | ||
Nicolas Dumazet
|
r9933 | def handle_timeout(self): | ||
pass | ||||
Bryan O'Sullivan
|
r6239 | |||
def dirstate_info(self): | ||||
try: | ||||
Nicolas Dumazet
|
r9350 | st = os.lstat(self.wprefix + '.hg/dirstate') | ||
Bryan O'Sullivan
|
r6239 | return st.st_mtime, st.st_ino | ||
except OSError, err: | ||||
if err.errno != errno.ENOENT: | ||||
raise | ||||
return 0, 0 | ||||
def filestatus(self, fn, st): | ||||
try: | ||||
Nicolas Dumazet
|
r9350 | type_, mode, size, time = self.dirstate._map[fn][:4] | ||
Bryan O'Sullivan
|
r6239 | except KeyError: | ||
type_ = '?' | ||||
if type_ == 'n': | ||||
st_mode, st_size, st_mtime = st | ||||
Matt Mackall
|
r7082 | if size == -1: | ||
return 'l' | ||||
Bryan O'Sullivan
|
r6239 | if size and (size != st_size or (mode ^ st_mode) & 0100): | ||
return 'm' | ||||
if time != int(st_mtime): | ||||
return 'l' | ||||
return 'n' | ||||
Renato Cunha
|
r11545 | if type_ == '?' and self.dirstate._dirignore(fn): | ||
# we must check not only if the file is ignored, but if any part | ||||
# of its path match an ignore pattern | ||||
Bryan O'Sullivan
|
r6239 | return 'i' | ||
return type_ | ||||
Nicolas Dumazet
|
r8599 | def updatefile(self, wfn, osstat): | ||
''' | ||||
update the file entry of an existing file. | ||||
osstat: (mode, size, time) tuple, as returned by os.lstat(wfn) | ||||
''' | ||||
self._updatestatus(wfn, self.filestatus(wfn, osstat)) | ||||
def deletefile(self, wfn, oldstatus): | ||||
''' | ||||
update the entry of a file which has been deleted. | ||||
oldstatus: char in statuskeys, status of the file before deletion | ||||
''' | ||||
if oldstatus == 'r': | ||||
newstatus = 'r' | ||||
elif oldstatus in 'almn': | ||||
newstatus = '!' | ||||
else: | ||||
newstatus = None | ||||
self.statcache.pop(wfn, None) | ||||
self._updatestatus(wfn, newstatus) | ||||
def _updatestatus(self, wfn, newstatus): | ||||
Nicolas Dumazet
|
r8382 | ''' | ||
Nicolas Dumazet
|
r9115 | Update the stored status of a file. | ||
Nicolas Dumazet
|
r8382 | |||
Nicolas Dumazet
|
r8599 | newstatus: - char in (statuskeys + 'ni'), new status to apply. | ||
- or None, to stop tracking wfn | ||||
Nicolas Dumazet
|
r8382 | ''' | ||
Nicolas Dumazet
|
r8787 | root, fn = split(wfn) | ||
Nicolas Dumazet
|
r9115 | d = self.tree.dir(root) | ||
Nicolas Dumazet
|
r8599 | |||
Nicolas Dumazet
|
r9115 | oldstatus = d.files.get(fn) | ||
Nicolas Dumazet
|
r8599 | # oldstatus can be either: | ||
# - None : fn is new | ||||
# - a char in statuskeys: fn is a (tracked) file | ||||
Nicolas Dumazet
|
r8382 | if self.ui.debugflag and oldstatus != newstatus: | ||
Nicolas Dumazet
|
r9115 | self.ui.note(_('status: %r %s -> %s\n') % | ||
Nicolas Dumazet
|
r8382 | (wfn, oldstatus, newstatus)) | ||
Nicolas Dumazet
|
r9115 | |||
if oldstatus and oldstatus in self.statuskeys \ | ||||
and oldstatus != newstatus: | ||||
del self.statustrees[oldstatus].dir(root).files[fn] | ||||
Nicolas Dumazet
|
r9348 | |||
if newstatus in (None, 'i'): | ||||
d.files.pop(fn, None) | ||||
elif oldstatus != newstatus: | ||||
Nicolas Dumazet
|
r9115 | d.files[fn] = newstatus | ||
Nicolas Dumazet
|
r9348 | if newstatus != 'n': | ||
self.statustrees[newstatus].dir(root).files[fn] = newstatus | ||||
Nicolas Dumazet
|
r7892 | |||
Bryan O'Sullivan
|
r6239 | def check_deleted(self, key): | ||
# Files that had been deleted but were present in the dirstate | ||||
# may have vanished from the dirstate; we must clean them up. | ||||
nuke = [] | ||||
Nicolas Dumazet
|
r9115 | for wfn, ignore in self.statustrees[key].walk(key): | ||
Nicolas Dumazet
|
r9350 | if wfn not in self.dirstate: | ||
Bryan O'Sullivan
|
r6239 | nuke.append(wfn) | ||
for wfn in nuke: | ||||
Nicolas Dumazet
|
r8787 | root, fn = split(wfn) | ||
Nicolas Dumazet
|
r9115 | del self.statustrees[key].dir(root).files[fn] | ||
del self.tree.dir(root).files[fn] | ||||
Thomas Arendsen Hein
|
r6287 | |||
Bryan O'Sullivan
|
r6239 | def update_hgignore(self): | ||
# An update of the ignore file can potentially change the | ||||
# states of all unknown and ignored files. | ||||
# XXX If the user has other ignore files outside the repo, or | ||||
# changes their list of ignore files at run time, we'll | ||||
# potentially never see changes to them. We could get the | ||||
# client to report to us what ignore data they're using. | ||||
# But it's easier to do nothing than to open that can of | ||||
# worms. | ||||
Nicolas Dumazet
|
r9350 | if '_ignore' in self.dirstate.__dict__: | ||
delattr(self.dirstate, '_ignore') | ||||
Martin Geisler
|
r6961 | self.ui.note(_('rescanning due to .hgignore change\n')) | ||
Nicolas Dumazet
|
r8604 | self.handle_timeout() | ||
Bryan O'Sullivan
|
r6239 | self.scan() | ||
Thomas Arendsen Hein
|
r6287 | |||
Bryan O'Sullivan
|
r6239 | def getstat(self, wpath): | ||
try: | ||||
return self.statcache[wpath] | ||||
except KeyError: | ||||
try: | ||||
return self.stat(wpath) | ||||
except OSError, err: | ||||
if err.errno != errno.ENOENT: | ||||
raise | ||||
Thomas Arendsen Hein
|
r6287 | |||
Bryan O'Sullivan
|
r6239 | def stat(self, wpath): | ||
try: | ||||
st = os.lstat(join(self.wprefix, wpath)) | ||||
ret = st.st_mode, st.st_size, st.st_mtime | ||||
self.statcache[wpath] = ret | ||||
return ret | ||||
Benoit Boissinot
|
r7280 | except OSError: | ||
Bryan O'Sullivan
|
r6239 | self.statcache.pop(wpath, None) | ||
raise | ||||
Thomas Arendsen Hein
|
r6287 | |||
Nicolas Dumazet
|
r9933 | class socketlistener(object): | ||
Nicolas Dumazet
|
r8610 | """ | ||
Listens for client queries on unix socket inotify.sock | ||||
""" | ||||
Nicolas Dumazet
|
r9350 | def __init__(self, ui, root, repowatcher, timeout): | ||
Bryan O'Sullivan
|
r6239 | self.ui = ui | ||
Nicolas Dumazet
|
r8335 | self.repowatcher = repowatcher | ||
Bryan O'Sullivan
|
r6239 | self.sock = socket.socket(socket.AF_UNIX) | ||
Nicolas Dumazet
|
r9350 | self.sockpath = join(root, '.hg/inotify.sock') | ||
Nicolas Dumazet
|
r12650 | |||
self.realsockpath = self.sockpath | ||||
if os.path.islink(self.sockpath): | ||||
if os.path.exists(self.sockpath): | ||||
self.realsockpath = os.readlink(self.sockpath) | ||||
else: | ||||
raise util.Abort('inotify-server: cannot start: ' | ||||
'.hg/inotify.sock is a broken symlink') | ||||
Bryan O'Sullivan
|
r6239 | try: | ||
Nicolas Dumazet
|
r12650 | self.sock.bind(self.realsockpath) | ||
Bryan O'Sullivan
|
r6239 | except socket.error, err: | ||
Renato Cunha
|
r11567 | if err.args[0] == errno.EADDRINUSE: | ||
Matt Mackall
|
r10282 | raise AlreadyStartedException(_('cannot start: socket is ' | ||
'already bound')) | ||||
Renato Cunha
|
r11567 | if err.args[0] == "AF_UNIX path too long": | ||
Benoit Boissinot
|
r6997 | tempdir = tempfile.mkdtemp(prefix="hg-inotify-") | ||
self.realsockpath = os.path.join(tempdir, "inotify.sock") | ||||
try: | ||||
self.sock.bind(self.realsockpath) | ||||
os.symlink(self.realsockpath, self.sockpath) | ||||
except (OSError, socket.error), inst: | ||||
try: | ||||
os.unlink(self.realsockpath) | ||||
except: | ||||
pass | ||||
os.rmdir(tempdir) | ||||
if inst.errno == errno.EEXIST: | ||||
Nicolas Dumazet
|
r9900 | raise AlreadyStartedException(_('cannot start: tried ' | ||
'linking .hg/inotify.sock to a temporary socket but' | ||||
' .hg/inotify.sock already exists')) | ||||
Benoit Boissinot
|
r6997 | raise | ||
else: | ||||
raise | ||||
Bryan O'Sullivan
|
r6239 | self.sock.listen(5) | ||
self.fileno = self.sock.fileno | ||||
Nicolas Dumazet
|
r8554 | def answer_stat_query(self, cs): | ||
Bryan O'Sullivan
|
r6239 | names = cs.read().split('\0') | ||
Thomas Arendsen Hein
|
r6287 | |||
Bryan O'Sullivan
|
r6239 | states = names.pop() | ||
self.ui.note(_('answering query for %r\n') % states) | ||||
Nicolas Dumazet
|
r9854 | visited = set() | ||
Bryan O'Sullivan
|
r6239 | if not names: | ||
def genresult(states, tree): | ||||
Nicolas Dumazet
|
r9115 | for fn, state in tree.walk(states): | ||
Bryan O'Sullivan
|
r6239 | yield fn | ||
else: | ||||
def genresult(states, tree): | ||||
for fn in names: | ||||
Nicolas Dumazet
|
r9854 | for f in tree.lookup(states, fn, visited): | ||
Nicolas Dumazet
|
r9115 | yield f | ||
Bryan O'Sullivan
|
r6239 | |||
Nicolas Dumazet
|
r8554 | return ['\0'.join(r) for r in [ | ||
Nicolas Dumazet
|
r8335 | genresult('l', self.repowatcher.statustrees['l']), | ||
genresult('m', self.repowatcher.statustrees['m']), | ||||
genresult('a', self.repowatcher.statustrees['a']), | ||||
genresult('r', self.repowatcher.statustrees['r']), | ||||
genresult('!', self.repowatcher.statustrees['!']), | ||||
'?' in states | ||||
and genresult('?', self.repowatcher.statustrees['?']) | ||||
or [], | ||||
Bryan O'Sullivan
|
r6239 | [], | ||
Nicolas Dumazet
|
r8335 | 'c' in states and genresult('n', self.repowatcher.tree) or [], | ||
Nicolas Dumazet
|
r9854 | visited | ||
Bryan O'Sullivan
|
r6239 | ]] | ||
Nicolas Dumazet
|
r8555 | def answer_dbug_query(self): | ||
return ['\0'.join(self.repowatcher.debug())] | ||||
Nicolas Dumazet
|
r9933 | def accept_connection(self): | ||
Nicolas Dumazet
|
r8554 | sock, addr = self.sock.accept() | ||
cs = common.recvcs(sock) | ||||
version = ord(cs.read(1)) | ||||
if version != common.version: | ||||
self.ui.warn(_('received query from incompatible client ' | ||||
'version %d\n') % version) | ||||
Simon Heimberg
|
r8952 | try: | ||
# try to send back our version to the client | ||||
# this way, the client too is informed of the mismatch | ||||
sock.sendall(chr(common.version)) | ||||
except: | ||||
pass | ||||
Nicolas Dumazet
|
r8554 | return | ||
type = cs.read(4) | ||||
if type == 'STAT': | ||||
results = self.answer_stat_query(cs) | ||||
Nicolas Dumazet
|
r8555 | elif type == 'DBUG': | ||
results = self.answer_dbug_query() | ||||
Nicolas Dumazet
|
r8554 | else: | ||
self.ui.warn(_('unrecognized query type: %s\n') % type) | ||||
return | ||||
Bryan O'Sullivan
|
r6239 | try: | ||
try: | ||||
Nicolas Dumazet
|
r8386 | v = chr(common.version) | ||
Nicolas Dumazet
|
r8553 | sock.sendall(v + type + struct.pack(common.resphdrfmts[type], | ||
*map(len, results))) | ||||
Bryan O'Sullivan
|
r6239 | sock.sendall(''.join(results)) | ||
finally: | ||||
sock.shutdown(socket.SHUT_WR) | ||||
except socket.error, err: | ||||
Renato Cunha
|
r11567 | if err.args[0] != errno.EPIPE: | ||
Bryan O'Sullivan
|
r6239 | raise | ||
Nikolaj Sjujskij
|
r15151 | if sys.platform.startswith('linux'): | ||
Nicolas Dumazet
|
r9933 | import linuxserver as _server | ||
else: | ||||
raise ImportError | ||||
Bryan O'Sullivan
|
r6239 | |||
Nicolas Dumazet
|
r9933 | master = _server.master | ||
Bryan O'Sullivan
|
r6239 | |||
Nicolas Dumazet
|
r9514 | def start(ui, dirstate, root, opts): | ||
Benoit Boissinot
|
r10494 | timeout = opts.get('idle_timeout') | ||
Nicolas Dumazet
|
r9514 | if timeout: | ||
Benoit Boissinot
|
r10494 | timeout = float(timeout) * 60000 | ||
else: | ||||
timeout = None | ||||
Nicolas Dumazet
|
r9514 | |||
class service(object): | ||||
def init(self): | ||||
Brendan Cully
|
r7451 | try: | ||
Nicolas Dumazet
|
r9514 | self.master = master(ui, dirstate, root, timeout) | ||
except AlreadyStartedException, inst: | ||||
Nicolas Dumazet
|
r9900 | raise util.Abort("inotify-server: %s" % inst) | ||
Bryan O'Sullivan
|
r6239 | |||
Nicolas Dumazet
|
r9514 | def run(self): | ||
try: | ||||
Benoit Boissinot
|
r10494 | try: | ||
self.master.run() | ||||
except TimeoutException: | ||||
pass | ||||
Nicolas Dumazet
|
r9514 | finally: | ||
self.master.shutdown() | ||||
Bryan O'Sullivan
|
r6239 | |||
Nicolas Dumazet
|
r9514 | if 'inserve' not in sys.argv: | ||
Patrick Mezard
|
r10239 | runargs = util.hgcmd() + ['inserve', '-R', root] | ||
Nicolas Dumazet
|
r9897 | else: | ||
Patrick Mezard
|
r10239 | runargs = util.hgcmd() + sys.argv[1:] | ||
Nicolas Dumazet
|
r9897 | |||
pidfile = ui.config('inotify', 'pidfile') | ||||
if opts['daemon'] and pidfile is not None and 'pid-file' not in runargs: | ||||
runargs.append("--pid-file=%s" % pidfile) | ||||
Bryan O'Sullivan
|
r6239 | |||
Nicolas Dumazet
|
r9514 | service = service() | ||
logfile = ui.config('inotify', 'log') | ||||
Nicolas Dumazet
|
r10013 | |||
appendpid = ui.configbool('inotify', 'appendpid', False) | ||||
Greg Ward
|
r11943 | ui.debug('starting inotify server: %s\n' % ' '.join(runargs)) | ||
Nicolas Dumazet
|
r9514 | cmdutil.service(opts, initfn=service.init, runfn=service.run, | ||
Nicolas Dumazet
|
r10013 | logfile=logfile, runargs=runargs, appendpid=appendpid) | ||