_objects.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
  26. static PyObject *sha_to_pyhex(const unsigned char *sha)
  27. {
  28. char hexsha[41];
  29. int i;
  30. for (i = 0; i < 20; i++) {
  31. hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
  32. hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
  33. }
  34. return PyString_FromStringAndSize(hexsha, 40);
  35. }
  36. static PyObject *py_parse_tree(PyObject *self, PyObject *args)
  37. {
  38. char *text, *start, *end;
  39. int len, namelen;
  40. PyObject *ret, *item, *name;
  41. if (!PyArg_ParseTuple(args, "s#", &text, &len))
  42. return NULL;
  43. /* TODO: currently this returns a list; if memory usage is a concern,
  44. * consider rewriting as a custom iterator object */
  45. ret = PyList_New(0);
  46. if (ret == NULL) {
  47. return NULL;
  48. }
  49. start = text;
  50. end = text + len;
  51. while (text < end) {
  52. long mode;
  53. mode = strtol(text, &text, 8);
  54. if (*text != ' ') {
  55. PyErr_SetString(PyExc_ValueError, "Expected space");
  56. Py_DECREF(ret);
  57. return NULL;
  58. }
  59. text++;
  60. namelen = strnlen(text, len - (text - start));
  61. name = PyString_FromStringAndSize(text, namelen);
  62. if (name == NULL) {
  63. Py_DECREF(ret);
  64. return NULL;
  65. }
  66. if (text + namelen + 20 >= end) {
  67. PyErr_SetString(PyExc_ValueError, "SHA truncated");
  68. Py_DECREF(ret);
  69. Py_DECREF(name);
  70. return NULL;
  71. }
  72. item = Py_BuildValue("(NlN)", name, mode,
  73. sha_to_pyhex((unsigned char *)text+namelen+1));
  74. if (item == NULL) {
  75. Py_DECREF(ret);
  76. Py_DECREF(name);
  77. return NULL;
  78. }
  79. if (PyList_Append(ret, item) == -1) {
  80. Py_DECREF(ret);
  81. Py_DECREF(item);
  82. return NULL;
  83. }
  84. Py_DECREF(item);
  85. text += namelen+21;
  86. }
  87. return ret;
  88. }
  89. struct tree_item {
  90. const char *name;
  91. int mode;
  92. PyObject *tuple;
  93. };
  94. int cmp_tree_item(const void *_a, const void *_b)
  95. {
  96. const struct tree_item *a = _a, *b = _b;
  97. const char *remain_a, *remain_b;
  98. int ret, common;
  99. if (strlen(a->name) > strlen(b->name)) {
  100. common = strlen(b->name);
  101. remain_a = a->name + common;
  102. remain_b = (S_ISDIR(b->mode)?"/":"");
  103. } else if (strlen(b->name) > strlen(a->name)) {
  104. common = strlen(a->name);
  105. remain_a = (S_ISDIR(a->mode)?"/":"");
  106. remain_b = b->name + common;
  107. } else { /* strlen(a->name) == strlen(b->name) */
  108. common = 0;
  109. remain_a = a->name;
  110. remain_b = b->name;
  111. }
  112. ret = strncmp(a->name, b->name, common);
  113. if (ret != 0)
  114. return ret;
  115. return strcmp(remain_a, remain_b);
  116. }
  117. static void free_tree_items(struct tree_item *items, int num) {
  118. int i;
  119. for (i = 0; i < num; i++) {
  120. Py_DECREF(items[i].tuple);
  121. }
  122. free(items);
  123. }
  124. static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
  125. {
  126. struct tree_item *qsort_entries;
  127. int num, i;
  128. PyObject *ret;
  129. Py_ssize_t pos = 0;
  130. PyObject *key, *value;
  131. if (!PyDict_Check(entries)) {
  132. PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
  133. return NULL;
  134. }
  135. num = PyDict_Size(entries);
  136. qsort_entries = malloc(num * sizeof(struct tree_item));
  137. if (qsort_entries == NULL) {
  138. PyErr_NoMemory();
  139. return NULL;
  140. }
  141. i = 0;
  142. while (PyDict_Next(entries, &pos, &key, &value)) {
  143. PyObject *py_mode, *py_int_mode, *py_sha;
  144. if (!PyString_Check(key)) {
  145. PyErr_SetString(PyExc_TypeError, "Name is not a string");
  146. free_tree_items(qsort_entries, i);
  147. return NULL;
  148. }
  149. if (PyTuple_Size(value) != 2) {
  150. PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
  151. free_tree_items(qsort_entries, i);
  152. return NULL;
  153. }
  154. py_mode = PyTuple_GET_ITEM(value, 0);
  155. py_int_mode = PyNumber_Int(py_mode);
  156. if (!py_int_mode) {
  157. PyErr_SetString(PyExc_TypeError, "Mode is not an integral type");
  158. free_tree_items(qsort_entries, i);
  159. return NULL;
  160. }
  161. py_sha = PyTuple_GET_ITEM(value, 1);
  162. if (!PyString_Check(py_sha)) {
  163. PyErr_SetString(PyExc_TypeError, "SHA is not a string");
  164. Py_DECREF(py_int_mode);
  165. free_tree_items(qsort_entries, i);
  166. return NULL;
  167. }
  168. qsort_entries[i].name = PyString_AS_STRING(key);
  169. qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
  170. qsort_entries[i].tuple = PyTuple_Pack(3, key, py_int_mode, py_sha);
  171. Py_DECREF(py_int_mode);
  172. i++;
  173. }
  174. qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);
  175. ret = PyList_New(num);
  176. if (ret == NULL) {
  177. free_tree_items(qsort_entries, i);
  178. PyErr_NoMemory();
  179. return NULL;
  180. }
  181. for (i = 0; i < num; i++) {
  182. PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
  183. }
  184. free(qsort_entries);
  185. return ret;
  186. }
  187. static PyMethodDef py_objects_methods[] = {
  188. { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
  189. { "sorted_tree_items", (PyCFunction)py_sorted_tree_items, METH_O, NULL },
  190. { NULL, NULL, 0, NULL }
  191. };
  192. void init_objects(void)
  193. {
  194. PyObject *m;
  195. m = Py_InitModule3("_objects", py_objects_methods, NULL);
  196. if (m == NULL)
  197. return;
  198. }