##// END OF EJS Templates
dirstate-item: fix Cext declaration of dm_nonnormal and dm_otherparent...
marmoute -
r48702:37bffc45 default
parent child Browse files
Show More
@@ -1,1006 +1,1004 b''
1 1 /*
2 2 parsers.c - efficient content parsing
3 3
4 4 Copyright 2008 Olivia Mackall <olivia@selenic.com> and others
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
10 10 #define PY_SSIZE_T_CLEAN
11 11 #include <Python.h>
12 12 #include <ctype.h>
13 13 #include <stddef.h>
14 14 #include <string.h>
15 15
16 16 #include "bitmanipulation.h"
17 17 #include "charencode.h"
18 18 #include "util.h"
19 19
20 20 #ifdef IS_PY3K
21 21 /* The mapping of Python types is meant to be temporary to get Python
22 22 * 3 to compile. We should remove this once Python 3 support is fully
23 23 * supported and proper types are used in the extensions themselves. */
24 24 #define PyInt_Check PyLong_Check
25 25 #define PyInt_FromLong PyLong_FromLong
26 26 #define PyInt_FromSsize_t PyLong_FromSsize_t
27 27 #define PyInt_AsLong PyLong_AsLong
28 28 #endif
29 29
30 30 static const char *const versionerrortext = "Python minor version mismatch";
31 31
32 32 static const int dirstate_v1_from_p2 = -2;
33 33 static const int dirstate_v1_nonnormal = -1;
34 34 static const int ambiguous_time = -1;
35 35
36 36 static PyObject *dict_new_presized(PyObject *self, PyObject *args)
37 37 {
38 38 Py_ssize_t expected_size;
39 39
40 40 if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size)) {
41 41 return NULL;
42 42 }
43 43
44 44 return _dict_new_presized(expected_size);
45 45 }
46 46
47 47 static inline dirstateItemObject *make_dirstate_item(char state, int mode,
48 48 int size, int mtime)
49 49 {
50 50 dirstateItemObject *t =
51 51 PyObject_New(dirstateItemObject, &dirstateItemType);
52 52 if (!t) {
53 53 return NULL;
54 54 }
55 55 t->state = state;
56 56 t->mode = mode;
57 57 t->size = size;
58 58 t->mtime = mtime;
59 59 return t;
60 60 }
61 61
62 62 static PyObject *dirstate_item_new(PyTypeObject *subtype, PyObject *args,
63 63 PyObject *kwds)
64 64 {
65 65 /* We do all the initialization here and not a tp_init function because
66 66 * dirstate_item is immutable. */
67 67 dirstateItemObject *t;
68 68 char state;
69 69 int size, mode, mtime;
70 70 if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) {
71 71 return NULL;
72 72 }
73 73
74 74 t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1);
75 75 if (!t) {
76 76 return NULL;
77 77 }
78 78 t->state = state;
79 79 t->mode = mode;
80 80 t->size = size;
81 81 t->mtime = mtime;
82 82
83 83 return (PyObject *)t;
84 84 }
85 85
86 86 static void dirstate_item_dealloc(PyObject *o)
87 87 {
88 88 PyObject_Del(o);
89 89 }
90 90
91 91 static Py_ssize_t dirstate_item_length(PyObject *o)
92 92 {
93 93 return 4;
94 94 }
95 95
96 96 static PyObject *dirstate_item_item(PyObject *o, Py_ssize_t i)
97 97 {
98 98 dirstateItemObject *t = (dirstateItemObject *)o;
99 99 switch (i) {
100 100 case 0:
101 101 return PyBytes_FromStringAndSize(&t->state, 1);
102 102 case 1:
103 103 return PyInt_FromLong(t->mode);
104 104 case 2:
105 105 return PyInt_FromLong(t->size);
106 106 case 3:
107 107 return PyInt_FromLong(t->mtime);
108 108 default:
109 109 PyErr_SetString(PyExc_IndexError, "index out of range");
110 110 return NULL;
111 111 }
112 112 }
113 113
114 114 static PySequenceMethods dirstate_item_sq = {
115 115 dirstate_item_length, /* sq_length */
116 116 0, /* sq_concat */
117 117 0, /* sq_repeat */
118 118 dirstate_item_item, /* sq_item */
119 119 0, /* sq_ass_item */
120 120 0, /* sq_contains */
121 121 0, /* sq_inplace_concat */
122 122 0 /* sq_inplace_repeat */
123 123 };
124 124
125 125 static PyObject *dirstate_item_v1_state(dirstateItemObject *self)
126 126 {
127 127 return PyBytes_FromStringAndSize(&self->state, 1);
128 128 };
129 129
130 130 static PyObject *dirstate_item_v1_mode(dirstateItemObject *self)
131 131 {
132 132 return PyInt_FromLong(self->mode);
133 133 };
134 134
135 135 static PyObject *dirstate_item_v1_size(dirstateItemObject *self)
136 136 {
137 137 return PyInt_FromLong(self->size);
138 138 };
139 139
140 140 static PyObject *dirstate_item_v1_mtime(dirstateItemObject *self)
141 141 {
142 142 return PyInt_FromLong(self->mtime);
143 143 };
144 144
145 static PyObject *dm_nonnormal(dirstateItemObject *self)
146 {
147 if (self->state != 'n' || self->mtime == ambiguous_time) {
148 Py_RETURN_TRUE;
149 } else {
150 Py_RETURN_FALSE;
151 }
152 };
153 static PyObject *dm_otherparent(dirstateItemObject *self)
154 {
155 if (self->size == dirstate_v1_from_p2) {
156 Py_RETURN_TRUE;
157 } else {
158 Py_RETURN_FALSE;
159 }
160 };
161
162 145 static PyObject *dirstate_item_need_delay(dirstateItemObject *self,
163 146 PyObject *value)
164 147 {
165 148 long now;
166 149 if (!pylong_to_long(value, &now)) {
167 150 return NULL;
168 151 }
169 152 if (self->state == 'n' && self->mtime == now) {
170 153 Py_RETURN_TRUE;
171 154 } else {
172 155 Py_RETURN_FALSE;
173 156 }
174 157 };
175 158
176 159 /* This will never change since it's bound to V1, unlike `make_dirstate_item`
177 160 */
178 161 static inline dirstateItemObject *
179 162 dirstate_item_from_v1_data(char state, int mode, int size, int mtime)
180 163 {
181 164 dirstateItemObject *t =
182 165 PyObject_New(dirstateItemObject, &dirstateItemType);
183 166 if (!t) {
184 167 return NULL;
185 168 }
186 169 t->state = state;
187 170 t->mode = mode;
188 171 t->size = size;
189 172 t->mtime = mtime;
190 173 return t;
191 174 }
192 175
193 176 /* This will never change since it's bound to V1, unlike `dirstate_item_new` */
194 177 static PyObject *dirstate_item_from_v1_meth(PyTypeObject *subtype,
195 178 PyObject *args)
196 179 {
197 180 /* We do all the initialization here and not a tp_init function because
198 181 * dirstate_item is immutable. */
199 182 dirstateItemObject *t;
200 183 char state;
201 184 int size, mode, mtime;
202 185 if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) {
203 186 return NULL;
204 187 }
205 188
206 189 t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1);
207 190 if (!t) {
208 191 return NULL;
209 192 }
210 193 t->state = state;
211 194 t->mode = mode;
212 195 t->size = size;
213 196 t->mtime = mtime;
214 197
215 198 return (PyObject *)t;
216 199 };
217 200
218 201 /* This means the next status call will have to actually check its content
219 202 to make sure it is correct. */
220 203 static PyObject *dirstate_item_set_possibly_dirty(dirstateItemObject *self)
221 204 {
222 205 self->mtime = ambiguous_time;
223 206 Py_RETURN_NONE;
224 207 }
225 208
226 209 static PyObject *dirstate_item_set_untracked(dirstateItemObject *self)
227 210 {
228 211 if (self->state == 'm') {
229 212 self->size = dirstate_v1_nonnormal;
230 213 } else if (self->state == 'n' && self->size == dirstate_v1_from_p2) {
231 214 self->size = dirstate_v1_from_p2;
232 215 } else {
233 216 self->size = 0;
234 217 }
235 218 self->state = 'r';
236 219 self->mode = 0;
237 220 self->mtime = 0;
238 221 Py_RETURN_NONE;
239 222 }
240 223
241 224 static PyMethodDef dirstate_item_methods[] = {
242 225 {"v1_state", (PyCFunction)dirstate_item_v1_state, METH_NOARGS,
243 226 "return a \"state\" suitable for v1 serialization"},
244 227 {"v1_mode", (PyCFunction)dirstate_item_v1_mode, METH_NOARGS,
245 228 "return a \"mode\" suitable for v1 serialization"},
246 229 {"v1_size", (PyCFunction)dirstate_item_v1_size, METH_NOARGS,
247 230 "return a \"size\" suitable for v1 serialization"},
248 231 {"v1_mtime", (PyCFunction)dirstate_item_v1_mtime, METH_NOARGS,
249 232 "return a \"mtime\" suitable for v1 serialization"},
250 233 {"need_delay", (PyCFunction)dirstate_item_need_delay, METH_O,
251 234 "True if the stored mtime would be ambiguous with the current time"},
252 235 {"from_v1_data", (PyCFunction)dirstate_item_from_v1_meth, METH_O,
253 236 "build a new DirstateItem object from V1 data"},
254 237 {"set_possibly_dirty", (PyCFunction)dirstate_item_set_possibly_dirty,
255 238 METH_NOARGS, "mark a file as \"possibly dirty\""},
256 239 {"set_untracked", (PyCFunction)dirstate_item_set_untracked, METH_NOARGS,
257 240 "mark a file as \"untracked\""},
258 {"dm_nonnormal", (PyCFunction)dm_nonnormal, METH_NOARGS,
259 "True is the entry is non-normal in the dirstatemap sense"},
260 {"dm_otherparent", (PyCFunction)dm_otherparent, METH_NOARGS,
261 "True is the entry is `otherparent` in the dirstatemap sense"},
262 241 {NULL} /* Sentinel */
263 242 };
264 243
265 244 static PyObject *dirstate_item_get_mode(dirstateItemObject *self)
266 245 {
267 246 return PyInt_FromLong(self->mode);
268 247 };
269 248
270 249 static PyObject *dirstate_item_get_size(dirstateItemObject *self)
271 250 {
272 251 return PyInt_FromLong(self->size);
273 252 };
274 253
275 254 static PyObject *dirstate_item_get_mtime(dirstateItemObject *self)
276 255 {
277 256 return PyInt_FromLong(self->mtime);
278 257 };
279 258
280 259 static PyObject *dirstate_item_get_state(dirstateItemObject *self)
281 260 {
282 261 return PyBytes_FromStringAndSize(&self->state, 1);
283 262 };
284 263
285 264 static PyObject *dirstate_item_get_tracked(dirstateItemObject *self)
286 265 {
287 266 if (self->state == 'a' || self->state == 'm' || self->state == 'n') {
288 267 Py_RETURN_TRUE;
289 268 } else {
290 269 Py_RETURN_FALSE;
291 270 }
292 271 };
293 272
294 273 static PyObject *dirstate_item_get_added(dirstateItemObject *self)
295 274 {
296 275 if (self->state == 'a') {
297 276 Py_RETURN_TRUE;
298 277 } else {
299 278 Py_RETURN_FALSE;
300 279 }
301 280 };
302 281
303 282 static PyObject *dirstate_item_get_merged(dirstateItemObject *self)
304 283 {
305 284 if (self->state == 'm') {
306 285 Py_RETURN_TRUE;
307 286 } else {
308 287 Py_RETURN_FALSE;
309 288 }
310 289 };
311 290
312 291 static PyObject *dirstate_item_get_merged_removed(dirstateItemObject *self)
313 292 {
314 293 if (self->state == 'r' && self->size == dirstate_v1_nonnormal) {
315 294 Py_RETURN_TRUE;
316 295 } else {
317 296 Py_RETURN_FALSE;
318 297 }
319 298 };
320 299
321 300 static PyObject *dirstate_item_get_from_p2(dirstateItemObject *self)
322 301 {
323 302 if (self->state == 'n' && self->size == dirstate_v1_from_p2) {
324 303 Py_RETURN_TRUE;
325 304 } else {
326 305 Py_RETURN_FALSE;
327 306 }
328 307 };
329 308
330 309 static PyObject *dirstate_item_get_from_p2_removed(dirstateItemObject *self)
331 310 {
332 311 if (self->state == 'r' && self->size == dirstate_v1_from_p2) {
333 312 Py_RETURN_TRUE;
334 313 } else {
335 314 Py_RETURN_FALSE;
336 315 }
337 316 };
338 317
339 318 static PyObject *dirstate_item_get_removed(dirstateItemObject *self)
340 319 {
341 320 if (self->state == 'r') {
342 321 Py_RETURN_TRUE;
343 322 } else {
344 323 Py_RETURN_FALSE;
345 324 }
346 325 };
347 326
327 static PyObject *dm_nonnormal(dirstateItemObject *self)
328 {
329 if (self->state != 'n' || self->mtime == ambiguous_time) {
330 Py_RETURN_TRUE;
331 } else {
332 Py_RETURN_FALSE;
333 }
334 };
335 static PyObject *dm_otherparent(dirstateItemObject *self)
336 {
337 if (self->size == dirstate_v1_from_p2) {
338 Py_RETURN_TRUE;
339 } else {
340 Py_RETURN_FALSE;
341 }
342 };
343
348 344 static PyGetSetDef dirstate_item_getset[] = {
349 345 {"mode", (getter)dirstate_item_get_mode, NULL, "mode", NULL},
350 346 {"size", (getter)dirstate_item_get_size, NULL, "size", NULL},
351 347 {"mtime", (getter)dirstate_item_get_mtime, NULL, "mtime", NULL},
352 348 {"state", (getter)dirstate_item_get_state, NULL, "state", NULL},
353 349 {"tracked", (getter)dirstate_item_get_tracked, NULL, "tracked", NULL},
354 350 {"added", (getter)dirstate_item_get_added, NULL, "added", NULL},
355 351 {"merged_removed", (getter)dirstate_item_get_merged_removed, NULL,
356 352 "merged_removed", NULL},
357 353 {"merged", (getter)dirstate_item_get_merged, NULL, "merged", NULL},
358 354 {"from_p2_removed", (getter)dirstate_item_get_from_p2_removed, NULL,
359 355 "from_p2_removed", NULL},
360 356 {"from_p2", (getter)dirstate_item_get_from_p2, NULL, "from_p2", NULL},
361 357 {"removed", (getter)dirstate_item_get_removed, NULL, "removed", NULL},
358 {"dm_nonnormal", (getter)dm_nonnormal, NULL, "dm_nonnormal", NULL},
359 {"dm_otherparent", (getter)dm_otherparent, NULL, "dm_otherparent", NULL},
362 360 {NULL} /* Sentinel */
363 361 };
364 362
365 363 PyTypeObject dirstateItemType = {
366 364 PyVarObject_HEAD_INIT(NULL, 0) /* header */
367 365 "dirstate_tuple", /* tp_name */
368 366 sizeof(dirstateItemObject), /* tp_basicsize */
369 367 0, /* tp_itemsize */
370 368 (destructor)dirstate_item_dealloc, /* tp_dealloc */
371 369 0, /* tp_print */
372 370 0, /* tp_getattr */
373 371 0, /* tp_setattr */
374 372 0, /* tp_compare */
375 373 0, /* tp_repr */
376 374 0, /* tp_as_number */
377 375 &dirstate_item_sq, /* tp_as_sequence */
378 376 0, /* tp_as_mapping */
379 377 0, /* tp_hash */
380 378 0, /* tp_call */
381 379 0, /* tp_str */
382 380 0, /* tp_getattro */
383 381 0, /* tp_setattro */
384 382 0, /* tp_as_buffer */
385 383 Py_TPFLAGS_DEFAULT, /* tp_flags */
386 384 "dirstate tuple", /* tp_doc */
387 385 0, /* tp_traverse */
388 386 0, /* tp_clear */
389 387 0, /* tp_richcompare */
390 388 0, /* tp_weaklistoffset */
391 389 0, /* tp_iter */
392 390 0, /* tp_iternext */
393 391 dirstate_item_methods, /* tp_methods */
394 392 0, /* tp_members */
395 393 dirstate_item_getset, /* tp_getset */
396 394 0, /* tp_base */
397 395 0, /* tp_dict */
398 396 0, /* tp_descr_get */
399 397 0, /* tp_descr_set */
400 398 0, /* tp_dictoffset */
401 399 0, /* tp_init */
402 400 0, /* tp_alloc */
403 401 dirstate_item_new, /* tp_new */
404 402 };
405 403
406 404 static PyObject *parse_dirstate(PyObject *self, PyObject *args)
407 405 {
408 406 PyObject *dmap, *cmap, *parents = NULL, *ret = NULL;
409 407 PyObject *fname = NULL, *cname = NULL, *entry = NULL;
410 408 char state, *cur, *str, *cpos;
411 409 int mode, size, mtime;
412 410 unsigned int flen, pos = 40;
413 411 Py_ssize_t len = 40;
414 412 Py_ssize_t readlen;
415 413
416 414 if (!PyArg_ParseTuple(
417 415 args, PY23("O!O!s#:parse_dirstate", "O!O!y#:parse_dirstate"),
418 416 &PyDict_Type, &dmap, &PyDict_Type, &cmap, &str, &readlen)) {
419 417 goto quit;
420 418 }
421 419
422 420 len = readlen;
423 421
424 422 /* read parents */
425 423 if (len < 40) {
426 424 PyErr_SetString(PyExc_ValueError,
427 425 "too little data for parents");
428 426 goto quit;
429 427 }
430 428
431 429 parents = Py_BuildValue(PY23("s#s#", "y#y#"), str, (Py_ssize_t)20,
432 430 str + 20, (Py_ssize_t)20);
433 431 if (!parents) {
434 432 goto quit;
435 433 }
436 434
437 435 /* read filenames */
438 436 while (pos >= 40 && pos < len) {
439 437 if (pos + 17 > len) {
440 438 PyErr_SetString(PyExc_ValueError,
441 439 "overflow in dirstate");
442 440 goto quit;
443 441 }
444 442 cur = str + pos;
445 443 /* unpack header */
446 444 state = *cur;
447 445 mode = getbe32(cur + 1);
448 446 size = getbe32(cur + 5);
449 447 mtime = getbe32(cur + 9);
450 448 flen = getbe32(cur + 13);
451 449 pos += 17;
452 450 cur += 17;
453 451 if (flen > len - pos) {
454 452 PyErr_SetString(PyExc_ValueError,
455 453 "overflow in dirstate");
456 454 goto quit;
457 455 }
458 456
459 457 entry = (PyObject *)dirstate_item_from_v1_data(state, mode,
460 458 size, mtime);
461 459 cpos = memchr(cur, 0, flen);
462 460 if (cpos) {
463 461 fname = PyBytes_FromStringAndSize(cur, cpos - cur);
464 462 cname = PyBytes_FromStringAndSize(
465 463 cpos + 1, flen - (cpos - cur) - 1);
466 464 if (!fname || !cname ||
467 465 PyDict_SetItem(cmap, fname, cname) == -1 ||
468 466 PyDict_SetItem(dmap, fname, entry) == -1) {
469 467 goto quit;
470 468 }
471 469 Py_DECREF(cname);
472 470 } else {
473 471 fname = PyBytes_FromStringAndSize(cur, flen);
474 472 if (!fname ||
475 473 PyDict_SetItem(dmap, fname, entry) == -1) {
476 474 goto quit;
477 475 }
478 476 }
479 477 Py_DECREF(fname);
480 478 Py_DECREF(entry);
481 479 fname = cname = entry = NULL;
482 480 pos += flen;
483 481 }
484 482
485 483 ret = parents;
486 484 Py_INCREF(ret);
487 485 quit:
488 486 Py_XDECREF(fname);
489 487 Py_XDECREF(cname);
490 488 Py_XDECREF(entry);
491 489 Py_XDECREF(parents);
492 490 return ret;
493 491 }
494 492
495 493 /*
496 494 * Build a set of non-normal and other parent entries from the dirstate dmap
497 495 */
498 496 static PyObject *nonnormalotherparententries(PyObject *self, PyObject *args)
499 497 {
500 498 PyObject *dmap, *fname, *v;
501 499 PyObject *nonnset = NULL, *otherpset = NULL, *result = NULL;
502 500 Py_ssize_t pos;
503 501
504 502 if (!PyArg_ParseTuple(args, "O!:nonnormalentries", &PyDict_Type,
505 503 &dmap)) {
506 504 goto bail;
507 505 }
508 506
509 507 nonnset = PySet_New(NULL);
510 508 if (nonnset == NULL) {
511 509 goto bail;
512 510 }
513 511
514 512 otherpset = PySet_New(NULL);
515 513 if (otherpset == NULL) {
516 514 goto bail;
517 515 }
518 516
519 517 pos = 0;
520 518 while (PyDict_Next(dmap, &pos, &fname, &v)) {
521 519 dirstateItemObject *t;
522 520 if (!dirstate_tuple_check(v)) {
523 521 PyErr_SetString(PyExc_TypeError,
524 522 "expected a dirstate tuple");
525 523 goto bail;
526 524 }
527 525 t = (dirstateItemObject *)v;
528 526
529 527 if (t->state == 'n' && t->size == -2) {
530 528 if (PySet_Add(otherpset, fname) == -1) {
531 529 goto bail;
532 530 }
533 531 }
534 532
535 533 if (t->state == 'n' && t->mtime != -1) {
536 534 continue;
537 535 }
538 536 if (PySet_Add(nonnset, fname) == -1) {
539 537 goto bail;
540 538 }
541 539 }
542 540
543 541 result = Py_BuildValue("(OO)", nonnset, otherpset);
544 542 if (result == NULL) {
545 543 goto bail;
546 544 }
547 545 Py_DECREF(nonnset);
548 546 Py_DECREF(otherpset);
549 547 return result;
550 548 bail:
551 549 Py_XDECREF(nonnset);
552 550 Py_XDECREF(otherpset);
553 551 Py_XDECREF(result);
554 552 return NULL;
555 553 }
556 554
557 555 /*
558 556 * Efficiently pack a dirstate object into its on-disk format.
559 557 */
560 558 static PyObject *pack_dirstate(PyObject *self, PyObject *args)
561 559 {
562 560 PyObject *packobj = NULL;
563 561 PyObject *map, *copymap, *pl, *mtime_unset = NULL;
564 562 Py_ssize_t nbytes, pos, l;
565 563 PyObject *k, *v = NULL, *pn;
566 564 char *p, *s;
567 565 int now;
568 566
569 567 if (!PyArg_ParseTuple(args, "O!O!O!i:pack_dirstate", &PyDict_Type, &map,
570 568 &PyDict_Type, &copymap, &PyTuple_Type, &pl,
571 569 &now)) {
572 570 return NULL;
573 571 }
574 572
575 573 if (PyTuple_Size(pl) != 2) {
576 574 PyErr_SetString(PyExc_TypeError, "expected 2-element tuple");
577 575 return NULL;
578 576 }
579 577
580 578 /* Figure out how much we need to allocate. */
581 579 for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
582 580 PyObject *c;
583 581 if (!PyBytes_Check(k)) {
584 582 PyErr_SetString(PyExc_TypeError, "expected string key");
585 583 goto bail;
586 584 }
587 585 nbytes += PyBytes_GET_SIZE(k) + 17;
588 586 c = PyDict_GetItem(copymap, k);
589 587 if (c) {
590 588 if (!PyBytes_Check(c)) {
591 589 PyErr_SetString(PyExc_TypeError,
592 590 "expected string key");
593 591 goto bail;
594 592 }
595 593 nbytes += PyBytes_GET_SIZE(c) + 1;
596 594 }
597 595 }
598 596
599 597 packobj = PyBytes_FromStringAndSize(NULL, nbytes);
600 598 if (packobj == NULL) {
601 599 goto bail;
602 600 }
603 601
604 602 p = PyBytes_AS_STRING(packobj);
605 603
606 604 pn = PyTuple_GET_ITEM(pl, 0);
607 605 if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
608 606 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
609 607 goto bail;
610 608 }
611 609 memcpy(p, s, l);
612 610 p += 20;
613 611 pn = PyTuple_GET_ITEM(pl, 1);
614 612 if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
615 613 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
616 614 goto bail;
617 615 }
618 616 memcpy(p, s, l);
619 617 p += 20;
620 618
621 619 for (pos = 0; PyDict_Next(map, &pos, &k, &v);) {
622 620 dirstateItemObject *tuple;
623 621 char state;
624 622 int mode, size, mtime;
625 623 Py_ssize_t len, l;
626 624 PyObject *o;
627 625 char *t;
628 626
629 627 if (!dirstate_tuple_check(v)) {
630 628 PyErr_SetString(PyExc_TypeError,
631 629 "expected a dirstate tuple");
632 630 goto bail;
633 631 }
634 632 tuple = (dirstateItemObject *)v;
635 633
636 634 state = tuple->state;
637 635 mode = tuple->mode;
638 636 size = tuple->size;
639 637 mtime = tuple->mtime;
640 638 if (state == 'n' && mtime == now) {
641 639 /* See pure/parsers.py:pack_dirstate for why we do
642 640 * this. */
643 641 mtime = -1;
644 642 mtime_unset = (PyObject *)make_dirstate_item(
645 643 state, mode, size, mtime);
646 644 if (!mtime_unset) {
647 645 goto bail;
648 646 }
649 647 if (PyDict_SetItem(map, k, mtime_unset) == -1) {
650 648 goto bail;
651 649 }
652 650 Py_DECREF(mtime_unset);
653 651 mtime_unset = NULL;
654 652 }
655 653 *p++ = state;
656 654 putbe32((uint32_t)mode, p);
657 655 putbe32((uint32_t)size, p + 4);
658 656 putbe32((uint32_t)mtime, p + 8);
659 657 t = p + 12;
660 658 p += 16;
661 659 len = PyBytes_GET_SIZE(k);
662 660 memcpy(p, PyBytes_AS_STRING(k), len);
663 661 p += len;
664 662 o = PyDict_GetItem(copymap, k);
665 663 if (o) {
666 664 *p++ = '\0';
667 665 l = PyBytes_GET_SIZE(o);
668 666 memcpy(p, PyBytes_AS_STRING(o), l);
669 667 p += l;
670 668 len += l + 1;
671 669 }
672 670 putbe32((uint32_t)len, t);
673 671 }
674 672
675 673 pos = p - PyBytes_AS_STRING(packobj);
676 674 if (pos != nbytes) {
677 675 PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
678 676 (long)pos, (long)nbytes);
679 677 goto bail;
680 678 }
681 679
682 680 return packobj;
683 681 bail:
684 682 Py_XDECREF(mtime_unset);
685 683 Py_XDECREF(packobj);
686 684 Py_XDECREF(v);
687 685 return NULL;
688 686 }
689 687
690 688 #define BUMPED_FIX 1
691 689 #define USING_SHA_256 2
692 690 #define FM1_HEADER_SIZE (4 + 8 + 2 + 2 + 1 + 1 + 1)
693 691
694 692 static PyObject *readshas(const char *source, unsigned char num,
695 693 Py_ssize_t hashwidth)
696 694 {
697 695 int i;
698 696 PyObject *list = PyTuple_New(num);
699 697 if (list == NULL) {
700 698 return NULL;
701 699 }
702 700 for (i = 0; i < num; i++) {
703 701 PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth);
704 702 if (hash == NULL) {
705 703 Py_DECREF(list);
706 704 return NULL;
707 705 }
708 706 PyTuple_SET_ITEM(list, i, hash);
709 707 source += hashwidth;
710 708 }
711 709 return list;
712 710 }
713 711
714 712 static PyObject *fm1readmarker(const char *databegin, const char *dataend,
715 713 uint32_t *msize)
716 714 {
717 715 const char *data = databegin;
718 716 const char *meta;
719 717
720 718 double mtime;
721 719 int16_t tz;
722 720 uint16_t flags;
723 721 unsigned char nsuccs, nparents, nmetadata;
724 722 Py_ssize_t hashwidth = 20;
725 723
726 724 PyObject *prec = NULL, *parents = NULL, *succs = NULL;
727 725 PyObject *metadata = NULL, *ret = NULL;
728 726 int i;
729 727
730 728 if (data + FM1_HEADER_SIZE > dataend) {
731 729 goto overflow;
732 730 }
733 731
734 732 *msize = getbe32(data);
735 733 data += 4;
736 734 mtime = getbefloat64(data);
737 735 data += 8;
738 736 tz = getbeint16(data);
739 737 data += 2;
740 738 flags = getbeuint16(data);
741 739 data += 2;
742 740
743 741 if (flags & USING_SHA_256) {
744 742 hashwidth = 32;
745 743 }
746 744
747 745 nsuccs = (unsigned char)(*data++);
748 746 nparents = (unsigned char)(*data++);
749 747 nmetadata = (unsigned char)(*data++);
750 748
751 749 if (databegin + *msize > dataend) {
752 750 goto overflow;
753 751 }
754 752 dataend = databegin + *msize; /* narrow down to marker size */
755 753
756 754 if (data + hashwidth > dataend) {
757 755 goto overflow;
758 756 }
759 757 prec = PyBytes_FromStringAndSize(data, hashwidth);
760 758 data += hashwidth;
761 759 if (prec == NULL) {
762 760 goto bail;
763 761 }
764 762
765 763 if (data + nsuccs * hashwidth > dataend) {
766 764 goto overflow;
767 765 }
768 766 succs = readshas(data, nsuccs, hashwidth);
769 767 if (succs == NULL) {
770 768 goto bail;
771 769 }
772 770 data += nsuccs * hashwidth;
773 771
774 772 if (nparents == 1 || nparents == 2) {
775 773 if (data + nparents * hashwidth > dataend) {
776 774 goto overflow;
777 775 }
778 776 parents = readshas(data, nparents, hashwidth);
779 777 if (parents == NULL) {
780 778 goto bail;
781 779 }
782 780 data += nparents * hashwidth;
783 781 } else {
784 782 parents = Py_None;
785 783 Py_INCREF(parents);
786 784 }
787 785
788 786 if (data + 2 * nmetadata > dataend) {
789 787 goto overflow;
790 788 }
791 789 meta = data + (2 * nmetadata);
792 790 metadata = PyTuple_New(nmetadata);
793 791 if (metadata == NULL) {
794 792 goto bail;
795 793 }
796 794 for (i = 0; i < nmetadata; i++) {
797 795 PyObject *tmp, *left = NULL, *right = NULL;
798 796 Py_ssize_t leftsize = (unsigned char)(*data++);
799 797 Py_ssize_t rightsize = (unsigned char)(*data++);
800 798 if (meta + leftsize + rightsize > dataend) {
801 799 goto overflow;
802 800 }
803 801 left = PyBytes_FromStringAndSize(meta, leftsize);
804 802 meta += leftsize;
805 803 right = PyBytes_FromStringAndSize(meta, rightsize);
806 804 meta += rightsize;
807 805 tmp = PyTuple_New(2);
808 806 if (!left || !right || !tmp) {
809 807 Py_XDECREF(left);
810 808 Py_XDECREF(right);
811 809 Py_XDECREF(tmp);
812 810 goto bail;
813 811 }
814 812 PyTuple_SET_ITEM(tmp, 0, left);
815 813 PyTuple_SET_ITEM(tmp, 1, right);
816 814 PyTuple_SET_ITEM(metadata, i, tmp);
817 815 }
818 816 ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, metadata, mtime,
819 817 (int)tz * 60, parents);
820 818 goto bail; /* return successfully */
821 819
822 820 overflow:
823 821 PyErr_SetString(PyExc_ValueError, "overflow in obsstore");
824 822 bail:
825 823 Py_XDECREF(prec);
826 824 Py_XDECREF(succs);
827 825 Py_XDECREF(metadata);
828 826 Py_XDECREF(parents);
829 827 return ret;
830 828 }
831 829
832 830 static PyObject *fm1readmarkers(PyObject *self, PyObject *args)
833 831 {
834 832 const char *data, *dataend;
835 833 Py_ssize_t datalen, offset, stop;
836 834 PyObject *markers = NULL;
837 835
838 836 if (!PyArg_ParseTuple(args, PY23("s#nn", "y#nn"), &data, &datalen,
839 837 &offset, &stop)) {
840 838 return NULL;
841 839 }
842 840 if (offset < 0) {
843 841 PyErr_SetString(PyExc_ValueError,
844 842 "invalid negative offset in fm1readmarkers");
845 843 return NULL;
846 844 }
847 845 if (stop > datalen) {
848 846 PyErr_SetString(
849 847 PyExc_ValueError,
850 848 "stop longer than data length in fm1readmarkers");
851 849 return NULL;
852 850 }
853 851 dataend = data + datalen;
854 852 data += offset;
855 853 markers = PyList_New(0);
856 854 if (!markers) {
857 855 return NULL;
858 856 }
859 857 while (offset < stop) {
860 858 uint32_t msize;
861 859 int error;
862 860 PyObject *record = fm1readmarker(data, dataend, &msize);
863 861 if (!record) {
864 862 goto bail;
865 863 }
866 864 error = PyList_Append(markers, record);
867 865 Py_DECREF(record);
868 866 if (error) {
869 867 goto bail;
870 868 }
871 869 data += msize;
872 870 offset += msize;
873 871 }
874 872 return markers;
875 873 bail:
876 874 Py_DECREF(markers);
877 875 return NULL;
878 876 }
879 877
880 878 static char parsers_doc[] = "Efficient content parsing.";
881 879
882 880 PyObject *encodedir(PyObject *self, PyObject *args);
883 881 PyObject *pathencode(PyObject *self, PyObject *args);
884 882 PyObject *lowerencode(PyObject *self, PyObject *args);
885 883 PyObject *parse_index2(PyObject *self, PyObject *args, PyObject *kwargs);
886 884
887 885 static PyMethodDef methods[] = {
888 886 {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
889 887 {"nonnormalotherparententries", nonnormalotherparententries, METH_VARARGS,
890 888 "create a set containing non-normal and other parent entries of given "
891 889 "dirstate\n"},
892 890 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
893 891 {"parse_index2", (PyCFunction)parse_index2, METH_VARARGS | METH_KEYWORDS,
894 892 "parse a revlog index\n"},
895 893 {"isasciistr", isasciistr, METH_VARARGS, "check if an ASCII string\n"},
896 894 {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"},
897 895 {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"},
898 896 {"dict_new_presized", dict_new_presized, METH_VARARGS,
899 897 "construct a dict with an expected size\n"},
900 898 {"make_file_foldmap", make_file_foldmap, METH_VARARGS,
901 899 "make file foldmap\n"},
902 900 {"jsonescapeu8fast", jsonescapeu8fast, METH_VARARGS,
903 901 "escape a UTF-8 byte string to JSON (fast path)\n"},
904 902 {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
905 903 {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
906 904 {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"},
907 905 {"fm1readmarkers", fm1readmarkers, METH_VARARGS,
908 906 "parse v1 obsolete markers\n"},
909 907 {NULL, NULL}};
910 908
911 909 void dirs_module_init(PyObject *mod);
912 910 void manifest_module_init(PyObject *mod);
913 911 void revlog_module_init(PyObject *mod);
914 912
915 913 static const int version = 20;
916 914
917 915 static void module_init(PyObject *mod)
918 916 {
919 917 PyObject *capsule = NULL;
920 918 PyModule_AddIntConstant(mod, "version", version);
921 919
922 920 /* This module constant has two purposes. First, it lets us unit test
923 921 * the ImportError raised without hard-coding any error text. This
924 922 * means we can change the text in the future without breaking tests,
925 923 * even across changesets without a recompile. Second, its presence
926 924 * can be used to determine whether the version-checking logic is
927 925 * present, which also helps in testing across changesets without a
928 926 * recompile. Note that this means the pure-Python version of parsers
929 927 * should not have this module constant. */
930 928 PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext);
931 929
932 930 dirs_module_init(mod);
933 931 manifest_module_init(mod);
934 932 revlog_module_init(mod);
935 933
936 934 capsule = PyCapsule_New(
937 935 make_dirstate_item,
938 936 "mercurial.cext.parsers.make_dirstate_item_CAPI", NULL);
939 937 if (capsule != NULL)
940 938 PyModule_AddObject(mod, "make_dirstate_item_CAPI", capsule);
941 939
942 940 if (PyType_Ready(&dirstateItemType) < 0) {
943 941 return;
944 942 }
945 943 Py_INCREF(&dirstateItemType);
946 944 PyModule_AddObject(mod, "DirstateItem", (PyObject *)&dirstateItemType);
947 945 }
948 946
949 947 static int check_python_version(void)
950 948 {
951 949 PyObject *sys = PyImport_ImportModule("sys"), *ver;
952 950 long hexversion;
953 951 if (!sys) {
954 952 return -1;
955 953 }
956 954 ver = PyObject_GetAttrString(sys, "hexversion");
957 955 Py_DECREF(sys);
958 956 if (!ver) {
959 957 return -1;
960 958 }
961 959 hexversion = PyInt_AsLong(ver);
962 960 Py_DECREF(ver);
963 961 /* sys.hexversion is a 32-bit number by default, so the -1 case
964 962 * should only occur in unusual circumstances (e.g. if sys.hexversion
965 963 * is manually set to an invalid value). */
966 964 if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) {
967 965 PyErr_Format(PyExc_ImportError,
968 966 "%s: The Mercurial extension "
969 967 "modules were compiled with Python " PY_VERSION
970 968 ", but "
971 969 "Mercurial is currently using Python with "
972 970 "sys.hexversion=%ld: "
973 971 "Python %s\n at: %s",
974 972 versionerrortext, hexversion, Py_GetVersion(),
975 973 Py_GetProgramFullPath());
976 974 return -1;
977 975 }
978 976 return 0;
979 977 }
980 978
981 979 #ifdef IS_PY3K
982 980 static struct PyModuleDef parsers_module = {PyModuleDef_HEAD_INIT, "parsers",
983 981 parsers_doc, -1, methods};
984 982
985 983 PyMODINIT_FUNC PyInit_parsers(void)
986 984 {
987 985 PyObject *mod;
988 986
989 987 if (check_python_version() == -1)
990 988 return NULL;
991 989 mod = PyModule_Create(&parsers_module);
992 990 module_init(mod);
993 991 return mod;
994 992 }
995 993 #else
996 994 PyMODINIT_FUNC initparsers(void)
997 995 {
998 996 PyObject *mod;
999 997
1000 998 if (check_python_version() == -1) {
1001 999 return;
1002 1000 }
1003 1001 mod = Py_InitModule3("parsers", methods, parsers_doc);
1004 1002 module_init(mod);
1005 1003 }
1006 1004 #endif
General Comments 0
You need to be logged in to leave comments. Login now