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()
77 << "\n(repository:" << p->baseUrl() << ")");
82 void propFindComplete(HTTP::Request* req, DAVCollection* col);
83 void propFindFailed(HTTP::Request* req, SVNRepository::ResultCode err);
87 namespace { // anonmouse
89 string makeAbsoluteUrl(const string& url, const string& base)
91 if (strutils::starts_with(url, "http://"))
92 return url; // already absolute
94 assert(strutils::starts_with(base, "http://"));
95 int schemeEnd = base.find("://");
96 int hostEnd = base.find('/', schemeEnd + 3);
101 return base.substr(0, hostEnd) + url;
104 // keep the responses small by only requesting the properties we actually
105 // care about; the ETag, length and MD5-sum
106 const char* PROPFIND_REQUEST_BODY =
107 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
108 "<D:propfind xmlns:D=\"DAV:\">"
109 "<D:prop xmlns:R=\"http://subversion.tigris.org/xmlns/dav/\">"
112 "<D:version-controlled-configuration/>"
116 class PropFindRequest : public HTTP::Request
119 PropFindRequest(SVNRepoPrivate* repo) :
120 Request(repo->baseUrl, "PROPFIND"),
124 requestHeader("Depth") = "0";
125 setBodyData( PROPFIND_REQUEST_BODY,
126 "application/xml; charset=\"utf-8\"" );
130 virtual void responseHeadersComplete()
132 if (responseCode() == 207) {
134 } else if (responseCode() == 404) {
135 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_NOT_FOUND);
137 SG_LOG(SG_IO, SG_WARN, "request for:" << url() <<
138 " return code " << responseCode());
139 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_SOCKET);
143 Request::responseHeadersComplete();
146 virtual void onDone()
148 if (responseCode() == 207) {
149 _davStatus.finishParse();
150 if (_davStatus.isValid()) {
151 _repo->propFindComplete(this, (DAVCollection*) _davStatus.resource());
153 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_SOCKET);
158 virtual void gotBodyData(const char* s, int n)
160 if (responseCode() != 207) {
163 _davStatus.parseXML(s, n);
166 virtual void onFail()
168 HTTP::Request::onFail();
170 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_SOCKET);
176 SVNRepoPrivate* _repo;
177 DAVMultiStatus _davStatus;
180 class UpdateReportRequest:
184 UpdateReportRequest(SVNRepoPrivate* repo,
185 const std::string& aVersionName,
187 HTTP::Request("", "REPORT"),
192 setUrl(repo->vccUrl);
193 std::string request =
194 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
195 "<S:update-report send-all=\"true\" xmlns:S=\"svn:\">\n"
196 "<S:src-path>" + repo->baseUrl + "</S:src-path>\n"
197 "<S:depth>unknown</S:depth>\n"
198 "<S:entry rev=\"" + aVersionName + "\" depth=\"infinity\" start-empty=\"true\"/>\n";
203 _repo->rootCollection->mergeUpdateReportDetails(0, entries);
204 BOOST_FOREACH(string e, entries)
210 request += "</S:update-report>";
212 setBodyData(request, "application/xml; charset=\"utf-8\"");
216 virtual void onDone()
222 if (responseCode() == 200) {
223 SVNRepository::ResultCode err = _parser.finishParse();
225 _repo->updateFailed(this, err);
228 _repo->svnUpdateDone();
230 } else if (responseCode() == 404) {
231 _repo->updateFailed(this, SVNRepository::SVN_ERROR_NOT_FOUND);
234 SG_LOG(SG_IO, SG_WARN, "SVN: request for:" << url() <<
235 " got HTTP status " << responseCode());
236 _repo->updateFailed(this, SVNRepository::SVN_ERROR_HTTP);
241 virtual void gotBodyData(const char* s, int n)
247 if (responseCode() != 200) {
251 SVNRepository::ResultCode err = _parser.parseXML(s, n);
254 SG_LOG(SG_IO, SG_WARN, this << ": SVN: request for:" << url() << " failed:" << err);
255 _repo->updateFailed(this, err);
260 virtual void onFail()
262 HTTP::Request::onFail();
264 _repo->updateFailed(this, SVNRepository::SVN_ERROR_SOCKET);
269 SVNReportParser _parser;
270 SVNRepoPrivate* _repo;
276 SVNRepository::SVNRepository(const SGPath& base, HTTP::Client *cl) :
277 _d(new SVNRepoPrivate(this))
280 _d->rootCollection = new SVNDirectory(this, base);
281 _d->baseUrl = _d->rootCollection->url();
284 SVNRepository::~SVNRepository()
286 delete _d->rootCollection;
289 void SVNRepository::setBaseUrl(const std::string &url)
292 _d->rootCollection->setBaseUrl(url);
295 std::string SVNRepository::baseUrl() const
300 HTTP::Client* SVNRepository::http() const
305 SGPath SVNRepository::fsBase() const
307 return _d->rootCollection->fsPath();
310 bool SVNRepository::isBare() const
312 if (!fsBase().exists() || Dir(fsBase()).isEmpty()) {
316 if (_d->vccUrl.empty()) {
323 void SVNRepository::update()
325 _d->status = SVN_NO_ERROR;
326 if (_d->targetRevision.empty() || _d->vccUrl.empty()) {
327 _d->isUpdating = true;
328 PropFindRequest* pfr = new PropFindRequest(_d.get());
329 http()->makeRequest(pfr);
333 if (_d->targetRevision == rootDir()->cachedRevision()) {
334 SG_LOG(SG_IO, SG_DEBUG, baseUrl() << " in sync at version " << _d->targetRevision);
335 _d->isUpdating = false;
339 _d->isUpdating = true;
340 UpdateReportRequest* urr = new UpdateReportRequest(_d.get(),
341 _d->targetRevision, isBare());
342 http()->makeRequest(urr);
345 bool SVNRepository::isDoingSync() const
347 if (_d->status != SVN_NO_ERROR) {
351 return _d->isUpdating || _d->rootCollection->isDoingSync();
354 SVNDirectory* SVNRepository::rootDir() const
356 return _d->rootCollection;
359 SVNRepository::ResultCode
360 SVNRepository::failure() const
365 ///////////////////////////////////////////////////////////////////////////
367 void SVNRepoPrivate::propFindComplete(HTTP::Request* req, DAVCollection* c)
369 targetRevision = c->versionName();
370 vccUrl = makeAbsoluteUrl(c->versionControlledConfiguration(), baseUrl);
371 rootCollection->collection()->setVersionControlledConfiguration(vccUrl);
375 void SVNRepoPrivate::propFindFailed(HTTP::Request *req, SVNRepository::ResultCode err)
377 if (err != SVNRepository::SVN_ERROR_NOT_FOUND) {
378 SG_LOG(SG_IO, SG_WARN, "PropFind failed for:" << req->url());
385 } // of namespace simgear