]> arthur.barton.de Git - bup.git/commitdiff
cmd/memtest: stop using weird mmap() and /dev/urandom tricks.
authorAvery Pennarun <apenwarr@gmail.com>
Sat, 4 Dec 2010 14:13:16 +0000 (06:13 -0800)
committerAvery Pennarun <apenwarr@gmail.com>
Sat, 4 Dec 2010 14:14:02 +0000 (06:14 -0800)
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 <apenwarr@gmail.com>
cmd/memtest-cmd.py
lib/bup/_helpers.c

index 7d9eadc8c2845a8e3d947962a13fc0ea023b2eab..fb202b749d65261ee0cf64d3b3d36e63c7f9c955 100755 (executable)
@@ -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
index 40235ee3ed105b06c7334cff736f14ce6b9b6b3f..242c51070320adf91eda68f8188b074f19c56723 100644 (file)
@@ -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,