summaryrefslogtreecommitdiffstats
path: root/src/svnqt/repositorydata.cpp
blob: 320139ffb1dc233dd0b4a49b288de4c7d92daef4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/***************************************************************************
 *   Copyright (C) 2006-2007 by Rajko Albrecht                             *
 *   [email protected]                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/
#include "svnqt/repositorydata.h"
#include "svnqt/svnqt_defines.h"
#include "svnqt/exception.h"
#include "svnqt/repositorylistener.h"
#include "svnqt/svnfilestream.h"

#include <svn_fs.h>
#include <svn_path.h>
#include <svn_config.h>

namespace svn {

namespace repository {

class RepoOutStream:public stream::SvnStream
{
public:
    RepoOutStream(RepositoryData*);
    virtual ~RepoOutStream(){}

    virtual bool isOk()const{return true;}
    virtual long write(const char*data,const unsigned long max);

protected:
    RepositoryData*m_Back;
};

RepoOutStream::RepoOutStream(RepositoryData*aBack)
    : SvnStream(false,true)
{
    m_Back = aBack;
}

long RepoOutStream::write(const char*data,const unsigned long max)
{
    if (m_Back) {
        TQString msg = TQString::FROMUTF8(data,max);
        m_Back->reposFsWarning(msg);
    }
    return max;
}

RepositoryData::RepositoryData(RepositoryListener*aListener)
{
    m_Repository = 0;
    m_Listener = aListener;
}


RepositoryData::~RepositoryData()
{
}

void RepositoryData::warning_func(void *baton, svn_error_t *err)
{
    RepositoryData*_r = (RepositoryData*)baton;

    if (_r) {
        TQString msg = svn::Exception::error2msg(err);
        svn_error_clear(err);
        _r->reposFsWarning(msg);
    }
}

void RepositoryData::reposFsWarning(const TQString&msg)
{
    if (m_Listener) {
        m_Listener->sendWarning(msg);
    }
}

svn_error_t*RepositoryData::cancel_func(void*baton)
{
    RepositoryListener*m_L = (RepositoryListener*)baton;
    if (m_L && m_L->isCanceld()) {
        return svn_error_create (SVN_ERR_CANCELLED, 0, TQString::FROMUTF8("Cancelled by user.").TOUTF8());
    }
    return SVN_NO_ERROR;
}

/*!
    \fn svn::RepositoryData::close()
 */
void RepositoryData::Close()
{
    m_Pool.renew();
    m_Repository = 0;
}


/*!
    \fn svn::RepositoryData::Open(const TQString&)
 */
svn_error_t * RepositoryData::Open(const TQString&path)
{
    Close();
    svn_error_t * error = svn_repos_open(&m_Repository,path.TOUTF8(),m_Pool);
    if (error!=0L) {
        m_Repository=0;
        return error;
    }
    svn_fs_set_warning_func(svn_repos_fs(m_Repository), RepositoryData::warning_func, this);
    return SVN_NO_ERROR;
}


/*!
    \fn svn::RepositoryData::CreateOpen(const TQString&path, const TQString&fstype, bool _bdbnosync = false, bool _bdbautologremove = true, bool nosvn1diff=false)
 */
