]> arthur.barton.de Git - bup.git/blob - lib/bup/_helpers.c
Always define _have_ns_fs_timestamps (True or False).
[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 <stdint.h>
10
11 #ifdef linux
12 #include <linux/fs.h>
13 #include <sys/ioctl.h>
14 #include <sys/stat.h>
15 #include <sys/time.h>
16 #endif
17
18
19 static PyObject *selftest(PyObject *self, PyObject *args)
20 {
21     if (!PyArg_ParseTuple(args, ""))
22         return NULL;
23     
24     return Py_BuildValue("i", !bupsplit_selftest());
25 }
26
27
28 static PyObject *blobbits(PyObject *self, PyObject *args)
29 {
30     if (!PyArg_ParseTuple(args, ""))
31         return NULL;
32     return Py_BuildValue("i", BUP_BLOBBITS);
33 }
34
35
36 static PyObject *splitbuf(PyObject *self, PyObject *args)
37 {
38     unsigned char *buf = NULL;
39     int len = 0, out = 0, bits = -1;
40
41     if (!PyArg_ParseTuple(args, "t#", &buf, &len))
42         return NULL;
43     out = bupsplit_find_ofs(buf, len, &bits);
44     return Py_BuildValue("ii", out, bits);
45 }
46
47
48 static PyObject *bitmatch(PyObject *self, PyObject *args)
49 {
50     unsigned char *buf1 = NULL, *buf2 = NULL;
51     int len1 = 0, len2 = 0;
52     int byte, bit;
53
54     if (!PyArg_ParseTuple(args, "t#t#", &buf1, &len1, &buf2, &len2))
55         return NULL;
56     
57     bit = 0;
58     for (byte = 0; byte < len1 && byte < len2; byte++)
59     {
60         int b1 = buf1[byte], b2 = buf2[byte];
61         if (b1 != b2)
62         {
63             for (bit = 0; bit < 8; bit++)
64                 if ( (b1 & (0x80 >> bit)) != (b2 & (0x80 >> bit)) )
65                     break;
66             break;
67         }
68     }
69     
70     return Py_BuildValue("i", byte*8 + bit);
71 }
72
73
74 static PyObject *firstword(PyObject *self, PyObject *args)
75 {
76     unsigned char *buf = NULL;
77     int len = 0;
78     uint32_t v;
79
80     if (!PyArg_ParseTuple(args, "t#", &buf, &len))
81         return NULL;
82     
83     if (len < 4)
84         return NULL;
85     
86     v = ntohl(*(uint32_t *)buf);
87     return Py_BuildValue("I", v);
88 }
89
90
91 static PyObject *extract_bits(PyObject *self, PyObject *args)
92 {
93     unsigned char *buf = NULL;
94     int len = 0, nbits = 0;
95     uint32_t v, mask;
96
97     if (!PyArg_ParseTuple(args, "t#i", &buf, &len, &nbits))
98         return NULL;
99     
100     if (len < 4)
101         return NULL;
102     
103     mask = (1<<nbits) - 1;
104     v = ntohl(*(uint32_t *)buf);
105     v = (v >> (32-nbits)) & mask;
106     return Py_BuildValue("I", v);
107 }
108
109
110 // I would have made this a lower-level function that just fills in a buffer
111 // with random values, and then written those values from python.  But that's
112 // about 20% slower in my tests, and since we typically generate random
113 // numbers for benchmarking other parts of bup, any slowness in generating
114 // random bytes will make our benchmarks inaccurate.  Plus nobody wants
115 // pseudorandom bytes much except for this anyway.
116 static PyObject *write_random(PyObject *self, PyObject *args)
117 {
118     uint32_t buf[1024/4];
119     int fd = -1, seed = 0;
120     ssize_t ret;
121     long long len = 0, kbytes = 0, written = 0;
122
123     if (!PyArg_ParseTuple(args, "iLi", &fd, &len, &seed))
124         return NULL;
125     
126     srandom(seed);
127     
128     for (kbytes = 0; kbytes < len/1024; kbytes++)
129     {
130         unsigned i;
131         for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
132             buf[i] = random();
133         ret = write(fd, buf, sizeof(buf));
134         if (ret < 0)
135             ret = 0;
136         written += ret;
137         if (ret < (int)sizeof(buf))
138             break;
139         if (kbytes/1024 > 0 && !(kbytes%1024))
140             fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
141     }
142     
143     // handle non-multiples of 1024
144     if (len % 1024)
145     {
146         unsigned i;
147         for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
148             buf[i] = random();
149         ret = write(fd, buf, len % 1024);
150         if (ret < 0)
151             ret = 0;
152         written += ret;
153     }
154     
155     if (kbytes/1024 > 0)
156         fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
157     return Py_BuildValue("L", written);
158 }
159
160
161 static PyObject *open_noatime(PyObject *self, PyObject *args)
162 {
163     char *filename = NULL;
164     int attrs, attrs_noatime, fd;
165     if (!PyArg_ParseTuple(args, "s", &filename))
166         return NULL;
167     attrs = O_RDONLY;
168 #ifdef O_NOFOLLOW
169     attrs |= O_NOFOLLOW;
170 #endif
171 #ifdef O_LARGEFILE
172     attrs |= O_LARGEFILE;
173 #endif
174     attrs_noatime = attrs;
175 #ifdef O_NOATIME
176     attrs_noatime |= O_NOATIME;
177 #endif
178     fd = open(filename, attrs_noatime);
179     if (fd < 0 && errno == EPERM)
180     {
181         // older Linux kernels would return EPERM if you used O_NOATIME
182         // and weren't the file's owner.  This pointless restriction was
183         // relaxed eventually, but we have to handle it anyway.
184         // (VERY old kernels didn't recognized O_NOATIME, but they would
185         // just harmlessly ignore it, so this branch won't trigger)
186         fd = open(filename, attrs);
187     }
188     if (fd < 0)
189         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
190     return Py_BuildValue("i", fd);
191 }
192
193
194 static PyObject *fadvise_done(PyObject *self, PyObject *args)
195 {
196     int fd = -1;
197     long long ofs = 0;
198     if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
199         return NULL;
200 #ifdef POSIX_FADV_DONTNEED
201     posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
202 #endif    
203     return Py_BuildValue("");
204 }
205
206
207 #ifdef linux
208 static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
209 {
210     int rc;
211     unsigned long attr;
212     char *path;
213     int fd;
214
215     if (!PyArg_ParseTuple(args, "s", &path))
216         return NULL;
217
218     fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
219     if (fd == -1)
220         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
221
222     attr = 0;
223     rc = ioctl(fd, FS_IOC_GETFLAGS, &attr);
224     if (rc == -1)
225     {
226         close(fd);
227         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
228     }
229
230     close(fd);
231     return Py_BuildValue("k", attr);
232 }
233
234
235 static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
236 {
237     int rc;
238     unsigned long attr;
239     char *path;
240     int fd;
241
242     if (!PyArg_ParseTuple(args, "sk", &path, &attr))
243         return NULL;
244
245     fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
246     if(fd == -1)
247         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
248
249     rc = ioctl(fd, FS_IOC_SETFLAGS, &attr);
250     if (rc == -1)
251     {
252         close(fd);
253         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
254     }
255
256     close(fd);
257     Py_RETURN_TRUE;
258 }
259 #endif /* def linux */
260
261
262 #if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
263 #define HAVE_BUP_UTIMENSAT 1
264
265 static PyObject *bup_utimensat(PyObject *self, PyObject *args)
266 {
267     int rc, dirfd, flags;
268     char *path;
269     long access, access_ns, modification, modification_ns;
270     struct timespec ts[2];
271
272     if (!PyArg_ParseTuple(args, "is((ll)(ll))i",
273                           &dirfd,
274                           &path,
275                           &access, &access_ns,
276                           &modification, &modification_ns,
277                           &flags))
278         return NULL;
279
280     if (isnan(access))
281     {
282         PyErr_SetString(PyExc_ValueError, "access time is NaN");
283         return NULL;
284     }
285     else if (isinf(access))
286     {
287         PyErr_SetString(PyExc_ValueError, "access time is infinite");
288         return NULL;
289     }
290     else if (isnan(modification))
291     {
292         PyErr_SetString(PyExc_ValueError, "modification time is NaN");
293         return NULL;
294     }
295     else if (isinf(modification))
296     {
297         PyErr_SetString(PyExc_ValueError, "modification time is infinite");
298         return NULL;
299     }
300
301     if (isnan(access_ns))
302     {
303         PyErr_SetString(PyExc_ValueError, "access time ns is NaN");
304         return NULL;
305     }
306     else if (isinf(access_ns))
307     {
308         PyErr_SetString(PyExc_ValueError, "access time ns is infinite");
309         return NULL;
310     }
311     else if (isnan(modification_ns))
312     {
313         PyErr_SetString(PyExc_ValueError, "modification time ns is NaN");
314         return NULL;
315     }
316     else if (isinf(modification_ns))
317     {
318         PyErr_SetString(PyExc_ValueError, "modification time ns is infinite");
319         return NULL;
320     }
321
322     ts[0].tv_sec = access;
323     ts[0].tv_nsec = access_ns;
324     ts[1].tv_sec = modification;
325     ts[1].tv_nsec = modification_ns;
326
327     rc = utimensat(dirfd, path, ts, flags);
328     if (rc != 0)
329         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
330
331     Py_RETURN_TRUE;
332 }
333
334 #endif /* _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L */
335
336
337 #ifdef linux /* and likely others */
338
339 #define HAVE_BUP_STAT 1
340 static PyObject *bup_stat(PyObject *self, PyObject *args)
341 {
342     int rc;
343     char *filename;
344
345     if (!PyArg_ParseTuple(args, "s", &filename))
346         return NULL;
347
348     struct stat st;
349     rc = stat(filename, &st);
350     if (rc != 0)
351         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
352
353     return Py_BuildValue("kkkkkkkk"
354                          "(ll)"
355                          "(ll)"
356                          "(ll)",
357                          (unsigned long) st.st_mode,
358                          (unsigned long) st.st_ino,
359                          (unsigned long) st.st_dev,
360                          (unsigned long) st.st_nlink,
361                          (unsigned long) st.st_uid,
362                          (unsigned long) st.st_gid,
363                          (unsigned long) st.st_rdev,
364                          (unsigned long) st.st_size,
365                          (long) st.st_atime,
366                          (long) st.st_atim.tv_nsec,
367                          (long) st.st_mtime,
368                          (long) st.st_mtim.tv_nsec,
369                          (long) st.st_ctime,
370                          (long) st.st_ctim.tv_nsec);
371 }
372
373
374 #define HAVE_BUP_LSTAT 1
375 static PyObject *bup_lstat(PyObject *self, PyObject *args)
376 {
377     int rc;
378     char *filename;
379
380     if (!PyArg_ParseTuple(args, "s", &filename))
381         return NULL;
382
383     struct stat st;
384     rc = lstat(filename, &st);
385     if (rc != 0)
386         return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
387
388     return Py_BuildValue("kkkkkkkk"
389                          "(ll)"
390                          "(ll)"
391                          "(ll)",
392                          (unsigned long) st.st_mode,
393                          (unsigned long) st.st_ino,
394                          (unsigned long) st.st_dev,
395                          (unsigned long) st.st_nlink,
396                          (unsigned long) st.st_uid,
397                          (unsigned long) st.st_gid,
398                          (unsigned long) st.st_rdev,
399                          (unsigned long) st.st_size,
400                          (long) st.st_atime,
401                          (long) st.st_atim.tv_nsec,
402                          (long) st.st_mtime,
403                          (long) st.st_mtim.tv_nsec,
404                          (long) st.st_ctime,
405                          (long) st.st_ctim.tv_nsec);
406 }
407
408
409 #define HAVE_BUP_FSTAT 1
410 static PyObject *bup_fstat(PyObject *self, PyObject *args)
411 {
412     int rc, fd;
413
414     if (!PyArg_ParseTuple(args, "i", &fd))
415         return NULL;
416
417     struct stat st;
418     rc = fstat(fd, &st);
419     if (rc != 0)
420         return PyErr_SetFromErrno(PyExc_IOError);
421
422     return Py_BuildValue("kkkkkkkk"
423                          "(ll)"
424                          "(ll)"
425                          "(ll)",
426                          (unsigned long) st.st_mode,
427                          (unsigned long) st.st_ino,
428                          (unsigned long) st.st_dev,
429                          (unsigned long) st.st_nlink,
430                          (unsigned long) st.st_uid,
431                          (unsigned long) st.st_gid,
432                          (unsigned long) st.st_rdev,
433                          (unsigned long) st.st_size,
434                          (long) st.st_atime,
435                          (long) st.st_atim.tv_nsec,
436                          (long) st.st_mtime,
437                          (long) st.st_mtim.tv_nsec,
438                          (long) st.st_ctime,
439                          (long) st.st_ctim.tv_nsec);
440 }
441
442 #endif /* def linux */
443
444
445 static PyMethodDef helper_methods[] = {
446     { "selftest", selftest, METH_VARARGS,
447         "Check that the rolling checksum rolls correctly (for unit tests)." },
448     { "blobbits", blobbits, METH_VARARGS,
449         "Return the number of bits in the rolling checksum." },
450     { "splitbuf", splitbuf, METH_VARARGS,
451         "Split a list of strings based on a rolling checksum." },
452     { "bitmatch", bitmatch, METH_VARARGS,
453         "Count the number of matching prefix bits between two strings." },
454     { "firstword", firstword, METH_VARARGS,
455         "Return an int corresponding to the first 32 bits of buf." },
456     { "extract_bits", extract_bits, METH_VARARGS,
457         "Take the first 'nbits' bits from 'buf' and return them as an int." },
458     { "write_random", write_random, METH_VARARGS,
459         "Write random bytes to the given file descriptor" },
460     { "open_noatime", open_noatime, METH_VARARGS,
461         "open() the given filename for read with O_NOATIME if possible" },
462     { "fadvise_done", fadvise_done, METH_VARARGS,
463         "Inform the kernel that we're finished with earlier parts of a file" },
464 #ifdef linux
465     { "get_linux_file_attr", bup_get_linux_file_attr, METH_VARARGS,
466       "Return the Linux attributes for the given file." },
467     { "set_linux_file_attr", bup_set_linux_file_attr, METH_VARARGS,
468       "Set the Linux attributes for the given file." },
469 #endif
470 #ifdef HAVE_BUP_UTIMENSAT
471     { "utimensat", bup_utimensat, METH_VARARGS,
472       "Change file timestamps with nanosecond precision." },
473 #endif
474 #ifdef HAVE_BUP_STAT
475     { "stat", bup_stat, METH_VARARGS,
476       "Extended version of stat." },
477 #endif
478 #ifdef HAVE_BUP_LSTAT
479     { "lstat", bup_lstat, METH_VARARGS,
480       "Extended version of lstat." },
481 #endif
482 #ifdef HAVE_BUP_FSTAT
483     { "fstat", bup_fstat, METH_VARARGS,
484       "Extended version of fstat." },
485 #endif
486     { NULL, NULL, 0, NULL },  // sentinel
487 };
488
489
490 PyMODINIT_FUNC init_helpers(void)
491 {
492     PyObject *m = Py_InitModule("_helpers", helper_methods);
493     if (m == NULL)
494         return;
495 #ifdef HAVE_BUP_UTIMENSAT
496     PyModule_AddObject(m, "AT_FDCWD", Py_BuildValue("i", AT_FDCWD));
497     PyModule_AddObject(m, "AT_SYMLINK_NOFOLLOW",
498                        Py_BuildValue("i", AT_SYMLINK_NOFOLLOW));
499 #endif
500 #ifdef HAVE_BUP_STAT
501     Py_INCREF(Py_True);
502     PyModule_AddObject(m, "_have_ns_fs_timestamps", Py_True);
503 #else
504     Py_INCREF(Py_False);
505     PyModule_AddObject(m, "_have_ns_fs_timestamps", Py_False);
506 #endif
507 }