]> arthur.barton.de Git - bup.git/blob - lib/bup/_helpers.c
Use Py_RETURN_TRUE in py_lutimes() and py_set_linux_file_attr().
[bup.git] / lib / bup / _helpers.c
1 #define _LARGEFILE64_SOURCE 1
2
3 #include "bupsplit.h"
4 #include <Python.h>
5 #include <assert.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <arpa/inet.h>
9 #include <linux/fs.h>
10 #include <stdint.h>
11 #include <sys/ioctl.h>
12 #include <sys/stat.h>
13 #include <sys/time.h>
14
15 static PyObject *selftest(PyObject *self, PyObject *args)
16 {
17     if (!PyArg_ParseTuple(args, ""))
18         return NULL;
19     
20     return Py_BuildValue("i", !bupsplit_selftest());
21 }
22
23
24 static PyObject *blobbits(PyObject *self, PyObject *args)
25 {
26     if (!PyArg_ParseTuple(args, ""))
27         return NULL;
28     return Py_BuildValue("i", BUP_BLOBBITS);
29 }
30
31
32 static PyObject *splitbuf(PyObject *self, PyObject *args)
33 {
34     unsigned char *buf = NULL;
35     int len = 0, out = 0, bits = -1;
36
37     if (!PyArg_ParseTuple(args, "t#", &buf, &len))
38         return NULL;
39     out = bupsplit_find_ofs(buf, len, &bits);
40     return Py_BuildValue("ii", out, bits);
41 }
42
43
44 static PyObject *bitmatch(PyObject *self, PyObject *args)
45 {
46     unsigned char *buf1 = NULL, *buf2 = NULL;
47     int len1 = 0, len2 = 0;
48     int byte, bit;
49
50     if (!PyArg_ParseTuple(args, "t#t#", &buf1, &len1, &buf2, &len2))
51         return NULL;
52     
53     bit = 0;
54     for (byte = 0; byte < len1 && byte < len2; byte++)
55     {
56         int b1 = buf1[byte], b2 = buf2[byte];
57         if (b1 != b2)
58         {
59             for (bit = 0; bit < 8; bit++)
60                 if ( (b1 & (0x80 >> bit)) != (b2 & (0x80 >> bit)) )
61                     break;
62             break;
63         }
64     }
65     
66     return Py_BuildValue("i", byte*8 + bit);
67 }
68
69
70 static PyObject *firstword(PyObject *self, PyObject *args)
71 {
72     unsigned char *buf = NULL;
73     int len = 0;
74     uint32_t v;
75
76     if (!PyArg_ParseTuple(args, "t#", &buf, &len))
77         return NULL;
78     
79     if (len < 4)
80         return NULL;
81     
82     v = ntohl(*(uint32_t *)buf);
83     return Py_BuildValue("I", v);
84 }
85
86
87 static PyObject *extract_bits(PyObject *self, PyObject *args)
88 {
89     unsigned char *buf = NULL;
90     int len = 0, nbits = 0;
91     uint32_t v, mask;
92
93     if (!PyArg_ParseTuple(args, "t#i", &buf, &len, &nbits))
94         return NULL;
95     
96     if (len < 4)
97         return NULL;
98     
99     mask = (1<<nbits) - 1;
100     v = ntohl(*(uint32_t *)buf);
101     v = (v >> (32-nbits)) & mask;
102     return Py_BuildValue("I", v);
103 }
104
105
106 // I would have made this a lower-level function that just fills in a buffer
107 // with random values, and then written those values from python.  But that's
108 // about 20% slower in my tests, and since we typically generate random
109 // numbers for benchmarking other parts of bup, any slowness in generating
110 // random bytes will make our benchmarks inaccurate.  Plus nobody wants
111 // pseudorandom bytes much except for this anyway.
112 static PyObject *write_random(PyObject *self, PyObject *args)
113 {
114     uint32_t buf[1024/4];
115     int fd = -1, seed = 0;
116     ssize_t ret;
117     long long len = 0, kbytes = 0, written = 0;
118
119     if (!PyArg_ParseTuple(args, "iLi", &fd, &len, &seed))
120         return NULL;
121     
122     srandom(seed);
123     
124     for (kbytes = 0; kbytes < len/1024; kbytes++)
125     {
126         unsigned i;
127         for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
128             buf[i] = random();
129         ret = write(fd, buf, sizeof(buf));
130         if (ret < 0)
131             ret = 0;
132         written += ret;
133         if (ret < (int)sizeof(buf))
134             break;
135         if (kbytes/1024 > 0 && !(kbytes%1024))
136             fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
137     }
138     
139     // handle non-multiples of 1024
140     if (len % 1024)
141     {
142         unsigned i;
143         for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
144             buf[i] = random();
145         ret = write(fd, buf, len % 1024);
146         if (ret < 0)
147             ret = 0;
148         written += ret;
149     }
150     
151     if (kbytes/1024 > 0)
152         fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
153     return Py_BuildValue("L", written);
154 }
155
156
157 static PyObject *open_noatime(PyObject *self, PyObject *args)
158 {
159     char *filename = NULL;
160     int attrs, attrs_noatime, fd;
161     if (!PyArg_ParseTuple(args, "s", &filename))
162         return NULL;
163     attrs = O_RDONLY;
164 #ifdef O_NOFOLLOW
165     attrs |= O_NOFOLLOW;
166 #endif
167 #ifdef O_LARGEFILE
168     attrs |= O_LARGEFILE;
169 #endif
170     attrs_noatime = attrs;
171 #ifdef O_NOATIME
172     attrs_noatime |= O_NOATIME;
173 #endif
174     fd = open(filename, attrs_noatime);
175     if (fd < 0 && errno == EPERM)
176     {
177         // older Linux kernels would return EPERM if you used O_NOATIME
178         // and weren't the file's owner.  This pointless restriction was
179         // relaxed eventually, but we have to handle it anyway.
180         // (VERY old kernels didn't recognized O_NOATIME, but they would
181         // just harmlessly ignore it, so this branch won't trigger)
182         fd = open(filename, attrs);
183     }
184     if (fd < 0)
185         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
186     return Py_BuildValue("i", fd);
187 }
188
189
190 static PyObject *fadvise_done(PyObject *self, PyObject *args)
191 {
192     int fd = -1;
193     long long ofs = 0;
194     if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
195         return NULL;
196 #ifdef POSIX_FADV_DONTNEED
197     posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
198 #endif    
199     return Py_BuildValue("");
200 }
201
202
203 static PyObject *py_get_linux_file_attr(PyObject *self, PyObject *args)
204 {
205     int rc;
206     unsigned long attr;
207     char *path;
208     int fd;
209
210     if (!PyArg_ParseTuple(args, "s", &path))
211         return NULL;
212
213     fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
214     if (fd == -1)
215         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
216
217     attr = 0;
218     rc = ioctl(fd, FS_IOC_GETFLAGS, &attr);
219     if (rc == -1)
220     {
221         close(fd);
222         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
223     }
224
225     close(fd);
226     return Py_BuildValue("k", attr);
227 }
228
229
230 static PyObject *py_set_linux_file_attr(PyObject *self, PyObject *args)
231 {
232     int rc;
233     unsigned long attr;
234     char *path;
235     int fd;
236
237     if (!PyArg_ParseTuple(args, "sk", &path, &attr))
238         return NULL;
239
240     fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
241     if(fd == -1)
242         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
243
244     rc = ioctl(fd, FS_IOC_SETFLAGS, &attr);
245     if (rc == -1)
246     {
247         close(fd);
248         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
249     }
250
251     close(fd);
252     Py_RETURN_TRUE;
253 }
254
255
256 static PyObject *py_lutimes(PyObject *self, PyObject *args)
257 {
258     int rc;
259     char *filename;
260     double access, modification;
261
262     if (!PyArg_ParseTuple(args, "s(dd)", &filename, &access, &modification))
263         return NULL;
264
265     if(isnan(access))
266     {
267         PyErr_SetString(PyExc_ValueError, "access time is NaN");
268         return NULL;
269     }
270     else if(isinf(access))
271     {
272         PyErr_SetString(PyExc_ValueError, "access time is infinite");
273         return NULL;
274     }
275     else if(isnan(modification))
276     {
277         PyErr_SetString(PyExc_ValueError, "modification time is NaN");
278         return NULL;
279     }
280     else if(isinf(modification))
281     {
282         PyErr_SetString(PyExc_ValueError, "modification time is infinite");
283         return NULL;
284     }
285
286     struct timeval tv[2];
287
288     double integral_part;
289     double fractional_part;
290
291     fractional_part = modf(access, &integral_part);
292     tv[0].tv_sec = integral_part;
293     tv[0].tv_usec = fractional_part * 1000000;
294
295     fractional_part = modf(modification, &integral_part);
296     tv[1].tv_sec = modification;
297     tv[1].tv_usec = fmod(modification, 1000000);
298
299     rc = lutimes(filename, tv);
300     if(rc != 0)
301         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
302
303     Py_RETURN_TRUE;
304 }
305
306
307 static PyMethodDef helper_methods[] = {
308     { "selftest", selftest, METH_VARARGS,
309         "Check that the rolling checksum rolls correctly (for unit tests)." },
310     { "blobbits", blobbits, METH_VARARGS,
311         "Return the number of bits in the rolling checksum." },
312     { "splitbuf", splitbuf, METH_VARARGS,
313         "Split a list of strings based on a rolling checksum." },
314     { "bitmatch", bitmatch, METH_VARARGS,
315         "Count the number of matching prefix bits between two strings." },
316     { "firstword", firstword, METH_VARARGS,
317         "Return an int corresponding to the first 32 bits of buf." },
318     { "extract_bits", extract_bits, METH_VARARGS,
319         "Take the first 'nbits' bits from 'buf' and return them as an int." },
320     { "write_random", write_random, METH_VARARGS,
321         "Write random bytes to the given file descriptor" },
322     { "open_noatime", open_noatime, METH_VARARGS,
323         "open() the given filename for read with O_NOATIME if possible" },
324     { "fadvise_done", fadvise_done, METH_VARARGS,
325         "Inform the kernel that we're finished with earlier parts of a file" },
326     { "get_linux_file_attr", py_get_linux_file_attr, METH_VARARGS,
327       "Return the Linux attributes for the given file." },
328     { "set_linux_file_attr", py_set_linux_file_attr, METH_VARARGS,
329       "Set the Linux attributes for the given file." },
330     { "lutimes", py_lutimes, METH_VARARGS,
331       "Set the access and modification times for the given file or symlink." },
332     { NULL, NULL, 0, NULL },  // sentinel
333 };
334
335 PyMODINIT_FUNC init_helpers(void)
336 {
337     Py_InitModule("_helpers", helper_methods);
338 }