sapling/ctreemanifest/manifest.cpp
Durham Goode 50d6b599f4 Move ctreemanifest and cdatapack out of remotefilelog
These don't really have any dependencies on remotefilelog, so let's move them
out.
2016-09-21 13:55:12 -07:00

227 lines
6.0 KiB
C++

// manifest.cpp - c++ implementation of a single manifest
//
// Copyright 2016 Facebook, Inc.
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
//
// no-check-code
#include "manifest.h"
Manifest::Manifest(PythonObj &rawobj) :
_rawobj(rawobj) {
char *parseptr, *endptr;
Py_ssize_t buf_sz;
PyString_AsStringAndSize(_rawobj, &parseptr, &buf_sz);
endptr = parseptr + buf_sz;
while (parseptr < endptr) {
ManifestEntry entry;
parseptr = entry.initialize(parseptr);
entries.push_back(entry);
}
}
Manifest *Manifest::copy() {
Manifest *copied = new Manifest();
copied->_rawobj = this->_rawobj;
for (std::list<ManifestEntry>::iterator thisIter = this->entries.begin();
thisIter != this->entries.end();
thisIter ++) {
copied->addChild(copied->entries.end(), &(*thisIter));
}
return copied;
}
ManifestIterator Manifest::getIterator() {
return ManifestIterator(this->entries.begin(), this->entries.end());
}
SortedManifestIterator Manifest::getSortedIterator() {
// populate the sorted list if it's not present.
if (this->entries.size() != this->mercurialSortedEntries.size()) {
this->mercurialSortedEntries.clear();
for (std::list<ManifestEntry>::iterator iterator = this->entries.begin();
iterator != this->entries.end();
iterator ++) {
this->mercurialSortedEntries.push_back(&(*iterator));
}
this->mercurialSortedEntries.sort(ManifestEntry::compareMercurialOrder);
}
return SortedManifestIterator(
this->mercurialSortedEntries.begin(),
this->mercurialSortedEntries.end());
}
/**
* Returns an iterator correctly positioned for a child of a given
* filename. If a child with the same name already exists, *exacthit will
* be set to true. Otherwise, it will be set to false.
*/
std::list<ManifestEntry>::iterator Manifest::findChild(
const char *filename, const size_t filenamelen, bool isdir,
bool *exacthit) {
for (std::list<ManifestEntry>::iterator iter = this->entries.begin();
iter != this->entries.end();
iter ++) {
size_t minlen = filenamelen < iter->filenamelen ?
filenamelen : iter->filenamelen;
// continue until we are lexicographically <= than the current location.
int cmp = strncmp(filename, iter->filename, minlen);
bool current_isdir = iter->isdirectory();
if (cmp == 0 && filenamelen == iter->filenamelen) {
if (current_isdir == isdir) {
*exacthit = true;
return iter;
} else if (current_isdir) {
// the current entry we're looking at is a directory, but we want to
// insert a file. we need to move to the next entry.
continue;
} else {
*exacthit = false;
return iter;
}
} else if (cmp > 0 ||
(cmp == 0 && filenamelen > iter->filenamelen)) {
continue;
} else {
*exacthit = false;
return iter;
}
}
*exacthit = false;
return this->entries.end();
}
ManifestEntry *Manifest::addChild(std::list<ManifestEntry>::iterator iterator,
const char *filename, const size_t filenamelen, const char *node,
const char *flag) {
ManifestEntry entry;
this->entries.insert(iterator, entry);
// move back to the element we just added.
--iterator;
// return a reference to the element we added, not the one on the stack.
ManifestEntry *result = &(*iterator);
result->initialize(filename, filenamelen, node, flag);
// invalidate the mercurial-ordered list of entries
this->mercurialSortedEntries.clear();
return result;
}
ManifestEntry *Manifest::addChild(std::list<ManifestEntry>::iterator iterator,
ManifestEntry *otherChild) {
ManifestEntry entry;
iterator = this->entries.insert(iterator, entry);
// return a reference to the element we added, not the one on the stack.
ManifestEntry *result = &(*iterator);
result->initialize(otherChild);
// invalidate the mercurial-ordered list of entries
this->mercurialSortedEntries.clear();
return result;
}
ManifestIterator::ManifestIterator(
std::list<ManifestEntry>::iterator iterator,
std::list<ManifestEntry>::const_iterator end) :
iterator(iterator), end(end) {
}
ManifestEntry *ManifestIterator::next() {
if (this->isfinished()) {
return NULL;
}
ManifestEntry *result = &(*this->iterator);
this->iterator ++;
return result;
}
ManifestEntry *ManifestIterator::currentvalue() const {
if (this->isfinished()) {
throw std::logic_error("iterator has no current value");
}
return &(*iterator);
}
bool ManifestIterator::isfinished() const {
return iterator == end;
}
SortedManifestIterator::SortedManifestIterator(
std::list<ManifestEntry *>::iterator iterator,
std::list<ManifestEntry *>::const_iterator end) :
iterator(iterator),
end(end) {
}
ManifestEntry *SortedManifestIterator::next() {
if (this->isfinished()) {
return NULL;
}
ManifestEntry *result = *this->iterator;
this->iterator ++;
return result;
}
ManifestEntry *SortedManifestIterator::currentvalue() const {
if (this->isfinished()) {
throw std::logic_error("iterator has no current value");
}
return *iterator;
}
bool SortedManifestIterator::isfinished() const {
return iterator == end;
}
void Manifest::serialize(std::string &result) {
result.erase();
result.reserve(16 * 1024 * 1024);
ManifestIterator iterator = this->getIterator();
ManifestEntry *entry;
while ((entry = iterator.next()) != NULL) {
result.append(entry->filename, entry->filenamelen);
result.append("\0", 1);
result.append(entry->node, HEX_NODE_SIZE);
if (entry->flag) {
result.append(entry->flag, 1);
}
result.append("\n", 1);
}
}
void Manifest::computeNode(const char *p1, const char *p2, char *result) {
std::string content;
this->serialize(content);
SHA_CTX ctx;
SHA1_Init(&ctx);
SHA1_Update(&ctx, p1, BIN_NODE_SIZE);
SHA1_Update(&ctx, p2, BIN_NODE_SIZE);
SHA1_Update(&ctx, content.c_str(), content.size());
SHA1_Final((unsigned char*)result, &ctx);
}