Merge branch 'from-jmcmullan' into develop
[fast-export.git] / svn-archive.c
blob6632d194414ce4c2a81ebae48f920ab52655430b
1 /*
2 * svn-archive.c
3 * ----------
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>
9 */
11 #define _XOPEN_SOURCE
12 #include <unistd.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <time.h>
17 #ifndef PATH_MAX
18 #define PATH_MAX 4096
19 #endif
21 #include <apr_general.h>
22 #include <apr_strings.h>
23 #include <apr_getopt.h>
24 #include <apr_lib.h>
26 #include <svn_types.h>
27 #include <svn_pools.h>
28 #include <svn_repos.h>
29 #include <svn_fs.h>
31 #undef SVN_ERR
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)
41 struct tm tm = {0};
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);
45 free(date);
46 return mktime(&tm);
49 int tar_header(apr_pool_t *pool, char *path, char *node, size_t f_size)
51 char buf[512];
52 unsigned int i, checksum;
53 svn_boolean_t is_dir;
55 memset(buf, 0, sizeof(buf));
57 if ((strlen(path) == 0) && (strlen(node) == 0)) {
58 return 0;
61 if (strlen(node) == 0) {
62 is_dir = 1;
63 } else {
64 is_dir = 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);
71 } else {
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);
90 checksum = 0;
91 for (i = 0; i < sizeof(buf); i++) {
92 checksum += buf[i];
94 strncpy(&buf[148], apr_psprintf(pool, "%07o", checksum & 0x1fffff), 7);
96 fwrite(buf, sizeof(char), sizeof(buf), stdout);
98 return 0;
101 int tar_footer()
103 char block[1024];
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];
111 apr_size_t len;
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);
122 do {
123 len = sizeof(buf);
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));
129 return 0;
132 int dump_tree(svn_fs_root_t *root, char *prefix, char *path, apr_pool_t *pool)
134 const void *key;
135 void *val;
136 char *node, *subpath, *full_path;
138 apr_pool_t *subpool;
139 apr_hash_t *dir_entries;
140 apr_hash_index_t *i;
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);
153 node = (char *)key;
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);
160 if (is_dir) {
161 dump_tree(root, prefix, subpath, subpool);
162 } else {
163 dump_blob(root, prefix, path, node, subpool);
167 svn_pool_destroy(subpool);
169 return 0;
172 int crawl_filesystem(char *repos_path, char *root_path, apr_pool_t *pool)
174 char *path;
176 apr_hash_t *props;
177 apr_hash_index_t *i;
179 svn_repos_t *repos;
180 svn_fs_t *fs;
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)
188 return -1;
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);
203 tar_footer();
205 fprintf(stderr, "done!\n");
207 return 0;
210 int main(int argc, char *argv[])
212 apr_pool_t *pool;
213 apr_getopt_t *options;
215 apr_getopt_option_t long_options[] = {
216 { "help", 'h', 0 },
217 { "prefix", 'p', 0 },
218 { "basename", 'b', 0 },
219 { "revision", 'r', 0 },
220 { NULL, 0, 0 }
223 if (argc < 2) {
224 fprintf(stderr, "usage: %s REPOS_PATH [prefix]\n", argv[0]);
225 return -1;
228 if (apr_initialize() != APR_SUCCESS) {
229 fprintf(stderr, "You lose at apr_initialize().\n");
230 return -1;
233 pool = svn_pool_create(NULL);
235 crawl_filesystem(argv[1], (argc == 3 ? argv[2] : TRUNK), pool);
237 apr_terminate();
239 return 0;