From dc311096770c9ab16aaaf326af8799ff5b4a2ea4 Mon Sep 17 00:00:00 2001 From: Johannes Berg Date: Sat, 30 May 2020 21:10:02 +0200 Subject: [PATCH] bup: add own gethostname() wrapper This is necessary because python3 insists that hostnames should be utf-8, which is a rather questionable assumption. Signed-off-by: Johannes Berg Reviewed-by: Rob Browning [rlb@defaultvalue.org: don't define HOST_NAME_MAX if it's not already] Signed-off-by: Rob Browning Tested-by: Rob Browning --- lib/bup/_helpers.c | 16 ++++++++++++++++ lib/bup/helpers.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/bup/_helpers.c b/lib/bup/_helpers.c index 4a6aaf5..946ff4a 100644 --- a/lib/bup/_helpers.c +++ b/lib/bup/_helpers.c @@ -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 }; diff --git a/lib/bup/helpers.py b/lib/bup/helpers.py index 5b0c458..788efb0 100644 --- a/lib/bup/helpers.py +++ b/lib/bup/helpers.py @@ -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 -- 2.39.2