1 // SVNReportParser -- parser for SVN report XML data
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.
20 # include <simgear_config.h>
23 #include "SVNReportParser.hxx"
32 #include <boost/foreach.hpp>
34 #include "simgear/misc/sg_path.hxx"
35 #include "simgear/misc/sg_dir.hxx"
36 #include "simgear/debug/logstream.hxx"
37 #include "simgear/xml/easyxml.hxx"
38 #include "simgear/misc/strutils.hxx"
39 #include "simgear/package/md5.h"
44 # include "sg_expat.h"
47 #include "SVNDirectory.hxx"
48 #include "SVNRepository.hxx"
49 #include "DAVMultiStatus.hxx"
56 using namespace simgear;
58 #define DAV_NS "DAV::"
59 #define SVN_NS "svn::"
60 #define SUBVERSION_DAV_NS "http://subversion.tigris.org/xmlns/dav/"
64 #define MAX_ENCODED_INT_LEN 10
67 decode_size(unsigned char* &p,
68 const unsigned char *end)
70 if (p + MAX_ENCODED_INT_LEN < end)
71 end = p + MAX_ENCODED_INT_LEN;
72 /* Decode bytes until we're done. */
76 result = (result << 7) | (*p & 0x7f);
77 if (((*p++ >> 7) & 0x1) == 0) {
86 try_decode_size(unsigned char* &p,
87 const unsigned char *end)
89 if (p + MAX_ENCODED_INT_LEN < end)
90 end = p + MAX_ENCODED_INT_LEN;
93 if (((*p++ >> 7) & 0x1) == 0) {
101 // const char* SVN_UPDATE_REPORT_TAG = SVN_NS "update-report";
102 // const char* SVN_TARGET_REVISION_TAG = SVN_NS "target-revision";
103 const char* SVN_OPEN_DIRECTORY_TAG = SVN_NS "open-directory";
104 const char* SVN_OPEN_FILE_TAG = SVN_NS "open-file";
105 const char* SVN_ADD_DIRECTORY_TAG = SVN_NS "add-directory";
106 const char* SVN_ADD_FILE_TAG = SVN_NS "add-file";
107 const char* SVN_TXDELTA_TAG = SVN_NS "txdelta";
108 const char* SVN_SET_PROP_TAG = SVN_NS "set-prop";
109 const char* SVN_PROP_TAG = SVN_NS "prop";
110 const char* SVN_DELETE_ENTRY_TAG = SVN_NS "delete-entry";
112 const char* SVN_DAV_MD5_CHECKSUM = SUBVERSION_DAV_NS ":md5-checksum";
114 const char* DAV_HREF_TAG = DAV_NS "href";
115 const char* DAV_CHECKED_IN_TAG = DAV_NS "checked-in";
118 const int svn_txdelta_source = 0;
119 const int svn_txdelta_target = 1;
120 const int svn_txdelta_new = 2;
122 const size_t DELTA_HEADER_SIZE = 4;
125 * helper struct to decode and store the SVN delta header
128 struct SVNDeltaWindow
132 static bool isWindowComplete(unsigned char* buffer, size_t bytes)
134 unsigned char* p = buffer;
135 unsigned char* pEnd = p + bytes;
136 // if we can't decode five sizes, certainly incomplete
137 for (int i=0; i<5; i++) {
138 if (!try_decode_size(p, pEnd)) {
144 // ignore these three
145 decode_size(p, pEnd);
146 decode_size(p, pEnd);
147 decode_size(p, pEnd);
148 size_t instructionLen = decode_size(p, pEnd);
149 size_t newLength = decode_size(p, pEnd);
150 size_t headerLength = p - buffer;
152 return (bytes >= (instructionLen + newLength + headerLength));
155 SVNDeltaWindow(unsigned char* p) :
159 sourceViewOffset = decode_size(p, p+20);
160 sourceViewLength = decode_size(p, p+20);
161 targetViewLength = decode_size(p, p+20);
162 instructionLength = decode_size(p, p+20);
163 newLength = decode_size(p, p+20);
165 headerLength = p - _ptr;
169 bool apply(std::vector<unsigned char>& output, std::istream& source)
171 unsigned char* pEnd = _ptr + instructionLength;
172 unsigned char* newData = pEnd;
174 while (_ptr < pEnd) {
175 int op = ((*_ptr >> 6) & 0x3);
177 SG_LOG(SG_IO, SG_INFO, "SVNDeltaWindow: bad opcode:" << op);
181 int length = *_ptr++ & 0x3f;
185 length = decode_size(_ptr, pEnd);
189 SG_LOG(SG_IO, SG_INFO, "SVNDeltaWindow: malformed stream, 0 length" << op);
193 // if op != new, decode another size value
194 if (op != svn_txdelta_new) {
195 offset = decode_size(_ptr, pEnd);
198 if (op == svn_txdelta_target) {
199 // this is inefficent, but ranges can overlap.
201 output.push_back(output[offset++]);
204 } else if (op == svn_txdelta_new) {
205 output.insert(output.end(), newData, newData + length);
207 } else if (op == svn_txdelta_source) {
208 source.seekg(offset);
209 char* sourceBuf = (char*) malloc(length);
211 source.read(sourceBuf, length);
212 output.insert(output.end(), sourceBuf, sourceBuf + length);
215 SG_LOG(SG_IO, SG_WARN, "bad opcode logic");
218 } // of instruction loop
225 return headerLength + instructionLength + newLength;
228 unsigned int sourceViewOffset;
229 size_t sourceViewLength,
240 } // of anonymous namespace
242 class SVNReportParser::SVNReportParserPrivate
245 SVNReportParserPrivate(SVNRepository* repo) :
247 status(SVNRepository::SVN_NO_ERROR),
249 currentPath(repo->fsBase())
252 currentDir = repo->rootDir();
255 ~SVNReportParserPrivate()
259 void startElement (const char * name, const char** attributes)
261 if (status != SVNRepository::SVN_NO_ERROR) {
265 ExpatAtts attrs(attributes);
266 tagStack.push_back(name);
267 if (!strcmp(name, SVN_TXDELTA_TAG)) {
269 } else if (!strcmp(name, SVN_ADD_FILE_TAG)) {
270 string fileName(attrs.getValue("name"));
271 SGPath filePath(currentDir->fsDir().file(fileName));
272 currentPath = filePath;
274 } else if (!strcmp(name, SVN_OPEN_FILE_TAG)) {
275 string fileName(attrs.getValue("name"));
276 SGPath filePath(Dir(currentPath).file(fileName));
277 currentPath = filePath;
279 if (!filePath.exists()) {
280 fail(SVNRepository::SVN_ERROR_FILE_NOT_FOUND);
285 } else if (!strcmp(name, SVN_ADD_DIRECTORY_TAG)) {
286 string dirName(attrs.getValue("name"));
287 Dir d(currentDir->fsDir().file(dirName));
289 // policy decision : if we're doing an add, wipe the existing
293 currentDir = currentDir->addChildDirectory(dirName);
294 currentPath = currentDir->fsPath();
295 currentDir->beginUpdateReport();
296 //cout << "addDir:" << currentPath << endl;
297 } else if (!strcmp(name, SVN_SET_PROP_TAG)) {
298 setPropName = attrs.getValue("name");
299 setPropValue.clear();
300 } else if (!strcmp(name, SVN_DAV_MD5_CHECKSUM)) {
302 } else if (!strcmp(name, SVN_OPEN_DIRECTORY_TAG)) {
304 if (attrs.getValue("name")) {
305 dirName = string(attrs.getValue("name"));
307 openDirectory(dirName);
308 } else if (!strcmp(name, SVN_DELETE_ENTRY_TAG)) {
309 string entryName(attrs.getValue("name"));
310 deleteEntry(entryName);
311 } else if (!strcmp(name, DAV_CHECKED_IN_TAG) ||
312 !strcmp(name, DAV_HREF_TAG) ||
313 !strcmp(name, SVN_PROP_TAG)) {
314 // don't warn on these ones
316 //SG_LOG(SG_IO, SG_WARN, "SVNReportParser: unhandled tag:" << name);
320 void openDirectory(const std::string& dirName)
322 if (dirName.empty()) {
323 // root directory, we shall assume
324 currentDir = tree->rootDir();
327 currentDir = currentDir->child(dirName);
331 currentPath = currentDir->fsPath();
332 currentDir->beginUpdateReport();
335 void deleteEntry(const std::string& entryName)
337 currentDir->deleteChildByName(entryName);
340 bool decodeTextDelta(const SGPath& outputPath)
342 std::vector<unsigned char> output, decoded;
343 strutils::decodeBase64(txDeltaData, decoded);
344 size_t bytesToDecode = decoded.size();
346 unsigned char* p = decoded.data();
347 if (memcmp(p, "SVN\0", DELTA_HEADER_SIZE) != 0) {
348 return false; // bad header
351 bytesToDecode -= DELTA_HEADER_SIZE;
352 p += DELTA_HEADER_SIZE;
353 std::ifstream source;
354 source.open(outputPath.c_str(), std::ios::in | std::ios::binary);
356 while (bytesToDecode > 0) {
357 if (!SVNDeltaWindow::isWindowComplete(p, bytesToDecode)) {
358 SG_LOG(SG_IO, SG_WARN, "SVN txdelta broken window");
362 SVNDeltaWindow window(p);
363 assert(bytesToDecode >= window.size());
364 window.apply(output, source);
365 bytesToDecode -= window.size();
372 f.open(outputPath.c_str(),
373 std::ios::out | std::ios::trunc | std::ios::binary);
374 f.write((char*) output.data(), output.size());
376 // compute MD5 while we have the file in memory
377 memset(&md5Context, 0, sizeof(SG_MD5_CTX));
378 SG_MD5Init(&md5Context);
379 SG_MD5Update(&md5Context, (unsigned char*) output.data(), output.size());
380 SG_MD5Final(&md5Context);
381 decodedFileMd5 = strutils::encodeHex(md5Context.digest, 16);
386 void endElement (const char * name)
388 if (status != SVNRepository::SVN_NO_ERROR) {
392 assert(tagStack.back() == name);
395 if (!strcmp(name, SVN_TXDELTA_TAG)) {
396 if (!decodeTextDelta(currentPath)) {
397 fail(SVNRepository::SVN_ERROR_TXDELTA);
399 } else if (!strcmp(name, SVN_ADD_FILE_TAG)) {
400 finishFile(currentPath);
401 } else if (!strcmp(name, SVN_OPEN_FILE_TAG)) {
402 finishFile(currentPath);
403 } else if (!strcmp(name, SVN_ADD_DIRECTORY_TAG)) {
405 currentPath = currentPath.dir();
406 currentDir->updateReportComplete();
407 currentDir = currentDir->parent();
408 } else if (!strcmp(name, SVN_SET_PROP_TAG)) {
409 if (setPropName == "svn:entry:committed-rev") {
410 revision = strutils::to_int(setPropValue);
411 currentVersionName = setPropValue;
413 // for directories we have the resource already
414 // for adding files, we might not; we set the version name
415 // above when ending the add/open-file element
416 currentDir->collection()->setVersionName(currentVersionName);
419 } else if (!strcmp(name, SVN_DAV_MD5_CHECKSUM)) {
420 // validate against (presumably) just written file
421 if (decodedFileMd5 != md5Sum) {
422 fail(SVNRepository::SVN_ERROR_CHECKSUM);
424 } else if (!strcmp(name, SVN_OPEN_DIRECTORY_TAG)) {
425 currentDir->updateReportComplete();
426 if (currentDir->parent()) {
427 // pop the collection stack
428 currentDir = currentDir->parent();
431 currentPath = currentDir->fsPath();
433 // std::cout << "element:" << name;
437 void finishFile(const SGPath& path)
439 currentPath = path.dir();
443 void data (const char * s, int length)
445 if (status != SVNRepository::SVN_NO_ERROR) {
449 if (tagStack.back() == SVN_SET_PROP_TAG) {
450 setPropValue.append(s, length);
451 } else if (tagStack.back() == SVN_TXDELTA_TAG) {
452 txDeltaData.append(s, length);
453 } else if (tagStack.back() == SVN_DAV_MD5_CHECKSUM) {
454 md5Sum.append(s, length);
458 void pi (const char * target, const char * data) {}
460 string tagN(const unsigned int n) const
462 size_t sz = tagStack.size();
467 return tagStack[sz - (1 + n)];
470 void fail(SVNRepository::ResultCode err)
476 DAVCollection* rootCollection;
477 SVNDirectory* currentDir;
478 SVNRepository::ResultCode status;
481 XML_Parser xmlParser;
484 string_list tagStack;
485 string currentVersionName;
490 unsigned int revision;
491 SG_MD5_CTX md5Context;
492 string md5Sum, decodedFileMd5;
493 std::string setPropName, setPropValue;
497 ////////////////////////////////////////////////////////////////////////
498 // Static callback functions for Expat.
499 ////////////////////////////////////////////////////////////////////////
501 #define VISITOR static_cast<SVNReportParser::SVNReportParserPrivate *>(userData)
504 start_element (void * userData, const char * name, const char ** atts)
506 VISITOR->startElement(name, atts);
510 end_element (void * userData, const char * name)
512 VISITOR->endElement(name);
516 character_data (void * userData, const char * s, int len)
518 VISITOR->data(s, len);
522 processing_instruction (void * userData,
526 VISITOR->pi(target, data);
531 ///////////////////////////////////////////////////////////////////////////////
533 SVNReportParser::SVNReportParser(SVNRepository* repo) :
534 _d(new SVNReportParserPrivate(repo))
539 SVNReportParser::~SVNReportParser()
543 SVNRepository::ResultCode
544 SVNReportParser::innerParseXML(const char* data, int size)
546 if (_d->status != SVNRepository::SVN_NO_ERROR) {
550 bool isEnd = (data == NULL);
551 if (!XML_Parse(_d->xmlParser, data, size, isEnd)) {
552 SG_LOG(SG_IO, SG_INFO, "SVN parse error:" << XML_ErrorString(XML_GetErrorCode(_d->xmlParser))
553 << " at line:" << XML_GetCurrentLineNumber(_d->xmlParser)
554 << " column " << XML_GetCurrentColumnNumber(_d->xmlParser));
556 XML_ParserFree(_d->xmlParser);
557 _d->parserInited = false;
558 return SVNRepository::SVN_ERROR_XML;
560 XML_ParserFree(_d->xmlParser);
561 _d->parserInited = false;
567 SVNRepository::ResultCode
568 SVNReportParser::parseXML(const char* data, int size)
570 if (_d->status != SVNRepository::SVN_NO_ERROR) {
574 if (!_d->parserInited) {
575 _d->xmlParser = XML_ParserCreateNS(0, ':');
576 XML_SetUserData(_d->xmlParser, _d.get());
577 XML_SetElementHandler(_d->xmlParser, start_element, end_element);
578 XML_SetCharacterDataHandler(_d->xmlParser, character_data);
579 XML_SetProcessingInstructionHandler(_d->xmlParser, processing_instruction);
580 _d->parserInited = true;
583 return innerParseXML(data, size);
586 SVNRepository::ResultCode SVNReportParser::finishParse()
588 if (_d->status != SVNRepository::SVN_NO_ERROR) {
592 return innerParseXML(NULL, 0);
595 std::string SVNReportParser::etagFromRevision(unsigned int revision)
597 // etags look like W/"7//", hopefully this is stable
598 // across different servers and similar
599 std::ostringstream os;
600 os << "W/\"" << revision << "//";