Show More
@@ -1,753 +1,754 b'' | |||
|
1 | 1 | /* |
|
2 | 2 | parsers.c - efficient content parsing |
|
3 | 3 | |
|
4 | 4 | Copyright 2008 Matt Mackall <mpm@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 | #define PY_SSIZE_T_CLEAN | |
|
10 | 11 | #include <Python.h> |
|
11 | 12 | #include <ctype.h> |
|
12 | 13 | #include <stddef.h> |
|
13 | 14 | #include <string.h> |
|
14 | 15 | |
|
15 | 16 | #include "bitmanipulation.h" |
|
16 | 17 | #include "charencode.h" |
|
17 | 18 | #include "util.h" |
|
18 | 19 | |
|
19 | 20 | #ifdef IS_PY3K |
|
20 | 21 | /* The mapping of Python types is meant to be temporary to get Python |
|
21 | 22 | * 3 to compile. We should remove this once Python 3 support is fully |
|
22 | 23 | * supported and proper types are used in the extensions themselves. */ |
|
23 | 24 | #define PyInt_Check PyLong_Check |
|
24 | 25 | #define PyInt_FromLong PyLong_FromLong |
|
25 | 26 | #define PyInt_FromSsize_t PyLong_FromSsize_t |
|
26 | 27 | #define PyInt_AsLong PyLong_AsLong |
|
27 | 28 | #endif |
|
28 | 29 | |
|
29 | 30 | static const char *const versionerrortext = "Python minor version mismatch"; |
|
30 | 31 | |
|
31 | 32 | static PyObject *dict_new_presized(PyObject *self, PyObject *args) |
|
32 | 33 | { |
|
33 | 34 | Py_ssize_t expected_size; |
|
34 | 35 | |
|
35 | 36 | if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size)) { |
|
36 | 37 | return NULL; |
|
37 | 38 | } |
|
38 | 39 | |
|
39 | 40 | return _dict_new_presized(expected_size); |
|
40 | 41 | } |
|
41 | 42 | |
|
42 | 43 | static inline dirstateTupleObject *make_dirstate_tuple(char state, int mode, |
|
43 | 44 | int size, int mtime) |
|
44 | 45 | { |
|
45 | 46 | dirstateTupleObject *t = |
|
46 | 47 | PyObject_New(dirstateTupleObject, &dirstateTupleType); |
|
47 | 48 | if (!t) { |
|
48 | 49 | return NULL; |
|
49 | 50 | } |
|
50 | 51 | t->state = state; |
|
51 | 52 | t->mode = mode; |
|
52 | 53 | t->size = size; |
|
53 | 54 | t->mtime = mtime; |
|
54 | 55 | return t; |
|
55 | 56 | } |
|
56 | 57 | |
|
57 | 58 | static PyObject *dirstate_tuple_new(PyTypeObject *subtype, PyObject *args, |
|
58 | 59 | PyObject *kwds) |
|
59 | 60 | { |
|
60 | 61 | /* We do all the initialization here and not a tp_init function because |
|
61 | 62 | * dirstate_tuple is immutable. */ |
|
62 | 63 | dirstateTupleObject *t; |
|
63 | 64 | char state; |
|
64 | 65 | int size, mode, mtime; |
|
65 | 66 | if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) { |
|
66 | 67 | return NULL; |
|
67 | 68 | } |
|
68 | 69 | |
|
69 | 70 | t = (dirstateTupleObject *)subtype->tp_alloc(subtype, 1); |
|
70 | 71 | if (!t) { |
|
71 | 72 | return NULL; |
|
72 | 73 | } |
|
73 | 74 | t->state = state; |
|
74 | 75 | t->mode = mode; |
|
75 | 76 | t->size = size; |
|
76 | 77 | t->mtime = mtime; |
|
77 | 78 | |
|
78 | 79 | return (PyObject *)t; |
|
79 | 80 | } |
|
80 | 81 | |
|
81 | 82 | static void dirstate_tuple_dealloc(PyObject *o) |
|
82 | 83 | { |
|
83 | 84 | PyObject_Del(o); |
|
84 | 85 | } |
|
85 | 86 | |
|
86 | 87 | static Py_ssize_t dirstate_tuple_length(PyObject *o) |
|
87 | 88 | { |
|
88 | 89 | return 4; |
|
89 | 90 | } |
|
90 | 91 | |
|
91 | 92 | static PyObject *dirstate_tuple_item(PyObject *o, Py_ssize_t i) |
|
92 | 93 | { |
|
93 | 94 | dirstateTupleObject *t = (dirstateTupleObject *)o; |
|
94 | 95 | switch (i) { |
|
95 | 96 | case 0: |
|
96 | 97 | return PyBytes_FromStringAndSize(&t->state, 1); |
|
97 | 98 | case 1: |
|
98 | 99 | return PyInt_FromLong(t->mode); |
|
99 | 100 | case 2: |
|
100 | 101 | return PyInt_FromLong(t->size); |
|
101 | 102 | case 3: |
|
102 | 103 | return PyInt_FromLong(t->mtime); |
|
103 | 104 | default: |
|
104 | 105 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
|
105 | 106 | return NULL; |
|
106 | 107 | } |
|
107 | 108 | } |
|
108 | 109 | |
|
109 | 110 | static PySequenceMethods dirstate_tuple_sq = { |
|
110 | 111 | dirstate_tuple_length, /* sq_length */ |
|
111 | 112 | 0, /* sq_concat */ |
|
112 | 113 | 0, /* sq_repeat */ |
|
113 | 114 | dirstate_tuple_item, /* sq_item */ |
|
114 | 115 | 0, /* sq_ass_item */ |
|
115 | 116 | 0, /* sq_contains */ |
|
116 | 117 | 0, /* sq_inplace_concat */ |
|
117 | 118 | 0 /* sq_inplace_repeat */ |
|
118 | 119 | }; |
|
119 | 120 | |
|
120 | 121 | PyTypeObject dirstateTupleType = { |
|
121 | 122 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ |
|
122 | 123 | "dirstate_tuple", /* tp_name */ |
|
123 | 124 | sizeof(dirstateTupleObject), /* tp_basicsize */ |
|
124 | 125 | 0, /* tp_itemsize */ |
|
125 | 126 | (destructor)dirstate_tuple_dealloc, /* tp_dealloc */ |
|
126 | 127 | 0, /* tp_print */ |
|
127 | 128 | 0, /* tp_getattr */ |
|
128 | 129 | 0, /* tp_setattr */ |
|
129 | 130 | 0, /* tp_compare */ |
|
130 | 131 | 0, /* tp_repr */ |
|
131 | 132 | 0, /* tp_as_number */ |
|
132 | 133 | &dirstate_tuple_sq, /* tp_as_sequence */ |
|
133 | 134 | 0, /* tp_as_mapping */ |
|
134 | 135 | 0, /* tp_hash */ |
|
135 | 136 | 0, /* tp_call */ |
|
136 | 137 | 0, /* tp_str */ |
|
137 | 138 | 0, /* tp_getattro */ |
|
138 | 139 | 0, /* tp_setattro */ |
|
139 | 140 | 0, /* tp_as_buffer */ |
|
140 | 141 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
141 | 142 | "dirstate tuple", /* tp_doc */ |
|
142 | 143 | 0, /* tp_traverse */ |
|
143 | 144 | 0, /* tp_clear */ |
|
144 | 145 | 0, /* tp_richcompare */ |
|
145 | 146 | 0, /* tp_weaklistoffset */ |
|
146 | 147 | 0, /* tp_iter */ |
|
147 | 148 | 0, /* tp_iternext */ |
|
148 | 149 | 0, /* tp_methods */ |
|
149 | 150 | 0, /* tp_members */ |
|
150 | 151 | 0, /* tp_getset */ |
|
151 | 152 | 0, /* tp_base */ |
|
152 | 153 | 0, /* tp_dict */ |
|
153 | 154 | 0, /* tp_descr_get */ |
|
154 | 155 | 0, /* tp_descr_set */ |
|
155 | 156 | 0, /* tp_dictoffset */ |
|
156 | 157 | 0, /* tp_init */ |
|
157 | 158 | 0, /* tp_alloc */ |
|
158 | 159 | dirstate_tuple_new, /* tp_new */ |
|
159 | 160 | }; |
|
160 | 161 | |
|
161 | 162 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) |
|
162 | 163 | { |
|
163 | 164 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; |
|
164 | 165 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; |
|
165 | 166 | char state, *cur, *str, *cpos; |
|
166 | 167 | int mode, size, mtime; |
|
167 |
unsigned int flen, |
|
|
168 | int readlen; | |
|
168 | unsigned int flen, pos = 40; | |
|
169 | Py_ssize_t len = 40; | |
|
170 | Py_ssize_t readlen; | |
|
169 | 171 | |
|
170 | 172 | if (!PyArg_ParseTuple( |
|
171 | 173 | args, PY23("O!O!s#:parse_dirstate", "O!O!y#:parse_dirstate"), |
|
172 | 174 | &PyDict_Type, &dmap, &PyDict_Type, &cmap, &str, &readlen)) { |
|
173 | 175 | goto quit; |
|
174 | 176 | } |
|
175 | 177 | |
|
176 | 178 | len = readlen; |
|
177 | 179 | |
|
178 | 180 | /* read parents */ |
|
179 | 181 | if (len < 40) { |
|
180 | 182 | PyErr_SetString(PyExc_ValueError, |
|
181 | 183 | "too little data for parents"); |
|
182 | 184 | goto quit; |
|
183 | 185 | } |
|
184 | 186 | |
|
185 | 187 | parents = Py_BuildValue(PY23("s#s#", "y#y#"), str, 20, str + 20, 20); |
|
186 | 188 | if (!parents) { |
|
187 | 189 | goto quit; |
|
188 | 190 | } |
|
189 | 191 | |
|
190 | 192 | /* read filenames */ |
|
191 | 193 | while (pos >= 40 && pos < len) { |
|
192 | 194 | if (pos + 17 > len) { |
|
193 | 195 | PyErr_SetString(PyExc_ValueError, |
|
194 | 196 | "overflow in dirstate"); |
|
195 | 197 | goto quit; |
|
196 | 198 | } |
|
197 | 199 | cur = str + pos; |
|
198 | 200 | /* unpack header */ |
|
199 | 201 | state = *cur; |
|
200 | 202 | mode = getbe32(cur + 1); |
|
201 | 203 | size = getbe32(cur + 5); |
|
202 | 204 | mtime = getbe32(cur + 9); |
|
203 | 205 | flen = getbe32(cur + 13); |
|
204 | 206 | pos += 17; |
|
205 | 207 | cur += 17; |
|
206 | 208 | if (flen > len - pos) { |
|
207 | 209 | PyErr_SetString(PyExc_ValueError, |
|
208 | 210 | "overflow in dirstate"); |
|
209 | 211 | goto quit; |
|
210 | 212 | } |
|
211 | 213 | |
|
212 | 214 | entry = |
|
213 | 215 | (PyObject *)make_dirstate_tuple(state, mode, size, mtime); |
|
214 | 216 | cpos = memchr(cur, 0, flen); |
|
215 | 217 | if (cpos) { |
|
216 | 218 | fname = PyBytes_FromStringAndSize(cur, cpos - cur); |
|
217 | 219 | cname = PyBytes_FromStringAndSize( |
|
218 | 220 | cpos + 1, flen - (cpos - cur) - 1); |
|
219 | 221 | if (!fname || !cname || |
|
220 | 222 | PyDict_SetItem(cmap, fname, cname) == -1 || |
|
221 | 223 | PyDict_SetItem(dmap, fname, entry) == -1) { |
|
222 | 224 | goto quit; |
|
223 | 225 | } |
|
224 | 226 | Py_DECREF(cname); |
|
225 | 227 | } else { |
|
226 | 228 | fname = PyBytes_FromStringAndSize(cur, flen); |
|
227 | 229 | if (!fname || |
|
228 | 230 | PyDict_SetItem(dmap, fname, entry) == -1) { |
|
229 | 231 | goto quit; |
|
230 | 232 | } |
|
231 | 233 | } |
|
232 | 234 | Py_DECREF(fname); |
|
233 | 235 | Py_DECREF(entry); |
|
234 | 236 | fname = cname = entry = NULL; |
|
235 | 237 | pos += flen; |
|
236 | 238 | } |
|
237 | 239 | |
|
238 | 240 | ret = parents; |
|
239 | 241 | Py_INCREF(ret); |
|
240 | 242 | quit: |
|
241 | 243 | Py_XDECREF(fname); |
|
242 | 244 | Py_XDECREF(cname); |
|
243 | 245 | Py_XDECREF(entry); |
|
244 | 246 | Py_XDECREF(parents); |
|
245 | 247 | return ret; |
|
246 | 248 | } |
|
247 | 249 | |
|
248 | 250 | /* |
|
249 | 251 | * Build a set of non-normal and other parent entries from the dirstate dmap |
|
250 | 252 | */ |
|
251 | 253 | static PyObject *nonnormalotherparententries(PyObject *self, PyObject *args) |
|
252 | 254 | { |
|
253 | 255 | PyObject *dmap, *fname, *v; |
|
254 | 256 | PyObject *nonnset = NULL, *otherpset = NULL, *result = NULL; |
|
255 | 257 | Py_ssize_t pos; |
|
256 | 258 | |
|
257 | 259 | if (!PyArg_ParseTuple(args, "O!:nonnormalentries", &PyDict_Type, |
|
258 | 260 | &dmap)) { |
|
259 | 261 | goto bail; |
|
260 | 262 | } |
|
261 | 263 | |
|
262 | 264 | nonnset = PySet_New(NULL); |
|
263 | 265 | if (nonnset == NULL) { |
|
264 | 266 | goto bail; |
|
265 | 267 | } |
|
266 | 268 | |
|
267 | 269 | otherpset = PySet_New(NULL); |
|
268 | 270 | if (otherpset == NULL) { |
|
269 | 271 | goto bail; |
|
270 | 272 | } |
|
271 | 273 | |
|
272 | 274 | pos = 0; |
|
273 | 275 | while (PyDict_Next(dmap, &pos, &fname, &v)) { |
|
274 | 276 | dirstateTupleObject *t; |
|
275 | 277 | if (!dirstate_tuple_check(v)) { |
|
276 | 278 | PyErr_SetString(PyExc_TypeError, |
|
277 | 279 | "expected a dirstate tuple"); |
|
278 | 280 | goto bail; |
|
279 | 281 | } |
|
280 | 282 | t = (dirstateTupleObject *)v; |
|
281 | 283 | |
|
282 | 284 | if (t->state == 'n' && t->size == -2) { |
|
283 | 285 | if (PySet_Add(otherpset, fname) == -1) { |
|
284 | 286 | goto bail; |
|
285 | 287 | } |
|
286 | 288 | } |
|
287 | 289 | |
|
288 | 290 | if (t->state == 'n' && t->mtime != -1) { |
|
289 | 291 | continue; |
|
290 | 292 | } |
|
291 | 293 | if (PySet_Add(nonnset, fname) == -1) { |
|
292 | 294 | goto bail; |
|
293 | 295 | } |
|
294 | 296 | } |
|
295 | 297 | |
|
296 | 298 | result = Py_BuildValue("(OO)", nonnset, otherpset); |
|
297 | 299 | if (result == NULL) { |
|
298 | 300 | goto bail; |
|
299 | 301 | } |
|
300 | 302 | Py_DECREF(nonnset); |
|
301 | 303 | Py_DECREF(otherpset); |
|
302 | 304 | return result; |
|
303 | 305 | bail: |
|
304 | 306 | Py_XDECREF(nonnset); |
|
305 | 307 | Py_XDECREF(otherpset); |
|
306 | 308 | Py_XDECREF(result); |
|
307 | 309 | return NULL; |
|
308 | 310 | } |
|
309 | 311 | |
|
310 | 312 | /* |
|
311 | 313 | * Efficiently pack a dirstate object into its on-disk format. |
|
312 | 314 | */ |
|
313 | 315 | static PyObject *pack_dirstate(PyObject *self, PyObject *args) |
|
314 | 316 | { |
|
315 | 317 | PyObject *packobj = NULL; |
|
316 | 318 | PyObject *map, *copymap, *pl, *mtime_unset = NULL; |
|
317 | 319 | Py_ssize_t nbytes, pos, l; |
|
318 | 320 | PyObject *k, *v = NULL, *pn; |
|
319 | 321 | char *p, *s; |
|
320 | 322 | int now; |
|
321 | 323 | |
|
322 | 324 | if (!PyArg_ParseTuple(args, "O!O!O!i:pack_dirstate", &PyDict_Type, &map, |
|
323 | 325 | &PyDict_Type, ©map, &PyTuple_Type, &pl, |
|
324 | 326 | &now)) { |
|
325 | 327 | return NULL; |
|
326 | 328 | } |
|
327 | 329 | |
|
328 | 330 | if (PyTuple_Size(pl) != 2) { |
|
329 | 331 | PyErr_SetString(PyExc_TypeError, "expected 2-element tuple"); |
|
330 | 332 | return NULL; |
|
331 | 333 | } |
|
332 | 334 | |
|
333 | 335 | /* Figure out how much we need to allocate. */ |
|
334 | 336 | for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) { |
|
335 | 337 | PyObject *c; |
|
336 | 338 | if (!PyBytes_Check(k)) { |
|
337 | 339 | PyErr_SetString(PyExc_TypeError, "expected string key"); |
|
338 | 340 | goto bail; |
|
339 | 341 | } |
|
340 | 342 | nbytes += PyBytes_GET_SIZE(k) + 17; |
|
341 | 343 | c = PyDict_GetItem(copymap, k); |
|
342 | 344 | if (c) { |
|
343 | 345 | if (!PyBytes_Check(c)) { |
|
344 | 346 | PyErr_SetString(PyExc_TypeError, |
|
345 | 347 | "expected string key"); |
|
346 | 348 | goto bail; |
|
347 | 349 | } |
|
348 | 350 | nbytes += PyBytes_GET_SIZE(c) + 1; |
|
349 | 351 | } |
|
350 | 352 | } |
|
351 | 353 | |
|
352 | 354 | packobj = PyBytes_FromStringAndSize(NULL, nbytes); |
|
353 | 355 | if (packobj == NULL) { |
|
354 | 356 | goto bail; |
|
355 | 357 | } |
|
356 | 358 | |
|
357 | 359 | p = PyBytes_AS_STRING(packobj); |
|
358 | 360 | |
|
359 | 361 | pn = PyTuple_GET_ITEM(pl, 0); |
|
360 | 362 | if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
361 | 363 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
362 | 364 | goto bail; |
|
363 | 365 | } |
|
364 | 366 | memcpy(p, s, l); |
|
365 | 367 | p += 20; |
|
366 | 368 | pn = PyTuple_GET_ITEM(pl, 1); |
|
367 | 369 | if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
368 | 370 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
369 | 371 | goto bail; |
|
370 | 372 | } |
|
371 | 373 | memcpy(p, s, l); |
|
372 | 374 | p += 20; |
|
373 | 375 | |
|
374 | 376 | for (pos = 0; PyDict_Next(map, &pos, &k, &v);) { |
|
375 | 377 | dirstateTupleObject *tuple; |
|
376 | 378 | char state; |
|
377 | 379 | int mode, size, mtime; |
|
378 | 380 | Py_ssize_t len, l; |
|
379 | 381 | PyObject *o; |
|
380 | 382 | char *t; |
|
381 | 383 | |
|
382 | 384 | if (!dirstate_tuple_check(v)) { |
|
383 | 385 | PyErr_SetString(PyExc_TypeError, |
|
384 | 386 | "expected a dirstate tuple"); |
|
385 | 387 | goto bail; |
|
386 | 388 | } |
|
387 | 389 | tuple = (dirstateTupleObject *)v; |
|
388 | 390 | |
|
389 | 391 | state = tuple->state; |
|
390 | 392 | mode = tuple->mode; |
|
391 | 393 | size = tuple->size; |
|
392 | 394 | mtime = tuple->mtime; |
|
393 | 395 | if (state == 'n' && mtime == now) { |
|
394 | 396 | /* See pure/parsers.py:pack_dirstate for why we do |
|
395 | 397 | * this. */ |
|
396 | 398 | mtime = -1; |
|
397 | 399 | mtime_unset = (PyObject *)make_dirstate_tuple( |
|
398 | 400 | state, mode, size, mtime); |
|
399 | 401 | if (!mtime_unset) { |
|
400 | 402 | goto bail; |
|
401 | 403 | } |
|
402 | 404 | if (PyDict_SetItem(map, k, mtime_unset) == -1) { |
|
403 | 405 | goto bail; |
|
404 | 406 | } |
|
405 | 407 | Py_DECREF(mtime_unset); |
|
406 | 408 | mtime_unset = NULL; |
|
407 | 409 | } |
|
408 | 410 | *p++ = state; |
|
409 | 411 | putbe32((uint32_t)mode, p); |
|
410 | 412 | putbe32((uint32_t)size, p + 4); |
|
411 | 413 | putbe32((uint32_t)mtime, p + 8); |
|
412 | 414 | t = p + 12; |
|
413 | 415 | p += 16; |
|
414 | 416 | len = PyBytes_GET_SIZE(k); |
|
415 | 417 | memcpy(p, PyBytes_AS_STRING(k), len); |
|
416 | 418 | p += len; |
|
417 | 419 | o = PyDict_GetItem(copymap, k); |
|
418 | 420 | if (o) { |
|
419 | 421 | *p++ = '\0'; |
|
420 | 422 | l = PyBytes_GET_SIZE(o); |
|
421 | 423 | memcpy(p, PyBytes_AS_STRING(o), l); |
|
422 | 424 | p += l; |
|
423 | 425 | len += l + 1; |
|
424 | 426 | } |
|
425 | 427 | putbe32((uint32_t)len, t); |
|
426 | 428 | } |
|
427 | 429 | |
|
428 | 430 | pos = p - PyBytes_AS_STRING(packobj); |
|
429 | 431 | if (pos != nbytes) { |
|
430 | 432 | PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld", |
|
431 | 433 | (long)pos, (long)nbytes); |
|
432 | 434 | goto bail; |
|
433 | 435 | } |
|
434 | 436 | |
|
435 | 437 | return packobj; |
|
436 | 438 | bail: |
|
437 | 439 | Py_XDECREF(mtime_unset); |
|
438 | 440 | Py_XDECREF(packobj); |
|
439 | 441 | Py_XDECREF(v); |
|
440 | 442 | return NULL; |
|
441 | 443 | } |
|
442 | 444 | |
|
443 | 445 | #define BUMPED_FIX 1 |
|
444 | 446 | #define USING_SHA_256 2 |
|
445 | 447 | #define FM1_HEADER_SIZE (4 + 8 + 2 + 2 + 1 + 1 + 1) |
|
446 | 448 | |
|
447 | 449 | static PyObject *readshas(const char *source, unsigned char num, |
|
448 | 450 | Py_ssize_t hashwidth) |
|
449 | 451 | { |
|
450 | 452 | int i; |
|
451 | 453 | PyObject *list = PyTuple_New(num); |
|
452 | 454 | if (list == NULL) { |
|
453 | 455 | return NULL; |
|
454 | 456 | } |
|
455 | 457 | for (i = 0; i < num; i++) { |
|
456 | 458 | PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth); |
|
457 | 459 | if (hash == NULL) { |
|
458 | 460 | Py_DECREF(list); |
|
459 | 461 | return NULL; |
|
460 | 462 | } |
|
461 | 463 | PyTuple_SET_ITEM(list, i, hash); |
|
462 | 464 | source += hashwidth; |
|
463 | 465 | } |
|
464 | 466 | return list; |
|
465 | 467 | } |
|
466 | 468 | |
|
467 | 469 | static PyObject *fm1readmarker(const char *databegin, const char *dataend, |
|
468 | 470 | uint32_t *msize) |
|
469 | 471 | { |
|
470 | 472 | const char *data = databegin; |
|
471 | 473 | const char *meta; |
|
472 | 474 | |
|
473 | 475 | double mtime; |
|
474 | 476 | int16_t tz; |
|
475 | 477 | uint16_t flags; |
|
476 | 478 | unsigned char nsuccs, nparents, nmetadata; |
|
477 | 479 | Py_ssize_t hashwidth = 20; |
|
478 | 480 | |
|
479 | 481 | PyObject *prec = NULL, *parents = NULL, *succs = NULL; |
|
480 | 482 | PyObject *metadata = NULL, *ret = NULL; |
|
481 | 483 | int i; |
|
482 | 484 | |
|
483 | 485 | if (data + FM1_HEADER_SIZE > dataend) { |
|
484 | 486 | goto overflow; |
|
485 | 487 | } |
|
486 | 488 | |
|
487 | 489 | *msize = getbe32(data); |
|
488 | 490 | data += 4; |
|
489 | 491 | mtime = getbefloat64(data); |
|
490 | 492 | data += 8; |
|
491 | 493 | tz = getbeint16(data); |
|
492 | 494 | data += 2; |
|
493 | 495 | flags = getbeuint16(data); |
|
494 | 496 | data += 2; |
|
495 | 497 | |
|
496 | 498 | if (flags & USING_SHA_256) { |
|
497 | 499 | hashwidth = 32; |
|
498 | 500 | } |
|
499 | 501 | |
|
500 | 502 | nsuccs = (unsigned char)(*data++); |
|
501 | 503 | nparents = (unsigned char)(*data++); |
|
502 | 504 | nmetadata = (unsigned char)(*data++); |
|
503 | 505 | |
|
504 | 506 | if (databegin + *msize > dataend) { |
|
505 | 507 | goto overflow; |
|
506 | 508 | } |
|
507 | 509 | dataend = databegin + *msize; /* narrow down to marker size */ |
|
508 | 510 | |
|
509 | 511 | if (data + hashwidth > dataend) { |
|
510 | 512 | goto overflow; |
|
511 | 513 | } |
|
512 | 514 | prec = PyBytes_FromStringAndSize(data, hashwidth); |
|
513 | 515 | data += hashwidth; |
|
514 | 516 | if (prec == NULL) { |
|
515 | 517 | goto bail; |
|
516 | 518 | } |
|
517 | 519 | |
|
518 | 520 | if (data + nsuccs * hashwidth > dataend) { |
|
519 | 521 | goto overflow; |
|
520 | 522 | } |
|
521 | 523 | succs = readshas(data, nsuccs, hashwidth); |
|
522 | 524 | if (succs == NULL) { |
|
523 | 525 | goto bail; |
|
524 | 526 | } |
|
525 | 527 | data += nsuccs * hashwidth; |
|
526 | 528 | |
|
527 | 529 | if (nparents == 1 || nparents == 2) { |
|
528 | 530 | if (data + nparents * hashwidth > dataend) { |
|
529 | 531 | goto overflow; |
|
530 | 532 | } |
|
531 | 533 | parents = readshas(data, nparents, hashwidth); |
|
532 | 534 | if (parents == NULL) { |
|
533 | 535 | goto bail; |
|
534 | 536 | } |
|
535 | 537 | data += nparents * hashwidth; |
|
536 | 538 | } else { |
|
537 | 539 | parents = Py_None; |
|
538 | 540 | Py_INCREF(parents); |
|
539 | 541 | } |
|
540 | 542 | |
|
541 | 543 | if (data + 2 * nmetadata > dataend) { |
|
542 | 544 | goto overflow; |
|
543 | 545 | } |
|
544 | 546 | meta = data + (2 * nmetadata); |
|
545 | 547 | metadata = PyTuple_New(nmetadata); |
|
546 | 548 | if (metadata == NULL) { |
|
547 | 549 | goto bail; |
|
548 | 550 | } |
|
549 | 551 | for (i = 0; i < nmetadata; i++) { |
|
550 | 552 | PyObject *tmp, *left = NULL, *right = NULL; |
|
551 | 553 | Py_ssize_t leftsize = (unsigned char)(*data++); |
|
552 | 554 | Py_ssize_t rightsize = (unsigned char)(*data++); |
|
553 | 555 | if (meta + leftsize + rightsize > dataend) { |
|
554 | 556 | goto overflow; |
|
555 | 557 | } |
|
556 | 558 | left = PyBytes_FromStringAndSize(meta, leftsize); |
|
557 | 559 | meta += leftsize; |
|
558 | 560 | right = PyBytes_FromStringAndSize(meta, rightsize); |
|
559 | 561 | meta += rightsize; |
|
560 | 562 | tmp = PyTuple_New(2); |
|
561 | 563 | if (!left || !right || !tmp) { |
|
562 | 564 | Py_XDECREF(left); |
|
563 | 565 | Py_XDECREF(right); |
|
564 | 566 | Py_XDECREF(tmp); |
|
565 | 567 | goto bail; |
|
566 | 568 | } |
|
567 | 569 | PyTuple_SET_ITEM(tmp, 0, left); |
|
568 | 570 | PyTuple_SET_ITEM(tmp, 1, right); |
|
569 | 571 | PyTuple_SET_ITEM(metadata, i, tmp); |
|
570 | 572 | } |
|
571 | 573 | ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, metadata, mtime, |
|
572 | 574 | (int)tz * 60, parents); |
|
573 | 575 | goto bail; /* return successfully */ |
|
574 | 576 | |
|
575 | 577 | overflow: |
|
576 | 578 | PyErr_SetString(PyExc_ValueError, "overflow in obsstore"); |
|
577 | 579 | bail: |
|
578 | 580 | Py_XDECREF(prec); |
|
579 | 581 | Py_XDECREF(succs); |
|
580 | 582 | Py_XDECREF(metadata); |
|
581 | 583 | Py_XDECREF(parents); |
|
582 | 584 | return ret; |
|
583 | 585 | } |
|
584 | 586 | |
|
585 | 587 | static PyObject *fm1readmarkers(PyObject *self, PyObject *args) |
|
586 | 588 | { |
|
587 | 589 | const char *data, *dataend; |
|
588 | int datalen; | |
|
589 | Py_ssize_t offset, stop; | |
|
590 | Py_ssize_t datalen, offset, stop; | |
|
590 | 591 | PyObject *markers = NULL; |
|
591 | 592 | |
|
592 | 593 | if (!PyArg_ParseTuple(args, PY23("s#nn", "y#nn"), &data, &datalen, |
|
593 | 594 | &offset, &stop)) { |
|
594 | 595 | return NULL; |
|
595 | 596 | } |
|
596 | 597 | if (offset < 0) { |
|
597 | 598 | PyErr_SetString(PyExc_ValueError, |
|
598 | 599 | "invalid negative offset in fm1readmarkers"); |
|
599 | 600 | return NULL; |
|
600 | 601 | } |
|
601 | 602 | if (stop > datalen) { |
|
602 | 603 | PyErr_SetString( |
|
603 | 604 | PyExc_ValueError, |
|
604 | 605 | "stop longer than data length in fm1readmarkers"); |
|
605 | 606 | return NULL; |
|
606 | 607 | } |
|
607 | 608 | dataend = data + datalen; |
|
608 | 609 | data += offset; |
|
609 | 610 | markers = PyList_New(0); |
|
610 | 611 | if (!markers) { |
|
611 | 612 | return NULL; |
|
612 | 613 | } |
|
613 | 614 | while (offset < stop) { |
|
614 | 615 | uint32_t msize; |
|
615 | 616 | int error; |
|
616 | 617 | PyObject *record = fm1readmarker(data, dataend, &msize); |
|
617 | 618 | if (!record) { |
|
618 | 619 | goto bail; |
|
619 | 620 | } |
|
620 | 621 | error = PyList_Append(markers, record); |
|
621 | 622 | Py_DECREF(record); |
|
622 | 623 | if (error) { |
|
623 | 624 | goto bail; |
|
624 | 625 | } |
|
625 | 626 | data += msize; |
|
626 | 627 | offset += msize; |
|
627 | 628 | } |
|
628 | 629 | return markers; |
|
629 | 630 | bail: |
|
630 | 631 | Py_DECREF(markers); |
|
631 | 632 | return NULL; |
|
632 | 633 | } |
|
633 | 634 | |
|
634 | 635 | static char parsers_doc[] = "Efficient content parsing."; |
|
635 | 636 | |
|
636 | 637 | PyObject *encodedir(PyObject *self, PyObject *args); |
|
637 | 638 | PyObject *pathencode(PyObject *self, PyObject *args); |
|
638 | 639 | PyObject *lowerencode(PyObject *self, PyObject *args); |
|
639 | 640 | PyObject *parse_index2(PyObject *self, PyObject *args); |
|
640 | 641 | |
|
641 | 642 | static PyMethodDef methods[] = { |
|
642 | 643 | {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"}, |
|
643 | 644 | {"nonnormalotherparententries", nonnormalotherparententries, METH_VARARGS, |
|
644 | 645 | "create a set containing non-normal and other parent entries of given " |
|
645 | 646 | "dirstate\n"}, |
|
646 | 647 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, |
|
647 | 648 | {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"}, |
|
648 | 649 | {"isasciistr", isasciistr, METH_VARARGS, "check if an ASCII string\n"}, |
|
649 | 650 | {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"}, |
|
650 | 651 | {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"}, |
|
651 | 652 | {"dict_new_presized", dict_new_presized, METH_VARARGS, |
|
652 | 653 | "construct a dict with an expected size\n"}, |
|
653 | 654 | {"make_file_foldmap", make_file_foldmap, METH_VARARGS, |
|
654 | 655 | "make file foldmap\n"}, |
|
655 | 656 | {"jsonescapeu8fast", jsonescapeu8fast, METH_VARARGS, |
|
656 | 657 | "escape a UTF-8 byte string to JSON (fast path)\n"}, |
|
657 | 658 | {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"}, |
|
658 | 659 | {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"}, |
|
659 | 660 | {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"}, |
|
660 | 661 | {"fm1readmarkers", fm1readmarkers, METH_VARARGS, |
|
661 | 662 | "parse v1 obsolete markers\n"}, |
|
662 | 663 | {NULL, NULL}}; |
|
663 | 664 | |
|
664 | 665 | void dirs_module_init(PyObject *mod); |
|
665 | 666 | void manifest_module_init(PyObject *mod); |
|
666 | 667 | void revlog_module_init(PyObject *mod); |
|
667 | 668 | |
|
668 | 669 | static const int version = 12; |
|
669 | 670 | |
|
670 | 671 | static void module_init(PyObject *mod) |
|
671 | 672 | { |
|
672 | 673 | PyModule_AddIntConstant(mod, "version", version); |
|
673 | 674 | |
|
674 | 675 | /* This module constant has two purposes. First, it lets us unit test |
|
675 | 676 | * the ImportError raised without hard-coding any error text. This |
|
676 | 677 | * means we can change the text in the future without breaking tests, |
|
677 | 678 | * even across changesets without a recompile. Second, its presence |
|
678 | 679 | * can be used to determine whether the version-checking logic is |
|
679 | 680 | * present, which also helps in testing across changesets without a |
|
680 | 681 | * recompile. Note that this means the pure-Python version of parsers |
|
681 | 682 | * should not have this module constant. */ |
|
682 | 683 | PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext); |
|
683 | 684 | |
|
684 | 685 | dirs_module_init(mod); |
|
685 | 686 | manifest_module_init(mod); |
|
686 | 687 | revlog_module_init(mod); |
|
687 | 688 | |
|
688 | 689 | if (PyType_Ready(&dirstateTupleType) < 0) { |
|
689 | 690 | return; |
|
690 | 691 | } |
|
691 | 692 | Py_INCREF(&dirstateTupleType); |
|
692 | 693 | PyModule_AddObject(mod, "dirstatetuple", |
|
693 | 694 | (PyObject *)&dirstateTupleType); |
|
694 | 695 | } |
|
695 | 696 | |
|
696 | 697 | static int check_python_version(void) |
|
697 | 698 | { |
|
698 | 699 | PyObject *sys = PyImport_ImportModule("sys"), *ver; |
|
699 | 700 | long hexversion; |
|
700 | 701 | if (!sys) { |
|
701 | 702 | return -1; |
|
702 | 703 | } |
|
703 | 704 | ver = PyObject_GetAttrString(sys, "hexversion"); |
|
704 | 705 | Py_DECREF(sys); |
|
705 | 706 | if (!ver) { |
|
706 | 707 | return -1; |
|
707 | 708 | } |
|
708 | 709 | hexversion = PyInt_AsLong(ver); |
|
709 | 710 | Py_DECREF(ver); |
|
710 | 711 | /* sys.hexversion is a 32-bit number by default, so the -1 case |
|
711 | 712 | * should only occur in unusual circumstances (e.g. if sys.hexversion |
|
712 | 713 | * is manually set to an invalid value). */ |
|
713 | 714 | if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) { |
|
714 | 715 | PyErr_Format(PyExc_ImportError, |
|
715 | 716 | "%s: The Mercurial extension " |
|
716 | 717 | "modules were compiled with Python " PY_VERSION |
|
717 | 718 | ", but " |
|
718 | 719 | "Mercurial is currently using Python with " |
|
719 | 720 | "sys.hexversion=%ld: " |
|
720 | 721 | "Python %s\n at: %s", |
|
721 | 722 | versionerrortext, hexversion, Py_GetVersion(), |
|
722 | 723 | Py_GetProgramFullPath()); |
|
723 | 724 | return -1; |
|
724 | 725 | } |
|
725 | 726 | return 0; |
|
726 | 727 | } |
|
727 | 728 | |
|
728 | 729 | #ifdef IS_PY3K |
|
729 | 730 | static struct PyModuleDef parsers_module = {PyModuleDef_HEAD_INIT, "parsers", |
|
730 | 731 | parsers_doc, -1, methods}; |
|
731 | 732 | |
|
732 | 733 | PyMODINIT_FUNC PyInit_parsers(void) |
|
733 | 734 | { |
|
734 | 735 | PyObject *mod; |
|
735 | 736 | |
|
736 | 737 | if (check_python_version() == -1) |
|
737 | 738 | return NULL; |
|
738 | 739 | mod = PyModule_Create(&parsers_module); |
|
739 | 740 | module_init(mod); |
|
740 | 741 | return mod; |
|
741 | 742 | } |
|
742 | 743 | #else |
|
743 | 744 | PyMODINIT_FUNC initparsers(void) |
|
744 | 745 | { |
|
745 | 746 | PyObject *mod; |
|
746 | 747 | |
|
747 | 748 | if (check_python_version() == -1) { |
|
748 | 749 | return; |
|
749 | 750 | } |
|
750 | 751 | mod = Py_InitModule3("parsers", methods, parsers_doc); |
|
751 | 752 | module_init(mod); |
|
752 | 753 | } |
|
753 | 754 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now