_objects.c 7.1 KB

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