_objects.c 8.5 KB

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