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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "DirList.h"
|
2016-08-25 03:01:41 +03:00
|
|
|
|
|
|
|
#include <linux/fuse.h>
|
2016-05-12 23:43:17 +03:00
|
|
|
#include "fuse_headers.h"
|
|
|
|
|
2016-08-25 03:01:41 +03:00
|
|
|
using folly::StringPiece;
|
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
namespace facebook {
|
|
|
|
namespace eden {
|
|
|
|
namespace fusell {
|
|
|
|
|
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) {
|
|
|
|
// The libfuse APIs unfortunately only accept null terminated strings,
|
|
|
|
// so we manually add the fuse_dirent object here rather than using
|
|
|
|
// fuse_add_direntry().
|
2016-05-12 23:43:17 +03:00
|
|
|
|
|
|
|
size_t avail = end_ - cur_;
|
2016-08-25 03:01:41 +03:00
|
|
|
auto entLength = FUSE_NAME_OFFSET + name.size();
|
|
|
|
auto fullSize = FUSE_DIRENT_ALIGN(entLength);
|
|
|
|
if (fullSize > avail) {
|
|
|
|
return false;
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
2016-08-25 03:01:41 +03:00
|
|
|
|
|
|
|
fuse_dirent* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DirList::add(StringPiece name, const struct stat& st, off_t off) {
|
|
|
|
return add(name, st.st_ino, mode_to_dtype(st.st_mode), off);
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
2016-08-25 03:01:41 +03:00
|
|
|
StringPiece DirList::getBuf() const {
|
|
|
|
return StringPiece(buf_.get(), cur_ - buf_.get());
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|