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_EXT2FS_EXT2_FS_H
31 #include <ext2fs/ext2_fs.h>
34 #if defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS) \
35 && defined(HAVE_EXT2FS_EXT2_FS_H)
36 #define BUP_HAVE_FILE_ATTRS 1
39 static int istty2 = 0;
41 // Probably we should use autoconf or something and set HAVE_PY_GETARGCARGV...
42 #if __WIN32__ || __CYGWIN__
44 // There's no 'ps' on win32 anyway, and Py_GetArgcArgv() isn't available.
45 static void unpythonize_argv(void) { }
47 #else // not __WIN32__
49 // For some reason this isn't declared in Python.h
50 extern void Py_GetArgcArgv(int *argc, char ***argv);
52 static void unpythonize_argv(void)
57 Py_GetArgcArgv(&argc, &argv);
59 for (i = 0; i < argc-1; i++)
61 if (argv[i] + strlen(argv[i]) + 1 != argv[i+1])
63 // The argv block doesn't work the way we expected; it's unsafe
69 arge = argv[argc-1] + strlen(argv[argc-1]) + 1;
71 if (strstr(argv[0], "python") && argv[1] == argv[0] + strlen(argv[0]) + 1)
75 p = strrchr(argv[1], '/');
81 memmove(argv[0], p, len);
82 memset(arge - diff, 0, diff);
83 for (i = 0; i < argc; i++)
84 argv[i] = argv[i+1] ? argv[i+1]-diff : NULL;
89 #endif // not __WIN32__ or __CYGWIN__
92 static PyObject *selftest(PyObject *self, PyObject *args)
94 if (!PyArg_ParseTuple(args, ""))
97 return Py_BuildValue("i", !bupsplit_selftest());
101 static PyObject *blobbits(PyObject *self, PyObject *args)
103 if (!PyArg_ParseTuple(args, ""))
105 return Py_BuildValue("i", BUP_BLOBBITS);
109 static PyObject *splitbuf(PyObject *self, PyObject *args)
111 unsigned char *buf = NULL;
112 int len = 0, out = 0, bits = -1;
114 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
116 out = bupsplit_find_ofs(buf, len, &bits);
117 if (out) assert(bits >= BUP_BLOBBITS);
118 return Py_BuildValue("ii", out, bits);
122 static PyObject *bitmatch(PyObject *self, PyObject *args)
124 unsigned char *buf1 = NULL, *buf2 = NULL;
125 int len1 = 0, len2 = 0;
128 if (!PyArg_ParseTuple(args, "t#t#", &buf1, &len1, &buf2, &len2))
132 for (byte = 0; byte < len1 && byte < len2; byte++)
134 int b1 = buf1[byte], b2 = buf2[byte];
137 for (bit = 0; bit < 8; bit++)
138 if ( (b1 & (0x80 >> bit)) != (b2 & (0x80 >> bit)) )
144 return Py_BuildValue("i", byte*8 + bit);
148 static PyObject *firstword(PyObject *self, PyObject *args)
150 unsigned char *buf = NULL;
154 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
160 v = ntohl(*(uint32_t *)buf);
161 return PyLong_FromUnsignedLong(v);
165 #define BLOOM2_HEADERLEN 16
172 static void to_bloom_address_bitmask4(const bits40_t *buf,
173 const int nbits, uint64_t *v, unsigned char *bitmask)
178 mask = (1<<nbits) - 1;
179 raw = (((uint64_t)ntohl(buf->high)) << 8) | buf->low;
180 bit = (raw >> (37-nbits)) & 0x7;
181 *v = (raw >> (40-nbits)) & mask;
185 static void to_bloom_address_bitmask5(const uint32_t *buf,
186 const int nbits, uint32_t *v, unsigned char *bitmask)
191 mask = (1<<nbits) - 1;
193 bit = (raw >> (29-nbits)) & 0x7;
194 *v = (raw >> (32-nbits)) & mask;
198 #define BLOOM_SET_BIT(name, address, itype, otype) \
199 static void name(unsigned char *bloom, const void *buf, const int nbits)\
201 unsigned char bitmask;\
203 address((itype *)buf, nbits, &v, &bitmask);\
204 bloom[BLOOM2_HEADERLEN+v] |= bitmask;\
206 BLOOM_SET_BIT(bloom_set_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
207 BLOOM_SET_BIT(bloom_set_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
210 #define BLOOM_GET_BIT(name, address, itype, otype) \
211 static int name(const unsigned char *bloom, const void *buf, const int nbits)\
213 unsigned char bitmask;\
215 address((itype *)buf, nbits, &v, &bitmask);\
216 return bloom[BLOOM2_HEADERLEN+v] & bitmask;\
218 BLOOM_GET_BIT(bloom_get_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
219 BLOOM_GET_BIT(bloom_get_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
222 static PyObject *bloom_add(PyObject *self, PyObject *args)
224 unsigned char *sha = NULL, *bloom = NULL;
226 int len = 0, blen = 0, nbits = 0, k = 0;
228 if (!PyArg_ParseTuple(args, "w#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
231 if (blen < 16+(1<<nbits) || len % 20 != 0)
238 for (end = sha + len; sha < end; sha += 20/k)
239 bloom_set_bit5(bloom, sha, nbits);
245 for (end = sha + len; sha < end; sha += 20/k)
246 bloom_set_bit4(bloom, sha, nbits);
252 return Py_BuildValue("i", len/20);
255 static PyObject *bloom_contains(PyObject *self, PyObject *args)
257 unsigned char *sha = NULL, *bloom = NULL;
258 int len = 0, blen = 0, nbits = 0, k = 0;
262 if (!PyArg_ParseTuple(args, "t#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
272 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
273 if (!bloom_get_bit5(bloom, sha, nbits))
274 return Py_BuildValue("Oi", Py_None, steps);
280 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
281 if (!bloom_get_bit4(bloom, sha, nbits))
282 return Py_BuildValue("Oi", Py_None, steps);
287 return Py_BuildValue("ii", 1, k);
291 static uint32_t _extract_bits(unsigned char *buf, int nbits)
295 mask = (1<<nbits) - 1;
296 v = ntohl(*(uint32_t *)buf);
297 v = (v >> (32-nbits)) & mask;
300 static PyObject *extract_bits(PyObject *self, PyObject *args)
302 unsigned char *buf = NULL;
303 int len = 0, nbits = 0;
305 if (!PyArg_ParseTuple(args, "t#i", &buf, &len, &nbits))
311 return PyLong_FromUnsignedLong(_extract_bits(buf, nbits));
316 unsigned char bytes[20];
328 static int _cmp_sha(const struct sha *sha1, const struct sha *sha2)
331 for (i = 0; i < sizeof(struct sha); i++)
332 if (sha1->bytes[i] != sha2->bytes[i])
333 return sha1->bytes[i] - sha2->bytes[i];
338 static void _fix_idx_order(struct idx **idxs, int *last_i)
341 int low, mid, high, c = 0;
344 if (idxs[*last_i]->cur >= idxs[*last_i]->end)
346 idxs[*last_i] = NULL;
359 mid = (low + high) / 2;
360 c = _cmp_sha(idx->cur, idxs[mid]->cur);
372 memmove(&idxs[mid+1], &idxs[mid], (*last_i-mid)*sizeof(struct idx *));
377 static uint32_t _get_idx_i(struct idx *idx)
379 if (idx->cur_name == NULL)
380 return idx->name_base;
381 return ntohl(*idx->cur_name) + idx->name_base;
384 #define MIDX4_HEADERLEN 12
386 static PyObject *merge_into(PyObject *self, PyObject *args)
388 PyObject *ilist = NULL;
389 unsigned char *fmap = NULL;
390 struct sha *sha_ptr, *sha_start = NULL;
391 uint32_t *table_ptr, *name_ptr, *name_start;
392 struct idx **idxs = NULL;
393 int flen = 0, bits = 0, i;
394 uint32_t total, count, prefix;
398 if (!PyArg_ParseTuple(args, "w#iIO", &fmap, &flen, &bits, &total, &ilist))
401 num_i = PyList_Size(ilist);
402 idxs = (struct idx **)PyMem_Malloc(num_i * sizeof(struct idx *));
404 for (i = 0; i < num_i; i++)
406 long len, sha_ofs, name_map_ofs;
407 idxs[i] = (struct idx *)PyMem_Malloc(sizeof(struct idx));
408 PyObject *itup = PyList_GetItem(ilist, i);
409 if (!PyArg_ParseTuple(itup, "t#llli", &idxs[i]->map, &idxs[i]->bytes,
410 &len, &sha_ofs, &name_map_ofs, &idxs[i]->name_base))
412 idxs[i]->cur = (struct sha *)&idxs[i]->map[sha_ofs];
413 idxs[i]->end = &idxs[i]->cur[len];
415 idxs[i]->cur_name = (uint32_t *)&idxs[i]->map[name_map_ofs];
417 idxs[i]->cur_name = NULL;
419 table_ptr = (uint32_t *)&fmap[MIDX4_HEADERLEN];
420 sha_start = sha_ptr = (struct sha *)&table_ptr[1<<bits];
421 name_start = name_ptr = (uint32_t *)&sha_ptr[total];
430 if (count % 102424 == 0 && istty2)
431 fprintf(stderr, "midx: writing %.2f%% (%d/%d)\r",
432 count*100.0/total, count, total);
434 new_prefix = _extract_bits((unsigned char *)idx->cur, bits);
435 while (prefix < new_prefix)
436 table_ptr[prefix++] = htonl(count);
437 memcpy(sha_ptr++, idx->cur, sizeof(struct sha));
438 *name_ptr++ = htonl(_get_idx_i(idx));
440 if (idx->cur_name != NULL)
442 _fix_idx_order(idxs, &last_i);
445 while (prefix < (1<<bits))
446 table_ptr[prefix++] = htonl(count);
447 assert(count == total);
448 assert(prefix == (1<<bits));
449 assert(sha_ptr == sha_start+count);
450 assert(name_ptr == name_start+count);
453 return PyLong_FromUnsignedLong(count);
456 // This function should technically be macro'd out if it's going to be used
457 // more than ocasionally. As of this writing, it'll actually never be called
458 // in real world bup scenarios (because our packs are < MAX_INT bytes).
459 static uint64_t htonll(uint64_t value)
461 static const int endian_test = 42;
463 if (*(char *)&endian_test == endian_test) // LSB-MSB
464 return ((uint64_t)htonl(value & 0xFFFFFFFF) << 32) | htonl(value >> 32);
465 return value; // already in network byte order MSB-LSB
468 #define PACK_IDX_V2_HEADERLEN 8
469 #define FAN_ENTRIES 256
471 static PyObject *write_idx(PyObject *self, PyObject *args)
473 PyObject *pf = NULL, *idx = NULL;
476 unsigned char *fmap = NULL;
480 int i, j, ofs64_count;
481 uint32_t *fan_ptr, *crc_ptr, *ofs_ptr;
484 if (!PyArg_ParseTuple(args, "Ow#OI", &pf, &fmap, &flen, &idx, &total))
487 fan_ptr = (uint32_t *)&fmap[PACK_IDX_V2_HEADERLEN];
488 sha_ptr = (struct sha *)&fan_ptr[FAN_ENTRIES];
489 crc_ptr = (uint32_t *)&sha_ptr[total];
490 ofs_ptr = (uint32_t *)&crc_ptr[total];
491 f = PyFile_AsFile(pf);
495 for (i = 0; i < FAN_ENTRIES; ++i)
498 part = PyList_GET_ITEM(idx, i);
500 plen = PyList_GET_SIZE(part);
502 *fan_ptr++ = htonl(count);
503 for (j = 0; j < plen; ++j)
505 unsigned char *sha = NULL;
509 if (!PyArg_ParseTuple(PyList_GET_ITEM(part, j), "t#IK",
510 &sha, &sha_len, &crc, &ofs))
512 if (sha_len != sizeof(struct sha))
514 memcpy(sha_ptr++, sha, sizeof(struct sha));
515 *crc_ptr++ = htonl(crc);
516 if (ofs > 0x7fffffff)
518 uint64_t nofs = htonll(ofs);
519 if (fwrite(&nofs, sizeof(uint64_t), 1, f) != 1)
520 return PyErr_SetFromErrno(PyExc_OSError);
521 ofs = 0x80000000 | ofs64_count++;
523 *ofs_ptr++ = htonl((uint32_t)ofs);
526 return PyLong_FromUnsignedLong(count);
530 // I would have made this a lower-level function that just fills in a buffer
531 // with random values, and then written those values from python. But that's
532 // about 20% slower in my tests, and since we typically generate random
533 // numbers for benchmarking other parts of bup, any slowness in generating
534 // random bytes will make our benchmarks inaccurate. Plus nobody wants
535 // pseudorandom bytes much except for this anyway.
536 static PyObject *write_random(PyObject *self, PyObject *args)
538 uint32_t buf[1024/4];
539 int fd = -1, seed = 0, verbose = 0;
541 long long len = 0, kbytes = 0, written = 0;
543 if (!PyArg_ParseTuple(args, "iLii", &fd, &len, &seed, &verbose))
548 for (kbytes = 0; kbytes < len/1024; kbytes++)
551 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
553 ret = write(fd, buf, sizeof(buf));
557 if (ret < (int)sizeof(buf))
559 if (verbose && kbytes/1024 > 0 && !(kbytes%1024))
560 fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
563 // handle non-multiples of 1024
567 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
569 ret = write(fd, buf, len % 1024);
576 fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
577 return Py_BuildValue("L", written);
581 static PyObject *random_sha(PyObject *self, PyObject *args)
583 static int seeded = 0;
584 uint32_t shabuf[20/4];
589 assert(sizeof(shabuf) == 20);
594 if (!PyArg_ParseTuple(args, ""))
597 memset(shabuf, 0, sizeof(shabuf));
598 for (i=0; i < 20/4; i++)
599 shabuf[i] = random();
600 return Py_BuildValue("s#", shabuf, 20);
604 static PyObject *open_noatime(PyObject *self, PyObject *args)
606 char *filename = NULL;
607 int attrs, attrs_noatime, fd;
608 if (!PyArg_ParseTuple(args, "s", &filename))
615 attrs |= O_LARGEFILE;
617 attrs_noatime = attrs;
619 attrs_noatime |= O_NOATIME;
621 fd = open(filename, attrs_noatime);
622 if (fd < 0 && errno == EPERM)
624 // older Linux kernels would return EPERM if you used O_NOATIME
625 // and weren't the file's owner. This pointless restriction was
626 // relaxed eventually, but we have to handle it anyway.
627 // (VERY old kernels didn't recognized O_NOATIME, but they would
628 // just harmlessly ignore it, so this branch won't trigger)
629 fd = open(filename, attrs);
632 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
633 return Py_BuildValue("i", fd);
637 static PyObject *fadvise_done(PyObject *self, PyObject *args)
641 if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
643 #ifdef POSIX_FADV_DONTNEED
644 posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
646 return Py_BuildValue("");
650 #ifdef BUP_HAVE_FILE_ATTRS
651 static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
658 if (!PyArg_ParseTuple(args, "s", &path))
661 fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
663 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
666 rc = ioctl(fd, FS_IOC_GETFLAGS, &attr);
670 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
674 return Py_BuildValue("k", attr);
676 #endif /* def BUP_HAVE_FILE_ATTRS */
679 #ifdef BUP_HAVE_FILE_ATTRS
680 static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
687 if (!PyArg_ParseTuple(args, "sk", &path, &attr))
690 fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
692 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
694 // Restrict attr to modifiable flags acdeijstuADST -- see
695 // chattr(1) and the e2fsprogs source. Letter to flag mapping is
696 // in pf.c flags_array[].
697 attr &= EXT2_APPEND_FL | EXT2_COMPR_FL | EXT2_NODUMP_FL | EXT4_EXTENTS_FL
698 | EXT2_IMMUTABLE_FL | EXT3_JOURNAL_DATA_FL | EXT2_SECRM_FL | EXT2_NOTAIL_FL
699 | EXT2_UNRM_FL | EXT2_NOATIME_FL | EXT2_DIRSYNC_FL | EXT2_SYNC_FL
702 rc = ioctl(fd, FS_IOC_SETFLAGS, &attr);
706 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
710 return Py_BuildValue("O", Py_None);
712 #endif /* def BUP_HAVE_FILE_ATTRS */
715 #if defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES) || defined(HAVE_LUTIMES)
717 static int bup_parse_xutime_args(char **path,
721 long *modification_ns,
722 PyObject *self, PyObject *args)
724 if (!PyArg_ParseTuple(args, "s((ll)(ll))",
727 modification, modification_ns))
732 PyErr_SetString(PyExc_ValueError, "access time is NaN");
735 else if (isinf(*access))
737 PyErr_SetString(PyExc_ValueError, "access time is infinite");
740 else if (isnan(*modification))
742 PyErr_SetString(PyExc_ValueError, "modification time is NaN");
745 else if (isinf(*modification))
747 PyErr_SetString(PyExc_ValueError, "modification time is infinite");
751 if (isnan(*access_ns))
753 PyErr_SetString(PyExc_ValueError, "access time ns is NaN");
756 else if (isinf(*access_ns))
758 PyErr_SetString(PyExc_ValueError, "access time ns is infinite");
761 else if (isnan(*modification_ns))
763 PyErr_SetString(PyExc_ValueError, "modification time ns is NaN");
766 else if (isinf(*modification_ns))
768 PyErr_SetString(PyExc_ValueError, "modification time ns is infinite");
775 #endif /* defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES)
776 || defined(HAVE_LUTIMES) */
779 #ifdef HAVE_UTIMENSAT
781 static PyObject *bup_xutime_ns(PyObject *self, PyObject *args,
786 long access, access_ns, modification, modification_ns;
787 struct timespec ts[2];
789 if (!bup_parse_xutime_args(&path, &access, &access_ns,
790 &modification, &modification_ns,
794 ts[0].tv_sec = access;
795 ts[0].tv_nsec = access_ns;
796 ts[1].tv_sec = modification;
797 ts[1].tv_nsec = modification_ns;
798 rc = utimensat(AT_FDCWD, path, ts,
799 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
801 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
803 return Py_BuildValue("O", Py_None);
807 #define BUP_HAVE_BUP_UTIME_NS 1
808 static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
810 return bup_xutime_ns(self, args, 1);
814 #define BUP_HAVE_BUP_LUTIME_NS 1
815 static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
817 return bup_xutime_ns(self, args, 0);
821 #else /* not defined(HAVE_UTIMENSAT) */
825 #define BUP_HAVE_BUP_UTIME_NS 1
826 static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
830 long access, access_ns, modification, modification_ns;
831 struct timeval tv[2];
833 if (!bup_parse_xutime_args(&path, &access, &access_ns,
834 &modification, &modification_ns,
838 tv[0].tv_sec = access;
839 tv[0].tv_usec = access_ns / 1000;
840 tv[1].tv_sec = modification;
841 tv[1].tv_usec = modification_ns / 1000;
842 rc = utimes(path, tv);
844 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
846 return Py_BuildValue("O", Py_None);
848 #endif /* def HAVE_UTIMES */
852 #define BUP_HAVE_BUP_LUTIME_NS 1
853 static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
857 long access, access_ns, modification, modification_ns;
858 struct timeval tv[2];
860 if (!bup_parse_xutime_args(&path, &access, &access_ns,
861 &modification, &modification_ns,
865 tv[0].tv_sec = access;
866 tv[0].tv_usec = access_ns / 1000;
867 tv[1].tv_sec = modification;
868 tv[1].tv_usec = modification_ns / 1000;
869 rc = lutimes(path, tv);
871 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
873 return Py_BuildValue("O", Py_None);
875 #endif /* def HAVE_LUTIMES */
878 #endif /* not defined(HAVE_UTIMENSAT) */
881 #ifdef HAVE_STAT_ST_ATIM
882 # define BUP_STAT_ATIME_NS(st) (st)->st_atim.tv_nsec
883 # define BUP_STAT_MTIME_NS(st) (st)->st_mtim.tv_nsec
884 # define BUP_STAT_CTIME_NS(st) (st)->st_ctim.tv_nsec
885 #elif defined HAVE_STAT_ST_ATIMENSEC
886 # define BUP_STAT_ATIME_NS(st) (st)->st_atimespec.tv_nsec
887 # define BUP_STAT_MTIME_NS(st) (st)->st_mtimespec.tv_nsec
888 # define BUP_STAT_CTIME_NS(st) (st)->st_ctimespec.tv_nsec
890 # define BUP_STAT_ATIME_NS(st) 0
891 # define BUP_STAT_MTIME_NS(st) 0
892 # define BUP_STAT_CTIME_NS(st) 0
896 static PyObject *stat_struct_to_py(const struct stat *st)
898 long atime_ns = BUP_STAT_ATIME_NS(st);
899 long mtime_ns = BUP_STAT_MTIME_NS(st);
900 long ctime_ns = BUP_STAT_CTIME_NS(st);
902 /* Enforce the current timespec nanosecond range expectations. */
903 if (atime_ns < 0 || atime_ns > 999999999)
905 PyErr_SetString(PyExc_ValueError, "invalid atime timespec nanoseconds");
908 if (mtime_ns < 0 || mtime_ns > 999999999)
910 PyErr_SetString(PyExc_ValueError, "invalid mtime timespec nanoseconds");
913 if (ctime_ns < 0 || ctime_ns > 999999999)
915 PyErr_SetString(PyExc_ValueError, "invalid ctime timespec nanoseconds");
919 return Py_BuildValue("kkkkkkkk(Ll)(Ll)(Ll)",
920 (unsigned long) st->st_mode,
921 (unsigned long) st->st_ino,
922 (unsigned long) st->st_dev,
923 (unsigned long) st->st_nlink,
924 (unsigned long) st->st_uid,
925 (unsigned long) st->st_gid,
926 (unsigned long) st->st_rdev,
927 (unsigned long) st->st_size,
928 (long long) st->st_atime,
930 (long long) st->st_mtime,
932 (long long) st->st_ctime,
937 static PyObject *bup_stat(PyObject *self, PyObject *args)
942 if (!PyArg_ParseTuple(args, "s", &filename))
946 rc = stat(filename, &st);
948 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
949 return stat_struct_to_py(&st);
953 static PyObject *bup_lstat(PyObject *self, PyObject *args)
958 if (!PyArg_ParseTuple(args, "s", &filename))
962 rc = lstat(filename, &st);
964 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
965 return stat_struct_to_py(&st);
969 static PyObject *bup_fstat(PyObject *self, PyObject *args)
973 if (!PyArg_ParseTuple(args, "i", &fd))
979 return PyErr_SetFromErrno(PyExc_OSError);
980 return stat_struct_to_py(&st);
984 static PyMethodDef helper_methods[] = {
985 { "selftest", selftest, METH_VARARGS,
986 "Check that the rolling checksum rolls correctly (for unit tests)." },
987 { "blobbits", blobbits, METH_VARARGS,
988 "Return the number of bits in the rolling checksum." },
989 { "splitbuf", splitbuf, METH_VARARGS,
990 "Split a list of strings based on a rolling checksum." },
991 { "bitmatch", bitmatch, METH_VARARGS,
992 "Count the number of matching prefix bits between two strings." },
993 { "firstword", firstword, METH_VARARGS,
994 "Return an int corresponding to the first 32 bits of buf." },
995 { "bloom_contains", bloom_contains, METH_VARARGS,
996 "Check if a bloom filter of 2^nbits bytes contains an object" },
997 { "bloom_add", bloom_add, METH_VARARGS,
998 "Add an object to a bloom filter of 2^nbits bytes" },
999 { "extract_bits", extract_bits, METH_VARARGS,
1000 "Take the first 'nbits' bits from 'buf' and return them as an int." },
1001 { "merge_into", merge_into, METH_VARARGS,
1002 "Merges a bunch of idx and midx files into a single midx." },
1003 { "write_idx", write_idx, METH_VARARGS,
1004 "Write a PackIdxV2 file from an idx list of lists of tuples" },
1005 { "write_random", write_random, METH_VARARGS,
1006 "Write random bytes to the given file descriptor" },
1007 { "random_sha", random_sha, METH_VARARGS,
1008 "Return a random 20-byte string" },
1009 { "open_noatime", open_noatime, METH_VARARGS,
1010 "open() the given filename for read with O_NOATIME if possible" },
1011 { "fadvise_done", fadvise_done, METH_VARARGS,
1012 "Inform the kernel that we're finished with earlier parts of a file" },
1013 #ifdef BUP_HAVE_FILE_ATTRS
1014 { "get_linux_file_attr", bup_get_linux_file_attr, METH_VARARGS,
1015 "Return the Linux attributes for the given file." },
1017 #ifdef BUP_HAVE_FILE_ATTRS
1018 { "set_linux_file_attr", bup_set_linux_file_attr, METH_VARARGS,
1019 "Set the Linux attributes for the given file." },
1021 #ifdef BUP_HAVE_BUP_UTIME_NS
1022 { "bup_utime_ns", bup_utime_ns, METH_VARARGS,
1023 "Change path timestamps with up to nanosecond precision." },
1025 #ifdef BUP_HAVE_BUP_LUTIME_NS
1026 { "bup_lutime_ns", bup_lutime_ns, METH_VARARGS,
1027 "Change path timestamps with up to nanosecond precision;"
1028 " don't follow symlinks." },
1030 { "stat", bup_stat, METH_VARARGS,
1031 "Extended version of stat." },
1032 { "lstat", bup_lstat, METH_VARARGS,
1033 "Extended version of lstat." },
1034 { "fstat", bup_fstat, METH_VARARGS,
1035 "Extended version of fstat." },
1036 { NULL, NULL, 0, NULL }, // sentinel
1040 PyMODINIT_FUNC init_helpers(void)
1043 PyObject *m = Py_InitModule("_helpers", helper_methods);
1046 e = getenv("BUP_FORCE_TTY");
1047 istty2 = isatty(2) || (atoi(e ? e : "0") & 2);