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