#!/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()
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:
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
}
+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;
"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,