From be4b6c102bb40ff0e5d3266853ef9d3c333785e7 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Sat, 4 Dec 2010 06:13:16 -0800 Subject: [PATCH] cmd/memtest: stop using weird mmap() and /dev/urandom tricks. I'll just write a C function that can rapidly generate random sha1s. This should make it more portable, hopefully fixing a problem reported by Michael Budde on a Linux/SPARC system. Signed-off-by: Avery Pennarun --- cmd/memtest-cmd.py | 12 ++++-------- lib/bup/_helpers.c | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cmd/memtest-cmd.py b/cmd/memtest-cmd.py index 7d9eadc..fb202b7 100755 --- a/cmd/memtest-cmd.py +++ b/cmd/memtest-cmd.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -import sys, re, struct, mmap, time, resource -from bup import git, options +import sys, re, struct, time, resource +from bup import git, options, _helpers from bup.helpers import * handle_ctrl_c() @@ -72,8 +72,7 @@ git.check_repo_or_die() m = git.PackIdxList(git.repo('objects/pack')) report(-1) -f = open('/dev/urandom') -a = mmap.mmap(-1, 20) +_helpers.random_partial_sha() report(0) if opt.existing: @@ -89,10 +88,7 @@ for c in xrange(opt.cycles): bin = objit.next() assert(m.exists(bin)) else: - b = f.read(3) - a[0:2] = b[0:2] - a[2] = chr(ord(b[2]) & 0xf0) - bin = str(a[0:20]) + bin = _helpers.random_partial_sha() # technically, a randomly generated object id might exist. # but the likelihood of that is the likelihood of finding diff --git a/lib/bup/_helpers.c b/lib/bup/_helpers.c index 40235ee..242c510 100644 --- a/lib/bup/_helpers.c +++ b/lib/bup/_helpers.c @@ -147,6 +147,27 @@ static PyObject *write_random(PyObject *self, PyObject *args) } +static PyObject *random_partial_sha(PyObject *self, PyObject *args) +{ + static int seeded = 0; + uint32_t shabuf[20/4]; + + if (!seeded) + { + assert(sizeof(shabuf) == 20); + srandom(time(NULL)); + seeded = 1; + } + + if (!PyArg_ParseTuple(args, "")) + return NULL; + + memset(shabuf, 0, sizeof(shabuf)); + shabuf[0] = random(); + return Py_BuildValue("s#", shabuf, 20); +} + + static PyObject *open_noatime(PyObject *self, PyObject *args) { char *filename = NULL; @@ -208,6 +229,8 @@ static PyMethodDef faster_methods[] = { "Take the first 'nbits' bits from 'buf' and return them as an int." }, { "write_random", write_random, METH_VARARGS, "Write random bytes to the given file descriptor" }, + { "random_partial_sha", random_partial_sha, METH_VARARGS, + "Return a 20-byte string with the first few bytes randomized" }, { "open_noatime", open_noatime, METH_VARARGS, "open() the given filename for read with O_NOATIME if possible" }, { "fadvise_done", fadvise_done, METH_VARARGS, -- 2.39.2