sapling/remotefilelog/ctreemanifest/manifest_entry.cpp
Tony Tung 33f0d41725 [ctree] modularize the code
Summary:
I like many small files.

There is one place where I'm making a functional change (convert.h) to satisfy angry compilers.

Test Plan: make local.

Reviewers: #fastmanifest, durham

Reviewed By: durham

Subscribers: mitrandir

Differential Revision: https://phabricator.intern.facebook.com/D3732584

Signature: t1:3732584:1471542758:d0b7804753ea4fd39a507090338ae3c5104dc7fa
2016-08-22 15:40:56 -07:00

108 lines
2.5 KiB
C++

// 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;
this->resolved = NULL;
}
ManifestEntry::ManifestEntry(
const char *filename, const size_t filenamelen,
const char *node,
char flag) {
this->ownedmemory = new char[
filenamelen +
1 + // null character
40 + // node hash
1 + // flag
1 // NL
];
// set up the pointers.
this->filename = (char *) this->ownedmemory;
if (node == NULL) {
this->node = NULL;
} else {
this->node = this->filename + filenamelen + 1;
}
this->flag = this->filename + filenamelen + 1 + 40;
// set up the null character and NL.
this->filename[filenamelen] = '\0';
*(this->flag + 1) = '\n';
// set up filenamelen
this->filenamelen = filenamelen;
// write the memory.
memcpy(this->filename, filename, filenamelen);
if (node != NULL) {
memcpy(this->node, node, 40);
}
if (flag == '\0') {
*this->flag = '\n';
} else {
*this->flag = flag;
}
}
/**
* Given the start of a file/dir entry in a manifest, returns a
* ManifestEntry structure with the parsed data.
*/
ManifestEntry::ManifestEntry(char *&entrystart) {
// 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;
if (*this->flag != '\n') {
entrystart = this->flag + 2;
} else {
// No flag
entrystart = this->flag + 1;
this->flag = NULL;
}
this->resolved = NULL;
}
bool ManifestEntry::isdirectory() const {
return this->flag && *this->flag == 't';
}
void ManifestEntry::appendtopath(std::string &path) {
path.append(this->filename, this->filenamelen);
if (this->isdirectory()) {
path.append(1, '/');
}
}
Manifest *ManifestEntry::get_manifest(ManifestFetcher fetcher, std::string &path) {
if (this->resolved == NULL) {
std::string binnode = binfromhex(node);
manifestkey key(&path, &binnode);
this->resolved = fetcher.get(key);
}
return this->resolved;
}