4 * Walk through a given revision of a local Subversion repository and export
5 * all of the contents as a tarfile.
7 * Author: Chris Lee <clee@kde.org>
8 * License: MIT <http://www.opensource.org/licenses/mit-license.php>
21 #include <apr_general.h>
22 #include <apr_strings.h>
23 #include <apr_getopt.h>
26 #include <svn_types.h>
27 #include <svn_pools.h>
28 #include <svn_repos.h>
32 #define SVN_ERR(expr) SVN_INT_ERR(expr)
33 #define apr_sane_push(arr, contents) *(char **)apr_array_push(arr) = contents
35 #define TRUNK "/trunk"
37 static time_t archive_time
;
39 time_t get_epoch(char *svn_date
)
42 char *date
= malloc(strlen(svn_date
) * sizeof(char *));
43 strncpy(date
, svn_date
, strlen(svn_date
) - 8);
44 strptime(date
, "%Y-%m-%dT%H:%M:%S", &tm
);
49 int tar_header(apr_pool_t
*pool
, char *path
, char *node
, size_t f_size
)
52 unsigned int i
, checksum
;
55 memset(buf
, 0, sizeof(buf
));
57 if ((strlen(path
) == 0) && (strlen(node
) == 0)) {
61 if (strlen(node
) == 0) {
67 if (strlen(path
) == 0) {
68 strncpy(buf
, apr_psprintf(pool
, "%s", node
), 99);
69 } else if (strlen(path
) + strlen(node
) < 100) {
70 strncpy(buf
, apr_psprintf(pool
, "%s/%s", path
+1, node
), 99);
72 fprintf(stderr
, "really long file path...\n");
73 strncpy(&buf
[0], node
, 99);
74 strncpy(&buf
[345], path
+1, 154);
77 strncpy(&buf
[100], apr_psprintf(pool
, "%07o", (is_dir
? 0755 : 0644)), 7);
78 strncpy(&buf
[108], apr_psprintf(pool
, "%07o", 1000), 7);
79 strncpy(&buf
[116], apr_psprintf(pool
, "%07o", 1000), 7);
80 strncpy(&buf
[124], apr_psprintf(pool
, "%011lo", f_size
), 11);
81 strncpy(&buf
[136], apr_psprintf(pool
, "%011lo", archive_time
), 11);
82 strncpy(&buf
[156], (is_dir
? "5" : "0"), 1);
83 strncpy(&buf
[257], "ustar ", 8);
84 strncpy(&buf
[265], "clee", 31);
85 strncpy(&buf
[297], "clee", 31);
86 // strncpy(&buf[329], apr_psprintf(pool, "%07o", 0), 7);
87 // strncpy(&buf[337], apr_psprintf(pool, "%07o", 0), 7);
89 strncpy(&buf
[148], " ", 8);
91 for (i
= 0; i
< sizeof(buf
); i
++) {
94 strncpy(&buf
[148], apr_psprintf(pool
, "%07o", checksum
& 0x1fffff), 7);
96 fwrite(buf
, sizeof(char), sizeof(buf
), stdout
);
104 memset(block
, 0, sizeof(block
));
105 fwrite(block
, sizeof(char), sizeof(block
), stdout
);
108 int dump_blob(svn_fs_root_t
*root
, char *prefix
, char *path
, char *node
, apr_pool_t
*pool
)
110 char *full_path
, buf
[512];
112 svn_stream_t
*stream
;
113 svn_filesize_t stream_length
;
115 full_path
= apr_psprintf(pool
, "%s%s/%s", prefix
, path
, node
);
117 SVN_ERR(svn_fs_file_length(&stream_length
, root
, full_path
, pool
));
118 SVN_ERR(svn_fs_file_contents(&stream
, root
, full_path
, pool
));
120 tar_header(pool
, path
, node
, stream_length
);
124 memset(buf
, '\0', sizeof(buf
));
125 SVN_ERR(svn_stream_read(stream
, buf
, &len
));
126 fwrite(buf
, sizeof(char), sizeof(buf
), stdout
);
127 } while (len
== sizeof(buf
));
132 int dump_tree(svn_fs_root_t
*root
, char *prefix
, char *path
, apr_pool_t
*pool
)
136 char *node
, *subpath
, *full_path
;
139 apr_hash_t
*dir_entries
;
142 svn_boolean_t is_dir
;
144 tar_header(pool
, path
, "", 0);
146 SVN_ERR(svn_fs_dir_entries(&dir_entries
, root
, apr_psprintf(pool
, "%s/%s", prefix
, path
), pool
));
148 subpool
= svn_pool_create(pool
);
150 for (i
= apr_hash_first(pool
, dir_entries
); i
; i
= apr_hash_next(i
)) {
151 svn_pool_clear(subpool
);
152 apr_hash_this(i
, &key
, NULL
, &val
);
155 subpath
= apr_psprintf(subpool
, "%s/%s", path
, node
);
156 full_path
= apr_psprintf(subpool
, "%s%s", prefix
, subpath
);
158 svn_fs_is_dir(&is_dir
, root
, full_path
, subpool
);
161 dump_tree(root
, prefix
, subpath
, subpool
);
163 dump_blob(root
, prefix
, path
, node
, subpool
);
167 svn_pool_destroy(subpool
);
172 int crawl_filesystem(char *repos_path
, char *root_path
, apr_pool_t
*pool
)
181 svn_string_t
*svndate
;
182 svn_revnum_t youngest_rev
, export_rev
;
183 svn_fs_root_t
*fs_root
;
185 SVN_ERR(svn_fs_initialize(pool
));
186 SVN_ERR(svn_repos_open(&repos
, repos_path
, pool
));
187 if ((fs
= svn_repos_fs(repos
)) == NULL
)
189 SVN_ERR(svn_fs_youngest_rev(&youngest_rev
, fs
, pool
));
191 export_rev
= youngest_rev
;
193 SVN_ERR(svn_fs_revision_root(&fs_root
, fs
, export_rev
, pool
));
194 SVN_ERR(svn_fs_revision_proplist(&props
, fs
, export_rev
, pool
));
196 svndate
= apr_hash_get(props
, "svn:date", APR_HASH_KEY_STRING
);
197 archive_time
= get_epoch((char *)svndate
->data
);
199 fprintf(stderr
, "Exporting archive of r%ld... \n", export_rev
);
201 dump_tree(fs_root
, root_path
, "", pool
);
205 fprintf(stderr
, "done!\n");
210 int main(int argc
, char *argv
[])
213 apr_getopt_t
*options
;
215 apr_getopt_option_t long_options
[] = {
217 { "prefix", 'p', 0 },
218 { "basename", 'b', 0 },
219 { "revision", 'r', 0 },
224 fprintf(stderr
, "usage: %s REPOS_PATH [prefix]\n", argv
[0]);
228 if (apr_initialize() != APR_SUCCESS
) {
229 fprintf(stderr
, "You lose at apr_initialize().\n");
233 pool
= svn_pool_create(NULL
);
235 crawl_filesystem(argv
[1], (argc
== 3 ? argv
[2] : TRUNK
), pool
);