]> arthur.barton.de Git - bup.git/blob - lib/bup/_helpers.c
_helpers.firstword(): a new function to extract the first 32 bits.
[bup.git] / lib / bup / _helpers.c
1 #include "bupsplit.h"
2 #include <Python.h>
3 #include <assert.h>
4 #include <stdint.h>
5 #include <fcntl.h>
6 #include <arpa/inet.h>
7
8 static PyObject *selftest(PyObject *self, PyObject *args)
9 {
10     if (!PyArg_ParseTuple(args, ""))
11         return NULL;
12     
13     return Py_BuildValue("i", !bupsplit_selftest());
14 }
15
16
17 static PyObject *blobbits(PyObject *self, PyObject *args)
18 {
19     if (!PyArg_ParseTuple(args, ""))
20         return NULL;
21     return Py_BuildValue("i", BUP_BLOBBITS);
22 }
23
24
25 static PyObject *splitbuf(PyObject *self, PyObject *args)
26 {
27     unsigned char *buf = NULL;
28     int len = 0, out = 0, bits = -1;
29
30     if (!PyArg_ParseTuple(args, "t#", &buf, &len))
31         return NULL;
32     out = bupsplit_find_ofs(buf, len, &bits);
33     return Py_BuildValue("ii", out, bits);
34 }
35
36
37 static PyObject *bitmatch(PyObject *self, PyObject *args)
38 {
39     unsigned char *buf1 = NULL, *buf2 = NULL;
40     int len1 = 0, len2 = 0;
41     int byte, bit;
42
43     if (!PyArg_ParseTuple(args, "t#t#", &buf1, &len1, &buf2, &len2))
44         return NULL;
45     
46     bit = 0;
47     for (byte = 0; byte < len1 && byte < len2; byte++)
48     {
49         int b1 = buf1[byte], b2 = buf2[byte];
50         if (b1 != b2)
51         {
52             for (bit = 0; bit < 8; bit++)
53                 if ( (b1 & (0x80 >> bit)) != (b2 & (0x80 >> bit)) )
54                     break;
55             break;
56         }
57     }
58     
59     return Py_BuildValue("i", byte*8 + bit);
60 }
61
62
63 static PyObject *firstword(PyObject *self, PyObject *args)
64 {
65     unsigned char *buf = NULL;
66     int len = 0;
67     uint32_t v;
68
69     if (!PyArg_ParseTuple(args, "t#", &buf, &len))
70         return NULL;
71     
72     if (len < 4)
73         return NULL;
74     
75     v = ntohl(*(uint32_t *)buf);
76     return Py_BuildValue("I", v);
77 }
78
79
80 // I would have made this a lower-level function that just fills in a buffer
81 // with random values, and then written those values from python.  But that's
82 // about 20% slower in my tests, and since we typically generate random
83 // numbers for benchmarking other parts of bup, any slowness in generating
84 // random bytes will make our benchmarks inaccurate.  Plus nobody wants
85 // pseudorandom bytes much except for this anyway.
86 static PyObject *write_random(PyObject *self, PyObject *args)
87 {
88     uint32_t buf[1024/4];
89     int fd = -1, seed = 0;
90     ssize_t ret;
91     long long len = 0, kbytes = 0, written = 0;
92
93     if (!PyArg_ParseTuple(args, "iLi", &fd, &len, &seed))
94         return NULL;
95     
96     srandom(seed);
97     
98     for (kbytes = 0; kbytes < len/1024; kbytes++)
99     {
100         unsigned i;
101         for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
102             buf[i] = random();
103         ret = write(fd, buf, sizeof(buf));
104         if (ret < 0)
105             ret = 0;
106         written += ret;
107         if (ret < (int)sizeof(buf))
108             break;
109         if (kbytes/1024 > 0 && !(kbytes%1024))
110             fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
111     }
112     
113     // handle non-multiples of 1024
114     if (len % 1024)
115     {
116         unsigned i;
117         for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
118             buf[i] = random();
119         ret = write(fd, buf, len % 1024);
120         if (ret < 0)
121             ret = 0;
122         written += ret;
123     }
124     
125     if (kbytes/1024 > 0)
126         fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
127     return Py_BuildValue("L", written);
128 }
129
130
131 static PyObject *open_noatime(PyObject *self, PyObject *args)
132 {
133     char *filename = NULL;
134     int attrs, attrs_noatime, fd;
135     if (!PyArg_ParseTuple(args, "s", &filename))
136         return NULL;
137     attrs = O_RDONLY;
138 #ifdef O_NOFOLLOW
139     attrs |= O_NOFOLLOW;
140 #endif
141 #ifdef O_LARGEFILE
142     attrs |= O_LARGEFILE;
143 #endif
144     attrs_noatime = attrs;
145 #ifdef O_NOATIME
146     attrs_noatime |= O_NOATIME;
147 #endif
148     fd = open(filename, attrs_noatime);
149     if (fd < 0 && errno == EPERM)
150     {
151         // older Linux kernels would return EPERM if you used O_NOATIME
152         // and weren't the file's owner.  This pointless restriction was
153         // relaxed eventually, but we have to handle it anyway.
154         // (VERY old kernels didn't recognized O_NOATIME, but they would
155         // just harmlessly ignore it, so this branch won't trigger)
156         fd = open(filename, attrs);
157     }
158     if (fd < 0)
159         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
160     return Py_BuildValue("i", fd);
161 }
162
163
164 static PyObject *fadvise_done(PyObject *self, PyObject *args)
165 {
166     int fd = -1;
167     long long ofs = 0;
168     if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
169         return NULL;
170 #ifdef POSIX_FADV_DONTNEED
171     posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
172 #endif    
173     return Py_BuildValue("");
174 }
175
176
177 static PyMethodDef faster_methods[] = {
178     { "selftest", selftest, METH_VARARGS,
179         "Check that the rolling checksum rolls correctly (for unit tests)." },
180     { "blobbits", blobbits, METH_VARARGS,
181         "Return the number of bits in the rolling checksum." },
182     { "splitbuf", splitbuf, METH_VARARGS,
183         "Split a list of strings based on a rolling checksum." },
184     { "bitmatch", bitmatch, METH_VARARGS,
185         "Count the number of matching prefix bits between two strings." },
186     { "firstword", firstword, METH_VARARGS,
187         "Return an int corresponding to the first 32 bits of buf." },
188     { "write_random", write_random, METH_VARARGS,
189         "Write random bytes to the given file descriptor" },
190     { "open_noatime", open_noatime, METH_VARARGS,
191         "open() the given filename for read with O_NOATIME if possible" },
192     { "fadvise_done", fadvise_done, METH_VARARGS,
193         "Inform the kernel that we're finished with earlier parts of a file" },
194     { NULL, NULL, 0, NULL },  // sentinel
195 };
196
197 PyMODINIT_FUNC init_helpers(void)
198 {
199     Py_InitModule("_helpers", faster_methods);
200 }