_objects.c 4.9 KB

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