2 * export-cmd.c -- Subversion export command
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_error.h"
32 #include "svn_dirent_uri.h"
34 #include "svn_cmdline.h"
37 #include "svn_private_config.h"
38 #include "private/svn_string_private.h"
39 #include "private/svn_client_private.h"
41 /*** The export editor code. ***/
43 /* ---------------------------------------------------------------------- */
45 /*** A dedicated 'export' editor, which does no .svn/ accounting. ***/
47 typedef struct edit_baton_t
49 apr_int64_t file_count;
50 apr_int64_t dir_count;
51 apr_int64_t byte_count;
52 apr_int64_t prop_count;
53 apr_int64_t prop_byte_count;
57 set_target_revision(void *edit_baton,
58 svn_revnum_t target_revision,
65 /* Just ensure that the main export directory exists. */
67 open_root(void *edit_baton,
68 svn_revnum_t base_revision,
72 *root_baton = edit_baton;
77 /* Ensure the directory exists, and send feedback. */
79 add_directory(const char *path,
81 const char *copyfrom_path,
82 svn_revnum_t copyfrom_revision,
86 edit_baton_t *eb = parent_baton;
89 *baton = parent_baton;
94 /* Build a file baton. */
96 add_file(const char *path,
98 const char *copyfrom_path,
99 svn_revnum_t copyfrom_revision,
103 edit_baton_t *eb = parent_baton;
106 *baton = parent_baton;
111 window_handler(svn_txdelta_window_t *window, void *baton)
113 edit_baton_t *eb = baton;
115 eb->byte_count += window->tview_len;
120 /* Write incoming data into the tmpfile stream */
123 apply_textdelta(void *file_baton,
124 const char *base_checksum,
126 svn_txdelta_window_handler_t *handler,
127 void **handler_baton)
129 *handler_baton = file_baton;
130 *handler = window_handler;
136 change_file_prop(void *file_baton,
138 const svn_string_t *value,
141 edit_baton_t *eb = file_baton;
143 eb->prop_byte_count += value->len;
149 change_dir_prop(void *dir_baton,
151 const svn_string_t *value,
154 edit_baton_t *eb = dir_baton;
161 close_file(void *file_baton,
162 const char *text_checksum,
168 /* Implement svn_write_fn_t, simply counting the incoming data. */
170 file_write_handler(void *baton, const char *data, apr_size_t *len)
172 edit_baton_t *eb = baton;
173 eb->byte_count += *len;
178 /*** Public Interfaces ***/
181 bench_null_export(svn_revnum_t *result_rev,
182 const char *from_path_or_url,
183 svn_opt_revision_t *peg_revision,
184 svn_opt_revision_t *revision,
187 svn_client_ctx_t *ctx,
191 svn_revnum_t edit_revision = SVN_INVALID_REVNUM;
192 svn_boolean_t from_is_url = svn_path_is_url(from_path_or_url);
194 SVN_ERR_ASSERT(peg_revision != NULL);
195 SVN_ERR_ASSERT(revision != NULL);
197 if (peg_revision->kind == svn_opt_revision_unspecified)
198 peg_revision->kind = svn_path_is_url(from_path_or_url)
199 ? svn_opt_revision_head
200 : svn_opt_revision_working;
202 if (revision->kind == svn_opt_revision_unspecified)
203 revision = peg_revision;
205 if (from_is_url || ! SVN_CLIENT__REVKIND_IS_LOCAL_TO_WC(revision->kind))
207 svn_client__pathrev_t *loc;
208 svn_ra_session_t *ra_session;
209 svn_node_kind_t kind;
210 edit_baton_t *eb = baton;
212 /* Get the RA connection. */
213 SVN_ERR(svn_client__ra_session_from_path2(&ra_session, &loc,
214 from_path_or_url, NULL,
216 revision, ctx, pool));
218 SVN_ERR(svn_ra_check_path(ra_session, "", loc->rev, &kind, pool));
220 if (kind == svn_node_file)
224 /* Since we don't use the editor, we must count "manually". */
225 svn_stream_t *stream = svn_stream_create(eb, pool);
226 svn_stream_set_write(stream, file_write_handler);
229 /* Since you cannot actually root an editor at a file, we
230 * manually drive a few functions of our editor. */
232 /* Step outside the editor-likeness for a moment, to actually talk
233 * to the repository. */
234 /* ### note: the stream will not be closed */
235 SVN_ERR(svn_ra_get_file(ra_session, "", loc->rev,
236 stream, NULL, &props, pool));
238 else if (kind == svn_node_dir)
240 void *edit_baton = NULL;
241 const svn_delta_editor_t *export_editor = NULL;
242 const svn_ra_reporter3_t *reporter;
245 svn_delta_editor_t *editor = svn_delta_default_editor(pool);
247 editor->set_target_revision = set_target_revision;
248 editor->open_root = open_root;
249 editor->add_directory = add_directory;
250 editor->add_file = add_file;
251 editor->apply_textdelta = apply_textdelta;
252 editor->close_file = close_file;
253 editor->change_file_prop = change_file_prop;
254 editor->change_dir_prop = change_dir_prop;
256 /* for ra_svn, we don't need an editior in quiet mode */
257 if (!quiet || strncmp(loc->repos_root_url, "svn:", 4))
258 SVN_ERR(svn_delta_get_cancellation_editor(ctx->cancel_func,
266 /* Manufacture a basic 'report' to the update reporter. */
267 SVN_ERR(svn_ra_do_update3(ra_session,
268 &reporter, &report_baton,
270 "", /* no sub-target */
272 FALSE, /* don't want copyfrom-args */
273 FALSE, /* don't want ignore_ancestry */
274 export_editor, edit_baton,
277 SVN_ERR(reporter->set_path(report_baton, "", loc->rev,
278 /* Depth is irrelevant, as we're
279 passing start_empty=TRUE anyway. */
281 TRUE, /* "help, my dir is empty!" */
284 SVN_ERR(reporter->finish_report(report_baton, pool));
286 /* We don't receive the "add directory" callback for the starting
290 else if (kind == svn_node_none)
292 return svn_error_createf(SVN_ERR_RA_ILLEGAL_URL, NULL,
293 _("URL '%s' doesn't exist"),
296 /* kind == svn_node_unknown not handled */
301 *result_rev = edit_revision;
309 /* This implements the `svn_opt_subcommand_t' interface. */
311 svn_cl__null_export(apr_getopt_t *os,
315 svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
316 svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
318 apr_array_header_t *targets;
320 svn_opt_revision_t peg_revision;
321 const char *truefrom;
322 edit_baton_t eb = { 0 };
324 SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
328 /* We want exactly 1 or 2 targets for this subcommand. */
329 if (targets->nelts < 1)
330 return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
331 if (targets->nelts > 2)
332 return svn_error_create(SVN_ERR_CL_ARG_PARSING_ERROR, 0, NULL);
334 /* The first target is the `from' path. */
335 from = APR_ARRAY_IDX(targets, 0, const char *);
337 /* Get the peg revision if present. */
338 SVN_ERR(svn_opt_parse_path(&peg_revision, &truefrom, from, pool));
340 if (opt_state->depth == svn_depth_unknown)
341 opt_state->depth = svn_depth_infinity;
344 err = bench_null_export(NULL, truefrom, &peg_revision,
345 &(opt_state->start_revision),
348 ctx, opt_state->quiet, pool);
350 if (!opt_state->quiet)
351 SVN_ERR(svn_cmdline_printf(pool,
352 _("%15s directories\n"
354 "%15s bytes in files\n"
356 "%15s bytes in properties\n"),
357 svn__ui64toa_sep(eb.dir_count, ',', pool),
358 svn__ui64toa_sep(eb.file_count, ',', pool),
359 svn__ui64toa_sep(eb.byte_count, ',', pool),
360 svn__ui64toa_sep(eb.prop_count, ',', pool),
361 svn__ui64toa_sep(eb.prop_byte_count, ',', pool)));
363 return svn_error_trace(err);