##// END OF EJS Templates
lazymanifest: fix typo s/typles/tuples/
Javi Merino -
r29254:c5912a0b stable
parent child Browse files
Show More
@@ -1,926 +1,926 b''
1 1 /*
2 2 * manifest.c - manifest type that does on-demand parsing.
3 3 *
4 4 * Copyright 2015, Google Inc.
5 5 *
6 6 * This software may be used and distributed according to the terms of
7 7 * the GNU General Public License, incorporated herein by reference.
8 8 */
9 9 #include <Python.h>
10 10
11 11 #include <assert.h>
12 12 #include <string.h>
13 13 #include <stdlib.h>
14 14
15 15 #include "util.h"
16 16
17 17 #define DEFAULT_LINES 100000
18 18
19 19 typedef struct {
20 20 char *start;
21 21 Py_ssize_t len; /* length of line including terminal newline */
22 22 char hash_suffix;
23 23 bool from_malloc;
24 24 bool deleted;
25 25 } line;
26 26
27 27 typedef struct {
28 28 PyObject_HEAD
29 29 PyObject *pydata;
30 30 line *lines;
31 31 int numlines; /* number of line entries */
32 32 int livelines; /* number of non-deleted lines */
33 33 int maxlines; /* allocated number of lines */
34 34 bool dirty;
35 35 } lazymanifest;
36 36
37 37 #define MANIFEST_OOM -1
38 38 #define MANIFEST_NOT_SORTED -2
39 39 #define MANIFEST_MALFORMED -3
40 40
41 41 /* defined in parsers.c */
42 42 PyObject *unhexlify(const char *str, int len);
43 43
44 44 /* get the length of the path for a line */
45 45 static size_t pathlen(line *l) {
46 46 return strlen(l->start);
47 47 }
48 48
49 49 /* get the node value of a single line */
50 50 static PyObject *nodeof(line *l) {
51 51 char *s = l->start;
52 52 ssize_t llen = pathlen(l);
53 53 PyObject *hash = unhexlify(s + llen + 1, 40);
54 54 if (!hash) {
55 55 return NULL;
56 56 }
57 57 if (l->hash_suffix != '\0') {
58 58 char newhash[21];
59 59 memcpy(newhash, PyString_AsString(hash), 20);
60 60 Py_DECREF(hash);
61 61 newhash[20] = l->hash_suffix;
62 62 hash = PyString_FromStringAndSize(newhash, 21);
63 63 }
64 64 return hash;
65 65 }
66 66
67 67 /* get the node hash and flags of a line as a tuple */
68 68 static PyObject *hashflags(line *l)
69 69 {
70 70 char *s = l->start;
71 71 size_t plen = pathlen(l);
72 72 PyObject *hash = nodeof(l);
73 73
74 74 /* 40 for hash, 1 for null byte, 1 for newline */
75 75 size_t hplen = plen + 42;
76 76 Py_ssize_t flen = l->len - hplen;
77 77 PyObject *flags;
78 78 PyObject *tup;
79 79
80 80 if (!hash)
81 81 return NULL;
82 82 flags = PyString_FromStringAndSize(s + hplen - 1, flen);
83 83 if (!flags) {
84 84 Py_DECREF(hash);
85 85 return NULL;
86 86 }
87 87 tup = PyTuple_Pack(2, hash, flags);
88 88 Py_DECREF(flags);
89 89 Py_DECREF(hash);
90 90 return tup;
91 91 }
92 92
93 93 /* if we're about to run out of space in the line index, add more */
94 94 static bool realloc_if_full(lazymanifest *self)
95 95 {
96 96 if (self->numlines == self->maxlines) {
97 97 self->maxlines *= 2;
98 98 self->lines = realloc(self->lines, self->maxlines * sizeof(line));
99 99 }
100 100 return !!self->lines;
101 101 }
102 102
103 103 /*
104 104 * Find the line boundaries in the manifest that 'data' points to and store
105 105 * information about each line in 'self'.
106 106 */
107 107 static int find_lines(lazymanifest *self, char *data, Py_ssize_t len)
108 108 {
109 109 char *prev = NULL;
110 110 while (len > 0) {
111 111 line *l;
112 112 char *next = memchr(data, '\n', len);
113 113 if (!next) {
114 114 return MANIFEST_MALFORMED;
115 115 }
116 116 next++; /* advance past newline */
117 117 if (!realloc_if_full(self)) {
118 118 return MANIFEST_OOM; /* no memory */
119 119 }
120 120 if (prev && strcmp(prev, data) > -1) {
121 121 /* This data isn't sorted, so we have to abort. */
122 122 return MANIFEST_NOT_SORTED;
123 123 }
124 124 l = self->lines + ((self->numlines)++);
125 125 l->start = data;
126 126 l->len = next - data;
127 127 l->hash_suffix = '\0';
128 128 l->from_malloc = false;
129 129 l->deleted = false;
130 130 len = len - l->len;
131 131 prev = data;
132 132 data = next;
133 133 }
134 134 self->livelines = self->numlines;
135 135 return 0;
136 136 }
137 137
138 138 static int lazymanifest_init(lazymanifest *self, PyObject *args)
139 139 {
140 140 char *data;
141 141 Py_ssize_t len;
142 142 int err, ret;
143 143 PyObject *pydata;
144 144 if (!PyArg_ParseTuple(args, "S", &pydata)) {
145 145 return -1;
146 146 }
147 147 err = PyString_AsStringAndSize(pydata, &data, &len);
148 148
149 149 self->dirty = false;
150 150 if (err == -1)
151 151 return -1;
152 152 self->pydata = pydata;
153 153 Py_INCREF(self->pydata);
154 154 Py_BEGIN_ALLOW_THREADS
155 155 self->lines = malloc(DEFAULT_LINES * sizeof(line));
156 156 self->maxlines = DEFAULT_LINES;
157 157 self->numlines = 0;
158 158 if (!self->lines)
159 159 ret = MANIFEST_OOM;
160 160 else
161 161 ret = find_lines(self, data, len);
162 162 Py_END_ALLOW_THREADS
163 163 switch (ret) {
164 164 case 0:
165 165 break;
166 166 case MANIFEST_OOM:
167 167 PyErr_NoMemory();
168 168 break;
169 169 case MANIFEST_NOT_SORTED:
170 170 PyErr_Format(PyExc_ValueError,
171 171 "Manifest lines not in sorted order.");
172 172 break;
173 173 case MANIFEST_MALFORMED:
174 174 PyErr_Format(PyExc_ValueError,
175 175 "Manifest did not end in a newline.");
176 176 break;
177 177 default:
178 178 PyErr_Format(PyExc_ValueError,
179 179 "Unknown problem parsing manifest.");
180 180 }
181 181 return ret == 0 ? 0 : -1;
182 182 }
183 183
184 184 static void lazymanifest_dealloc(lazymanifest *self)
185 185 {
186 186 /* free any extra lines we had to allocate */
187 187 int i;
188 188 for (i = 0; i < self->numlines; i++) {
189 189 if (self->lines[i].from_malloc) {
190 190 free(self->lines[i].start);
191 191 }
192 192 }
193 193 if (self->lines) {
194 194 free(self->lines);
195 195 self->lines = NULL;
196 196 }
197 197 if (self->pydata) {
198 198 Py_DECREF(self->pydata);
199 199 self->pydata = NULL;
200 200 }
201 201 PyObject_Del(self);
202 202 }
203 203
204 204 /* iteration support */
205 205
206 206 typedef struct {
207 207 PyObject_HEAD lazymanifest *m;
208 208 Py_ssize_t pos;
209 209 } lmIter;
210 210
211 211 static void lmiter_dealloc(PyObject *o)
212 212 {
213 213 lmIter *self = (lmIter *)o;
214 214 Py_DECREF(self->m);
215 215 PyObject_Del(self);
216 216 }
217 217
218 218 static line *lmiter_nextline(lmIter *self)
219 219 {
220 220 do {
221 221 self->pos++;
222 222 if (self->pos >= self->m->numlines) {
223 223 return NULL;
224 224 }
225 225 /* skip over deleted manifest entries */
226 226 } while (self->m->lines[self->pos].deleted);
227 227 return self->m->lines + self->pos;
228 228 }
229 229
230 230 static PyObject *lmiter_iterentriesnext(PyObject *o)
231 231 {
232 232 size_t pl;
233 233 line *l;
234 234 Py_ssize_t consumed;
235 235 PyObject *ret = NULL, *path = NULL, *hash = NULL, *flags = NULL;
236 236 l = lmiter_nextline((lmIter *)o);
237 237 if (!l) {
238 238 goto done;
239 239 }
240 240 pl = pathlen(l);
241 241 path = PyString_FromStringAndSize(l->start, pl);
242 242 hash = nodeof(l);
243 243 consumed = pl + 41;
244 244 flags = PyString_FromStringAndSize(l->start + consumed,
245 245 l->len - consumed - 1);
246 246 if (!path || !hash || !flags) {
247 247 goto done;
248 248 }
249 249 ret = PyTuple_Pack(3, path, hash, flags);
250 250 done:
251 251 Py_XDECREF(path);
252 252 Py_XDECREF(hash);
253 253 Py_XDECREF(flags);
254 254 return ret;
255 255 }
256 256
257 257 static PyTypeObject lazymanifestEntriesIterator = {
258 258 PyObject_HEAD_INIT(NULL)
259 259 0, /*ob_size */
260 260 "parsers.lazymanifest.entriesiterator", /*tp_name */
261 261 sizeof(lmIter), /*tp_basicsize */
262 262 0, /*tp_itemsize */
263 263 lmiter_dealloc, /*tp_dealloc */
264 264 0, /*tp_print */
265 265 0, /*tp_getattr */
266 266 0, /*tp_setattr */
267 267 0, /*tp_compare */
268 268 0, /*tp_repr */
269 269 0, /*tp_as_number */
270 270 0, /*tp_as_sequence */
271 271 0, /*tp_as_mapping */
272 272 0, /*tp_hash */
273 273 0, /*tp_call */
274 274 0, /*tp_str */
275 275 0, /*tp_getattro */
276 276 0, /*tp_setattro */
277 277 0, /*tp_as_buffer */
278 278 /* tp_flags: Py_TPFLAGS_HAVE_ITER tells python to
279 279 use tp_iter and tp_iternext fields. */
280 280 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
281 281 "Iterator for 3-tuples in a lazymanifest.", /* tp_doc */
282 282 0, /* tp_traverse */
283 283 0, /* tp_clear */
284 284 0, /* tp_richcompare */
285 285 0, /* tp_weaklistoffset */
286 286 PyObject_SelfIter, /* tp_iter: __iter__() method */
287 287 lmiter_iterentriesnext, /* tp_iternext: next() method */
288 288 };
289 289
290 290 static PyObject *lmiter_iterkeysnext(PyObject *o)
291 291 {
292 292 size_t pl;
293 293 line *l = lmiter_nextline((lmIter *)o);
294 294 if (!l) {
295 295 return NULL;
296 296 }
297 297 pl = pathlen(l);
298 298 return PyString_FromStringAndSize(l->start, pl);
299 299 }
300 300
301 301 static PyTypeObject lazymanifestKeysIterator = {
302 302 PyObject_HEAD_INIT(NULL)
303 303 0, /*ob_size */
304 304 "parsers.lazymanifest.keysiterator", /*tp_name */
305 305 sizeof(lmIter), /*tp_basicsize */
306 306 0, /*tp_itemsize */
307 307 lmiter_dealloc, /*tp_dealloc */
308 308 0, /*tp_print */
309 309 0, /*tp_getattr */
310 310 0, /*tp_setattr */
311 311 0, /*tp_compare */
312 312 0, /*tp_repr */
313 313 0, /*tp_as_number */
314 314 0, /*tp_as_sequence */
315 315 0, /*tp_as_mapping */
316 316 0, /*tp_hash */
317 317 0, /*tp_call */
318 318 0, /*tp_str */
319 319 0, /*tp_getattro */
320 320 0, /*tp_setattro */
321 321 0, /*tp_as_buffer */
322 322 /* tp_flags: Py_TPFLAGS_HAVE_ITER tells python to
323 323 use tp_iter and tp_iternext fields. */
324 324 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
325 325 "Keys iterator for a lazymanifest.", /* tp_doc */
326 326 0, /* tp_traverse */
327 327 0, /* tp_clear */
328 328 0, /* tp_richcompare */
329 329 0, /* tp_weaklistoffset */
330 330 PyObject_SelfIter, /* tp_iter: __iter__() method */
331 331 lmiter_iterkeysnext, /* tp_iternext: next() method */
332 332 };
333 333
334 334 static lazymanifest *lazymanifest_copy(lazymanifest *self);
335 335
336 336 static PyObject *lazymanifest_getentriesiter(lazymanifest *self)
337 337 {
338 338 lmIter *i = NULL;
339 339 lazymanifest *t = lazymanifest_copy(self);
340 340 if (!t) {
341 341 PyErr_NoMemory();
342 342 return NULL;
343 343 }
344 344 i = PyObject_New(lmIter, &lazymanifestEntriesIterator);
345 345 if (i) {
346 346 i->m = t;
347 347 i->pos = -1;
348 348 } else {
349 349 Py_DECREF(t);
350 350 PyErr_NoMemory();
351 351 }
352 352 return (PyObject *)i;
353 353 }
354 354
355 355 static PyObject *lazymanifest_getkeysiter(lazymanifest *self)
356 356 {
357 357 lmIter *i = NULL;
358 358 lazymanifest *t = lazymanifest_copy(self);
359 359 if (!t) {
360 360 PyErr_NoMemory();
361 361 return NULL;
362 362 }
363 363 i = PyObject_New(lmIter, &lazymanifestKeysIterator);
364 364 if (i) {
365 365 i->m = t;
366 366 i->pos = -1;
367 367 } else {
368 368 Py_DECREF(t);
369 369 PyErr_NoMemory();
370 370 }
371 371 return (PyObject *)i;
372 372 }
373 373
374 374 /* __getitem__ and __setitem__ support */
375 375
376 376 static Py_ssize_t lazymanifest_size(lazymanifest *self)
377 377 {
378 378 return self->livelines;
379 379 }
380 380
381 381 static int linecmp(const void *left, const void *right)
382 382 {
383 383 return strcmp(((const line *)left)->start,
384 384 ((const line *)right)->start);
385 385 }
386 386
387 387 static PyObject *lazymanifest_getitem(lazymanifest *self, PyObject *key)
388 388 {
389 389 line needle;
390 390 line *hit;
391 391 if (!PyString_Check(key)) {
392 392 PyErr_Format(PyExc_TypeError,
393 393 "getitem: manifest keys must be a string.");
394 394 return NULL;
395 395 }
396 396 needle.start = PyString_AsString(key);
397 397 hit = bsearch(&needle, self->lines, self->numlines, sizeof(line),
398 398 &linecmp);
399 399 if (!hit || hit->deleted) {
400 400 PyErr_Format(PyExc_KeyError, "No such manifest entry.");
401 401 return NULL;
402 402 }
403 403 return hashflags(hit);
404 404 }
405 405
406 406 static int lazymanifest_delitem(lazymanifest *self, PyObject *key)
407 407 {
408 408 line needle;
409 409 line *hit;
410 410 if (!PyString_Check(key)) {
411 411 PyErr_Format(PyExc_TypeError,
412 412 "delitem: manifest keys must be a string.");
413 413 return -1;
414 414 }
415 415 needle.start = PyString_AsString(key);
416 416 hit = bsearch(&needle, self->lines, self->numlines, sizeof(line),
417 417 &linecmp);
418 418 if (!hit || hit->deleted) {
419 419 PyErr_Format(PyExc_KeyError,
420 420 "Tried to delete nonexistent manifest entry.");
421 421 return -1;
422 422 }
423 423 self->dirty = true;
424 424 hit->deleted = true;
425 425 self->livelines--;
426 426 return 0;
427 427 }
428 428
429 429 /* Do a binary search for the insertion point for new, creating the
430 430 * new entry if needed. */
431 431 static int internalsetitem(lazymanifest *self, line *new) {
432 432 int start = 0, end = self->numlines;
433 433 while (start < end) {
434 434 int pos = start + (end - start) / 2;
435 435 int c = linecmp(new, self->lines + pos);
436 436 if (c < 0)
437 437 end = pos;
438 438 else if (c > 0)
439 439 start = pos + 1;
440 440 else {
441 441 if (self->lines[pos].deleted)
442 442 self->livelines++;
443 443 if (self->lines[pos].from_malloc)
444 444 free(self->lines[pos].start);
445 445 start = pos;
446 446 goto finish;
447 447 }
448 448 }
449 449 /* being here means we need to do an insert */
450 450 if (!realloc_if_full(self)) {
451 451 PyErr_NoMemory();
452 452 return -1;
453 453 }
454 454 memmove(self->lines + start + 1, self->lines + start,
455 455 (self->numlines - start) * sizeof(line));
456 456 self->numlines++;
457 457 self->livelines++;
458 458 finish:
459 459 self->lines[start] = *new;
460 460 self->dirty = true;
461 461 return 0;
462 462 }
463 463
464 464 static int lazymanifest_setitem(
465 465 lazymanifest *self, PyObject *key, PyObject *value)
466 466 {
467 467 char *path;
468 468 Py_ssize_t plen;
469 469 PyObject *pyhash;
470 470 Py_ssize_t hlen;
471 471 char *hash;
472 472 PyObject *pyflags;
473 473 char *flags;
474 474 Py_ssize_t flen;
475 475 size_t dlen;
476 476 char *dest;
477 477 int i;
478 478 line new;
479 479 if (!PyString_Check(key)) {
480 480 PyErr_Format(PyExc_TypeError,
481 481 "setitem: manifest keys must be a string.");
482 482 return -1;
483 483 }
484 484 if (!value) {
485 485 return lazymanifest_delitem(self, key);
486 486 }
487 487 if (!PyTuple_Check(value) || PyTuple_Size(value) != 2) {
488 488 PyErr_Format(PyExc_TypeError,
489 489 "Manifest values must be a tuple of (node, flags).");
490 490 return -1;
491 491 }
492 492 if (PyString_AsStringAndSize(key, &path, &plen) == -1) {
493 493 return -1;
494 494 }
495 495
496 496 pyhash = PyTuple_GetItem(value, 0);
497 497 if (!PyString_Check(pyhash)) {
498 498 PyErr_Format(PyExc_TypeError,
499 499 "node must be a 20-byte string");
500 500 return -1;
501 501 }
502 502 hlen = PyString_Size(pyhash);
503 503 /* Some parts of the codebase try and set 21 or 22
504 504 * byte "hash" values in order to perturb things for
505 505 * status. We have to preserve at least the 21st
506 506 * byte. Sigh. If there's a 22nd byte, we drop it on
507 507 * the floor, which works fine.
508 508 */
509 509 if (hlen != 20 && hlen != 21 && hlen != 22) {
510 510 PyErr_Format(PyExc_TypeError,
511 511 "node must be a 20-byte string");
512 512 return -1;
513 513 }
514 514 hash = PyString_AsString(pyhash);
515 515
516 516 pyflags = PyTuple_GetItem(value, 1);
517 517 if (!PyString_Check(pyflags) || PyString_Size(pyflags) > 1) {
518 518 PyErr_Format(PyExc_TypeError,
519 519 "flags must a 0 or 1 byte string");
520 520 return -1;
521 521 }
522 522 if (PyString_AsStringAndSize(pyflags, &flags, &flen) == -1) {
523 523 return -1;
524 524 }
525 525 /* one null byte and one newline */
526 526 dlen = plen + 41 + flen + 1;
527 527 dest = malloc(dlen);
528 528 if (!dest) {
529 529 PyErr_NoMemory();
530 530 return -1;
531 531 }
532 532 memcpy(dest, path, plen + 1);
533 533 for (i = 0; i < 20; i++) {
534 534 /* Cast to unsigned, so it will not get sign-extended when promoted
535 535 * to int (as is done when passing to a variadic function)
536 536 */
537 537 sprintf(dest + plen + 1 + (i * 2), "%02x", (unsigned char)hash[i]);
538 538 }
539 539 memcpy(dest + plen + 41, flags, flen);
540 540 dest[plen + 41 + flen] = '\n';
541 541 new.start = dest;
542 542 new.len = dlen;
543 543 new.hash_suffix = '\0';
544 544 if (hlen > 20) {
545 545 new.hash_suffix = hash[20];
546 546 }
547 547 new.from_malloc = true; /* is `start` a pointer we allocated? */
548 548 new.deleted = false; /* is this entry deleted? */
549 549 if (internalsetitem(self, &new)) {
550 550 return -1;
551 551 }
552 552 return 0;
553 553 }
554 554
555 555 static PyMappingMethods lazymanifest_mapping_methods = {
556 556 (lenfunc)lazymanifest_size, /* mp_length */
557 557 (binaryfunc)lazymanifest_getitem, /* mp_subscript */
558 558 (objobjargproc)lazymanifest_setitem, /* mp_ass_subscript */
559 559 };
560 560
561 561 /* sequence methods (important or __contains__ builds an iterator) */
562 562
563 563 static int lazymanifest_contains(lazymanifest *self, PyObject *key)
564 564 {
565 565 line needle;
566 566 line *hit;
567 567 if (!PyString_Check(key)) {
568 568 /* Our keys are always strings, so if the contains
569 569 * check is for a non-string, just return false. */
570 570 return 0;
571 571 }
572 572 needle.start = PyString_AsString(key);
573 573 hit = bsearch(&needle, self->lines, self->numlines, sizeof(line),
574 574 &linecmp);
575 575 if (!hit || hit->deleted) {
576 576 return 0;
577 577 }
578 578 return 1;
579 579 }
580 580
581 581 static PySequenceMethods lazymanifest_seq_meths = {
582 582 (lenfunc)lazymanifest_size, /* sq_length */
583 583 0, /* sq_concat */
584 584 0, /* sq_repeat */
585 585 0, /* sq_item */
586 586 0, /* sq_slice */
587 587 0, /* sq_ass_item */
588 588 0, /* sq_ass_slice */
589 589 (objobjproc)lazymanifest_contains, /* sq_contains */
590 590 0, /* sq_inplace_concat */
591 591 0, /* sq_inplace_repeat */
592 592 };
593 593
594 594
595 595 /* Other methods (copy, diff, etc) */
596 596 static PyTypeObject lazymanifestType;
597 597
598 598 /* If the manifest has changes, build the new manifest text and reindex it. */
599 599 static int compact(lazymanifest *self) {
600 600 int i;
601 601 ssize_t need = 0;
602 602 char *data;
603 603 line *src, *dst;
604 604 PyObject *pydata;
605 605 if (!self->dirty)
606 606 return 0;
607 607 for (i = 0; i < self->numlines; i++) {
608 608 if (!self->lines[i].deleted) {
609 609 need += self->lines[i].len;
610 610 }
611 611 }
612 612 pydata = PyString_FromStringAndSize(NULL, need);
613 613 if (!pydata)
614 614 return -1;
615 615 data = PyString_AsString(pydata);
616 616 if (!data) {
617 617 return -1;
618 618 }
619 619 src = self->lines;
620 620 dst = self->lines;
621 621 for (i = 0; i < self->numlines; i++, src++) {
622 622 char *tofree = NULL;
623 623 if (src->from_malloc) {
624 624 tofree = src->start;
625 625 }
626 626 if (!src->deleted) {
627 627 memcpy(data, src->start, src->len);
628 628 *dst = *src;
629 629 dst->start = data;
630 630 dst->from_malloc = false;
631 631 data += dst->len;
632 632 dst++;
633 633 }
634 634 free(tofree);
635 635 }
636 636 Py_DECREF(self->pydata);
637 637 self->pydata = pydata;
638 638 self->numlines = self->livelines;
639 639 self->dirty = false;
640 640 return 0;
641 641 }
642 642
643 643 static PyObject *lazymanifest_text(lazymanifest *self)
644 644 {
645 645 if (compact(self) != 0) {
646 646 PyErr_NoMemory();
647 647 return NULL;
648 648 }
649 649 Py_INCREF(self->pydata);
650 650 return self->pydata;
651 651 }
652 652
653 653 static lazymanifest *lazymanifest_copy(lazymanifest *self)
654 654 {
655 655 lazymanifest *copy = NULL;
656 656 if (compact(self) != 0) {
657 657 goto nomem;
658 658 }
659 659 copy = PyObject_New(lazymanifest, &lazymanifestType);
660 660 if (!copy) {
661 661 goto nomem;
662 662 }
663 663 copy->numlines = self->numlines;
664 664 copy->livelines = self->livelines;
665 665 copy->dirty = false;
666 666 copy->lines = malloc(self->maxlines *sizeof(line));
667 667 if (!copy->lines) {
668 668 goto nomem;
669 669 }
670 670 memcpy(copy->lines, self->lines, self->numlines * sizeof(line));
671 671 copy->maxlines = self->maxlines;
672 672 copy->pydata = self->pydata;
673 673 Py_INCREF(copy->pydata);
674 674 return copy;
675 675 nomem:
676 676 PyErr_NoMemory();
677 677 Py_XDECREF(copy);
678 678 return NULL;
679 679 }
680 680
681 681 static lazymanifest *lazymanifest_filtercopy(
682 682 lazymanifest *self, PyObject *matchfn)
683 683 {
684 684 lazymanifest *copy = NULL;
685 685 int i;
686 686 if (!PyCallable_Check(matchfn)) {
687 687 PyErr_SetString(PyExc_TypeError, "matchfn must be callable");
688 688 return NULL;
689 689 }
690 690 /* compact ourselves first to avoid double-frees later when we
691 691 * compact tmp so that it doesn't have random pointers to our
692 692 * underlying from_malloc-data (self->pydata is safe) */
693 693 if (compact(self) != 0) {
694 694 goto nomem;
695 695 }
696 696 copy = PyObject_New(lazymanifest, &lazymanifestType);
697 697 if (!copy) {
698 698 goto nomem;
699 699 }
700 700 copy->dirty = true;
701 701 copy->lines = malloc(self->maxlines * sizeof(line));
702 702 if (!copy->lines) {
703 703 goto nomem;
704 704 }
705 705 copy->maxlines = self->maxlines;
706 706 copy->numlines = 0;
707 707 copy->pydata = self->pydata;
708 708 Py_INCREF(self->pydata);
709 709 for (i = 0; i < self->numlines; i++) {
710 710 PyObject *arglist = NULL, *result = NULL;
711 711 arglist = Py_BuildValue("(s)", self->lines[i].start);
712 712 if (!arglist) {
713 713 return NULL;
714 714 }
715 715 result = PyObject_CallObject(matchfn, arglist);
716 716 Py_DECREF(arglist);
717 717 /* if the callback raised an exception, just let it
718 718 * through and give up */
719 719 if (!result) {
720 720 free(copy->lines);
721 721 Py_DECREF(self->pydata);
722 722 return NULL;
723 723 }
724 724 if (PyObject_IsTrue(result)) {
725 725 assert(!(self->lines[i].from_malloc));
726 726 copy->lines[copy->numlines++] = self->lines[i];
727 727 }
728 728 Py_DECREF(result);
729 729 }
730 730 copy->livelines = copy->numlines;
731 731 return copy;
732 732 nomem:
733 733 PyErr_NoMemory();
734 734 Py_XDECREF(copy);
735 735 return NULL;
736 736 }
737 737
738 738 static PyObject *lazymanifest_diff(lazymanifest *self, PyObject *args)
739 739 {
740 740 lazymanifest *other;
741 741 PyObject *pyclean = NULL;
742 742 bool listclean;
743 743 PyObject *emptyTup = NULL, *ret = NULL;
744 744 PyObject *es;
745 745 int sneedle = 0, oneedle = 0;
746 746 if (!PyArg_ParseTuple(args, "O!|O", &lazymanifestType, &other, &pyclean)) {
747 747 return NULL;
748 748 }
749 749 listclean = (!pyclean) ? false : PyObject_IsTrue(pyclean);
750 750 es = PyString_FromString("");
751 751 if (!es) {
752 752 goto nomem;
753 753 }
754 754 emptyTup = PyTuple_Pack(2, Py_None, es);
755 755 Py_DECREF(es);
756 756 if (!emptyTup) {
757 757 goto nomem;
758 758 }
759 759 ret = PyDict_New();
760 760 if (!ret) {
761 761 goto nomem;
762 762 }
763 763 while (sneedle != self->numlines || oneedle != other->numlines) {
764 764 line *left = self->lines + sneedle;
765 765 line *right = other->lines + oneedle;
766 766 int result;
767 767 PyObject *key;
768 768 PyObject *outer;
769 769 /* If we're looking at a deleted entry and it's not
770 770 * the end of the manifest, just skip it. */
771 771 if (left->deleted && sneedle < self->numlines) {
772 772 sneedle++;
773 773 continue;
774 774 }
775 775 if (right->deleted && oneedle < other->numlines) {
776 776 oneedle++;
777 777 continue;
778 778 }
779 779 /* if we're at the end of either manifest, then we
780 780 * know the remaining items are adds so we can skip
781 781 * the strcmp. */
782 782 if (sneedle == self->numlines) {
783 783 result = 1;
784 784 } else if (oneedle == other->numlines) {
785 785 result = -1;
786 786 } else {
787 787 result = linecmp(left, right);
788 788 }
789 789 key = result <= 0 ?
790 790 PyString_FromString(left->start) :
791 791 PyString_FromString(right->start);
792 792 if (!key)
793 793 goto nomem;
794 794 if (result < 0) {
795 795 PyObject *l = hashflags(left);
796 796 if (!l) {
797 797 goto nomem;
798 798 }
799 799 outer = PyTuple_Pack(2, l, emptyTup);
800 800 Py_DECREF(l);
801 801 if (!outer) {
802 802 goto nomem;
803 803 }
804 804 PyDict_SetItem(ret, key, outer);
805 805 Py_DECREF(outer);
806 806 sneedle++;
807 807 } else if (result > 0) {
808 808 PyObject *r = hashflags(right);
809 809 if (!r) {
810 810 goto nomem;
811 811 }
812 812 outer = PyTuple_Pack(2, emptyTup, r);
813 813 Py_DECREF(r);
814 814 if (!outer) {
815 815 goto nomem;
816 816 }
817 817 PyDict_SetItem(ret, key, outer);
818 818 Py_DECREF(outer);
819 819 oneedle++;
820 820 } else {
821 821 /* file exists in both manifests */
822 822 if (left->len != right->len
823 823 || memcmp(left->start, right->start, left->len)
824 824 || left->hash_suffix != right->hash_suffix) {
825 825 PyObject *l = hashflags(left);
826 826 PyObject *r;
827 827 if (!l) {
828 828 goto nomem;
829 829 }
830 830 r = hashflags(right);
831 831 if (!r) {
832 832 Py_DECREF(l);
833 833 goto nomem;
834 834 }
835 835 outer = PyTuple_Pack(2, l, r);
836 836 Py_DECREF(l);
837 837 Py_DECREF(r);
838 838 if (!outer) {
839 839 goto nomem;
840 840 }
841 841 PyDict_SetItem(ret, key, outer);
842 842 Py_DECREF(outer);
843 843 } else if (listclean) {
844 844 PyDict_SetItem(ret, key, Py_None);
845 845 }
846 846 sneedle++;
847 847 oneedle++;
848 848 }
849 849 Py_DECREF(key);
850 850 }
851 851 Py_DECREF(emptyTup);
852 852 return ret;
853 853 nomem:
854 854 PyErr_NoMemory();
855 855 Py_XDECREF(ret);
856 856 Py_XDECREF(emptyTup);
857 857 return NULL;
858 858 }
859 859
860 860 static PyMethodDef lazymanifest_methods[] = {
861 861 {"iterkeys", (PyCFunction)lazymanifest_getkeysiter, METH_NOARGS,
862 862 "Iterate over file names in this lazymanifest."},
863 863 {"iterentries", (PyCFunction)lazymanifest_getentriesiter, METH_NOARGS,
864 "Iterate over (path, nodeid, flags) typles in this lazymanifest."},
864 "Iterate over (path, nodeid, flags) tuples in this lazymanifest."},
865 865 {"copy", (PyCFunction)lazymanifest_copy, METH_NOARGS,
866 866 "Make a copy of this lazymanifest."},
867 867 {"filtercopy", (PyCFunction)lazymanifest_filtercopy, METH_O,
868 868 "Make a copy of this manifest filtered by matchfn."},
869 869 {"diff", (PyCFunction)lazymanifest_diff, METH_VARARGS,
870 870 "Compare this lazymanifest to another one."},
871 871 {"text", (PyCFunction)lazymanifest_text, METH_NOARGS,
872 872 "Encode this manifest to text."},
873 873 {NULL},
874 874 };
875 875
876 876 static PyTypeObject lazymanifestType = {
877 877 PyObject_HEAD_INIT(NULL)
878 878 0, /* ob_size */
879 879 "parsers.lazymanifest", /* tp_name */
880 880 sizeof(lazymanifest), /* tp_basicsize */
881 881 0, /* tp_itemsize */
882 882 (destructor)lazymanifest_dealloc, /* tp_dealloc */
883 883 0, /* tp_print */
884 884 0, /* tp_getattr */
885 885 0, /* tp_setattr */
886 886 0, /* tp_compare */
887 887 0, /* tp_repr */
888 888 0, /* tp_as_number */
889 889 &lazymanifest_seq_meths, /* tp_as_sequence */
890 890 &lazymanifest_mapping_methods, /* tp_as_mapping */
891 891 0, /* tp_hash */
892 892 0, /* tp_call */
893 893 0, /* tp_str */
894 894 0, /* tp_getattro */
895 895 0, /* tp_setattro */
896 896 0, /* tp_as_buffer */
897 897 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_SEQUENCE_IN, /* tp_flags */
898 898 "TODO(augie)", /* tp_doc */
899 899 0, /* tp_traverse */
900 900 0, /* tp_clear */
901 901 0, /* tp_richcompare */
902 902 0, /* tp_weaklistoffset */
903 903 (getiterfunc)lazymanifest_getkeysiter, /* tp_iter */
904 904 0, /* tp_iternext */
905 905 lazymanifest_methods, /* tp_methods */
906 906 0, /* tp_members */
907 907 0, /* tp_getset */
908 908 0, /* tp_base */
909 909 0, /* tp_dict */
910 910 0, /* tp_descr_get */
911 911 0, /* tp_descr_set */
912 912 0, /* tp_dictoffset */
913 913 (initproc)lazymanifest_init, /* tp_init */
914 914 0, /* tp_alloc */
915 915 };
916 916
917 917 void manifest_module_init(PyObject * mod)
918 918 {
919 919 lazymanifestType.tp_new = PyType_GenericNew;
920 920 if (PyType_Ready(&lazymanifestType) < 0)
921 921 return;
922 922 Py_INCREF(&lazymanifestType);
923 923
924 924 PyModule_AddObject(mod, "lazymanifest",
925 925 (PyObject *)&lazymanifestType);
926 926 }
General Comments 0
You need to be logged in to leave comments. Login now