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 PyObject *open_noatime(PyObject *self, PyObject *args)
611 char *filename = NULL;
612 int attrs, attrs_noatime, fd;
613 if (!PyArg_ParseTuple(args, "s", &filename))
620 attrs |= O_LARGEFILE;
622 attrs_noatime = attrs;
624 attrs_noatime |= O_NOATIME;
626 fd = open(filename, attrs_noatime);
627 if (fd < 0 && errno == EPERM)
629 // older Linux kernels would return EPERM if you used O_NOATIME
630 // and weren't the file's owner. This pointless restriction was
631 // relaxed eventually, but we have to handle it anyway.
632 // (VERY old kernels didn't recognized O_NOATIME, but they would
633 // just harmlessly ignore it, so this branch won't trigger)
634 fd = open(filename, attrs);
637 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
638 return Py_BuildValue("i", fd);
642 static PyObject *fadvise_done(PyObject *self, PyObject *args)
646 if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
648 #ifdef POSIX_FADV_DONTNEED
649 posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
651 return Py_BuildValue("");
655 #ifdef BUP_HAVE_FILE_ATTRS
656 static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
663 if (!PyArg_ParseTuple(args, "s", &path))
666 fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
668 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
671 rc = ioctl(fd, FS_IOC_GETFLAGS, &attr);
675 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
679 return Py_BuildValue("k", attr);
681 #endif /* def BUP_HAVE_FILE_ATTRS */
684 #ifdef BUP_HAVE_FILE_ATTRS
685 static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
688 unsigned long orig_attr, attr;
692 if (!PyArg_ParseTuple(args, "sk", &path, &attr))
695 fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
697 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
699 // Restrict attr to modifiable flags acdeijstuADST -- see
700 // chattr(1) and the e2fsprogs source. Letter to flag mapping is
701 // in pf.c flags_array[].
702 attr &= FS_APPEND_FL | FS_COMPR_FL | FS_NODUMP_FL | FS_EXTENT_FL
703 | FS_IMMUTABLE_FL | FS_JOURNAL_DATA_FL | FS_SECRM_FL | FS_NOTAIL_FL
704 | FS_UNRM_FL | FS_NOATIME_FL | FS_DIRSYNC_FL | FS_SYNC_FL
705 | FS_TOPDIR_FL | FS_NOCOW_FL;
707 // The extents flag can't be removed, so don't (see chattr(1) and chattr.c).
708 rc = ioctl(fd, FS_IOC_GETFLAGS, &orig_attr);
712 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
714 attr |= (orig_attr & FS_EXTENT_FL);
716 rc = ioctl(fd, FS_IOC_SETFLAGS, &attr);
720 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
724 return Py_BuildValue("O", Py_None);
726 #endif /* def BUP_HAVE_FILE_ATTRS */
729 #if defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES) || defined(HAVE_LUTIMES)
731 static int bup_parse_xutime_args(char **path,
735 long *modification_ns,
736 PyObject *self, PyObject *args)
738 if (!PyArg_ParseTuple(args, "s((ll)(ll))",
741 modification, modification_ns))
746 PyErr_SetString(PyExc_ValueError, "access time is NaN");
749 else if (isinf(*access))
751 PyErr_SetString(PyExc_ValueError, "access time is infinite");
754 else if (isnan(*modification))
756 PyErr_SetString(PyExc_ValueError, "modification time is NaN");
759 else if (isinf(*modification))
761 PyErr_SetString(PyExc_ValueError, "modification time is infinite");
765 if (isnan(*access_ns))
767 PyErr_SetString(PyExc_ValueError, "access time ns is NaN");
770 else if (isinf(*access_ns))
772 PyErr_SetString(PyExc_ValueError, "access time ns is infinite");
775 else if (isnan(*modification_ns))
777 PyErr_SetString(PyExc_ValueError, "modification time ns is NaN");
780 else if (isinf(*modification_ns))
782 PyErr_SetString(PyExc_ValueError, "modification time ns is infinite");
789 #endif /* defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES)
790 || defined(HAVE_LUTIMES) */
793 #ifdef HAVE_UTIMENSAT
795 static PyObject *bup_xutime_ns(PyObject *self, PyObject *args,
800 long access, access_ns, modification, modification_ns;
801 struct timespec ts[2];
803 if (!bup_parse_xutime_args(&path, &access, &access_ns,
804 &modification, &modification_ns,
808 ts[0].tv_sec = access;
809 ts[0].tv_nsec = access_ns;
810 ts[1].tv_sec = modification;
811 ts[1].tv_nsec = modification_ns;
812 rc = utimensat(AT_FDCWD, path, ts,
813 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
815 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
817 return Py_BuildValue("O", Py_None);
821 #define BUP_HAVE_BUP_UTIME_NS 1
822 static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
824 return bup_xutime_ns(self, args, 1);
828 #define BUP_HAVE_BUP_LUTIME_NS 1
829 static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
831 return bup_xutime_ns(self, args, 0);
835 #else /* not defined(HAVE_UTIMENSAT) */
839 #define BUP_HAVE_BUP_UTIME_NS 1
840 static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
844 long access, access_ns, modification, modification_ns;
845 struct timeval tv[2];
847 if (!bup_parse_xutime_args(&path, &access, &access_ns,
848 &modification, &modification_ns,
852 tv[0].tv_sec = access;
853 tv[0].tv_usec = access_ns / 1000;
854 tv[1].tv_sec = modification;
855 tv[1].tv_usec = modification_ns / 1000;
856 rc = utimes(path, tv);
858 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
860 return Py_BuildValue("O", Py_None);
862 #endif /* def HAVE_UTIMES */
866 #define BUP_HAVE_BUP_LUTIME_NS 1
867 static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
871 long access, access_ns, modification, modification_ns;
872 struct timeval tv[2];
874 if (!bup_parse_xutime_args(&path, &access, &access_ns,
875 &modification, &modification_ns,
879 tv[0].tv_sec = access;
880 tv[0].tv_usec = access_ns / 1000;
881 tv[1].tv_sec = modification;
882 tv[1].tv_usec = modification_ns / 1000;
883 rc = lutimes(path, tv);
885 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
887 return Py_BuildValue("O", Py_None);
889 #endif /* def HAVE_LUTIMES */
892 #endif /* not defined(HAVE_UTIMENSAT) */
895 #ifdef HAVE_STAT_ST_ATIM
896 # define BUP_STAT_ATIME_NS(st) (st)->st_atim.tv_nsec
897 # define BUP_STAT_MTIME_NS(st) (st)->st_mtim.tv_nsec
898 # define BUP_STAT_CTIME_NS(st) (st)->st_ctim.tv_nsec
899 #elif defined HAVE_STAT_ST_ATIMENSEC
900 # define BUP_STAT_ATIME_NS(st) (st)->st_atimespec.tv_nsec
901 # define BUP_STAT_MTIME_NS(st) (st)->st_mtimespec.tv_nsec
902 # define BUP_STAT_CTIME_NS(st) (st)->st_ctimespec.tv_nsec
904 # define BUP_STAT_ATIME_NS(st) 0
905 # define BUP_STAT_MTIME_NS(st) 0
906 # define BUP_STAT_CTIME_NS(st) 0
910 static PyObject *stat_struct_to_py(const struct stat *st)
912 long atime_ns = BUP_STAT_ATIME_NS(st);
913 long mtime_ns = BUP_STAT_MTIME_NS(st);
914 long ctime_ns = BUP_STAT_CTIME_NS(st);
916 /* Enforce the current timespec nanosecond range expectations. */
917 if (atime_ns < 0 || atime_ns > 999999999)
919 PyErr_SetString(PyExc_ValueError, "invalid atime timespec nanoseconds");
922 if (mtime_ns < 0 || mtime_ns > 999999999)
924 PyErr_SetString(PyExc_ValueError, "invalid mtime timespec nanoseconds");
927 if (ctime_ns < 0 || ctime_ns > 999999999)
929 PyErr_SetString(PyExc_ValueError, "invalid ctime timespec nanoseconds");
933 return Py_BuildValue("kkkkkkkk(Ll)(Ll)(Ll)",
934 (unsigned long) st->st_mode,
935 (unsigned long) st->st_ino,
936 (unsigned long) st->st_dev,
937 (unsigned long) st->st_nlink,
938 (unsigned long) st->st_uid,
939 (unsigned long) st->st_gid,
940 (unsigned long) st->st_rdev,
941 (unsigned long) st->st_size,
942 (long long) st->st_atime,
944 (long long) st->st_mtime,
946 (long long) st->st_ctime,
951 static PyObject *bup_stat(PyObject *self, PyObject *args)
956 if (!PyArg_ParseTuple(args, "s", &filename))
960 rc = stat(filename, &st);
962 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
963 return stat_struct_to_py(&st);
967 static PyObject *bup_lstat(PyObject *self, PyObject *args)
972 if (!PyArg_ParseTuple(args, "s", &filename))
976 rc = lstat(filename, &st);
978 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
979 return stat_struct_to_py(&st);
983 static PyObject *bup_fstat(PyObject *self, PyObject *args)
987 if (!PyArg_ParseTuple(args, "i", &fd))
993 return PyErr_SetFromErrno(PyExc_OSError);
994 return stat_struct_to_py(&st);
998 static PyMethodDef helper_methods[] = {
999 { "selftest", selftest, METH_VARARGS,
1000 "Check that the rolling checksum rolls correctly (for unit tests)." },
1001 { "blobbits", blobbits, METH_VARARGS,
1002 "Return the number of bits in the rolling checksum." },
1003 { "splitbuf", splitbuf, METH_VARARGS,
1004 "Split a list of strings based on a rolling checksum." },
1005 { "bitmatch", bitmatch, METH_VARARGS,
1006 "Count the number of matching prefix bits between two strings." },
1007 { "firstword", firstword, METH_VARARGS,
1008 "Return an int corresponding to the first 32 bits of buf." },
1009 { "bloom_contains", bloom_contains, METH_VARARGS,
1010 "Check if a bloom filter of 2^nbits bytes contains an object" },
1011 { "bloom_add", bloom_add, METH_VARARGS,
1012 "Add an object to a bloom filter of 2^nbits bytes" },
1013 { "extract_bits", extract_bits, METH_VARARGS,
1014 "Take the first 'nbits' bits from 'buf' and return them as an int." },
1015 { "merge_into", merge_into, METH_VARARGS,
1016 "Merges a bunch of idx and midx files into a single midx." },
1017 { "write_idx", write_idx, METH_VARARGS,
1018 "Write a PackIdxV2 file from an idx list of lists of tuples" },
1019 { "write_random", write_random, METH_VARARGS,
1020 "Write random bytes to the given file descriptor" },
1021 { "random_sha", random_sha, METH_VARARGS,
1022 "Return a random 20-byte string" },
1023 { "open_noatime", open_noatime, METH_VARARGS,
1024 "open() the given filename for read with O_NOATIME if possible" },
1025 { "fadvise_done", fadvise_done, METH_VARARGS,
1026 "Inform the kernel that we're finished with earlier parts of a file" },
1027 #ifdef BUP_HAVE_FILE_ATTRS
1028 { "get_linux_file_attr", bup_get_linux_file_attr, METH_VARARGS,
1029 "Return the Linux attributes for the given file." },
1031 #ifdef BUP_HAVE_FILE_ATTRS
1032 { "set_linux_file_attr", bup_set_linux_file_attr, METH_VARARGS,
1033 "Set the Linux attributes for the given file." },
1035 #ifdef BUP_HAVE_BUP_UTIME_NS
1036 { "bup_utime_ns", bup_utime_ns, METH_VARARGS,
1037 "Change path timestamps with up to nanosecond precision." },
1039 #ifdef BUP_HAVE_BUP_LUTIME_NS
1040 { "bup_lutime_ns", bup_lutime_ns, METH_VARARGS,
1041 "Change path timestamps with up to nanosecond precision;"
1042 " don't follow symlinks." },
1044 { "stat", bup_stat, METH_VARARGS,
1045 "Extended version of stat." },
1046 { "lstat", bup_lstat, METH_VARARGS,
1047 "Extended version of lstat." },
1048 { "fstat", bup_fstat, METH_VARARGS,
1049 "Extended version of fstat." },
1050 { NULL, NULL, 0, NULL }, // sentinel
1054 PyMODINIT_FUNC init_helpers(void)
1057 PyObject *m = Py_InitModule("_helpers", helper_methods);
1060 e = getenv("BUP_FORCE_TTY");
1061 istty2 = isatty(2) || (atoi(e ? e : "0") & 2);