浏览代码

Merge pull request #3484 from ThomasWaldmann/fix-getfqdn

fix getfqdn
TW 7 年之前
父节点
当前提交
f83e5b7f80

+ 1 - 1
src/borg/helpers/checks.py

@@ -28,7 +28,7 @@ def check_extension_modules():
         raise ExtensionModuleError
         raise ExtensionModuleError
     if borg.crypto.low_level.API_VERSION != '1.1_02':
     if borg.crypto.low_level.API_VERSION != '1.1_02':
         raise ExtensionModuleError
         raise ExtensionModuleError
-    if platform.API_VERSION != platform.OS_API_VERSION != '1.1_02':
+    if platform.API_VERSION != platform.OS_API_VERSION != '1.1_03':
         raise ExtensionModuleError
         raise ExtensionModuleError
     if item.API_VERSION != '1.1_03':
     if item.API_VERSION != '1.1_03':
         raise ExtensionModuleError
         raise ExtensionModuleError

+ 1 - 1
src/borg/helpers/parseformat.py

@@ -179,8 +179,8 @@ def format_line(format, data):
 
 
 def replace_placeholders(text):
 def replace_placeholders(text):
     """Replace placeholders in text with their values."""
     """Replace placeholders in text with their values."""
+    from ..platform import fqdn
     current_time = datetime.now()
     current_time = datetime.now()
