sapling/eden/fs/model/TreeEntry.h

156 lines
3.6 KiB
C
Raw Normal View History

/*
* 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.
*/
#pragma once
#include "eden/fs/model/Hash.h"
#include "eden/fs/utils/DirType.h"
#include "eden/fs/utils/PathFuncs.h"
#include <folly/String.h>
#include <folly/io/Cursor.h>
#include <folly/io/IOBuf.h>
#include <iosfwd>
#include <optional>
namespace facebook::eden {
/**
* Represents the allowed types of entries in version control trees.
*
* Currently missing from this list: git submodules.
*/
enum class TreeEntryType : uint8_t {
TREE,
REGULAR_FILE,
EXECUTABLE_FILE,
SYMLINK,
};
/**
* Computes an initial mode_t, including permission bits, from a FileType.
*/
mode_t modeFromTreeEntryType(TreeEntryType ft);
/**
* Converts an arbitrary mode_t to the appropriate TreeEntryType if the file
* can be tracked by version control. If not, returns std::nullopt.
*/
std::optional<TreeEntryType> treeEntryTypeFromMode(mode_t mode);
class TreeEntry {
public:
separate out ObjectId [proxy hash removal 1/n] Summary: The goal of this stack is to remove Proxy Hash type, but to achieve that we need first to address some tech debt in Eden codebase. For the long time EdenFs had single Hash type that was used for many different use cases. One of major uses for Hash type is identifies internal EdenFs objects such as blobs, trees, and others. We seem to reach agreement that we need a different type for those identifiers, so we introduce separate ObjectId type in this diff to denote new identifier type and replace _some_ usage of Hash with ObjectId. We still retain original Hash type for other use cases. Roughly speaking, this is how this diff separates between Hash and ObjectId: **ObjectId**: * Everything that is stored in local store(blobs, trees, commits) **Hash20**: * Explicit hashes(Sha1 of the blob) * Hg identifiers: manifest id and blob hg ig For now, in this diff ObjectId has exactly same content as Hash, but this will change in the future diffs. Doing this way allows to keep diff size manageable, while migrating to new ObjectId right away would produce insanely large diff that would be both hard to make and review. There are few more things that needs to be done before we can get to the meat of removing proxy hashes: 1) Replace include Hash.h with ObjectId.h where needed 2) Remove Hash type, explicitly rename rest of Hash usages to Hash20 3) Modify content of ObjectId to support new use cases 4) Modify serialized metadata and possibly other places that assume ObjectId size is fixed and equal to Hash20 size Reviewed By: chadaustin Differential Revision: D31316477 fbshipit-source-id: 0d5e4460a461bcaac6b9fd884517e129aeaf4baf
2021-10-01 20:24:21 +03:00
explicit TreeEntry(
const ObjectId& hash,
PathComponent name,
TreeEntryType type)
: type_(type), hash_(hash), name_(std::move(name)) {}
explicit TreeEntry(
separate out ObjectId [proxy hash removal 1/n] Summary: The goal of this stack is to remove Proxy Hash type, but to achieve that we need first to address some tech debt in Eden codebase. For the long time EdenFs had single Hash type that was used for many different use cases. One of major uses for Hash type is identifies internal EdenFs objects such as blobs, trees, and others. We seem to reach agreement that we need a different type for those identifiers, so we introduce separate ObjectId type in this diff to denote new identifier type and replace _some_ usage of Hash with ObjectId. We still retain original Hash type for other use cases. Roughly speaking, this is how this diff separates between Hash and ObjectId: **ObjectId**: * Everything that is stored in local store(blobs, trees, commits) **Hash20**: * Explicit hashes(Sha1 of the blob) * Hg identifiers: manifest id and blob hg ig For now, in this diff ObjectId has exactly same content as Hash, but this will change in the future diffs. Doing this way allows to keep diff size manageable, while migrating to new ObjectId right away would produce insanely large diff that would be both hard to make and review. There are few more things that needs to be done before we can get to the meat of removing proxy hashes: 1) Replace include Hash.h with ObjectId.h where needed 2) Remove Hash type, explicitly rename rest of Hash usages to Hash20 3) Modify content of ObjectId to support new use cases 4) Modify serialized metadata and possibly other places that assume ObjectId size is fixed and equal to Hash20 size Reviewed By: chadaustin Differential Revision: D31316477 fbshipit-source-id: 0d5e4460a461bcaac6b9fd884517e129aeaf4baf
2021-10-01 20:24:21 +03:00
const ObjectId& hash,
PathComponent name,
TreeEntryType type,
std::optional<uint64_t> size,
std::optional<Hash20> contentSha1)
: type_(type),
hash_(hash),
name_(std::move(name)),
size_(size),
contentSha1_(contentSha1) {}
separate out ObjectId [proxy hash removal 1/n] Summary: The goal of this stack is to remove Proxy Hash type, but to achieve that we need first to address some tech debt in Eden codebase. For the long time EdenFs had single Hash type that was used for many different use cases. One of major uses for Hash type is identifies internal EdenFs objects such as blobs, trees, and others. We seem to reach agreement that we need a different type for those identifiers, so we introduce separate ObjectId type in this diff to denote new identifier type and replace _some_ usage of Hash with ObjectId. We still retain original Hash type for other use cases. Roughly speaking, this is how this diff separates between Hash and ObjectId: **ObjectId**: * Everything that is stored in local store(blobs, trees, commits) **Hash20**: * Explicit hashes(Sha1 of the blob) * Hg identifiers: manifest id and blob hg ig For now, in this diff ObjectId has exactly same content as Hash, but this will change in the future diffs. Doing this way allows to keep diff size manageable, while migrating to new ObjectId right away would produce insanely large diff that would be both hard to make and review. There are few more things that needs to be done before we can get to the meat of removing proxy hashes: 1) Replace include Hash.h with ObjectId.h where needed 2) Remove Hash type, explicitly rename rest of Hash usages to Hash20 3) Modify content of ObjectId to support new use cases 4) Modify serialized metadata and possibly other places that assume ObjectId size is fixed and equal to Hash20 size Reviewed By: chadaustin Differential Revision: D31316477 fbshipit-source-id: 0d5e4460a461bcaac6b9fd884517e129aeaf4baf
2021-10-01 20:24:21 +03:00
const ObjectId& getHash() const {
return hash_;
}
const PathComponent& getName() const {
return name_;
}
bool isTree() const {
return type_ == TreeEntryType::TREE;
}
TreeEntryType getType() const {
#ifdef _WIN32
// XXX(T66590035): instead of doing this here, this should be done in the
// Windows specific code that interpret these.
switch (type_) {
case TreeEntryType::REGULAR_FILE:
case TreeEntryType::EXECUTABLE_FILE:
case TreeEntryType::SYMLINK:
return TreeEntryType::REGULAR_FILE;
default:
return type_;
}
#else
return type_;
#endif
}
dtype_t getDType() const {
switch (type_) {
case TreeEntryType::TREE:
return dtype_t::Dir;
case TreeEntryType::REGULAR_FILE:
case TreeEntryType::EXECUTABLE_FILE:
return dtype_t::Regular;
#ifndef _WIN32
case TreeEntryType::SYMLINK:
return dtype_t::Symlink;
#endif
default:
return dtype_t::Unknown;
}
}
std::string toLogString() const;
const std::optional<uint64_t>& getSize() const {
return size_;
}
const std::optional<Hash20>& getContentSha1() const {
return contentSha1_;
}
/**
* An estimate of the memory footprint of this treeEntry outside of the data
* directly stored in this object.
*/
size_t getIndirectSizeBytes() const;
/**
* Computes exact serialized size of this entry.
*/
size_t serializedSize() const;
/**
* Serializes entry into appender, consuming exactly serializedSize() bytes.
*/
void serialize(folly::io::Appender& appender) const;
/**
* Deserialize tree entry.
*/
static std::optional<TreeEntry> deserialize(folly::StringPiece& data);
private:
TreeEntryType type_;
separate out ObjectId [proxy hash removal 1/n] Summary: The goal of this stack is to remove Proxy Hash type, but to achieve that we need first to address some tech debt in Eden codebase. For the long time EdenFs had single Hash type that was used for many different use cases. One of major uses for Hash type is identifies internal EdenFs objects such as blobs, trees, and others. We seem to reach agreement that we need a different type for those identifiers, so we introduce separate ObjectId type in this diff to denote new identifier type and replace _some_ usage of Hash with ObjectId. We still retain original Hash type for other use cases. Roughly speaking, this is how this diff separates between Hash and ObjectId: **ObjectId**: * Everything that is stored in local store(blobs, trees, commits) **Hash20**: * Explicit hashes(Sha1 of the blob) * Hg identifiers: manifest id and blob hg ig For now, in this diff ObjectId has exactly same content as Hash, but this will change in the future diffs. Doing this way allows to keep diff size manageable, while migrating to new ObjectId right away would produce insanely large diff that would be both hard to make and review. There are few more things that needs to be done before we can get to the meat of removing proxy hashes: 1) Replace include Hash.h with ObjectId.h where needed 2) Remove Hash type, explicitly rename rest of Hash usages to Hash20 3) Modify content of ObjectId to support new use cases 4) Modify serialized metadata and possibly other places that assume ObjectId size is fixed and equal to Hash20 size Reviewed By: chadaustin Differential Revision: D31316477 fbshipit-source-id: 0d5e4460a461bcaac6b9fd884517e129aeaf4baf
2021-10-01 20:24:21 +03:00
ObjectId hash_;
PathComponent name_;
std::optional<uint64_t> size_;
std::optional<Hash20> contentSha1_;
static constexpr uint64_t NO_SIZE = std::numeric_limits<uint64_t>::max();
};
std::ostream& operator<<(std::ostream& os, TreeEntryType type);
bool operator==(const TreeEntry& entry1, const TreeEntry& entry2);
bool operator!=(const TreeEntry& entry1, const TreeEntry& entry2);
} // namespace facebook::eden