Show More
@@ -1,414 +1,426 b'' | |||||
1 | /* |
|
1 | /* | |
2 | parsers.c - efficient content parsing |
|
2 | parsers.c - efficient content parsing | |
3 |
|
3 | |||
4 | Copyright 2008 Matt Mackall <mpm@selenic.com> and others |
|
4 | Copyright 2008 Matt Mackall <mpm@selenic.com> and others | |
5 |
|
5 | |||
6 | This software may be used and distributed according to the terms of |
|
6 | This software may be used and distributed according to the terms of | |
7 | the GNU General Public License, incorporated herein by reference. |
|
7 | the GNU General Public License, incorporated herein by reference. | |
8 | */ |
|
8 | */ | |
9 |
|
9 | |||
10 | #include <Python.h> |
|
10 | #include <Python.h> | |
11 | #include <ctype.h> |
|
11 | #include <ctype.h> | |
12 | #include <string.h> |
|
12 | #include <string.h> | |
13 |
|
13 | |||
14 | static int hexdigit(char c) |
|
14 | static int hexdigit(char c) | |
15 | { |
|
15 | { | |
16 | if (c >= '0' && c <= '9') |
|
16 | if (c >= '0' && c <= '9') | |
17 | return c - '0'; |
|
17 | return c - '0'; | |
18 | if (c >= 'a' && c <= 'f') |
|
18 | if (c >= 'a' && c <= 'f') | |
19 | return c - 'a' + 10; |
|
19 | return c - 'a' + 10; | |
20 | if (c >= 'A' && c <= 'F') |
|
20 | if (c >= 'A' && c <= 'F') | |
21 | return c - 'A' + 10; |
|
21 | return c - 'A' + 10; | |
22 |
|
22 | |||
23 | PyErr_SetString(PyExc_ValueError, "input contains non-hex character"); |
|
23 | PyErr_SetString(PyExc_ValueError, "input contains non-hex character"); | |
24 | return 0; |
|
24 | return 0; | |
25 | } |
|
25 | } | |
26 |
|
26 | |||
27 | /* |
|
27 | /* | |
28 | * Turn a hex-encoded string into binary. |
|
28 | * Turn a hex-encoded string into binary. | |
29 | */ |
|
29 | */ | |
30 | static PyObject *unhexlify(const char *str, int len) |
|
30 | static PyObject *unhexlify(const char *str, int len) | |
31 | { |
|
31 | { | |
32 | PyObject *ret; |
|
32 | PyObject *ret; | |
33 | const char *c; |
|
33 | const char *c; | |
34 | char *d; |
|
34 | char *d; | |
35 |
|
35 | |||
36 | ret = PyString_FromStringAndSize(NULL, len / 2); |
|
36 | ret = PyString_FromStringAndSize(NULL, len / 2); | |
37 | if (!ret) |
|
37 | if (!ret) | |
38 | return NULL; |
|
38 | return NULL; | |
39 |
|
39 | |||
40 | d = PyString_AS_STRING(ret); |
|
40 | d = PyString_AS_STRING(ret); | |
41 | for (c = str; c < str + len;) { |
|
41 | for (c = str; c < str + len;) { | |
42 | int hi = hexdigit(*c++); |
|
42 | int hi = hexdigit(*c++); | |
43 | int lo = hexdigit(*c++); |
|
43 | int lo = hexdigit(*c++); | |
44 | *d++ = (hi << 4) | lo; |
|
44 | *d++ = (hi << 4) | lo; | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | return ret; |
|
47 | return ret; | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | /* |
|
50 | /* | |
51 | * This code assumes that a manifest is stitched together with newline |
|
51 | * This code assumes that a manifest is stitched together with newline | |
52 | * ('\n') characters. |
|
52 | * ('\n') characters. | |
53 | */ |
|
53 | */ | |
54 | static PyObject *parse_manifest(PyObject *self, PyObject *args) |
|
54 | static PyObject *parse_manifest(PyObject *self, PyObject *args) | |
55 | { |
|
55 | { | |
56 | PyObject *mfdict, *fdict; |
|
56 | PyObject *mfdict, *fdict; | |
57 | char *str, *cur, *start, *zero; |
|
57 | char *str, *cur, *start, *zero; | |
58 | int len; |
|
58 | int len; | |
59 |
|
59 | |||
60 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest", |
|
60 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest", | |
61 | &PyDict_Type, &mfdict, |
|
61 | &PyDict_Type, &mfdict, | |
62 | &PyDict_Type, &fdict, |
|
62 | &PyDict_Type, &fdict, | |
63 | &str, &len)) |
|
63 | &str, &len)) | |
64 | goto quit; |
|
64 | goto quit; | |
65 |
|
65 | |||
66 | for (start = cur = str, zero = NULL; cur < str + len; cur++) { |
|
66 | for (start = cur = str, zero = NULL; cur < str + len; cur++) { | |
67 | PyObject *file = NULL, *node = NULL; |
|
67 | PyObject *file = NULL, *node = NULL; | |
68 | PyObject *flags = NULL; |
|
68 | PyObject *flags = NULL; | |
69 | int nlen; |
|
69 | int nlen; | |
70 |
|
70 | |||
71 | if (!*cur) { |
|
71 | if (!*cur) { | |
72 | zero = cur; |
|
72 | zero = cur; | |
73 | continue; |
|
73 | continue; | |
74 | } |
|
74 | } | |
75 | else if (*cur != '\n') |
|
75 | else if (*cur != '\n') | |
76 | continue; |
|
76 | continue; | |
77 |
|
77 | |||
78 | if (!zero) { |
|
78 | if (!zero) { | |
79 | PyErr_SetString(PyExc_ValueError, |
|
79 | PyErr_SetString(PyExc_ValueError, | |
80 | "manifest entry has no separator"); |
|
80 | "manifest entry has no separator"); | |
81 | goto quit; |
|
81 | goto quit; | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | file = PyString_FromStringAndSize(start, zero - start); |
|
84 | file = PyString_FromStringAndSize(start, zero - start); | |
85 | if (!file) |
|
85 | if (!file) | |
86 | goto bail; |
|
86 | goto bail; | |
87 |
|
87 | |||
88 | nlen = cur - zero - 1; |
|
88 | nlen = cur - zero - 1; | |
89 |
|
89 | |||
90 | node = unhexlify(zero + 1, nlen > 40 ? 40 : nlen); |
|
90 | node = unhexlify(zero + 1, nlen > 40 ? 40 : nlen); | |
91 | if (!node) |
|
91 | if (!node) | |
92 | goto bail; |
|
92 | goto bail; | |
93 |
|
93 | |||
94 | if (nlen > 40) { |
|
94 | if (nlen > 40) { | |
95 | PyObject *flags; |
|
95 | PyObject *flags; | |
96 |
|
96 | |||
97 | flags = PyString_FromStringAndSize(zero + 41, |
|
97 | flags = PyString_FromStringAndSize(zero + 41, | |
98 | nlen - 40); |
|
98 | nlen - 40); | |
99 | if (!flags) |
|
99 | if (!flags) | |
100 | goto bail; |
|
100 | goto bail; | |
101 |
|
101 | |||
102 | if (PyDict_SetItem(fdict, file, flags) == -1) |
|
102 | if (PyDict_SetItem(fdict, file, flags) == -1) | |
103 | goto bail; |
|
103 | goto bail; | |
104 | } |
|
104 | } | |
105 |
|
105 | |||
106 | if (PyDict_SetItem(mfdict, file, node) == -1) |
|
106 | if (PyDict_SetItem(mfdict, file, node) == -1) | |
107 | goto bail; |
|
107 | goto bail; | |
108 |
|
108 | |||
109 | start = cur + 1; |
|
109 | start = cur + 1; | |
110 | zero = NULL; |
|
110 | zero = NULL; | |
111 |
|
111 | |||
112 | Py_XDECREF(flags); |
|
112 | Py_XDECREF(flags); | |
113 | Py_XDECREF(node); |
|
113 | Py_XDECREF(node); | |
114 | Py_XDECREF(file); |
|
114 | Py_XDECREF(file); | |
115 | continue; |
|
115 | continue; | |
116 | bail: |
|
116 | bail: | |
117 | Py_XDECREF(flags); |
|
117 | Py_XDECREF(flags); | |
118 | Py_XDECREF(node); |
|
118 | Py_XDECREF(node); | |
119 | Py_XDECREF(file); |
|
119 | Py_XDECREF(file); | |
120 | goto quit; |
|
120 | goto quit; | |
121 | } |
|
121 | } | |
122 |
|
122 | |||
123 | if (len > 0 && *(cur - 1) != '\n') { |
|
123 | if (len > 0 && *(cur - 1) != '\n') { | |
124 | PyErr_SetString(PyExc_ValueError, |
|
124 | PyErr_SetString(PyExc_ValueError, | |
125 | "manifest contains trailing garbage"); |
|
125 | "manifest contains trailing garbage"); | |
126 | goto quit; |
|
126 | goto quit; | |
127 | } |
|
127 | } | |
128 |
|
128 | |||
129 | Py_INCREF(Py_None); |
|
129 | Py_INCREF(Py_None); | |
130 | return Py_None; |
|
130 | return Py_None; | |
131 | quit: |
|
131 | quit: | |
132 | return NULL; |
|
132 | return NULL; | |
133 | } |
|
133 | } | |
134 |
|
134 | |||
135 | #ifdef _WIN32 |
|
135 | #ifdef _WIN32 | |
136 | # ifdef _MSC_VER |
|
136 | # ifdef _MSC_VER | |
137 | /* msvc 6.0 has problems */ |
|
137 | /* msvc 6.0 has problems */ | |
138 | # define inline __inline |
|
138 | # define inline __inline | |
139 | typedef unsigned long uint32_t; |
|
139 | typedef unsigned long uint32_t; | |
140 | typedef unsigned __int64 uint64_t; |
|
140 | typedef unsigned __int64 uint64_t; | |
141 | # else |
|
141 | # else | |
142 | # include <stdint.h> |
|
142 | # include <stdint.h> | |
143 | # endif |
|
143 | # endif | |
144 | static uint32_t ntohl(uint32_t x) |
|
144 | static uint32_t ntohl(uint32_t x) | |
145 | { |
|
145 | { | |
146 | return ((x & 0x000000ffUL) << 24) | |
|
146 | return ((x & 0x000000ffUL) << 24) | | |
147 | ((x & 0x0000ff00UL) << 8) | |
|
147 | ((x & 0x0000ff00UL) << 8) | | |
148 | ((x & 0x00ff0000UL) >> 8) | |
|
148 | ((x & 0x00ff0000UL) >> 8) | | |
149 | ((x & 0xff000000UL) >> 24); |
|
149 | ((x & 0xff000000UL) >> 24); | |
150 | } |
|
150 | } | |
151 | #else |
|
151 | #else | |
152 | /* not windows */ |
|
152 | /* not windows */ | |
153 | # include <sys/types.h> |
|
153 | # include <sys/types.h> | |
154 | # if defined __BEOS__ && !defined __HAIKU__ |
|
154 | # if defined __BEOS__ && !defined __HAIKU__ | |
155 | # include <ByteOrder.h> |
|
155 | # include <ByteOrder.h> | |
156 | # else |
|
156 | # else | |
157 | # include <arpa/inet.h> |
|
157 | # include <arpa/inet.h> | |
158 | # endif |
|
158 | # endif | |
159 | # include <inttypes.h> |
|
159 | # include <inttypes.h> | |
160 | #endif |
|
160 | #endif | |
161 |
|
161 | |||
162 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) |
|
162 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) | |
163 | { |
|
163 | { | |
164 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; |
|
164 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; | |
165 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; |
|
165 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; | |
166 | char *str, *cur, *end, *cpos; |
|
166 | char *str, *cur, *end, *cpos; | |
167 | int state, mode, size, mtime, flen; |
|
167 | int state, mode, size, mtime, flen; | |
168 | int len; |
|
168 | int len; | |
169 | char decode[16]; /* for alignment */ |
|
169 | char decode[16]; /* for alignment */ | |
170 |
|
170 | |||
171 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate", |
|
171 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate", | |
172 | &PyDict_Type, &dmap, |
|
172 | &PyDict_Type, &dmap, | |
173 | &PyDict_Type, &cmap, |
|
173 | &PyDict_Type, &cmap, | |
174 | &str, &len)) |
|
174 | &str, &len)) | |
175 | goto quit; |
|
175 | goto quit; | |
176 |
|
176 | |||
177 | /* read parents */ |
|
177 | /* read parents */ | |
178 | if (len < 40) |
|
178 | if (len < 40) | |
179 | goto quit; |
|
179 | goto quit; | |
180 |
|
180 | |||
181 | parents = Py_BuildValue("s#s#", str, 20, str + 20, 20); |
|
181 | parents = Py_BuildValue("s#s#", str, 20, str + 20, 20); | |
182 | if (!parents) |
|
182 | if (!parents) | |
183 | goto quit; |
|
183 | goto quit; | |
184 |
|
184 | |||
185 | /* read filenames */ |
|
185 | /* read filenames */ | |
186 | cur = str + 40; |
|
186 | cur = str + 40; | |
187 | end = str + len; |
|
187 | end = str + len; | |
188 |
|
188 | |||
189 | while (cur < end - 17) { |
|
189 | while (cur < end - 17) { | |
190 | /* unpack header */ |
|
190 | /* unpack header */ | |
191 | state = *cur; |
|
191 | state = *cur; | |
192 | memcpy(decode, cur + 1, 16); |
|
192 | memcpy(decode, cur + 1, 16); | |
193 | mode = ntohl(*(uint32_t *)(decode)); |
|
193 | mode = ntohl(*(uint32_t *)(decode)); | |
194 | size = ntohl(*(uint32_t *)(decode + 4)); |
|
194 | size = ntohl(*(uint32_t *)(decode + 4)); | |
195 | mtime = ntohl(*(uint32_t *)(decode + 8)); |
|
195 | mtime = ntohl(*(uint32_t *)(decode + 8)); | |
196 | flen = ntohl(*(uint32_t *)(decode + 12)); |
|
196 | flen = ntohl(*(uint32_t *)(decode + 12)); | |
197 | cur += 17; |
|
197 | cur += 17; | |
198 | if (cur + flen > end) |
|
198 | if (cur + flen > end) | |
199 | goto quit; |
|
199 | goto quit; | |
200 |
|
200 | |||
201 | entry = Py_BuildValue("ciii", state, mode, size, mtime); |
|
201 | entry = Py_BuildValue("ciii", state, mode, size, mtime); | |
202 | PyObject_GC_UnTrack(entry); /* don't waste time with this */ |
|
202 | PyObject_GC_UnTrack(entry); /* don't waste time with this */ | |
203 | if (!entry) |
|
203 | if (!entry) | |
204 | goto quit; |
|
204 | goto quit; | |
205 |
|
205 | |||
206 | cpos = memchr(cur, 0, flen); |
|
206 | cpos = memchr(cur, 0, flen); | |
207 | if (cpos) { |
|
207 | if (cpos) { | |
208 | fname = PyString_FromStringAndSize(cur, cpos - cur); |
|
208 | fname = PyString_FromStringAndSize(cur, cpos - cur); | |
209 | cname = PyString_FromStringAndSize(cpos + 1, |
|
209 | cname = PyString_FromStringAndSize(cpos + 1, | |
210 | flen - (cpos - cur) - 1); |
|
210 | flen - (cpos - cur) - 1); | |
211 | if (!fname || !cname || |
|
211 | if (!fname || !cname || | |
212 | PyDict_SetItem(cmap, fname, cname) == -1 || |
|
212 | PyDict_SetItem(cmap, fname, cname) == -1 || | |
213 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
213 | PyDict_SetItem(dmap, fname, entry) == -1) | |
214 | goto quit; |
|
214 | goto quit; | |
215 | Py_DECREF(cname); |
|
215 | Py_DECREF(cname); | |
216 | } else { |
|
216 | } else { | |
217 | fname = PyString_FromStringAndSize(cur, flen); |
|
217 | fname = PyString_FromStringAndSize(cur, flen); | |
218 | if (!fname || |
|
218 | if (!fname || | |
219 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
219 | PyDict_SetItem(dmap, fname, entry) == -1) | |
220 | goto quit; |
|
220 | goto quit; | |
221 | } |
|
221 | } | |
222 | cur += flen; |
|
222 | cur += flen; | |
223 | Py_DECREF(fname); |
|
223 | Py_DECREF(fname); | |
224 | Py_DECREF(entry); |
|
224 | Py_DECREF(entry); | |
225 | fname = cname = entry = NULL; |
|
225 | fname = cname = entry = NULL; | |
226 | } |
|
226 | } | |
227 |
|
227 | |||
228 | ret = parents; |
|
228 | ret = parents; | |
229 | Py_INCREF(ret); |
|
229 | Py_INCREF(ret); | |
230 | quit: |
|
230 | quit: | |
231 | Py_XDECREF(fname); |
|
231 | Py_XDECREF(fname); | |
232 | Py_XDECREF(cname); |
|
232 | Py_XDECREF(cname); | |
233 | Py_XDECREF(entry); |
|
233 | Py_XDECREF(entry); | |
234 | Py_XDECREF(parents); |
|
234 | Py_XDECREF(parents); | |
235 | return ret; |
|
235 | return ret; | |
236 | } |
|
236 | } | |
237 |
|
237 | |||
238 | const char nullid[20]; |
|
238 | const char nullid[20]; | |
239 | const int nullrev = -1; |
|
239 | const int nullrev = -1; | |
240 |
|
240 | |||
|
241 | /* create an index tuple, insert into the nodemap */ | |||
|
242 | static PyObject * _build_idx_entry(PyObject *nodemap, int n, uint64_t offset_flags, | |||
|
243 | int comp_len, int uncomp_len, int base_rev, | |||
|
244 | int link_rev, int parent_1, int parent_2, | |||
|
245 | const char *c_node_id) | |||
|
246 | { | |||
|
247 | int err; | |||
|
248 | PyObject *entry, *node_id, *n_obj; | |||
|
249 | ||||
|
250 | node_id = PyString_FromStringAndSize(c_node_id, 20); | |||
|
251 | n_obj = PyInt_FromLong(n); | |||
|
252 | if (!node_id || !n_obj) | |||
|
253 | err = -1; | |||
|
254 | else | |||
|
255 | err = PyDict_SetItem(nodemap, node_id, n_obj); | |||
|
256 | ||||
|
257 | Py_XDECREF(n_obj); | |||
|
258 | if (err) | |||
|
259 | goto error_dealloc; | |||
|
260 | ||||
|
261 | entry = Py_BuildValue("LiiiiiiN", offset_flags, comp_len, | |||
|
262 | uncomp_len, base_rev, link_rev, | |||
|
263 | parent_1, parent_2, node_id); | |||
|
264 | if (!entry) | |||
|
265 | goto error_dealloc; | |||
|
266 | PyObject_GC_UnTrack(entry); /* don't waste time with this */ | |||
|
267 | ||||
|
268 | return entry; | |||
|
269 | ||||
|
270 | error_dealloc: | |||
|
271 | Py_XDECREF(node_id); | |||
|
272 | return NULL; | |||
|
273 | } | |||
|
274 | ||||
241 | /* RevlogNG format (all in big endian, data may be inlined): |
|
275 | /* RevlogNG format (all in big endian, data may be inlined): | |
242 | * 6 bytes: offset |
|
276 | * 6 bytes: offset | |
243 | * 2 bytes: flags |
|
277 | * 2 bytes: flags | |
244 | * 4 bytes: compressed length |
|
278 | * 4 bytes: compressed length | |
245 | * 4 bytes: uncompressed length |
|
279 | * 4 bytes: uncompressed length | |
246 | * 4 bytes: base revision |
|
280 | * 4 bytes: base revision | |
247 | * 4 bytes: link revision |
|
281 | * 4 bytes: link revision | |
248 | * 4 bytes: parent 1 revision |
|
282 | * 4 bytes: parent 1 revision | |
249 | * 4 bytes: parent 2 revision |
|
283 | * 4 bytes: parent 2 revision | |
250 | * 32 bytes: nodeid (only 20 bytes used) |
|
284 | * 32 bytes: nodeid (only 20 bytes used) | |
251 | */ |
|
285 | */ | |
252 | static int _parse_index_ng (const char *data, int size, int inlined, |
|
286 | static int _parse_index_ng (const char *data, int size, int inlined, | |
253 | PyObject *index, PyObject *nodemap) |
|
287 | PyObject *index, PyObject *nodemap) | |
254 | { |
|
288 | { | |
255 | PyObject *entry = NULL, *node_id = NULL, *n_obj = NULL; |
|
289 | PyObject *entry; | |
256 | PyObject *nullrev_obj = NULL, *nullid_obj = NULL; |
|
290 | int n = 0, err; | |
|
291 | uint64_t offset_flags; | |||
257 | int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2; |
|
292 | int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2; | |
258 | uint64_t offset_flags; |
|
293 | const char *c_node_id; | |
259 | int n = 0; |
|
|||
260 | const char *end = data + size; |
|
294 | const char *end = data + size; | |
261 |
|
295 | |||
262 | while (data < end) { |
|
296 | while (data < end) { | |
263 | offset_flags = ntohl(*((uint32_t *) (data + 4))); |
|
297 | offset_flags = ntohl(*((uint32_t *) (data + 4))); | |
264 | if (n == 0) /* mask out version number for the first entry */ |
|
298 | if (n == 0) /* mask out version number for the first entry */ | |
265 | offset_flags &= 0xFFFF; |
|
299 | offset_flags &= 0xFFFF; | |
266 | else { |
|
300 | else { | |
267 | uint32_t offset_high = ntohl(*((uint32_t *) data)); |
|
301 | uint32_t offset_high = ntohl(*((uint32_t *) data)); | |
268 | offset_flags |= ((uint64_t) offset_high) << 32; |
|
302 | offset_flags |= ((uint64_t) offset_high) << 32; | |
269 | } |
|
303 | } | |
270 |
|
304 | |||
271 |
|
||||
272 | comp_len = ntohl(*((uint32_t *) (data + 8))); |
|
305 | comp_len = ntohl(*((uint32_t *) (data + 8))); | |
273 | uncomp_len = ntohl(*((uint32_t *) (data + 12))); |
|
306 | uncomp_len = ntohl(*((uint32_t *) (data + 12))); | |
274 | base_rev = ntohl(*((uint32_t *) (data + 16))); |
|
307 | base_rev = ntohl(*((uint32_t *) (data + 16))); | |
275 | link_rev = ntohl(*((uint32_t *) (data + 20))); |
|
308 | link_rev = ntohl(*((uint32_t *) (data + 20))); | |
276 | parent_1 = ntohl(*((uint32_t *) (data + 24))); |
|
309 | parent_1 = ntohl(*((uint32_t *) (data + 24))); | |
277 | parent_2 = ntohl(*((uint32_t *) (data + 28))); |
|
310 | parent_2 = ntohl(*((uint32_t *) (data + 28))); | |
278 | node_id = PyString_FromStringAndSize(data + 32, 20); |
|
311 | c_node_id = data + 32; | |
279 | n_obj = PyInt_FromLong(n); |
|
312 | ||
280 | if (!node_id || !n_obj || |
|
313 | entry = _build_idx_entry(nodemap, n, offset_flags, | |
281 | PyDict_SetItem(nodemap, node_id, n_obj) != 0) |
|
314 | comp_len, uncomp_len, base_rev, | |
282 | goto quit; |
|
315 | link_rev, parent_1, parent_2, | |
283 | Py_DECREF(n_obj); |
|
316 | c_node_id); | |
284 |
|
||||
285 | entry = Py_BuildValue("LiiiiiiN", offset_flags, comp_len, |
|
|||
286 | uncomp_len, base_rev, link_rev, |
|
|||
287 | parent_1, parent_2, node_id); |
|
|||
288 | PyObject_GC_UnTrack(entry); /* don't waste time with this */ |
|
|||
289 | if (!entry) |
|
317 | if (!entry) | |
290 | goto quit; |
|
318 | return 0; | |
291 |
|
319 | |||
292 | /* append to or set value in the index list */ |
|
|||
293 | if (inlined) { |
|
320 | if (inlined) { | |
294 |
|
|
321 | err = PyList_Append(index, entry); | |
295 | goto quit; |
|
|||
296 | Py_DECREF(entry); |
|
322 | Py_DECREF(entry); | |
297 | } else { |
|
323 | if (err) | |
|
324 | return 0; | |||
|
325 | } else | |||
298 | PyList_SET_ITEM(index, n, entry); /* steals reference */ |
|
326 | PyList_SET_ITEM(index, n, entry); /* steals reference */ | |
299 | } |
|
|||
300 |
|
327 | |||
301 | data += 64 + (inlined ? comp_len : 0); |
|
328 | data += 64 + (inlined ? comp_len : 0); | |
302 | n++; |
|
329 | n++; | |
303 | } |
|
330 | } | |
304 | if (data > end) { |
|
331 | if (data > end) { | |
305 | if (!PyErr_Occurred()) |
|
332 | if (!PyErr_Occurred()) | |
306 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
333 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); | |
307 | goto quit; |
|
334 | return 0; | |
308 | } |
|
335 | } | |
309 |
|
336 | |||
310 | /* create the nullid/nullrev entry in the nodemap and the |
|
337 | /* create the nullid/nullrev entry in the nodemap and the | |
311 | * magic nullid entry in the index at [-1] */ |
|
338 | * magic nullid entry in the index at [-1] */ | |
312 | nullid_obj = PyString_FromStringAndSize(nullid, 20); |
|
339 | entry = _build_idx_entry(nodemap, | |
313 | nullrev_obj = PyInt_FromLong(nullrev); |
|
340 | nullrev, 0, 0, 0, -1, -1, -1, -1, nullid); | |
314 | if (!nodemap || !nullid_obj || !nullrev_obj || |
|
|||
315 | PyDict_SetItem(nodemap, nullid_obj, nullrev_obj) != 0) |
|
|||
316 | goto quit; |
|
|||
317 | Py_DECREF(nullrev_obj); |
|
|||
318 |
|
||||
319 | entry = Py_BuildValue("iiiiiiiN", 0, 0, 0, -1, -1, -1, -1, nullid_obj); |
|
|||
320 | PyObject_GC_UnTrack(entry); /* don't waste time with this */ |
|
|||
321 | if (!entry) |
|
341 | if (!entry) | |
322 | goto quit; |
|
342 | return 0; | |
323 | if (inlined) { |
|
343 | if (inlined) { | |
324 |
|
|
344 | err = PyList_Append(index, entry); | |
325 | goto quit; |
|
|||
326 | Py_DECREF(entry); |
|
345 | Py_DECREF(entry); | |
327 | } else { |
|
346 | if (err) | |
|
347 | return 0; | |||
|
348 | } else | |||
328 | PyList_SET_ITEM(index, n, entry); /* steals reference */ |
|
349 | PyList_SET_ITEM(index, n, entry); /* steals reference */ | |
329 | } |
|
|||
330 |
|
350 | |||
331 | return 1; |
|
351 | return 1; | |
332 |
|
||||
333 | quit: |
|
|||
334 | Py_XDECREF(n_obj); |
|
|||
335 | Py_XDECREF(node_id); |
|
|||
336 | Py_XDECREF(entry); |
|
|||
337 | Py_XDECREF(nullrev_obj); |
|
|||
338 | Py_XDECREF(nullid_obj); |
|
|||
339 | return 0; |
|
|||
340 | } |
|
352 | } | |
341 |
|
353 | |||
342 |
|
||||
343 |
|
||||
344 | /* This function parses a index file and returns a Python tuple of the |
|
354 | /* This function parses a index file and returns a Python tuple of the | |
345 | * following format: (index, nodemap, cache) |
|
355 | * following format: (index, nodemap, cache) | |
346 | * |
|
356 | * | |
347 | * index: a list of tuples containing the RevlogNG records |
|
357 | * index: a list of tuples containing the RevlogNG records | |
348 | * nodemap: a dict mapping node ids to indices in the index list |
|
358 | * nodemap: a dict mapping node ids to indices in the index list | |
349 | * cache: if data is inlined, a tuple (index_file_content, 0) else None |
|
359 | * cache: if data is inlined, a tuple (index_file_content, 0) else None | |
350 | */ |
|
360 | */ | |
351 | static PyObject *parse_index(PyObject *self, PyObject *args) |
|
361 | static PyObject *parse_index(PyObject *self, PyObject *args) | |
352 | { |
|
362 | { | |
353 | const char *data; |
|
363 | const char *data; | |
354 | int size, inlined; |
|
364 | int size, inlined; | |
355 | PyObject *rval = NULL, *index = NULL, *nodemap = NULL, *cache = NULL; |
|
365 | PyObject *rval = NULL, *index = NULL, *nodemap = NULL, *cache = NULL; | |
356 | PyObject *data_obj = NULL, *inlined_obj; |
|
366 | PyObject *data_obj = NULL, *inlined_obj; | |
357 |
|
367 | |||
358 | if (!PyArg_ParseTuple(args, "s#O", &data, &size, &inlined_obj)) |
|
368 | if (!PyArg_ParseTuple(args, "s#O", &data, &size, &inlined_obj)) | |
359 | return NULL; |
|
369 | return NULL; | |
360 | inlined = inlined_obj && PyObject_IsTrue(inlined_obj); |
|
370 | inlined = inlined_obj && PyObject_IsTrue(inlined_obj); | |
361 |
|
371 | |||
362 | /* If no data is inlined, we know the size of the index list in |
|
372 | /* If no data is inlined, we know the size of the index list in | |
363 | * advance: size divided by size of one one revlog record (64 bytes) |
|
373 | * advance: size divided by size of one one revlog record (64 bytes) | |
364 | * plus one for the nullid */ |
|
374 | * plus one for the nullid */ | |
365 | index = inlined ? PyList_New(0) : PyList_New(size / 64 + 1); |
|
375 | index = inlined ? PyList_New(0) : PyList_New(size / 64 + 1); | |
366 | if (!index) |
|
376 | if (!index) | |
367 | goto quit; |
|
377 | goto quit; | |
368 |
|
378 | |||
369 | nodemap = PyDict_New(); |
|
379 | nodemap = PyDict_New(); | |
|
380 | if (!nodemap) | |||
|
381 | goto quit; | |||
370 |
|
382 | |||
371 | /* set up the cache return value */ |
|
383 | /* set up the cache return value */ | |
372 | if (inlined) { |
|
384 | if (inlined) { | |
373 | /* Note that the reference to data_obj is only borrowed */ |
|
385 | /* Note that the reference to data_obj is only borrowed */ | |
374 | data_obj = PyTuple_GET_ITEM(args, 0); |
|
386 | data_obj = PyTuple_GET_ITEM(args, 0); | |
375 | cache = Py_BuildValue("iO", 0, data_obj); |
|
387 | cache = Py_BuildValue("iO", 0, data_obj); | |
376 | if (!cache) |
|
388 | if (!cache) | |
377 | goto quit; |
|
389 | goto quit; | |
378 | } else { |
|
390 | } else { | |
379 | cache = Py_None; |
|
391 | cache = Py_None; | |
380 | Py_INCREF(Py_None); |
|
392 | Py_INCREF(Py_None); | |
381 | } |
|
393 | } | |
382 |
|
394 | |||
383 | /* actually populate the index and the nodemap with data */ |
|
395 | /* actually populate the index and the nodemap with data */ | |
384 | if (!_parse_index_ng (data, size, inlined, index, nodemap)) |
|
396 | if (!_parse_index_ng (data, size, inlined, index, nodemap)) | |
385 | goto quit; |
|
397 | goto quit; | |
386 |
|
398 | |||
387 | rval = Py_BuildValue("NNN", index, nodemap, cache); |
|
399 | rval = Py_BuildValue("NNN", index, nodemap, cache); | |
388 | if (!rval) |
|
400 | if (!rval) | |
389 | goto quit; |
|
401 | goto quit; | |
390 | return rval; |
|
402 | return rval; | |
391 |
|
403 | |||
392 | quit: |
|
404 | quit: | |
393 | Py_XDECREF(index); |
|
405 | Py_XDECREF(index); | |
394 | Py_XDECREF(nodemap); |
|
406 | Py_XDECREF(nodemap); | |
395 | Py_XDECREF(cache); |
|
407 | Py_XDECREF(cache); | |
396 | Py_XDECREF(rval); |
|
408 | Py_XDECREF(rval); | |
397 | Py_XDECREF(data_obj); |
|
409 | Py_XDECREF(data_obj); | |
398 | return NULL; |
|
410 | return NULL; | |
399 | } |
|
411 | } | |
400 |
|
412 | |||
401 |
|
413 | |||
402 | static char parsers_doc[] = "Efficient content parsing."; |
|
414 | static char parsers_doc[] = "Efficient content parsing."; | |
403 |
|
415 | |||
404 | static PyMethodDef methods[] = { |
|
416 | static PyMethodDef methods[] = { | |
405 | {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"}, |
|
417 | {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"}, | |
406 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, |
|
418 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, | |
407 | {"parse_index", parse_index, METH_VARARGS, "parse a revlog index\n"}, |
|
419 | {"parse_index", parse_index, METH_VARARGS, "parse a revlog index\n"}, | |
408 | {NULL, NULL} |
|
420 | {NULL, NULL} | |
409 | }; |
|
421 | }; | |
410 |
|
422 | |||
411 | PyMODINIT_FUNC initparsers(void) |
|
423 | PyMODINIT_FUNC initparsers(void) | |
412 | { |
|
424 | { | |
413 | Py_InitModule3("parsers", methods, parsers_doc); |
|
425 | Py_InitModule3("parsers", methods, parsers_doc); | |
414 | } |
|
426 | } |
General Comments 0
You need to be logged in to leave comments.
Login now