Dir::children() returns not only the entry name, but also whether it is a file or a directory entry

This commit is contained in:
Sebastian Messmer 2015-03-10 21:51:42 +01:00
parent 43c4f29927
commit 7ec0af147d
5 changed files with 39 additions and 16 deletions

View File

@ -37,7 +37,7 @@ void CryDir::stat(struct ::stat *result) const {
unique_ptr<fspp::File> CryDir::createFile(const string &name, mode_t mode) { unique_ptr<fspp::File> CryDir::createFile(const string &name, mode_t mode) {
auto child = _device->CreateBlob(); auto child = _device->CreateBlob();
_blob->AddChild(name, child->key()); _blob->AddChildFile(name, child->key());
//TODO Do we need a return value in createDir for fspp? If not, change fspp! //TODO Do we need a return value in createDir for fspp? If not, change fspp!
auto fileblob = make_unique<FileBlob>(std::move(child)); auto fileblob = make_unique<FileBlob>(std::move(child));
fileblob->InitializeEmptyFile(); fileblob->InitializeEmptyFile();
@ -46,7 +46,7 @@ unique_ptr<fspp::File> CryDir::createFile(const string &name, mode_t mode) {
unique_ptr<fspp::Dir> CryDir::createDir(const string &name, mode_t mode) { unique_ptr<fspp::Dir> CryDir::createDir(const string &name, mode_t mode) {
auto child = _device->CreateBlob(); auto child = _device->CreateBlob();
_blob->AddChild(name, child->key()); _blob->AddChildDir(name, child->key());
//TODO I don't think we need a return value in createDir for fspp. Change fspp! //TODO I don't think we need a return value in createDir for fspp. Change fspp!
auto dirblob = make_unique<DirBlob>(std::move(child)); auto dirblob = make_unique<DirBlob>(std::move(child));
dirblob->InitializeEmptyDir(); dirblob->InitializeEmptyDir();
@ -57,7 +57,7 @@ void CryDir::rmdir() {
throw FuseErrnoException(ENOTSUP); throw FuseErrnoException(ENOTSUP);
} }
unique_ptr<vector<string>> CryDir::children() const { unique_ptr<vector<fspp::Dir::Entry>> CryDir::children() const {
return _blob->GetChildren(); return _blob->GetChildren();
} }

View File

@ -19,7 +19,8 @@ public:
std::unique_ptr<fspp::Dir> createDir(const std::string &name, mode_t mode) override; std::unique_ptr<fspp::Dir> createDir(const std::string &name, mode_t mode) override;
void rmdir() override; void rmdir() override;
std::unique_ptr<std::vector<std::string>> children() const override; //TODO Make Entry a public class instead of hidden in DirBlob (which is not publicly visible)
std::unique_ptr<std::vector<fspp::Dir::Entry>> children() const override;
private: private:
CryDevice *_device; CryDevice *_device;

View File

@ -1,9 +1,10 @@
# Biicode configuration file # Biicode configuration file
[requirements] [requirements]
messmer/blockstore: 1 messmer/blobstore
messmer/blockstore
messmer/cpp-utils: 2 messmer/cpp-utils: 2
messmer/fspp: 0 messmer/fspp
[parent] [parent]
messmer/cryfs: 0 messmer/cryfs: 0

View File

@ -47,11 +47,11 @@ bool DirBlob::IsDir(const blobstore::Blob &blob) {
return magicNumber(blob) == MagicNumbers::DIR; return magicNumber(blob) == MagicNumbers::DIR;
} }
unique_ptr<vector<string>> DirBlob::GetChildren() const { unique_ptr<vector<fspp::Dir::Entry>> DirBlob::GetChildren() const {
Data entries(_blob->size()-1); Data entries(_blob->size()-1);
_blob->read(entries.data(), 1, _blob->size()-1); _blob->read(entries.data(), 1, _blob->size()-1);
auto result = make_unique<vector<string>>(); auto result = make_unique<vector<fspp::Dir::Entry>>();
const char *pos = (const char*)entries.data(); const char *pos = (const char*)entries.data();
while(pos < (const char*)entries.data()+entries.size()) { while(pos < (const char*)entries.data()+entries.size()) {
@ -61,24 +61,40 @@ unique_ptr<vector<string>> DirBlob::GetChildren() const {
return result; return result;
} }
const char *DirBlob::readAndAddNextChild(const char *pos, vector<string> *result) const { 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;
size_t length = strlen(pos); size_t length = strlen(pos);
result->emplace_back(pos, length); std::string name(pos, length);
result->emplace_back(fspp::Dir::Entry(type, name));
const char *posAfterName = pos + length + 1; const char *posAfterName = pos + length + 1;
const char *posAfterKey = posAfterName + strlen(posAfterName) + 1; const char *posAfterKey = posAfterName + strlen(posAfterName) + 1;
return posAfterKey; return posAfterKey;
} }
void DirBlob::AddChild(const std::string &name, const Key &blobKey) { 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) {
//TODO blob.resize(blob.size()+X) has to traverse tree twice. Better would be blob.addSize(X) which returns old size //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(); uint64_t oldBlobSize = _blob->size();
string blobKeyStr = blobKey.ToString(); string blobKeyStr = blobKey.ToString();
_blob->resize(oldBlobSize + name.size() + 1 + blobKeyStr.size() + 1); _blob->resize(oldBlobSize + name.size() + 1 + blobKeyStr.size() + 1);
//Write entry type
unsigned char entryTypeMagicNumber = static_cast<unsigned char>(entryType);
_blob->write(&entryTypeMagicNumber, oldBlobSize, 1);
//Write entry name inclusive null terminator //Write entry name inclusive null terminator
_blob->write(name.c_str(), oldBlobSize, name.size()+1); _blob->write(name.c_str(), oldBlobSize + 1, name.size()+1);
//Write blob key inclusive null terminator //Write blob key inclusive null terminator
_blob->write(blobKeyStr.c_str(), oldBlobSize + name.size() + 1, blobKeyStr.size()+1); _blob->write(blobKeyStr.c_str(), oldBlobSize + 1 + name.size() + 1, blobKeyStr.size()+1);
} }
Key DirBlob::GetBlobKeyForName(const string &name) const { Key DirBlob::GetBlobKeyForName(const string &name) const {
@ -89,6 +105,7 @@ Key DirBlob::GetBlobKeyForName(const string &name) const {
const char *pos = (const char*)entries.data(); const char *pos = (const char*)entries.data();
while(pos < (const char*)entries.data()+entries.size()) { while(pos < (const char*)entries.data()+entries.size()) {
pos += 1; // Skip entry type magic number (whether it is a dir or a file)
size_t name_length = strlen(pos); size_t name_length = strlen(pos);
if (name_length == name.size() && 0==std::memcmp(pos, name.c_str(), name_length)) { if (name_length == name.size() && 0==std::memcmp(pos, name.c_str(), name_length)) {
pos += strlen(pos) + 1; // Skip name pos += strlen(pos) + 1; // Skip name

View File

@ -5,6 +5,7 @@
#include <messmer/blobstore/interface/Blob.h> #include <messmer/blobstore/interface/Blob.h>
#include <messmer/blockstore/utils/Key.h> #include <messmer/blockstore/utils/Key.h>
#include "messmer/cpp-utils/macros.h" #include "messmer/cpp-utils/macros.h"
#include <messmer/fspp/fs_interface/Dir.h>
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -17,8 +18,9 @@ public:
virtual ~DirBlob(); virtual ~DirBlob();
void InitializeEmptyDir(); void InitializeEmptyDir();
std::unique_ptr<std::vector<std::string>> GetChildren() const; std::unique_ptr<std::vector<fspp::Dir::Entry>> GetChildren() const;
void AddChild(const std::string &name, const blockstore::Key &blobKey); void AddChildDir(const std::string &name, const blockstore::Key &blobKey);
void AddChildFile(const std::string &name, const blockstore::Key &blobKey);
blockstore::Key GetBlobKeyForName(const std::string &name) const; blockstore::Key GetBlobKeyForName(const std::string &name) const;
static bool IsDir(const blobstore::Blob &blob); static bool IsDir(const blobstore::Blob &blob);
@ -27,7 +29,9 @@ private:
unsigned char magicNumber() const; unsigned char magicNumber() const;
static const unsigned char magicNumber(const blobstore::Blob &blob); static const unsigned char magicNumber(const blobstore::Blob &blob);
const char *readAndAddNextChild(const char *pos, std::vector<std::string> *result) const; void AddChild(const std::string &name, const blockstore::Key &blobKey, fspp::Dir::EntryType type);
const char *readAndAddNextChild(const char *pos, std::vector<fspp::Dir::Entry> *result) const;
std::unique_ptr<blobstore::Blob> _blob; std::unique_ptr<blobstore::Blob> _blob;