Persist objects to files with mmap.
[svn-fe.git] / svndump.c
blobef7beaa62407641ba7473e6c87488a4795a870a8
1 /*
2 * Parse and rearrange a svnadmin dump.
3 * Create the dump with:
4 * svnadmin dump --incremental -r<startrev>:<endrev> <repository> >outfile
5 */
7 #define _GNU_SOURCE
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <time.h>
13 #include "mkgmtime.h"
15 #include "repo_tree.h"
16 #include "fast_export.h"
17 #include "line_buffer.h"
18 #include "obj_pool.h"
19 #include "string_pool.h"
21 #define NODEACT_REPLACE 4
22 #define NODEACT_DELETE 3
23 #define NODEACT_ADD 2
24 #define NODEACT_CHANGE 1
25 #define NODEACT_UNKNOWN 0
27 #define DUMP_CTX 0
28 #define REV_CTX 1
29 #define NODE_CTX 2
31 #define LENGTH_UNKNOWN (~0)
33 #define BLOB_MARK_OFFSET 1000000000
35 /* Create memory pool for log messages */
36 obj_pool_gen(log, char, 4096);
38 static char* log_copy(uint32_t length, char *log)
40 char *buffer;
41 log_free(log_pool.size);
42 buffer = log_pointer(log_alloc(length));
43 strncpy(buffer, log, length);
44 return buffer;
47 static struct {
48 uint32_t action, propLength, textLength, srcRev, srcMode, mark, type;
49 uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
50 } node_ctx;
52 static struct {
53 uint32_t revision, author;
54 time_t timestamp;
55 char *log;
56 } rev_ctx;
58 static struct {
59 uint32_t uuid, url;
60 } dump_ctx;
62 static struct {
63 uint32_t svn_log, svn_author, svn_date, svn_executable, svn_special, uuid,
64 revision_number, node_path, node_kind, node_action,
65 node_copyfrom_path, node_copyfrom_rev, text_content_length,
66 prop_content_length, content_length;
67 } keys;
69 static void reset_node_ctx(char *fname)
71 node_ctx.type = 0;
72 node_ctx.action = NODEACT_UNKNOWN;
73 node_ctx.propLength = LENGTH_UNKNOWN;
74 node_ctx.textLength = LENGTH_UNKNOWN;
75 node_ctx.src[0] = ~0;
76 node_ctx.srcRev = 0;
77 node_ctx.srcMode = 0;
78 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
79 node_ctx.mark = 0;
82 static void reset_rev_ctx(uint32_t revision)
84 rev_ctx.revision = revision;
85 rev_ctx.timestamp = 0;
86 rev_ctx.log = NULL;
87 rev_ctx.author = ~0;
90 static void reset_dump_ctx(uint32_t url)
92 dump_ctx.url = url;
93 dump_ctx.uuid = ~0;
96 static void init_keys(void)
98 keys.svn_log = pool_intern("svn:log");
99 keys.svn_author = pool_intern("svn:author");
100 keys.svn_date = pool_intern("svn:date");
101 keys.svn_executable = pool_intern("svn:executable");
102 keys.svn_special = pool_intern("svn:special");
103 keys.uuid = pool_intern("UUID");
104 keys.revision_number = pool_intern("Revision-number");
105 keys.node_path = pool_intern("Node-path");
106 keys.node_kind = pool_intern("Node-kind");
107 keys.node_action = pool_intern("Node-action");
108 keys.node_copyfrom_path = pool_intern("Node-copyfrom-path");
109 keys.node_copyfrom_rev = pool_intern("Node-copyfrom-rev");
110 keys.text_content_length = pool_intern("Text-content-length");
111 keys.prop_content_length = pool_intern("Prop-content-length");
112 keys.content_length = pool_intern("Content-length");
115 static uint32_t next_blob_mark(void)
117 static uint32_t mark = BLOB_MARK_OFFSET;
118 return mark++;
121 static void read_props(void)
123 struct tm tm;
124 uint32_t len;
125 uint32_t key = ~0;
126 char *val = NULL;
127 char *t;
128 while ((t = buffer_read_line()) && strcmp(t, "PROPS-END")) {
129 if (!strncmp(t, "K ", 2)) {
130 len = atoi(&t[2]);
131 key = pool_intern(buffer_read_string(len));
132 buffer_read_line();
133 } else if (!strncmp(t, "V ", 2)) {
134 len = atoi(&t[2]);
135 val = buffer_read_string(len);
136 if (key == keys.svn_log) {
137 /* Value length excludes terminating nul. */
138 rev_ctx.log = log_copy(len + 1, val);
139 } else if (key == keys.svn_author) {
140 rev_ctx.author = pool_intern(val);
141 } else if (key == keys.svn_date) {
142 strptime(val, "%FT%T", &tm);
143 rev_ctx.timestamp = mkgmtime(&tm);
144 } else if (key == keys.svn_executable) {
145 node_ctx.type = REPO_MODE_EXE;
146 } else if (key == keys.svn_special) {
147 node_ctx.type = REPO_MODE_LNK;
149 key = ~0;
150 buffer_read_line();
155 static void handle_node(void)
157 if (node_ctx.propLength != LENGTH_UNKNOWN && node_ctx.propLength) {
158 read_props();
161 if (node_ctx.srcRev) {
162 node_ctx.srcMode = repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
165 if (node_ctx.textLength != LENGTH_UNKNOWN &&
166 node_ctx.type != REPO_MODE_DIR) {
167 node_ctx.mark = next_blob_mark();
170 if (node_ctx.action == NODEACT_DELETE) {
171 repo_delete(node_ctx.dst);
172 } else if (node_ctx.action == NODEACT_CHANGE ||
173 node_ctx.action == NODEACT_REPLACE) {
174 if (node_ctx.action == NODEACT_REPLACE &&
175 node_ctx.type == REPO_MODE_DIR) {
176 repo_replace(node_ctx.dst, node_ctx.mark);
177 } else if (node_ctx.propLength != LENGTH_UNKNOWN ) {
178 repo_modify(node_ctx.dst, node_ctx.type, node_ctx.mark);
179 } else if (node_ctx.textLength != LENGTH_UNKNOWN) {
180 node_ctx.srcMode = repo_replace(node_ctx.dst, node_ctx.mark);
182 } else if (node_ctx.action == NODEACT_ADD) {
183 if (node_ctx.srcRev &&
184 node_ctx.propLength == LENGTH_UNKNOWN &&
185 node_ctx.textLength != LENGTH_UNKNOWN) {
186 node_ctx.srcMode = repo_replace(node_ctx.dst, node_ctx.mark);
187 } else if ((node_ctx.type == REPO_MODE_DIR && !node_ctx.srcRev) ||
188 node_ctx.textLength != LENGTH_UNKNOWN){
189 repo_add(node_ctx.dst, node_ctx.type, node_ctx.mark);
193 if (node_ctx.propLength == LENGTH_UNKNOWN && node_ctx.srcMode) {
194 node_ctx.type = node_ctx.srcMode;
197 if (node_ctx.mark) {
198 fast_export_blob(node_ctx.type, node_ctx.mark, node_ctx.textLength);
199 } else if (node_ctx.textLength != LENGTH_UNKNOWN) {
200 buffer_skip_bytes(node_ctx.textLength);
204 static void handle_revision(void)
206 repo_commit(rev_ctx.revision, rev_ctx.author, rev_ctx.log, dump_ctx.uuid,
207 dump_ctx.url, rev_ctx.timestamp);
210 static void svndump_read(uint32_t url)
212 char *val;
213 char *t;
214 uint32_t active_ctx = DUMP_CTX;
215 uint32_t len;
216 uint32_t key;
218 reset_dump_ctx(url);
219 while ((t = buffer_read_line())) {
220 val = strstr(t, ": ");
221 if (!val) continue;
222 *val++ = '\0';
223 *val++ = '\0';
224 key = pool_intern(t);
226 if(key == keys.uuid) {
227 dump_ctx.uuid = pool_intern(val);
228 } else if (key == keys.revision_number) {
229 if (active_ctx == NODE_CTX) handle_node();
230 if (active_ctx != DUMP_CTX) handle_revision();
231 active_ctx = REV_CTX;
232 reset_rev_ctx(atoi(val));
233 } else if (key == keys.node_path) {
234 if (active_ctx == NODE_CTX)
235 handle_node();
236 active_ctx = NODE_CTX;
237 reset_node_ctx(val);
238 } else if (key == keys.node_kind) {
239 if (!strcmp(val, "dir")) {
240 node_ctx.type = REPO_MODE_DIR;
241 } else if (!strcmp(val, "file")) {
242 node_ctx.type = REPO_MODE_BLB;
243 } else {
244 fprintf(stderr, "Unknown node-kind: %s\n", val);
246 } else if (key == keys.node_action) {
247 if (!strcmp(val, "delete")) {
248 node_ctx.action = NODEACT_DELETE;
249 } else if (!strcmp(val, "add")) {
250 node_ctx.action = NODEACT_ADD;
251 } else if (!strcmp(val, "change")) {
252 node_ctx.action = NODEACT_CHANGE;
253 } else if (!strcmp(val, "replace")) {
254 node_ctx.action = NODEACT_REPLACE;
255 } else {
256 fprintf(stderr, "Unknown node-action: %s\n", val);
257 node_ctx.action = NODEACT_UNKNOWN;
259 } else if (key == keys.node_copyfrom_path) {
260 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
261 } else if (key == keys.node_copyfrom_rev) {
262 node_ctx.srcRev = atoi(val);
263 } else if (key == keys.text_content_length) {
264 node_ctx.textLength = atoi(val);
265 } else if (key == keys.prop_content_length) {
266 node_ctx.propLength = atoi(val);
267 } else if (key == keys.content_length) {
268 len = atoi(val);
269 buffer_read_line();
270 if (active_ctx == REV_CTX) {
271 read_props();
272 } else if (active_ctx == NODE_CTX) {
273 handle_node();
274 active_ctx = REV_CTX;
275 } else {
276 fprintf(stderr, "Unexpected content length header: %d\n", len);
277 buffer_skip_bytes(len);
281 if (active_ctx == NODE_CTX) handle_node();
282 if (active_ctx != DUMP_CTX) handle_revision();
285 static void svndump_reset(void)
287 log_reset();
288 buffer_reset();
289 repo_reset();
290 reset_dump_ctx(~0);
291 reset_rev_ctx(0);
292 reset_node_ctx(NULL);
295 int main(int argc, char **argv)
297 init_keys();
298 svndump_read((argc > 1) ? pool_intern(argv[1]) : ~0);
299 svndump_reset();
300 return 0;