d9699ec6cb4a72e52873929f679048e32d176819
[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, *start, *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         start = text;
56         end = text + len;
57
58         while (text < end) {
59                 long mode;
60                 mode = strtol(text, &text, 8);
61
62                 if (*text != ' ') {
63                         PyErr_SetString(PyExc_ValueError, "Expected space");
64                         Py_DECREF(ret);
65                         return NULL;
66                 }
67
68                 text++;
69
70                 namelen = strnlen(text, len - (text - start));
71
72                 name = PyString_FromStringAndSize(text, namelen);
73                 if (name == NULL) {
74                         Py_DECREF(ret);
75                         return NULL;
76                 }
77
78                 if (text + namelen + 20 >= end) {
79                         PyErr_SetString(PyExc_ValueError, "SHA truncated");
80                         Py_DECREF(ret);
81                         Py_DECREF(name);
82                         return NULL;
83                 }
84
85                 item = Py_BuildValue("(NlN)", name, mode,
86                                                          sha_to_pyhex((unsigned char *)text+namelen+1));
87                 if (item == NULL) {
88                         Py_DECREF(ret);
89                         Py_DECREF(name);
90                         return NULL;
91                 }
92                 if (PyList_Append(ret, item) == -1) {
93                         Py_DECREF(ret);
94                         Py_DECREF(item);
95                         return NULL;
96                 }
97                 Py_DECREF(item);
98
99                 text += namelen+21;
100         }
101
102         return ret;
103 }
104
105 struct tree_item {
106         const char *name;
107         int mode;
108         PyObject *tuple;
109 };
110
111 int cmp_tree_item(const void *_a, const void *_b)
112 {
113         const struct tree_item *a = _a, *b = _b;
114         const char *remain_a, *remain_b;
115         int ret, common;
116         if (strlen(a->name) > strlen(b->name)) {
117                 common = strlen(b->name);
118                 remain_a = a->name + common;
119                 remain_b = (S_ISDIR(b->mode)?"/":"");
120         } else if (strlen(b->name) > strlen(a->name)) { 
121                 common = strlen(a->name);
122                 remain_a = (S_ISDIR(a->mode)?"/":"");
123                 remain_b = b->name + common;
124         } else { /* strlen(a->name) == strlen(b->name) */
125                 common = 0;
126                 remain_a = a->name;
127                 remain_b = b->name;
128         }
129         ret = strncmp(a->name, b->name, common);
130         if (ret != 0)
131                 return ret;
132         return strcmp(remain_a, remain_b);
133 }
134
135 static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
136 {
137         struct tree_item *qsort_entries;
138         int num, i;
139         PyObject *ret;
140         Py_ssize_t pos = 0; 
141         PyObject *key, *value;
142
143         if (!PyDict_Check(entries)) {
144                 PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
145                 return NULL;
146         }
147
148         num = PyDict_Size(entries);
149         qsort_entries = malloc(num * sizeof(struct tree_item));
150         if (qsort_entries == NULL) {
151                 PyErr_NoMemory();
152                 return NULL;
153         }
154
155         i = 0;
156         while (PyDict_Next(entries, &pos, &key, &value)) {
157                 PyObject *py_mode, *py_sha;
158                 
159                 if (PyTuple_Size(value) != 2) {
160                         PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
161                         free(qsort_entries);
162                         return NULL;
163                 }
164
165                 py_mode = PyTuple_GET_ITEM(value, 0);
166                 py_sha = PyTuple_GET_ITEM(value, 1);
167                 qsort_entries[i].tuple = Py_BuildValue("(OOO)", key, py_mode, py_sha);
168                 if (!PyString_CheckExact(key)) {
169                         PyErr_SetString(PyExc_TypeError, "Name is not a string");
170                         free(qsort_entries);
171                         return NULL;
172                 }
173                 qsort_entries[i].name = PyString_AS_STRING(key);
174                 if (!PyInt_CheckExact(py_mode)) {
175                         PyErr_SetString(PyExc_TypeError, "Mode is not an int");
176                         free(qsort_entries);
177                         return NULL;
178                 }
179                 qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
180                 i++;
181         }
182
183         qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);
184
185         ret = PyList_New(num);
186         if (ret == NULL) {
187                 free(qsort_entries);
188                 PyErr_NoMemory();
189                 return NULL;
190         }
191
192         for (i = 0; i < num; i++) {
193                 PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
194         }
195
196         free(qsort_entries);
197
198         return ret;
199 }
200
201 static PyMethodDef py_objects_methods[] = {
202         { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
203         { "sorted_tree_items", (PyCFunction)py_sorted_tree_items, METH_O, NULL },
204         { NULL, NULL, 0, NULL }
205 };
206
207 void init_objects(void)
208 {
209         PyObject *m;
210
211         m = Py_InitModule3("_objects", py_objects_methods, NULL);
212         if (m == NULL)
213                 return;
214 }