To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / extra / fast-export / svn-archive.c @ 1560:2c67e414ab46

History | View | Annotate | Download (6.19 KB)

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.
6
 *
7
 * Author: Chris Lee <clee@kde.org>
8
 * License: MIT <http://www.opensource.org/licenses/mit-license.php>
9
 */
10

    
11
#define _XOPEN_SOURCE
12
#include <unistd.h>
13
#include <string.h>
14
#include <stdio.h>
15
#include <time.h>
16

    
17
#ifndef PATH_MAX
18
#define PATH_MAX 4096
19
#endif
20

    
21
#include <apr_general.h>
22
#include <apr_strings.h>
23
#include <apr_getopt.h>
24
#include <apr_lib.h>
25

    
26
#include <svn_types.h>
27
#include <svn_pools.h>
28
#include <svn_repos.h>
29
#include <svn_fs.h>
30

    
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
34

    
35
#define TRUNK "/trunk"
36

    
37
static time_t archive_time;
38

    
39
time_t get_epoch(char *svn_date)
40
{
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);
47
}
48

    
49
int tar_header(apr_pool_t *pool, char *path, char *node, size_t f_size)
50
{
51
    char          buf[512];
52
    unsigned int  i, checksum;
53
    svn_boolean_t is_dir;
54

    
55
    memset(buf, 0, sizeof(buf));
56

    
57
    if ((strlen(path) == 0) && (strlen(node) == 0)) {
58
        return 0;
59
    }
60

    
61
    if (strlen(node) == 0) {
62
        is_dir = 1;
63
    } else {
64
        is_dir = 0;
65
    }
66

    
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);
75
    }
76

    
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);
88

    
89
    strncpy(&buf[148], "        ", 8);
90
    checksum = 0;
91
    for (i = 0; i < sizeof(buf); i++) {
92
        checksum += buf[i];
93
    }
94
    strncpy(&buf[148], apr_psprintf(pool, "%07o", checksum & 0x1fffff), 7);
95

    
96
    fwrite(buf, sizeof(char), sizeof(buf), stdout);
97

    
98
    return 0;
99
}
100

    
101
int tar_footer()
102
{
103
    char block[1024];
104
    memset(block, 0, sizeof(block));
105
    fwrite(block, sizeof(char), sizeof(block), stdout);
106
}
107

    
108
int dump_blob(svn_fs_root_t *root, char *prefix, char *path, char *node, apr_pool_t *pool)
109
{
110
    char           *full_path, buf[512];
111
    apr_size_t     len;
112
    svn_stream_t   *stream;
113
    svn_filesize_t stream_length;
114

    
115
    full_path = apr_psprintf(pool, "%s%s/%s", prefix, path, node);
116

    
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));
119

    
120
    tar_header(pool, path, node, stream_length);
121

    
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));
128

    
129
    return 0;
130
}
131

    
132
int dump_tree(svn_fs_root_t *root, char *prefix, char *path, apr_pool_t *pool)
133
{
134
    const void       *key;
135
    void             *val;
136
    char             *node, *subpath, *full_path;
137

    
138
    apr_pool_t       *subpool;
139
    apr_hash_t       *dir_entries;
140
    apr_hash_index_t *i;
141

    
142
    svn_boolean_t    is_dir;
143

    
144
    tar_header(pool, path, "", 0);
145

    
146
    SVN_ERR(svn_fs_dir_entries(&dir_entries, root, apr_psprintf(pool, "%s/%s", prefix, path), pool));
147

    
148
    subpool = svn_pool_create(pool);
149

    
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;
154

    
155
        subpath = apr_psprintf(subpool, "%s/%s", path, node);
156
        full_path = apr_psprintf(subpool, "%s%s", prefix, subpath);
157

    
158
        svn_fs_is_dir(&is_dir, root, full_path, subpool);
159

    
160
        if (is_dir) {
161
            dump_tree(root, prefix, subpath, subpool);
162
        } else {
163
            dump_blob(root, prefix, path, node, subpool);
164
        }
165
    }
166

    
167
    svn_pool_destroy(subpool);
168

    
169
    return 0;
170
}
171

    
172
int crawl_filesystem(char *repos_path, char *root_path, apr_pool_t *pool)
173
{
174
    char                 *path;
175

    
176
    apr_hash_t           *props;
177
    apr_hash_index_t     *i;
178

    
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;
184

    
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));
190

    
191
    export_rev = youngest_rev;
192

    
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));
195

    
196
    svndate = apr_hash_get(props, "svn:date", APR_HASH_KEY_STRING);
197
    archive_time = get_epoch((char *)svndate->data);
198

    
199
    fprintf(stderr, "Exporting archive of r%ld... \n", export_rev);
200

    
201
    dump_tree(fs_root, root_path, "", pool);
202

    
203
    tar_footer();
204

    
205
    fprintf(stderr, "done!\n");
206

    
207
    return 0;
208
}
209

    
210
int main(int argc, char *argv[])
211
{
212
    apr_pool_t           *pool;
213
    apr_getopt_t         *options;
214

    
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 }
221
    };
222

    
223
    if (argc < 2) {
224
        fprintf(stderr, "usage: %s REPOS_PATH [prefix]\n", argv[0]);
225
        return -1;
226
    }
227

    
228
    if (apr_initialize() != APR_SUCCESS) {
229
        fprintf(stderr, "You lose at apr_initialize().\n");
230
        return -1;
231
    }
232

    
233
    pool = svn_pool_create(NULL);
234

    
235
    crawl_filesystem(argv[1], (argc == 3 ? argv[2] : TRUNK), pool);
236

    
237
    apr_terminate();
238

    
239
    return 0;
240
}