PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/subversion/subversion/libsvn_delta/path_driver.c

https://github.com/schwern/alien-svn
C | 298 lines | 176 code | 47 blank | 75 comment | 27 complexity | fd0ff0fa22074067e970b3a55ac1d23b MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * path_driver.c -- drive an editor across a set of paths
  3. *
  4. * ====================================================================
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. * ====================================================================
  22. */
  23. #include <apr_pools.h>
  24. #include <apr_strings.h>
  25. #include "svn_types.h"
  26. #include "svn_delta.h"
  27. #include "svn_pools.h"
  28. #include "svn_dirent_uri.h"
  29. #include "svn_path.h"
  30. #include "svn_sorts.h"
  31. #include "private/svn_fspath.h"
  32. /*** Helper functions. ***/
  33. typedef struct dir_stack_t
  34. {
  35. void *dir_baton; /* the dir baton. */
  36. apr_pool_t *pool; /* the pool associated with the dir baton. */
  37. } dir_stack_t;
  38. /* Call EDITOR's open_directory() function with the PATH argument, then
  39. * add the resulting dir baton to the dir baton stack.
  40. */
  41. static svn_error_t *
  42. open_dir(apr_array_header_t *db_stack,
  43. const svn_delta_editor_t *editor,
  44. const char *path,
  45. apr_pool_t *pool)
  46. {
  47. void *parent_db, *db;
  48. dir_stack_t *item;
  49. apr_pool_t *subpool;
  50. /* Assert that we are in a stable state. */
  51. SVN_ERR_ASSERT(db_stack && db_stack->nelts);
  52. /* Get the parent dir baton. */
  53. item = APR_ARRAY_IDX(db_stack, db_stack->nelts - 1, void *);
  54. parent_db = item->dir_baton;
  55. /* Call the EDITOR's open_directory function to get a new directory
  56. baton. */
  57. subpool = svn_pool_create(pool);
  58. SVN_ERR(editor->open_directory(path, parent_db, SVN_INVALID_REVNUM, subpool,
  59. &db));
  60. /* Now add the dir baton to the stack. */
  61. item = apr_pcalloc(subpool, sizeof(*item));
  62. item->dir_baton = db;
  63. item->pool = subpool;
  64. APR_ARRAY_PUSH(db_stack, dir_stack_t *) = item;
  65. return SVN_NO_ERROR;
  66. }
  67. /* Pop a directory from the dir baton stack and update the stack
  68. * pointer.
  69. *
  70. * This function calls the EDITOR's close_directory() function.
  71. */
  72. static svn_error_t *
  73. pop_stack(apr_array_header_t *db_stack,
  74. const svn_delta_editor_t *editor)
  75. {
  76. dir_stack_t *item;
  77. /* Assert that we are in a stable state. */
  78. SVN_ERR_ASSERT(db_stack && db_stack->nelts);
  79. /* Close the most recent directory pushed to the stack. */
  80. item = APR_ARRAY_IDX(db_stack, db_stack->nelts - 1, dir_stack_t *);
  81. (void) apr_array_pop(db_stack);
  82. SVN_ERR(editor->close_directory(item->dir_baton, item->pool));
  83. svn_pool_destroy(item->pool);
  84. return SVN_NO_ERROR;
  85. }
  86. /* Count the number of path components in PATH. */
  87. static int
  88. count_components(const char *path)
  89. {
  90. int count = 1;
  91. const char *instance = path;
  92. if ((strlen(path) == 1) && (path[0] == '/'))
  93. return 0;
  94. do
  95. {
  96. instance++;
  97. instance = strchr(instance, '/');
  98. if (instance)
  99. count++;
  100. }
  101. while (instance);
  102. return count;
  103. }
  104. /*** Public interfaces ***/
  105. svn_error_t *
  106. svn_delta_path_driver2(const svn_delta_editor_t *editor,
  107. void *edit_baton,
  108. const apr_array_header_t *paths,
  109. svn_boolean_t sort_paths,
  110. svn_delta_path_driver_cb_func_t callback_func,
  111. void *callback_baton,
  112. apr_pool_t *pool)
  113. {
  114. apr_array_header_t *db_stack = apr_array_make(pool, 4, sizeof(void *));
  115. const char *last_path = NULL;
  116. int i = 0;
  117. void *parent_db = NULL, *db = NULL;
  118. const char *path;
  119. apr_pool_t *subpool, *iterpool;
  120. dir_stack_t *item;
  121. /* Do nothing if there are no paths. */
  122. if (! paths->nelts)
  123. return SVN_NO_ERROR;
  124. subpool = svn_pool_create(pool);
  125. iterpool = svn_pool_create(pool);
  126. /* sort paths if necessary */
  127. if (sort_paths && paths->nelts > 1)
  128. {
  129. apr_array_header_t *sorted = apr_array_copy(subpool, paths);
  130. qsort(sorted->elts, sorted->nelts, sorted->elt_size,
  131. svn_sort_compare_paths);
  132. paths = sorted;
  133. }
  134. item = apr_pcalloc(subpool, sizeof(*item));
  135. /* If the root of the edit is also a target path, we want to call
  136. the callback function to let the user open the root directory and
  137. do what needs to be done. Otherwise, we'll do the open_root()
  138. ourselves. */
  139. path = APR_ARRAY_IDX(paths, 0, const char *);
  140. if (svn_path_is_empty(path))
  141. {
  142. SVN_ERR(callback_func(&db, NULL, callback_baton, path, subpool));
  143. last_path = path;
  144. i++;
  145. }
  146. else
  147. {
  148. SVN_ERR(editor->open_root(edit_baton, SVN_INVALID_REVNUM, subpool, &db));
  149. }
  150. item->pool = subpool;
  151. item->dir_baton = db;
  152. APR_ARRAY_PUSH(db_stack, void *) = item;
  153. /* Now, loop over the commit items, traversing the URL tree and
  154. driving the editor. */
  155. for (; i < paths->nelts; i++)
  156. {
  157. const char *pdir, *bname;
  158. const char *common = "";
  159. size_t common_len;
  160. /* Clear the iteration pool. */
  161. svn_pool_clear(iterpool);
  162. /* Get the next path. */
  163. path = APR_ARRAY_IDX(paths, i, const char *);
  164. /*** Step A - Find the common ancestor of the last path and the
  165. current one. For the first iteration, this is just the
  166. empty string. ***/
  167. if (i > 0)
  168. common = (last_path[0] == '/')
  169. ? svn_fspath__get_longest_ancestor(last_path, path, iterpool)
  170. : svn_relpath_get_longest_ancestor(last_path, path, iterpool);
  171. common_len = strlen(common);
  172. /*** Step B - Close any directories between the last path and
  173. the new common ancestor, if any need to be closed.
  174. Sometimes there is nothing to do here (like, for the first
  175. iteration, or when the last path was an ancestor of the
  176. current one). ***/
  177. if ((i > 0) && (strlen(last_path) > common_len))
  178. {
  179. const char *rel = last_path + (common_len ? (common_len + 1) : 0);
  180. int count = count_components(rel);
  181. while (count--)
  182. {
  183. SVN_ERR(pop_stack(db_stack, editor));
  184. }
  185. }
  186. /*** Step C - Open any directories between the common ancestor
  187. and the parent of the current path. ***/
  188. if (*path == '/')
  189. svn_fspath__split(&pdir, &bname, path, iterpool);
  190. else
  191. svn_relpath_split(&pdir, &bname, path, iterpool);
  192. if (strlen(pdir) > common_len)
  193. {
  194. const char *piece = pdir + common_len + 1;
  195. while (1)
  196. {
  197. const char *rel = pdir;
  198. /* Find the first separator. */
  199. piece = strchr(piece, '/');
  200. /* Calculate REL as the portion of PDIR up to (but not
  201. including) the location to which PIECE is pointing. */
  202. if (piece)
  203. rel = apr_pstrmemdup(iterpool, pdir, piece - pdir);
  204. /* Open the subdirectory. */
  205. SVN_ERR(open_dir(db_stack, editor, rel, pool));
  206. /* If we found a '/', advance our PIECE pointer to
  207. character just after that '/'. Otherwise, we're
  208. done. */
  209. if (piece)
  210. piece++;
  211. else
  212. break;
  213. }
  214. }
  215. /*** Step D - Tell our caller to handle the current path. ***/
  216. item = APR_ARRAY_IDX(db_stack, db_stack->nelts - 1, void *);
  217. parent_db = item->dir_baton;
  218. subpool = svn_pool_create(pool);
  219. SVN_ERR(callback_func(&db, parent_db, callback_baton, path, subpool));
  220. if (db)
  221. {
  222. item = apr_pcalloc(subpool, sizeof(*item));
  223. item->dir_baton = db;
  224. item->pool = subpool;
  225. APR_ARRAY_PUSH(db_stack, void *) = item;
  226. }
  227. else
  228. {
  229. svn_pool_destroy(subpool);
  230. }
  231. /*** Step E - Save our state for the next iteration. If our
  232. caller opened or added PATH as a directory, that becomes
  233. our LAST_PATH. Otherwise, we use PATH's parent
  234. directory. ***/
  235. /* NOTE: The variable LAST_PATH needs to outlive the loop. */
  236. if (db)
  237. last_path = path; /* lives in a pool outside our control. */
  238. else
  239. last_path = apr_pstrdup(pool, pdir); /* duping into POOL. */
  240. }
  241. /* Destroy the iteration subpool. */
  242. svn_pool_destroy(iterpool);
  243. /* Close down any remaining open directory batons. */
  244. while (db_stack->nelts)
  245. {
  246. SVN_ERR(pop_stack(db_stack, editor));
  247. }
  248. return SVN_NO_ERROR;
  249. }