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"),
123 requestHeader("Depth") = "0";
124 setBodyData( PROPFIND_REQUEST_BODY,
125 "application/xml; charset=\"utf-8\"" );
129 virtual void responseHeadersComplete()
131 if (responseCode() == 207) {
133 } else if (responseCode() == 404) {
134 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_NOT_FOUND);
136 SG_LOG(SG_IO, SG_WARN, "request for:" << url() <<
137 " return code " << responseCode());
138 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_SOCKET);
143 virtual void onDone()
145 if (responseCode() == 207) {
146 _davStatus.finishParse();
147 if (_davStatus.isValid()) {
148 _repo->propFindComplete(this, (DAVCollection*) _davStatus.resource());
150 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_SOCKET);
155 virtual void gotBodyData(const char* s, int n)
157 if (responseCode() != 207) {
160 _davStatus.parseXML(s, n);
163 virtual void onFail()
165 HTTP::Request::onFail();
167 _repo->propFindFailed(this, SVNRepository::SVN_ERROR_SOCKET);
173 SVNRepoPrivate* _repo;
174 DAVMultiStatus _davStatus;
177 class UpdateReportRequest:
181 UpdateReportRequest(SVNRepoPrivate* repo,
182 const std::string& aVersionName,
184 HTTP::Request("", "REPORT"),
189 setUrl(repo->vccUrl);
190 std::string request =
191 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
192 "<S:update-report send-all=\"true\" xmlns:S=\"svn:\">\n"
193 "<S:src-path>" + repo->baseUrl + "</S:src-path>\n"
194 "<S:depth>unknown</S:depth>\n"
195 "<S:entry rev=\"" + aVersionName + "\" depth=\"infinity\" start-empty=\"true\"/>\n";
200 _repo->rootCollection->mergeUpdateReportDetails(0, entries);
201 BOOST_FOREACH(string e, entries)
207 request += "</S:update-report>";
209 setBodyData(request, "application/xml; charset=\"utf-8\"");
213 virtual void onDone()
219 if (responseCode() == 200) {
220 SVNRepository::ResultCode err = _parser.finishParse();
222 _repo->updateFailed(this, err);
225 _repo->svnUpdateDone();
227 } else if (responseCode() == 404) {
228 _repo->updateFailed(this, SVNRepository::SVN_ERROR_NOT_FOUND);
231 SG_LOG(SG_IO, SG_WARN, "SVN: request for:" << url() <<
232 " got HTTP status " << responseCode());
233 _repo->updateFailed(this, SVNRepository::SVN_ERROR_HTTP);
238 virtual void gotBodyData(const char* s, int n)
244 if (responseCode() != 200) {
248 SVNRepository::ResultCode err = _parser.parseXML(s, n);
251 SG_LOG(SG_IO, SG_WARN, this << ": SVN: request for:" << url() << " failed:" << err);
252 _repo->updateFailed(this, err);
257 virtual void onFail()
259 HTTP::Request::onFail();
261 _repo->updateFailed(this, SVNRepository::SVN_ERROR_SOCKET);
266 SVNReportParser _parser;
267 SVNRepoPrivate* _repo;
273 SVNRepository::SVNRepository(const SGPath& base, HTTP::Client *cl) :
274 _d(new SVNRepoPrivate(this))
277 _d->rootCollection = new SVNDirectory(this, base);
278 _d->baseUrl = _d->rootCollection->url();
281 SVNRepository::~SVNRepository()
283 delete _d->rootCollection;
286 void SVNRepository::setBaseUrl(const std::string &url)
289 _d->rootCollection->setBaseUrl(url);
292 std::string SVNRepository::baseUrl() const
297 HTTP::Client* SVNRepository::http() const
302 SGPath SVNRepository::fsBase() const
304 return _d->rootCollection->fsPath();
307 bool SVNRepository::isBare() const
309 if (!fsBase().exists() || Dir(fsBase()).isEmpty()) {
313 if (_d->vccUrl.empty()) {
320 void SVNRepository::update()
322 _d->status = SVN_NO_ERROR;
323 if (_d->targetRevision.empty() || _d->vccUrl.empty()) {
324 _d->isUpdating = true;
325 PropFindRequest* pfr = new PropFindRequest(_d.get());
326 http()->makeRequest(pfr);
330 if (_d->targetRevision == rootDir()->cachedRevision()) {
331 SG_LOG(SG_IO, SG_DEBUG, baseUrl() << " in sync at version " << _d->targetRevision);
332 _d->isUpdating = false;
336 _d->isUpdating = true;
337 UpdateReportRequest* urr = new UpdateReportRequest(_d.get(),
338 _d->targetRevision, isBare());
339 http()->makeRequest(urr);
342 bool SVNRepository::isDoingSync() const
344 if (_d->status != SVN_NO_ERROR) {
348 return _d->isUpdating || _d->rootCollection->isDoingSync();
351 SVNDirectory* SVNRepository::rootDir() const
353 return _d->rootCollection;
356 SVNRepository::ResultCode
357 SVNRepository::failure() const
362 ///////////////////////////////////////////////////////////////////////////
364 void SVNRepoPrivate::propFindComplete(HTTP::Request* req, DAVCollection* c)
366 targetRevision = c->versionName();
367 vccUrl = makeAbsoluteUrl(c->versionControlledConfiguration(), baseUrl);
368 rootCollection->collection()->setVersionControlledConfiguration(vccUrl);
372 void SVNRepoPrivate::propFindFailed(HTTP::Request *req, SVNRepository::ResultCode err)
374 if (err != SVNRepository::SVN_ERROR_NOT_FOUND) {
375 SG_LOG(SG_IO, SG_WARN, "PropFind failed for:" << req->url());
382 } // of namespace simgear