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>
31 #if defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS)
32 #define BUP_HAVE_FILE_ATTRS 1
36 // Of course, this assumes it's a bitfield value.
40 static int istty2 = 0;
42 // Probably we should use autoconf or something and set HAVE_PY_GETARGCARGV...
43 #if __WIN32__ || __CYGWIN__
45 // There's no 'ps' on win32 anyway, and Py_GetArgcArgv() isn't available.
46 static void unpythonize_argv(void) { }
48 #else // not __WIN32__
50 // For some reason this isn't declared in Python.h
51 extern void Py_GetArgcArgv(int *argc, char ***argv);
53 static void unpythonize_argv(void)
58 Py_GetArgcArgv(&argc, &argv);
60 for (i = 0; i < argc-1; i++)
62 if (argv[i] + strlen(argv[i]) + 1 != argv[i+1])
64 // The argv block doesn't work the way we expected; it's unsafe
70 arge = argv[argc-1] + strlen(argv[argc-1]) + 1;
72 if (strstr(argv[0], "python") && argv[1] == argv[0] + strlen(argv[0]) + 1)
76 p = strrchr(argv[1], '/');
82 memmove(argv[0], p, len);
83 memset(arge - diff, 0, diff);
84 for (i = 0; i < argc; i++)
85 argv[i] = argv[i+1] ? argv[i+1]-diff : NULL;
90 #endif // not __WIN32__ or __CYGWIN__
93 static PyObject *selftest(PyObject *self, PyObject *args)
95 if (!PyArg_ParseTuple(args, ""))
98 return Py_BuildValue("i", !bupsplit_selftest());
102 static PyObject *blobbits(PyObject *self, PyObject *args)
104 if (!PyArg_ParseTuple(args, ""))
106 return Py_BuildValue("i", BUP_BLOBBITS);
110 static PyObject *splitbuf(PyObject *self, PyObject *args)
112 unsigned char *buf = NULL;
113 int len = 0, out = 0, bits = -1;
115 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
117 out = bupsplit_find_ofs(buf, len, &bits);
118 if (out) assert(bits >= BUP_BLOBBITS);
119 return Py_BuildValue("ii", out, bits);
123 static PyObject *bitmatch(PyObject *self, PyObject *args)
125 unsigned char *buf1 = NULL, *buf2 = NULL;
126 int len1 = 0, len2 = 0;
129 if (!PyArg_ParseTuple(args, "t#t#", &buf1, &len1, &buf2, &len2))
133 for (byte = 0; byte < len1 && byte < len2; byte++)
135 int b1 = buf1[byte], b2 = buf2[byte];
138 for (bit = 0; bit < 8; bit++)
139 if ( (b1 & (0x80 >> bit)) != (b2 & (0x80 >> bit)) )
145 return Py_BuildValue("i", byte*8 + bit);
149 static PyObject *firstword(PyObject *self, PyObject *args)
151 unsigned char *buf = NULL;
155 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
161 v = ntohl(*(uint32_t *)buf);
162 return PyLong_FromUnsignedLong(v);
166 #define BLOOM2_HEADERLEN 16
173 static void to_bloom_address_bitmask4(const bits40_t *buf,
174 const int nbits, uint64_t *v, unsigned char *bitmask)
179 mask = (1<<nbits) - 1;
180 raw = (((uint64_t)ntohl(buf->high)) << 8) | buf->low;
181 bit = (raw >> (37-nbits)) & 0x7;
182 *v = (raw >> (40-nbits)) & mask;
186 static void to_bloom_address_bitmask5(const uint32_t *buf,
187 const int nbits, uint32_t *v, unsigned char *bitmask)
192 mask = (1<<nbits) - 1;
194 bit = (raw >> (29-nbits)) & 0x7;
195 *v = (raw >> (32-nbits)) & mask;
199 #define BLOOM_SET_BIT(name, address, itype, otype) \
200 static void name(unsigned char *bloom, const void *buf, const int nbits)\
202 unsigned char bitmask;\
204 address((itype *)buf, nbits, &v, &bitmask);\
205 bloom[BLOOM2_HEADERLEN+v] |= bitmask;\
207 BLOOM_SET_BIT(bloom_set_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
208 BLOOM_SET_BIT(bloom_set_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
211 #define BLOOM_GET_BIT(name, address, itype, otype) \
212 static int name(const unsigned char *bloom, const void *buf, const int nbits)\
214 unsigned char bitmask;\
216 address((itype *)buf, nbits, &v, &bitmask);\
217 return bloom[BLOOM2_HEADERLEN+v] & bitmask;\
219 BLOOM_GET_BIT(bloom_get_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
220 BLOOM_GET_BIT(bloom_get_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
223 static PyObject *bloom_add(PyObject *self, PyObject *args)
225 unsigned char *sha = NULL, *bloom = NULL;
227 int len = 0, blen = 0, nbits = 0, k = 0;
229 if (!PyArg_ParseTuple(args, "w#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
232 if (blen < 16+(1<<nbits) || len % 20 != 0)
239 for (end = sha + len; sha < end; sha += 20/k)
240 bloom_set_bit5(bloom, sha, nbits);
246 for (end = sha + len; sha < end; sha += 20/k)
247 bloom_set_bit4(bloom, sha, nbits);
253 return Py_BuildValue("i", len/20);
256 static PyObject *bloom_contains(PyObject *self, PyObject *args)
258 unsigned char *sha = NULL, *bloom = NULL;
259 int len = 0, blen = 0, nbits = 0, k = 0;
263 if (!PyArg_ParseTuple(args, "t#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
273 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
274 if (!bloom_get_bit5(bloom, sha, nbits))
275 return Py_BuildValue("Oi", Py_None, steps);
281 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
282 if (!bloom_get_bit4(bloom, sha, nbits))
283 return Py_BuildValue("Oi", Py_None, steps);
288 return Py_BuildValue("ii", 1, k);
292 static uint32_t _extract_bits(unsigned char *buf, int nbits)
296 mask = (1<<nbits) - 1;
297 v = ntohl(*(uint32_t *)buf);
298 v = (v >> (32-nbits)) & mask;
301 static PyObject *extract_bits(PyObject *self, PyObject *args)
303 unsigned char *buf = NULL;
304 int len = 0, nbits = 0;
306 if (!PyArg_ParseTuple(args, "t#i", &buf, &len, &nbits))
312 return PyLong_FromUnsignedLong(_extract_bits(buf, nbits));
317 unsigned char bytes[20];
329 static int _cmp_sha(const struct sha *sha1, const struct sha *sha2)
332 for (i = 0; i < sizeof(struct sha); i++)
333 if (sha1->bytes[i] != sha2->bytes[i])
334 return sha1->bytes[i] - sha2->bytes[i];
339 static void _fix_idx_order(struct idx **idxs, int *last_i)
342 int low, mid, high, c = 0;
345 if (idxs[*last_i]->cur >= idxs[*last_i]->end)
347 idxs[*last_i] = NULL;
360 mid = (low + high) / 2;
361 c = _cmp_sha(idx->cur, idxs[mid]->cur);
373 memmove(&idxs[mid+1], &idxs[mid], (*last_i-mid)*sizeof(struct idx *));
378 static uint32_t _get_idx_i(struct idx *idx)
380 if (idx->cur_name == NULL)
381 return idx->name_base;
382 return ntohl(*idx->cur_name) + idx->name_base;
385 #define MIDX4_HEADERLEN 12
387 static PyObject *merge_into(PyObject *self, PyObject *args)
389 PyObject *ilist = NULL;
390 unsigned char *fmap = NULL;
391 struct sha *sha_ptr, *sha_start = NULL;
392 uint32_t *table_ptr, *name_ptr, *name_start;
393 struct idx **idxs = NULL;
394 int flen = 0, bits = 0, i;
395 uint32_t total, count, prefix;
399 if (!PyArg_ParseTuple(args, "w#iIO", &fmap, &flen, &bits, &total, &ilist))
402 num_i = PyList_Size(ilist);
403 idxs = (struct idx **)PyMem_Malloc(num_i * sizeof(struct idx *));
405 for (i = 0; i < num_i; i++)
407 long len, sha_ofs, name_map_ofs;
408 idxs[i] = (struct idx *)PyMem_Malloc(sizeof(struct idx));
409 PyObject *itup = PyList_GetItem(ilist, i);
410 if (!PyArg_ParseTuple(itup, "t#llli", &idxs[i]->map, &idxs[i]->bytes,
411 &len, &sha_ofs, &name_map_ofs, &idxs[i]->name_base))
413 idxs[i]->cur = (struct sha *)&idxs[i]->map[sha_ofs];
414 idxs[i]->end = &idxs[i]->cur[len];
416 idxs[i]->cur_name = (uint32_t *)&idxs[i]->map[name_map_ofs];
418 idxs[i]->cur_name = NULL;
420 table_ptr = (uint32_t *)&fmap[MIDX4_HEADERLEN];
421 sha_start = sha_ptr = (struct sha *)&table_ptr[1<<bits];
422 name_start = name_ptr = (uint32_t *)&sha_ptr[total];
431 if (count % 102424 == 0 && istty2)
432 fprintf(stderr, "midx: writing %.2f%% (%d/%d)\r",
433 count*100.0/total, count, total);
435 new_prefix = _extract_bits((unsigned char *)idx->cur, bits);
436 while (prefix < new_prefix)
437 table_ptr[prefix++] = htonl(count);
438 memcpy(sha_ptr++, idx->cur, sizeof(struct sha));
439 *name_ptr++ = htonl(_get_idx_i(idx));
441 if (idx->cur_name != NULL)
443 _fix_idx_order(idxs, &last_i);
446 while (prefix < (1<<bits))
447 table_ptr[prefix++] = htonl(count);
448 assert(count == total);
449 assert(prefix == (1<<bits));
450 assert(sha_ptr == sha_start+count);
451 assert(name_ptr == name_start+count);
454 return PyLong_FromUnsignedLong(count);
457 // This function should technically be macro'd out if it's going to be used
458 // more than ocasionally. As of this writing, it'll actually never be called
459 // in real world bup scenarios (because our packs are < MAX_INT bytes).
460 static uint64_t htonll(uint64_t value)
462 static const int endian_test = 42;
464 if (*(char *)&endian_test == endian_test) // LSB-MSB
465 return ((uint64_t)htonl(value & 0xFFFFFFFF) << 32) | htonl(value >> 32);
466 return value; // already in network byte order MSB-LSB
469 #define PACK_IDX_V2_HEADERLEN 8
470 #define FAN_ENTRIES 256
472 static PyObject *write_idx(PyObject *self, PyObject *args)
474 PyObject *pf = NULL, *idx = NULL;
477 unsigned char *fmap = NULL;
481 int i, j, ofs64_count;
482 uint32_t *fan_ptr, *crc_ptr, *ofs_ptr;
485 if (!PyArg_ParseTuple(args, "Ow#OI", &pf, &fmap, &flen, &idx, &total))
488 fan_ptr = (uint32_t *)&fmap[PACK_IDX_V2_HEADERLEN];
489 sha_ptr = (struct sha *)&fan_ptr[FAN_ENTRIES];
490 crc_ptr = (uint32_t *)&sha_ptr[total];
491 ofs_ptr = (uint32_t *)&crc_ptr[total];
492 f = PyFile_AsFile(pf);
496 for (i = 0; i < FAN_ENTRIES; ++i)
499 part = PyList_GET_ITEM(idx, i);
501 plen = PyList_GET_SIZE(part);
503 *fan_ptr++ = htonl(count);
504 for (j = 0; j < plen; ++j)
506 unsigned char *sha = NULL;
510 if (!PyArg_ParseTuple(PyList_GET_ITEM(part, j), "t#IK",
511 &sha, &sha_len, &crc, &ofs))
513 if (sha_len != sizeof(struct sha))
515 memcpy(sha_ptr++, sha, sizeof(struct sha));
516 *crc_ptr++ = htonl(crc);
517 if (ofs > 0x7fffffff)
519 uint64_t nofs = htonll(ofs);
520 if (fwrite(&nofs, sizeof(uint64_t), 1, f) != 1)
521 return PyErr_SetFromErrno(PyExc_OSError);
522 ofs = 0x80000000 | ofs64_count++;
524 *ofs_ptr++ = htonl((uint32_t)ofs);
527 return PyLong_FromUnsignedLong(count);
531 // I would have made this a lower-level function that just fills in a buffer
532 // with random values, and then written those values from python. But that's
533 // about 20% slower in my tests, and since we typically generate random
534 // numbers for benchmarking other parts of bup, any slowness in generating
535 // random bytes will make our benchmarks inaccurate. Plus nobody wants
536 // pseudorandom bytes much except for this anyway.
537 static PyObject *write_random(PyObject *self, PyObject *args)
539 uint32_t buf[1024/4];
540 int fd = -1, seed = 0, verbose = 0;
542 long long len = 0, kbytes = 0, written = 0;
544 if (!PyArg_ParseTuple(args, "iLii", &fd, &len, &seed, &verbose))
549 for (kbytes = 0; kbytes < len/1024; kbytes++)
552 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
554 ret = write(fd, buf, sizeof(buf));
558 if (ret < (int)sizeof(buf))
560 if (verbose && kbytes/1024 > 0 && !(kbytes%1024))
561 fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
564 // handle non-multiples of 1024
568 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
570 ret = write(fd, buf, len % 1024);
577 fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
578 return Py_BuildValue("L", written);
582 static PyObject *random_sha(PyObject *self, PyObject *args)
584 static int seeded = 0;
585 uint32_t shabuf[20/4];
590 assert(sizeof(shabuf) == 20);
595 if (!PyArg_ParseTuple(args, ""))
598 memset(shabuf, 0, sizeof(shabuf));
599 for (i=0; i < 20/4; i++)
600 shabuf[i] = random();
601 return Py_BuildValue("s#", shabuf, 20);
605 static int _open_noatime(const char *filename, int attrs)
607 int attrs_noatime, fd;
613 attrs |= O_LARGEFILE;
615 attrs_noatime = attrs;
617 attrs_noatime |= O_NOATIME;
619 fd = open(filename, attrs_noatime);
620 if (fd < 0 && errno == EPERM)
622 // older Linux kernels would return EPERM if you used O_NOATIME
623 // and weren't the file's owner. This pointless restriction was
624 // relaxed eventually, but we have to handle it anyway.
625 // (VERY old kernels didn't recognized O_NOATIME, but they would
626 // just harmlessly ignore it, so this branch won't trigger)
627 fd = open(filename, attrs);
633 static PyObject *open_noatime(PyObject *self, PyObject *args)
635 char *filename = NULL;
637 if (!PyArg_ParseTuple(args, "s", &filename))
639 fd = _open_noatime(filename, 0);
641 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
642 return Py_BuildValue("i", fd);
646 static PyObject *fadvise_done(PyObject *self, PyObject *args)
650 if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
652 #ifdef POSIX_FADV_DONTNEED
653 posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
655 return Py_BuildValue("");
659 #ifdef BUP_HAVE_FILE_ATTRS
660 static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
667 if (!PyArg_ParseTuple(args, "s", &path))
670 fd = _open_noatime(path, O_NONBLOCK);
672 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
675 rc = ioctl(fd, FS_IOC_GETFLAGS, &attr);
679 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
683 return Py_BuildValue("k", attr);
685 #endif /* def BUP_HAVE_FILE_ATTRS */
688 #ifdef BUP_HAVE_FILE_ATTRS
689 static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
692 unsigned long orig_attr, attr;
696 if (!PyArg_ParseTuple(args, "sk", &path, &attr))
699 fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
701 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
703 // Restrict attr to modifiable flags acdeijstuADST -- see
704 // chattr(1) and the e2fsprogs source. Letter to flag mapping is
705 // in pf.c flags_array[].
706 attr &= FS_APPEND_FL | FS_COMPR_FL | FS_NODUMP_FL | FS_EXTENT_FL
707 | FS_IMMUTABLE_FL | FS_JOURNAL_DATA_FL | FS_SECRM_FL | FS_NOTAIL_FL
708 | FS_UNRM_FL | FS_NOATIME_FL | FS_DIRSYNC_FL | FS_SYNC_FL
709 | FS_TOPDIR_FL | FS_NOCOW_FL;
711 // The extents flag can't be removed, so don't (see chattr(1) and chattr.c).
712 rc = ioctl(fd, FS_IOC_GETFLAGS, &orig_attr);
716 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
718 attr |= (orig_attr & FS_EXTENT_FL);
720 rc = ioctl(fd, FS_IOC_SETFLAGS, &attr);
724 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
728 return Py_BuildValue("O", Py_None);
730 #endif /* def BUP_HAVE_FILE_ATTRS */
733 #if defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES) || defined(HAVE_LUTIMES)
735 static int bup_parse_xutime_args(char **path,
739 long *modification_ns,
740 PyObject *self, PyObject *args)
742 if (!PyArg_ParseTuple(args, "s((ll)(ll))",
745 modification, modification_ns))
750 PyErr_SetString(PyExc_ValueError, "access time is NaN");
753 else if (isinf(*access))
755 PyErr_SetString(PyExc_ValueError, "access time is infinite");
758 else if (isnan(*modification))
760 PyErr_SetString(PyExc_ValueError, "modification time is NaN");
763 else if (isinf(*modification))
765 PyErr_SetString(PyExc_ValueError, "modification time is infinite");
769 if (isnan(*access_ns))
771 PyErr_SetString(PyExc_ValueError, "access time ns is NaN");
774 else if (isinf(*access_ns))
776 PyErr_SetString(PyExc_ValueError, "access time ns is infinite");
779 else if (isnan(*modification_ns))
781 PyErr_SetString(PyExc_ValueError, "modification time ns is NaN");
784 else if (isinf(*modification_ns))
786 PyErr_SetString(PyExc_ValueError, "modification time ns is infinite");
793 #endif /* defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES)
794 || defined(HAVE_LUTIMES) */
797 #ifdef HAVE_UTIMENSAT
799 static PyObject *bup_xutime_ns(PyObject *self, PyObject *args,
804 long access, access_ns, modification, modification_ns;
805 struct timespec ts[2];
807 if (!bup_parse_xutime_args(&path, &access, &access_ns,
808 &modification, &modification_ns,
812 ts[0].tv_sec = access;
813 ts[0].tv_nsec = access_ns;
814 ts[1].tv_sec = modification;
815 ts[1].tv_nsec = modification_ns;
816 rc = utimensat(AT_FDCWD, path, ts,
817 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
819 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
821 return Py_BuildValue("O", Py_None);
825 #define BUP_HAVE_BUP_UTIME_NS 1
826 static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
828 return bup_xutime_ns(self, args, 1);
832 #define BUP_HAVE_BUP_LUTIME_NS 1
833 static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
835 return bup_xutime_ns(self, args, 0);
839 #else /* not defined(HAVE_UTIMENSAT) */
843 #define BUP_HAVE_BUP_UTIME_NS 1
844 static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
848 long access, access_ns, modification, modification_ns;
849 struct timeval tv[2];
851 if (!bup_parse_xutime_args(&path, &access, &access_ns,
852 &modification, &modification_ns,
856 tv[0].tv_sec = access;
857 tv[0].tv_usec = access_ns / 1000;
858 tv[1].tv_sec = modification;
859 tv[1].tv_usec = modification_ns / 1000;
860 rc = utimes(path, tv);
862 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
864 return Py_BuildValue("O", Py_None);
866 #endif /* def HAVE_UTIMES */
870 #define BUP_HAVE_BUP_LUTIME_NS 1
871 static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
875 long access, access_ns, modification, modification_ns;
876 struct timeval tv[2];
878 if (!bup_parse_xutime_args(&path, &access, &access_ns,
879 &modification, &modification_ns,
883 tv[0].tv_sec = access;
884 tv[0].tv_usec = access_ns / 1000;
885 tv[1].tv_sec = modification;
886 tv[1].tv_usec = modification_ns / 1000;
887 rc = lutimes(path, tv);
889 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
891 return Py_BuildValue("O", Py_None);
893 #endif /* def HAVE_LUTIMES */
896 #endif /* not defined(HAVE_UTIMENSAT) */
899 #ifdef HAVE_STAT_ST_ATIM
900 # define BUP_STAT_ATIME_NS(st) (st)->st_atim.tv_nsec
901 # define BUP_STAT_MTIME_NS(st) (st)->st_mtim.tv_nsec
902 # define BUP_STAT_CTIME_NS(st) (st)->st_ctim.tv_nsec
903 #elif defined HAVE_STAT_ST_ATIMENSEC
904 # define BUP_STAT_ATIME_NS(st) (st)->st_atimespec.tv_nsec
905 # define BUP_STAT_MTIME_NS(st) (st)->st_mtimespec.tv_nsec
906 # define BUP_STAT_CTIME_NS(st) (st)->st_ctimespec.tv_nsec
908 # define BUP_STAT_ATIME_NS(st) 0
909 # define BUP_STAT_MTIME_NS(st) 0
910 # define BUP_STAT_CTIME_NS(st) 0
914 static PyObject *stat_struct_to_py(const struct stat *st)
916 long atime_ns = BUP_STAT_ATIME_NS(st);
917 long mtime_ns = BUP_STAT_MTIME_NS(st);
918 long ctime_ns = BUP_STAT_CTIME_NS(st);
920 /* Enforce the current timespec nanosecond range expectations. */
921 if (atime_ns < 0 || atime_ns > 999999999)
923 PyErr_SetString(PyExc_ValueError, "invalid atime timespec nanoseconds");
926 if (mtime_ns < 0 || mtime_ns > 999999999)
928 PyErr_SetString(PyExc_ValueError, "invalid mtime timespec nanoseconds");
931 if (ctime_ns < 0 || ctime_ns > 999999999)
933 PyErr_SetString(PyExc_ValueError, "invalid ctime timespec nanoseconds");
937 return Py_BuildValue("kkkkkkkk(Ll)(Ll)(Ll)",
938 (unsigned long) st->st_mode,
939 (unsigned long) st->st_ino,
940 (unsigned long) st->st_dev,
941 (unsigned long) st->st_nlink,
942 (unsigned long) st->st_uid,
943 (unsigned long) st->st_gid,
944 (unsigned long) st->st_rdev,
945 (unsigned long) st->st_size,
946 (long long) st->st_atime,
948 (long long) st->st_mtime,
950 (long long) st->st_ctime,
955 static PyObject *bup_stat(PyObject *self, PyObject *args)
960 if (!PyArg_ParseTuple(args, "s", &filename))
964 rc = stat(filename, &st);
966 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
967 return stat_struct_to_py(&st);
971 static PyObject *bup_lstat(PyObject *self, PyObject *args)
976 if (!PyArg_ParseTuple(args, "s", &filename))
980 rc = lstat(filename, &st);
982 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
983 return stat_struct_to_py(&st);
987 static PyObject *bup_fstat(PyObject *self, PyObject *args)
991 if (!PyArg_ParseTuple(args, "i", &fd))
997 return PyErr_SetFromErrno(PyExc_OSError);
998 return stat_struct_to_py(&st);
1002 static PyMethodDef helper_methods[] = {
1003 { "selftest", selftest, METH_VARARGS,
1004 "Check that the rolling checksum rolls correctly (for unit tests)." },
1005 { "blobbits", blobbits, METH_VARARGS,
1006 "Return the number of bits in the rolling checksum." },
1007 { "splitbuf", splitbuf, METH_VARARGS,
1008 "Split a list of strings based on a rolling checksum." },
1009 { "bitmatch", bitmatch, METH_VARARGS,
1010 "Count the number of matching prefix bits between two strings." },
1011 { "firstword", firstword, METH_VARARGS,
1012 "Return an int corresponding to the first 32 bits of buf." },
1013 { "bloom_contains", bloom_contains, METH_VARARGS,
1014 "Check if a bloom filter of 2^nbits bytes contains an object" },
1015 { "bloom_add", bloom_add, METH_VARARGS,
1016 "Add an object to a bloom filter of 2^nbits bytes" },
1017 { "extract_bits", extract_bits, METH_VARARGS,
1018 "Take the first 'nbits' bits from 'buf' and return them as an int." },
1019 { "merge_into", merge_into, METH_VARARGS,
1020 "Merges a bunch of idx and midx files into a single midx." },
1021 { "write_idx", write_idx, METH_VARARGS,
1022 "Write a PackIdxV2 file from an idx list of lists of tuples" },
1023 { "write_random", write_random, METH_VARARGS,
1024 "Write random bytes to the given file descriptor" },
1025 { "random_sha", random_sha, METH_VARARGS,
1026 "Return a random 20-byte string" },
1027 { "open_noatime", open_noatime, METH_VARARGS,
1028 "open() the given filename for read with O_NOATIME if possible" },
1029 { "fadvise_done", fadvise_done, METH_VARARGS,
1030 "Inform the kernel that we're finished with earlier parts of a file" },
1031 #ifdef BUP_HAVE_FILE_ATTRS
1032 { "get_linux_file_attr", bup_get_linux_file_attr, METH_VARARGS,
1033 "Return the Linux attributes for the given file." },
1035 #ifdef BUP_HAVE_FILE_ATTRS
1036 { "set_linux_file_attr", bup_set_linux_file_attr, METH_VARARGS,
1037 "Set the Linux attributes for the given file." },
1039 #ifdef BUP_HAVE_BUP_UTIME_NS
1040 { "bup_utime_ns", bup_utime_ns, METH_VARARGS,
1041 "Change path timestamps with up to nanosecond precision." },
1043 #ifdef BUP_HAVE_BUP_LUTIME_NS
1044 { "bup_lutime_ns", bup_lutime_ns, METH_VARARGS,
1045 "Change path timestamps with up to nanosecond precision;"
1046 " don't follow symlinks." },
1048 { "stat", bup_stat, METH_VARARGS,
1049 "Extended version of stat." },
1050 { "lstat", bup_lstat, METH_VARARGS,
1051 "Extended version of lstat." },
1052 { "fstat", bup_fstat, METH_VARARGS,
1053 "Extended version of fstat." },
1054 { NULL, NULL, 0, NULL }, // sentinel
1058 PyMODINIT_FUNC init_helpers(void)
1061 PyObject *m = Py_InitModule("_helpers", helper_methods);
1064 e = getenv("BUP_FORCE_TTY");
1065 istty2 = isatty(2) || (atoi(e ? e : "0") & 2);