1 #define _LARGEFILE64_SOURCE 1
2 #define PY_SSIZE_T_CLEAN 1
4 #include "../../config/config.h"
10 #include <arpa/inet.h>
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
18 #ifdef HAVE_SYS_STAT_H
25 #ifdef HAVE_LINUX_FS_H
28 #ifdef HAVE_SYS_IOCTL_H
29 #include <sys/ioctl.h>
32 #if defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS)
33 #define BUP_HAVE_FILE_ATTRS 1
37 // Of course, this assumes it's a bitfield value.
41 static int istty2 = 0;
43 // Probably we should use autoconf or something and set HAVE_PY_GETARGCARGV...
44 #if __WIN32__ || __CYGWIN__
46 // There's no 'ps' on win32 anyway, and Py_GetArgcArgv() isn't available.
47 static void unpythonize_argv(void) { }
49 #else // not __WIN32__
51 // For some reason this isn't declared in Python.h
52 extern void Py_GetArgcArgv(int *argc, char ***argv);
54 static void unpythonize_argv(void)
59 Py_GetArgcArgv(&argc, &argv);
61 for (i = 0; i < argc-1; i++)
63 if (argv[i] + strlen(argv[i]) + 1 != argv[i+1])
65 // The argv block doesn't work the way we expected; it's unsafe
71 arge = argv[argc-1] + strlen(argv[argc-1]) + 1;
73 if (strstr(argv[0], "python") && argv[1] == argv[0] + strlen(argv[0]) + 1)
77 p = strrchr(argv[1], '/');
83 memmove(argv[0], p, len);
84 memset(arge - diff, 0, diff);
85 for (i = 0; i < argc; i++)
86 argv[i] = argv[i+1] ? argv[i+1]-diff : NULL;
91 #endif // not __WIN32__ or __CYGWIN__
94 static PyObject *selftest(PyObject *self, PyObject *args)
96 if (!PyArg_ParseTuple(args, ""))
99 return Py_BuildValue("i", !bupsplit_selftest());
103 static PyObject *blobbits(PyObject *self, PyObject *args)
105 if (!PyArg_ParseTuple(args, ""))
107 return Py_BuildValue("i", BUP_BLOBBITS);
111 static PyObject *splitbuf(PyObject *self, PyObject *args)
113 unsigned char *buf = NULL;
115 int out = 0, bits = -1;
117 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
119 assert(len <= INT_MAX);
120 out = bupsplit_find_ofs(buf, len, &bits);
121 if (out) assert(bits >= BUP_BLOBBITS);
122 return Py_BuildValue("ii", out, bits);
126 static PyObject *bitmatch(PyObject *self, PyObject *args)
128 unsigned char *buf1 = NULL, *buf2 = NULL;
129 Py_ssize_t 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 assert(byte <= (INT_MAX >> 3));
150 return Py_BuildValue("i", byte*8 + bit);
154 static PyObject *firstword(PyObject *self, PyObject *args)
156 unsigned char *buf = NULL;
160 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
166 v = ntohl(*(uint32_t *)buf);
167 return PyLong_FromUnsignedLong(v);
171 #define BLOOM2_HEADERLEN 16
178 static void to_bloom_address_bitmask4(const bits40_t *buf,
179 const int nbits, uint64_t *v, unsigned char *bitmask)
184 mask = (1<<nbits) - 1;
185 raw = (((uint64_t)ntohl(buf->high)) << 8) | buf->low;
186 bit = (raw >> (37-nbits)) & 0x7;
187 *v = (raw >> (40-nbits)) & mask;
191 static void to_bloom_address_bitmask5(const uint32_t *buf,
192 const int nbits, uint32_t *v, unsigned char *bitmask)
197 mask = (1<<nbits) - 1;
199 bit = (raw >> (29-nbits)) & 0x7;
200 *v = (raw >> (32-nbits)) & mask;
204 #define BLOOM_SET_BIT(name, address, itype, otype) \
205 static void name(unsigned char *bloom, const void *buf, const int nbits)\
207 unsigned char bitmask;\
209 address((itype *)buf, nbits, &v, &bitmask);\
210 bloom[BLOOM2_HEADERLEN+v] |= bitmask;\
212 BLOOM_SET_BIT(bloom_set_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
213 BLOOM_SET_BIT(bloom_set_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
216 #define BLOOM_GET_BIT(name, address, itype, otype) \
217 static int name(const unsigned char *bloom, const void *buf, const int nbits)\
219 unsigned char bitmask;\
221 address((itype *)buf, nbits, &v, &bitmask);\
222 return bloom[BLOOM2_HEADERLEN+v] & bitmask;\
224 BLOOM_GET_BIT(bloom_get_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
225 BLOOM_GET_BIT(bloom_get_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
228 static PyObject *bloom_add(PyObject *self, PyObject *args)
230 unsigned char *sha = NULL, *bloom = NULL;
232 Py_ssize_t len = 0, blen = 0;
233 int nbits = 0, k = 0;
235 if (!PyArg_ParseTuple(args, "w#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
238 if (blen < 16+(1<<nbits) || len % 20 != 0)
245 for (end = sha + len; sha < end; sha += 20/k)
246 bloom_set_bit5(bloom, sha, nbits);
252 for (end = sha + len; sha < end; sha += 20/k)
253 bloom_set_bit4(bloom, sha, nbits);
259 return Py_BuildValue("n", len/20);
262 static PyObject *bloom_contains(PyObject *self, PyObject *args)
264 unsigned char *sha = NULL, *bloom = NULL;
265 Py_ssize_t len = 0, blen = 0;
266 int nbits = 0, k = 0;
270 if (!PyArg_ParseTuple(args, "t#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
280 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
281 if (!bloom_get_bit5(bloom, sha, nbits))
282 return Py_BuildValue("Oi", Py_None, steps);
288 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
289 if (!bloom_get_bit4(bloom, sha, nbits))
290 return Py_BuildValue("Oi", Py_None, steps);
295 return Py_BuildValue("ii", 1, k);
299 static uint32_t _extract_bits(unsigned char *buf, int nbits)
303 mask = (1<<nbits) - 1;
304 v = ntohl(*(uint32_t *)buf);
305 v = (v >> (32-nbits)) & mask;
310 static PyObject *extract_bits(PyObject *self, PyObject *args)
312 unsigned char *buf = NULL;
316 if (!PyArg_ParseTuple(args, "t#i", &buf, &len, &nbits))
322 return PyLong_FromUnsignedLong(_extract_bits(buf, nbits));
327 unsigned char bytes[20];
341 static int _cmp_sha(const struct sha *sha1, const struct sha *sha2)
344 for (i = 0; i < sizeof(struct sha); i++)
345 if (sha1->bytes[i] != sha2->bytes[i])
346 return sha1->bytes[i] - sha2->bytes[i];
351 static void _fix_idx_order(struct idx **idxs, int *last_i)
354 int low, mid, high, c = 0;
357 if (idxs[*last_i]->cur >= idxs[*last_i]->end)
359 idxs[*last_i] = NULL;
372 mid = (low + high) / 2;
373 c = _cmp_sha(idx->cur, idxs[mid]->cur);
385 memmove(&idxs[mid+1], &idxs[mid], (*last_i-mid)*sizeof(struct idx *));
390 static uint32_t _get_idx_i(struct idx *idx)
392 if (idx->cur_name == NULL)
393 return idx->name_base;
394 return ntohl(*idx->cur_name) + idx->name_base;
397 #define MIDX4_HEADERLEN 12
399 static PyObject *merge_into(PyObject *self, PyObject *args)
401 PyObject *ilist = NULL;
402 unsigned char *fmap = NULL;
403 struct sha *sha_ptr, *sha_start = NULL;
404 uint32_t *table_ptr, *name_ptr, *name_start;
405 struct idx **idxs = NULL;
409 uint32_t count, prefix;
413 if (!PyArg_ParseTuple(args, "w#iIO", &fmap, &flen, &bits, &total, &ilist))
416 num_i = PyList_Size(ilist);
417 idxs = (struct idx **)PyMem_Malloc(num_i * sizeof(struct idx *));
419 for (i = 0; i < num_i; i++)
421 long len, sha_ofs, name_map_ofs;
422 idxs[i] = (struct idx *)PyMem_Malloc(sizeof(struct idx));
423 PyObject *itup = PyList_GetItem(ilist, i);
424 if (!PyArg_ParseTuple(itup, "t#llli", &idxs[i]->map, &idxs[i]->bytes,
425 &len, &sha_ofs, &name_map_ofs, &idxs[i]->name_base))
427 idxs[i]->cur = (struct sha *)&idxs[i]->map[sha_ofs];
428 idxs[i]->end = &idxs[i]->cur[len];
430 idxs[i]->cur_name = (uint32_t *)&idxs[i]->map[name_map_ofs];
432 idxs[i]->cur_name = NULL;
434 table_ptr = (uint32_t *)&fmap[MIDX4_HEADERLEN];
435 sha_start = sha_ptr = (struct sha *)&table_ptr[1<<bits];
436 name_start = name_ptr = (uint32_t *)&sha_ptr[total];
445 if (count % 102424 == 0 && istty2)
446 fprintf(stderr, "midx: writing %.2f%% (%d/%d)\r",
447 count*100.0/total, count, total);
449 new_prefix = _extract_bits((unsigned char *)idx->cur, bits);
450 while (prefix < new_prefix)
451 table_ptr[prefix++] = htonl(count);
452 memcpy(sha_ptr++, idx->cur, sizeof(struct sha));
453 *name_ptr++ = htonl(_get_idx_i(idx));
455 if (idx->cur_name != NULL)
457 _fix_idx_order(idxs, &last_i);
460 while (prefix < (1<<bits))
461 table_ptr[prefix++] = htonl(count);
462 assert(count == total);
463 assert(prefix == (1<<bits));
464 assert(sha_ptr == sha_start+count);
465 assert(name_ptr == name_start+count);
468 return PyLong_FromUnsignedLong(count);
471 // This function should technically be macro'd out if it's going to be used
472 // more than ocasionally. As of this writing, it'll actually never be called
473 // in real world bup scenarios (because our packs are < MAX_INT bytes).
474 static uint64_t htonll(uint64_t value)
476 static const int endian_test = 42;
478 if (*(char *)&endian_test == endian_test) // LSB-MSB
479 return ((uint64_t)htonl(value & 0xFFFFFFFF) << 32) | htonl(value >> 32);
480 return value; // already in network byte order MSB-LSB
483 #define PACK_IDX_V2_HEADERLEN 8
484 #define FAN_ENTRIES 256
486 static PyObject *write_idx(PyObject *self, PyObject *args)
488 PyObject *pf = NULL, *idx = NULL;
491 unsigned char *fmap = NULL;
493 unsigned int total = 0;
495 int i, j, ofs64_count;
496 uint32_t *fan_ptr, *crc_ptr, *ofs_ptr;
499 if (!PyArg_ParseTuple(args, "Ow#OI", &pf, &fmap, &flen, &idx, &total))
502 fan_ptr = (uint32_t *)&fmap[PACK_IDX_V2_HEADERLEN];
503 sha_ptr = (struct sha *)&fan_ptr[FAN_ENTRIES];
504 crc_ptr = (uint32_t *)&sha_ptr[total];
505 ofs_ptr = (uint32_t *)&crc_ptr[total];
506 f = PyFile_AsFile(pf);
510 for (i = 0; i < FAN_ENTRIES; ++i)
513 part = PyList_GET_ITEM(idx, i);
515 plen = PyList_GET_SIZE(part);
517 *fan_ptr++ = htonl(count);
518 for (j = 0; j < plen; ++j)
520 unsigned char *sha = NULL;
521 Py_ssize_t sha_len = 0;
522 unsigned int crc = 0;
523 unsigned PY_LONG_LONG ofs_py = 0;
525 if (!PyArg_ParseTuple(PyList_GET_ITEM(part, j), "t#IK",
526 &sha, &sha_len, &crc, &ofs_py))
528 assert(crc <= UINT32_MAX);
529 assert(ofs_py <= UINT64_MAX);
531 if (sha_len != sizeof(struct sha))
533 memcpy(sha_ptr++, sha, sizeof(struct sha));
534 *crc_ptr++ = htonl(crc);
535 if (ofs > 0x7fffffff)
537 const uint64_t nofs = htonll(ofs);
538 if (fwrite(&nofs, sizeof(uint64_t), 1, f) != 1)
539 return PyErr_SetFromErrno(PyExc_OSError);
540 ofs = 0x80000000 | ofs64_count++;
542 *ofs_ptr++ = htonl((uint32_t)ofs);
545 return PyLong_FromUnsignedLong(count);
549 // I would have made this a lower-level function that just fills in a buffer
550 // with random values, and then written those values from python. But that's
551 // about 20% slower in my tests, and since we typically generate random
552 // numbers for benchmarking other parts of bup, any slowness in generating
553 // random bytes will make our benchmarks inaccurate. Plus nobody wants
554 // pseudorandom bytes much except for this anyway.
555 static PyObject *write_random(PyObject *self, PyObject *args)
557 uint32_t buf[1024/4];
558 int fd = -1, seed = 0, verbose = 0;
560 long long len = 0, kbytes = 0, written = 0;
562 if (!PyArg_ParseTuple(args, "iLii", &fd, &len, &seed, &verbose))
567 for (kbytes = 0; kbytes < len/1024; kbytes++)
570 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
572 ret = write(fd, buf, sizeof(buf));
576 if (ret < (int)sizeof(buf))
578 if (verbose && kbytes/1024 > 0 && !(kbytes%1024))
579 fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
582 // handle non-multiples of 1024
586 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
588 ret = write(fd, buf, len % 1024);
595 fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
596 return Py_BuildValue("L", written);
600 static PyObject *random_sha(PyObject *self, PyObject *args)
602 static int seeded = 0;
603 uint32_t shabuf[20/4];
608 assert(sizeof(shabuf) == 20);
613 if (!PyArg_ParseTuple(args, ""))
616 memset(shabuf, 0, sizeof(shabuf));
617 for (i=0; i < 20/4; i++)
618 shabuf[i] = random();
619 return Py_BuildValue("s#", shabuf, 20);
623 static int _open_noatime(const char *filename, int attrs)
625 int attrs_noatime, fd;
631 attrs |= O_LARGEFILE;
633 attrs_noatime = attrs;
635 attrs_noatime |= O_NOATIME;
637 fd = open(filename, attrs_noatime);
638 if (fd < 0 && errno == EPERM)
640 // older Linux kernels would return EPERM if you used O_NOATIME
641 // and weren't the file's owner. This pointless restriction was
642 // relaxed eventually, but we have to handle it anyway.
643 // (VERY old kernels didn't recognized O_NOATIME, but they would
644 // just harmlessly ignore it, so this branch won't trigger)
645 fd = open(filename, attrs);
651 static PyObject *open_noatime(PyObject *self, PyObject *args)
653 char *filename = NULL;
655 if (!PyArg_ParseTuple(args, "s", &filename))
657 fd = _open_noatime(filename, 0);
659 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
660 return Py_BuildValue("i", fd);
664 static PyObject *fadvise_done(PyObject *self, PyObject *args)
668 if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
670 #ifdef POSIX_FADV_DONTNEED
671 posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
673 return Py_BuildValue("");
677 #ifdef BUP_HAVE_FILE_ATTRS
678 static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
685 if (!PyArg_ParseTuple(args, "s", &path))
688 fd = _open_noatime(path, O_NONBLOCK);
690 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
693 rc = ioctl(fd, FS_IOC_GETFLAGS, &attr);
697 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
701 return Py_BuildValue("k", attr);
703 #endif /* def BUP_HAVE_FILE_ATTRS */
706 #ifdef BUP_HAVE_FILE_ATTRS
707 static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
710 unsigned long orig_attr, attr;
714 if (!PyArg_ParseTuple(args, "sk", &path, &attr))
717 fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
719 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
721 // Restrict attr to modifiable flags acdeijstuADST -- see
722 // chattr(1) and the e2fsprogs source. Letter to flag mapping is
723 // in pf.c flags_array[].
724 attr &= FS_APPEND_FL | FS_COMPR_FL | FS_NODUMP_FL | FS_EXTENT_FL
725 | FS_IMMUTABLE_FL | FS_JOURNAL_DATA_FL | FS_SECRM_FL | FS_NOTAIL_FL
726 | FS_UNRM_FL | FS_NOATIME_FL | FS_DIRSYNC_FL | FS_SYNC_FL
727 | FS_TOPDIR_FL | FS_NOCOW_FL;
729 // The extents flag can't be removed, so don't (see chattr(1) and chattr.c).
730 rc = ioctl(fd, FS_IOC_GETFLAGS, &orig_attr);
734 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
736 attr |= (orig_attr & FS_EXTENT_FL);
738 rc = ioctl(fd, FS_IOC_SETFLAGS, &attr);
742 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
746 return Py_BuildValue("O", Py_None);
748 #endif /* def BUP_HAVE_FILE_ATTRS */
751 #if defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES) || defined(HAVE_LUTIMES)
753 static int bup_parse_xutime_args(char **path,
757 long *modification_ns,
758 PyObject *self, PyObject *args)
760 if (!PyArg_ParseTuple(args, "s((ll)(ll))",
763 modification, modification_ns))
768 PyErr_SetString(PyExc_ValueError, "access time is NaN");
771 else if (isinf(*access))
773 PyErr_SetString(PyExc_ValueError, "access time is infinite");
776 else if (isnan(*modification))
778 PyErr_SetString(PyExc_ValueError, "modification time is NaN");
781 else if (isinf(*modification))
783 PyErr_SetString(PyExc_ValueError, "modification time is infinite");
787 if (isnan(*access_ns))
789 PyErr_SetString(PyExc_ValueError, "access time ns is NaN");
792 else if (isinf(*access_ns))
794 PyErr_SetString(PyExc_ValueError, "access time ns is infinite");
797 else if (isnan(*modification_ns))
799 PyErr_SetString(PyExc_ValueError, "modification time ns is NaN");
802 else if (isinf(*modification_ns))
804 PyErr_SetString(PyExc_ValueError, "modification time ns is infinite");
811 #endif /* defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES)
812 || defined(HAVE_LUTIMES) */
815 #ifdef HAVE_UTIMENSAT
817 static PyObject *bup_xutime_ns(PyObject *self, PyObject *args,
822 long access, access_ns, modification, modification_ns;
823 struct timespec ts[2];
825 if (!bup_parse_xutime_args(&path, &access, &access_ns,
826 &modification, &modification_ns,
830 ts[0].tv_sec = access;
831 ts[0].tv_nsec = access_ns;
832 ts[1].tv_sec = modification;
833 ts[1].tv_nsec = modification_ns;
834 rc = utimensat(AT_FDCWD, path, ts,
835 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
837 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
839 return Py_BuildValue("O", Py_None);
843 #define BUP_HAVE_BUP_UTIME_NS 1
844 static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
846 return bup_xutime_ns(self, args, 1);
850 #define BUP_HAVE_BUP_LUTIME_NS 1
851 static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
853 return bup_xutime_ns(self, args, 0);
857 #else /* not defined(HAVE_UTIMENSAT) */
861 #define BUP_HAVE_BUP_UTIME_NS 1
862 static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
866 long access, access_ns, modification, modification_ns;
867 struct timeval tv[2];
869 if (!bup_parse_xutime_args(&path, &access, &access_ns,
870 &modification, &modification_ns,
874 tv[0].tv_sec = access;
875 tv[0].tv_usec = access_ns / 1000;
876 tv[1].tv_sec = modification;
877 tv[1].tv_usec = modification_ns / 1000;
878 rc = utimes(path, tv);
880 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
882 return Py_BuildValue("O", Py_None);
884 #endif /* def HAVE_UTIMES */
888 #define BUP_HAVE_BUP_LUTIME_NS 1
889 static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
893 long access, access_ns, modification, modification_ns;
894 struct timeval tv[2];
896 if (!bup_parse_xutime_args(&path, &access, &access_ns,
897 &modification, &modification_ns,
901 tv[0].tv_sec = access;
902 tv[0].tv_usec = access_ns / 1000;
903 tv[1].tv_sec = modification;
904 tv[1].tv_usec = modification_ns / 1000;
905 rc = lutimes(path, tv);
907 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
909 return Py_BuildValue("O", Py_None);
911 #endif /* def HAVE_LUTIMES */
914 #endif /* not defined(HAVE_UTIMENSAT) */
917 #ifdef HAVE_STAT_ST_ATIM
918 # define BUP_STAT_ATIME_NS(st) (st)->st_atim.tv_nsec
919 # define BUP_STAT_MTIME_NS(st) (st)->st_mtim.tv_nsec
920 # define BUP_STAT_CTIME_NS(st) (st)->st_ctim.tv_nsec
921 #elif defined HAVE_STAT_ST_ATIMENSEC
922 # define BUP_STAT_ATIME_NS(st) (st)->st_atimespec.tv_nsec
923 # define BUP_STAT_MTIME_NS(st) (st)->st_mtimespec.tv_nsec
924 # define BUP_STAT_CTIME_NS(st) (st)->st_ctimespec.tv_nsec
926 # define BUP_STAT_ATIME_NS(st) 0
927 # define BUP_STAT_MTIME_NS(st) 0
928 # define BUP_STAT_CTIME_NS(st) 0
932 static void set_invalid_timespec_msg(const char *field,
935 const char *filename,
938 if (filename != NULL)
939 PyErr_Format(PyExc_ValueError,
940 "invalid %s timespec (%lld %ld) for file \"%s\"",
941 field, sec, nsec, filename);
943 PyErr_Format(PyExc_ValueError,
944 "invalid %s timespec (%lld %ld) for file descriptor %d",
945 field, sec, nsec, fd);
949 static int normalize_timespec_values(const char *name,
952 const char *filename,
955 if (*nsec < -999999999 || *nsec > 999999999)
957 set_invalid_timespec_msg(name, *sec, *nsec, filename, fd);
962 if (*sec == LONG_MIN)
964 set_invalid_timespec_msg(name, *sec, *nsec, filename, fd);
974 static PyObject *stat_struct_to_py(const struct stat *st,
975 const char *filename,
978 long long atime = st->st_atime;
979 long long mtime = st->st_mtime;
980 long long ctime = st->st_ctime;
981 long atime_ns = BUP_STAT_ATIME_NS(st);
982 long mtime_ns = BUP_STAT_MTIME_NS(st);
983 long ctime_ns = BUP_STAT_CTIME_NS(st);
985 if (!normalize_timespec_values("atime", &atime, &atime_ns, filename, fd))
987 if (!normalize_timespec_values("mtime", &mtime, &mtime_ns, filename, fd))
989 if (!normalize_timespec_values("ctime", &ctime, &ctime_ns, filename, fd))
992 return Py_BuildValue("kkkkkkkk(Ll)(Ll)(Ll)",
993 (unsigned long) st->st_mode,
994 (unsigned long) st->st_ino,
995 (unsigned long) st->st_dev,
996 (unsigned long) st->st_nlink,
997 (unsigned long) st->st_uid,
998 (unsigned long) st->st_gid,
999 (unsigned long) st->st_rdev,
1000 (unsigned long) st->st_size,
1010 static PyObject *bup_stat(PyObject *self, PyObject *args)
1015 if (!PyArg_ParseTuple(args, "s", &filename))
1019 rc = stat(filename, &st);
1021 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
1022 return stat_struct_to_py(&st, filename, 0);
1026 static PyObject *bup_lstat(PyObject *self, PyObject *args)
1031 if (!PyArg_ParseTuple(args, "s", &filename))
1035 rc = lstat(filename, &st);
1037 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
1038 return stat_struct_to_py(&st, filename, 0);
1042 static PyObject *bup_fstat(PyObject *self, PyObject *args)
1046 if (!PyArg_ParseTuple(args, "i", &fd))
1050 rc = fstat(fd, &st);
1052 return PyErr_SetFromErrno(PyExc_OSError);
1053 return stat_struct_to_py(&st, NULL, fd);
1057 static PyMethodDef helper_methods[] = {
1058 { "selftest", selftest, METH_VARARGS,
1059 "Check that the rolling checksum rolls correctly (for unit tests)." },
1060 { "blobbits", blobbits, METH_VARARGS,
1061 "Return the number of bits in the rolling checksum." },
1062 { "splitbuf", splitbuf, METH_VARARGS,
1063 "Split a list of strings based on a rolling checksum." },
1064 { "bitmatch", bitmatch, METH_VARARGS,
1065 "Count the number of matching prefix bits between two strings." },
1066 { "firstword", firstword, METH_VARARGS,
1067 "Return an int corresponding to the first 32 bits of buf." },
1068 { "bloom_contains", bloom_contains, METH_VARARGS,
1069 "Check if a bloom filter of 2^nbits bytes contains an object" },
1070 { "bloom_add", bloom_add, METH_VARARGS,
1071 "Add an object to a bloom filter of 2^nbits bytes" },
1072 { "extract_bits", extract_bits, METH_VARARGS,
1073 "Take the first 'nbits' bits from 'buf' and return them as an int." },
1074 { "merge_into", merge_into, METH_VARARGS,
1075 "Merges a bunch of idx and midx files into a single midx." },
1076 { "write_idx", write_idx, METH_VARARGS,
1077 "Write a PackIdxV2 file from an idx list of lists of tuples" },
1078 { "write_random", write_random, METH_VARARGS,
1079 "Write random bytes to the given file descriptor" },
1080 { "random_sha", random_sha, METH_VARARGS,
1081 "Return a random 20-byte string" },
1082 { "open_noatime", open_noatime, METH_VARARGS,
1083 "open() the given filename for read with O_NOATIME if possible" },
1084 { "fadvise_done", fadvise_done, METH_VARARGS,
1085 "Inform the kernel that we're finished with earlier parts of a file" },
1086 #ifdef BUP_HAVE_FILE_ATTRS
1087 { "get_linux_file_attr", bup_get_linux_file_attr, METH_VARARGS,
1088 "Return the Linux attributes for the given file." },
1090 #ifdef BUP_HAVE_FILE_ATTRS
1091 { "set_linux_file_attr", bup_set_linux_file_attr, METH_VARARGS,
1092 "Set the Linux attributes for the given file." },
1094 #ifdef BUP_HAVE_BUP_UTIME_NS
1095 { "bup_utime_ns", bup_utime_ns, METH_VARARGS,
1096 "Change path timestamps with up to nanosecond precision." },
1098 #ifdef BUP_HAVE_BUP_LUTIME_NS
1099 { "bup_lutime_ns", bup_lutime_ns, METH_VARARGS,
1100 "Change path timestamps with up to nanosecond precision;"
1101 " don't follow symlinks." },
1103 { "stat", bup_stat, METH_VARARGS,
1104 "Extended version of stat." },
1105 { "lstat", bup_lstat, METH_VARARGS,
1106 "Extended version of lstat." },
1107 { "fstat", bup_fstat, METH_VARARGS,
1108 "Extended version of fstat." },
1109 { NULL, NULL, 0, NULL }, // sentinel
1113 PyMODINIT_FUNC init_helpers(void)
1116 PyObject *m = Py_InitModule("_helpers", helper_methods);
1119 e = getenv("BUP_FORCE_TTY");
1120 istty2 = isatty(2) || (atoi(e ? e : "0") & 2);