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