_objects.c 4.6 KB

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