Add check() methods to object classes for consistency checking.
[jelmer/dulwich-libgit2.git] / dulwich / _objects.c
1 /* 
2  * Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; version 2
7  * of the License or (at your option) a later version of the License.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA  02110-1301, USA.
18  */
19
20 #include <Python.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23
24 #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
25
26 static PyObject *sha_to_pyhex(const unsigned char *sha)
27 {
28         char hexsha[41];
29         int i;
30         for (i = 0; i < 20; i++) {
31                 hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
32                 hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
33         }
34         
35         return PyString_FromStringAndSize(hexsha, 40);
36 }
37
38 static PyObject *py_parse_tree(PyObject *self, PyObject *args)
39 {
40         char *text, *end;
41         int len, namelen;
42         PyObject *ret, *item, *name;
43
44         if (!PyArg_ParseTuple(args, "s#", &text, &len))
45                 return NULL;
46
47         /* TODO: currently this returns a list; if memory usage is a concern,
48         * consider rewriting as a custom iterator object */
49         ret = PyList_New(0);
50
51         if (ret == NULL) {
52                 return NULL;
53         }
54
55         end = text + len;
56
57         while (text < end) {
58                 long mode;
59                 mode = strtol(text, &text, 8);
60
61                 if (*text != ' ') {
62                         PyErr_SetString(PyExc_RuntimeError, "Expected space");
63                         Py_DECREF(ret);
64                         return NULL;
65                 }
66
67                 text++;
68
69                 namelen = strlen(text);
70
71                 name = PyString_FromStringAndSize(text, namelen);
72                 if (name == NULL) {
73                         Py_DECREF(ret);
74                         return NULL;
75                 }
76
77                 if (text + namelen + 20 >= end) {
78                         PyErr_SetString(PyExc_RuntimeError, "SHA truncated");
79                         Py_DECREF(ret);
80                         Py_DECREF(name);
81                         return NULL;
82                 }
83
84                 item = Py_BuildValue("(NlN)", name, mode,
85                                                          sha_to_pyhex((unsigned char *)text+namelen+1));
86                 if (item == NULL) {
87                         Py_DECREF(ret);
88                         Py_DECREF(name);
89                         return NULL;
90                 }
91                 if (PyList_Append(ret, item) == -1) {
92                         Py_DECREF(ret);
93                         Py_DECREF(item);
94                         return NULL;
95                 }
96                 Py_DECREF(item);
97
98                 text += namelen+21;
99         }
100
101         return ret;
102 }
103
104 struct tree_item {
105         const char *name;
106         int mode;
107         PyObject *tuple;
108 };
109
110 int cmp_tree_item(const void *_a, const void *_b)
111 {
112         const struct tree_item *a = _a, *b = _b;
113         const char *remain_a, *remain_b;
114         int ret, common;
115         if (strlen(a->name) > strlen(b->name)) {
116                 common = strlen(b->name);
117                 remain_a = a->name + common;
118                 remain_b = (S_ISDIR(b->mode)?"/":"");
119         } else if (strlen(b->name) > strlen(a->name)) { 
120                 common = strlen(a->name);
121                 remain_a = (S_ISDIR(a->mode)?"/":"");
122                 remain_b = b->name + common;
123         } else { /* strlen(a->name) == strlen(b->name) */
124                 common = 0;
125                 remain_a = a->name;
126                 remain_b = b->name;
127         }
128         ret = strncmp(a->name, b->name, common);
129         if (ret != 0)
130                 return ret;
131         return strcmp(remain_a, remain_b);
132 }
133
134 static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
135 {
136         struct tree_item *qsort_entries;
137         int num, i;
138         PyObject *ret;
139         Py_ssize_t pos = 0; 
140         PyObject *key, *value;
141
142         if (!PyDict_Check(entries)) {
143                 PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
144                 return NULL;
145         }
146
147         num = PyDict_Size(entries);
148         qsort_entries = malloc(num * sizeof(struct tree_item));
149         if (qsort_entries == NULL) {
150                 PyErr_NoMemory();
151                 return NULL;
152         }
153
154         i = 0;
155         while (PyDict_Next(entries, &pos, &key, &value)) {
156                 PyObject *py_mode, *py_sha;
157                 
158                 if (PyTuple_Size(value) != 2) {
159                         PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
160                         free(qsort_entries);
161                         return NULL;
162                 }
163
164                 py_mode = PyTuple_GET_ITEM(value, 0);
165                 py_sha = PyTuple_GET_ITEM(value, 1);
166                 qsort_entries[i].tuple = Py_BuildValue("(OOO)", key, py_mode, py_sha);
167                 if (!PyString_CheckExact(key)) {
168                         PyErr_SetString(PyExc_TypeError, "Name is not a string");
169                         free(qsort_entries);
170                         return NULL;
171                 }
172                 qsort_entries[i].name = PyString_AS_STRING(key);
173                 if (!PyInt_CheckExact(py_mode)) {
174                         PyErr_SetString(PyExc_TypeError, "Mode is not an int");
175                         free(qsort_entries);
176                         return NULL;
177                 }
178                 qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
179                 i++;
180         }
181
182         qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);
183
184         ret = PyList_New(num);
185         if (ret == NULL) {
186                 free(qsort_entries);
187                 PyErr_NoMemory();
188                 return NULL;
189         }
190
191         for (i = 0; i < num; i++) {
192                 PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
193         }
194
195         free(qsort_entries);
196
197         return ret;
198 }
199
200 static PyMethodDef py_objects_methods[] = {
201         { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
202         { "sorted_tree_items", (PyCFunction)py_sorted_tree_items, METH_O, NULL },
203         { NULL, NULL, 0, NULL }
204 };
205
206 void init_objects(void)
207 {
208         PyObject *m;
209
210         m = Py_InitModule3("_objects", py_objects_methods, NULL);
211         if (m == NULL)
212                 return;
213 }