-    fqdn = socket.getfqdn()
     data = {
     data = {
         'pid': os.getpid(),
         'pid': os.getpid(),
         'fqdn': fqdn,
         'fqdn': fqdn,

+ 2 - 2
src/borg/platform/__init__.py

@@ -10,12 +10,12 @@ from .base import acl_get, acl_set
 from .base import set_flags, get_flags
 from .base import set_flags, get_flags
 from .base import SaveFile, SyncFile, sync_dir, fdatasync, safe_fadvise
 from .base import SaveFile, SyncFile, sync_dir, fdatasync, safe_fadvise
 from .base import swidth, API_VERSION
 from .base import swidth, API_VERSION
-from .base import process_alive, get_process_id, local_pid_alive
+from .base import process_alive, get_process_id, local_pid_alive, fqdn, hostname, hostid
 
 
 OS_API_VERSION = API_VERSION
 OS_API_VERSION = API_VERSION
 
 
 if not sys.platform.startswith(('win32', )):
 if not sys.platform.startswith(('win32', )):
-    from .posix import process_alive, get_process_id, local_pid_alive
+    from .posix import process_alive, local_pid_alive
 
 
 if sys.platform.startswith('linux'):  # pragma: linux only
 if sys.platform.startswith('linux'):  # pragma: linux only
     from .linux import API_VERSION as OS_API_VERSION
     from .linux import API_VERSION as OS_API_VERSION

+ 38 - 4
src/borg/platform/base.py

@@ -1,5 +1,7 @@
 import errno
 import errno
 import os
 import os
+import socket
+import uuid
 
 
 from borg.helpers import truncate_and_unlink
 from borg.helpers import truncate_and_unlink
 
 
@@ -15,7 +17,7 @@ platform API: that way platform APIs provided by the platform-specific support m
 are correctly composed into the base functionality.
 are correctly composed into the base functionality.
 """
 """
 
 
-API_VERSION = '1.1_02'
+API_VERSION = '1.1_03'
 
 
 fdatasync = getattr(os, 'fdatasync', os.fsync)
 fdatasync = getattr(os, 'fdatasync', os.fsync)
 
 
@@ -183,12 +185,44 @@ def swidth(s):
     return len(s)
     return len(s)
 
 
 
 
+# patched socket.getfqdn() - see https://bugs.python.org/issue5004
+def getfqdn(name=''):
+    """Get fully qualified domain name from name.
+
+    An empty argument is interpreted as meaning the local host.
+    """
+    name = name.strip()
+    if not name or name == '0.0.0.0':
+        name = socket.gethostname()
+    try:
+        addrs = socket.getaddrinfo(name, None, 0, socket.SOCK_DGRAM, 0, socket.AI_CANONNAME)
+    except socket.error:
+        pass
+    else:
+        for addr in addrs:
+            if addr[3]:
+                name = addr[3]
+                break
+    return name
+
+
+# for performance reasons, only determine hostname / fqdn / hostid once.
+# XXX this sometimes requires live internet access for issuing a DNS query in the background.
+hostname = socket.gethostname()
+fqdn = getfqdn(hostname)
+hostid = '%s@%s' % (fqdn, uuid.getnode())
+
+
 def get_process_id():
 def get_process_id():
     """
     """
-    Return identification tuple (hostname, pid, thread_id) for 'us'. If this is a FUSE process, then the PID will be
-    that of the parent, not the forked FUSE child.
+    Return identification tuple (hostname, pid, thread_id) for 'us'.
+    This always returns the current pid, which might be different from before, e.g. if daemonize() was used.
+
+    Note: Currently thread_id is *always* zero.
     """
     """
-    raise NotImplementedError
+    thread_id = 0
+    pid = os.getpid()
+    return hostid, pid, thread_id
 
 
 
 
 def process_alive(host, pid, thread):
 def process_alive(host, pid, thread):

+ 1 - 1
src/borg/platform/darwin.pyx

@@ -4,7 +4,7 @@ from ..helpers import user2uid, group2gid
 from ..helpers import safe_decode, safe_encode
 from ..helpers import safe_decode, safe_encode
 from .posix import swidth
 from .posix import swidth
 
 
-API_VERSION = '1.1_02'
+API_VERSION = '1.1_03'
 
 
 cdef extern from "sys/acl.h":
 cdef extern from "sys/acl.h":
     ctypedef struct _acl_t:
     ctypedef struct _acl_t:

+ 1 - 1
src/borg/platform/freebsd.pyx

@@ -4,7 +4,7 @@ from ..helpers import posix_acl_use_stored_uid_gid
 from ..helpers import safe_encode, safe_decode
 from ..helpers import safe_encode, safe_decode
 from .posix import swidth
 from .posix import swidth
 
 
-API_VERSION = '1.1_02'
+API_VERSION = '1.1_03'
 
 
 cdef extern from "errno.h":
 cdef extern from "errno.h":
     int errno
     int errno

+ 1 - 1
src/borg/platform/linux.pyx

@@ -13,7 +13,7 @@ from .posix import swidth
 from libc cimport errno
 from libc cimport errno
 from libc.stdint cimport int64_t
 from libc.stdint cimport int64_t
 
 
-API_VERSION = '1.1_02'
+API_VERSION = '1.1_03'
 
 
 cdef extern from "sys/types.h":
 cdef extern from "sys/types.h":
     int ACL_TYPE_ACCESS
     int ACL_TYPE_ACCESS

+ 2 - 21
src/borg/platform/posix.pyx

@@ -1,8 +1,5 @@
 import errno
 import errno
 import os
 import os
-import uuid
-import socket
-import subprocess
 
 
 
 
 cdef extern from "wchar.h":
 cdef extern from "wchar.h":
@@ -18,23 +15,6 @@ def swidth(s):
         return str_len
         return str_len
 
 
 
 
-# for performance reasons, only determine the hostname once.
-# XXX this sometimes requires live internet access for issuing a DNS query in the background.
-_hostname = '%s@%s' % (socket.getfqdn(), uuid.getnode())
-
-
-def get_process_id():
-    """
-    Return identification tuple (hostname, pid, thread_id) for 'us'.
-    This always returns the current pid, which might be different from before, e.g. if daemonize() was used.
-
-    Note: Currently thread_id is *always* zero.
-    """
-    thread_id = 0
-    pid = os.getpid()
-    return _hostname, pid, thread_id
-
-
 def process_alive(host, pid, thread):
 def process_alive(host, pid, thread):
     """
     """
     Check if the (host, pid, thread_id) combination corresponds to a potentially alive process.
     Check if the (host, pid, thread_id) combination corresponds to a potentially alive process.
@@ -43,8 +23,9 @@ def process_alive(host, pid, thread):
     returns always True, since there is no real way to check.
     returns always True, since there is no real way to check.
     """
     """
     from . import local_pid_alive
     from . import local_pid_alive
+    from . import hostid
 
 
-    if host != _hostname:
+    if host != hostid:
         return True
         return True
 
 
     if thread != 0:
     if thread != 0: