13 // For some reason this isn't declared in Python.h
14 extern void Py_GetArgcArgv(int *argc, char ***argv);
16 static void unpythonize_argv(void)
21 Py_GetArgcArgv(&argc, &argv);
23 for (i = 0; i < argc-1; i++)
25 if (argv[i] + strlen(argv[i]) + 1 != argv[i+1])
27 // The argv block doesn't work the way we expected; it's unsafe
33 arge = argv[argc-1] + strlen(argv[argc-1]) + 1;
35 if (strstr(argv[0], "python") && argv[1] == argv[0] + strlen(argv[0]) + 1)
39 p = strrchr(argv[1], '/');
45 memmove(argv[0], p, len);
46 memset(arge - diff, 0, diff);
47 for (i = 0; i < argc; i++)
48 argv[i] = argv[i+1] ? argv[i+1]-diff : NULL;
54 static PyObject *selftest(PyObject *self, PyObject *args)
56 if (!PyArg_ParseTuple(args, ""))
59 return Py_BuildValue("i", !bupsplit_selftest());
63 static PyObject *blobbits(PyObject *self, PyObject *args)
65 if (!PyArg_ParseTuple(args, ""))
67 return Py_BuildValue("i", BUP_BLOBBITS);
71 static PyObject *splitbuf(PyObject *self, PyObject *args)
73 unsigned char *buf = NULL;
74 int len = 0, out = 0, bits = -1;
76 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
78 out = bupsplit_find_ofs(buf, len, &bits);
79 return Py_BuildValue("ii", out, bits);
83 static PyObject *bitmatch(PyObject *self, PyObject *args)
85 unsigned char *buf1 = NULL, *buf2 = NULL;
86 int len1 = 0, len2 = 0;
89 if (!PyArg_ParseTuple(args, "t#t#", &buf1, &len1, &buf2, &len2))
93 for (byte = 0; byte < len1 && byte < len2; byte++)
95 int b1 = buf1[byte], b2 = buf2[byte];
98 for (bit = 0; bit < 8; bit++)
99 if ( (b1 & (0x80 >> bit)) != (b2 & (0x80 >> bit)) )
105 return Py_BuildValue("i", byte*8 + bit);
109 static PyObject *firstword(PyObject *self, PyObject *args)
111 unsigned char *buf = NULL;
115 if (!PyArg_ParseTuple(args, "t#", &buf, &len))
121 v = ntohl(*(uint32_t *)buf);
122 return PyLong_FromUnsignedLong(v);
132 static void to_bloom_address_bitmask4(const bits40_t *buf,
133 const int nbits, uint64_t *v, unsigned char *bitmask)
138 mask = (1<<nbits) - 1;
139 raw = (((uint64_t)ntohl(buf->high)) << 8) | buf->low;
140 bit = (raw >> (37-nbits)) & 0x7;
141 *v = (raw >> (40-nbits)) & mask;
145 static void to_bloom_address_bitmask5(const uint32_t *buf,
146 const int nbits, uint32_t *v, unsigned char *bitmask)
151 mask = (1<<nbits) - 1;
153 bit = (raw >> (29-nbits)) & 0x7;
154 *v = (raw >> (32-nbits)) & mask;
159 #define BLOOM_SET_BIT(name, address, itype, otype) \
160 static void name(unsigned char *bloom, const void *buf, const int nbits)\
162 unsigned char bitmask;\
164 address((itype *)buf, nbits, &v, &bitmask);\
165 bloom[16+v] |= bitmask;\
167 BLOOM_SET_BIT(bloom_set_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
168 BLOOM_SET_BIT(bloom_set_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
171 #define BLOOM_GET_BIT(name, address, itype, otype) \
172 static int name(const unsigned char *bloom, const void *buf, const int nbits)\
174 unsigned char bitmask;\
176 address((itype *)buf, nbits, &v, &bitmask);\
177 return bloom[16+v] & bitmask;\
179 BLOOM_GET_BIT(bloom_get_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
180 BLOOM_GET_BIT(bloom_get_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
183 static PyObject *bloom_add(PyObject *self, PyObject *args)
185 unsigned char *sha = NULL, *bloom = NULL;
187 int len = 0, blen = 0, nbits = 0, k = 0;
189 if (!PyArg_ParseTuple(args, "w#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
192 if (blen < 16+(1<<nbits) || len % 20 != 0)
199 for (end = sha + len; sha < end; sha += 20/k)
200 bloom_set_bit5(bloom, sha, nbits);
206 for (end = sha + len; sha < end; sha += 20/k)
207 bloom_set_bit4(bloom, sha, nbits);
213 return Py_BuildValue("i", len/20);
216 static PyObject *bloom_contains(PyObject *self, PyObject *args)
218 unsigned char *sha = NULL, *bloom = NULL;
219 int len = 0, blen = 0, nbits = 0, k = 0;
223 if (!PyArg_ParseTuple(args, "t#s#ii", &bloom, &blen, &sha, &len, &nbits, &k))
233 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
234 if (!bloom_get_bit5(bloom, sha, nbits))
235 return Py_BuildValue("Oi", Py_None, steps);
241 for (steps = 1, end = sha + 20; sha < end; sha += 20/k, steps++)
242 if (!bloom_get_bit4(bloom, sha, nbits))
243 return Py_BuildValue("Oi", Py_None, steps);
248 return Py_BuildValue("Oi", Py_True, k);
252 static uint32_t _extract_bits(unsigned char *buf, int nbits)
256 mask = (1<<nbits) - 1;
257 v = ntohl(*(uint32_t *)buf);
258 v = (v >> (32-nbits)) & mask;
261 static PyObject *extract_bits(PyObject *self, PyObject *args)
263 unsigned char *buf = NULL;
264 int len = 0, nbits = 0;
266 if (!PyArg_ParseTuple(args, "t#i", &buf, &len, &nbits))
272 return PyLong_FromUnsignedLong(_extract_bits(buf, nbits));
277 unsigned char bytes[20];
289 static int _cmp_sha(const struct sha *sha1, const struct sha *sha2)
292 for (i = 0; i < 20; i++)
293 if (sha1->bytes[i] != sha2->bytes[i])
294 return sha1->bytes[i] - sha2->bytes[i];
299 static void _fix_idx_order(struct idx **idxs, int *last_i)
302 int low, mid, high, c = 0;
305 if (idxs[*last_i]->cur >= idxs[*last_i]->end)
307 idxs[*last_i] = NULL;
320 mid = (low + high) / 2;
321 c = _cmp_sha(idx->cur, idxs[mid]->cur);
333 memmove(&idxs[mid+1], &idxs[mid], (*last_i-mid)*sizeof(struct idx *));
338 static uint32_t _get_idx_i(struct idx *idx)
340 if (idx->cur_name == NULL)
341 return idx->name_base;
342 return ntohl(*idx->cur_name) + idx->name_base;
346 static PyObject *merge_into(PyObject *self, PyObject *args)
348 PyObject *ilist = NULL;
349 unsigned char *fmap = NULL;
350 struct sha *sha_ptr, *last = NULL;
351 uint32_t *table_ptr, *name_ptr;
352 struct idx **idxs = NULL;
353 int flen = 0, bits = 0, i;
354 uint32_t total, count, prefix;
358 if (!PyArg_ParseTuple(args, "w#iIO", &fmap, &flen, &bits, &total, &ilist))
361 num_i = PyList_Size(ilist);
362 idxs = (struct idx **)PyMem_Malloc(num_i * sizeof(struct idx *));
364 for (i = 0; i < num_i; i++)
366 long len, sha_ofs, name_map_ofs;
367 idxs[i] = (struct idx *)PyMem_Malloc(sizeof(struct idx));
368 PyObject *itup = PyList_GetItem(ilist, i);
369 if (!PyArg_ParseTuple(itup, "t#llli", &idxs[i]->map, &idxs[i]->bytes,
370 &len, &sha_ofs, &name_map_ofs, &idxs[i]->name_base))
372 idxs[i]->cur = (struct sha *)&idxs[i]->map[sha_ofs];
373 idxs[i]->end = &idxs[i]->cur[len];
375 idxs[i]->cur_name = (uint32_t *)&idxs[i]->map[name_map_ofs];
377 idxs[i]->cur_name = NULL;
379 table_ptr = (uint32_t *)&fmap[12];
380 sha_ptr = (struct sha *)&table_ptr[1<<bits];
381 name_ptr = (uint32_t *)&sha_ptr[total];
390 if (count % 102424 == 0 && istty)
391 fprintf(stderr, "midx: writing %.2f%% (%d/%d)\r",
392 count*100.0/total, count, total);
394 new_prefix = _extract_bits((unsigned char *)idx->cur, bits);
395 while (prefix < new_prefix)
396 table_ptr[prefix++] = htonl(count);
397 if (last == NULL || _cmp_sha(last, idx->cur) != 0)
399 memcpy(sha_ptr++, idx->cur, 20);
400 *name_ptr++ = htonl(_get_idx_i(idx));
404 if (idx->cur_name != NULL)
406 _fix_idx_order(idxs, &last_i);
409 table_ptr[prefix] = htonl(count);
412 return PyLong_FromUnsignedLong(count);
415 // This function should techniclly be macro'd out if it's going to be used
416 // more than ocasionally. As of this writing, it'll actually never be called
417 // in real world bup scenarios (because our packs are < MAX_INT bytes).
418 static uint64_t htonll(uint64_t value)
420 static const int endian_test = 42;
422 if (*(char *)&endian_test == endian_test) // LSB-MSB
423 return ((uint64_t)htonl(value & 0xFFFFFFFF) << 32) | htonl(value >> 32);
424 return value; // already in network byte order MSB-LSB
427 static PyObject *write_idx(PyObject *self, PyObject *args)
429 PyObject *pf = NULL, *idx = NULL;
432 unsigned char *fmap = NULL;
436 int i, j, ofs64_count;
437 uint32_t *fan_ptr, *crc_ptr, *ofs_ptr;
440 if (!PyArg_ParseTuple(args, "Ow#OI", &pf, &fmap, &flen, &idx, &total))
443 fan_ptr = (uint32_t *)&fmap[8];
444 sha_ptr = (struct sha *)&fan_ptr[256];
445 crc_ptr = (uint32_t *)&sha_ptr[total];
446 ofs_ptr = (uint32_t *)&crc_ptr[total];
447 f = PyFile_AsFile(pf);
451 for (i = 0; i < 256; ++i)
454 part = PyList_GET_ITEM(idx, i);
456 plen = PyList_GET_SIZE(part);
458 *fan_ptr++ = htonl(count);
459 for (j = 0; j < plen; ++j)
461 unsigned char *sha = NULL;
465 if (!PyArg_ParseTuple(PyList_GET_ITEM(part, j), "t#IK",
466 &sha, &sha_len, &crc, &ofs))
470 memcpy(sha_ptr++, sha, 20);
471 *crc_ptr++ = htonl(crc);
472 if (ofs > 0x7fffffff)
474 uint64_t nofs = htonll(ofs);
475 fwrite(&nofs, 8, 1, f);
476 ofs = 0x80000000 | ofs64_count++;
478 *ofs_ptr++ = htonl((uint32_t)ofs);
481 return PyLong_FromUnsignedLong(count);
485 // I would have made this a lower-level function that just fills in a buffer
486 // with random values, and then written those values from python. But that's
487 // about 20% slower in my tests, and since we typically generate random
488 // numbers for benchmarking other parts of bup, any slowness in generating
489 // random bytes will make our benchmarks inaccurate. Plus nobody wants
490 // pseudorandom bytes much except for this anyway.
491 static PyObject *write_random(PyObject *self, PyObject *args)
493 uint32_t buf[1024/4];
494 int fd = -1, seed = 0, verbose = 0;
496 long long len = 0, kbytes = 0, written = 0;
498 if (!PyArg_ParseTuple(args, "iLii", &fd, &len, &seed, &verbose))
503 for (kbytes = 0; kbytes < len/1024; kbytes++)
506 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
508 ret = write(fd, buf, sizeof(buf));
512 if (ret < (int)sizeof(buf))
514 if (verbose && kbytes/1024 > 0 && !(kbytes%1024))
515 fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
518 // handle non-multiples of 1024
522 for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
524 ret = write(fd, buf, len % 1024);
531 fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
532 return Py_BuildValue("L", written);
536 static PyObject *random_sha(PyObject *self, PyObject *args)
538 static int seeded = 0;
539 uint32_t shabuf[20/4];
544 assert(sizeof(shabuf) == 20);
549 if (!PyArg_ParseTuple(args, ""))
552 memset(shabuf, 0, sizeof(shabuf));
553 for (i=0; i < 20/4; i++)
554 shabuf[i] = random();
555 return Py_BuildValue("s#", shabuf, 20);
559 static PyObject *open_noatime(PyObject *self, PyObject *args)
561 char *filename = NULL;
562 int attrs, attrs_noatime, fd;
563 if (!PyArg_ParseTuple(args, "s", &filename))
570 attrs |= O_LARGEFILE;
572 attrs_noatime = attrs;
574 attrs_noatime |= O_NOATIME;
576 fd = open(filename, attrs_noatime);
577 if (fd < 0 && errno == EPERM)
579 // older Linux kernels would return EPERM if you used O_NOATIME
580 // and weren't the file's owner. This pointless restriction was
581 // relaxed eventually, but we have to handle it anyway.
582 // (VERY old kernels didn't recognized O_NOATIME, but they would
583 // just harmlessly ignore it, so this branch won't trigger)
584 fd = open(filename, attrs);
587 return PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
588 return Py_BuildValue("i", fd);
592 static PyObject *fadvise_done(PyObject *self, PyObject *args)
596 if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
598 #ifdef POSIX_FADV_DONTNEED
599 posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
601 return Py_BuildValue("");
605 static PyMethodDef faster_methods[] = {
606 { "selftest", selftest, METH_VARARGS,
607 "Check that the rolling checksum rolls correctly (for unit tests)." },
608 { "blobbits", blobbits, METH_VARARGS,
609 "Return the number of bits in the rolling checksum." },
610 { "splitbuf", splitbuf, METH_VARARGS,
611 "Split a list of strings based on a rolling checksum." },
612 { "bitmatch", bitmatch, METH_VARARGS,
613 "Count the number of matching prefix bits between two strings." },
614 { "firstword", firstword, METH_VARARGS,
615 "Return an int corresponding to the first 32 bits of buf." },
616 { "bloom_contains", bloom_contains, METH_VARARGS,
617 "Check if a bloom filter of 2^nbits bytes contains an object" },
618 { "bloom_add", bloom_add, METH_VARARGS,
619 "Add an object to a bloom filter of 2^nbits bytes" },
620 { "extract_bits", extract_bits, METH_VARARGS,
621 "Take the first 'nbits' bits from 'buf' and return them as an int." },
622 { "merge_into", merge_into, METH_VARARGS,
623 "Merges a bunch of idx and midx files into a single midx." },
624 { "write_idx", write_idx, METH_VARARGS,
625 "Write a PackIdxV2 file from an idx list of lists of tuples" },
626 { "write_random", write_random, METH_VARARGS,
627 "Write random bytes to the given file descriptor" },
628 { "random_sha", random_sha, METH_VARARGS,
629 "Return a random 20-byte string" },
630 { "open_noatime", open_noatime, METH_VARARGS,
631 "open() the given filename for read with O_NOATIME if possible" },
632 { "fadvise_done", fadvise_done, METH_VARARGS,
633 "Inform the kernel that we're finished with earlier parts of a file" },
634 { NULL, NULL, 0, NULL }, // sentinel
637 PyMODINIT_FUNC init_helpers(void)
639 Py_InitModule("_helpers", faster_methods);
640 istty = isatty(2) || getenv("BUP_FORCE_TTY");