1 /* Licensed under a two-clause BSD-style license.
2 * See LICENSE for details.
9 #include "svn_dirent_uri.h"
11 #include "dumpr_util.h"
13 #define ARE_VALID_COPY_ARGS(p,r) ((p) && SVN_IS_VALID_REVNUM(r))
15 /* Make a directory baton to represent the directory was path
16 (relative to EDIT_BATON's path) is PATH.
18 CMP_PATH/CMP_REV are the path/revision against which this directory
19 should be compared for changes. If either is omitted (NULL for the
20 path, SVN_INVALID_REVNUM for the rev), just compare this directory
21 PATH against itself in the previous revision.
23 PARENT_DIR_BATON is the directory baton of this directory's parent,
24 or NULL if this is the top-level directory of the edit. ADDED
25 indicated if this directory is newly added in this revision.
26 Perform all allocations in POOL. */
27 static struct dir_baton
*make_dir_baton(const char *path
,
31 void *parent_dir_baton
,
35 struct dump_edit_baton
*eb
= edit_baton
;
36 struct dir_baton
*pb
= parent_dir_baton
;
37 struct dir_baton
*new_db
= apr_pcalloc(pool
, sizeof(*new_db
));
38 const char *full_path
;
39 apr_array_header_t
*compose_path
= apr_array_make(pool
, 2, sizeof(const char *));
41 /* A path relative to nothing? I don't think so. */
42 SVN_ERR_ASSERT_NO_RETURN(!path
|| pb
);
44 /* Construct the full path of this node. */
46 APR_ARRAY_PUSH(compose_path
, const char *) = "/";
47 APR_ARRAY_PUSH(compose_path
, const char *) = path
;
48 full_path
= svn_path_compose(compose_path
, pool
);
51 full_path
= apr_pstrdup(pool
, "/");
53 /* Remove leading slashes from copyfrom paths. */
55 cmp_path
= ((*cmp_path
== '/') ? cmp_path
+ 1 : cmp_path
);
58 new_db
->parent_dir_baton
= pb
;
59 new_db
->path
= full_path
;
60 new_db
->cmp_path
= cmp_path
? apr_pstrdup(pool
, cmp_path
) : NULL
;
61 new_db
->cmp_rev
= cmp_rev
;
62 new_db
->added
= added
;
63 new_db
->written_out
= FALSE
;
64 new_db
->deleted_entries
= apr_hash_make(pool
);
71 * Write out a node record for PATH of type KIND under EB->FS_ROOT.
72 * ACTION describes what is happening to the node (see enum svn_node_action).
73 * Write record to writable EB->STREAM, using EB->BUFFER to write in chunks.
75 * If the node was itself copied, IS_COPY is TRUE and the
76 * path/revision of the copy source are in CMP_PATH/CMP_REV. If
77 * IS_COPY is FALSE, yet CMP_PATH/CMP_REV are valid, this node is part
78 * of a copied subtree.
80 static svn_error_t
*dump_node(struct dump_edit_baton
*eb
,
81 const char *path
, /* an absolute path. */
83 enum svn_node_action action
,
88 /* Write out metadata headers for this file node. */
89 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
90 SVN_REPOS_DUMPFILE_NODE_PATH
": %s\n",
91 (*path
== '/') ? path
+ 1 : path
));
93 if (kind
== svn_node_file
)
94 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
95 SVN_REPOS_DUMPFILE_NODE_KIND
": file\n"));
96 else if (kind
== svn_node_dir
)
97 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
98 SVN_REPOS_DUMPFILE_NODE_KIND
": dir\n"));
100 /* Remove leading slashes from copyfrom paths. */
102 cmp_path
= ((*cmp_path
== '/') ? cmp_path
+ 1 : cmp_path
);
105 /* Appropriately handle the four svn_node_action actions */
107 case svn_node_action_change
:
108 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
109 SVN_REPOS_DUMPFILE_NODE_ACTION
113 case svn_node_action_replace
:
115 /* a simple delete+add, implied by a single 'replace' action. */
116 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
117 SVN_REPOS_DUMPFILE_NODE_ACTION
120 eb
->dump_props_pending
= TRUE
;
123 /* More complex case: eb->is_copy is true, and
124 cmp_path/ cmp_rev are present: delete the original,
125 and then re-add it */
127 /* the path & kind headers have already been printed; just
128 add a delete action, and end the current record.*/
129 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
130 SVN_REPOS_DUMPFILE_NODE_ACTION
133 /* recurse: print an additional add-with-history record. */
134 SVN_ERR(dump_node(eb
, path
, kind
, svn_node_action_add
,
135 cmp_path
, cmp_rev
, pool
));
137 /* we can leave this routine quietly now, don't need to dump
138 any content; that was already done in the second record. */
139 eb
->must_dump_props
= FALSE
;
143 case svn_node_action_delete
:
144 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
145 SVN_REPOS_DUMPFILE_NODE_ACTION
148 /* we can leave this routine quietly now, don't need to dump
150 SVN_ERR(svn_stream_printf(eb
->stream
, pool
, "\n\n"));
151 eb
->must_dump_props
= FALSE
;
154 case svn_node_action_add
:
155 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
156 SVN_REPOS_DUMPFILE_NODE_ACTION
": add\n"));
159 /* eb->dump_props_pending for files is handled in
160 close_file which is called immediately.
161 However, directories are not closed until
162 all the work inside them have been done;
163 eb->dump_props_pending for directories is
164 handled in all the functions that can
165 possibly be called after add_directory:
166 add_directory, open_directory,
167 delete_entry, close_directory, add_file,
168 open_file and change_dir_prop;
169 change_dir_prop is a special case
172 eb
->dump_props_pending
= TRUE
;
176 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
177 SVN_REPOS_DUMPFILE_NODE_COPYFROM_REV
179 SVN_REPOS_DUMPFILE_NODE_COPYFROM_PATH
183 /* Dump the text only if apply_textdelta sets
184 eb->must_dump_text */
186 /* UGLY hack: If a directory was copied from a
187 previous revision, nothing else can be done, and
188 close_file won't be called to write two blank
189 lines; write them here */
190 if (kind
== svn_node_dir
)
191 SVN_ERR(svn_stream_printf(eb
->stream
, pool
, "\n\n"));
198 /* Dump property headers */
199 SVN_ERR(dump_props(eb
, &(eb
->must_dump_props
), FALSE
, pool
));
204 static svn_error_t
*open_root(void *edit_baton
,
205 svn_revnum_t base_revision
,
209 /* Allocate a special pool for the edit_baton to avoid pool
211 struct dump_edit_baton
*eb
= edit_baton
;
212 eb
->pool
= svn_pool_create(pool
);
213 eb
->properties
= apr_hash_make(eb
->pool
);
214 eb
->del_properties
= apr_hash_make(eb
->pool
);
215 eb
->propstring
= svn_stringbuf_create("", eb
->pool
);
218 *root_baton
= make_dir_baton(NULL
, NULL
, SVN_INVALID_REVNUM
,
219 edit_baton
, NULL
, FALSE
, pool
);
223 static svn_error_t
*delete_entry(const char *path
,
224 svn_revnum_t revision
,
228 struct dir_baton
*pb
= parent_baton
;
229 const char *mypath
= apr_pstrdup(pb
->pool
, path
);
231 /* Some pending properties to dump? */
232 SVN_ERR(dump_props(pb
->eb
, &(pb
->eb
->dump_props_pending
), TRUE
, pool
));
234 /* remember this path needs to be deleted */
235 apr_hash_set(pb
->deleted_entries
, mypath
, APR_HASH_KEY_STRING
, pb
);
240 static svn_error_t
*add_directory(const char *path
,
242 const char *copyfrom_path
,
243 svn_revnum_t copyfrom_rev
,
247 struct dir_baton
*pb
= parent_baton
;
249 struct dir_baton
*new_db
250 = make_dir_baton(path
, copyfrom_path
, copyfrom_rev
, pb
->eb
, pb
, TRUE
, pool
);
252 /* Some pending properties to dump? */
253 SVN_ERR(dump_props(pb
->eb
, &(pb
->eb
->dump_props_pending
), TRUE
, pool
));
255 /* This might be a replacement -- is the path already deleted? */
256 val
= apr_hash_get(pb
->deleted_entries
, path
, APR_HASH_KEY_STRING
);
258 /* Detect an add-with-history */
259 pb
->eb
->is_copy
= ARE_VALID_COPY_ARGS(copyfrom_path
, copyfrom_rev
);
262 SVN_ERR(dump_node(pb
->eb
, path
,
264 val
? svn_node_action_replace
: svn_node_action_add
,
265 pb
->eb
->is_copy
? copyfrom_path
: NULL
,
266 pb
->eb
->is_copy
? copyfrom_rev
: SVN_INVALID_REVNUM
,
270 /* Delete the path, it's now been dumped */
271 apr_hash_set(pb
->deleted_entries
, path
, APR_HASH_KEY_STRING
, NULL
);
273 new_db
->written_out
= TRUE
;
275 *child_baton
= new_db
;
279 static svn_error_t
*open_directory(const char *path
,
281 svn_revnum_t base_revision
,
285 struct dir_baton
*pb
= parent_baton
;
286 struct dir_baton
*new_db
;
287 const char *cmp_path
= NULL
;
288 svn_revnum_t cmp_rev
= SVN_INVALID_REVNUM
;
289 apr_array_header_t
*compose_path
= apr_array_make(pool
, 2, sizeof(const char *));
291 /* Some pending properties to dump? */
292 SVN_ERR(dump_props(pb
->eb
, &(pb
->eb
->dump_props_pending
), TRUE
, pool
));
294 /* If the parent directory has explicit comparison path and rev,
295 record the same for this one. */
296 if (pb
&& ARE_VALID_COPY_ARGS(pb
->cmp_path
, pb
->cmp_rev
)) {
297 APR_ARRAY_PUSH(compose_path
, const char *) = pb
->cmp_path
;
298 APR_ARRAY_PUSH(compose_path
, const char *) = svn_relpath_basename(path
, pool
);
299 cmp_path
= svn_path_compose(compose_path
, pool
);
300 cmp_rev
= pb
->cmp_rev
;
303 new_db
= make_dir_baton(path
, cmp_path
, cmp_rev
, pb
->eb
, pb
, FALSE
, pool
);
304 *child_baton
= new_db
;
308 static svn_error_t
*close_directory(void *dir_baton
,
311 struct dir_baton
*db
= dir_baton
;
312 struct dump_edit_baton
*eb
= db
->eb
;
313 apr_hash_index_t
*hi
;
314 apr_pool_t
*subpool
= svn_pool_create(pool
);
316 /* Some pending properties to dump? */
317 SVN_ERR(dump_props(eb
, &(eb
->dump_props_pending
), TRUE
, pool
));
319 /* Dump the directory entries */
320 for (hi
= apr_hash_first(pool
, db
->deleted_entries
); hi
;
321 hi
= apr_hash_next(hi
)) {
324 apr_hash_this(hi
, &key
, NULL
, NULL
);
327 svn_pool_clear(subpool
);
329 SVN_ERR(dump_node(db
->eb
, path
,
330 svn_node_unknown
, svn_node_action_delete
,
331 NULL
, SVN_INVALID_REVNUM
, subpool
));
334 svn_pool_destroy(subpool
);
338 static svn_error_t
*add_file(const char *path
,
340 const char *copyfrom_path
,
341 svn_revnum_t copyfrom_rev
,
345 struct dir_baton
*pb
= parent_baton
;
348 /* Some pending properties to dump? */
349 SVN_ERR(dump_props(pb
->eb
, &(pb
->eb
->dump_props_pending
), TRUE
, pool
));
351 /* This might be a replacement -- is the path already deleted? */
352 val
= apr_hash_get(pb
->deleted_entries
, path
, APR_HASH_KEY_STRING
);
354 /* Detect add-with-history. */
355 pb
->eb
->is_copy
= ARE_VALID_COPY_ARGS(copyfrom_path
, copyfrom_rev
);
358 SVN_ERR(dump_node(pb
->eb
, path
,
360 val
? svn_node_action_replace
: svn_node_action_add
,
361 pb
->eb
->is_copy
? copyfrom_path
: NULL
,
362 pb
->eb
->is_copy
? copyfrom_rev
: SVN_INVALID_REVNUM
,
366 /* delete the path, it's now been dumped. */
367 apr_hash_set(pb
->deleted_entries
, path
, APR_HASH_KEY_STRING
, NULL
);
369 /* Build a nice file baton to pass to change_file_prop and apply_textdelta */
370 pb
->eb
->changed_path
= path
;
371 *file_baton
= pb
->eb
;
376 static svn_error_t
*open_file(const char *path
,
378 svn_revnum_t ancestor_revision
,
382 struct dir_baton
*pb
= parent_baton
;
383 const char *cmp_path
= NULL
;
384 svn_revnum_t cmp_rev
= SVN_INVALID_REVNUM
;
386 /* Some pending properties to dump? */
387 SVN_ERR(dump_props(pb
->eb
, &(pb
->eb
->dump_props_pending
), TRUE
, pool
));
389 apr_array_header_t
*compose_path
= apr_array_make(pool
, 2, sizeof(const char *));
390 /* If the parent directory has explicit comparison path and rev,
391 record the same for this one. */
392 if (pb
&& ARE_VALID_COPY_ARGS(pb
->cmp_path
, pb
->cmp_rev
)) {
393 APR_ARRAY_PUSH(compose_path
, const char *) = pb
->cmp_path
;
394 APR_ARRAY_PUSH(compose_path
, const char *) = svn_relpath_basename(path
, pool
);
395 cmp_path
= svn_path_compose(compose_path
, pool
);
396 cmp_rev
= pb
->cmp_rev
;
399 SVN_ERR(dump_node(pb
->eb
, path
,
400 svn_node_file
, svn_node_action_change
,
401 cmp_path
, cmp_rev
, pool
));
403 /* Build a nice file baton to pass to change_file_prop and apply_textdelta */
404 pb
->eb
->changed_path
= path
;
405 *file_baton
= pb
->eb
;
410 static svn_error_t
*change_dir_prop(void *parent_baton
,
412 const svn_string_t
*value
,
415 struct dir_baton
*db
= parent_baton
;
417 if (svn_property_kind(NULL
, name
) != svn_prop_regular_kind
)
420 value
? apr_hash_set(db
->eb
->properties
, apr_pstrdup(pool
, name
),
421 APR_HASH_KEY_STRING
, svn_string_dup(value
, pool
)) :
422 apr_hash_set(db
->eb
->del_properties
, apr_pstrdup(pool
, name
),
423 APR_HASH_KEY_STRING
, (void *)0x1);
425 /* This function is what distinguishes between a directory that is
426 opened to merely get somewhere, vs. one that is opened because it
427 actually changed by itself */
428 if (! db
->written_out
) {
429 /* If eb->dump_props_pending was set, it means that the
430 node information corresponding to add_directory has already
431 been written; just don't unset it and dump_node will dump
432 the properties before doing anything else. If it wasn't
433 set, node information hasn't been written yet: so dump the
434 node itself before dumping the props */
436 SVN_ERR(dump_node(db
->eb
, db
->path
,
437 svn_node_dir
, svn_node_action_change
,
438 db
->cmp_path
, db
->cmp_rev
, pool
));
440 SVN_ERR(dump_props(db
->eb
, NULL
, TRUE
, pool
));
441 db
->written_out
= TRUE
;
446 static svn_error_t
*change_file_prop(void *file_baton
,
448 const svn_string_t
*value
,
451 struct dump_edit_baton
*eb
= file_baton
;
453 if (svn_property_kind(NULL
, name
) != svn_prop_regular_kind
)
456 apr_hash_set(eb
->properties
, apr_pstrdup(pool
, name
),
457 APR_HASH_KEY_STRING
, value
?
458 svn_string_dup(value
, pool
): (void *)0x1);
460 /* Dump the property headers and wait; close_file might need
461 to write text headers too depending on whether
462 apply_textdelta is called */
463 eb
->dump_props_pending
= TRUE
;
468 static svn_error_t
*window_handler(svn_txdelta_window_t
*window
, void *baton
)
470 struct handler_baton
*hb
= baton
;
471 struct dump_edit_baton
*eb
= hb
->eb
;
472 static svn_error_t
*err
;
474 err
= hb
->apply_handler(window
, hb
->apply_baton
);
475 if (window
!= NULL
&& !err
)
481 /* Write information about the filepath to hb->eb */
482 eb
->temp_filepath
= apr_pstrdup(eb
->pool
,
486 SVN_ERR(svn_io_file_close(hb
->temp_file
, hb
->pool
));
487 SVN_ERR(svn_stream_close(hb
->temp_filestream
));
488 svn_pool_destroy(hb
->pool
);
492 static svn_error_t
*apply_textdelta(void *file_baton
, const char *base_checksum
,
494 svn_txdelta_window_handler_t
*handler
,
495 void **handler_baton
)
497 struct dump_edit_baton
*eb
= file_baton
;
498 apr_status_t apr_err
;
501 /* Custom handler_baton allocated in a separate pool */
502 apr_pool_t
*handler_pool
= svn_pool_create(pool
);
503 struct handler_baton
*hb
= apr_pcalloc(handler_pool
, sizeof(*hb
));
504 hb
->pool
= handler_pool
;
507 /* Use a temporary file to measure the text-content-length */
508 SVN_ERR(svn_io_temp_dir(&tempdir
, hb
->pool
));
510 hb
->temp_filepath
= svn_dirent_join(tempdir
, "XXXXXX", hb
->pool
);
511 apr_err
= apr_file_mktemp(&(hb
->temp_file
), hb
->temp_filepath
,
512 APR_CREATE
| APR_READ
| APR_WRITE
| APR_EXCL
,
514 if (apr_err
!= APR_SUCCESS
)
515 SVN_ERR(svn_error_wrap_apr(apr_err
, NULL
));
517 hb
->temp_filestream
= svn_stream_from_aprfile2(hb
->temp_file
, TRUE
, hb
->pool
);
519 /* Prepare to write the delta to the temporary file */
520 svn_txdelta_to_svndiff2(&(hb
->apply_handler
), &(hb
->apply_baton
),
521 hb
->temp_filestream
, 0, hb
->pool
);
522 eb
->must_dump_text
= TRUE
;
524 /* The actual writing takes place when this function has finished */
525 /* Set the handler and handler_baton */
526 *handler
= window_handler
;
532 static svn_error_t
*close_file(void *file_baton
,
533 const char *text_checksum
,
536 struct dump_edit_baton
*eb
= file_baton
;
537 apr_file_t
*temp_file
;
538 svn_stream_t
*temp_filestream
;
539 apr_finfo_t
*info
= apr_pcalloc(pool
, sizeof(apr_finfo_t
));
541 /* We didn't write the property headers because we were
542 waiting for file_prop_change; write them now */
543 SVN_ERR(dump_props(eb
, &(eb
->dump_props_pending
), FALSE
, pool
));
545 /* The prop headers have already been dumped in dump_node */
546 /* Dump the text headers */
547 if (eb
->must_dump_text
) {
548 /* text-delta header */
549 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
550 SVN_REPOS_DUMPFILE_TEXT_DELTA
553 /* Measure the length */
554 SVN_ERR(svn_io_stat(info
, eb
->temp_filepath
, APR_FINFO_SIZE
, pool
));
556 /* text-content-length header */
557 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
558 SVN_REPOS_DUMPFILE_TEXT_CONTENT_LENGTH
560 (unsigned long)info
->size
));
561 /* text-content-md5 header */
562 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
563 SVN_REPOS_DUMPFILE_TEXT_CONTENT_MD5
568 /* content-length header: if both text and props are absent,
570 if (eb
->must_dump_props
|| eb
->dump_props_pending
)
571 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
572 SVN_REPOS_DUMPFILE_CONTENT_LENGTH
574 (unsigned long)info
->size
+ eb
->propstring
->len
));
575 else if (eb
->must_dump_text
)
576 SVN_ERR(svn_stream_printf(eb
->stream
, pool
,
577 SVN_REPOS_DUMPFILE_CONTENT_LENGTH
579 (unsigned long)info
->size
));
581 /* Dump the props; the propstring should have already been
582 written in dump_node or above */
583 if (eb
->must_dump_props
|| eb
->dump_props_pending
) {
584 SVN_ERR(svn_stream_write(eb
->stream
, eb
->propstring
->data
,
585 &(eb
->propstring
->len
)));
588 eb
->must_dump_props
= eb
->dump_props_pending
= FALSE
;
589 apr_hash_clear(eb
->properties
);
590 apr_hash_clear(eb
->del_properties
);
594 if (eb
->must_dump_text
) {
596 /* Open the temporary file, map it to a stream, copy
597 the stream to eb->stream, close and delete the
599 SVN_ERR(svn_io_file_open(&temp_file
, eb
->temp_filepath
, APR_READ
, 0600, pool
));
600 temp_filestream
= svn_stream_from_aprfile2(temp_file
, TRUE
, pool
);
601 SVN_ERR(svn_stream_copy3(temp_filestream
, eb
->stream
, NULL
, NULL
, pool
));
604 SVN_ERR(svn_io_file_close(temp_file
, pool
));
605 SVN_ERR(svn_stream_close(temp_filestream
));
606 SVN_ERR(svn_io_remove_file2(eb
->temp_filepath
, TRUE
, pool
));
607 eb
->must_dump_text
= FALSE
;
610 SVN_ERR(svn_stream_printf(eb
->stream
, pool
, "\n\n"));
615 static svn_error_t
*close_edit(void *edit_baton
, apr_pool_t
*pool
)
617 struct dump_edit_baton
*eb
= edit_baton
;
618 svn_pool_destroy(eb
->pool
);
619 (eb
->current_rev
) ++;
624 svn_error_t
*get_dump_editor(const svn_delta_editor_t
**editor
,
626 svn_revnum_t from_rev
,
629 struct dump_edit_baton
*eb
= apr_pcalloc(pool
, sizeof(struct dump_edit_baton
));
630 eb
->current_rev
= from_rev
;
631 SVN_ERR(svn_stream_for_stdout(&(eb
->stream
), pool
));
632 svn_delta_editor_t
*de
= svn_delta_default_editor(pool
);
634 de
->open_root
= open_root
;
635 de
->delete_entry
= delete_entry
;
636 de
->add_directory
= add_directory
;
637 de
->open_directory
= open_directory
;
638 de
->close_directory
= close_directory
;
639 de
->change_dir_prop
= change_dir_prop
;
640 de
->change_file_prop
= change_file_prop
;
641 de
->apply_textdelta
= apply_textdelta
;
642 de
->add_file
= add_file
;
643 de
->open_file
= open_file
;
644 de
->close_file
= close_file
;
645 de
->close_edit
= close_edit
;
647 /* Set the edit_baton and editor */