1 // DAVMirrorTree -- mirror a DAV tree to the local file-system
3 // Copyright (C) 2012 James Turner <zakalawe@mac.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "SVNRepository.hxx"
30 #include <boost/foreach.hpp>
32 #include "simgear/debug/logstream.hxx"
33 #include "simgear/misc/strutils.hxx"
34 #include <simgear/misc/sg_dir.hxx>
35 #include <simgear/io/HTTPClient.hxx>
36 #include <simgear/io/DAVMultiStatus.hxx>
37 #include <simgear/io/SVNDirectory.hxx>
38 #include <simgear/io/sg_file.hxx>
39 #include <simgear/io/SVNReportParser.hxx>
49 typedef std::vector<HTTP::Request_ptr> RequestVector;
54 SVNRepoPrivate(SVNRepository* parent) :
57 status(SVNRepository::SVN_NO_ERROR)
60 SVNRepository* p; // link back to outer
61 SVNDirectory* rootCollection;
65 std::string targetRevision;
67 SVNRepository::ResultCode status;
74 void updateFailed(HTTP::Request* req, SVNRepository::ResultCode err)
76 SG_LOG(SG_IO, SG_WARN, "SVN: failed to update from:" << req->url());
81 void propFindComplete(HTTP::Request* req, DAVCollection* col);
82 void propFindFailed(HTTP::Request* req, SVNRepository::ResultCode err);
86 namespace { // anonmouse
88 string makeAbsoluteUrl(const string& url, const string& base)
90 if (strutils::starts_with(url, "http://"))
91 return url; // already absolute
93 assert(strutils::starts_with(base, "http://"));
94 int schemeEnd = base.find("://");
95 int hostEnd = base.find('/', schemeEnd + 3);
100 return base.substr(0, hostEnd) + url;
103 // keep the responses small by only requesting the properties we actually
104 // care about; the ETag, length and MD5-sum
105 const char* PROPFIND_REQUEST_BODY =
106 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
107 "<D:propfind xmlns:D=\"DAV:\">"
108 "<D:prop xmlns:R=\"http://subversion.tigris.org/xmlns/dav/\">"
111 "<D:version-controlled-configuration/>"
115 class PropFindRequest : public HTTP::Request
118 PropFindRequest(SVNRepoPrivate* repo) :
119 Request(repo->baseUrl, "PROPFIND"),
124 virtual string_list requestHeaders() const
127 r.push_back("Depth");
131 virtual string header(const string& name) const
133 if (name == "Depth") {
140 virtual string requestBodyType() const
142 return "application/xml; charset=\"utf-8\"";
145 virtual int requestBodyLength() const
147 return strlen(PROPFIND_REQUEST_BODY);
150 virtual int getBodyData(char* buf, int count) const
152 int bodyLen = strlen(PROPFIND_REQUEST_BODY);
153 assert(count >= bodyLen);
154 memcpy(buf, PROPFIND_REQUEST_BODY, bodyLen);
159 virtual void responseHeadersComplete()
161 if (responseCode() == 207) {
163 } else if (responseCode() == 404) {
164 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_NOT_FOUND);
166 SG_LOG(SG_IO, SG_WARN, "request for:" << url() <<
167 " return code " << responseCode());
168 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_SOCKET);
172 virtual void responseComplete()
174 if (responseCode() == 207) {
175 _davStatus.finishParse();
176 _repo->propFindComplete(this, (DAVCollection*) _davStatus.resource());
180 virtual void gotBodyData(const char* s, int n)
182 if (responseCode() != 207) {
185 _davStatus.parseXML(s, n);
188 virtual void failed()
190 HTTP::Request::failed();
191 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_SOCKET);
195 SVNRepoPrivate* _repo;
196 DAVMultiStatus _davStatus;
199 class UpdateReportRequest : public HTTP::Request
202 UpdateReportRequest(SVNRepoPrivate* repo,
203 const std::string& aVersionName,
205 HTTP::Request("", "REPORT"),
211 setUrl(repo->vccUrl);
214 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
215 "<S:update-report send-all=\"true\" xmlns:S=\"svn:\">\n"
216 "<S:src-path>" + repo->baseUrl + "</S:src-path>\n"
217 "<S:depth>unknown</S:depth>\n";
219 _request += "<S:entry rev=\"" + aVersionName + "\" depth=\"infinity\" start-empty=\"true\"/>\n";
223 _repo->rootCollection->mergeUpdateReportDetails(0, entries);
224 BOOST_FOREACH(string e, entries) {
225 _request += e + "\n";
229 _request += "</S:update-report>";
232 virtual string requestBodyType() const
234 return "application/xml; charset=\"utf-8\"";
237 virtual int requestBodyLength() const
239 return _request.size();
242 virtual int getBodyData(char* buf, int count) const
244 int len = std::min(count, requestBodyLength() - _requestSent);
245 memcpy(buf, _request.c_str() + _requestSent, len);
251 virtual void responseHeadersComplete()
256 virtual void responseComplete()
262 if (responseCode() == 200) {
263 SVNRepository::ResultCode err = _parser.finishParse();
265 _repo->updateFailed(this, err);
268 _repo->svnUpdateDone();
270 } else if (responseCode() == 404) {
271 _repo->updateFailed(this, SVNRepository::SVN_ERROR_NOT_FOUND);
274 SG_LOG(SG_IO, SG_WARN, "SVN: request for:" << url() <<
275 " return code " << responseCode());
276 _repo->updateFailed(this, SVNRepository::SVN_ERROR_SOCKET);
281 virtual void gotBodyData(const char* s, int n)
287 if (responseCode() != 200) {
291 SVNRepository::ResultCode err = _parser.parseXML(s, n);
294 SG_LOG(SG_IO, SG_WARN, "SVN: request for:" << url() << " failed:" << err);
295 _repo->updateFailed(this, err);
299 virtual void failed()
301 HTTP::Request::failed();
302 _repo->updateFailed(this, SVNRepository::SVN_ERROR_SOCKET);
306 mutable int _requestSent;
307 SVNReportParser _parser;
308 SVNRepoPrivate* _repo;
314 SVNRepository::SVNRepository(const SGPath& base, HTTP::Client *cl) :
315 _d(new SVNRepoPrivate(this))
318 _d->rootCollection = new SVNDirectory(this, base);
319 _d->baseUrl = _d->rootCollection->url();
322 SVNRepository::~SVNRepository()
324 delete _d->rootCollection;
327 void SVNRepository::setBaseUrl(const std::string &url)
330 _d->rootCollection->setBaseUrl(url);
333 std::string SVNRepository::baseUrl() const
338 HTTP::Client* SVNRepository::http() const
343 SGPath SVNRepository::fsBase() const
345 return _d->rootCollection->fsPath();
348 bool SVNRepository::isBare() const
350 if (!fsBase().exists() || Dir(fsBase()).isEmpty()) {
354 if (_d->vccUrl.empty()) {
361 void SVNRepository::update()
363 _d->status = SVN_NO_ERROR;
364 if (_d->targetRevision.empty() || _d->vccUrl.empty()) {
365 _d->isUpdating = true;
366 PropFindRequest* pfr = new PropFindRequest(_d.get());
367 http()->makeRequest(pfr);
371 if (_d->targetRevision == rootDir()->cachedRevision()) {
372 SG_LOG(SG_IO, SG_DEBUG, baseUrl() << " in sync at version " << _d->targetRevision);
373 _d->isUpdating = false;
377 _d->isUpdating = true;
378 UpdateReportRequest* urr = new UpdateReportRequest(_d.get(),
379 _d->targetRevision, isBare());
380 http()->makeRequest(urr);
383 bool SVNRepository::isDoingSync() const
385 if (_d->status != SVN_NO_ERROR) {
389 return _d->isUpdating || _d->rootCollection->isDoingSync();
392 SVNDirectory* SVNRepository::rootDir() const
394 return _d->rootCollection;
397 SVNRepository::ResultCode
398 SVNRepository::failure() const
403 ///////////////////////////////////////////////////////////////////////////
405 void SVNRepoPrivate::propFindComplete(HTTP::Request* req, DAVCollection* c)
407 targetRevision = c->versionName();
408 vccUrl = makeAbsoluteUrl(c->versionControlledConfiguration(), baseUrl);
409 rootCollection->collection()->setVersionControlledConfiguration(vccUrl);
413 void SVNRepoPrivate::propFindFailed(HTTP::Request *req, SVNRepository::ResultCode err)
415 if (err != SVNRepository::SVN_ERROR_NOT_FOUND) {
416 SG_LOG(SG_IO, SG_WARN, "PropFind failed for:" << req->url());
423 } // of namespace simgear