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"),
122 requestHeader("Depth") = "0";
123 setBodyData( PROPFIND_REQUEST_BODY,
124 "application/xml; charset=\"utf-8\"" );
128 virtual void responseHeadersComplete()
130 if (responseCode() == 207) {
132 } else if (responseCode() == 404) {
133 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_NOT_FOUND);
135 SG_LOG(SG_IO, SG_WARN, "request for:" << url() <<
136 " return code " << responseCode());
137 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_SOCKET);
141 virtual void onDone()
143 if (responseCode() == 207) {
144 _davStatus.finishParse();
145 if (_davStatus.isValid()) {
146 _repo->propFindComplete(this, (DAVCollection*) _davStatus.resource());
148 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_SOCKET);
153 virtual void gotBodyData(const char* s, int n)
155 if (responseCode() != 207) {
158 _davStatus.parseXML(s, n);
161 virtual void onFail()
163 HTTP::Request::onFail();
164 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_SOCKET);
168 SVNRepoPrivate* _repo;
169 DAVMultiStatus _davStatus;
172 class UpdateReportRequest:
176 UpdateReportRequest(SVNRepoPrivate* repo,
177 const std::string& aVersionName,
179 HTTP::Request("", "REPORT"),
184 setUrl(repo->vccUrl);
185 std::string request =
186 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
187 "<S:update-report send-all=\"true\" xmlns:S=\"svn:\">\n"
188 "<S:src-path>" + repo->baseUrl + "</S:src-path>\n"
189 "<S:depth>unknown</S:depth>\n"
190 "<S:entry rev=\"" + aVersionName + "\" depth=\"infinity\" start-empty=\"true\"/>\n";
195 _repo->rootCollection->mergeUpdateReportDetails(0, entries);
196 BOOST_FOREACH(string e, entries)
202 request += "</S:update-report>";
204 setBodyData(request, "application/xml; charset=\"utf-8\"");
208 virtual void onDone()
214 if (responseCode() == 200) {
215 SVNRepository::ResultCode err = _parser.finishParse();
217 _repo->updateFailed(this, err);
220 _repo->svnUpdateDone();
222 } else if (responseCode() == 404) {
223 _repo->updateFailed(this, SVNRepository::SVN_ERROR_NOT_FOUND);
226 SG_LOG(SG_IO, SG_WARN, "SVN: request for:" << url() <<
227 " return code " << responseCode());
228 _repo->updateFailed(this, SVNRepository::SVN_ERROR_SOCKET);
233 virtual void gotBodyData(const char* s, int n)
239 if (responseCode() != 200) {
243 SVNRepository::ResultCode err = _parser.parseXML(s, n);
246 SG_LOG(SG_IO, SG_WARN, "SVN: request for:" << url() << " failed:" << err);
247 _repo->updateFailed(this, err);
251 virtual void onFail()
253 HTTP::Request::onFail();
254 _repo->updateFailed(this, SVNRepository::SVN_ERROR_SOCKET);
257 SVNReportParser _parser;
258 SVNRepoPrivate* _repo;
264 SVNRepository::SVNRepository(const SGPath& base, HTTP::Client *cl) :
265 _d(new SVNRepoPrivate(this))
268 _d->rootCollection = new SVNDirectory(this, base);
269 _d->baseUrl = _d->rootCollection->url();
272 SVNRepository::~SVNRepository()
274 delete _d->rootCollection;
277 void SVNRepository::setBaseUrl(const std::string &url)
280 _d->rootCollection->setBaseUrl(url);
283 std::string SVNRepository::baseUrl() const
288 HTTP::Client* SVNRepository::http() const
293 SGPath SVNRepository::fsBase() const
295 return _d->rootCollection->fsPath();
298 bool SVNRepository::isBare() const
300 if (!fsBase().exists() || Dir(fsBase()).isEmpty()) {
304 if (_d->vccUrl.empty()) {
311 void SVNRepository::update()
313 _d->status = SVN_NO_ERROR;
314 if (_d->targetRevision.empty() || _d->vccUrl.empty()) {
315 _d->isUpdating = true;
316 PropFindRequest* pfr = new PropFindRequest(_d.get());
317 http()->makeRequest(pfr);
321 if (_d->targetRevision == rootDir()->cachedRevision()) {
322 SG_LOG(SG_IO, SG_DEBUG, baseUrl() << " in sync at version " << _d->targetRevision);
323 _d->isUpdating = false;
327 _d->isUpdating = true;
328 UpdateReportRequest* urr = new UpdateReportRequest(_d.get(),
329 _d->targetRevision, isBare());
330 http()->makeRequest(urr);
333 bool SVNRepository::isDoingSync() const
335 if (_d->status != SVN_NO_ERROR) {
339 return _d->isUpdating || _d->rootCollection->isDoingSync();
342 SVNDirectory* SVNRepository::rootDir() const
344 return _d->rootCollection;
347 SVNRepository::ResultCode
348 SVNRepository::failure() const
353 ///////////////////////////////////////////////////////////////////////////
355 void SVNRepoPrivate::propFindComplete(HTTP::Request* req, DAVCollection* c)
357 targetRevision = c->versionName();
358 vccUrl = makeAbsoluteUrl(c->versionControlledConfiguration(), baseUrl);
359 rootCollection->collection()->setVersionControlledConfiguration(vccUrl);
363 void SVNRepoPrivate::propFindFailed(HTTP::Request *req, SVNRepository::ResultCode err)
365 if (err != SVNRepository::SVN_ERROR_NOT_FOUND) {
366 SG_LOG(SG_IO, SG_WARN, "PropFind failed for:" << req->url());
373 } // of namespace simgear