2016-08-23 01:40:56 +03:00
|
|
|
// manifest_entry.cpp - c++ implementation of a single manifest entry
|
|
|
|
//
|
|
|
|
// 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_entry.h"
|
|
|
|
|
|
|
|
ManifestEntry::ManifestEntry() {
|
|
|
|
this->filename = NULL;
|
|
|
|
this->filenamelen = 0;
|
|
|
|
this->node = NULL;
|
|
|
|
this->flag = NULL;
|
2016-08-26 23:49:17 +03:00
|
|
|
this->ownedmemory = NULL;
|
2016-08-23 01:40:56 +03:00
|
|
|
}
|
|
|
|
|
2016-08-26 23:49:17 +03:00
|
|
|
void ManifestEntry::initialize(
|
2016-08-23 01:40:56 +03:00
|
|
|
const char *filename, const size_t filenamelen,
|
|
|
|
const char *node,
|
2016-09-18 23:40:14 +03:00
|
|
|
const char *flag) {
|
|
|
|
if (flag != NULL && *flag == MANIFEST_DIRECTORY_FLAG) {
|
2016-10-15 02:01:12 +03:00
|
|
|
this->resolved = ManifestPtr(new Manifest());
|
2016-09-06 22:40:15 +03:00
|
|
|
}
|
2016-08-23 01:40:56 +03:00
|
|
|
this->ownedmemory = new char[
|
|
|
|
filenamelen +
|
|
|
|
1 + // null character
|
2016-09-11 02:27:53 +03:00
|
|
|
HEX_NODE_SIZE + // node hash
|
2016-08-23 01:40:56 +03:00
|
|
|
1 + // flag
|
|
|
|
1 // NL
|
|
|
|
];
|
|
|
|
|
|
|
|
// set up the pointers.
|
2016-08-26 23:49:17 +03:00
|
|
|
this->filename = this->ownedmemory;
|
2016-08-23 01:40:56 +03:00
|
|
|
if (node == NULL) {
|
|
|
|
this->node = NULL;
|
|
|
|
} else {
|
|
|
|
this->node = this->filename + filenamelen + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up the null character and NL.
|
|
|
|
this->filename[filenamelen] = '\0';
|
2016-09-08 23:34:56 +03:00
|
|
|
*(this->filename + filenamelen + 1 + HEX_NODE_SIZE + 1) = '\n';
|
2016-08-23 01:40:56 +03:00
|
|
|
|
|
|
|
// set up filenamelen
|
|
|
|
this->filenamelen = filenamelen;
|
|
|
|
|
|
|
|
// write the memory.
|
|
|
|
memcpy(this->filename, filename, filenamelen);
|
|
|
|
if (node != NULL) {
|
2016-09-11 02:27:53 +03:00
|
|
|
memcpy(this->node, node, HEX_NODE_SIZE);
|
2016-08-23 01:40:56 +03:00
|
|
|
}
|
2016-09-18 23:40:14 +03:00
|
|
|
if (flag == NULL) {
|
2016-09-08 23:34:56 +03:00
|
|
|
*(this->filename + filenamelen + 1 + HEX_NODE_SIZE) = '\n';
|
|
|
|
this->flag = NULL;
|
2016-08-23 01:40:56 +03:00
|
|
|
} else {
|
2016-09-08 23:34:56 +03:00
|
|
|
this->flag = this->filename + filenamelen + 1 + HEX_NODE_SIZE;
|
2016-09-18 23:40:14 +03:00
|
|
|
*this->flag = *flag;
|
2016-08-23 01:40:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:25:34 +03:00
|
|
|
char *ManifestEntry::initialize(char *entrystart) {
|
2016-08-23 01:40:56 +03:00
|
|
|
// Each entry is of the format:
|
|
|
|
//
|
|
|
|
// <filename>\0<40-byte hash><optional 1 byte flag>\n
|
|
|
|
//
|
|
|
|
// Where flags can be 't' to represent a sub directory
|
|
|
|
this->filename = entrystart;
|
|
|
|
char *nulldelimiter = strchr(entrystart, '\0');
|
|
|
|
this->filenamelen = nulldelimiter - entrystart;
|
|
|
|
|
|
|
|
this->node = nulldelimiter + 1;
|
|
|
|
|
|
|
|
this->flag = nulldelimiter + 41;
|
2016-09-19 23:25:34 +03:00
|
|
|
char *nextpointer;
|
2016-08-23 01:40:56 +03:00
|
|
|
if (*this->flag != '\n') {
|
2016-09-19 23:25:34 +03:00
|
|
|
nextpointer = this->flag + 2;
|
2016-08-23 01:40:56 +03:00
|
|
|
} else {
|
|
|
|
// No flag
|
2016-09-19 23:25:34 +03:00
|
|
|
nextpointer = this->flag + 1;
|
2016-08-23 01:40:56 +03:00
|
|
|
this->flag = NULL;
|
|
|
|
}
|
2016-10-15 02:01:12 +03:00
|
|
|
this->resolved = ManifestPtr();
|
2016-08-26 23:49:17 +03:00
|
|
|
this->ownedmemory = NULL;
|
2016-09-19 23:25:34 +03:00
|
|
|
|
|
|
|
return nextpointer;
|
2016-08-23 01:40:56 +03:00
|
|
|
}
|
|
|
|
|
2016-09-12 21:44:47 +03:00
|
|
|
void ManifestEntry::initialize(ManifestEntry *other) {
|
|
|
|
if (other->ownedmemory) {
|
2016-09-18 23:40:14 +03:00
|
|
|
this->initialize(other->filename, other->filenamelen,
|
|
|
|
other->node, other->flag);
|
2016-10-15 02:01:12 +03:00
|
|
|
if (other->resolved.isnull()) {
|
|
|
|
this->resolved = ManifestPtr();
|
2016-09-19 23:25:34 +03:00
|
|
|
}
|
2016-09-12 21:44:47 +03:00
|
|
|
} else {
|
|
|
|
// Else it points at a piece of memory owned by something else
|
|
|
|
this->initialize(other->filename);
|
|
|
|
}
|
|
|
|
|
2016-10-15 02:01:12 +03:00
|
|
|
if (!other->resolved.isnull()) {
|
2016-10-15 02:01:12 +03:00
|
|
|
if (other->resolved->isMutable()) {
|
|
|
|
this->resolved = other->resolved->copy();
|
|
|
|
} else {
|
|
|
|
this->resolved = other->resolved;
|
|
|
|
}
|
2016-09-12 21:44:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-27 00:00:37 +03:00
|
|
|
ManifestEntry::~ManifestEntry() {
|
|
|
|
if (this->ownedmemory != NULL) {
|
|
|
|
delete [] this->ownedmemory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 01:40:56 +03:00
|
|
|
bool ManifestEntry::isdirectory() const {
|
2016-09-06 22:38:54 +03:00
|
|
|
return this->flag && *this->flag == MANIFEST_DIRECTORY_FLAG;
|
2016-08-23 01:40:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ManifestEntry::appendtopath(std::string &path) {
|
|
|
|
path.append(this->filename, this->filenamelen);
|
|
|
|
if (this->isdirectory()) {
|
|
|
|
path.append(1, '/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 02:01:12 +03:00
|
|
|
ManifestPtr ManifestEntry::get_manifest(
|
2016-08-23 01:42:03 +03:00
|
|
|
ManifestFetcher fetcher, const char *path, size_t pathlen) {
|
2016-10-15 02:01:12 +03:00
|
|
|
if (this->resolved.isnull()) {
|
2016-08-23 01:40:56 +03:00
|
|
|
std::string binnode = binfromhex(node);
|
2016-08-23 01:42:03 +03:00
|
|
|
this->resolved = fetcher.get(path, pathlen, binnode);
|
2016-08-23 01:40:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return this->resolved;
|
|
|
|
}
|
2016-09-08 23:39:48 +03:00
|
|
|
|
2016-09-18 23:40:14 +03:00
|
|
|
void ManifestEntry::update(const char *node, const char *flag) {
|
2016-09-08 23:39:48 +03:00
|
|
|
// we cannot flip between file and directory.
|
|
|
|
bool wasdir = this->flag != NULL && *this->flag == MANIFEST_DIRECTORY_FLAG;
|
2016-09-18 23:40:14 +03:00
|
|
|
bool willbedir = flag != NULL && *flag == MANIFEST_DIRECTORY_FLAG;
|
2016-09-08 23:39:48 +03:00
|
|
|
|
|
|
|
if (wasdir != willbedir) {
|
|
|
|
throw std::logic_error("changing to/from directory is not permitted");
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we didn't previously own the memory, we should now.
|
|
|
|
if (this->ownedmemory == NULL) {
|
2016-10-15 02:01:12 +03:00
|
|
|
ManifestPtr oldresolved = this->resolved;
|
2016-09-08 23:39:48 +03:00
|
|
|
this->initialize(this->filename, this->filenamelen, node, flag);
|
2016-09-19 23:25:34 +03:00
|
|
|
this->resolved = oldresolved;
|
2016-09-08 23:39:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize node if it's not already done.
|
|
|
|
if (this->node == NULL) {
|
|
|
|
this->node = this->filename + this->filenamelen + 1;
|
|
|
|
}
|
|
|
|
memcpy(this->node, node, HEX_NODE_SIZE);
|
|
|
|
|
2016-09-18 23:40:14 +03:00
|
|
|
if (flag == NULL) {
|
2016-09-08 23:39:48 +03:00
|
|
|
*(this->filename + this->filenamelen + 1 + HEX_NODE_SIZE) = '\n';
|
|
|
|
this->flag = NULL;
|
|
|
|
} else {
|
|
|
|
this->flag = this->filename + filenamelen + 1 + HEX_NODE_SIZE;
|
2016-09-18 23:40:14 +03:00
|
|
|
*this->flag = *flag;
|
2016-09-08 23:39:48 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-18 23:42:09 +03:00
|
|
|
|
|
|
|
static size_t mercurialOrderFilenameLength(const ManifestEntry &entry) {
|
|
|
|
return entry.filenamelen +
|
|
|
|
((entry.flag != NULL && *entry.flag == MANIFEST_DIRECTORY_FLAG) ?
|
|
|
|
1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char mercurialOrderFilenameCharAt(
|
|
|
|
const ManifestEntry &entry, size_t offset) {
|
|
|
|
if (offset < entry.filenamelen) {
|
|
|
|
return entry.filename[offset];
|
|
|
|
} else if (offset == entry.filenamelen &&
|
|
|
|
(entry.flag != NULL && *entry.flag == MANIFEST_DIRECTORY_FLAG)) {
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::out_of_range("Illegal index for manifest entry");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ManifestEntry::compareMercurialOrder(
|
|
|
|
ManifestEntry * const &left,
|
|
|
|
ManifestEntry * const &right) {
|
|
|
|
size_t leftlen = mercurialOrderFilenameLength(*left);
|
|
|
|
size_t rightlen = mercurialOrderFilenameLength(*right);
|
|
|
|
size_t minlen = (leftlen < rightlen) ? leftlen : rightlen;
|
|
|
|
|
|
|
|
for (size_t ix = 0; ix < minlen; ix ++) {
|
2016-09-18 23:46:47 +03:00
|
|
|
unsigned char leftchar = mercurialOrderFilenameCharAt(*left, ix);
|
|
|
|
unsigned char rightchar = mercurialOrderFilenameCharAt(*right, ix);
|
2016-09-18 23:42:09 +03:00
|
|
|
|
|
|
|
if (leftchar < rightchar) {
|
|
|
|
return true;
|
|
|
|
} else if (leftchar > rightchar) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// same up to minlen.
|
|
|
|
if (leftlen < rightlen) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-19 23:25:34 +03:00
|
|
|
|
|
|
|
int ManifestEntry::compareName(ManifestEntry *left, ManifestEntry *right) {
|
|
|
|
assert(left || right);
|
|
|
|
|
|
|
|
// If left is empty, then it is greater than right. This makes this function
|
|
|
|
// useful for iterating right after left has already finished.
|
|
|
|
if (!left) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (!right) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t minlen = left->filenamelen < right->filenamelen ?
|
|
|
|
left->filenamelen : right->filenamelen;
|
|
|
|
int cmp = strncmp(left->filename, right->filename, minlen);
|
|
|
|
if (cmp == 0 && left->filenamelen == right->filenamelen) {
|
|
|
|
return 0;
|
|
|
|
} else if (cmp > 0 ||
|
|
|
|
(cmp == 0 && left->filenamelen > right->filenamelen)) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|