2 * copy_foreign.c: copy from other repository support.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * ====================================================================
24 /* ==================================================================== */
30 #include "svn_client.h"
31 #include "svn_delta.h"
32 #include "svn_dirent_uri.h"
33 #include "svn_error.h"
34 #include "svn_error_codes.h"
36 #include "svn_pools.h"
37 #include "svn_props.h"
44 #include "private/svn_subr_private.h"
45 #include "private/svn_wc_private.h"
46 #include "svn_private_config.h"
51 const char *anchor_abspath;
53 svn_wc_context_t *wc_ctx;
54 svn_wc_notify_func2_t notify_func;
62 struct dir_baton_t *pb;
63 struct edit_baton_t *eb;
65 const char *local_abspath;
67 svn_boolean_t created;
68 apr_hash_t *properties;
73 /* svn_delta_editor_t function */
75 edit_open(void *edit_baton,
76 svn_revnum_t base_revision,
77 apr_pool_t *result_pool,
80 struct edit_baton_t *eb = edit_baton;
81 apr_pool_t *dir_pool = svn_pool_create(eb->pool);
82 struct dir_baton_t *db = apr_pcalloc(dir_pool, sizeof(*db));
87 db->local_abspath = eb->anchor_abspath;
89 SVN_ERR(svn_io_make_dir_recursively(eb->anchor_abspath, dir_pool));
96 /* svn_delta_editor_t function */
98 edit_close(void *edit_baton,
99 apr_pool_t *scratch_pool)
105 dir_add(const char *path,
107 const char *copyfrom_path,
108 svn_revnum_t copyfrom_revision,
109 apr_pool_t *result_pool,
112 struct dir_baton_t *pb = parent_baton;
113 struct edit_baton_t *eb = pb->eb;
114 apr_pool_t *dir_pool = svn_pool_create(pb->pool);
115 struct dir_baton_t *db = apr_pcalloc(dir_pool, sizeof(*db));
116 svn_boolean_t under_root;
125 SVN_ERR(svn_dirent_is_under_root(&under_root, &db->local_abspath,
126 eb->anchor_abspath, path, db->pool));
129 return svn_error_createf(
130 SVN_ERR_WC_OBSTRUCTED_UPDATE, NULL,
131 _("Path '%s' is not in the working copy"),
132 svn_dirent_local_style(path, db->pool));
135 SVN_ERR(svn_io_make_dir_recursively(db->local_abspath, db->pool));
142 dir_change_prop(void *dir_baton,
144 const svn_string_t *value,
145 apr_pool_t *scratch_pool)
147 struct dir_baton_t *db = dir_baton;
148 struct edit_baton_t *eb = db->eb;
149 svn_prop_kind_t prop_kind;
151 prop_kind = svn_property_kind2(name);
153 if (prop_kind != svn_prop_regular_kind
154 || ! strcmp(name, SVN_PROP_MERGEINFO))
156 /* We can't handle DAV, ENTRY and merge specific props here */
162 /* We can still store them in the hash for immediate addition
163 with the svn_wc_add_from_disk3() call */
164 if (! db->properties)
165 db->properties = apr_hash_make(db->pool);
168 svn_hash_sets(db->properties, apr_pstrdup(db->pool, name),
169 svn_string_dup(value, db->pool));
173 /* We have already notified for this directory, so don't do that again */
174 SVN_ERR(svn_wc_prop_set4(eb->wc_ctx, db->local_abspath, name, value,
175 svn_depth_empty, FALSE, NULL,
176 NULL, NULL, /* Cancellation */
177 NULL, NULL, /* Notification */
184 /* Releases the directory baton if there are no more users */
186 maybe_done(struct dir_baton_t *db)
192 struct dir_baton_t *pb = db->pb;
194 svn_pool_clear(db->pool);
197 SVN_ERR(maybe_done(pb));
204 ensure_added(struct dir_baton_t *db,
205 apr_pool_t *scratch_pool)
211 SVN_ERR(ensure_added(db->pb, scratch_pool));
215 /* Add the directory with all the already collected properties */
216 SVN_ERR(svn_wc_add_from_disk3(db->eb->wc_ctx,
219 TRUE /* skip checks */,
221 db->eb->notify_baton,
228 dir_close(void *dir_baton,
229 apr_pool_t *scratch_pool)
231 struct dir_baton_t *db = dir_baton;
232 /*struct edit_baton_t *eb = db->eb;*/
234 SVN_ERR(ensure_added(db, scratch_pool));
236 SVN_ERR(maybe_done(db));
245 struct dir_baton_t *pb;
246 struct edit_baton_t *eb;
248 const char *local_abspath;
249 apr_hash_t *properties;
251 svn_boolean_t writing;
252 unsigned char digest[APR_MD5_DIGESTSIZE];
254 const char *tmp_path;
258 file_add(const char *path,
260 const char *copyfrom_path,
261 svn_revnum_t copyfrom_revision,
262 apr_pool_t *result_pool,
265 struct dir_baton_t *pb = parent_baton;
266 struct edit_baton_t *eb = pb->eb;
267 apr_pool_t *file_pool = svn_pool_create(pb->pool);
268 struct file_baton_t *fb = apr_pcalloc(file_pool, sizeof(*fb));
269 svn_boolean_t under_root;
273 fb->pool = file_pool;
277 SVN_ERR(svn_dirent_is_under_root(&under_root, &fb->local_abspath,
278 eb->anchor_abspath, path, fb->pool));
281 return svn_error_createf(
282 SVN_ERR_WC_OBSTRUCTED_UPDATE, NULL,
283 _("Path '%s' is not in the working copy"),
284 svn_dirent_local_style(path, fb->pool));
292 file_change_prop(void *file_baton,
294 const svn_string_t *value,
295 apr_pool_t *scratch_pool)
297 struct file_baton_t *fb = file_baton;
298 svn_prop_kind_t prop_kind;
300 prop_kind = svn_property_kind2(name);
302 if (prop_kind != svn_prop_regular_kind
303 || ! strcmp(name, SVN_PROP_MERGEINFO))
305 /* We can't handle DAV, ENTRY and merge specific props here */
309 /* We store all properties in the hash for immediate addition
310 with the svn_wc_add_from_disk3() call */
311 if (! fb->properties)
312 fb->properties = apr_hash_make(fb->pool);
315 svn_hash_sets(fb->properties, apr_pstrdup(fb->pool, name),
316 svn_string_dup(value, fb->pool));
322 file_textdelta(void *file_baton,
323 const char *base_checksum,
324 apr_pool_t *result_pool,
325 svn_txdelta_window_handler_t *handler,
326 void **handler_baton)
328 struct file_baton_t *fb = file_baton;
329 svn_stream_t *target;
331 SVN_ERR_ASSERT(! fb->writing);
333 SVN_ERR(svn_stream_open_writable(&target, fb->local_abspath, fb->pool,
337 svn_txdelta_apply(svn_stream_empty(fb->pool) /* source */,
342 /* Provide the handler directly */
343 handler, handler_baton);
349 file_close(void *file_baton,
350 const char *text_checksum,
351 apr_pool_t *scratch_pool)
353 struct file_baton_t *fb = file_baton;
354 struct edit_baton_t *eb = fb->eb;
355 struct dir_baton_t *pb = fb->pb;
357 SVN_ERR(ensure_added(pb, fb->pool));
361 svn_checksum_t *expected_checksum;
362 svn_checksum_t *actual_checksum;
364 SVN_ERR(svn_checksum_parse_hex(&expected_checksum, svn_checksum_md5,
365 text_checksum, fb->pool));
366 actual_checksum = svn_checksum__from_digest_md5(fb->digest, fb->pool);
368 if (! svn_checksum_match(expected_checksum, actual_checksum))
369 return svn_error_trace(
370 svn_checksum_mismatch_err(expected_checksum,
373 _("Checksum mismatch for '%s'"),
374 svn_dirent_local_style(
379 SVN_ERR(svn_wc_add_from_disk3(eb->wc_ctx, fb->local_abspath, fb->properties,
380 TRUE /* skip checks */,
381 eb->notify_func, eb->notify_baton,
384 svn_pool_destroy(fb->pool);
385 SVN_ERR(maybe_done(pb));
391 copy_foreign_dir(svn_ra_session_t *ra_session,
392 svn_client__pathrev_t *location,
393 svn_wc_context_t *wc_ctx,
394 const char *dst_abspath,
396 svn_wc_notify_func2_t notify_func,
398 svn_cancel_func_t cancel_func,
400 apr_pool_t *scratch_pool)
402 struct edit_baton_t eb;
403 svn_delta_editor_t *editor = svn_delta_default_editor(scratch_pool);
404 const svn_delta_editor_t *wrapped_editor;
406 const svn_ra_reporter3_t *reporter;
407 void *reporter_baton;
409 eb.pool = scratch_pool;
410 eb.anchor_abspath = dst_abspath;
413 eb.notify_func = notify_func;
414 eb.notify_baton = notify_baton;
416 editor->open_root = edit_open;
417 editor->close_edit = edit_close;
419 editor->add_directory = dir_add;
420 editor->change_dir_prop = dir_change_prop;
421 editor->close_directory = dir_close;
423 editor->add_file = file_add;
424 editor->change_file_prop = file_change_prop;
425 editor->apply_textdelta = file_textdelta;
426 editor->close_file = file_close;
428 SVN_ERR(svn_delta_get_cancellation_editor(cancel_func, cancel_baton,
430 &wrapped_editor, &wrapped_baton,
433 SVN_ERR(svn_ra_do_update3(ra_session, &reporter, &reporter_baton,
434 location->rev, "", svn_depth_infinity,
435 FALSE, FALSE, wrapped_editor, wrapped_baton,
436 scratch_pool, scratch_pool));
438 SVN_ERR(reporter->set_path(reporter_baton, "", location->rev, depth,
439 TRUE /* incomplete */,
440 NULL, scratch_pool));
442 SVN_ERR(reporter->finish_report(reporter_baton, scratch_pool));
449 svn_client__copy_foreign(const char *url,
450 const char *dst_abspath,
451 svn_opt_revision_t *peg_revision,
452 svn_opt_revision_t *revision,
454 svn_boolean_t make_parents,
455 svn_boolean_t already_locked,
456 svn_client_ctx_t *ctx,
457 apr_pool_t *scratch_pool)
459 svn_ra_session_t *ra_session;
460 svn_client__pathrev_t *loc;
461 svn_node_kind_t kind;
462 svn_node_kind_t wc_kind;
463 const char *dir_abspath;
465 SVN_ERR_ASSERT(svn_path_is_url(url));
466 SVN_ERR_ASSERT(svn_dirent_is_absolute(dst_abspath));
468 /* Do we need to validate/update revisions? */
470 SVN_ERR(svn_client__ra_session_from_path2(&ra_session, &loc,
476 SVN_ERR(svn_ra_check_path(ra_session, "", loc->rev, &kind, scratch_pool));
478 if (kind != svn_node_file && kind != svn_node_dir)
479 return svn_error_createf(
480 SVN_ERR_ILLEGAL_TARGET, NULL,
481 _("'%s' is not a valid location inside a repository"),
484 SVN_ERR(svn_wc_read_kind2(&wc_kind, ctx->wc_ctx, dst_abspath, FALSE, TRUE,
487 if (wc_kind != svn_node_none)
489 return svn_error_createf(
490 SVN_ERR_ENTRY_EXISTS, NULL,
491 _("'%s' is already under version control"),
492 svn_dirent_local_style(dst_abspath, scratch_pool));
495 dir_abspath = svn_dirent_dirname(dst_abspath, scratch_pool);
496 SVN_ERR(svn_wc_read_kind2(&wc_kind, ctx->wc_ctx, dir_abspath,
497 FALSE, FALSE, scratch_pool));
499 if (wc_kind == svn_node_none)
502 SVN_ERR(svn_client__make_local_parents(dir_abspath, make_parents, ctx,
505 SVN_ERR(svn_wc_read_kind2(&wc_kind, ctx->wc_ctx, dir_abspath,
506 FALSE, FALSE, scratch_pool));
509 if (wc_kind != svn_node_dir)
510 return svn_error_createf(
511 SVN_ERR_ENTRY_NOT_FOUND, NULL,
512 _("Can't add '%s', because no parent directory is found"),
513 svn_dirent_local_style(dst_abspath, scratch_pool));
516 if (kind == svn_node_file)
518 svn_stream_t *target;
520 apr_hash_index_t *hi;
521 SVN_ERR(svn_stream_open_writable(&target, dst_abspath, scratch_pool,
524 SVN_ERR(svn_ra_get_file(ra_session, "", loc->rev, target, NULL, &props,
528 for (hi = apr_hash_first(scratch_pool, props); hi;
529 hi = apr_hash_next(hi))
531 const char *name = apr_hash_this_key(hi);
533 if (svn_property_kind2(name) != svn_prop_regular_kind
534 || ! strcmp(name, SVN_PROP_MERGEINFO))
536 /* We can't handle DAV, ENTRY and merge specific props here */
537 svn_hash_sets(props, name, NULL);
542 SVN_WC__CALL_WITH_WRITE_LOCK(
543 svn_wc_add_from_disk3(ctx->wc_ctx, dst_abspath, props,
544 TRUE /* skip checks */,
545 ctx->notify_func2, ctx->notify_baton2,
547 ctx->wc_ctx, dir_abspath, FALSE, scratch_pool);
549 SVN_ERR(svn_wc_add_from_disk3(ctx->wc_ctx, dst_abspath, props,
550 TRUE /* skip checks */,
551 ctx->notify_func2, ctx->notify_baton2,
557 SVN_WC__CALL_WITH_WRITE_LOCK(
558 copy_foreign_dir(ra_session, loc,
559 ctx->wc_ctx, dst_abspath,
561 ctx->notify_func2, ctx->notify_baton2,
562 ctx->cancel_func, ctx->cancel_baton,
564 ctx->wc_ctx, dir_abspath, FALSE, scratch_pool);
566 SVN_ERR(copy_foreign_dir(ra_session, loc,
567 ctx->wc_ctx, dst_abspath,
569 ctx->notify_func2, ctx->notify_baton2,
570 ctx->cancel_func, ctx->cancel_baton,