sapling/eden/fs/fuse/DirList.cpp
Chad Austin 1c84f3f115 split InodeNumber into its own file
Summary:
It's common for code to use InodeNumber without needing to include the
main FUSE headers or vice versa. Split them into two separate headers.

Reviewed By: strager

Differential Revision: D13979868

fbshipit-source-id: c5eeb6a3697bb538729a403434dc4f0f7408cda0
2019-02-08 16:21:35 -08:00

70 lines
2.0 KiB
C++

/*
* Copyright (c) 2016-present, Facebook, Inc.
* 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.
*
*/
#include "eden/fs/fuse/DirList.h"
#include "eden/fs/fuse/FuseTypes.h"
#include "eden/fs/fuse/InodeNumber.h"
using folly::StringPiece;
namespace facebook {
namespace eden {
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) {
const size_t avail = end_ - cur_;
const auto entLength = FUSE_NAME_OFFSET + name.size();
const auto fullSize = FUSE_DIRENT_ALIGN(entLength);
if (fullSize > avail) {
return false;
}
fuse_dirent* const dirent = reinterpret_cast<fuse_dirent*>(cur_);
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());
}
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;
}
} // namespace eden
} // namespace facebook