1 #define _LARGEFILE64_SOURCE 1
3 #include "../../config/config.h"
14 #ifdef HAVE_SYS_TYPES_H
15 #include <sys/types.h>
17 #ifdef HAVE_SYS_STAT_H
24 #ifdef HAVE_LINUX_FS_H
27 #ifdef HAVE_SYS_IOCTL_H
28 #include <sys/ioctl.h>
30 #ifdef HAVE_LINUX_EXT2_FS_H
31 #include <linux/ext2_fs.h>
34 #if defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS) \
35 && defined(HAVE_LINUX_EXT2_FS_H)
36 #define BUP_HAVE_FILE_ATTRS 1
40 // Of course, this assumes it's a bitfield value.
44 static int istty2 = 0;
46 // Probably we should use autoconf or something and set HAVE_PY_GETARGCARGV...
47 #if __WIN32__ || __CYGWIN__
49 // There's no 'ps' on win32 anyway, and Py_GetArgcArgv() isn't available.
50 static void unpythonize_argv(void) { }
52 #else // not __WIN32__
54 // For some reason this isn't declared in Python.h
55 extern void Py_GetArgcArgv(int *argc, char ***argv);
57 static void unpythonize_argv(void)
62 Py_GetArgcArgv(&argc, &argv);
64 for (i = 0; i < argc-1; i++)
66 if (argv[i] + strlen(argv[i]) + 1 != argv[i+1])
68 // The argv block doesn't work the way we expected; it's unsafe
74 arge = argv[argc-1] + strlen(argv[argc-1]) + 1;
76 if (strstr(argv[0], "python") && argv[1] == argv[0] + strlen(argv[0]) + 1)
80 p = strrchr(argv[1], '/');
86 memmove(argv[0], p, len);
87 memset(arge - diff, 0, diff);
88 for (i = 0; i < argc; i++)
89 argv[i] = argv[i+1] ? argv[i+1]-diff : NULL;
94 #endif // not __WIN32__ or __CYGWIN__
97 static PyObject *selftest(PyObject *self, PyObject *args)
99 if (!PyArg_ParseTuple(args, ""))
102 return Py_BuildValue("i", !bupsplit_selftest());
106 static PyObject *blobbits(PyObject *self, PyObject *args)
108 if (!PyArg_ParseTuple(args, ""))
110 return Py_BuildValue("i", BUP_BLOBBITS);
114 static PyObject *splitbuf(PyObject *self, PyObject *args)
116 unsigned char *buf = NULL;
117 int len = 0, out = 0, bits = -1;
119 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
121 out = bupsplit_find_ofs(buf, len, &bits);
122 if (out) assert(bits >= BUP_BLOBBITS);
123 return Py_BuildValue("ii", out, bits);
127 static PyObject *bitmatch(PyObject *self, PyObject *args)
129 unsigned char *buf1 = NULL, *buf2 = NULL;
130 int len1 = 0, len2 = 0;
133 if (!PyArg_ParseTuple(args, "t#t#", &buf1, &len1, &buf2, &len2))
137 for (byte = 0; byte < len1 && byte < len2; byte++)
139 int b1 = buf1[byte], b2 = buf2[byte];
142 for (bit = 0; bit < 8; bit++)
143 if ( (b1 & (0x80 >> bit)) != (b2 & (0x80 >> bit)) )
149 return Py_BuildValue("i", byte*8 + bit);
153 static PyObject *firstword(PyObject *self, PyObject *args)
155 unsigned char *buf = NULL;
159 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
165 v = ntohl(*(uint32_t *)buf);
166 return PyLong_FromUnsignedLong(v);
170 #define BLOOM2_HEADERLEN 16
177 static void to_bloom_address_bitmask4(const bits40_t *buf,
178 const int nbits, uint64_t *v, unsigned char *bitmask)
183 mask = (1<<nbits) - 1;
184 raw = (((uint64_t)ntohl(buf->high)) << 8) | buf->low;
185 bit = (raw >> (37-nbits)) & 0x7;
186 *v = (raw >> (40-nbits)) & mask;
190 static void to_bloom_address_bitmask5(const uint32_t *buf,
191 const int nbits, uint32_t *v, unsigned char *bitmask)
196 mask = (1<<nbits) - 1;
198 bit = (raw >> (29-nbits)) & 0x7;
199 *v = (raw >> (32-nbits)) & mask;
203 #define BLOOM_SET_BIT(name, address, itype, otype) \
204 static void name(unsigned char *bloom, const void *buf, const int nbits)\
206 unsigned char bitmask;\
208 address((itype *)buf, nbits, &v, &bitmask);\
209 bloom[BLOOM2_HEADERLEN+v] |= bitmask;\
211 BLOOM_SET_BIT(bloom_set_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
212 BLOOM_SET_BIT(bloom_set_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
215 #define BLOOM_GET_BIT(name, address, itype, otype) \
216 static int name(const unsigned char *bloom, const void *buf, const int nbits)\
218 unsigned char bitmask;\
220 address((itype *)buf, nbits, &v, &bitmask);\
221 return bloom[BLOOM2_HEADERLEN+v] & bitmask;\
223 BLOOM_GET_BIT(bloom_get_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
224 BLOOM_GET_BIT(bloom_get_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
227 static PyObject *bloom_add(PyObject *self, PyObject *args)
229 unsigned char *sha = NULL, *bloom = NULL;
231 int len = 0, blen = 0, nbits = 0, k = 0;
233 if (!PyArg_ParseTuple(args, "w#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
236 if (blen < 16+(1<<nbits) || len % 20 != 0)
243 for (end = sha + len; sha < end; sha += 20/k)
244 bloom_set_bit5(bloom, sha, nbits);
250 for (end = sha + len; sha < end; sha += 20/k)
251 bloom_set_bit4(bloom, sha, nbits);
257 return Py_BuildValue("i", len/20);
260 static PyObject *bloom_contains(PyObject *self, PyObject *args)
262 unsigned char *sha = NULL, *bloom = NULL;
263 int len = 0, blen = 0, nbits = 0, k = 0;
267 if (!PyArg_ParseTuple(args, "t#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
277 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
278 if (!bloom_get_bit5(bloom, sha, nbits))
279 return Py_BuildValue("Oi", Py_None, steps);
285 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
286 if (!bloom_get_bit4(bloom, sha, nbits))
287 return Py_BuildValue("Oi", Py_None, steps);
292 return Py_BuildValue("ii", 1, k);
296 static uint32_t _extract_bits(unsigned char *buf, int nbits)
300 mask = (1<<nbits) - 1;
301 v = ntohl(*(uint32_t *)buf);
302 v = (v >> (32-nbits)) & mask;
305 static PyObject *extract_bits(PyObject *self, PyObject *args)
307 unsigned char *buf = NULL;
308 int len = 0, nbits = 0;
310 if (!PyArg_ParseTuple(args, "t#i", &buf, &len, &nbits))
316 return PyLong_FromUnsignedLong(_extract_bits(buf, nbits));
321 unsigned char bytes[20];
333 static int _cmp_sha(const struct sha *sha1, const struct sha *sha2)
336 for (i = 0; i < sizeof(struct sha); i++)
337 if (sha1->bytes[i] != sha2->bytes[i])
338 return sha1->bytes[i] - sha2->bytes[i];
343 static void _fix_idx_order(struct idx **idxs, int *last_i)
346 int low, mid, high, c = 0;
349 if (idxs[*last_i]->cur >= idxs[*last_i]->end)
351 idxs[*last_i] = NULL;
364 mid = (low + high) / 2;
365 c = _cmp_sha(idx->cur, idxs[mid]->cur);
377 memmove(&idxs[mid+1], &idxs[mid], (*last_i-mid)*sizeof(struct idx *));
382 static uint32_t _get_idx_i(struct idx *idx)
384 if (idx->cur_name == NULL)
385 return idx->name_base;
386 return ntohl(*idx->cur_name) + idx->name_base;
389 #define MIDX4_HEADERLEN 12
391 static PyObject *merge_into(PyObject *self, PyObject *args)
393 PyObject *ilist = NULL;
394 unsigned char *fmap = NULL;
395 struct sha *sha_ptr, *sha_start = NULL;
396 uint32_t *table_ptr, *name_ptr, *name_start;
397 struct idx **idxs = NULL;
398 int flen = 0, bits = 0, i;
399 uint32_t total, count, prefix;
403 if (!PyArg_ParseTuple(args, "w#iIO", &fmap, &flen, &bits, &total, &ilist))
406 num_i = PyList_Size(ilist);
407 idxs = (struct idx **)PyMem_Malloc(num_i * sizeof(struct idx *));
409 for (i = 0; i < num_i; i++)
411 long len, sha_ofs, name_map_ofs;
412 idxs[i] = (struct idx *)PyMem_Malloc(sizeof(struct idx));
413 PyObject *itup = PyList_GetItem(ilist, i);
414 if (!PyArg_ParseTuple(itup, "t#llli", &idxs[i]->map, &idxs[i]->bytes,
415 &len, &sha_ofs, &name_map_ofs, &idxs[i]->name_base))
417 idxs[i]->cur = (struct sha *)&idxs[i]->map[sha_ofs];
418 idxs[i]->end = &idxs[i]->cur[len];
420 idxs[i]->cur_name = (uint32_t *)&idxs[i]->map[name_map_ofs];
422 idxs[i]->cur_name = NULL;
424 table_ptr = (uint32_t *)&fmap[MIDX4_HEADERLEN];
425 sha_start = sha_ptr = (struct sha *)&table_ptr[1<<bits];
426 name_start = name_ptr = (uint32_t *)&sha_ptr[total];
435 if (count % 102424 == 0 && istty2)
436 fprintf(stderr, "midx: writing %.2f%% (%d/%d)\r",
437 count*100.0/total, count, total);
439 new_prefix = _extract_bits((unsigned char *)idx->cur, bits);
440 while (prefix < new_prefix)
441 table_ptr[prefix++] = htonl(count);
442 memcpy(sha_ptr++, idx->cur, sizeof(struct sha));
443 *name_ptr++ = htonl(_get_idx_i(idx));
445 if (idx->cur_name != NULL)
447 _fix_idx_order(idxs, &last_i);
450 while (prefix < (1<<bits))
451 table_ptr[prefix++] = htonl(count);
452 assert(count == total);
453 assert(prefix == (1<<bits));
454 assert(sha_ptr == sha_start+count);
455 assert(name_ptr == name_start+count);
458 return PyLong_FromUnsignedLong(count);
461 // This function should technically be macro'd out if it's going to be used
462 // more than ocasionally. As of this writing, it'll actually never be called
463 // in real world bup scenarios (because our packs are < MAX_INT bytes).
464 static uint64_t htonll(uint64_t value)
466 static const int endian_test = 42;
468 if (*(char *)&endian_test == endian_test) // LSB-MSB
469 return ((uint64_t)htonl(value & 0xFFFFFFFF) << 32) | htonl(value >> 32);
470 return value; // already in network byte order MSB-LSB
473 #define PACK_IDX_V2_HEADERLEN 8
474 #define FAN_ENTRIES 256
476 static PyObject *write_idx(PyObject *self, PyObject *args)
478 PyObject *pf = NULL, *idx = NULL;
481 unsigned char *fmap = NULL;
485 int i, j, ofs64_count;
486 uint32_t *fan_ptr, *crc_ptr, *ofs_ptr;
489 if (!PyArg_ParseTuple(args, "Ow#OI", &pf, &fmap, &flen, &idx, &total))
492 fan_ptr = (uint32_t *)&fmap[PACK_IDX_V2_HEADERLEN];
493 sha_ptr = (struct sha *)&fan_ptr[FAN_ENTRIES];
494 crc_ptr = (uint32_t *)&sha_ptr[total];
495 ofs_ptr = (uint32_t *)&crc_ptr[total];
496 f = PyFile_AsFile(pf);
500 for (i = 0; i < FAN_ENTRIES; ++i)
503 part = PyList_GET_ITEM(idx, i);
505 plen = PyList_GET_SIZE(part);
507 *fan_ptr++ = htonl(count);
508 for (j = 0; j < plen; ++j)
510 unsigned char *sha = NULL;
514 if (!PyArg_ParseTuple(PyList_GET_ITEM(part, j), "t#IK",
515 &sha, &sha_len, &crc, &ofs))
517 if (sha_len != sizeof(struct sha))
519 memcpy(sha_ptr++, sha, sizeof(struct sha));
520 *crc_ptr++ = htonl(crc);
521 if (ofs > 0x7fffffff)
523 uint64_t nofs = htonll(ofs);
524 if (fwrite(&nofs, sizeof(uint64_t), 1, f) != 1)
525 return PyErr_SetFromErrno(PyExc_OSError);
526 ofs = 0x80000000 | ofs64_count++;
528 *ofs_ptr++ = htonl((uint32_t)ofs);
531 return PyLong_FromUnsignedLong(count);
535 // I would have made this a lower-level function that just fills in a buffer
536 // with random values, and then written those values from python. But that's
537 // about 20% slower in my tests, and since we typically generate random
538 // numbers for benchmarking other parts of bup, any slowness in generating
539 // random bytes will make our benchmarks inaccurate. Plus nobody wants
540 // pseudorandom bytes much except for this anyway.
541 static PyObject *write_random(PyObject *self, PyObject *args)
543 uint32_t buf[1024/4];
544 int fd = -1, seed = 0, verbose = 0;
546 long long len = 0, kbytes = 0, written = 0;
548 if (!PyArg_ParseTuple(args, "iLii", &fd, &len, &seed, &verbose))
553 for (kbytes = 0; kbytes < len/1024; kbytes++)
556 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
558 ret = write(fd, buf, sizeof(buf));
562 if (ret < (int)sizeof(buf))
564 if (verbose && kbytes/1024 > 0 && !(kbytes%1024))
565 fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
568 // handle non-multiples of 1024
572 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
574 ret = write(fd, buf, len % 1024);
581 fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
582 return Py_BuildValue("L", written);
586 static PyObject *random_sha(PyObject *self, PyObject *args)
588 static int seeded = 0;
589 uint32_t shabuf[20/4];
594 assert(sizeof(shabuf) == 20);
599 if (!PyArg_ParseTuple(args, ""))
602 memset(shabuf, 0, sizeof(shabuf));
603 for (i=0; i < 20/4; i++)
604 shabuf[i] = random();
605 return Py_BuildValue("s#", shabuf, 20);
609 static int _open_noatime(const char *filename, int attrs)
611 int attrs_noatime, fd;
617 attrs |= O_LARGEFILE;
619 attrs_noatime = attrs;
621 attrs_noatime |= O_NOATIME;
623 fd = open(filename, attrs_noatime);
624 if (fd < 0 && errno == EPERM)
626 // older Linux kernels would return EPERM if you used O_NOATIME
627 // and weren't the file's owner. This pointless restriction was
628 // relaxed eventually, but we have to handle it anyway.
629 // (VERY old kernels didn't recognized O_NOATIME, but they would
630 // just harmlessly ignore it, so this branch won't trigger)
631 fd = open(filename, attrs);
637 static PyObject *open_noatime(PyObject *self, PyObject *args)
639 char *filename = NULL;
641 if (!PyArg_ParseTuple(args, "s", &filename))
643 fd = _open_noatime(filename, 0);
645 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
646 return Py_BuildValue("i", fd);
650 static PyObject *fadvise_done(PyObject *self, PyObject *args)
654 if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
656 #ifdef POSIX_FADV_DONTNEED
657 posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
659 return Py_BuildValue("");
663 #ifdef BUP_HAVE_FILE_ATTRS
664 static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
671 if (!PyArg_ParseTuple(args, "s", &path))
674 fd = _open_noatime(path, O_NONBLOCK);
676 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
679 rc = ioctl(fd, FS_IOC_GETFLAGS, &attr);
683 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
687 return Py_BuildValue("k", attr);
689 #endif /* def BUP_HAVE_FILE_ATTRS */
692 #ifdef BUP_HAVE_FILE_ATTRS
693 static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
696 unsigned long orig_attr, attr;
700 if (!PyArg_ParseTuple(args, "sk", &path, &attr))
703 fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
705 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
707 // Restrict attr to modifiable flags acdeijstuADST -- see
708 // chattr(1) and the e2fsprogs source. Letter to flag mapping is
709 // in pf.c flags_array[].
710 attr &= FS_APPEND_FL | FS_COMPR_FL | FS_NODUMP_FL | FS_EXTENT_FL
711 | FS_IMMUTABLE_FL | FS_JOURNAL_DATA_FL | FS_SECRM_FL | FS_NOTAIL_FL
712 | FS_UNRM_FL | FS_NOATIME_FL | FS_DIRSYNC_FL | FS_SYNC_FL
713 | FS_TOPDIR_FL | FS_NOCOW_FL;
715 // The extents flag can't be removed, so don't (see chattr(1) and chattr.c).
716 rc = ioctl(fd, FS_IOC_GETFLAGS, &orig_attr);
720 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
722 attr |= (orig_attr & FS_EXTENT_FL);
724 rc = ioctl(fd, FS_IOC_SETFLAGS, &attr);
728 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
732 return Py_BuildValue("O", Py_None);
734 #endif /* def BUP_HAVE_FILE_ATTRS */
737 #if defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES) || defined(HAVE_LUTIMES)
739 static int bup_parse_xutime_args(char **path,
743 long *modification_ns,
744 PyObject *self, PyObject *args)
746 if (!PyArg_ParseTuple(args, "s((ll)(ll))",
749 modification, modification_ns))
754 PyErr_SetString(PyExc_ValueError, "access time is NaN");
757 else if (isinf(*access))
759 PyErr_SetString(PyExc_ValueError, "access time is infinite");
762 else if (isnan(*modification))
764 PyErr_SetString(PyExc_ValueError, "modification time is NaN");
767 else if (isinf(*modification))
769 PyErr_SetString(PyExc_ValueError, "modification time is infinite");
773 if (isnan(*access_ns))
775 PyErr_SetString(PyExc_ValueError, "access time ns is NaN");
778 else if (isinf(*access_ns))
780 PyErr_SetString(PyExc_ValueError, "access time ns is infinite");
783 else if (isnan(*modification_ns))
785 PyErr_SetString(PyExc_ValueError, "modification time ns is NaN");
788 else if (isinf(*modification_ns))
790 PyErr_SetString(PyExc_ValueError, "modification time ns is infinite");
797 #endif /* defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES)
798 || defined(HAVE_LUTIMES) */
801 #ifdef HAVE_UTIMENSAT
803 static PyObject *bup_xutime_ns(PyObject *self, PyObject *args,
808 long access, access_ns, modification, modification_ns;
809 struct timespec ts[2];
811 if (!bup_parse_xutime_args(&path, &access, &access_ns,
812 &modification, &modification_ns,
816 ts[0].tv_sec = access;
817 ts[0].tv_nsec = access_ns;
818 ts[1].tv_sec = modification;
819 ts[1].tv_nsec = modification_ns;
820 rc = utimensat(AT_FDCWD, path, ts,
821 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
823 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
825 return Py_BuildValue("O", Py_None);
829 #define BUP_HAVE_BUP_UTIME_NS 1
830 static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
832 return bup_xutime_ns(self, args, 1);
836 #define BUP_HAVE_BUP_LUTIME_NS 1
837 static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
839 return bup_xutime_ns(self, args, 0);
843 #else /* not defined(HAVE_UTIMENSAT) */
847 #define BUP_HAVE_BUP_UTIME_NS 1
848 static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
852 long access, access_ns, modification, modification_ns;
853 struct timeval tv[2];
855 if (!bup_parse_xutime_args(&path, &access, &access_ns,
856 &modification, &modification_ns,
860 tv[0].tv_sec = access;
861 tv[0].tv_usec = access_ns / 1000;
862 tv[1].tv_sec = modification;
863 tv[1].tv_usec = modification_ns / 1000;
864 rc = utimes(path, tv);
866 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
868 return Py_BuildValue("O", Py_None);
870 #endif /* def HAVE_UTIMES */
874 #define BUP_HAVE_BUP_LUTIME_NS 1
875 static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
879 long access, access_ns, modification, modification_ns;
880 struct timeval tv[2];
882 if (!bup_parse_xutime_args(&path, &access, &access_ns,
883 &modification, &modification_ns,
887 tv[0].tv_sec = access;
888 tv[0].tv_usec = access_ns / 1000;
889 tv[1].tv_sec = modification;
890 tv[1].tv_usec = modification_ns / 1000;
891 rc = lutimes(path, tv);
893 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
895 return Py_BuildValue("O", Py_None);
897 #endif /* def HAVE_LUTIMES */
900 #endif /* not defined(HAVE_UTIMENSAT) */
903 #ifdef HAVE_STAT_ST_ATIM
904 # define BUP_STAT_ATIME_NS(st) (st)->st_atim.tv_nsec
905 # define BUP_STAT_MTIME_NS(st) (st)->st_mtim.tv_nsec
906 # define BUP_STAT_CTIME_NS(st) (st)->st_ctim.tv_nsec
907 #elif defined HAVE_STAT_ST_ATIMENSEC
908 # define BUP_STAT_ATIME_NS(st) (st)->st_atimespec.tv_nsec
909 # define BUP_STAT_MTIME_NS(st) (st)->st_mtimespec.tv_nsec
910 # define BUP_STAT_CTIME_NS(st) (st)->st_ctimespec.tv_nsec
912 # define BUP_STAT_ATIME_NS(st) 0
913 # define BUP_STAT_MTIME_NS(st) 0
914 # define BUP_STAT_CTIME_NS(st) 0
918 static PyObject *stat_struct_to_py(const struct stat *st)
920 long atime_ns = BUP_STAT_ATIME_NS(st);
921 long mtime_ns = BUP_STAT_MTIME_NS(st);
922 long ctime_ns = BUP_STAT_CTIME_NS(st);
924 /* Enforce the current timespec nanosecond range expectations. */
925 if (atime_ns < 0 || atime_ns > 999999999)
927 PyErr_SetString(PyExc_ValueError, "invalid atime timespec nanoseconds");
930 if (mtime_ns < 0 || mtime_ns > 999999999)
932 PyErr_SetString(PyExc_ValueError, "invalid mtime timespec nanoseconds");
935 if (ctime_ns < 0 || ctime_ns > 999999999)
937 PyErr_SetString(PyExc_ValueError, "invalid ctime timespec nanoseconds");
941 return Py_BuildValue("kkkkkkkk(Ll)(Ll)(Ll)",
942 (unsigned long) st->st_mode,
943 (unsigned long) st->st_ino,
944 (unsigned long) st->st_dev,
945 (unsigned long) st->st_nlink,
946 (unsigned long) st->st_uid,
947 (unsigned long) st->st_gid,
948 (unsigned long) st->st_rdev,
949 (unsigned long) st->st_size,
950 (long long) st->st_atime,
952 (long long) st->st_mtime,
954 (long long) st->st_ctime,
959 static PyObject *bup_stat(PyObject *self, PyObject *args)
964 if (!PyArg_ParseTuple(args, "s", &filename))
968 rc = stat(filename, &st);
970 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
971 return stat_struct_to_py(&st);
975 static PyObject *bup_lstat(PyObject *self, PyObject *args)
980 if (!PyArg_ParseTuple(args, "s", &filename))
984 rc = lstat(filename, &st);
986 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
987 return stat_struct_to_py(&st);
991 static PyObject *bup_fstat(PyObject *self, PyObject *args)
995 if (!PyArg_ParseTuple(args, "i", &fd))
1001 return PyErr_SetFromErrno(PyExc_OSError);
1002 return stat_struct_to_py(&st);
1006 static PyMethodDef helper_methods[] = {
1007 { "selftest", selftest, METH_VARARGS,
1008 "Check that the rolling checksum rolls correctly (for unit tests)." },
1009 { "blobbits", blobbits, METH_VARARGS,
1010 "Return the number of bits in the rolling checksum." },
1011 { "splitbuf", splitbuf, METH_VARARGS,
1012 "Split a list of strings based on a rolling checksum." },
1013 { "bitmatch", bitmatch, METH_VARARGS,
1014 "Count the number of matching prefix bits between two strings." },
1015 { "firstword", firstword, METH_VARARGS,
1016 "Return an int corresponding to the first 32 bits of buf." },
1017 { "bloom_contains", bloom_contains, METH_VARARGS,
1018 "Check if a bloom filter of 2^nbits bytes contains an object" },
1019 { "bloom_add", bloom_add, METH_VARARGS,
1020 "Add an object to a bloom filter of 2^nbits bytes" },
1021 { "extract_bits", extract_bits, METH_VARARGS,
1022 "Take the first 'nbits' bits from 'buf' and return them as an int." },
1023 { "merge_into", merge_into, METH_VARARGS,
1024 "Merges a bunch of idx and midx files into a single midx." },
1025 { "write_idx", write_idx, METH_VARARGS,
1026 "Write a PackIdxV2 file from an idx list of lists of tuples" },
1027 { "write_random", write_random, METH_VARARGS,
1028 "Write random bytes to the given file descriptor" },
1029 { "random_sha", random_sha, METH_VARARGS,
1030 "Return a random 20-byte string" },
1031 { "open_noatime", open_noatime, METH_VARARGS,
1032 "open() the given filename for read with O_NOATIME if possible" },
1033 { "fadvise_done", fadvise_done, METH_VARARGS,
1034 "Inform the kernel that we're finished with earlier parts of a file" },
1035 #ifdef BUP_HAVE_FILE_ATTRS
1036 { "get_linux_file_attr", bup_get_linux_file_attr, METH_VARARGS,
1037 "Return the Linux attributes for the given file." },
1039 #ifdef BUP_HAVE_FILE_ATTRS
1040 { "set_linux_file_attr", bup_set_linux_file_attr, METH_VARARGS,
1041 "Set the Linux attributes for the given file." },
1043 #ifdef BUP_HAVE_BUP_UTIME_NS
1044 { "bup_utime_ns", bup_utime_ns, METH_VARARGS,
1045 "Change path timestamps with up to nanosecond precision." },
1047 #ifdef BUP_HAVE_BUP_LUTIME_NS
1048 { "bup_lutime_ns", bup_lutime_ns, METH_VARARGS,
1049 "Change path timestamps with up to nanosecond precision;"
1050 " don't follow symlinks." },
1052 { "stat", bup_stat, METH_VARARGS,
1053 "Extended version of stat." },
1054 { "lstat", bup_lstat, METH_VARARGS,
1055 "Extended version of lstat." },
1056 { "fstat", bup_fstat, METH_VARARGS,
1057 "Extended version of fstat." },
1058 { NULL, NULL, 0, NULL }, // sentinel
1062 PyMODINIT_FUNC init_helpers(void)
1065 PyObject *m = Py_InitModule("_helpers", helper_methods);
1068 e = getenv("BUP_FORCE_TTY");
1069 istty2 = isatty(2) || (atoi(e ? e : "0") & 2);