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