1 #define _LARGEFILE64_SOURCE 1
13 #include <sys/ioctl.h>
19 static PyObject *selftest(PyObject *self, PyObject *args)
21 if (!PyArg_ParseTuple(args, ""))
24 return Py_BuildValue("i", !bupsplit_selftest());
28 static PyObject *blobbits(PyObject *self, PyObject *args)
30 if (!PyArg_ParseTuple(args, ""))
32 return Py_BuildValue("i", BUP_BLOBBITS);
36 static PyObject *splitbuf(PyObject *self, PyObject *args)
38 unsigned char *buf = NULL;
39 int len = 0, out = 0, bits = -1;
41 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
43 out = bupsplit_find_ofs(buf, len, &bits);
44 return Py_BuildValue("ii", out, bits);
48 static PyObject *bitmatch(PyObject *self, PyObject *args)
50 unsigned char *buf1 = NULL, *buf2 = NULL;
51 int len1 = 0, len2 = 0;
54 if (!PyArg_ParseTuple(args, "t#t#", &buf1, &len1, &buf2, &len2))
58 for (byte = 0; byte < len1 && byte < len2; byte++)
60 int b1 = buf1[byte], b2 = buf2[byte];
63 for (bit = 0; bit < 8; bit++)
64 if ( (b1 & (0x80 >> bit)) != (b2 & (0x80 >> bit)) )
70 return Py_BuildValue("i", byte*8 + bit);
74 static PyObject *firstword(PyObject *self, PyObject *args)
76 unsigned char *buf = NULL;
80 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
86 v = ntohl(*(uint32_t *)buf);
87 return Py_BuildValue("I", v);
91 static PyObject *extract_bits(PyObject *self, PyObject *args)
93 unsigned char *buf = NULL;
94 int len = 0, nbits = 0;
97 if (!PyArg_ParseTuple(args, "t#i", &buf, &len, &nbits))
103 mask = (1<<nbits) - 1;
104 v = ntohl(*(uint32_t *)buf);
105 v = (v >> (32-nbits)) & mask;
106 return Py_BuildValue("I", v);
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)
118 uint32_t buf[1024/4];
119 int fd = -1, seed = 0;
121 long long len = 0, kbytes = 0, written = 0;
123 if (!PyArg_ParseTuple(args, "iLi", &fd, &len, &seed))
128 for (kbytes = 0; kbytes < len/1024; kbytes++)
131 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
133 ret = write(fd, buf, sizeof(buf));
137 if (ret < (int)sizeof(buf))
139 if (kbytes/1024 > 0 && !(kbytes%1024))
140 fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
143 // handle non-multiples of 1024
147 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
149 ret = write(fd, buf, len % 1024);
156 fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
157 return Py_BuildValue("L", written);
161 static PyObject *open_noatime(PyObject *self, PyObject *args)
163 char *filename = NULL;
164 int attrs, attrs_noatime, fd;
165 if (!PyArg_ParseTuple(args, "s", &filename))
172 attrs |= O_LARGEFILE;
174 attrs_noatime = attrs;
176 attrs_noatime |= O_NOATIME;
178 fd = open(filename, attrs_noatime);
179 if (fd < 0 && errno == EPERM)
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);
189 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
190 return Py_BuildValue("i", fd);
194 static PyObject *fadvise_done(PyObject *self, PyObject *args)
198 if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
200 #ifdef POSIX_FADV_DONTNEED
201 posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
203 return Py_BuildValue("");
208 static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
215 if (!PyArg_ParseTuple(args, "s", &path))
218 fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
220 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
223 rc = ioctl(fd, FS_IOC_GETFLAGS, &attr);
227 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
231 return Py_BuildValue("k", attr);
235 static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
242 if (!PyArg_ParseTuple(args, "sk", &path, &attr))
245 fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
247 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
249 rc = ioctl(fd, FS_IOC_SETFLAGS, &attr);
253 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
259 #endif /* def linux */
262 #if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
263 #define HAVE_BUP_UTIMENSAT 1
265 static PyObject *bup_utimensat(PyObject *self, PyObject *args)
267 int rc, dirfd, flags;
269 long access, access_ns, modification, modification_ns;
270 struct timespec ts[2];
272 if (!PyArg_ParseTuple(args, "is((ll)(ll))i",
276 &modification, &modification_ns,
282 PyErr_SetString(PyExc_ValueError, "access time is NaN");
285 else if (isinf(access))
287 PyErr_SetString(PyExc_ValueError, "access time is infinite");
290 else if (isnan(modification))
292 PyErr_SetString(PyExc_ValueError, "modification time is NaN");
295 else if (isinf(modification))
297 PyErr_SetString(PyExc_ValueError, "modification time is infinite");
301 if (isnan(access_ns))
303 PyErr_SetString(PyExc_ValueError, "access time ns is NaN");
306 else if (isinf(access_ns))
308 PyErr_SetString(PyExc_ValueError, "access time ns is infinite");
311 else if (isnan(modification_ns))
313 PyErr_SetString(PyExc_ValueError, "modification time ns is NaN");
316 else if (isinf(modification_ns))
318 PyErr_SetString(PyExc_ValueError, "modification time ns is infinite");
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;
327 rc = utimensat(dirfd, path, ts, flags);
329 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
334 #endif /* _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L */
337 #ifdef linux /* and likely others */
338 #define HAVE_BUP_LSTAT 1
340 static PyObject *bup_lstat(PyObject *self, PyObject *args)
345 if (!PyArg_ParseTuple(args, "s", &filename))
349 rc = lstat(filename, &st);
351 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
353 return Py_BuildValue("kkkkkkkk"
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,
366 (long) st.st_atim.tv_nsec,
368 (long) st.st_mtim.tv_nsec,
370 (long) st.st_ctim.tv_nsec);
373 #endif /* def linux */
376 static PyMethodDef helper_methods[] = {
377 { "selftest", selftest, METH_VARARGS,
378 "Check that the rolling checksum rolls correctly (for unit tests)." },
379 { "blobbits", blobbits, METH_VARARGS,
380 "Return the number of bits in the rolling checksum." },
381 { "splitbuf", splitbuf, METH_VARARGS,
382 "Split a list of strings based on a rolling checksum." },
383 { "bitmatch", bitmatch, METH_VARARGS,
384 "Count the number of matching prefix bits between two strings." },
385 { "firstword", firstword, METH_VARARGS,
386 "Return an int corresponding to the first 32 bits of buf." },
387 { "extract_bits", extract_bits, METH_VARARGS,
388 "Take the first 'nbits' bits from 'buf' and return them as an int." },
389 { "write_random", write_random, METH_VARARGS,
390 "Write random bytes to the given file descriptor" },
391 { "open_noatime", open_noatime, METH_VARARGS,
392 "open() the given filename for read with O_NOATIME if possible" },
393 { "fadvise_done", fadvise_done, METH_VARARGS,
394 "Inform the kernel that we're finished with earlier parts of a file" },
396 { "get_linux_file_attr", bup_get_linux_file_attr, METH_VARARGS,
397 "Return the Linux attributes for the given file." },
398 { "set_linux_file_attr", bup_set_linux_file_attr, METH_VARARGS,
399 "Set the Linux attributes for the given file." },
401 #ifdef HAVE_BUP_UTIMENSAT
402 { "utimensat", bup_utimensat, METH_VARARGS,
403 "Change file timestamps with nanosecond precision." },
405 #ifdef HAVE_BUP_LSTAT
406 { "lstat", bup_lstat, METH_VARARGS,
407 "Extended version of lstat." },
409 { NULL, NULL, 0, NULL }, // sentinel
413 PyMODINIT_FUNC init_helpers(void)
415 PyObject *m = Py_InitModule("_helpers", helper_methods);
418 #ifdef HAVE_BUP_UTIMENSAT
419 PyModule_AddObject(m, "AT_FDCWD", Py_BuildValue("i", AT_FDCWD));
420 PyModule_AddObject(m, "AT_SYMLINK_NOFOLLOW",
421 Py_BuildValue("i", AT_SYMLINK_NOFOLLOW));