2015-02-27 00:51:51 +01:00
|
|
|
#include "DirBlob.h"
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
//TODO Remove and replace with exception hierarchy
|
|
|
|
#include "messmer/fspp/fuse/FuseErrnoException.h"
|
|
|
|
|
|
|
|
#include <messmer/blockstore/utils/Data.h>
|
|
|
|
#include "MagicNumbers.h"
|
|
|
|
|
|
|
|
using std::unique_ptr;
|
|
|
|
using std::make_unique;
|
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
2015-03-11 00:23:52 +01:00
|
|
|
using std::pair;
|
|
|
|
using std::make_pair;
|
2015-02-27 00:51:51 +01:00
|
|
|
|
|
|
|
using blobstore::Blob;
|
|
|
|
using blockstore::Key;
|
|
|
|
using blockstore::Data;
|
|
|
|
|
|
|
|
//TODO Refactor: Keep a parsed dir structure (list of entries and blob keys they're pointing to) in memory and serialize/deserialize it
|
|
|
|
|
|
|
|
namespace cryfs {
|
|
|
|
|
|
|
|
DirBlob::DirBlob(unique_ptr<Blob> blob)
|
|
|
|
: _blob(std::move(blob)) {
|
2015-03-11 00:23:52 +01:00
|
|
|
assert(magicNumber() == MagicNumbers::DIR);
|
2015-02-27 00:51:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DirBlob::~DirBlob() {
|
|
|
|
}
|
|
|
|
|
2015-03-11 00:23:52 +01:00
|
|
|
unique_ptr<DirBlob> DirBlob::InitializeEmptyDir(unique_ptr<Blob> blob) {
|
|
|
|
blob->resize(1);
|
2015-02-27 00:51:51 +01:00
|
|
|
unsigned char magicNumber = MagicNumbers::DIR;
|
2015-03-11 00:23:52 +01:00
|
|
|
blob->write(&magicNumber, 0, 1);
|
|
|
|
return make_unique<DirBlob>(std::move(blob));
|
2015-02-27 00:51:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char DirBlob::magicNumber() const {
|
|
|
|
unsigned char number;
|
2015-03-11 00:23:52 +01:00
|
|
|
_blob->read(&number, 0, 1);
|
2015-02-27 00:51:51 +01:00
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:51:42 +01:00
|
|
|
unique_ptr<vector<fspp::Dir::Entry>> DirBlob::GetChildren() const {
|
2015-02-27 00:51:51 +01:00
|
|
|
Data entries(_blob->size()-1);
|
|
|
|
_blob->read(entries.data(), 1, _blob->size()-1);
|
|
|
|
|
2015-03-10 21:51:42 +01:00
|
|
|
auto result = make_unique<vector<fspp::Dir::Entry>>();
|
2015-02-27 00:51:51 +01:00
|
|
|
|
|
|
|
const char *pos = (const char*)entries.data();
|
|
|
|
while(pos < (const char*)entries.data()+entries.size()) {
|
|
|
|
pos = readAndAddNextChild(pos, result.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:51:42 +01:00
|
|
|
const char *DirBlob::readAndAddNextChild(const char *pos, vector<fspp::Dir::Entry> *result) const {
|
|
|
|
// Read type magic number (whether it is a dir or a file)
|
|
|
|
fspp::Dir::EntryType type = static_cast<fspp::Dir::EntryType>(*reinterpret_cast<const unsigned char*>(pos));
|
|
|
|
pos += 1;
|
|
|
|
|
2015-02-27 00:51:51 +01:00
|
|
|
size_t length = strlen(pos);
|
2015-03-10 21:51:42 +01:00
|
|
|
std::string name(pos, length);
|
|
|
|
result->emplace_back(fspp::Dir::Entry(type, name));
|
2015-02-27 00:51:51 +01:00
|
|
|
const char *posAfterName = pos + length + 1;
|
|
|
|
const char *posAfterKey = posAfterName + strlen(posAfterName) + 1;
|
|
|
|
return posAfterKey;
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:51:42 +01:00
|
|
|
void DirBlob::AddChildDir(const std::string &name, const Key &blobKey) {
|
|
|
|
AddChild(name, blobKey, fspp::Dir::EntryType::DIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirBlob::AddChildFile(const std::string &name, const Key &blobKey) {
|
|
|
|
AddChild(name, blobKey, fspp::Dir::EntryType::FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirBlob::AddChild(const std::string &name, const Key &blobKey, fspp::Dir::EntryType entryType) {
|
2015-02-27 00:51:51 +01:00
|
|
|
//TODO blob.resize(blob.size()+X) has to traverse tree twice. Better would be blob.addSize(X) which returns old size
|
|
|
|
uint64_t oldBlobSize = _blob->size();
|
|
|
|
string blobKeyStr = blobKey.ToString();
|
|
|
|
_blob->resize(oldBlobSize + name.size() + 1 + blobKeyStr.size() + 1);
|
|
|
|
|
2015-03-10 21:51:42 +01:00
|
|
|
//Write entry type
|
|
|
|
unsigned char entryTypeMagicNumber = static_cast<unsigned char>(entryType);
|
|
|
|
_blob->write(&entryTypeMagicNumber, oldBlobSize, 1);
|
2015-02-27 00:51:51 +01:00
|
|
|
//Write entry name inclusive null terminator
|
2015-03-10 21:51:42 +01:00
|
|
|
_blob->write(name.c_str(), oldBlobSize + 1, name.size()+1);
|
2015-02-27 00:51:51 +01:00
|
|
|
//Write blob key inclusive null terminator
|
2015-03-10 21:51:42 +01:00
|
|
|
_blob->write(blobKeyStr.c_str(), oldBlobSize + 1 + name.size() + 1, blobKeyStr.size()+1);
|
2015-02-27 00:51:51 +01:00
|
|
|
}
|
|
|
|
|
2015-03-11 00:23:52 +01:00
|
|
|
pair<fspp::Dir::EntryType, Key> DirBlob::GetChild(const string &name) const {
|
2015-02-27 00:51:51 +01:00
|
|
|
Data entries(_blob->size()-1);
|
|
|
|
_blob->read(entries.data(), 1, _blob->size()-1);
|
|
|
|
|
|
|
|
const char *pos = (const char*)entries.data();
|
|
|
|
while(pos < (const char*)entries.data()+entries.size()) {
|
2015-03-11 00:23:52 +01:00
|
|
|
fspp::Dir::EntryType type = static_cast<fspp::Dir::EntryType>(*reinterpret_cast<const unsigned char*>(pos));
|
2015-03-10 21:51:42 +01:00
|
|
|
pos += 1; // Skip entry type magic number (whether it is a dir or a file)
|
2015-02-27 00:51:51 +01:00
|
|
|
size_t name_length = strlen(pos);
|
|
|
|
if (name_length == name.size() && 0==std::memcmp(pos, name.c_str(), name_length)) {
|
|
|
|
pos += strlen(pos) + 1; // Skip name
|
2015-03-11 00:23:52 +01:00
|
|
|
return make_pair(type, Key::FromString(pos)); // Return key
|
2015-02-27 00:51:51 +01:00
|
|
|
}
|
|
|
|
pos += strlen(pos) + 1; // Skip name
|
|
|
|
pos += strlen(pos) + 1; // Skip key
|
|
|
|
}
|
|
|
|
throw fspp::fuse::FuseErrnoException(ENOENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|