##// END OF EJS Templates
cext: define S_IFLNK on Python 2.7 and Windows...
Gregory Szorc -
r49149:3620ab28 stable
parent child Browse files
Show More
@@ -1,1331 +1,1337 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 #else
29 /* Windows on Python 2.7 doesn't define S_IFLNK. Python 3+ defines via
30 * pyport.h. */
31 #ifndef S_IFLNK
32 #define S_IFLNK 0120000
33 #endif
28 34 #endif
29 35
30 36 static const char *const versionerrortext = "Python minor version mismatch";
31 37
32 38 static const int dirstate_v1_from_p2 = -2;
33 39 static const int dirstate_v1_nonnormal = -1;
34 40 static const int ambiguous_time = -1;
35 41
36 42 static PyObject *dict_new_presized(PyObject *self, PyObject *args)
37 43 {
38 44 Py_ssize_t expected_size;
39 45
40 46 if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size)) {
41 47 return NULL;
42 48 }
43 49
44 50 return _dict_new_presized(expected_size);
45 51 }
46 52
47 53 static PyObject *dirstate_item_new(PyTypeObject *subtype, PyObject *args,
48 54 PyObject *kwds)
49 55 {
50 56 /* We do all the initialization here and not a tp_init function because
51 57 * dirstate_item is immutable. */
52 58 dirstateItemObject *t;
53 59 int wc_tracked;
54 60 int p1_tracked;
55 61 int p2_info;
56 62 int has_meaningful_data;
57 63 int has_meaningful_mtime;
58 64 int mode;
59 65 int size;
60 66 int mtime_s;
61 67 int mtime_ns;
62 68 PyObject *parentfiledata;
63 69 PyObject *fallback_exec;
64 70 PyObject *fallback_symlink;
65 71 static char *keywords_name[] = {
66 72 "wc_tracked", "p1_tracked", "p2_info",
67 73 "has_meaningful_data", "has_meaningful_mtime", "parentfiledata",
68 74 "fallback_exec", "fallback_symlink", NULL,
69 75 };
70 76 wc_tracked = 0;
71 77 p1_tracked = 0;
72 78 p2_info = 0;
73 79 has_meaningful_mtime = 1;
74 80 has_meaningful_data = 1;
75 81 parentfiledata = Py_None;
76 82 fallback_exec = Py_None;
77 83 fallback_symlink = Py_None;
78 84 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iiiiiOOO", keywords_name,
79 85 &wc_tracked, &p1_tracked, &p2_info,
80 86 &has_meaningful_data,
81 87 &has_meaningful_mtime, &parentfiledata,
82 88 &fallback_exec, &fallback_symlink)) {
83 89 return NULL;
84 90 }
85 91 t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1);
86 92 if (!t) {
87 93 return NULL;
88 94 }
89 95
90 96 t->flags = 0;
91 97 if (wc_tracked) {
92 98 t->flags |= dirstate_flag_wc_tracked;
93 99 }
94 100 if (p1_tracked) {
95 101 t->flags |= dirstate_flag_p1_tracked;
96 102 }
97 103 if (p2_info) {
98 104 t->flags |= dirstate_flag_p2_info;
99 105 }
100 106
101 107 if (fallback_exec != Py_None) {
102 108 t->flags |= dirstate_flag_has_fallback_exec;
103 109 if (PyObject_IsTrue(fallback_exec)) {
104 110 t->flags |= dirstate_flag_fallback_exec;
105 111 }
106 112 }
107 113 if (fallback_symlink != Py_None) {
108 114 t->flags |= dirstate_flag_has_fallback_symlink;
109 115 if (PyObject_IsTrue(fallback_symlink)) {
110 116 t->flags |= dirstate_flag_fallback_symlink;
111 117 }
112 118 }
113 119
114 120 if (parentfiledata != Py_None) {
115 121 if (!PyArg_ParseTuple(parentfiledata, "ii(ii)", &mode, &size,
116 122 &mtime_s, &mtime_ns)) {
117 123 return NULL;
118 124 }
119 125 } else {
120 126 has_meaningful_data = 0;
121 127 has_meaningful_mtime = 0;
122 128 }
123 129 if (has_meaningful_data) {
124 130 t->flags |= dirstate_flag_has_meaningful_data;
125 131 t->mode = mode;
126 132 t->size = size;
127 133 } else {
128 134 t->mode = 0;
129 135 t->size = 0;
130 136 }
131 137 if (has_meaningful_mtime) {
132 138 t->flags |= dirstate_flag_has_mtime;
133 139 t->mtime_s = mtime_s;
134 140 t->mtime_ns = mtime_ns;
135 141 } else {
136 142 t->mtime_s = 0;
137 143 t->mtime_ns = 0;
138 144 }
139 145 return (PyObject *)t;
140 146 }
141 147
142 148 static void dirstate_item_dealloc(PyObject *o)
143 149 {
144 150 PyObject_Del(o);
145 151 }
146 152
147 153 static inline bool dirstate_item_c_tracked(dirstateItemObject *self)
148 154 {
149 155 return (self->flags & dirstate_flag_wc_tracked);
150 156 }
151 157
152 158 static inline bool dirstate_item_c_any_tracked(dirstateItemObject *self)
153 159 {
154 160 const int mask = dirstate_flag_wc_tracked | dirstate_flag_p1_tracked |
155 161 dirstate_flag_p2_info;
156 162 return (self->flags & mask);
157 163 }
158 164
159 165 static inline bool dirstate_item_c_added(dirstateItemObject *self)
160 166 {
161 167 const int mask = (dirstate_flag_wc_tracked | dirstate_flag_p1_tracked |
162 168 dirstate_flag_p2_info);
163 169 const int target = dirstate_flag_wc_tracked;
164 170 return (self->flags & mask) == target;
165 171 }
166 172
167 173 static inline bool dirstate_item_c_removed(dirstateItemObject *self)
168 174 {
169 175 if (self->flags & dirstate_flag_wc_tracked) {
170 176 return false;
171 177 }
172 178 return (self->flags &
173 179 (dirstate_flag_p1_tracked | dirstate_flag_p2_info));
174 180 }
175 181
176 182 static inline bool dirstate_item_c_merged(dirstateItemObject *self)
177 183 {
178 184 return ((self->flags & dirstate_flag_wc_tracked) &&
179 185 (self->flags & dirstate_flag_p1_tracked) &&
180 186 (self->flags & dirstate_flag_p2_info));
181 187 }
182 188
183 189 static inline bool dirstate_item_c_from_p2(dirstateItemObject *self)
184 190 {
185 191 return ((self->flags & dirstate_flag_wc_tracked) &&
186 192 !(self->flags & dirstate_flag_p1_tracked) &&
187 193 (self->flags & dirstate_flag_p2_info));
188 194 }
189 195
190 196 static inline char dirstate_item_c_v1_state(dirstateItemObject *self)
191 197 {
192 198 if (dirstate_item_c_removed(self)) {
193 199 return 'r';
194 200 } else if (dirstate_item_c_merged(self)) {
195 201 return 'm';
196 202 } else if (dirstate_item_c_added(self)) {
197 203 return 'a';
198 204 } else {
199 205 return 'n';
200 206 }
201 207 }
202 208
203 209 static inline bool dirstate_item_c_has_fallback_exec(dirstateItemObject *self)
204 210 {
205 211 return (bool)self->flags & dirstate_flag_has_fallback_exec;
206 212 }
207 213
208 214 static inline bool
209 215 dirstate_item_c_has_fallback_symlink(dirstateItemObject *self)
210 216 {
211 217 return (bool)self->flags & dirstate_flag_has_fallback_symlink;
212 218 }
213 219
214 220 static inline int dirstate_item_c_v1_mode(dirstateItemObject *self)
215 221 {
216 222 if (self->flags & dirstate_flag_has_meaningful_data) {
217 223 return self->mode;
218 224 } else {
219 225 return 0;
220 226 }
221 227 }
222 228
223 229 static inline int dirstate_item_c_v1_size(dirstateItemObject *self)
224 230 {
225 231 if (!(self->flags & dirstate_flag_wc_tracked) &&
226 232 (self->flags & dirstate_flag_p2_info)) {
227 233 if (self->flags & dirstate_flag_p1_tracked) {
228 234 return dirstate_v1_nonnormal;
229 235 } else {
230 236 return dirstate_v1_from_p2;
231 237 }
232 238 } else if (dirstate_item_c_removed(self)) {
233 239 return 0;
234 240 } else if (self->flags & dirstate_flag_p2_info) {
235 241 return dirstate_v1_from_p2;
236 242 } else if (dirstate_item_c_added(self)) {
237 243 return dirstate_v1_nonnormal;
238 244 } else if (self->flags & dirstate_flag_has_meaningful_data) {
239 245 return self->size;
240 246 } else {
241 247 return dirstate_v1_nonnormal;
242 248 }
243 249 }
244 250
245 251 static inline int dirstate_item_c_v1_mtime(dirstateItemObject *self)
246 252 {
247 253 if (dirstate_item_c_removed(self)) {
248 254 return 0;
249 255 } else if (!(self->flags & dirstate_flag_has_mtime) ||
250 256 !(self->flags & dirstate_flag_p1_tracked) ||
251 257 !(self->flags & dirstate_flag_wc_tracked) ||
252 258 (self->flags & dirstate_flag_p2_info)) {
253 259 return ambiguous_time;
254 260 } else {
255 261 return self->mtime_s;
256 262 }
257 263 }
258 264
259 265 static PyObject *dirstate_item_v2_data(dirstateItemObject *self)
260 266 {
261 267 int flags = self->flags;
262 268 int mode = dirstate_item_c_v1_mode(self);
263 269 #ifdef S_IXUSR
264 270 /* This is for platforms with an exec bit */
265 271 if ((mode & S_IXUSR) != 0) {
266 272 flags |= dirstate_flag_mode_exec_perm;
267 273 } else {
268 274 flags &= ~dirstate_flag_mode_exec_perm;
269 275 }
270 276 #else
271 277 flags &= ~dirstate_flag_mode_exec_perm;
272 278 #endif
273 279 #ifdef S_ISLNK
274 280 /* This is for platforms with support for symlinks */
275 281 if (S_ISLNK(mode)) {
276 282 flags |= dirstate_flag_mode_is_symlink;
277 283 } else {
278 284 flags &= ~dirstate_flag_mode_is_symlink;
279 285 }
280 286 #else
281 287 flags &= ~dirstate_flag_mode_is_symlink;
282 288 #endif
283 289 return Py_BuildValue("iiii", flags, self->size, self->mtime_s,
284 290 self->mtime_ns);
285 291 };
286 292
287 293 static PyObject *dirstate_item_v1_state(dirstateItemObject *self)
288 294 {
289 295 char state = dirstate_item_c_v1_state(self);
290 296 return PyBytes_FromStringAndSize(&state, 1);
291 297 };
292 298
293 299 static PyObject *dirstate_item_v1_mode(dirstateItemObject *self)
294 300 {
295 301 return PyInt_FromLong(dirstate_item_c_v1_mode(self));
296 302 };
297 303
298 304 static PyObject *dirstate_item_v1_size(dirstateItemObject *self)
299 305 {
300 306 return PyInt_FromLong(dirstate_item_c_v1_size(self));
301 307 };
302 308
303 309 static PyObject *dirstate_item_v1_mtime(dirstateItemObject *self)
304 310 {
305 311 return PyInt_FromLong(dirstate_item_c_v1_mtime(self));
306 312 };
307 313
308 314 static PyObject *dirstate_item_need_delay(dirstateItemObject *self,
309 315 PyObject *now)
310 316 {
311 317 int now_s;
312 318 int now_ns;
313 319 if (!PyArg_ParseTuple(now, "ii", &now_s, &now_ns)) {
314 320 return NULL;
315 321 }
316 322 if (dirstate_item_c_v1_state(self) == 'n' && self->mtime_s == now_s) {
317 323 Py_RETURN_TRUE;
318 324 } else {
319 325 Py_RETURN_FALSE;
320 326 }
321 327 };
322 328
323 329 static PyObject *dirstate_item_mtime_likely_equal_to(dirstateItemObject *self,
324 330 PyObject *other)
325 331 {
326 332 int other_s;
327 333 int other_ns;
328 334 if (!PyArg_ParseTuple(other, "ii", &other_s, &other_ns)) {
329 335 return NULL;
330 336 }
331 337 if ((self->flags & dirstate_flag_has_mtime) &&
332 338 self->mtime_s == other_s &&
333 339 (self->mtime_ns == other_ns || self->mtime_ns == 0 ||
334 340 other_ns == 0)) {
335 341 Py_RETURN_TRUE;
336 342 } else {
337 343 Py_RETURN_FALSE;
338 344 }
339 345 };
340 346
341 347 /* This will never change since it's bound to V1
342 348 */
343 349 static inline dirstateItemObject *
344 350 dirstate_item_from_v1_data(char state, int mode, int size, int mtime)
345 351 {
346 352 dirstateItemObject *t =
347 353 PyObject_New(dirstateItemObject, &dirstateItemType);
348 354 if (!t) {
349 355 return NULL;
350 356 }
351 357 t->flags = 0;
352 358 t->mode = 0;
353 359 t->size = 0;
354 360 t->mtime_s = 0;
355 361 t->mtime_ns = 0;
356 362
357 363 if (state == 'm') {
358 364 t->flags = (dirstate_flag_wc_tracked |
359 365 dirstate_flag_p1_tracked | dirstate_flag_p2_info);
360 366 } else if (state == 'a') {
361 367 t->flags = dirstate_flag_wc_tracked;
362 368 } else if (state == 'r') {
363 369 if (size == dirstate_v1_nonnormal) {
364 370 t->flags =
365 371 dirstate_flag_p1_tracked | dirstate_flag_p2_info;
366 372 } else if (size == dirstate_v1_from_p2) {
367 373 t->flags = dirstate_flag_p2_info;
368 374 } else {
369 375 t->flags = dirstate_flag_p1_tracked;
370 376 }
371 377 } else if (state == 'n') {
372 378 if (size == dirstate_v1_from_p2) {
373 379 t->flags =
374 380 dirstate_flag_wc_tracked | dirstate_flag_p2_info;
375 381 } else if (size == dirstate_v1_nonnormal) {
376 382 t->flags =
377 383 dirstate_flag_wc_tracked | dirstate_flag_p1_tracked;
378 384 } else if (mtime == ambiguous_time) {
379 385 t->flags = (dirstate_flag_wc_tracked |
380 386 dirstate_flag_p1_tracked |
381 387 dirstate_flag_has_meaningful_data);
382 388 t->mode = mode;
383 389 t->size = size;
384 390 } else {
385 391 t->flags = (dirstate_flag_wc_tracked |
386 392 dirstate_flag_p1_tracked |
387 393 dirstate_flag_has_meaningful_data |
388 394 dirstate_flag_has_mtime);
389 395 t->mode = mode;
390 396 t->size = size;
391 397 t->mtime_s = mtime;
392 398 }
393 399 } else {
394 400 PyErr_Format(PyExc_RuntimeError,
395 401 "unknown state: `%c` (%d, %d, %d)", state, mode,
396 402 size, mtime, NULL);
397 403 Py_DECREF(t);
398 404 return NULL;
399 405 }
400 406
401 407 return t;
402 408 }
403 409
404 410 /* This will never change since it's bound to V1, unlike `dirstate_item_new` */
405 411 static PyObject *dirstate_item_from_v1_meth(PyTypeObject *subtype,
406 412 PyObject *args)
407 413 {
408 414 /* We do all the initialization here and not a tp_init function because
409 415 * dirstate_item is immutable. */
410 416 char state;
411 417 int size, mode, mtime;
412 418 if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) {
413 419 return NULL;
414 420 }
415 421 return (PyObject *)dirstate_item_from_v1_data(state, mode, size, mtime);
416 422 };
417 423
418 424 static PyObject *dirstate_item_from_v2_meth(PyTypeObject *subtype,
419 425 PyObject *args)
420 426 {
421 427 dirstateItemObject *t =
422 428 PyObject_New(dirstateItemObject, &dirstateItemType);
423 429 if (!t) {
424 430 return NULL;
425 431 }
426 432 if (!PyArg_ParseTuple(args, "iiii", &t->flags, &t->size, &t->mtime_s,
427 433 &t->mtime_ns)) {
428 434 return NULL;
429 435 }
430 436 if (t->flags & dirstate_flag_expected_state_is_modified) {
431 437 t->flags &= ~(dirstate_flag_expected_state_is_modified |
432 438 dirstate_flag_has_meaningful_data |
433 439 dirstate_flag_has_mtime);
434 440 }
435 441 if (t->flags & dirstate_flag_mtime_second_ambiguous) {
436 442 /* The current code is not able to do the more subtle comparison
437 443 * that the MTIME_SECOND_AMBIGUOUS requires. So we ignore the
438 444 * mtime */
439 445 t->flags &= ~(dirstate_flag_mtime_second_ambiguous |
440 446 dirstate_flag_has_meaningful_data |
441 447 dirstate_flag_has_mtime);
442 448 }
443 449 t->mode = 0;
444 450 if (t->flags & dirstate_flag_has_meaningful_data) {
445 451 if (t->flags & dirstate_flag_mode_exec_perm) {
446 452 t->mode = 0755;
447 453 } else {
448 454 t->mode = 0644;
449 455 }
450 456 if (t->flags & dirstate_flag_mode_is_symlink) {
451 457 t->mode |= S_IFLNK;
452 458 } else {
453 459 t->mode |= S_IFREG;
454 460 }
455 461 }
456 462 return (PyObject *)t;
457 463 };
458 464
459 465 /* This means the next status call will have to actually check its content
460 466 to make sure it is correct. */
461 467 static PyObject *dirstate_item_set_possibly_dirty(dirstateItemObject *self)
462 468 {
463 469 self->flags &= ~dirstate_flag_has_mtime;
464 470 Py_RETURN_NONE;
465 471 }
466 472
467 473 /* See docstring of the python implementation for details */
468 474 static PyObject *dirstate_item_set_clean(dirstateItemObject *self,
469 475 PyObject *args)
470 476 {
471 477 int size, mode, mtime_s, mtime_ns;
472 478 if (!PyArg_ParseTuple(args, "ii(ii)", &mode, &size, &mtime_s,
473 479 &mtime_ns)) {
474 480 return NULL;
475 481 }
476 482 self->flags = dirstate_flag_wc_tracked | dirstate_flag_p1_tracked |
477 483 dirstate_flag_has_meaningful_data |
478 484 dirstate_flag_has_mtime;
479 485 self->mode = mode;
480 486 self->size = size;
481 487 self->mtime_s = mtime_s;
482 488 self->mtime_ns = mtime_ns;
483 489 Py_RETURN_NONE;
484 490 }
485 491
486 492 static PyObject *dirstate_item_set_tracked(dirstateItemObject *self)
487 493 {
488 494 self->flags |= dirstate_flag_wc_tracked;
489 495 self->flags &= ~dirstate_flag_has_mtime;
490 496 Py_RETURN_NONE;
491 497 }
492 498
493 499 static PyObject *dirstate_item_set_untracked(dirstateItemObject *self)
494 500 {
495 501 self->flags &= ~dirstate_flag_wc_tracked;
496 502 self->mode = 0;
497 503 self->size = 0;
498 504 self->mtime_s = 0;
499 505 self->mtime_ns = 0;
500 506 Py_RETURN_NONE;
501 507 }
502 508
503 509 static PyObject *dirstate_item_drop_merge_data(dirstateItemObject *self)
504 510 {
505 511 if (self->flags & dirstate_flag_p2_info) {
506 512 self->flags &= ~(dirstate_flag_p2_info |
507 513 dirstate_flag_has_meaningful_data |
508 514 dirstate_flag_has_mtime);
509 515 self->mode = 0;
510 516 self->size = 0;
511 517 self->mtime_s = 0;
512 518 self->mtime_ns = 0;
513 519 }
514 520 Py_RETURN_NONE;
515 521 }
516 522 static PyMethodDef dirstate_item_methods[] = {
517 523 {"v2_data", (PyCFunction)dirstate_item_v2_data, METH_NOARGS,
518 524 "return data suitable for v2 serialization"},
519 525 {"v1_state", (PyCFunction)dirstate_item_v1_state, METH_NOARGS,
520 526 "return a \"state\" suitable for v1 serialization"},
521 527 {"v1_mode", (PyCFunction)dirstate_item_v1_mode, METH_NOARGS,
522 528 "return a \"mode\" suitable for v1 serialization"},
523 529 {"v1_size", (PyCFunction)dirstate_item_v1_size, METH_NOARGS,
524 530 "return a \"size\" suitable for v1 serialization"},
525 531 {"v1_mtime", (PyCFunction)dirstate_item_v1_mtime, METH_NOARGS,
526 532 "return a \"mtime\" suitable for v1 serialization"},
527 533 {"need_delay", (PyCFunction)dirstate_item_need_delay, METH_O,
528 534 "True if the stored mtime would be ambiguous with the current time"},
529 535 {"mtime_likely_equal_to", (PyCFunction)dirstate_item_mtime_likely_equal_to,
530 536 METH_O, "True if the stored mtime is likely equal to the given mtime"},
531 537 {"from_v1_data", (PyCFunction)dirstate_item_from_v1_meth,
532 538 METH_VARARGS | METH_CLASS, "build a new DirstateItem object from V1 data"},
533 539 {"from_v2_data", (PyCFunction)dirstate_item_from_v2_meth,
534 540 METH_VARARGS | METH_CLASS, "build a new DirstateItem object from V2 data"},
535 541 {"set_possibly_dirty", (PyCFunction)dirstate_item_set_possibly_dirty,
536 542 METH_NOARGS, "mark a file as \"possibly dirty\""},
537 543 {"set_clean", (PyCFunction)dirstate_item_set_clean, METH_VARARGS,
538 544 "mark a file as \"clean\""},
539 545 {"set_tracked", (PyCFunction)dirstate_item_set_tracked, METH_NOARGS,
540 546 "mark a file as \"tracked\""},
541 547 {"set_untracked", (PyCFunction)dirstate_item_set_untracked, METH_NOARGS,
542 548 "mark a file as \"untracked\""},
543 549 {"drop_merge_data", (PyCFunction)dirstate_item_drop_merge_data, METH_NOARGS,
544 550 "remove all \"merge-only\" from a DirstateItem"},
545 551 {NULL} /* Sentinel */
546 552 };
547 553
548 554 static PyObject *dirstate_item_get_mode(dirstateItemObject *self)
549 555 {
550 556 return PyInt_FromLong(dirstate_item_c_v1_mode(self));
551 557 };
552 558
553 559 static PyObject *dirstate_item_get_size(dirstateItemObject *self)
554 560 {
555 561 return PyInt_FromLong(dirstate_item_c_v1_size(self));
556 562 };
557 563
558 564 static PyObject *dirstate_item_get_mtime(dirstateItemObject *self)
559 565 {
560 566 return PyInt_FromLong(dirstate_item_c_v1_mtime(self));
561 567 };
562 568
563 569 static PyObject *dirstate_item_get_state(dirstateItemObject *self)
564 570 {
565 571 char state = dirstate_item_c_v1_state(self);
566 572 return PyBytes_FromStringAndSize(&state, 1);
567 573 };
568 574
569 575 static PyObject *dirstate_item_get_has_fallback_exec(dirstateItemObject *self)
570 576 {
571 577 if (dirstate_item_c_has_fallback_exec(self)) {
572 578 Py_RETURN_TRUE;
573 579 } else {
574 580 Py_RETURN_FALSE;
575 581 }
576 582 };
577 583
578 584 static PyObject *dirstate_item_get_fallback_exec(dirstateItemObject *self)
579 585 {
580 586 if (dirstate_item_c_has_fallback_exec(self)) {
581 587 if (self->flags & dirstate_flag_fallback_exec) {
582 588 Py_RETURN_TRUE;
583 589 } else {
584 590 Py_RETURN_FALSE;
585 591 }
586 592 } else {
587 593 Py_RETURN_NONE;
588 594 }
589 595 };
590 596
591 597 static int dirstate_item_set_fallback_exec(dirstateItemObject *self,
592 598 PyObject *value)
593 599 {
594 600 if ((value == Py_None) || (value == NULL)) {
595 601 self->flags &= ~dirstate_flag_has_fallback_exec;
596 602 } else {
597 603 self->flags |= dirstate_flag_has_fallback_exec;
598 604 if (PyObject_IsTrue(value)) {
599 605 self->flags |= dirstate_flag_fallback_exec;
600 606 } else {
601 607 self->flags &= ~dirstate_flag_fallback_exec;
602 608 }
603 609 }
604 610 return 0;
605 611 };
606 612
607 613 static PyObject *
608 614 dirstate_item_get_has_fallback_symlink(dirstateItemObject *self)
609 615 {
610 616 if (dirstate_item_c_has_fallback_symlink(self)) {
611 617 Py_RETURN_TRUE;
612 618 } else {
613 619 Py_RETURN_FALSE;
614 620 }
615 621 };
616 622
617 623 static PyObject *dirstate_item_get_fallback_symlink(dirstateItemObject *self)
618 624 {
619 625 if (dirstate_item_c_has_fallback_symlink(self)) {
620 626 if (self->flags & dirstate_flag_fallback_symlink) {
621 627 Py_RETURN_TRUE;
622 628 } else {
623 629 Py_RETURN_FALSE;
624 630 }
625 631 } else {
626 632 Py_RETURN_NONE;
627 633 }
628 634 };
629 635
630 636 static int dirstate_item_set_fallback_symlink(dirstateItemObject *self,
631 637 PyObject *value)
632 638 {
633 639 if ((value == Py_None) || (value == NULL)) {
634 640 self->flags &= ~dirstate_flag_has_fallback_symlink;
635 641 } else {
636 642 self->flags |= dirstate_flag_has_fallback_symlink;
637 643 if (PyObject_IsTrue(value)) {
638 644 self->flags |= dirstate_flag_fallback_symlink;
639 645 } else {
640 646 self->flags &= ~dirstate_flag_fallback_symlink;
641 647 }
642 648 }
643 649 return 0;
644 650 };
645 651
646 652 static PyObject *dirstate_item_get_tracked(dirstateItemObject *self)
647 653 {
648 654 if (dirstate_item_c_tracked(self)) {
649 655 Py_RETURN_TRUE;
650 656 } else {
651 657 Py_RETURN_FALSE;
652 658 }
653 659 };
654 660 static PyObject *dirstate_item_get_p1_tracked(dirstateItemObject *self)
655 661 {
656 662 if (self->flags & dirstate_flag_p1_tracked) {
657 663 Py_RETURN_TRUE;
658 664 } else {
659 665 Py_RETURN_FALSE;
660 666 }
661 667 };
662 668
663 669 static PyObject *dirstate_item_get_added(dirstateItemObject *self)
664 670 {
665 671 if (dirstate_item_c_added(self)) {
666 672 Py_RETURN_TRUE;
667 673 } else {
668 674 Py_RETURN_FALSE;
669 675 }
670 676 };
671 677
672 678 static PyObject *dirstate_item_get_p2_info(dirstateItemObject *self)
673 679 {
674 680 if (self->flags & dirstate_flag_wc_tracked &&
675 681 self->flags & dirstate_flag_p2_info) {
676 682 Py_RETURN_TRUE;
677 683 } else {
678 684 Py_RETURN_FALSE;
679 685 }
680 686 };
681 687
682 688 static PyObject *dirstate_item_get_merged(dirstateItemObject *self)
683 689 {
684 690 if (dirstate_item_c_merged(self)) {
685 691 Py_RETURN_TRUE;
686 692 } else {
687 693 Py_RETURN_FALSE;
688 694 }
689 695 };
690 696
691 697 static PyObject *dirstate_item_get_from_p2(dirstateItemObject *self)
692 698 {
693 699 if (dirstate_item_c_from_p2(self)) {
694 700 Py_RETURN_TRUE;
695 701 } else {
696 702 Py_RETURN_FALSE;
697 703 }
698 704 };
699 705
700 706 static PyObject *dirstate_item_get_maybe_clean(dirstateItemObject *self)
701 707 {
702 708 if (!(self->flags & dirstate_flag_wc_tracked)) {
703 709 Py_RETURN_FALSE;
704 710 } else if (!(self->flags & dirstate_flag_p1_tracked)) {
705 711 Py_RETURN_FALSE;
706 712 } else if (self->flags & dirstate_flag_p2_info) {
707 713 Py_RETURN_FALSE;
708 714 } else {
709 715 Py_RETURN_TRUE;
710 716 }
711 717 };
712 718
713 719 static PyObject *dirstate_item_get_any_tracked(dirstateItemObject *self)
714 720 {
715 721 if (dirstate_item_c_any_tracked(self)) {
716 722 Py_RETURN_TRUE;
717 723 } else {
718 724 Py_RETURN_FALSE;
719 725 }
720 726 };
721 727
722 728 static PyObject *dirstate_item_get_removed(dirstateItemObject *self)
723 729 {
724 730 if (dirstate_item_c_removed(self)) {
725 731 Py_RETURN_TRUE;
726 732 } else {
727 733 Py_RETURN_FALSE;
728 734 }
729 735 };
730 736
731 737 static PyGetSetDef dirstate_item_getset[] = {
732 738 {"mode", (getter)dirstate_item_get_mode, NULL, "mode", NULL},
733 739 {"size", (getter)dirstate_item_get_size, NULL, "size", NULL},
734 740 {"mtime", (getter)dirstate_item_get_mtime, NULL, "mtime", NULL},
735 741 {"state", (getter)dirstate_item_get_state, NULL, "state", NULL},
736 742 {"has_fallback_exec", (getter)dirstate_item_get_has_fallback_exec, NULL,
737 743 "has_fallback_exec", NULL},
738 744 {"fallback_exec", (getter)dirstate_item_get_fallback_exec,
739 745 (setter)dirstate_item_set_fallback_exec, "fallback_exec", NULL},
740 746 {"has_fallback_symlink", (getter)dirstate_item_get_has_fallback_symlink,
741 747 NULL, "has_fallback_symlink", NULL},
742 748 {"fallback_symlink", (getter)dirstate_item_get_fallback_symlink,
743 749 (setter)dirstate_item_set_fallback_symlink, "fallback_symlink", NULL},
744 750 {"tracked", (getter)dirstate_item_get_tracked, NULL, "tracked", NULL},
745 751 {"p1_tracked", (getter)dirstate_item_get_p1_tracked, NULL, "p1_tracked",
746 752 NULL},
747 753 {"added", (getter)dirstate_item_get_added, NULL, "added", NULL},
748 754 {"p2_info", (getter)dirstate_item_get_p2_info, NULL, "p2_info", NULL},
749 755 {"merged", (getter)dirstate_item_get_merged, NULL, "merged", NULL},
750 756 {"from_p2", (getter)dirstate_item_get_from_p2, NULL, "from_p2", NULL},
751 757 {"maybe_clean", (getter)dirstate_item_get_maybe_clean, NULL, "maybe_clean",
752 758 NULL},
753 759 {"any_tracked", (getter)dirstate_item_get_any_tracked, NULL, "any_tracked",
754 760 NULL},
755 761 {"removed", (getter)dirstate_item_get_removed, NULL, "removed", NULL},
756 762 {NULL} /* Sentinel */
757 763 };
758 764
759 765 PyTypeObject dirstateItemType = {
760 766 PyVarObject_HEAD_INIT(NULL, 0) /* header */
761 767 "dirstate_tuple", /* tp_name */
762 768 sizeof(dirstateItemObject), /* tp_basicsize */
763 769 0, /* tp_itemsize */
764 770 (destructor)dirstate_item_dealloc, /* tp_dealloc */
765 771 0, /* tp_print */
766 772 0, /* tp_getattr */
767 773 0, /* tp_setattr */
768 774 0, /* tp_compare */
769 775 0, /* tp_repr */
770 776 0, /* tp_as_number */
771 777 0, /* tp_as_sequence */
772 778 0, /* tp_as_mapping */
773 779 0, /* tp_hash */
774 780 0, /* tp_call */
775 781 0, /* tp_str */
776 782 0, /* tp_getattro */
777 783 0, /* tp_setattro */
778 784 0, /* tp_as_buffer */
779 785 Py_TPFLAGS_DEFAULT, /* tp_flags */
780 786 "dirstate tuple", /* tp_doc */
781 787 0, /* tp_traverse */
782 788 0, /* tp_clear */
783 789 0, /* tp_richcompare */
784 790 0, /* tp_weaklistoffset */
785 791 0, /* tp_iter */
786 792 0, /* tp_iternext */
787 793 dirstate_item_methods, /* tp_methods */
788 794 0, /* tp_members */
789 795 dirstate_item_getset, /* tp_getset */
790 796 0, /* tp_base */
791 797 0, /* tp_dict */
792 798 0, /* tp_descr_get */
793 799 0, /* tp_descr_set */
794 800 0, /* tp_dictoffset */
795 801 0, /* tp_init */
796 802 0, /* tp_alloc */
797 803 dirstate_item_new, /* tp_new */
798 804 };
799 805
800 806 static PyObject *parse_dirstate(PyObject *self, PyObject *args)
801 807 {
802 808 PyObject *dmap, *cmap, *parents = NULL, *ret = NULL;
803 809 PyObject *fname = NULL, *cname = NULL, *entry = NULL;
804 810 char state, *cur, *str, *cpos;
805 811 int mode, size, mtime;
806 812 unsigned int flen, pos = 40;
807 813 Py_ssize_t len = 40;
808 814 Py_ssize_t readlen;
809 815
810 816 if (!PyArg_ParseTuple(
811 817 args, PY23("O!O!s#:parse_dirstate", "O!O!y#:parse_dirstate"),
812 818 &PyDict_Type, &dmap, &PyDict_Type, &cmap, &str, &readlen)) {
813 819 goto quit;
814 820 }
815 821
816 822 len = readlen;
817 823
818 824 /* read parents */
819 825 if (len < 40) {
820 826 PyErr_SetString(PyExc_ValueError,
821 827 "too little data for parents");
822 828 goto quit;
823 829 }
824 830
825 831 parents = Py_BuildValue(PY23("s#s#", "y#y#"), str, (Py_ssize_t)20,
826 832 str + 20, (Py_ssize_t)20);
827 833 if (!parents) {
828 834 goto quit;
829 835 }
830 836
831 837 /* read filenames */
832 838 while (pos >= 40 && pos < len) {
833 839 if (pos + 17 > len) {
834 840 PyErr_SetString(PyExc_ValueError,
835 841 "overflow in dirstate");
836 842 goto quit;
837 843 }
838 844 cur = str + pos;
839 845 /* unpack header */
840 846 state = *cur;
841 847 mode = getbe32(cur + 1);
842 848 size = getbe32(cur + 5);
843 849 mtime = getbe32(cur + 9);
844 850 flen = getbe32(cur + 13);
845 851 pos += 17;
846 852 cur += 17;
847 853 if (flen > len - pos) {
848 854 PyErr_SetString(PyExc_ValueError,
849 855 "overflow in dirstate");
850 856 goto quit;
851 857 }
852 858
853 859 entry = (PyObject *)dirstate_item_from_v1_data(state, mode,
854 860 size, mtime);
855 861 if (!entry)
856 862 goto quit;
857 863 cpos = memchr(cur, 0, flen);
858 864 if (cpos) {
859 865 fname = PyBytes_FromStringAndSize(cur, cpos - cur);
860 866 cname = PyBytes_FromStringAndSize(
861 867 cpos + 1, flen - (cpos - cur) - 1);
862 868 if (!fname || !cname ||
863 869 PyDict_SetItem(cmap, fname, cname) == -1 ||
864 870 PyDict_SetItem(dmap, fname, entry) == -1) {
865 871 goto quit;
866 872 }
867 873 Py_DECREF(cname);
868 874 } else {
869 875 fname = PyBytes_FromStringAndSize(cur, flen);
870 876 if (!fname ||
871 877 PyDict_SetItem(dmap, fname, entry) == -1) {
872 878 goto quit;
873 879 }
874 880 }
875 881 Py_DECREF(fname);
876 882 Py_DECREF(entry);
877 883 fname = cname = entry = NULL;
878 884 pos += flen;
879 885 }
880 886
881 887 ret = parents;
882 888 Py_INCREF(ret);
883 889 quit:
884 890 Py_XDECREF(fname);
885 891 Py_XDECREF(cname);
886 892 Py_XDECREF(entry);
887 893 Py_XDECREF(parents);
888 894 return ret;
889 895 }
890 896
891 897 /*
892 898 * Efficiently pack a dirstate object into its on-disk format.
893 899 */
894 900 static PyObject *pack_dirstate(PyObject *self, PyObject *args)
895 901 {
896 902 PyObject *packobj = NULL;
897 903 PyObject *map, *copymap, *pl, *mtime_unset = NULL;
898 904 Py_ssize_t nbytes, pos, l;
899 905 PyObject *k, *v = NULL, *pn;
900 906 char *p, *s;
901 907 int now_s;
902 908 int now_ns;
903 909
904 910 if (!PyArg_ParseTuple(args, "O!O!O!(ii):pack_dirstate", &PyDict_Type,
905 911 &map, &PyDict_Type, &copymap, &PyTuple_Type, &pl,
906 912 &now_s, &now_ns)) {
907 913 return NULL;
908 914 }
909 915
910 916 if (PyTuple_Size(pl) != 2) {
911 917 PyErr_SetString(PyExc_TypeError, "expected 2-element tuple");
912 918 return NULL;
913 919 }
914 920
915 921 /* Figure out how much we need to allocate. */
916 922 for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
917 923 PyObject *c;
918 924 if (!PyBytes_Check(k)) {
919 925 PyErr_SetString(PyExc_TypeError, "expected string key");
920 926 goto bail;
921 927 }
922 928 nbytes += PyBytes_GET_SIZE(k) + 17;
923 929 c = PyDict_GetItem(copymap, k);
924 930 if (c) {
925 931 if (!PyBytes_Check(c)) {
926 932 PyErr_SetString(PyExc_TypeError,
927 933 "expected string key");
928 934 goto bail;
929 935 }
930 936 nbytes += PyBytes_GET_SIZE(c) + 1;
931 937 }
932 938 }
933 939
934 940 packobj = PyBytes_FromStringAndSize(NULL, nbytes);
935 941 if (packobj == NULL) {
936 942 goto bail;
937 943 }
938 944
939 945 p = PyBytes_AS_STRING(packobj);
940 946
941 947 pn = PyTuple_GET_ITEM(pl, 0);
942 948 if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
943 949 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
944 950 goto bail;
945 951 }
946 952 memcpy(p, s, l);
947 953 p += 20;
948 954 pn = PyTuple_GET_ITEM(pl, 1);
949 955 if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
950 956 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
951 957 goto bail;
952 958 }
953 959 memcpy(p, s, l);
954 960 p += 20;
955 961
956 962 for (pos = 0; PyDict_Next(map, &pos, &k, &v);) {
957 963 dirstateItemObject *tuple;
958 964 char state;
959 965 int mode, size, mtime;
960 966 Py_ssize_t len, l;
961 967 PyObject *o;
962 968 char *t;
963 969
964 970 if (!dirstate_tuple_check(v)) {
965 971 PyErr_SetString(PyExc_TypeError,
966 972 "expected a dirstate tuple");
967 973 goto bail;
968 974 }
969 975 tuple = (dirstateItemObject *)v;
970 976
971 977 state = dirstate_item_c_v1_state(tuple);
972 978 mode = dirstate_item_c_v1_mode(tuple);
973 979 size = dirstate_item_c_v1_size(tuple);
974 980 mtime = dirstate_item_c_v1_mtime(tuple);
975 981 if (state == 'n' && tuple->mtime_s == now_s) {
976 982 /* See pure/parsers.py:pack_dirstate for why we do
977 983 * this. */
978 984 mtime = -1;
979 985 mtime_unset = (PyObject *)dirstate_item_from_v1_data(
980 986 state, mode, size, mtime);
981 987 if (!mtime_unset) {
982 988 goto bail;
983 989 }
984 990 if (PyDict_SetItem(map, k, mtime_unset) == -1) {
985 991 goto bail;
986 992 }
987 993 Py_DECREF(mtime_unset);
988 994 mtime_unset = NULL;
989 995 }
990 996 *p++ = state;
991 997 putbe32((uint32_t)mode, p);
992 998 putbe32((uint32_t)size, p + 4);
993 999 putbe32((uint32_t)mtime, p + 8);
994 1000 t = p + 12;
995 1001 p += 16;
996 1002 len = PyBytes_GET_SIZE(k);
997 1003 memcpy(p, PyBytes_AS_STRING(k), len);
998 1004 p += len;
999 1005 o = PyDict_GetItem(copymap, k);
1000 1006 if (o) {
1001 1007 *p++ = '\0';
1002 1008 l = PyBytes_GET_SIZE(o);
1003 1009 memcpy(p, PyBytes_AS_STRING(o), l);
1004 1010 p += l;
1005 1011 len += l + 1;
1006 1012 }
1007 1013 putbe32((uint32_t)len, t);
1008 1014 }
1009 1015
1010 1016 pos = p - PyBytes_AS_STRING(packobj);
1011 1017 if (pos != nbytes) {
1012 1018 PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
1013 1019 (long)pos, (long)nbytes);
1014 1020 goto bail;
1015 1021 }
1016 1022
1017 1023 return packobj;
1018 1024 bail:
1019 1025 Py_XDECREF(mtime_unset);
1020 1026 Py_XDECREF(packobj);
1021 1027 Py_XDECREF(v);
1022 1028 return NULL;
1023 1029 }
1024 1030
1025 1031 #define BUMPED_FIX 1
1026 1032 #define USING_SHA_256 2
1027 1033 #define FM1_HEADER_SIZE (4 + 8 + 2 + 2 + 1 + 1 + 1)
1028 1034
1029 1035 static PyObject *readshas(const char *source, unsigned char num,
1030 1036 Py_ssize_t hashwidth)
1031 1037 {
1032 1038 int i;
1033 1039 PyObject *list = PyTuple_New(num);
1034 1040 if (list == NULL) {
1035 1041 return NULL;
1036 1042 }
1037 1043 for (i = 0; i < num; i++) {
1038 1044 PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth);
1039 1045 if (hash == NULL) {
1040 1046 Py_DECREF(list);
1041 1047 return NULL;
1042 1048 }
1043 1049 PyTuple_SET_ITEM(list, i, hash);
1044 1050 source += hashwidth;
1045 1051 }
1046 1052 return list;
1047 1053 }
1048 1054
1049 1055 static PyObject *fm1readmarker(const char *databegin, const char *dataend,
1050 1056 uint32_t *msize)
1051 1057 {
1052 1058 const char *data = databegin;
1053 1059 const char *meta;
1054 1060
1055 1061 double mtime;
1056 1062 int16_t tz;
1057 1063 uint16_t flags;
1058 1064 unsigned char nsuccs, nparents, nmetadata;
1059 1065 Py_ssize_t hashwidth = 20;
1060 1066
1061 1067 PyObject *prec = NULL, *parents = NULL, *succs = NULL;
1062 1068 PyObject *metadata = NULL, *ret = NULL;
1063 1069 int i;
1064 1070
1065 1071 if (data + FM1_HEADER_SIZE > dataend) {
1066 1072 goto overflow;
1067 1073 }
1068 1074
1069 1075 *msize = getbe32(data);
1070 1076 data += 4;
1071 1077 mtime = getbefloat64(data);
1072 1078 data += 8;
1073 1079 tz = getbeint16(data);
1074 1080 data += 2;
1075 1081 flags = getbeuint16(data);
1076 1082 data += 2;
1077 1083
1078 1084 if (flags & USING_SHA_256) {
1079 1085 hashwidth = 32;
1080 1086 }
1081 1087
1082 1088 nsuccs = (unsigned char)(*data++);
1083 1089 nparents = (unsigned char)(*data++);
1084 1090 nmetadata = (unsigned char)(*data++);
1085 1091
1086 1092 if (databegin + *msize > dataend) {
1087 1093 goto overflow;
1088 1094 }
1089 1095 dataend = databegin + *msize; /* narrow down to marker size */
1090 1096
1091 1097 if (data + hashwidth > dataend) {
1092 1098 goto overflow;
1093 1099 }
1094 1100 prec = PyBytes_FromStringAndSize(data, hashwidth);
1095 1101 data += hashwidth;
1096 1102 if (prec == NULL) {
1097 1103 goto bail;
1098 1104 }
1099 1105
1100 1106 if (data + nsuccs * hashwidth > dataend) {
1101 1107 goto overflow;
1102 1108 }
1103 1109 succs = readshas(data, nsuccs, hashwidth);
1104 1110 if (succs == NULL) {
1105 1111 goto bail;
1106 1112 }
1107 1113 data += nsuccs * hashwidth;
1108 1114
1109 1115 if (nparents == 1 || nparents == 2) {
1110 1116 if (data + nparents * hashwidth > dataend) {
1111 1117 goto overflow;
1112 1118 }
1113 1119 parents = readshas(data, nparents, hashwidth);
1114 1120 if (parents == NULL) {
1115 1121 goto bail;
1116 1122 }
1117 1123 data += nparents * hashwidth;
1118 1124 } else {
1119 1125 parents = Py_None;
1120 1126 Py_INCREF(parents);
1121 1127 }
1122 1128
1123 1129 if (data + 2 * nmetadata > dataend) {
1124 1130 goto overflow;
1125 1131 }
1126 1132 meta = data + (2 * nmetadata);
1127 1133 metadata = PyTuple_New(nmetadata);
1128 1134 if (metadata == NULL) {
1129 1135 goto bail;
1130 1136 }
1131 1137 for (i = 0; i < nmetadata; i++) {
1132 1138 PyObject *tmp, *left = NULL, *right = NULL;
1133 1139 Py_ssize_t leftsize = (unsigned char)(*data++);
1134 1140 Py_ssize_t rightsize = (unsigned char)(*data++);
1135 1141 if (meta + leftsize + rightsize > dataend) {
1136 1142 goto overflow;
1137 1143 }
1138 1144 left = PyBytes_FromStringAndSize(meta, leftsize);
1139 1145 meta += leftsize;
1140 1146 right = PyBytes_FromStringAndSize(meta, rightsize);
1141 1147 meta += rightsize;
1142 1148 tmp = PyTuple_New(2);
1143 1149 if (!left || !right || !tmp) {
1144 1150 Py_XDECREF(left);
1145 1151 Py_XDECREF(right);
1146 1152 Py_XDECREF(tmp);
1147 1153 goto bail;
1148 1154 }
1149 1155 PyTuple_SET_ITEM(tmp, 0, left);
1150 1156 PyTuple_SET_ITEM(tmp, 1, right);
1151 1157 PyTuple_SET_ITEM(metadata, i, tmp);
1152 1158 }
1153 1159 ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, metadata, mtime,
1154 1160 (int)tz * 60, parents);
1155 1161 goto bail; /* return successfully */
1156 1162
1157 1163 overflow:
1158 1164 PyErr_SetString(PyExc_ValueError, "overflow in obsstore");
1159 1165 bail:
1160 1166 Py_XDECREF(prec);
1161 1167 Py_XDECREF(succs);
1162 1168 Py_XDECREF(metadata);
1163 1169 Py_XDECREF(parents);
1164 1170 return ret;
1165 1171 }
1166 1172
1167 1173 static PyObject *fm1readmarkers(PyObject *self, PyObject *args)
1168 1174 {
1169 1175 const char *data, *dataend;
1170 1176 Py_ssize_t datalen, offset, stop;
1171 1177 PyObject *markers = NULL;
1172 1178
1173 1179 if (!PyArg_ParseTuple(args, PY23("s#nn", "y#nn"), &data, &datalen,
1174 1180 &offset, &stop)) {
1175 1181 return NULL;
1176 1182 }
1177 1183 if (offset < 0) {
1178 1184 PyErr_SetString(PyExc_ValueError,
1179 1185 "invalid negative offset in fm1readmarkers");
1180 1186 return NULL;
1181 1187 }
1182 1188 if (stop > datalen) {
1183 1189 PyErr_SetString(
1184 1190 PyExc_ValueError,
1185 1191 "stop longer than data length in fm1readmarkers");
1186 1192 return NULL;
1187 1193 }
1188 1194 dataend = data + datalen;
1189 1195 data += offset;
1190 1196 markers = PyList_New(0);
1191 1197 if (!markers) {
1192 1198 return NULL;
1193 1199 }
1194 1200 while (offset < stop) {
1195 1201 uint32_t msize;
1196 1202 int error;
1197 1203 PyObject *record = fm1readmarker(data, dataend, &msize);
1198 1204 if (!record) {
1199 1205 goto bail;
1200 1206 }
1201 1207 error = PyList_Append(markers, record);
1202 1208 Py_DECREF(record);
1203 1209 if (error) {
1204 1210 goto bail;
1205 1211 }
1206 1212 data += msize;
1207 1213 offset += msize;
1208 1214 }
1209 1215 return markers;
1210 1216 bail:
1211 1217 Py_DECREF(markers);
1212 1218 return NULL;
1213 1219 }
1214 1220
1215 1221 static char parsers_doc[] = "Efficient content parsing.";
1216 1222
1217 1223 PyObject *encodedir(PyObject *self, PyObject *args);
1218 1224 PyObject *pathencode(PyObject *self, PyObject *args);
1219 1225 PyObject *lowerencode(PyObject *self, PyObject *args);
1220 1226 PyObject *parse_index2(PyObject *self, PyObject *args, PyObject *kwargs);
1221 1227
1222 1228 static PyMethodDef methods[] = {
1223 1229 {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
1224 1230 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
1225 1231 {"parse_index2", (PyCFunction)parse_index2, METH_VARARGS | METH_KEYWORDS,
1226 1232 "parse a revlog index\n"},
1227 1233 {"isasciistr", isasciistr, METH_VARARGS, "check if an ASCII string\n"},
1228 1234 {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"},
1229 1235 {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"},
1230 1236 {"dict_new_presized", dict_new_presized, METH_VARARGS,
1231 1237 "construct a dict with an expected size\n"},
1232 1238 {"make_file_foldmap", make_file_foldmap, METH_VARARGS,
1233 1239 "make file foldmap\n"},
1234 1240 {"jsonescapeu8fast", jsonescapeu8fast, METH_VARARGS,
1235 1241 "escape a UTF-8 byte string to JSON (fast path)\n"},
1236 1242 {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
1237 1243 {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
1238 1244 {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"},
1239 1245 {"fm1readmarkers", fm1readmarkers, METH_VARARGS,
1240 1246 "parse v1 obsolete markers\n"},
1241 1247 {NULL, NULL}};
1242 1248
1243 1249 void dirs_module_init(PyObject *mod);
1244 1250 void manifest_module_init(PyObject *mod);
1245 1251 void revlog_module_init(PyObject *mod);
1246 1252
1247 1253 static const int version = 20;
1248 1254
1249 1255 static void module_init(PyObject *mod)
1250 1256 {
1251 1257 PyModule_AddIntConstant(mod, "version", version);
1252 1258
1253 1259 /* This module constant has two purposes. First, it lets us unit test
1254 1260 * the ImportError raised without hard-coding any error text. This
1255 1261 * means we can change the text in the future without breaking tests,
1256 1262 * even across changesets without a recompile. Second, its presence
1257 1263 * can be used to determine whether the version-checking logic is
1258 1264 * present, which also helps in testing across changesets without a
1259 1265 * recompile. Note that this means the pure-Python version of parsers
1260 1266 * should not have this module constant. */
1261 1267 PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext);
1262 1268
1263 1269 dirs_module_init(mod);
1264 1270 manifest_module_init(mod);
1265 1271 revlog_module_init(mod);
1266 1272
1267 1273 if (PyType_Ready(&dirstateItemType) < 0) {
1268 1274 return;
1269 1275 }
1270 1276 Py_INCREF(&dirstateItemType);
1271 1277 PyModule_AddObject(mod, "DirstateItem", (PyObject *)&dirstateItemType);
1272 1278 }
1273 1279
1274 1280 static int check_python_version(void)
1275 1281 {
1276 1282 PyObject *sys = PyImport_ImportModule("sys"), *ver;
1277 1283 long hexversion;
1278 1284 if (!sys) {
1279 1285 return -1;
1280 1286 }
1281 1287 ver = PyObject_GetAttrString(sys, "hexversion");
1282 1288 Py_DECREF(sys);
1283 1289 if (!ver) {
1284 1290 return -1;
1285 1291 }
1286 1292 hexversion = PyInt_AsLong(ver);
1287 1293 Py_DECREF(ver);
1288 1294 /* sys.hexversion is a 32-bit number by default, so the -1 case
1289 1295 * should only occur in unusual circumstances (e.g. if sys.hexversion
1290 1296 * is manually set to an invalid value). */
1291 1297 if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) {
1292 1298 PyErr_Format(PyExc_ImportError,
1293 1299 "%s: The Mercurial extension "
1294 1300 "modules were compiled with Python " PY_VERSION
1295 1301 ", but "
1296 1302 "Mercurial is currently using Python with "
1297 1303 "sys.hexversion=%ld: "
1298 1304 "Python %s\n at: %s",
1299 1305 versionerrortext, hexversion, Py_GetVersion(),
1300 1306 Py_GetProgramFullPath());
1301 1307 return -1;
1302 1308 }
1303 1309 return 0;
1304 1310 }
1305 1311
1306 1312 #ifdef IS_PY3K
1307 1313 static struct PyModuleDef parsers_module = {PyModuleDef_HEAD_INIT, "parsers",
1308 1314 parsers_doc, -1, methods};
1309 1315
1310 1316 PyMODINIT_FUNC PyInit_parsers(void)
1311 1317 {
1312 1318 PyObject *mod;
1313 1319
1314 1320 if (check_python_version() == -1)
1315 1321 return NULL;
1316 1322 mod = PyModule_Create(&parsers_module);
1317 1323 module_init(mod);
1318 1324 return mod;
1319 1325 }
1320 1326 #else
1321 1327 PyMODINIT_FUNC initparsers(void)
1322 1328 {
1323 1329 PyObject *mod;
1324 1330
1325 1331 if (check_python_version() == -1) {
1326 1332 return;
1327 1333 }
1328 1334 mod = Py_InitModule3("parsers", methods, parsers_doc);
1329 1335 module_init(mod);
1330 1336 }
1331 1337 #endif
General Comments 0
You need to be logged in to leave comments. Login now