hg-fast-export.sh Use hg-fast-export.py, not dead hg2git.py
[fast-export/barak.git] / svn-archive.c
blobb983f908fd23aeeca96f85e2953436c3831944bd
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_fs.h>
30 #undef SVN_ERR
31 #define SVN_ERR(expr) SVN_INT_ERR(expr)
32 #define apr_sane_push(arr, contents) *(char **)apr_array_push(arr) = contents
34 #define TRUNK "/trunk"
36 static time_t archive_time;
38 time_t get_epoch(char *svn_date)
40 struct tm tm = {0};
41 char *date = malloc(strlen(svn_date) * sizeof(char *));
42 strncpy(date, svn_date, strlen(svn_date) - 8);
43 strptime(date, "%Y-%m-%dT%H:%M:%S", &tm);
44 free(date);
45 return mktime(&tm);
48 int tar_header(apr_pool_t *pool, char *path, char *node, size_t f_size)
50 char buf[512];
51 unsigned int i, checksum;
52 svn_boolean_t is_dir;
54 memset(buf, 0, sizeof(buf));
56 if ((strlen(path) == 0) && (strlen(node) == 0)) {
57 return 0;
60 if (strlen(node) == 0) {
61 is_dir = 1;
62 } else {
63 is_dir = 0;
66 if (strlen(path) == 0) {
67 strncpy(buf, apr_psprintf(pool, "%s", node), 99);
68 } else if (strlen(path) + strlen(node) < 100) {
69 strncpy(buf, apr_psprintf(pool, "%s/%s", path+1, node), 99);
70 } else {
71 fprintf(stderr, "really long file path...\n");
72 strncpy(&buf[0], node, 99);
73 strncpy(&buf[345], path+1, 154);
76 strncpy(&buf[100], apr_psprintf(pool, "%07o", (is_dir ? 0755 : 0644)), 7);
77 strncpy(&buf[108], apr_psprintf(pool, "%07o", 1000), 7);
78 strncpy(&buf[116], apr_psprintf(pool, "%07o", 1000), 7);
79 strncpy(&buf[124], apr_psprintf(pool, "%011lo", f_size), 11);
80 strncpy(&buf[136], apr_psprintf(pool, "%011lo", archive_time), 11);
81 strncpy(&buf[156], (is_dir ? "5" : "0"), 1);
82 strncpy(&buf[257], "ustar ", 8);
83 strncpy(&buf[265], "clee", 31);
84 strncpy(&buf[297], "clee", 31);
85 // strncpy(&buf[329], apr_psprintf(pool, "%07o", 0), 7);
86 // strncpy(&buf[337], apr_psprintf(pool, "%07o", 0), 7);
88 strncpy(&buf[148], " ", 8);
89 checksum = 0;
90 for (i = 0; i < sizeof(buf); i++) {
91 checksum += buf[i];
93 strncpy(&buf[148], apr_psprintf(pool, "%07o", checksum & 0x1fffff), 7);
95 fwrite(buf, sizeof(char), sizeof(buf), stdout);
97 return 0;
100 int tar_footer()
102 char block[1024];
103 memset(block, 0, sizeof(block));
104 fwrite(block, sizeof(char), sizeof(block), stdout);
107 int dump_blob(svn_fs_root_t *root, char *prefix, char *path, char *node, apr_pool_t *pool)
109 char *full_path, buf[512];
110 apr_size_t len;
111 svn_stream_t *stream;
112 svn_filesize_t stream_length;
114 full_path = apr_psprintf(pool, "%s%s/%s", prefix, path, node);
116 SVN_ERR(svn_fs_file_length(&stream_length, root, full_path, pool));
117 SVN_ERR(svn_fs_file_contents(&stream, root, full_path, pool));
119 tar_header(pool, path, node, stream_length);
121 do {
122 len = sizeof(buf);
123 memset(buf, '\0', sizeof(buf));
124 SVN_ERR(svn_stream_read(stream, buf, &len));
125 fwrite(buf, sizeof(char), sizeof(buf), stdout);
126 } while (len == sizeof(buf));
128 return 0;
131 int dump_tree(svn_fs_root_t *root, char *prefix, char *path, apr_pool_t *pool)
133 const void *key;
134 void *val;
135 char *node, *subpath, *full_path;
137 apr_pool_t *subpool;
138 apr_hash_t *dir_entries;
139 apr_hash_index_t *i;
141 svn_boolean_t is_dir;
143 tar_header(pool, path, "", 0);
145 SVN_ERR(svn_fs_dir_entries(&dir_entries, root, apr_psprintf(pool, "%s/%s", prefix, path), pool));
147 subpool = svn_pool_create(pool);
149 for (i = apr_hash_first(pool, dir_entries); i; i = apr_hash_next(i)) {
150 svn_pool_clear(subpool);
151 apr_hash_this(i, &key, NULL, &val);
152 node = (char *)key;
154 subpath = apr_psprintf(subpool, "%s/%s", path, node);
155 full_path = apr_psprintf(subpool, "%s%s", prefix, subpath);
157 svn_fs_is_dir(&is_dir, root, full_path, subpool);
159 if (is_dir) {
160 dump_tree(root, prefix, subpath, subpool);
161 } else {
162 dump_blob(root, prefix, path, node, subpool);
166 svn_pool_destroy(subpool);
168 return 0;
171 int crawl_filesystem(char *repos_path, char *root_path, apr_pool_t *pool)
173 char *path;
175 apr_hash_t *props;
176 apr_hash_index_t *i;
178 svn_fs_t *fs;
179 svn_string_t *svndate;
180 svn_revnum_t youngest_rev, export_rev;
181 svn_fs_root_t *fs_root;
183 SVN_ERR(svn_fs_initialize(pool));
184 SVN_ERR(svn_fs_open(&fs, repos_path, NULL, pool));
185 SVN_ERR(svn_fs_youngest_rev(&youngest_rev, fs, pool));
187 export_rev = youngest_rev;
189 SVN_ERR(svn_fs_revision_root(&fs_root, fs, export_rev, pool));
190 SVN_ERR(svn_fs_revision_proplist(&props, fs, export_rev, pool));
192 svndate = apr_hash_get(props, "svn:date", APR_HASH_KEY_STRING);
193 archive_time = get_epoch((char *)svndate->data);
195 fprintf(stderr, "Exporting archive of r%ld... \n", export_rev);
197 dump_tree(fs_root, root_path, "", pool);
199 tar_footer();
201 fprintf(stderr, "done!\n");
203 return 0;
206 int main(int argc, char *argv[])
208 apr_pool_t *pool;
209 apr_getopt_t *options;
211 apr_getopt_option_t long_options[] = {
212 { "help", 'h', 0 },
213 { "prefix", 'p', 0 },
214 { "basename", 'b', 0 },
215 { "revision", 'r', 0 },
216 { NULL, 0, 0 }
219 if (argc < 2) {
220 fprintf(stderr, "usage: %s REPOS_PATH [prefix]\n", argv[0]);
221 return -1;
224 if (apr_initialize() != APR_SUCCESS) {
225 fprintf(stderr, "You lose at apr_initialize().\n");
226 return -1;
229 pool = svn_pool_create(NULL);
231 crawl_filesystem(argv[1], (argc == 3 ? argv[2] : TRUNK), pool);
233 apr_terminate();
235 return 0;