Return error if trying to add an already existing entry to a directory

This commit is contained in:
Sebastian Meßmer 2015-03-18 02:11:10 +01:00
parent c47095474c
commit 4d113613d9
2 changed files with 16 additions and 0 deletions

View File

@ -57,6 +57,17 @@ unique_ptr<vector<fspp::Dir::Entry>> DirBlob::GetChildren() const {
return result; return result;
} }
bool DirBlob::hasChild(const string &name) const {
//TODO Faster implementation without creating children array possible
auto children = GetChildren();
for (const auto &child : *children) {
if (child.name == name) {
return true;
}
}
return false;
}
const char *DirBlob::readAndAddNextChild(const char *pos, vector<fspp::Dir::Entry> *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) // 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)); fspp::Dir::EntryType type = static_cast<fspp::Dir::EntryType>(*reinterpret_cast<const unsigned char*>(pos));
@ -79,6 +90,10 @@ void DirBlob::AddChildFile(const std::string &name, const Key &blobKey) {
} }
void DirBlob::AddChild(const std::string &name, const Key &blobKey, fspp::Dir::EntryType entryType) { void DirBlob::AddChild(const std::string &name, const Key &blobKey, fspp::Dir::EntryType entryType) {
if (hasChild(name)) {
throw fspp::fuse::FuseErrnoException(EEXIST);
}
//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();

View File

@ -32,6 +32,7 @@ private:
const char *readAndAddNextChild(const char *pos, std::vector<fspp::Dir::Entry> *result) const; const char *readAndAddNextChild(const char *pos, std::vector<fspp::Dir::Entry> *result) const;
const char *getStartingPosOfEntry(const char *pos, const std::string &name) const; const char *getStartingPosOfEntry(const char *pos, const std::string &name) const;
bool hasChild(const std::string &name) const;
std::unique_ptr<blobstore::Blob> _blob; std::unique_ptr<blobstore::Blob> _blob;