c16e5ccd347d73d78f65f70ee45a6f7b6de044b5
[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 #if (PY_VERSION_HEX < 0x02050000)
25 typedef int Py_ssize_t;
26 #endif
27
28 #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
29
30 static PyObject *sha_to_pyhex(const unsigned char *sha)
31 {
32         char hexsha[41];
33         int i;
34         for (i = 0; i < 20; i++) {
35                 hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
36                 hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
37         }
38
39         return PyString_FromStringAndSize(hexsha, 40);
40 }
41
42 static PyObject *py_parse_tree(PyObject *self, PyObject *args)
43 {
44         char *text, *start, *end;
45         int len, namelen;
46         PyObject *ret, *item, *name;
47
48         if (!PyArg_ParseTuple(args, "s#", &text, &len))
49                 return NULL;
50
51         /* TODO: currently this returns a list; if memory usage is a concern,
52         * consider rewriting as a custom iterator object */
53         ret = PyList_New(0);
54
55         if (ret == NULL) {
56                 return NULL;
57         }
58
59         start = text;
60         end = text + len;
61
62         while (text < end) {
63                 long mode;
64                 mode = strtol(text, &text, 8);
65
66                 if (*text != ' ') {
67                         PyErr_SetString(PyExc_ValueError, "Expected space");
68                         Py_DECREF(ret);
69                         return NULL;
70                 }
71
72                 text++;
73
74                 namelen = strnlen(text, len - (text - start));
75
76                 name = PyString_FromStringAndSize(text, namelen);
77                 if (name == NULL) {
78                         Py_DECREF(ret);
79                         return NULL;
80                 }
81
82                 if (text + namelen + 20 >= end) {
83                         PyErr_SetString(PyExc_ValueError, "SHA truncated");
84                         Py_DECREF(ret);
85                         Py_DECREF(name);
86                         return NULL;
87                 }
88
89                 item = Py_BuildValue("(NlN)", name, mode,
90                                                          sha_to_pyhex((unsigned char *)text+namelen+1));
91                 if (item == NULL) {
92                         Py_DECREF(ret);
93                         Py_DECREF(name);
94                         return NULL;
95                 }
96                 if (PyList_Append(ret, item) == -1) {
97                         Py_DECREF(ret);
98                         Py_DECREF(item);
99                         return NULL;
100                 }
101                 Py_DECREF(item);
102
103                 text += namelen+21;
104         }
105
106         return ret;
107 }
108
109 struct tree_item {
110         const char *name;
111         int mode;
112         PyObject *tuple;
113 };
114
115 int cmp_tree_item(const void *_a, const void *_b)
116 {
117         const struct tree_item *a = _a, *b = _b;
118         const char *remain_a, *remain_b;
119         int ret, common;
120         if (strlen(a->name) > strlen(b->name)) {
121                 common = strlen(b->name);
122                 remain_a = a->name + common;
123                 remain_b = (S_ISDIR(b->mode)?"/":"");
124         } else if (strlen(b->name) > strlen(a->name)) {
125                 common = strlen(a->name);
126                 remain_a = (S_ISDIR(a->mode)?"/":"");
127                 remain_b = b->name + common;
128         } else { /* strlen(a->name) == strlen(b->name) */
129                 common = 0;
130                 remain_a = a->name;
131                 remain_b = b->name;
132         }
133         ret = strncmp(a->name, b->name, common);
134         if (ret != 0)
135                 return ret;
136         return strcmp(remain_a, remain_b);
137 }
138
139 static void free_tree_items(struct tree_item *items, int num) {
140         int i;
141         for (i = 0; i < num; i++) {
142                 Py_DECREF(items[i].tuple);
143         }
144         free(items);
145 }
146
147 static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
148 {
149         struct tree_item *qsort_entries;
150         int num, i;
151         PyObject *ret;
152         Py_ssize_t pos = 0;
153         PyObject *key, *value;
154
155         if (!PyDict_Check(entries)) {
156                 PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
157                 return NULL;
158         }
159
160         num = PyDict_Size(entries);
161         qsort_entries = malloc(num * sizeof(struct tree_item));
162         if (qsort_entries == NULL) {
163                 PyErr_NoMemory();
164                 return NULL;
165         }
166
167         i = 0;
168         while (PyDict_Next(entries, &pos, &key, &value)) {
169                 PyObject *py_mode, *py_int_mode, *py_sha;
170
171                 if (!PyString_Check(key)) {
172                         PyErr_SetString(PyExc_TypeError, "Name is not a string");
173                         free_tree_items(qsort_entries, i);
174                         return NULL;
175                 }
176
177                 if (PyTuple_Size(value) != 2) {
178                         PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
179                         free_tree_items(qsort_entries, i);
180                         return NULL;
181                 }
182
183                 py_mode = PyTuple_GET_ITEM(value, 0);
184                 py_int_mode = PyNumber_Int(py_mode);
185                 if (!py_int_mode) {
186                         PyErr_SetString(PyExc_TypeError, "Mode is not an integral type");
187                         free_tree_items(qsort_entries, i);
188                         return NULL;
189                 }
190
191                 py_sha = PyTuple_GET_ITEM(value, 1);
192                 if (!PyString_Check(py_sha)) {
193                         PyErr_SetString(PyExc_TypeError, "SHA is not a string");
194                         Py_DECREF(py_int_mode);
195                         free_tree_items(qsort_entries, i);
196                         return NULL;
197                 }
198                 qsort_entries[i].name = PyString_AS_STRING(key);
199                 qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
200                 qsort_entries[i].tuple = PyTuple_Pack(3, key, py_int_mode, py_sha);
201                 Py_DECREF(py_int_mode);
202                 i++;
203         }
204
205         qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);
206
207         ret = PyList_New(num);
208         if (ret == NULL) {
209                 free_tree_items(qsort_entries, i);
210                 PyErr_NoMemory();
211                 return NULL;
212         }
213
214         for (i = 0; i < num; i++) {
215                 PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
216         }
217
218         free(qsort_entries);
219
220         return ret;
221 }
222
223 static PyMethodDef py_objects_methods[] = {
224         { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
225         { "sorted_tree_items", (PyCFunction)py_sorted_tree_items, METH_O, NULL },
226         { NULL, NULL, 0, NULL }
227 };
228
229 void init_objects(void)
230 {
231         PyObject *m;
232
233         m = Py_InitModule3("_objects", py_objects_methods, NULL);
234         if (m == NULL)
235                 return;
236 }