1 #define _LARGEFILE64_SOURCE 1
2 #define PY_SSIZE_T_CLEAN 1
4 #include "../../config/config.h"
6 // According to Python, its header has to go first:
7 // http://docs.python.org/2/c-api/intro.html#include-files
13 #include <arpa/inet.h>
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
22 #ifdef HAVE_SYS_STAT_H
29 #ifdef HAVE_LINUX_FS_H
32 #ifdef HAVE_SYS_IOCTL_H
33 #include <sys/ioctl.h>
38 #if defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS)
39 #define BUP_HAVE_FILE_ATTRS 1
43 // Of course, this assumes it's a bitfield value.
47 static int istty2 = 0;
50 #define INTEGER_TO_PY(x) \
51 (((x) >= 0) ? PyLong_FromUnsignedLongLong(x) : PyLong_FromLongLong(x))
54 static int bup_ulong_from_pyint(unsigned long *x, PyObject *py,
57 const long tmp = PyInt_AsLong(py);
58 if (tmp == -1 && PyErr_Occurred())
60 if (PyErr_ExceptionMatches(PyExc_OverflowError))
61 PyErr_Format(PyExc_OverflowError, "%s too big for unsigned long",
67 PyErr_Format(PyExc_OverflowError,
68 "negative %s cannot be converted to unsigned long", name);
76 static int bup_ulong_from_py(unsigned long *x, PyObject *py, const char *name)
79 return bup_ulong_from_pyint(x, py, name);
81 if (!PyLong_Check(py))
83 PyErr_Format(PyExc_TypeError, "expected integer %s", name);
87 const unsigned long tmp = PyLong_AsUnsignedLong(py);
90 if (PyErr_ExceptionMatches(PyExc_OverflowError))
91 PyErr_Format(PyExc_OverflowError, "%s too big for unsigned long",
100 static int bup_uint_from_py(unsigned int *x, PyObject *py, const char *name)
103 if (!bup_ulong_from_py(&tmp, py, name))
108 PyErr_Format(PyExc_OverflowError, "%s too big for unsigned int", name);
115 static int bup_ullong_from_py(unsigned PY_LONG_LONG *x, PyObject *py,
121 if (bup_ulong_from_pyint(&tmp, py, name))
129 if (!PyLong_Check(py))
131 PyErr_Format(PyExc_TypeError, "integer argument expected for %s", name);
135 const unsigned PY_LONG_LONG tmp = PyLong_AsUnsignedLongLong(py);
136 if (tmp == (unsigned long long) -1 && PyErr_Occurred())
138 if (PyErr_ExceptionMatches(PyExc_OverflowError))
139 PyErr_Format(PyExc_OverflowError,
140 "%s too big for unsigned long long", name);
148 // Probably we should use autoconf or something and set HAVE_PY_GETARGCARGV...
149 #if __WIN32__ || __CYGWIN__
151 // There's no 'ps' on win32 anyway, and Py_GetArgcArgv() isn't available.
152 static void unpythonize_argv(void) { }
154 #else // not __WIN32__
156 // For some reason this isn't declared in Python.h
157 extern void Py_GetArgcArgv(int *argc, char ***argv);
159 static void unpythonize_argv(void)
164 Py_GetArgcArgv(&argc, &argv);
166 for (i = 0; i < argc-1; i++)
168 if (argv[i] + strlen(argv[i]) + 1 != argv[i+1])
170 // The argv block doesn't work the way we expected; it's unsafe
176 arge = argv[argc-1] + strlen(argv[argc-1]) + 1;
178 if (strstr(argv[0], "python") && argv[1] == argv[0] + strlen(argv[0]) + 1)
182 p = strrchr(argv[1], '/');
188 memmove(argv[0], p, len);
189 memset(arge - diff, 0, diff);
190 for (i = 0; i < argc; i++)
191 argv[i] = argv[i+1] ? argv[i+1]-diff : NULL;
196 #endif // not __WIN32__ or __CYGWIN__
199 static PyObject *selftest(PyObject *self, PyObject *args)
201 if (!PyArg_ParseTuple(args, ""))
204 return Py_BuildValue("i", !bupsplit_selftest());
208 static PyObject *blobbits(PyObject *self, PyObject *args)
210 if (!PyArg_ParseTuple(args, ""))
212 return Py_BuildValue("i", BUP_BLOBBITS);
216 static PyObject *splitbuf(PyObject *self, PyObject *args)
218 unsigned char *buf = NULL;
220 int out = 0, bits = -1;
222 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
224 assert(len <= INT_MAX);
225 out = bupsplit_find_ofs(buf, len, &bits);
226 if (out) assert(bits >= BUP_BLOBBITS);
227 return Py_BuildValue("ii", out, bits);
231 static PyObject *bitmatch(PyObject *self, PyObject *args)
233 unsigned char *buf1 = NULL, *buf2 = NULL;
234 Py_ssize_t len1 = 0, len2 = 0;
238 if (!PyArg_ParseTuple(args, "t#t#", &buf1, &len1, &buf2, &len2))
242 for (byte = 0; byte < len1 && byte < len2; byte++)
244 int b1 = buf1[byte], b2 = buf2[byte];
247 for (bit = 0; bit < 8; bit++)
248 if ( (b1 & (0x80 >> bit)) != (b2 & (0x80 >> bit)) )
254 assert(byte <= (INT_MAX >> 3));
255 return Py_BuildValue("i", byte*8 + bit);
259 static PyObject *firstword(PyObject *self, PyObject *args)
261 unsigned char *buf = NULL;
265 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
271 v = ntohl(*(uint32_t *)buf);
272 return PyLong_FromUnsignedLong(v);
276 #define BLOOM2_HEADERLEN 16
278 static void to_bloom_address_bitmask4(const unsigned char *buf,
279 const int nbits, uint64_t *v, unsigned char *bitmask)
285 memcpy(&high, buf, 4);
286 mask = (1<<nbits) - 1;
287 raw = (((uint64_t)ntohl(high) << 8) | buf[4]);
288 bit = (raw >> (37-nbits)) & 0x7;
289 *v = (raw >> (40-nbits)) & mask;
293 static void to_bloom_address_bitmask5(const unsigned char *buf,
294 const int nbits, uint32_t *v, unsigned char *bitmask)
300 memcpy(&high, buf, 4);
301 mask = (1<<nbits) - 1;
303 bit = (raw >> (29-nbits)) & 0x7;
304 *v = (raw >> (32-nbits)) & mask;
308 #define BLOOM_SET_BIT(name, address, otype) \
309 static void name(unsigned char *bloom, const unsigned char *buf, const int nbits)\
311 unsigned char bitmask;\
313 address(buf, nbits, &v, &bitmask);\
314 bloom[BLOOM2_HEADERLEN+v] |= bitmask;\
316 BLOOM_SET_BIT(bloom_set_bit4, to_bloom_address_bitmask4, uint64_t)
317 BLOOM_SET_BIT(bloom_set_bit5, to_bloom_address_bitmask5, uint32_t)
320 #define BLOOM_GET_BIT(name, address, otype) \
321 static int name(const unsigned char *bloom, const unsigned char *buf, const int nbits)\
323 unsigned char bitmask;\
325 address(buf, nbits, &v, &bitmask);\
326 return bloom[BLOOM2_HEADERLEN+v] & bitmask;\
328 BLOOM_GET_BIT(bloom_get_bit4, to_bloom_address_bitmask4, uint64_t)
329 BLOOM_GET_BIT(bloom_get_bit5, to_bloom_address_bitmask5, uint32_t)
332 static PyObject *bloom_add(PyObject *self, PyObject *args)
334 unsigned char *sha = NULL, *bloom = NULL;
336 Py_ssize_t len = 0, blen = 0;
337 int nbits = 0, k = 0;
339 if (!PyArg_ParseTuple(args, "w#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
342 if (blen < 16+(1<<nbits) || len % 20 != 0)
349 for (end = sha + len; sha < end; sha += 20/k)
350 bloom_set_bit5(bloom, sha, nbits);
356 for (end = sha + len; sha < end; sha += 20/k)
357 bloom_set_bit4(bloom, sha, nbits);
363 return Py_BuildValue("n", len/20);
366 static PyObject *bloom_contains(PyObject *self, PyObject *args)
368 unsigned char *sha = NULL, *bloom = NULL;
369 Py_ssize_t len = 0, blen = 0;
370 int nbits = 0, k = 0;
374 if (!PyArg_ParseTuple(args, "t#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
384 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
385 if (!bloom_get_bit5(bloom, sha, nbits))
386 return Py_BuildValue("Oi", Py_None, steps);
392 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
393 if (!bloom_get_bit4(bloom, sha, nbits))
394 return Py_BuildValue("Oi", Py_None, steps);
399 return Py_BuildValue("ii", 1, k);
403 static uint32_t _extract_bits(unsigned char *buf, int nbits)
407 mask = (1<<nbits) - 1;
408 v = ntohl(*(uint32_t *)buf);
409 v = (v >> (32-nbits)) & mask;
414 static PyObject *extract_bits(PyObject *self, PyObject *args)
416 unsigned char *buf = NULL;
420 if (!PyArg_ParseTuple(args, "t#i", &buf, &len, &nbits))
426 return PyLong_FromUnsignedLong(_extract_bits(buf, nbits));
431 unsigned char bytes[20];
445 static int _cmp_sha(const struct sha *sha1, const struct sha *sha2)
448 for (i = 0; i < sizeof(struct sha); i++)
449 if (sha1->bytes[i] != sha2->bytes[i])
450 return sha1->bytes[i] - sha2->bytes[i];
455 static void _fix_idx_order(struct idx **idxs, int *last_i)
458 int low, mid, high, c = 0;
461 if (idxs[*last_i]->cur >= idxs[*last_i]->end)
463 idxs[*last_i] = NULL;
476 mid = (low + high) / 2;
477 c = _cmp_sha(idx->cur, idxs[mid]->cur);
489 memmove(&idxs[mid+1], &idxs[mid], (*last_i-mid)*sizeof(struct idx *));
494 static uint32_t _get_idx_i(struct idx *idx)
496 if (idx->cur_name == NULL)
497 return idx->name_base;
498 return ntohl(*idx->cur_name) + idx->name_base;
501 #define MIDX4_HEADERLEN 12
503 static PyObject *merge_into(PyObject *self, PyObject *args)
505 PyObject *py_total, *ilist = NULL;
506 unsigned char *fmap = NULL;
507 struct sha *sha_ptr, *sha_start = NULL;
508 uint32_t *table_ptr, *name_ptr, *name_start;
509 struct idx **idxs = NULL;
513 uint32_t count, prefix;
517 if (!PyArg_ParseTuple(args, "w#iOO",
518 &fmap, &flen, &bits, &py_total, &ilist))
521 if (!bup_uint_from_py(&total, py_total, "total"))
524 num_i = PyList_Size(ilist);
525 idxs = (struct idx **)PyMem_Malloc(num_i * sizeof(struct idx *));
527 for (i = 0; i < num_i; i++)
529 long len, sha_ofs, name_map_ofs;
530 idxs[i] = (struct idx *)PyMem_Malloc(sizeof(struct idx));
531 PyObject *itup = PyList_GetItem(ilist, i);
532 if (!PyArg_ParseTuple(itup, "t#llli", &idxs[i]->map, &idxs[i]->bytes,
533 &len, &sha_ofs, &name_map_ofs, &idxs[i]->name_base))
535 idxs[i]->cur = (struct sha *)&idxs[i]->map[sha_ofs];
536 idxs[i]->end = &idxs[i]->cur[len];
538 idxs[i]->cur_name = (uint32_t *)&idxs[i]->map[name_map_ofs];
540 idxs[i]->cur_name = NULL;
542 table_ptr = (uint32_t *)&fmap[MIDX4_HEADERLEN];
543 sha_start = sha_ptr = (struct sha *)&table_ptr[1<<bits];
544 name_start = name_ptr = (uint32_t *)&sha_ptr[total];
553 if (count % 102424 == 0 && istty2)
554 fprintf(stderr, "midx: writing %.2f%% (%d/%d)\r",
555 count*100.0/total, count, total);
557 new_prefix = _extract_bits((unsigned char *)idx->cur, bits);
558 while (prefix < new_prefix)
559 table_ptr[prefix++] = htonl(count);
560 memcpy(sha_ptr++, idx->cur, sizeof(struct sha));
561 *name_ptr++ = htonl(_get_idx_i(idx));
563 if (idx->cur_name != NULL)
565 _fix_idx_order(idxs, &last_i);
568 while (prefix < (1<<bits))
569 table_ptr[prefix++] = htonl(count);
570 assert(count == total);
571 assert(prefix == (1<<bits));
572 assert(sha_ptr == sha_start+count);
573 assert(name_ptr == name_start+count);
576 return PyLong_FromUnsignedLong(count);
579 // This function should technically be macro'd out if it's going to be used
580 // more than ocasionally. As of this writing, it'll actually never be called
581 // in real world bup scenarios (because our packs are < MAX_INT bytes).
582 static uint64_t htonll(uint64_t value)
584 static const int endian_test = 42;
586 if (*(char *)&endian_test == endian_test) // LSB-MSB
587 return ((uint64_t)htonl(value & 0xFFFFFFFF) << 32) | htonl(value >> 32);
588 return value; // already in network byte order MSB-LSB
591 #define FAN_ENTRIES 256
593 static PyObject *write_idx(PyObject *self, PyObject *args)
595 char *filename = NULL;
596 PyObject *py_total, *idx = NULL;
598 unsigned char *fmap = NULL;
600 unsigned int total = 0;
602 int i, j, ofs64_count;
603 uint32_t *fan_ptr, *crc_ptr, *ofs_ptr;
607 if (!PyArg_ParseTuple(args, "sw#OO",
608 &filename, &fmap, &flen, &idx, &py_total))
611 if (!bup_uint_from_py(&total, py_total, "total"))
614 if (PyList_Size (idx) != FAN_ENTRIES) // Check for list of the right length.
615 return PyErr_Format (PyExc_TypeError, "idx must contain %d entries",
618 const char idx_header[] = "\377tOc\0\0\0\002";
619 memcpy (fmap, idx_header, sizeof(idx_header) - 1);
621 fan_ptr = (uint32_t *)&fmap[sizeof(idx_header) - 1];
622 sha_ptr = (struct sha *)&fan_ptr[FAN_ENTRIES];
623 crc_ptr = (uint32_t *)&sha_ptr[total];
624 ofs_ptr = (uint32_t *)&crc_ptr[total];
625 ofs64_ptr = (uint64_t *)&ofs_ptr[total];
629 for (i = 0; i < FAN_ENTRIES; ++i)
632 part = PyList_GET_ITEM(idx, i);
634 plen = PyList_GET_SIZE(part);
636 *fan_ptr++ = htonl(count);
637 for (j = 0; j < plen; ++j)
639 unsigned char *sha = NULL;
640 Py_ssize_t sha_len = 0;
641 PyObject *crc_py, *ofs_py;
643 unsigned PY_LONG_LONG ofs_ull;
645 if (!PyArg_ParseTuple(PyList_GET_ITEM(part, j), "t#OO",
646 &sha, &sha_len, &crc_py, &ofs_py))
648 if(!bup_uint_from_py(&crc, crc_py, "crc"))
650 if(!bup_ullong_from_py(&ofs_ull, ofs_py, "ofs"))
652 assert(crc <= UINT32_MAX);
653 assert(ofs_ull <= UINT64_MAX);
655 if (sha_len != sizeof(struct sha))
657 memcpy(sha_ptr++, sha, sizeof(struct sha));
658 *crc_ptr++ = htonl(crc);
659 if (ofs > 0x7fffffff)
661 *ofs64_ptr++ = htonll(ofs);
662 ofs = 0x80000000 | ofs64_count++;
664 *ofs_ptr++ = htonl((uint32_t)ofs);
668 int rc = msync(fmap, flen, MS_ASYNC);
670 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
672 return PyLong_FromUnsignedLong(count);
676 // I would have made this a lower-level function that just fills in a buffer
677 // with random values, and then written those values from python. But that's
678 // about 20% slower in my tests, and since we typically generate random
679 // numbers for benchmarking other parts of bup, any slowness in generating
680 // random bytes will make our benchmarks inaccurate. Plus nobody wants
681 // pseudorandom bytes much except for this anyway.
682 static PyObject *write_random(PyObject *self, PyObject *args)
684 uint32_t buf[1024/4];
685 int fd = -1, seed = 0, verbose = 0;
687 long long len = 0, kbytes = 0, written = 0;
689 if (!PyArg_ParseTuple(args, "iLii", &fd, &len, &seed, &verbose))
694 for (kbytes = 0; kbytes < len/1024; kbytes++)
697 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
699 ret = write(fd, buf, sizeof(buf));
703 if (ret < (int)sizeof(buf))
705 if (verbose && kbytes/1024 > 0 && !(kbytes%1024))
706 fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
709 // handle non-multiples of 1024
713 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
715 ret = write(fd, buf, len % 1024);
722 fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
723 return Py_BuildValue("L", written);
727 static PyObject *random_sha(PyObject *self, PyObject *args)
729 static int seeded = 0;
730 uint32_t shabuf[20/4];
735 assert(sizeof(shabuf) == 20);
740 if (!PyArg_ParseTuple(args, ""))
743 memset(shabuf, 0, sizeof(shabuf));
744 for (i=0; i < 20/4; i++)
745 shabuf[i] = random();
746 return Py_BuildValue("s#", shabuf, 20);
750 static int _open_noatime(const char *filename, int attrs)
752 int attrs_noatime, fd;
758 attrs |= O_LARGEFILE;
760 attrs_noatime = attrs;
762 attrs_noatime |= O_NOATIME;
764 fd = open(filename, attrs_noatime);
765 if (fd < 0 && errno == EPERM)
767 // older Linux kernels would return EPERM if you used O_NOATIME
768 // and weren't the file's owner. This pointless restriction was
769 // relaxed eventually, but we have to handle it anyway.
770 // (VERY old kernels didn't recognized O_NOATIME, but they would
771 // just harmlessly ignore it, so this branch won't trigger)
772 fd = open(filename, attrs);
778 static PyObject *open_noatime(PyObject *self, PyObject *args)
780 char *filename = NULL;
782 if (!PyArg_ParseTuple(args, "s", &filename))
784 fd = _open_noatime(filename, 0);
786 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
787 return Py_BuildValue("i", fd);
791 static PyObject *fadvise_done(PyObject *self, PyObject *args)
795 if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
797 #ifdef POSIX_FADV_DONTNEED
798 posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
800 return Py_BuildValue("");
804 #ifdef BUP_HAVE_FILE_ATTRS
805 static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
812 if (!PyArg_ParseTuple(args, "s", &path))
815 fd = _open_noatime(path, O_NONBLOCK);
817 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
820 rc = ioctl(fd, FS_IOC_GETFLAGS, &attr);
824 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
828 return Py_BuildValue("I", attr);
830 #endif /* def BUP_HAVE_FILE_ATTRS */
834 #ifdef BUP_HAVE_FILE_ATTRS
835 static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
838 unsigned int orig_attr, attr;
843 if (!PyArg_ParseTuple(args, "sO", &path, &py_attr))
846 if (!bup_uint_from_py(&attr, py_attr, "attr"))
849 fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
851 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
853 // Restrict attr to modifiable flags acdeijstuADST -- see
854 // chattr(1) and the e2fsprogs source. Letter to flag mapping is
855 // in pf.c flags_array[].
856 attr &= FS_APPEND_FL | FS_COMPR_FL | FS_NODUMP_FL | FS_EXTENT_FL
857 | FS_IMMUTABLE_FL | FS_JOURNAL_DATA_FL | FS_SECRM_FL | FS_NOTAIL_FL
858 | FS_UNRM_FL | FS_NOATIME_FL | FS_DIRSYNC_FL | FS_SYNC_FL
859 | FS_TOPDIR_FL | FS_NOCOW_FL;
861 // The extents flag can't be removed, so don't (see chattr(1) and chattr.c).
862 rc = ioctl(fd, FS_IOC_GETFLAGS, &orig_attr);
866 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
868 attr |= (orig_attr & FS_EXTENT_FL);
870 rc = ioctl(fd, FS_IOC_SETFLAGS, &attr);
874 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
878 return Py_BuildValue("O", Py_None);
880 #endif /* def BUP_HAVE_FILE_ATTRS */
883 #ifndef HAVE_UTIMENSAT
885 #error "cannot find utimensat or utimes()"
888 #error "cannot find utimensat or lutimes()"
893 #define INTEGRAL_ASSIGNMENT_FITS(dest, src) \
896 *(dest) == (src) && (*(dest) < 1) == ((src) < 1); \
900 #define ASSIGN_PYLONG_TO_INTEGRAL(dest, pylong, overflow) \
904 const long long ltmp = PyLong_AsLongLong(pylong); \
905 if (ltmp == -1 && PyErr_Occurred()) \
907 if (PyErr_ExceptionMatches(PyExc_OverflowError)) \
909 const unsigned long ultmp = PyLong_AsUnsignedLongLong(pylong); \
910 if (ultmp == (unsigned long long) -1 && PyErr_Occurred()) \
912 if (PyErr_ExceptionMatches(PyExc_OverflowError)) \
918 if (INTEGRAL_ASSIGNMENT_FITS((dest), ultmp)) \
926 if (INTEGRAL_ASSIGNMENT_FITS((dest), ltmp)) \
935 #ifdef HAVE_UTIMENSAT
937 static PyObject *bup_utimensat(PyObject *self, PyObject *args)
942 PyObject *access_py, *modification_py;
943 struct timespec ts[2];
945 if (!PyArg_ParseTuple(args, "is((Ol)(Ol))i",
948 &access_py, &(ts[0].tv_nsec),
949 &modification_py, &(ts[1].tv_nsec),
954 if (!ASSIGN_PYLONG_TO_INTEGRAL(&(ts[0].tv_sec), access_py, &overflow))
957 PyErr_SetString(PyExc_ValueError,
958 "unable to convert access time seconds for utimensat");
961 if (!ASSIGN_PYLONG_TO_INTEGRAL(&(ts[1].tv_sec), modification_py, &overflow))
964 PyErr_SetString(PyExc_ValueError,
965 "unable to convert modification time seconds for utimensat");
968 rc = utimensat(fd, path, ts, flag);
970 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
972 return Py_BuildValue("O", Py_None);
975 #endif /* def HAVE_UTIMENSAT */
978 #if defined(HAVE_UTIMES) || defined(HAVE_LUTIMES)
980 static int bup_parse_xutimes_args(char **path,
981 struct timeval tv[2],
984 PyObject *access_py, *modification_py;
985 long long access_us, modification_us; // POSIX guarantees tv_usec is signed.
987 if (!PyArg_ParseTuple(args, "s((OL)(OL))",
989 &access_py, &access_us,
990 &modification_py, &modification_us))
994 if (!ASSIGN_PYLONG_TO_INTEGRAL(&(tv[0].tv_sec), access_py, &overflow))
997 PyErr_SetString(PyExc_ValueError, "unable to convert access time seconds to timeval");
1000 if (!INTEGRAL_ASSIGNMENT_FITS(&(tv[0].tv_usec), access_us))
1002 PyErr_SetString(PyExc_ValueError, "unable to convert access time nanoseconds to timeval");
1005 if (!ASSIGN_PYLONG_TO_INTEGRAL(&(tv[1].tv_sec), modification_py, &overflow))
1008 PyErr_SetString(PyExc_ValueError, "unable to convert modification time seconds to timeval");
1011 if (!INTEGRAL_ASSIGNMENT_FITS(&(tv[1].tv_usec), modification_us))
1013 PyErr_SetString(PyExc_ValueError, "unable to convert modification time nanoseconds to timeval");
1019 #endif /* defined(HAVE_UTIMES) || defined(HAVE_LUTIMES) */
1023 static PyObject *bup_utimes(PyObject *self, PyObject *args)
1026 struct timeval tv[2];
1027 if (!bup_parse_xutimes_args(&path, tv, args))
1029 int rc = utimes(path, tv);
1031 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
1032 return Py_BuildValue("O", Py_None);
1034 #endif /* def HAVE_UTIMES */
1038 static PyObject *bup_lutimes(PyObject *self, PyObject *args)
1041 struct timeval tv[2];
1042 if (!bup_parse_xutimes_args(&path, tv, args))
1044 int rc = lutimes(path, tv);
1046 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
1048 return Py_BuildValue("O", Py_None);
1050 #endif /* def HAVE_LUTIMES */
1053 #ifdef HAVE_STAT_ST_ATIM
1054 # define BUP_STAT_ATIME_NS(st) (st)->st_atim.tv_nsec
1055 # define BUP_STAT_MTIME_NS(st) (st)->st_mtim.tv_nsec
1056 # define BUP_STAT_CTIME_NS(st) (st)->st_ctim.tv_nsec
1057 #elif defined HAVE_STAT_ST_ATIMENSEC
1058 # define BUP_STAT_ATIME_NS(st) (st)->st_atimespec.tv_nsec
1059 # define BUP_STAT_MTIME_NS(st) (st)->st_mtimespec.tv_nsec
1060 # define BUP_STAT_CTIME_NS(st) (st)->st_ctimespec.tv_nsec
1062 # define BUP_STAT_ATIME_NS(st) 0
1063 # define BUP_STAT_MTIME_NS(st) 0
1064 # define BUP_STAT_CTIME_NS(st) 0
1068 static PyObject *stat_struct_to_py(const struct stat *st,
1069 const char *filename,
1072 // We can check the known (via POSIX) signed and unsigned types at
1073 // compile time, but not (easily) the unspecified types, so handle
1074 // those via INTEGER_TO_PY(). Assumes ns values will fit in a
1076 return Py_BuildValue("OKOOOOOL(Ol)(Ol)(Ol)",
1077 INTEGER_TO_PY(st->st_mode),
1078 (unsigned PY_LONG_LONG) st->st_ino,
1079 INTEGER_TO_PY(st->st_dev),
1080 INTEGER_TO_PY(st->st_nlink),
1081 INTEGER_TO_PY(st->st_uid),
1082 INTEGER_TO_PY(st->st_gid),
1083 INTEGER_TO_PY(st->st_rdev),
1084 (PY_LONG_LONG) st->st_size,
1085 INTEGER_TO_PY(st->st_atime),
1086 (long) BUP_STAT_ATIME_NS(st),
1087 INTEGER_TO_PY(st->st_mtime),
1088 (long) BUP_STAT_MTIME_NS(st),
1089 INTEGER_TO_PY(st->st_ctime),
1090 (long) BUP_STAT_CTIME_NS(st));
1094 static PyObject *bup_stat(PyObject *self, PyObject *args)
1099 if (!PyArg_ParseTuple(args, "s", &filename))
1103 rc = stat(filename, &st);
1105 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
1106 return stat_struct_to_py(&st, filename, 0);
1110 static PyObject *bup_lstat(PyObject *self, PyObject *args)
1115 if (!PyArg_ParseTuple(args, "s", &filename))
1119 rc = lstat(filename, &st);
1121 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
1122 return stat_struct_to_py(&st, filename, 0);
1126 static PyObject *bup_fstat(PyObject *self, PyObject *args)
1130 if (!PyArg_ParseTuple(args, "i", &fd))
1134 rc = fstat(fd, &st);
1136 return PyErr_SetFromErrno(PyExc_OSError);
1137 return stat_struct_to_py(&st, NULL, fd);
1141 static PyMethodDef helper_methods[] = {
1142 { "selftest", selftest, METH_VARARGS,
1143 "Check that the rolling checksum rolls correctly (for unit tests)." },
1144 { "blobbits", blobbits, METH_VARARGS,
1145 "Return the number of bits in the rolling checksum." },
1146 { "splitbuf", splitbuf, METH_VARARGS,
1147 "Split a list of strings based on a rolling checksum." },
1148 { "bitmatch", bitmatch, METH_VARARGS,
1149 "Count the number of matching prefix bits between two strings." },
1150 { "firstword", firstword, METH_VARARGS,
1151 "Return an int corresponding to the first 32 bits of buf." },
1152 { "bloom_contains", bloom_contains, METH_VARARGS,
1153 "Check if a bloom filter of 2^nbits bytes contains an object" },
1154 { "bloom_add", bloom_add, METH_VARARGS,
1155 "Add an object to a bloom filter of 2^nbits bytes" },
1156 { "extract_bits", extract_bits, METH_VARARGS,
1157 "Take the first 'nbits' bits from 'buf' and return them as an int." },
1158 { "merge_into", merge_into, METH_VARARGS,
1159 "Merges a bunch of idx and midx files into a single midx." },
1160 { "write_idx", write_idx, METH_VARARGS,
1161 "Write a PackIdxV2 file from an idx list of lists of tuples" },
1162 { "write_random", write_random, METH_VARARGS,
1163 "Write random bytes to the given file descriptor" },
1164 { "random_sha", random_sha, METH_VARARGS,
1165 "Return a random 20-byte string" },
1166 { "open_noatime", open_noatime, METH_VARARGS,
1167 "open() the given filename for read with O_NOATIME if possible" },
1168 { "fadvise_done", fadvise_done, METH_VARARGS,
1169 "Inform the kernel that we're finished with earlier parts of a file" },
1170 #ifdef BUP_HAVE_FILE_ATTRS
1171 { "get_linux_file_attr", bup_get_linux_file_attr, METH_VARARGS,
1172 "Return the Linux attributes for the given file." },
1174 #ifdef BUP_HAVE_FILE_ATTRS
1175 { "set_linux_file_attr", bup_set_linux_file_attr, METH_VARARGS,
1176 "Set the Linux attributes for the given file." },
1178 #ifdef HAVE_UTIMENSAT
1179 { "bup_utimensat", bup_utimensat, METH_VARARGS,
1180 "Change path timestamps with nanosecond precision (POSIX)." },
1183 { "bup_utimes", bup_utimes, METH_VARARGS,
1184 "Change path timestamps with microsecond precision." },
1187 { "bup_lutimes", bup_lutimes, METH_VARARGS,
1188 "Change path timestamps with microsecond precision;"
1189 " don't follow symlinks." },
1191 { "stat", bup_stat, METH_VARARGS,
1192 "Extended version of stat." },
1193 { "lstat", bup_lstat, METH_VARARGS,
1194 "Extended version of lstat." },
1195 { "fstat", bup_fstat, METH_VARARGS,
1196 "Extended version of fstat." },
1197 { NULL, NULL, 0, NULL }, // sentinel
1201 PyMODINIT_FUNC init_helpers(void)
1203 // FIXME: migrate these tests to configure. Check against the
1204 // type we're going to use when passing to python. Other stat
1205 // types are tested at runtime.
1206 assert(sizeof(ino_t) <= sizeof(unsigned PY_LONG_LONG));
1207 assert(sizeof(off_t) <= sizeof(PY_LONG_LONG));
1208 assert(sizeof(blksize_t) <= sizeof(PY_LONG_LONG));
1209 assert(sizeof(blkcnt_t) <= sizeof(PY_LONG_LONG));
1210 // Just be sure (relevant when passing timestamps back to Python above).
1211 assert(sizeof(PY_LONG_LONG) <= sizeof(long long));
1212 assert(sizeof(unsigned PY_LONG_LONG) <= sizeof(unsigned long long));
1215 PyObject *m = Py_InitModule("_helpers", helper_methods);
1219 #ifdef HAVE_UTIMENSAT
1222 value = INTEGER_TO_PY(AT_FDCWD);
1223 PyObject_SetAttrString(m, "AT_FDCWD", value);
1225 value = INTEGER_TO_PY(AT_SYMLINK_NOFOLLOW);
1226 PyObject_SetAttrString(m, "AT_SYMLINK_NOFOLLOW", value);
1228 value = INTEGER_TO_PY(UTIME_NOW);
1229 PyObject_SetAttrString(m, "UTIME_NOW", value);
1234 e = getenv("BUP_FORCE_TTY");
1235 istty2 = isatty(2) || (atoi(e ? e : "0") & 2);