svn_error_t * RepositoryData::CreateOpen(const TQString&path, const TQString&fstype, bool _bdbnosync,
                                          bool _bdbautologremove,
                                          bool _pre_1_4_compat,
                                          bool _pre_1_5_compat)
{
    Close();
    const char* _type;
    if (fstype.lower()=="bdb") {
        _type="bdb";
    } else {
        _type="fsfs";
    }
    apr_hash_t *config;
    apr_hash_t *fs_config = apr_hash_make(m_Pool);

    apr_hash_set(fs_config, SVN_FS_CONFIG_BDB_TXN_NOSYNC,
                APR_HASH_KEY_STRING,
                (_bdbnosync ? "1" : "0"));
    apr_hash_set(fs_config, SVN_FS_CONFIG_BDB_LOG_AUTOREMOVE,
                APR_HASH_KEY_STRING,
                (_bdbautologremove ? "1" : "0"));
    apr_hash_set(fs_config, SVN_FS_CONFIG_FS_TYPE,
                 APR_HASH_KEY_STRING,
                 _type);

#if ((SVN_VER_MAJOR == 1) && (SVN_VER_MINOR >= 4) || SVN_VER_MAJOR>1)
    if (_pre_1_4_compat) {
        tqDebug("Pre 14");
        apr_hash_set(fs_config, SVN_FS_CONFIG_PRE_1_4_COMPATIBLE,
            APR_HASH_KEY_STRING,"1");
    }
#else
    Q_UNUSED(_pre_1_4_compat);
#endif
#if ((SVN_VER_MAJOR == 1) && (SVN_VER_MINOR >= 5) || SVN_VER_MAJOR>1)
    if (_pre_1_5_compat) {
        tqDebug("Pre 15");
        apr_hash_set(fs_config, SVN_FS_CONFIG_PRE_1_5_COMPATIBLE,
                     APR_HASH_KEY_STRING,"1");
    }
#else
    Q_UNUSED(_pre_1_5_compat);
#endif
    /// @todo config as extra paramter? Meanwhile default config only
    /// (see svn::ContextData)
    SVN_ERR(svn_config_get_config(&config, 0, m_Pool));
    const char*repository_path = apr_pstrdup (m_Pool,path.TOUTF8());

    repository_path = svn_path_internal_style(repository_path, m_Pool);

    if (svn_path_is_url(repository_path)) {
        return svn_error_createf(SVN_ERR_CL_ARG_PARSING_ERROR, NULL,
            "'%s' is an URL when it should be a path",repository_path);
    }
    SVN_ERR(svn_repos_create(&m_Repository, repository_path,
            NULL, NULL,config, fs_config,m_Pool));

    svn_fs_set_warning_func(svn_repos_fs(m_Repository), RepositoryData::warning_func, this);

    return SVN_NO_ERROR;
}


/*!
    \fn svn::RepositoryData::dump(const TQString&output,const svn::Revision&start,const svn::Revision&end, bool incremental, bool use_deltas)
 */
svn_error_t* RepositoryData::dump(const TQString&output,const svn::Revision&start,const svn::Revision&end, bool incremental, bool use_deltas)
{
    if (!m_Repository) {
        return svn_error_create(SVN_ERR_CANCELLED,0,"No repository selected.");
    }
    Pool pool;
    svn::stream::SvnFileOStream out(output);
    RepoOutStream backstream(this);
    svn_revnum_t _s,_e;
    _s = start.revnum();
    _e = end.revnum();
    SVN_ERR(svn_repos_dump_fs2(m_Repository,out,backstream,_s,_e,incremental,use_deltas,
        RepositoryData::cancel_func,m_Listener,pool));

    return SVN_NO_ERROR;
}

svn_error_t* RepositoryData::loaddump(const TQString&dump,svn_repos_load_uuid uuida, const TQString&parentFolder, bool usePre, bool usePost)
{
    if (!m_Repository) {
        return svn_error_create(SVN_ERR_CANCELLED,0,"No repository selected.");
    }
    svn::stream::SvnFileIStream infile(dump);
    RepoOutStream backstream(this);
    Pool pool;
    const char*src_path = apr_pstrdup (pool,dump.TOUTF8());
    const char*dest_path;
    if (parentFolder.isEmpty()) {
        dest_path=0;
    } else {
        dest_path=apr_pstrdup (pool,parentFolder.TOUTF8());
    }

    src_path = svn_path_internal_style(src_path, pool);
    SVN_ERR(svn_repos_load_fs2(m_Repository,infile,backstream,uuida,dest_path,usePre?1:0,usePost?1:0,RepositoryData::cancel_func,m_Listener,pool));
    return SVN_NO_ERROR;
}

svn_error_t* RepositoryData::hotcopy(const TQString&src,const TQString&dest,bool cleanlogs)
{
    Pool pool;
    const char*src_path = apr_pstrdup (pool,src.TOUTF8());
    const char*dest_path = apr_pstrdup (pool,dest.TOUTF8());
    src_path = svn_path_internal_style(src_path, pool);
    dest_path = svn_path_internal_style(dest_path, pool);
    SVN_ERR(svn_repos_hotcopy(src_path,dest_path,cleanlogs?1:0,pool));
    return SVN_NO_ERROR;
}

}

}