_objects.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C) 2009 Jelmer Vernooij <jelmer@jelmer.uk>
  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. #define PY_SSIZE_T_CLEAN
  21. #include <Python.h>
  22. #include <stdlib.h>
  23. #include <sys/stat.h>
  24. #if defined(__MINGW32_VERSION) || defined(__APPLE__)
  25. size_t rep_strnlen(char *text, size_t maxlen);
  26. size_t rep_strnlen(char *text, size_t maxlen)
  27. {
  28. const char *last = memchr(text, '\0', maxlen);
  29. return last ? (size_t) (last - text) : maxlen;
  30. }
  31. #define strnlen rep_strnlen
  32. #endif
  33. #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
  34. static PyObject *tree_entry_cls;
  35. static PyObject *object_format_exception_cls;
  36. static PyObject *sha_to_pyhex(const unsigned char *sha)
  37. {
  38. char hexsha[41];
  39. int i;
  40. for (i = 0; i < 20; i++) {
  41. hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
  42. hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
  43. }
  44. return PyBytes_FromStringAndSize(hexsha, 40);
  45. }
  46. static PyObject *py_parse_tree(PyObject *self, PyObject *args, PyObject *kw)
  47. {
  48. char *text, *start, *end;
  49. Py_ssize_t len; int strict;
  50. size_t namelen;
  51. PyObject *ret, *item, *name, *sha, *py_strict = NULL;
  52. static char *kwlist[] = {"text", "strict", NULL};
  53. if (!PyArg_ParseTupleAndKeywords(args, kw, "y#|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 = PyBytes_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. /* Not all environments define S_ISDIR */
  121. #if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
  122. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  123. #endif
  124. int cmp_tree_item(const void *_a, const void *_b)
  125. {
  126. const struct tree_item *a = _a, *b = _b;
  127. const char *remain_a, *remain_b;
  128. int ret;
  129. size_t common;
  130. if (strlen(a->name) > strlen(b->name)) {
  131. common = strlen(b->name);
  132. remain_a = a->name + common;
  133. remain_b = (S_ISDIR(b->mode)?"/":"");
  134. } else if (strlen(b->name) > strlen(a->name)) {
  135. common = strlen(a->name);
  136. remain_a = (S_ISDIR(a->mode)?"/":"");
  137. remain_b = b->name + common;
  138. } else { /* strlen(a->name) == strlen(b->name) */
  139. common = 0;
  140. remain_a = a->name;
  141. remain_b = b->name;
  142. }
  143. ret = strncmp(a->name, b->name, common);
  144. if (ret != 0)
  145. return ret;
  146. return strcmp(remain_a, remain_b);
  147. }
  148. int cmp_tree_item_name_order(const void *_a, const void *_b) {
  149. const struct tree_item *a = _a, *b = _b;
  150. return strcmp(a->name, b->name);
  151. }
  152. static PyObject *py_sorted_tree_items(PyObject *self, PyObject *args)
  153. {
  154. struct tree_item *qsort_entries = NULL;
  155. int name_order, n = 0, i;
  156. PyObject *entries, *py_name_order, *ret, *key, *value, *py_mode, *py_sha;
  157. Py_ssize_t pos = 0, num_entries;
  158. int (*cmp)(const void *, const void *);
  159. if (!PyArg_ParseTuple(args, "OO", &entries, &py_name_order))
  160. goto error;
  161. if (!PyDict_Check(entries)) {
  162. PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
  163. goto error;
  164. }
  165. name_order = PyObject_IsTrue(py_name_order);
  166. if (name_order == -1)
  167. goto error;
  168. cmp = name_order ? cmp_tree_item_name_order : cmp_tree_item;
  169. num_entries = PyDict_Size(entries);
  170. if (PyErr_Occurred())
  171. goto error;
  172. qsort_entries = PyMem_New(struct tree_item, num_entries);
  173. if (!qsort_entries) {
  174. PyErr_NoMemory();
  175. goto error;
  176. }
  177. while (PyDict_Next(entries, &pos, &key, &value)) {
  178. if (!PyBytes_Check(key)) {
  179. PyErr_SetString(PyExc_TypeError, "Name is not a string");
  180. goto error;
  181. }
  182. if (PyTuple_Size(value) != 2) {
  183. PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
  184. goto error;
  185. }
  186. py_mode = PyTuple_GET_ITEM(value, 0);
  187. if (!PyLong_Check(py_mode)) {
  188. PyErr_SetString(PyExc_TypeError, "Mode is not an integral type");
  189. goto error;
  190. }
  191. py_sha = PyTuple_GET_ITEM(value, 1);
  192. if (!PyBytes_Check(py_sha)) {
  193. PyErr_SetString(PyExc_TypeError, "SHA is not a string");
  194. goto error;
  195. }
  196. qsort_entries[n].name = PyBytes_AS_STRING(key);
  197. qsort_entries[n].mode = PyLong_AsLong(py_mode);
  198. qsort_entries[n].tuple = PyObject_CallFunctionObjArgs(
  199. tree_entry_cls, key, py_mode, py_sha, NULL);
  200. if (qsort_entries[n].tuple == NULL)
  201. goto error;
  202. n++;
  203. }
  204. qsort(qsort_entries, num_entries, sizeof(struct tree_item), cmp);
  205. ret = PyList_New(num_entries);
  206. if (ret == NULL) {
  207. PyErr_NoMemory();
  208. goto error;
  209. }
  210. for (i = 0; i < num_entries; i++) {
  211. PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
  212. }
  213. PyMem_Free(qsort_entries);
  214. return ret;
  215. error:
  216. for (i = 0; i < n; i++) {
  217. Py_XDECREF(qsort_entries[i].tuple);
  218. }
  219. PyMem_Free(qsort_entries);
  220. return NULL;
  221. }
  222. static PyMethodDef py_objects_methods[] = {
  223. { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS | METH_KEYWORDS,
  224. NULL },
  225. { "sorted_tree_items", py_sorted_tree_items, METH_VARARGS, NULL },
  226. { NULL, NULL, 0, NULL }
  227. };
  228. static PyObject *
  229. moduleinit(void)
  230. {
  231. PyObject *m, *objects_mod, *errors_mod;
  232. static struct PyModuleDef moduledef = {
  233. PyModuleDef_HEAD_INIT,
  234. "_objects", /* m_name */
  235. NULL, /* m_doc */
  236. -1, /* m_size */
  237. py_objects_methods, /* m_methods */
  238. NULL, /* m_reload */
  239. NULL, /* m_traverse */
  240. NULL, /* m_clear*/
  241. NULL, /* m_free */
  242. };
  243. m = PyModule_Create(&moduledef);
  244. if (m == NULL) {
  245. return NULL;
  246. }
  247. errors_mod = PyImport_ImportModule("dulwich.errors");
  248. if (errors_mod == NULL) {
  249. return NULL;
  250. }
  251. object_format_exception_cls = PyObject_GetAttrString(
  252. errors_mod, "ObjectFormatException");
  253. Py_DECREF(errors_mod);
  254. if (object_format_exception_cls == NULL) {
  255. return NULL;
  256. }
  257. /* This is a circular import but should be safe since this module is
  258. * imported at at the very bottom of objects.py. */
  259. objects_mod = PyImport_ImportModule("dulwich.objects");
  260. if (objects_mod == NULL) {
  261. return NULL;
  262. }
  263. tree_entry_cls = PyObject_GetAttrString(objects_mod, "TreeEntry");
  264. Py_DECREF(objects_mod);
  265. if (tree_entry_cls == NULL) {
  266. return NULL;
  267. }
  268. return m;
  269. }
  270. PyMODINIT_FUNC
  271. PyInit__objects(void)
  272. {
  273. return moduleinit();
  274. }