_objects.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include <Python.h>
  20. #include <stdlib.h>
  21. #include <sys/stat.h>
  22. #if (PY_VERSION_HEX < 0x02050000)
  23. typedef int Py_ssize_t;
  24. #endif
  25. #if defined(__MINGW32_VERSION) || defined(__APPLE__)
  26. size_t rep_strnlen(char *text, size_t maxlen);
  27. size_t rep_strnlen(char *text, size_t maxlen)
  28. {
  29. const char *last = memchr(text, '\0', maxlen);
  30. return last ? (size_t) (last - text) : maxlen;
  31. }
  32. #define strnlen rep_strnlen
  33. #endif
  34. #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
  35. static PyObject *tree_entry_cls;
  36. static PyObject *object_format_exception_cls;
  37. static PyObject *sha_to_pyhex(const unsigned char *sha)
  38. {
  39. char hexsha[41];
  40. int i;
  41. for (i = 0; i < 20; i++) {
  42. hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
  43. hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
  44. }
  45. return PyString_FromStringAndSize(hexsha, 40);
  46. }
  47. static PyObject *py_parse_tree(PyObject *self, PyObject *args, PyObject *kw)
  48. {
  49. char *text, *start, *end;
  50. int len, namelen, strict;
  51. PyObject *ret, *item, *name, *sha, *py_strict = NULL;
  52. static char *kwlist[] = {"text", "strict", NULL};
  53. if (!PyArg_ParseTupleAndKeywords(args, kw, "s#|O", kwlist,
  54. &text, &len, &py_strict))
  55. return NULL;
  56. strict = py_strict ? PyObject_IsTrue(py_strict) : 0;
  57. /* TODO: currently this returns a list; if memory usage is a concern,
  58. * consider rewriting as a custom iterator object */
  59. ret = PyList_New(0);
  60. if (ret == NULL) {
  61. return NULL;
  62. }
  63. start = text;
  64. end = text + len;
  65. while (text < end) {
  66. long mode;
  67. if (strict && text[0] == '0') {
  68. PyErr_SetString(object_format_exception_cls,
  69. "Illegal leading zero on mode");
  70. Py_DECREF(ret);
  71. return NULL;
  72. }
  73. mode = strtol(text, &text, 8);
  74. if (*text != ' ') {
  75. PyErr_SetString(PyExc_ValueError, "Expected space");
  76. Py_DECREF(ret);
  77. return NULL;
  78. }
  79. text++;
  80. namelen = strnlen(text, len - (text - start));
  81. name = PyString_FromStringAndSize(text, namelen);
  82. if (name == NULL) {
  83. Py_DECREF(ret);
  84. return NULL;
  85. }
  86. if (text + namelen + 20 >= end) {
  87. PyErr_SetString(PyExc_ValueError, "SHA truncated");
  88. Py_DECREF(ret);
  89. Py_DECREF(name);
  90. return NULL;
  91. }
  92. sha = sha_to_pyhex((unsigned char *)text+namelen+1);
  93. if (sha == NULL) {
  94. Py_DECREF(ret);
  95. Py_DECREF(name);
  96. return NULL;
  97. }
  98. item = Py_BuildValue("(NlN)", name, mode, sha);
  99. if (item == NULL) {
  100. Py_DECREF(ret);
  101. Py_DECREF(sha);
  102. Py_DECREF(name);
  103. return NULL;
  104. }
  105. if (PyList_Append(ret, item) == -1) {
  106. Py_DECREF(ret);
  107. Py_DECREF(item);
  108. return NULL;
  109. }
  110. Py_DECREF(item);
  111. text += namelen+21;
  112. }
  113. return ret;
  114. }
  115. struct tree_item {
  116. const char *name;
  117. int mode;
  118. PyObject *tuple;
  119. };
  120. int cmp_tree_item(const void *_a, const void *_b)
  121. {
  122. const struct tree_item *a = _a, *b = _b;
  123. const char *remain_a, *remain_b;
  124. int ret, common;
  125. if (strlen(a->name) > strlen(b->name)) {
  126. common = strlen(b->name);
  127. remain_a = a->name + common;
  128. remain_b = (S_ISDIR(b->mode)?"/":"");
  129. } else if (strlen(b->name) > strlen(a->name)) {
  130. common = strlen(a->name);
  131. remain_a = (S_ISDIR(a->mode)?"/":"");
  132. remain_b = b->name + common;
  133. } else { /* strlen(a->name) == strlen(b->name) */
  134. common = 0;
  135. remain_a = a->name;
  136. remain_b = b->name;
  137. }
  138. ret = strncmp(a->name, b->name, common);
  139. if (ret != 0)
  140. return ret;
  141. return strcmp(remain_a, remain_b);
  142. }
  143. int cmp_tree_item_name_order(const void *_a, const void *_b) {
  144. const struct tree_item *a = _a, *b = _b;
  145. return strcmp(a->name, b->name);
  146. }
  147. static PyObject *py_sorted_tree_items(PyObject *self, PyObject *args)
  148. {
  149. struct tree_item *qsort_entries = NULL;
  150. int name_order, num_entries, n = 0, i;
  151. PyObject *entries, *py_name_order, *ret, *key, *value, *py_mode, *py_sha;
  152. Py_ssize_t pos = 0;
  153. int (*cmp)(const void *, const void *);
  154. if (!PyArg_ParseTuple(args, "OO", &entries, &py_name_order))
  155. goto error;
  156. if (!PyDict_Check(entries)) {
  157. PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
  158. goto error;
  159. }
  160. name_order = PyObject_IsTrue(py_name_order);
  161. if (name_order == -1)
  162. goto error;
  163. cmp = name_order ? cmp_tree_item_name_order : cmp_tree_item;
  164. num_entries = PyDict_Size(entries);
  165. if (PyErr_Occurred())
  166. goto error;
  167. qsort_entries = PyMem_New(struct tree_item, num_entries);
  168. if (!qsort_entries) {
  169. PyErr_NoMemory();
  170. goto error;
  171. }
  172. while (PyDict_Next(entries, &pos, &key, &value)) {
  173. if (!PyString_Check(key)) {
  174. PyErr_SetString(PyExc_TypeError, "Name is not a string");
  175. goto error;
  176. }
  177. if (PyTuple_Size(value) != 2) {
  178. PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
  179. goto error;
  180. }
  181. py_mode = PyTuple_GET_ITEM(value, 0);
  182. if (!PyInt_Check(py_mode) && !PyLong_Check(py_mode)) {
  183. PyErr_SetString(PyExc_TypeError, "Mode is not an integral type");
  184. goto error;
  185. }
  186. py_sha = PyTuple_GET_ITEM(value, 1);
  187. if (!PyString_Check(py_sha)) {
  188. PyErr_SetString(PyExc_TypeError, "SHA is not a string");
  189. goto error;
  190. }
  191. qsort_entries[n].name = PyString_AS_STRING(key);
  192. qsort_entries[n].mode = PyInt_AsLong(py_mode);
  193. qsort_entries[n].tuple = PyObject_CallFunctionObjArgs(
  194. tree_entry_cls, key, py_mode, py_sha, NULL);
  195. if (qsort_entries[n].tuple == NULL)
  196. goto error;
  197. n++;
  198. }
  199. qsort(qsort_entries, num_entries, sizeof(struct tree_item), cmp);
  200. ret = PyList_New(num_entries);
  201. if (ret == NULL) {
  202. PyErr_NoMemory();
  203. goto error;
  204. }
  205. for (i = 0; i < num_entries; i++) {
  206. PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
  207. }
  208. PyMem_Free(qsort_entries);
  209. return ret;
  210. error:
  211. for (i = 0; i < n; i++) {
  212. Py_XDECREF(qsort_entries[i].tuple);
  213. }
  214. PyMem_Free(qsort_entries);
  215. return NULL;
  216. }
  217. static PyMethodDef py_objects_methods[] = {
  218. { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS | METH_KEYWORDS,
  219. NULL },
  220. { "sorted_tree_items", py_sorted_tree_items, METH_VARARGS, NULL },
  221. { NULL, NULL, 0, NULL }
  222. };
  223. PyMODINIT_FUNC
  224. init_objects(void)
  225. {
  226. PyObject *m, *objects_mod, *errors_mod;
  227. m = Py_InitModule3("_objects", py_objects_methods, NULL);
  228. if (m == NULL) {
  229. #if PY_MAJOR_VERSION < 3
  230. return;
  231. #else
  232. return NULL;
  233. #endif
  234. }
  235. errors_mod = PyImport_ImportModule("dulwich.errors");
  236. if (errors_mod == NULL) {
  237. #if PY_MAJOR_VERSION < 3
  238. return;
  239. #else
  240. return NULL;
  241. #endif
  242. }
  243. object_format_exception_cls = PyObject_GetAttrString(
  244. errors_mod, "ObjectFormatException");
  245. Py_DECREF(errors_mod);
  246. if (object_format_exception_cls == NULL) {
  247. #if PY_MAJOR_VERSION < 3
  248. return;
  249. #else
  250. return NULL;
  251. #endif
  252. }
  253. /* This is a circular import but should be safe since this module is
  254. * imported at at the very bottom of objects.py. */
  255. objects_mod = PyImport_ImportModule("dulwich.objects");
  256. if (objects_mod == NULL) {
  257. #if PY_MAJOR_VERSION < 3
  258. return;
  259. #else
  260. return NULL;
  261. #endif
  262. }
  263. tree_entry_cls = PyObject_GetAttrString(objects_mod, "TreeEntry");
  264. Py_DECREF(objects_mod);
  265. if (tree_entry_cls == NULL) {
  266. #if PY_MAJOR_VERSION < 3
  267. return;
  268. #else
  269. return NULL;
  270. #endif
  271. }
  272. }