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