# HG changeset patch # User Yuya Nishihara # Date 1463817124 -32400 # Node ID 3239e2fdd2e2d794143556d69d871e7e33be1664 # Parent 02de1dbd4f6e07cd31b0f763ddc9d953147a29ed chgserver: extract utility to bind unix domain socket to long path This is common problem of using sockaddr_un. diff -r 02de1dbd4f6e -r 3239e2fdd2e2 hgext/chgserver.py --- a/hgext/chgserver.py Sat May 21 16:42:59 2016 +0900 +++ b/hgext/chgserver.py Sat May 21 16:52:04 2016 +0900 @@ -578,18 +578,7 @@ # use a unique temp address so we can stat the file and do ownership # check later tempaddress = _tempaddress(self.server_address) - # use relative path instead of full path at bind() if possible, since - # AF_UNIX path has very small length limit (107 chars) on common - # platforms (see sys/un.h) - dirname, basename = os.path.split(tempaddress) - bakwdfd = None - if dirname: - bakwdfd = os.open('.', os.O_DIRECTORY) - os.chdir(dirname) - self.socket.bind(basename) - if bakwdfd: - os.fchdir(bakwdfd) - os.close(bakwdfd) + util.bindunixsocket(self.socket, tempaddress) self._socketstat = os.stat(tempaddress) # rename will replace the old socket file if exists atomically. the # old server will detect ownership change and exit. diff -r 02de1dbd4f6e -r 3239e2fdd2e2 mercurial/posix.py --- a/mercurial/posix.py Sat May 21 16:42:59 2016 +0900 +++ b/mercurial/posix.py Sat May 21 16:52:04 2016 +0900 @@ -598,3 +598,18 @@ return ''.join(chunks) finally: fcntl.fcntl(pipe, fcntl.F_SETFL, oldflags) + +def bindunixsocket(sock, path): + """Bind the UNIX domain socket to the specified path""" + # use relative path instead of full path at bind() if possible, since + # AF_UNIX path has very small length limit (107 chars) on common + # platforms (see sys/un.h) + dirname, basename = os.path.split(path) + bakwdfd = None + if dirname: + bakwdfd = os.open('.', os.O_DIRECTORY) + os.chdir(dirname) + sock.bind(basename) + if bakwdfd: + os.fchdir(bakwdfd) + os.close(bakwdfd) diff -r 02de1dbd4f6e -r 3239e2fdd2e2 mercurial/util.py --- a/mercurial/util.py Sat May 21 16:42:59 2016 +0900 +++ b/mercurial/util.py Sat May 21 16:52:04 2016 +0900 @@ -70,6 +70,7 @@ _ = i18n._ +bindunixsocket = platform.bindunixsocket cachestat = platform.cachestat checkexec = platform.checkexec checklink = platform.checklink diff -r 02de1dbd4f6e -r 3239e2fdd2e2 mercurial/windows.py --- a/mercurial/windows.py Sat May 21 16:42:59 2016 +0900 +++ b/mercurial/windows.py Sat May 21 16:52:04 2016 +0900 @@ -471,3 +471,6 @@ chunks.append(s) return ''.join(chunks) + +def bindunixsocket(sock, path): + raise NotImplementedError('unsupported platform')