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>
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
)
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
);
48 int tar_header(apr_pool_t
*pool
, char *path
, char *node
, size_t f_size
)
51 unsigned int i
, checksum
;
54 memset(buf
, 0, sizeof(buf
));
56 if ((strlen(path
) == 0) && (strlen(node
) == 0)) {
60 if (strlen(node
) == 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);
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);
90 for (i
= 0; i
< sizeof(buf
); i
++) {
93 strncpy(&buf
[148], apr_psprintf(pool
, "%07o", checksum
& 0x1fffff), 7);
95 fwrite(buf
, sizeof(char), sizeof(buf
), stdout
);
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];
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
);
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
));
131 int dump_tree(svn_fs_root_t
*root
, char *prefix
, char *path
, apr_pool_t
*pool
)
135 char *node
, *subpath
, *full_path
;
138 apr_hash_t
*dir_entries
;
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
);
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
);
160 dump_tree(root
, prefix
, subpath
, subpool
);
162 dump_blob(root
, prefix
, path
, node
, subpool
);
166 svn_pool_destroy(subpool
);
171 int crawl_filesystem(char *repos_path
, char *root_path
, apr_pool_t
*pool
)
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
);
201 fprintf(stderr
, "done!\n");
206 int main(int argc
, char *argv
[])
209 apr_getopt_t
*options
;
211 apr_getopt_option_t long_options
[] = {
213 { "prefix", 'p', 0 },
214 { "basename", 'b', 0 },
215 { "revision", 'r', 0 },
220 fprintf(stderr
, "usage: %s REPOS_PATH [prefix]\n", argv
[0]);
224 if (apr_initialize() != APR_SUCCESS
) {
225 fprintf(stderr
, "You lose at apr_initialize().\n");
229 pool
= svn_pool_create(NULL
);
231 crawl_filesystem(argv
[1], (argc
== 3 ? argv
[2] : TRUNK
), pool
);