mirror of
https://github.com/facebook/sapling.git
synced 2024-10-06 14:58:03 +03:00
6f60c48ea8
Summary: Requiring that callers ensure that ObjectFetchContext outlives the asynchronous result is a burden and a memory-safety risk. Without Rust's borrow checker, we can't feel confident this is safe under all error and timeout conditions. It also requires we use folly::collectAll and collectAllSafe, which means that an error during fanout has to wait until the fanout completes before we can report the error to our caller. Moreover, tying the ObjectFetchContext's lifetime to the FUSE/NFS/Thrift/etc. request's lifetime gives us incorrect statistics about how long the request takes. I consider this an API design mistake (on my part). Correct the situation by decoupling ObjectFetchContext from RequestContext, and begin reference-counting ObjectFetchContext. This does increase the number of atomic RMW operations, but we can alleviate those where they matter by manually checking *Future::isReady() before calling .then*. Reviewed By: xavierd Differential Revision: D40744706 fbshipit-source-id: 5baf654f6d65790ed0cafb45c0bc7f2aaa8ecec2
80 lines
2.2 KiB
C++
80 lines
2.2 KiB
C++
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This software may be used and distributed according to the terms of the
|
|
* GNU General Public License version 2.
|
|
*/
|
|
|
|
#include "eden/fs/store/PathLoader.h"
|
|
#include <vector>
|
|
#include "eden/fs/model/Tree.h"
|
|
#include "eden/fs/service/gen-cpp2/eden_constants.h"
|
|
#include "eden/fs/store/ObjectStore.h"
|
|
#include "eden/fs/utils/EdenError.h"
|
|
#include "eden/fs/utils/ImmediateFuture.h"
|
|
|
|
namespace facebook::eden {
|
|
|
|
namespace {
|
|
|
|
struct ResolveTreeContext {
|
|
std::vector<PathComponent> components;
|
|
};
|
|
|
|
ImmediateFuture<std::shared_ptr<const Tree>> resolveTree(
|
|
std::shared_ptr<ResolveTreeContext> ctx,
|
|
ObjectStore& objectStore,
|
|
const ObjectFetchContextPtr& fetchContext,
|
|
std::shared_ptr<const Tree> root,
|
|
size_t index) {
|
|
if (index == ctx->components.size()) {
|
|
return std::move(root);
|
|
}
|
|
|
|
auto child = root->find(ctx->components[index]);
|
|
if (child == root->end()) {
|
|
throw newEdenError(
|
|
ENOENT,
|
|
EdenErrorType::POSIX_ERROR,
|
|
"no child with name ",
|
|
ctx->components[index]);
|
|
}
|
|
|
|
if (!child->second.isTree()) {
|
|
throw newEdenError(
|
|
ENOTDIR,
|
|
EdenErrorType::POSIX_ERROR,
|
|
"child is not tree ",
|
|
ctx->components[index]);
|
|
}
|
|
|
|
return objectStore.getTree(child->second.getHash(), fetchContext)
|
|
.thenValue([ctx = std::move(ctx),
|
|
&objectStore,
|
|
fetchContext = fetchContext.copy(),
|
|
index](std::shared_ptr<const Tree>&& tree) mutable {
|
|
return resolveTree(
|
|
ctx, objectStore, fetchContext, std::move(tree), index + 1);
|
|
});
|
|
}
|
|
|
|
} // namespace
|
|
|
|
ImmediateFuture<std::shared_ptr<const Tree>> resolveTree(
|
|
ObjectStore& objectStore,
|
|
const ObjectFetchContextPtr& fetchContext,
|
|
std::shared_ptr<const Tree> root,
|
|
RelativePathPiece path) {
|
|
// Don't do anything fancy with lifetimes and just get this correct as simply
|
|
// as possible. There's room for optimization if it matters.
|
|
auto ctx = std::make_shared<ResolveTreeContext>();
|
|
for (auto c : path.components()) {
|
|
ctx->components.emplace_back(c);
|
|
}
|
|
|
|
return resolveTree(
|
|
std::move(ctx), objectStore, fetchContext, std::move(root), 0);
|
|
}
|
|
|
|
} // namespace facebook::eden
|