]> arthur.barton.de Git - bup.git/commitdiff
bup: add own gethostname() wrapper
authorJohannes Berg <johannes@sipsolutions.net>
Sat, 30 May 2020 19:10:02 +0000 (21:10 +0200)
committerRob Browning <rlb@defaultvalue.org>
Fri, 19 Jun 2020 22:50:38 +0000 (17:50 -0500)
This is necessary because python3 insists that hostnames should
be utf-8, which is a rather questionable assumption.

Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Reviewed-by: Rob Browning <rlb@defaultvalue.org>
[rlb@defaultvalue.org: don't define HOST_NAME_MAX if it's not already]
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/_helpers.c
lib/bup/helpers.py

index 4a6aaf592986cd63f0fed918e2b646af258dddf6..946ff4ad63763ba61847d44e5092502070a1eb05 100644 (file)
@@ -1862,6 +1862,20 @@ static PyObject *bup_getgrnam(PyObject *self, PyObject *args)
     return result;
 }
 
+static PyObject *bup_gethostname(PyObject *mod, PyObject *ignore)
+{
+#ifdef HOST_NAME_MAX
+    char buf[HOST_NAME_MAX + 1] = {};
+#else
+    /* 'SUSv2 guarantees that "Host names are limited to 255 bytes".' */
+    char buf[256] = {};
+#endif
+
+    if (gethostname(buf, sizeof(buf) - 1))
+        return PyErr_SetFromErrno(PyExc_IOError);
+    return PyBytes_FromString(buf);
+}
+
 static PyMethodDef helper_methods[] = {
     { "write_sparsely", bup_write_sparsely, METH_VARARGS,
       "Write buf excepting zeros at the end. Return trailing zero count." },
@@ -1949,6 +1963,8 @@ static PyMethodDef helper_methods[] = {
       "Return the group database entry for the given group name,"
       " as a tuple with all C strings as bytes(), or None if the group does"
       " not exist." },
+    { "gethostname", bup_gethostname, METH_NOARGS,
+      "Return the current hostname (as bytes)" },
     { NULL, NULL, 0, NULL },  // sentinel
 };
 
index 5b0c458f0a8a3abcaf748856a5f89660a90eb971..788efb07a9c4ffb30a414a4cbf6dd211104748ae 100644 (file)
@@ -434,7 +434,7 @@ def hostname():
     """Get the FQDN of this machine."""
     global _hostname
     if not _hostname:
-        _hostname = socket.getfqdn().encode('iso-8859-1')
+        _hostname = _helpers.gethostname()
     return _hostname