2016-05-12 23:43:17 +03:00
|
|
|
/*
|
2017-01-21 09:02:33 +03:00
|
|
|
* Copyright (c) 2016-present, Facebook, Inc.
|
2016-05-12 23:43:17 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2018-03-20 03:01:15 +03:00
|
|
|
#include "eden/fs/fuse/DirList.h"
|
2016-08-25 03:01:41 +03:00
|
|
|
|
2018-01-03 03:25:03 +03:00
|
|
|
#include "eden/fs/fuse/FuseTypes.h"
|
2019-02-09 01:57:33 +03:00
|
|
|
#include "eden/fs/fuse/InodeNumber.h"
|
2016-05-12 23:43:17 +03:00
|
|
|
|
2016-08-25 03:01:41 +03:00
|
|
|
using folly::StringPiece;
|
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
namespace facebook {
|
|
|
|
namespace eden {
|
|
|
|
|
2016-08-25 03:01:41 +03:00
|
|
|
DirList::DirList(size_t maxSize)
|
|
|
|
: buf_(new char[maxSize]), end_(buf_.get() + maxSize), cur_(buf_.get()) {}
|
|
|
|
|
|
|
|
bool DirList::add(StringPiece name, ino_t inode, dtype_t type, off_t off) {
|
2018-01-18 00:29:59 +03:00
|
|
|
const size_t avail = end_ - cur_;
|
|
|
|
const auto entLength = FUSE_NAME_OFFSET + name.size();
|
|
|
|
const auto fullSize = FUSE_DIRENT_ALIGN(entLength);
|
2016-08-25 03:01:41 +03:00
|
|
|
if (fullSize > avail) {
|
|
|
|
return false;
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
2016-08-25 03:01:41 +03:00
|
|
|
|
2018-01-18 00:29:59 +03:00
|
|
|
fuse_dirent* const dirent = reinterpret_cast<fuse_dirent*>(cur_);
|
2016-08-25 03:01:41 +03:00
|
|
|
dirent->ino = inode;
|
|
|
|
dirent->off = off;
|
|
|
|
dirent->namelen = name.size();
|
|
|
|
dirent->type = static_cast<decltype(dirent->type)>(type);
|
|
|
|
memcpy(dirent->name, name.data(), name.size());
|
|
|
|
if (fullSize > entLength) {
|
|
|
|
// 0 out any padding
|
|
|
|
memset(cur_ + entLength, 0, fullSize - entLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_ += fullSize;
|
|
|
|
DCHECK_LE(cur_, end_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringPiece DirList::getBuf() const {
|
|
|
|
return StringPiece(buf_.get(), cur_ - buf_.get());
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
2018-03-20 03:01:15 +03:00
|
|
|
|
2018-12-05 12:32:48 +03:00
|
|
|
std::vector<DirList::ExtractedEntry> DirList::extract() const {
|
|
|
|
std::vector<DirList::ExtractedEntry> result;
|
|
|
|
|
|
|
|
char* p = buf_.get();
|
|
|
|
while (p != cur_) {
|
|
|
|
auto entry = reinterpret_cast<fuse_dirent*>(p);
|
|
|
|
result.emplace_back(
|
|
|
|
ExtractedEntry{std::string{entry->name, entry->name + entry->namelen},
|
|
|
|
entry->ino,
|
|
|
|
static_cast<dtype_t>(entry->type),
|
|
|
|
static_cast<off_t>(entry->off)});
|
|
|
|
|
|
|
|
p += FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + entry->namelen);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-11-04 01:58:04 +03:00
|
|
|
} // namespace eden
|
|
|
|
} // namespace facebook
|