#include "svn_wc.h"
#include "svn_client.h"
#include "svn_pools.h"
#include "svn_error.h"
#include "svn_dirent_uri.h"
#include "svn_path.h"
#include "client.h"
#include "private/svn_wc_private.h"
#include "svn_private_config.h"
struct url_uuid_t
{
const char *root;
const char *uuid;
};
struct validator_baton_t
{
svn_client_ctx_t *ctx;
const char *path;
apr_array_header_t *url_uuids;
apr_pool_t *pool;
};
static svn_error_t *
validator_func(void *baton,
const char *uuid,
const char *url,
const char *root_url,
apr_pool_t *pool)
{
struct validator_baton_t *b = baton;
struct url_uuid_t *url_uuid = NULL;
const char *disable_checks;
apr_array_header_t *uuids = b->url_uuids;
int i;
for (i = 0; i < uuids->nelts; ++i)
{
struct url_uuid_t *uu = &APR_ARRAY_IDX(uuids, i,
struct url_uuid_t);
if (svn_uri__is_ancestor(uu->root, url))
{
url_uuid = uu;
break;
}
}
disable_checks = getenv("SVN_I_LOVE_CORRUPTED_WORKING_COPIES_SO_DISABLE_RELOCATE_VALIDATION");
if (disable_checks && (strcmp(disable_checks, "yes") == 0))
{
url_uuid = apr_pcalloc(pool, sizeof(*url_uuid));
url_uuid->root = apr_pstrdup(pool, root_url);
url_uuid->uuid = apr_pstrdup(pool, uuid);
}
if (! url_uuid)
{
apr_pool_t *sesspool = svn_pool_create(pool);
svn_ra_session_t *ra_session;
SVN_ERR(svn_client__open_ra_session_internal(&ra_session, NULL, url, NULL,
NULL, FALSE, TRUE,
b->ctx, sesspool));
url_uuid = &APR_ARRAY_PUSH(uuids, struct url_uuid_t);
SVN_ERR(svn_ra_get_uuid2(ra_session, &(url_uuid->uuid), pool));
SVN_ERR(svn_ra_get_repos_root2(ra_session, &(url_uuid->root), pool));
svn_pool_destroy(sesspool);
}
if (root_url
&& strcmp(root_url, url_uuid->root) != 0)
return svn_error_createf(SVN_ERR_CLIENT_INVALID_RELOCATION, NULL,
_("'%s' is not the root of the repository"),
url);
if (uuid && strcmp(uuid, url_uuid->uuid) != 0)
return svn_error_createf
(SVN_ERR_CLIENT_INVALID_RELOCATION, NULL,
_("The repository at '%s' has uuid '%s', but the WC has '%s'"),
url, url_uuid->uuid, uuid);
return SVN_NO_ERROR;
}
static svn_error_t *
relocate_externals(const char *local_abspath,
apr_array_header_t *ext_desc,
const char *old_parent_repos_root_url,
const char *new_parent_repos_root_url,
svn_boolean_t ignore_externals,
svn_client_ctx_t *ctx,
apr_pool_t *scratch_pool)
{
const char *url;
apr_pool_t *iterpool;
int i;
SVN_ERR(svn_client_url_from_path2(&url, local_abspath, ctx,
scratch_pool, scratch_pool));
iterpool = svn_pool_create(scratch_pool);
for (i = 0; i < ext_desc->nelts; i++)
{
svn_wc_external_item2_t *ext_item =
APR_ARRAY_IDX(ext_desc, i, svn_wc_external_item2_t *);
const char *target_repos_root_url;
const char *target_abspath;
svn_error_t *err;
svn_pool_clear(iterpool);
if (! ((strncmp("../", ext_item->url, 3) == 0) ||
(strncmp("^/", ext_item->url, 2) == 0)))
continue;
SVN_ERR(svn_dirent_get_absolute(&target_abspath,
svn_dirent_join(local_abspath,
ext_item->target_dir,
iterpool),
iterpool));
err = svn_client_root_url_from_path(&target_repos_root_url,
target_abspath, ctx, iterpool);
if (err && err->apr_err == SVN_ERR_WC_PATH_NOT_FOUND)
{
svn_error_clear(err);
continue;
}
else
SVN_ERR(err);
if (strcmp(target_repos_root_url, old_parent_repos_root_url) == 0)
SVN_ERR(svn_client_relocate2(target_abspath,
old_parent_repos_root_url,
new_parent_repos_root_url,
ignore_externals, ctx, iterpool));
}
svn_pool_destroy(iterpool);
return SVN_NO_ERROR;
}
svn_error_t *
svn_client_relocate2(const char *wcroot_dir,
const char *from_prefix,
const char *to_prefix,
svn_boolean_t ignore_externals,
svn_client_ctx_t *ctx,
apr_pool_t *pool)
{
struct validator_baton_t vb;
const char *local_abspath;
apr_hash_t *externals_hash = NULL;
apr_hash_index_t *hi;
apr_pool_t *iterpool = NULL;
const char *old_repos_root_url, *new_repos_root_url;
vb.ctx = ctx;
vb.path = wcroot_dir;
vb.url_uuids = apr_array_make(pool, 1, sizeof(struct url_uuid_t));
vb.pool = pool;
if (svn_path_is_url(wcroot_dir))
return svn_error_createf(SVN_ERR_ILLEGAL_TARGET, NULL,
_("'%s' is not a local path"),
wcroot_dir);
SVN_ERR(svn_dirent_get_absolute(&local_abspath, wcroot_dir, pool));
if (ignore_externals)
{
return svn_error_trace(svn_wc_relocate4(ctx->wc_ctx, local_abspath,
from_prefix, to_prefix,
validator_func, &vb, pool));
}
SVN_ERR(svn_client_root_url_from_path(&old_repos_root_url, local_abspath,
ctx, pool));
SVN_ERR(svn_wc_relocate4(ctx->wc_ctx, local_abspath, from_prefix, to_prefix,
validator_func, &vb, pool));
SVN_ERR(svn_client_root_url_from_path(&new_repos_root_url, local_abspath,
ctx, pool));
SVN_ERR(svn_wc__externals_gather_definitions(&externals_hash, NULL,
ctx->wc_ctx, local_abspath,
svn_depth_infinity,
pool, pool));
if (! apr_hash_count(externals_hash))
return SVN_NO_ERROR;
iterpool = svn_pool_create(pool);
for (hi = apr_hash_first(pool, externals_hash);
hi != NULL;
hi = apr_hash_next(hi))
{
const char *this_abspath = svn__apr_hash_index_key(hi);
const char *value = svn__apr_hash_index_val(hi);
apr_array_header_t *ext_desc;
svn_pool_clear(iterpool);
SVN_ERR(svn_wc_parse_externals_description3(&ext_desc, this_abspath,
value, FALSE,
iterpool));
if (ext_desc->nelts)
SVN_ERR(relocate_externals(this_abspath, ext_desc, old_repos_root_url,
new_repos_root_url, ignore_externals, ctx,
iterpool));
}
svn_pool_destroy(iterpool);
return SVN_NO_ERROR;
}