2
0

_objects.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
  3. *
  4. * Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. * General Public License as public by the Free Software Foundation; version 2.0
  6. * or (at your option) any later version. You can redistribute it and/or
  7. * modify it under the terms of either of these two licenses.
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. * You should have received a copy of the licenses; if not, see
  16. * <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. * and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. * License, Version 2.0.
  19. */
  20. #include <Python.h>
  21. #include <stdlib.h>
  22. #include <sys/stat.h>
  23. #if (PY_VERSION_HEX < 0x02050000)
  24. typedef int Py_ssize_t;
  25. #endif
  26. #if PY_MAJOR_VERSION >= 3
  27. #define PyInt_Check(obj) 0
  28. #define PyInt_CheckExact(obj) 0
  29. #define PyInt_AsLong PyLong_AsLong
  30. #define PyString_AS_STRING PyBytes_AS_STRING
  31. #define PyString_Check PyBytes_Check
  32. #define PyString_FromStringAndSize PyBytes_FromStringAndSize
  33. #endif
  34. #if defined(__MINGW32_VERSION) || defined(__APPLE__)
  35. size_t rep_strnlen(char *text, size_t maxlen);
  36. size_t rep_strnlen(char *text, size_t maxlen)
  37. {
  38. const char *last = memchr(text, '\0', maxlen);
  39. return last ? (size_t) (last - text) : maxlen;
  40. }
  41. #define strnlen rep_strnlen
  42. #endif
  43. #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
  44. static PyObject *tree_entry_cls;
  45. static PyObject *object_format_exception_cls;
  46. static PyObject *sha_to_pyhex(const unsigned char *sha)
  47. {
  48. char hexsha[41];
  49. int i;
  50. for (i = 0; i < 20; i++) {
  51. hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
  52. hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
  53. }
  54. return PyString_FromStringAndSize(hexsha, 40);
  55. }
  56. static PyObject *py_parse_tree(PyObject *self, PyObject *args, PyObject *kw)
  57. {
  58. char *text, *start, *end;
  59. int len, namelen, strict;
  60. PyObject *ret, *item, *name, *sha, *py_strict = NULL;
  61. static char *kwlist[] = {"text", "strict", NULL};
  62. #if PY_MAJOR_VERSION >= 3
  63. if (!PyArg_ParseTupleAndKeywords(args, kw, "y#|O", kwlist,
  64. &text, &len, &py_strict))
  65. #else
  66. if (!PyArg_ParseTupleAndKeywords(args, kw, "s#|O", kwlist,
  67. &text, &len, &py_strict))
  68. #endif
  69. return NULL;
  70. strict = py_strict ? PyObject_IsTrue(py_strict) : 0;
  71. /* TODO: currently this returns a list; if memory usage is a concern,
  72. * consider rewriting as a custom iterator object */
  73. ret = PyList_New(0);
  74. if (ret == NULL) {
  75. return NULL;
  76. }
  77. start = text;
  78. end = text + len;
  79. while (text < end) {
  80. long mode;
  81. if (strict && text[0] == '0') {
  82. PyErr_SetString(object_format_exception_cls,
  83. "Illegal leading zero on mode");
  84. Py_DECREF(ret);
  85. return NULL;
  86. }
  87. mode = strtol(text, &text, 8);
  88. if (*text != ' ') {
  89. PyErr_SetString(PyExc_ValueError, "Expected space");
  90. Py_DECREF(ret);
  91. return NULL;
  92. }
  93. text++;
  94. namelen = strnlen(text, len - (text - start));
  95. name = PyString_FromStringAndSize(text, namelen);
  96. if (name == NULL) {
  97. Py_DECREF(ret);
  98. return NULL;
  99. }
  100. if (text + namelen + 20 >= end) {
  101. PyErr_SetString(PyExc_ValueError, "SHA truncated");
  102. Py_DECREF(ret);
  103. Py_DECREF(name);
  104. return NULL;
  105. }
  106. sha = sha_to_pyhex((unsigned char *)text+namelen+1);
  107. if (sha == NULL) {
  108. Py_DECREF(ret);
  109. Py_DECREF(name);
  110. return NULL;
  111. }
  112. item = Py_BuildValue("(NlN)", name, mode, sha);
  113. if (item == NULL) {
  114. Py_DECREF(ret);
  115. Py_DECREF(sha);
  116. Py_DECREF(name);
  117. return NULL;
  118. }
  119. if (PyList_Append(ret, item) == -1) {
  120. Py_DECREF(ret);
  121. Py_DECREF(item);
  122. return NULL;
  123. }
  124. Py_DECREF(item);
  125. text += namelen+21;
  126. }
  127. return ret;
  128. }
  129. struct tree_item {
  130. const char *name;
  131. int mode;
  132. PyObject *tuple;
  133. };
  134. int cmp_tree_item(const void *_a, const void *_b)
  135. {
  136. const struct tree_item *a = _a, *b = _b;
  137. const char *remain_a, *remain_b;
  138. int ret, common;
  139. if (strlen(a->name) > strlen(b->name)) {
  140. common = strlen(b->name);
  141. remain_a = a->name + common;
  142. remain_b = (S_ISDIR(b->mode)?"/":"");
  143. } else if (strlen(b->name) > strlen(a->name)) {
  144. common = strlen(a->name);
  145. remain_a = (S_ISDIR(a->mode)?"/":"");
  146. remain_b = b->name + common;
  147. } else { /* strlen(a->name) == strlen(b->name) */
  148. common = 0;
  149. remain_a = a->name;
  150. remain_b = b->name;
  151. }
  152. ret = strncmp(a->name, b->name, common);
  153. if (ret != 0)
  154. return ret;
  155. return strcmp(remain_a, remain_b);
  156. }
  157. int cmp_tree_item_name_order(const void *_a, const void *_b) {
  158. const struct tree_item *a = _a, *b = _b;
  159. return strcmp(a->name, b->name);
  160. }
  161. static PyObject *py_sorted_tree_items(PyObject *self, PyObject *args)
  162. {
  163. struct tree_item *qsort_entries = NULL;
  164. int name_order, num_entries, n = 0, i;
  165. PyObject *entries, *py_name_order, *ret, *key, *value, *py_mode, *py_sha;
  166. Py_ssize_t pos = 0;
  167. int (*cmp)(const void *, const void *);
  168. if (!PyArg_ParseTuple(args, "OO", &entries, &py_name_order))
  169. goto error;
  170. if (!PyDict_Check(entries)) {
  171. PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
  172. goto error;
  173. }
  174. name_order = PyObject_IsTrue(py_name_order);
  175. if (name_order == -1)
  176. goto error;
  177. cmp = name_order ? cmp_tree_item_name_order : cmp_tree_item;
  178. num_entries = PyDict_Size(entries);
  179. if (PyErr_Occurred())
  180. goto error;
  181. qsort_entries = PyMem_New(struct tree_item, num_entries);
  182. if (!qsort_entries) {
  183. PyErr_NoMemory();
  184. goto error;
  185. }
  186. while (PyDict_Next(entries, &pos, &key, &value)) {
  187. if (!PyString_Check(key)) {
  188. PyErr_SetString(PyExc_TypeError, "Name is not a string");
  189. goto error;
  190. }
  191. if (PyTuple_Size(value) != 2) {
  192. PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
  193. goto error;
  194. }
  195. py_mode = PyTuple_GET_ITEM(value, 0);
  196. if (!PyInt_Check(py_mode) && !PyLong_Check(py_mode)) {
  197. PyErr_SetString(PyExc_TypeError, "Mode is not an integral type");
  198. goto error;
  199. }
  200. py_sha = PyTuple_GET_ITEM(value, 1);
  201. if (!PyString_Check(py_sha)) {
  202. PyErr_SetString(PyExc_TypeError, "SHA is not a string");
  203. goto error;
  204. }
  205. qsort_entries[n].name = PyString_AS_STRING(key);
  206. qsort_entries[n].mode = PyInt_AsLong(py_mode);
  207. qsort_entries[n].tuple = PyObject_CallFunctionObjArgs(
  208. tree_entry_cls, key, py_mode, py_sha, NULL);
  209. if (qsort_entries[n].tuple == NULL)
  210. goto error;
  211. n++;
  212. }
  213. qsort(qsort_entries, num_entries, sizeof(struct tree_item), cmp);
  214. ret = PyList_New(num_entries);
  215. if (ret == NULL) {
  216. PyErr_NoMemory();
  217. goto error;
  218. }
  219. for (i = 0; i < num_entries; i++) {
  220. PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
  221. }
  222. PyMem_Free(qsort_entries);
  223. return ret;
  224. error:
  225. for (i = 0; i < n; i++) {
  226. Py_XDECREF(qsort_entries[i].tuple);
  227. }
  228. PyMem_Free(qsort_entries);
  229. return NULL;
  230. }
  231. static PyMethodDef py_objects_methods[] = {
  232. { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS | METH_KEYWORDS,
  233. NULL },
  234. { "sorted_tree_items", py_sorted_tree_items, METH_VARARGS, NULL },
  235. { NULL, NULL, 0, NULL }
  236. };
  237. static PyObject *
  238. moduleinit(void)
  239. {
  240. PyObject *m, *objects_mod, *errors_mod;
  241. #if PY_MAJOR_VERSION >= 3
  242. static struct PyModuleDef moduledef = {
  243. PyModuleDef_HEAD_INIT,
  244. "_objects", /* m_name */
  245. NULL, /* m_doc */
  246. -1, /* m_size */
  247. py_objects_methods, /* m_methods */
  248. NULL, /* m_reload */
  249. NULL, /* m_traverse */
  250. NULL, /* m_clear*/
  251. NULL, /* m_free */
  252. };
  253. m = PyModule_Create(&moduledef);
  254. #else
  255. m = Py_InitModule3("_objects", py_objects_methods, NULL);
  256. #endif
  257. if (m == NULL) {
  258. return NULL;
  259. }
  260. errors_mod = PyImport_ImportModule("dulwich.errors");
  261. if (errors_mod == NULL) {
  262. return NULL;
  263. }
  264. object_format_exception_cls = PyObject_GetAttrString(
  265. errors_mod, "ObjectFormatException");
  266. Py_DECREF(errors_mod);
  267. if (object_format_exception_cls == NULL) {
  268. return NULL;
  269. }
  270. /* This is a circular import but should be safe since this module is
  271. * imported at at the very bottom of objects.py. */
  272. objects_mod = PyImport_ImportModule("dulwich.objects");
  273. if (objects_mod == NULL) {
  274. return NULL;
  275. }
  276. tree_entry_cls = PyObject_GetAttrString(objects_mod, "TreeEntry");
  277. Py_DECREF(objects_mod);
  278. if (tree_entry_cls == NULL) {
  279. return NULL;
  280. }
  281. return m;
  282. }
  283. #if PY_MAJOR_VERSION >= 3
  284. PyMODINIT_FUNC
  285. PyInit__objects(void)
  286. {
  287. return moduleinit();
  288. }
  289. #else
  290. PyMODINIT_FUNC
  291. init_objects(void)
  292. {
  293. moduleinit();
  294. }
  295. #endif