Patchwork [2,of,4] chgserver: extract utility to bind unix domain socket to long path

login
register
mail settings
Submitter Yuya Nishihara
Date July 13, 2016, 12:09 p.m.
Message ID <5897fd557178c4b39349.1468411773@mimosa>
Download mbox | patch
Permalink /patch/15825/
State Accepted
Headers show

Comments

Yuya Nishihara - July 13, 2016, 12:09 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1463817124 -32400
#      Sat May 21 16:52:04 2016 +0900
# Node ID 5897fd557178c4b39349a0abd52a1ef5ade1702b
# Parent  4e4bf46112a5de1cccf339db1c9de4fb2f391085
chgserver: extract utility to bind unix domain socket to long path

This is common problem of using sockaddr_un.

Patch

diff --git a/hgext/chgserver.py b/hgext/chgserver.py
--- a/hgext/chgserver.py
+++ b/hgext/chgserver.py
@@ -578,18 +578,7 @@  class AutoExitMixIn:  # use old-style to
         # 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 --git a/mercurial/posix.py b/mercurial/posix.py
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -598,3 +598,18 @@  def readpipe(pipe):
         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 --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -70,6 +70,7 @@  else:
 
 _ = i18n._
 
+bindunixsocket = platform.bindunixsocket
 cachestat = platform.cachestat
 checkexec = platform.checkexec
 checklink = platform.checklink
diff --git a/mercurial/windows.py b/mercurial/windows.py
--- a/mercurial/windows.py
+++ b/mercurial/windows.py
@@ -471,3 +471,6 @@  def readpipe(pipe):
         chunks.append(s)
 
     return ''.join(chunks)
+
+def bindunixsocket(sock, path):
+    raise NotImplementedError('unsupported platform')