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
173 static void to_bloom_address_bitmask4(const unsigned char *buf,
174 const int nbits, uint64_t *v, unsigned char *bitmask)
180 memcpy(&high, buf, 4);
181 mask = (1<<nbits) - 1;
182 raw = (((uint64_t)ntohl(high) << 8) | buf[4]);
183 bit = (raw >> (37-nbits)) & 0x7;
184 *v = (raw >> (40-nbits)) & mask;
188 static void to_bloom_address_bitmask5(const unsigned char *buf,
189 const int nbits, uint32_t *v, unsigned char *bitmask)
195 memcpy(&high, buf, 4);
196 mask = (1<<nbits) - 1;
198 bit = (raw >> (29-nbits)) & 0x7;
199 *v = (raw >> (32-nbits)) & mask;
203 #define BLOOM_SET_BIT(name, address, otype) \
204 static void name(unsigned char *bloom, const unsigned char *buf, const int nbits)\
206 unsigned char bitmask;\
208 address(buf, nbits, &v, &bitmask);\
209 bloom[BLOOM2_HEADERLEN+v] |= bitmask;\
211 BLOOM_SET_BIT(bloom_set_bit4, to_bloom_address_bitmask4, uint64_t)
212 BLOOM_SET_BIT(bloom_set_bit5, to_bloom_address_bitmask5, uint32_t)
215 #define BLOOM_GET_BIT(name, address, otype) \
216 static int name(const unsigned char *bloom, const unsigned char *buf, const int nbits)\
218 unsigned char bitmask;\
220 address(buf, nbits, &v, &bitmask);\
221 return bloom[BLOOM2_HEADERLEN+v] & bitmask;\
223 BLOOM_GET_BIT(bloom_get_bit4, to_bloom_address_bitmask4, uint64_t)
224 BLOOM_GET_BIT(bloom_get_bit5, to_bloom_address_bitmask5, uint32_t)
227 static PyObject *bloom_add(PyObject *self, PyObject *args)
229 unsigned char *sha = NULL, *bloom = NULL;
231 Py_ssize_t len = 0, blen = 0;
232 int nbits = 0, k = 0;
234 if (!PyArg_ParseTuple(args, "w#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
237 if (blen < 16+(1<<nbits) || len % 20 != 0)
244 for (end = sha + len; sha < end; sha += 20/k)
245 bloom_set_bit5(bloom, sha, nbits);
251 for (end = sha + len; sha < end; sha += 20/k)
252 bloom_set_bit4(bloom, sha, nbits);
258 return Py_BuildValue("n", len/20);
261 static PyObject *bloom_contains(PyObject *self, PyObject *args)
263 unsigned char *sha = NULL, *bloom = NULL;
264 Py_ssize_t len = 0, blen = 0;
265 int nbits = 0, k = 0;
269 if (!PyArg_ParseTuple(args, "t#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
279 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
280 if (!bloom_get_bit5(bloom, sha, nbits))
281 return Py_BuildValue("Oi", Py_None, steps);
287 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
288 if (!bloom_get_bit4(bloom, sha, nbits))
289 return Py_BuildValue("Oi", Py_None, steps);
294 return Py_BuildValue("ii", 1, k);
298 static uint32_t _extract_bits(unsigned char *buf, int nbits)
302 mask = (1<<nbits) - 1;
303 v = ntohl(*(uint32_t *)buf);
304 v = (v >> (32-nbits)) & mask;
309 static PyObject *extract_bits(PyObject *self, PyObject *args)
311 unsigned char *buf = NULL;
315 if (!PyArg_ParseTuple(args, "t#i", &buf, &len, &nbits))
321 return PyLong_FromUnsignedLong(_extract_bits(buf, nbits));
326 unsigned char bytes[20];
340 static int _cmp_sha(const struct sha *sha1, const struct sha *sha2)
343 for (i = 0; i < sizeof(struct sha); i++)
344 if (sha1->bytes[i] != sha2->bytes[i])
345 return sha1->bytes[i] - sha2->bytes[i];
350 static void _fix_idx_order(struct idx **idxs, int *last_i)
353 int low, mid, high, c = 0;
356 if (idxs[*last_i]->cur >= idxs[*last_i]->end)
358 idxs[*last_i] = NULL;
371 mid = (low + high) / 2;
372 c = _cmp_sha(idx->cur, idxs[mid]->cur);
384 memmove(&idxs[mid+1], &idxs[mid], (*last_i-mid)*sizeof(struct idx *));
389 static uint32_t _get_idx_i(struct idx *idx)
391 if (idx->cur_name == NULL)
392 return idx->name_base;
393 return ntohl(*idx->cur_name) + idx->name_base;
396 #define MIDX4_HEADERLEN 12
398 static PyObject *merge_into(PyObject *self, PyObject *args)
400 PyObject *ilist = NULL;
401 unsigned char *fmap = NULL;
402 struct sha *sha_ptr, *sha_start = NULL;
403 uint32_t *table_ptr, *name_ptr, *name_start;
404 struct idx **idxs = NULL;
408 uint32_t count, prefix;
412 if (!PyArg_ParseTuple(args, "w#iIO", &fmap, &flen, &bits, &total, &ilist))
415 num_i = PyList_Size(ilist);
416 idxs = (struct idx **)PyMem_Malloc(num_i * sizeof(struct idx *));
418 for (i = 0; i < num_i; i++)
420 long len, sha_ofs, name_map_ofs;
421 idxs[i] = (struct idx *)PyMem_Malloc(sizeof(struct idx));
422 PyObject *itup = PyList_GetItem(ilist, i);
423 if (!PyArg_ParseTuple(itup, "t#llli", &idxs[i]->map, &idxs[i]->bytes,
424 &len, &sha_ofs, &name_map_ofs, &idxs[i]->name_base))
426 idxs[i]->cur = (struct sha *)&idxs[i]->map[sha_ofs];
427 idxs[i]->end = &idxs[i]->cur[len];
429 idxs[i]->cur_name = (uint32_t *)&idxs[i]->map[name_map_ofs];
431 idxs[i]->cur_name = NULL;
433 table_ptr = (uint32_t *)&fmap[MIDX4_HEADERLEN];
434 sha_start = sha_ptr = (struct sha *)&table_ptr[1<<bits];
435 name_start = name_ptr = (uint32_t *)&sha_ptr[total];
444 if (count % 102424 == 0 && istty2)
445 fprintf(stderr, "midx: writing %.2f%% (%d/%d)\r",
446 count*100.0/total, count, total);
448 new_prefix = _extract_bits((unsigned char *)idx->cur, bits);
449 while (prefix < new_prefix)
450 table_ptr[prefix++] = htonl(count);
451 memcpy(sha_ptr++, idx->cur, sizeof(struct sha));
452 *name_ptr++ = htonl(_get_idx_i(idx));
454 if (idx->cur_name != NULL)
456 _fix_idx_order(idxs, &last_i);
459 while (prefix < (1<<bits))
460 table_ptr[prefix++] = htonl(count);
461 assert(count == total);
462 assert(prefix == (1<<bits));
463 assert(sha_ptr == sha_start+count);
464 assert(name_ptr == name_start+count);
467 return PyLong_FromUnsignedLong(count);
470 // This function should technically be macro'd out if it's going to be used
471 // more than ocasionally. As of this writing, it'll actually never be called
472 // in real world bup scenarios (because our packs are < MAX_INT bytes).
473 static uint64_t htonll(uint64_t value)
475 static const int endian_test = 42;
477 if (*(char *)&endian_test == endian_test) // LSB-MSB
478 return ((uint64_t)htonl(value & 0xFFFFFFFF) << 32) | htonl(value >> 32);
479 return value; // already in network byte order MSB-LSB
482 #define PACK_IDX_V2_HEADERLEN 8
483 #define FAN_ENTRIES 256
485 static PyObject *write_idx(PyObject *self, PyObject *args)
487 PyObject *pf = NULL, *idx = NULL;
490 unsigned char *fmap = NULL;
492 unsigned int total = 0;
494 int i, j, ofs64_count;
495 uint32_t *fan_ptr, *crc_ptr, *ofs_ptr;
498 if (!PyArg_ParseTuple(args, "Ow#OI", &pf, &fmap, &flen, &idx, &total))
501 fan_ptr = (uint32_t *)&fmap[PACK_IDX_V2_HEADERLEN];
502 sha_ptr = (struct sha *)&fan_ptr[FAN_ENTRIES];
503 crc_ptr = (uint32_t *)&sha_ptr[total];
504 ofs_ptr = (uint32_t *)&crc_ptr[total];
505 f = PyFile_AsFile(pf);
509 for (i = 0; i < FAN_ENTRIES; ++i)
512 part = PyList_GET_ITEM(idx, i);
514 plen = PyList_GET_SIZE(part);
516 *fan_ptr++ = htonl(count);
517 for (j = 0; j < plen; ++j)
519 unsigned char *sha = NULL;
520 Py_ssize_t sha_len = 0;
521 unsigned int crc = 0;
522 unsigned PY_LONG_LONG ofs_py = 0;
524 if (!PyArg_ParseTuple(PyList_GET_ITEM(part, j), "t#IK",
525 &sha, &sha_len, &crc, &ofs_py))
527 assert(crc <= UINT32_MAX);
528 assert(ofs_py <= UINT64_MAX);
530 if (sha_len != sizeof(struct sha))
532 memcpy(sha_ptr++, sha, sizeof(struct sha));
533 *crc_ptr++ = htonl(crc);
534 if (ofs > 0x7fffffff)
536 const uint64_t nofs = htonll(ofs);
537 if (fwrite(&nofs, sizeof(uint64_t), 1, f) != 1)
538 return PyErr_SetFromErrno(PyExc_OSError);
539 ofs = 0x80000000 | ofs64_count++;
541 *ofs_ptr++ = htonl((uint32_t)ofs);
544 return PyLong_FromUnsignedLong(count);
548 // I would have made this a lower-level function that just fills in a buffer
549 // with random values, and then written those values from python. But that's
550 // about 20% slower in my tests, and since we typically generate random
551 // numbers for benchmarking other parts of bup, any slowness in generating
552 // random bytes will make our benchmarks inaccurate. Plus nobody wants
553 // pseudorandom bytes much except for this anyway.
554 static PyObject *write_random(PyObject *self, PyObject *args)
556 uint32_t buf[1024/4];
557 int fd = -1, seed = 0, verbose = 0;
559 long long len = 0, kbytes = 0, written = 0;
561 if (!PyArg_ParseTuple(args, "iLii", &fd, &len, &seed, &verbose))
566 for (kbytes = 0; kbytes < len/1024; kbytes++)
569 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
571 ret = write(fd, buf, sizeof(buf));
575 if (ret < (int)sizeof(buf))
577 if (verbose && kbytes/1024 > 0 && !(kbytes%1024))
578 fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
581 // handle non-multiples of 1024
585 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
587 ret = write(fd, buf, len % 1024);
594 fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
595 return Py_BuildValue("L", written);
599 static PyObject *random_sha(PyObject *self, PyObject *args)
601 static int seeded = 0;
602 uint32_t shabuf[20/4];
607 assert(sizeof(shabuf) == 20);
612 if (!PyArg_ParseTuple(args, ""))
615 memset(shabuf, 0, sizeof(shabuf));
616 for (i=0; i < 20/4; i++)
617 shabuf[i] = random();
618 return Py_BuildValue("s#", shabuf, 20);
622 static int _open_noatime(const char *filename, int attrs)
624 int attrs_noatime, fd;
630 attrs |= O_LARGEFILE;
632 attrs_noatime = attrs;
634 attrs_noatime |= O_NOATIME;
636 fd = open(filename, attrs_noatime);
637 if (fd < 0 && errno == EPERM)
639 // older Linux kernels would return EPERM if you used O_NOATIME
640 // and weren't the file's owner. This pointless restriction was
641 // relaxed eventually, but we have to handle it anyway.
642 // (VERY old kernels didn't recognized O_NOATIME, but they would
643 // just harmlessly ignore it, so this branch won't trigger)
644 fd = open(filename, attrs);
650 static PyObject *open_noatime(PyObject *self, PyObject *args)
652 char *filename = NULL;
654 if (!PyArg_ParseTuple(args, "s", &filename))
656 fd = _open_noatime(filename, 0);
658 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
659 return Py_BuildValue("i", fd);
663 static PyObject *fadvise_done(PyObject *self, PyObject *args)
667 if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
669 #ifdef POSIX_FADV_DONTNEED
670 posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
672 return Py_BuildValue("");
676 #ifdef BUP_HAVE_FILE_ATTRS
677 static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
684 if (!PyArg_ParseTuple(args, "s", &path))
687 fd = _open_noatime(path, O_NONBLOCK);
689 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
692 rc = ioctl(fd, FS_IOC_GETFLAGS, &attr);
696 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
700 return Py_BuildValue("k", attr);
702 #endif /* def BUP_HAVE_FILE_ATTRS */
705 #ifdef BUP_HAVE_FILE_ATTRS
706 static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
709 unsigned long orig_attr, attr;
713 if (!PyArg_ParseTuple(args, "sk", &path, &attr))
716 fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
718 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
720 // Restrict attr to modifiable flags acdeijstuADST -- see
721 // chattr(1) and the e2fsprogs source. Letter to flag mapping is
722 // in pf.c flags_array[].
723 attr &= FS_APPEND_FL | FS_COMPR_FL | FS_NODUMP_FL | FS_EXTENT_FL
724 | FS_IMMUTABLE_FL | FS_JOURNAL_DATA_FL | FS_SECRM_FL | FS_NOTAIL_FL
725 | FS_UNRM_FL | FS_NOATIME_FL | FS_DIRSYNC_FL | FS_SYNC_FL
726 | FS_TOPDIR_FL | FS_NOCOW_FL;
728 // The extents flag can't be removed, so don't (see chattr(1) and chattr.c).
729 rc = ioctl(fd, FS_IOC_GETFLAGS, &orig_attr);
733 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
735 attr |= (orig_attr & FS_EXTENT_FL);
737 rc = ioctl(fd, FS_IOC_SETFLAGS, &attr);
741 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
745 return Py_BuildValue("O", Py_None);
747 #endif /* def BUP_HAVE_FILE_ATTRS */
750 #if defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES) || defined(HAVE_LUTIMES)
752 static int bup_parse_xutime_args(char **path,
756 long *modification_ns,
757 PyObject *self, PyObject *args)
759 if (!PyArg_ParseTuple(args, "s((ll)(ll))",
762 modification, modification_ns))
767 PyErr_SetString(PyExc_ValueError, "access time is NaN");
770 else if (isinf(*access))
772 PyErr_SetString(PyExc_ValueError, "access time is infinite");
775 else if (isnan(*modification))
777 PyErr_SetString(PyExc_ValueError, "modification time is NaN");
780 else if (isinf(*modification))
782 PyErr_SetString(PyExc_ValueError, "modification time is infinite");
786 if (isnan(*access_ns))
788 PyErr_SetString(PyExc_ValueError, "access time ns is NaN");
791 else if (isinf(*access_ns))
793 PyErr_SetString(PyExc_ValueError, "access time ns is infinite");
796 else if (isnan(*modification_ns))
798 PyErr_SetString(PyExc_ValueError, "modification time ns is NaN");
801 else if (isinf(*modification_ns))
803 PyErr_SetString(PyExc_ValueError, "modification time ns is infinite");
810 #endif /* defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES)
811 || defined(HAVE_LUTIMES) */
814 #ifdef HAVE_UTIMENSAT
816 static PyObject *bup_xutime_ns(PyObject *self, PyObject *args,
821 long access, access_ns, modification, modification_ns;
822 struct timespec ts[2];
824 if (!bup_parse_xutime_args(&path, &access, &access_ns,
825 &modification, &modification_ns,
829 ts[0].tv_sec = access;
830 ts[0].tv_nsec = access_ns;
831 ts[1].tv_sec = modification;
832 ts[1].tv_nsec = modification_ns;
833 rc = utimensat(AT_FDCWD, path, ts,
834 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
836 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
838 return Py_BuildValue("O", Py_None);
842 #define BUP_HAVE_BUP_UTIME_NS 1
843 static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
845 return bup_xutime_ns(self, args, 1);
849 #define BUP_HAVE_BUP_LUTIME_NS 1
850 static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
852 return bup_xutime_ns(self, args, 0);
856 #else /* not defined(HAVE_UTIMENSAT) */
860 #define BUP_HAVE_BUP_UTIME_NS 1
861 static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
865 long access, access_ns, modification, modification_ns;
866 struct timeval tv[2];
868 if (!bup_parse_xutime_args(&path, &access, &access_ns,
869 &modification, &modification_ns,
873 tv[0].tv_sec = access;
874 tv[0].tv_usec = access_ns / 1000;
875 tv[1].tv_sec = modification;
876 tv[1].tv_usec = modification_ns / 1000;
877 rc = utimes(path, tv);
879 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
881 return Py_BuildValue("O", Py_None);
883 #endif /* def HAVE_UTIMES */
887 #define BUP_HAVE_BUP_LUTIME_NS 1
888 static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
892 long access, access_ns, modification, modification_ns;
893 struct timeval tv[2];
895 if (!bup_parse_xutime_args(&path, &access, &access_ns,
896 &modification, &modification_ns,
900 tv[0].tv_sec = access;
901 tv[0].tv_usec = access_ns / 1000;
902 tv[1].tv_sec = modification;
903 tv[1].tv_usec = modification_ns / 1000;
904 rc = lutimes(path, tv);
906 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
908 return Py_BuildValue("O", Py_None);
910 #endif /* def HAVE_LUTIMES */
913 #endif /* not defined(HAVE_UTIMENSAT) */
916 #ifdef HAVE_STAT_ST_ATIM
917 # define BUP_STAT_ATIME_NS(st) (st)->st_atim.tv_nsec
918 # define BUP_STAT_MTIME_NS(st) (st)->st_mtim.tv_nsec
919 # define BUP_STAT_CTIME_NS(st) (st)->st_ctim.tv_nsec
920 #elif defined HAVE_STAT_ST_ATIMENSEC
921 # define BUP_STAT_ATIME_NS(st) (st)->st_atimespec.tv_nsec
922 # define BUP_STAT_MTIME_NS(st) (st)->st_mtimespec.tv_nsec
923 # define BUP_STAT_CTIME_NS(st) (st)->st_ctimespec.tv_nsec
925 # define BUP_STAT_ATIME_NS(st) 0
926 # define BUP_STAT_MTIME_NS(st) 0
927 # define BUP_STAT_CTIME_NS(st) 0
931 static void set_invalid_timespec_msg(const char *field,
934 const char *filename,
937 if (filename != NULL)
938 PyErr_Format(PyExc_ValueError,
939 "invalid %s timespec (%lld %ld) for file \"%s\"",
940 field, sec, nsec, filename);
942 PyErr_Format(PyExc_ValueError,
943 "invalid %s timespec (%lld %ld) for file descriptor %d",
944 field, sec, nsec, fd);
948 static int normalize_timespec_values(const char *name,
951 const char *filename,
954 if (*nsec < -999999999 || *nsec > 999999999)
956 set_invalid_timespec_msg(name, *sec, *nsec, filename, fd);
961 if (*sec == LONG_MIN)
963 set_invalid_timespec_msg(name, *sec, *nsec, filename, fd);
973 static PyObject *stat_struct_to_py(const struct stat *st,
974 const char *filename,
977 long long atime = st->st_atime;
978 long long mtime = st->st_mtime;
979 long long ctime = st->st_ctime;
980 long atime_ns = BUP_STAT_ATIME_NS(st);
981 long mtime_ns = BUP_STAT_MTIME_NS(st);
982 long ctime_ns = BUP_STAT_CTIME_NS(st);
984 if (!normalize_timespec_values("atime", &atime, &atime_ns, filename, fd))
986 if (!normalize_timespec_values("mtime", &mtime, &mtime_ns, filename, fd))
988 if (!normalize_timespec_values("ctime", &ctime, &ctime_ns, filename, fd))
991 return Py_BuildValue("kkkkkkkk(Ll)(Ll)(Ll)",
992 (unsigned long) st->st_mode,
993 (unsigned long) st->st_ino,
994 (unsigned long) st->st_dev,
995 (unsigned long) st->st_nlink,
996 (unsigned long) st->st_uid,
997 (unsigned long) st->st_gid,
998 (unsigned long) st->st_rdev,
999 (unsigned long) st->st_size,
1009 static PyObject *bup_stat(PyObject *self, PyObject *args)
1014 if (!PyArg_ParseTuple(args, "s", &filename))
1018 rc = stat(filename, &st);
1020 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
1021 return stat_struct_to_py(&st, filename, 0);
1025 static PyObject *bup_lstat(PyObject *self, PyObject *args)
1030 if (!PyArg_ParseTuple(args, "s", &filename))
1034 rc = lstat(filename, &st);
1036 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
1037 return stat_struct_to_py(&st, filename, 0);
1041 static PyObject *bup_fstat(PyObject *self, PyObject *args)
1045 if (!PyArg_ParseTuple(args, "i", &fd))
1049 rc = fstat(fd, &st);
1051 return PyErr_SetFromErrno(PyExc_OSError);
1052 return stat_struct_to_py(&st, NULL, fd);
1056 static PyMethodDef helper_methods[] = {
1057 { "selftest", selftest, METH_VARARGS,
1058 "Check that the rolling checksum rolls correctly (for unit tests)." },
1059 { "blobbits", blobbits, METH_VARARGS,
1060 "Return the number of bits in the rolling checksum." },
1061 { "splitbuf", splitbuf, METH_VARARGS,
1062 "Split a list of strings based on a rolling checksum." },
1063 { "bitmatch", bitmatch, METH_VARARGS,
1064 "Count the number of matching prefix bits between two strings." },
1065 { "firstword", firstword, METH_VARARGS,
1066 "Return an int corresponding to the first 32 bits of buf." },
1067 { "bloom_contains", bloom_contains, METH_VARARGS,
1068 "Check if a bloom filter of 2^nbits bytes contains an object" },
1069 { "bloom_add", bloom_add, METH_VARARGS,
1070 "Add an object to a bloom filter of 2^nbits bytes" },
1071 { "extract_bits", extract_bits, METH_VARARGS,
1072 "Take the first 'nbits' bits from 'buf' and return them as an int." },
1073 { "merge_into", merge_into, METH_VARARGS,
1074 "Merges a bunch of idx and midx files into a single midx." },
1075 { "write_idx", write_idx, METH_VARARGS,
1076 "Write a PackIdxV2 file from an idx list of lists of tuples" },
1077 { "write_random", write_random, METH_VARARGS,
1078 "Write random bytes to the given file descriptor" },
1079 { "random_sha", random_sha, METH_VARARGS,
1080 "Return a random 20-byte string" },
1081 { "open_noatime", open_noatime, METH_VARARGS,
1082 "open() the given filename for read with O_NOATIME if possible" },
1083 { "fadvise_done", fadvise_done, METH_VARARGS,
1084 "Inform the kernel that we're finished with earlier parts of a file" },
1085 #ifdef BUP_HAVE_FILE_ATTRS
1086 { "get_linux_file_attr", bup_get_linux_file_attr, METH_VARARGS,
1087 "Return the Linux attributes for the given file." },
1089 #ifdef BUP_HAVE_FILE_ATTRS
1090 { "set_linux_file_attr", bup_set_linux_file_attr, METH_VARARGS,
1091 "Set the Linux attributes for the given file." },
1093 #ifdef BUP_HAVE_BUP_UTIME_NS
1094 { "bup_utime_ns", bup_utime_ns, METH_VARARGS,
1095 "Change path timestamps with up to nanosecond precision." },
1097 #ifdef BUP_HAVE_BUP_LUTIME_NS
1098 { "bup_lutime_ns", bup_lutime_ns, METH_VARARGS,
1099 "Change path timestamps with up to nanosecond precision;"
1100 " don't follow symlinks." },
1102 { "stat", bup_stat, METH_VARARGS,
1103 "Extended version of stat." },
1104 { "lstat", bup_lstat, METH_VARARGS,
1105 "Extended version of lstat." },
1106 { "fstat", bup_fstat, METH_VARARGS,
1107 "Extended version of fstat." },
1108 { NULL, NULL, 0, NULL }, // sentinel
1112 PyMODINIT_FUNC init_helpers(void)
1115 PyObject *m = Py_InitModule("_helpers", helper_methods);
1118 e = getenv("BUP_FORCE_TTY");
1119 istty2 = isatty(2) || (atoi(e ? e : "0") & 2);