_objects.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
  118. {
  119. struct tree_item *qsort_entries;
  120. int num, i;
  121. PyObject *ret;
  122. Py_ssize_t pos = 0;
  123. PyObject *key, *value;
  124. if (!PyDict_Check(entries)) {
  125. PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
  126. return NULL;
  127. }
  128. num = PyDict_Size(entries);
  129. qsort_entries = malloc(num * sizeof(struct tree_item));
  130. if (qsort_entries == NULL) {
  131. PyErr_NoMemory();
  132. return NULL;
  133. }
  134. i = 0;
  135. while (PyDict_Next(entries, &pos, &key, &value)) {
  136. PyObject *py_mode, *py_int_mode, *py_sha;
  137. if (PyTuple_Size(value) != 2) {
  138. PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
  139. free(qsort_entries);
  140. return NULL;
  141. }
  142. py_mode = PyTuple_GET_ITEM(value, 0);
  143. py_int_mode = PyNumber_Int(py_mode);
  144. if (!py_int_mode) {
  145. PyErr_SetString(PyExc_TypeError, "Mode is not an integral type");
  146. free(qsort_entries);
  147. return NULL;
  148. }
  149. py_sha = PyTuple_GET_ITEM(value, 1);
  150. if (!PyString_CheckExact(key)) {
  151. PyErr_SetString(PyExc_TypeError, "Name is not a string");
  152. free(qsort_entries);
  153. return NULL;
  154. }
  155. qsort_entries[i].name = PyString_AS_STRING(key);
  156. qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
  157. qsort_entries[i].tuple = PyTuple_Pack(3, key, py_mode, py_sha);
  158. i++;
  159. }
  160. qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);
  161. ret = PyList_New(num);
  162. if (ret == NULL) {
  163. free(qsort_entries);
  164. PyErr_NoMemory();
  165. return NULL;
  166. }
  167. for (i = 0; i < num; i++) {
  168. PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
  169. }
  170. free(qsort_entries);
  171. return ret;
  172. }
  173. static PyMethodDef py_objects_methods[] = {
  174. { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
  175. { "sorted_tree_items", (PyCFunction)py_sorted_tree_items, METH_O, NULL },
  176. { NULL, NULL, 0, NULL }
  177. };
  178. void init_objects(void)
  179. {
  180. PyObject *m;
  181. m = Py_InitModule3("_objects", py_objects_methods, NULL);
  182. if (m == NULL)
  183. return;
  184. }