#include "svn_hash.h"
#include "svn_pools.h"
#include "svn_repos.h"
#include "svn_path.h"
#include "svn_props.h"
#include "svn_subst.h"
#include "svn_dirent_uri.h"
#include "private/svn_fspath.h"
#include "svnrdump.h"
#define ARE_VALID_COPY_ARGS(p,r) ((p) && SVN_IS_VALID_REVNUM(r))
#if 0
#define LDR_DBG(x) SVN_DBG(x)
#else
#define LDR_DBG(x) while(0)
#endif
struct dir_baton
{
struct dump_edit_baton *eb;
struct dir_baton *parent_dir_baton;
svn_boolean_t added;
svn_boolean_t written_out;
const char *abspath;
const char *copyfrom_path;
svn_revnum_t copyfrom_rev;
apr_hash_t *deleted_entries;
};
struct handler_baton
{
svn_txdelta_window_handler_t apply_handler;
void *apply_baton;
};
struct dump_edit_baton {
svn_stream_t *stream;
apr_pool_t *pool;
apr_hash_t *props;
apr_hash_t *deleted_props;
svn_stringbuf_t *propstring;
const char *delta_abspath;
apr_file_t *delta_file;
const char *base_checksum;
svn_boolean_t dump_text;
svn_boolean_t dump_props;
svn_boolean_t dump_newlines;
};
static struct dir_baton *
make_dir_baton(const char *path,
const char *copyfrom_path,
svn_revnum_t copyfrom_rev,
void *edit_baton,
void *parent_dir_baton,
svn_boolean_t added,
apr_pool_t *pool)
{
struct dump_edit_baton *eb = edit_baton;
struct dir_baton *pb = parent_dir_baton;
struct dir_baton *new_db = apr_pcalloc(pool, sizeof(*new_db));
const char *abspath;
if (pb)
abspath = svn_fspath__canonicalize(path, pool);
else
abspath = "/";
if (copyfrom_path)
copyfrom_path = svn_relpath_canonicalize(copyfrom_path, pool);
new_db->eb = eb;
new_db->parent_dir_baton = pb;
new_db->abspath = abspath;
new_db->copyfrom_path = copyfrom_path;
new_db->copyfrom_rev = copyfrom_rev;
new_db->added = added;
new_db->written_out = FALSE;
new_db->deleted_entries = apr_hash_make(pool);
return new_db;
}
static svn_error_t *
do_dump_props(struct dump_edit_baton *eb,
svn_boolean_t *trigger_var,
svn_boolean_t dump_data_too,
apr_pool_t *pool)
{
svn_stream_t *propstream;
apr_hash_t *normal_props;
if (trigger_var && !*trigger_var)
return SVN_NO_ERROR;
SVN_ERR(svn_rdump__normalize_props(&normal_props, eb->props, eb->pool));
svn_stringbuf_setempty(eb->propstring);
propstream = svn_stream_from_stringbuf(eb->propstring, eb->pool);
SVN_ERR(svn_hash_write_incremental(normal_props, eb->deleted_props,
propstream, "PROPS-END", pool));
SVN_ERR(svn_stream_close(propstream));
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_PROP_DELTA
": true\n"));
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_PROP_CONTENT_LENGTH
": %" APR_SIZE_T_FMT "\n", eb->propstring->len));
if (dump_data_too)
{
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_CONTENT_LENGTH
": %" APR_SIZE_T_FMT "\n\n",
eb->propstring->len));
SVN_ERR(svn_stream_write(eb->stream, eb->propstring->data,
&(eb->propstring->len)));
SVN_ERR(svn_stream_printf(eb->stream, pool, "\n\n"));
SVN_ERR(svn_hash__clear(eb->props, eb->pool));
SVN_ERR(svn_hash__clear(eb->deleted_props, eb->pool));
if (trigger_var)
*trigger_var = FALSE;
}
return SVN_NO_ERROR;
}
static svn_error_t *
do_dump_newlines(struct dump_edit_baton *eb,
svn_boolean_t *trigger_var,
apr_pool_t *pool)
{
if (trigger_var && *trigger_var)
{
SVN_ERR(svn_stream_printf(eb->stream, pool, "\n\n"));
*trigger_var = FALSE;
}
return SVN_NO_ERROR;
}
static svn_error_t *
dump_node(struct dump_edit_baton *eb,
const char *path,
svn_node_kind_t kind,
enum svn_node_action action,
svn_boolean_t is_copy,
const char *copyfrom_path,
svn_revnum_t copyfrom_rev,
apr_pool_t *pool)
{
if (path)
path = svn_relpath_canonicalize(path, pool);
if (copyfrom_path)
copyfrom_path = svn_relpath_canonicalize(copyfrom_path, pool);
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_NODE_PATH ": %s\n", path));
if (kind == svn_node_file)
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_NODE_KIND ": file\n"));
else if (kind == svn_node_dir)
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_NODE_KIND ": dir\n"));
switch (action)
{
case svn_node_action_change:
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_NODE_ACTION
": change\n"));
break;
case svn_node_action_replace:
if (!is_copy)
{
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_NODE_ACTION
": replace\n"));
eb->dump_props = TRUE;
break;
}
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_NODE_ACTION
": delete\n\n"));
SVN_ERR(dump_node(eb, path, kind, svn_node_action_add,
is_copy, copyfrom_path, copyfrom_rev, pool));
break;
case svn_node_action_delete:
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_NODE_ACTION
": delete\n"));
SVN_ERR(svn_stream_printf(eb->stream, pool, "\n\n"));
break;
case svn_node_action_add:
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_NODE_ACTION ": add\n"));
if (!is_copy)
{
eb->dump_props = TRUE;
break;
}
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_NODE_COPYFROM_REV
": %ld\n"
SVN_REPOS_DUMPFILE_NODE_COPYFROM_PATH
": %s\n",
copyfrom_rev, copyfrom_path));
if (kind == svn_node_dir)
eb->dump_newlines = TRUE;
break;
}
return SVN_NO_ERROR;
}
static svn_error_t *
open_root(void *edit_baton,
svn_revnum_t base_revision,
apr_pool_t *pool,
void **root_baton)
{
struct dump_edit_baton *eb = edit_baton;
svn_pool_clear(eb->pool);
eb->props = apr_hash_make(eb->pool);
eb->deleted_props = apr_hash_make(eb->pool);
eb->propstring = svn_stringbuf_create("", eb->pool);
*root_baton = make_dir_baton(NULL, NULL, SVN_INVALID_REVNUM,
edit_baton, NULL, FALSE, eb->pool);
LDR_DBG(("open_root %p\n", *root_baton));
return SVN_NO_ERROR;
}
static svn_error_t *
delete_entry(const char *path,
svn_revnum_t revision,
void *parent_baton,
apr_pool_t *pool)
{
struct dir_baton *pb = parent_baton;
LDR_DBG(("delete_entry %s\n", path));
SVN_ERR(do_dump_props(pb->eb, &(pb->eb->dump_props), TRUE, pool));
SVN_ERR(do_dump_newlines(pb->eb, &(pb->eb->dump_newlines), pool));
apr_hash_set(pb->deleted_entries, apr_pstrdup(pb->eb->pool, path),
APR_HASH_KEY_STRING, pb);
return SVN_NO_ERROR;
}
static svn_error_t *
add_directory(const char *path,
void *parent_baton,
const char *copyfrom_path,
svn_revnum_t copyfrom_rev,
apr_pool_t *pool,
void **child_baton)
{
struct dir_baton *pb = parent_baton;
void *val;
struct dir_baton *new_db;
svn_boolean_t is_copy;
LDR_DBG(("add_directory %s\n", path));
new_db = make_dir_baton(path, copyfrom_path, copyfrom_rev, pb->eb,
pb, TRUE, pb->eb->pool);
SVN_ERR(do_dump_props(pb->eb, &(pb->eb->dump_props), TRUE, pool));
SVN_ERR(do_dump_newlines(pb->eb, &(pb->eb->dump_newlines), pool));
val = apr_hash_get(pb->deleted_entries, path, APR_HASH_KEY_STRING);
is_copy = ARE_VALID_COPY_ARGS(copyfrom_path, copyfrom_rev);
SVN_ERR(dump_node(pb->eb, path,
svn_node_dir,
val ? svn_node_action_replace : svn_node_action_add,
is_copy,
is_copy ? copyfrom_path : NULL,
is_copy ? copyfrom_rev : SVN_INVALID_REVNUM,
pool));
if (val)
apr_hash_set(pb->deleted_entries, path, APR_HASH_KEY_STRING, NULL);
new_db->written_out = TRUE;
*child_baton = new_db;
return SVN_NO_ERROR;
}
static svn_error_t *
open_directory(const char *path,
void *parent_baton,
svn_revnum_t base_revision,
apr_pool_t *pool,
void **child_baton)
{
struct dir_baton *pb = parent_baton;
struct dir_baton *new_db;
const char *copyfrom_path = NULL;
svn_revnum_t copyfrom_rev = SVN_INVALID_REVNUM;
LDR_DBG(("open_directory %s\n", path));
SVN_ERR(do_dump_props(pb->eb, &(pb->eb->dump_props), TRUE, pool));
SVN_ERR(do_dump_newlines(pb->eb, &(pb->eb->dump_newlines), pool));
if (ARE_VALID_COPY_ARGS(pb->copyfrom_path, pb->copyfrom_rev))
{
copyfrom_path = svn_relpath_join(pb->copyfrom_path,
svn_relpath_basename(path, NULL),
pb->eb->pool);
copyfrom_rev = pb->copyfrom_rev;
}
new_db = make_dir_baton(path, copyfrom_path, copyfrom_rev, pb->eb, pb,
FALSE, pb->eb->pool);
*child_baton = new_db;
return SVN_NO_ERROR;
}
static svn_error_t *
close_directory(void *dir_baton,
apr_pool_t *pool)
{
struct dir_baton *db = dir_baton;
struct dump_edit_baton *eb = db->eb;
apr_hash_index_t *hi;
LDR_DBG(("close_directory %p\n", dir_baton));
SVN_ERR(do_dump_props(eb, &(eb->dump_props), TRUE, pool));
SVN_ERR(do_dump_newlines(eb, &(eb->dump_newlines), pool));
for (hi = apr_hash_first(pool, db->deleted_entries); hi;
hi = apr_hash_next(hi))
{
const char *path = svn__apr_hash_index_key(hi);
SVN_ERR(dump_node(db->eb, path, svn_node_unknown, svn_node_action_delete,
FALSE, NULL, SVN_INVALID_REVNUM, pool));
}
SVN_ERR(svn_hash__clear(db->deleted_entries, pool));
return SVN_NO_ERROR;
}
static svn_error_t *
add_file(const char *path,
void *parent_baton,
const char *copyfrom_path,
svn_revnum_t copyfrom_rev,
apr_pool_t *pool,
void **file_baton)
{
struct dir_baton *pb = parent_baton;
void *val;
svn_boolean_t is_copy;
LDR_DBG(("add_file %s\n", path));
SVN_ERR(do_dump_props(pb->eb, &(pb->eb->dump_props), TRUE, pool));
SVN_ERR(do_dump_newlines(pb->eb, &(pb->eb->dump_newlines), pool));
val = apr_hash_get(pb->deleted_entries, path, APR_HASH_KEY_STRING);
is_copy = ARE_VALID_COPY_ARGS(copyfrom_path, copyfrom_rev);
SVN_ERR(dump_node(pb->eb, path,
svn_node_file,
val ? svn_node_action_replace : svn_node_action_add,
is_copy,
is_copy ? copyfrom_path : NULL,
is_copy ? copyfrom_rev : SVN_INVALID_REVNUM,
pool));
if (val)
apr_hash_set(pb->deleted_entries, path, APR_HASH_KEY_STRING, NULL);
*file_baton = pb->eb;
return SVN_NO_ERROR;
}
static svn_error_t *
open_file(const char *path,
void *parent_baton,
svn_revnum_t ancestor_revision,
apr_pool_t *pool,
void **file_baton)
{
struct dir_baton *pb = parent_baton;
const char *copyfrom_path = NULL;
svn_revnum_t copyfrom_rev = SVN_INVALID_REVNUM;
LDR_DBG(("open_file %s\n", path));
SVN_ERR(do_dump_props(pb->eb, &(pb->eb->dump_props), TRUE, pool));
SVN_ERR(do_dump_newlines(pb->eb, &(pb->eb->dump_newlines), pool));
if (ARE_VALID_COPY_ARGS(pb->copyfrom_path, pb->copyfrom_rev))
{
copyfrom_path = svn_relpath_join(pb->copyfrom_path,
svn_relpath_basename(path, NULL),
pb->eb->pool);
copyfrom_rev = pb->copyfrom_rev;
}
SVN_ERR(dump_node(pb->eb, path, svn_node_file, svn_node_action_change,
FALSE, copyfrom_path, copyfrom_rev, pool));
*file_baton = pb->eb;
return SVN_NO_ERROR;
}
static svn_error_t *
change_dir_prop(void *parent_baton,
const char *name,
const svn_string_t *value,
apr_pool_t *pool)
{
struct dir_baton *db = parent_baton;
LDR_DBG(("change_dir_prop %p\n", parent_baton));
if (svn_property_kind(NULL, name) != svn_prop_regular_kind)
return SVN_NO_ERROR;
if (value)
apr_hash_set(db->eb->props, apr_pstrdup(db->eb->pool, name),
APR_HASH_KEY_STRING, svn_string_dup(value, db->eb->pool));
else
apr_hash_set(db->eb->deleted_props, apr_pstrdup(db->eb->pool, name),
APR_HASH_KEY_STRING, "");
if (! db->written_out)
{
SVN_ERR(dump_node(db->eb, db->abspath, svn_node_dir,
svn_node_action_change, FALSE, db->copyfrom_path,
db->copyfrom_rev, pool));
db->written_out = TRUE;
}
db->eb->dump_newlines = FALSE;
db->eb->dump_props = TRUE;
return SVN_NO_ERROR;
}
static svn_error_t *
change_file_prop(void *file_baton,
const char *name,
const svn_string_t *value,
apr_pool_t *pool)
{
struct dump_edit_baton *eb = file_baton;
LDR_DBG(("change_file_prop %p\n", file_baton));
if (svn_property_kind(NULL, name) != svn_prop_regular_kind)
return SVN_NO_ERROR;
if (value)
apr_hash_set(eb->props, apr_pstrdup(eb->pool, name),
APR_HASH_KEY_STRING, svn_string_dup(value, eb->pool));
else
apr_hash_set(eb->deleted_props, apr_pstrdup(eb->pool, name),
APR_HASH_KEY_STRING, "");
eb->dump_props = TRUE;
return SVN_NO_ERROR;
}
static svn_error_t *
window_handler(svn_txdelta_window_t *window, void *baton)
{
struct handler_baton *hb = baton;
static svn_error_t *err;
err = hb->apply_handler(window, hb->apply_baton);
if (window != NULL && !err)
return SVN_NO_ERROR;
if (err)
SVN_ERR(err);
return SVN_NO_ERROR;
}
static svn_error_t *
apply_textdelta(void *file_baton, const char *base_checksum,
apr_pool_t *pool,
svn_txdelta_window_handler_t *handler,
void **handler_baton)
{
struct dump_edit_baton *eb = file_baton;
struct handler_baton *hb;
svn_stream_t *delta_filestream;
hb = apr_pcalloc(eb->pool, sizeof(*hb));
LDR_DBG(("apply_textdelta %p\n", file_baton));
delta_filestream = svn_stream_from_aprfile2(eb->delta_file, TRUE, pool);
svn_txdelta_to_svndiff3(&(hb->apply_handler), &(hb->apply_baton),
delta_filestream, 0,
SVN_DELTA_COMPRESSION_LEVEL_DEFAULT, pool);
eb->dump_text = TRUE;
eb->base_checksum = apr_pstrdup(eb->pool, base_checksum);
SVN_ERR(svn_stream_close(delta_filestream));
*handler = window_handler;
*handler_baton = hb;
return SVN_NO_ERROR;
}
static svn_error_t *
close_file(void *file_baton,
const char *text_checksum,
apr_pool_t *pool)
{
struct dump_edit_baton *eb = file_baton;
apr_finfo_t *info = apr_pcalloc(pool, sizeof(apr_finfo_t));
LDR_DBG(("close_file %p\n", file_baton));
SVN_ERR(do_dump_props(eb, &(eb->dump_props), FALSE, pool));
if (eb->dump_text)
{
apr_status_t err;
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_TEXT_DELTA
": true\n"));
err = apr_file_info_get(info, APR_FINFO_SIZE, eb->delta_file);
if (err)
SVN_ERR(svn_error_wrap_apr(err, NULL));
if (eb->base_checksum)
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_TEXT_DELTA_BASE_MD5
": %s\n",
eb->base_checksum));
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_TEXT_CONTENT_LENGTH
": %lu\n",
(unsigned long)info->size));
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_TEXT_CONTENT_MD5
": %s\n",
text_checksum));
}
if (eb->dump_props)
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_CONTENT_LENGTH
": %ld\n\n",
(unsigned long)info->size + eb->propstring->len));
else if (eb->dump_text)
SVN_ERR(svn_stream_printf(eb->stream, pool,
SVN_REPOS_DUMPFILE_CONTENT_LENGTH
": %ld\n\n",
(unsigned long)info->size));
if (eb->dump_props)
{
SVN_ERR(svn_stream_write(eb->stream, eb->propstring->data,
&(eb->propstring->len)));
eb->dump_props = FALSE;
SVN_ERR(svn_hash__clear(eb->props, eb->pool));
SVN_ERR(svn_hash__clear(eb->deleted_props, eb->pool));
}
if (eb->dump_text)
{
svn_stream_t *delta_filestream;
apr_off_t offset = 0;
SVN_ERR(svn_io_file_seek(eb->delta_file, APR_SET, &offset, pool));
delta_filestream = svn_stream_from_aprfile2(eb->delta_file, TRUE, pool);
SVN_ERR(svn_stream_copy3(delta_filestream, eb->stream, NULL, NULL, pool));
SVN_ERR(svn_stream_close(delta_filestream));
SVN_ERR(svn_io_file_trunc(eb->delta_file, 0, pool));
eb->dump_text = FALSE;
}
SVN_ERR(svn_stream_printf(eb->stream, pool, "\n\n"));
return SVN_NO_ERROR;
}
static svn_error_t *
close_edit(void *edit_baton, apr_pool_t *pool)
{
return SVN_NO_ERROR;
}
svn_error_t *
svn_rdump__get_dump_editor(const svn_delta_editor_t **editor,
void **edit_baton,
svn_stream_t *stream,
svn_cancel_func_t cancel_func,
void *cancel_baton,
apr_pool_t *pool)
{
struct dump_edit_baton *eb;
svn_delta_editor_t *de;
eb = apr_pcalloc(pool, sizeof(struct dump_edit_baton));
eb->stream = stream;
eb->pool = svn_pool_create(pool);
SVN_ERR(svn_io_open_unique_file3(&(eb->delta_file), &(eb->delta_abspath),
NULL, svn_io_file_del_on_close, pool, pool));
de = svn_delta_default_editor(pool);
de->open_root = open_root;
de->delete_entry = delete_entry;
de->add_directory = add_directory;
de->open_directory = open_directory;
de->close_directory = close_directory;
de->change_dir_prop = change_dir_prop;
de->change_file_prop = change_file_prop;
de->apply_textdelta = apply_textdelta;
de->add_file = add_file;
de->open_file = open_file;
de->close_file = close_file;
de->close_edit = close_edit;
*edit_baton = eb;
*editor = de;
return svn_delta_get_cancellation_editor(cancel_func, cancel_baton,
de, eb, editor, edit_baton, pool);
}