2017-04-04 01:47:53 +03:00
|
|
|
/*
|
2019-06-20 02:58:25 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-04-04 01:47:53 +03:00
|
|
|
*
|
2019-06-20 02:58:25 +03:00
|
|
|
* This software may be used and distributed according to the terms of the
|
|
|
|
* GNU General Public License version 2.
|
2017-04-04 01:47:53 +03:00
|
|
|
*/
|
2019-10-11 15:26:59 +03:00
|
|
|
|
2017-04-04 01:47:53 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <folly/Range.h>
|
2018-10-24 03:03:12 +03:00
|
|
|
#include <optional>
|
2017-04-04 01:47:53 +03:00
|
|
|
#include <string>
|
2018-03-21 02:34:01 +03:00
|
|
|
|
2017-04-04 01:47:53 +03:00
|
|
|
#include "eden/fs/model/Hash.h"
|
2021-06-03 21:05:25 +03:00
|
|
|
#include "eden/fs/model/RootId.h"
|
2019-10-29 19:20:57 +03:00
|
|
|
#include "eden/fs/service/gen-cpp2/eden_types.h"
|
2021-02-24 06:56:18 +03:00
|
|
|
#include "eden/fs/utils/EdenError.h"
|
2021-06-08 03:24:45 +03:00
|
|
|
#include "folly/String.h"
|
2017-04-04 01:47:53 +03:00
|
|
|
|
2021-06-03 21:05:25 +03:00
|
|
|
namespace facebook::eden {
|
2017-04-04 01:47:53 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a Hash to a std::string to be returned via thrift as a thrift
|
|
|
|
* BinaryHash data type.
|
|
|
|
*/
|
|
|
|
inline std::string thriftHash(const Hash& hash) {
|
|
|
|
return folly::StringPiece{hash.getBytes()}.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-24 03:03:12 +03:00
|
|
|
* Convert an optional<Hash> to a std::string to be returned via thrift
|
2017-04-04 01:47:53 +03:00
|
|
|
* as a thrift BinaryHash data type.
|
|
|
|
*/
|
2018-10-24 03:03:12 +03:00
|
|
|
inline std::string thriftHash(const std::optional<Hash>& hash) {
|
|
|
|
if (hash.has_value()) {
|
|
|
|
return thriftHash(hash.value());
|
|
|
|
}
|
|
|
|
return std::string{};
|
|
|
|
}
|
|
|
|
|
2017-04-07 03:44:05 +03:00
|
|
|
/**
|
2018-03-21 02:34:01 +03:00
|
|
|
* Convert thrift BinaryHash data type into a Hash object.
|
|
|
|
*
|
|
|
|
* This allows the input to be either a 20-byte binary string, or a 40-byte
|
|
|
|
* hexadecimal string.
|
2017-04-07 03:44:05 +03:00
|
|
|
*/
|
2021-06-03 21:05:25 +03:00
|
|
|
inline Hash hashFromThrift(folly::StringPiece commitID) {
|
2018-03-21 02:34:01 +03:00
|
|
|
if (commitID.size() == Hash::RAW_SIZE) {
|
|
|
|
// This looks like 20 bytes of binary data.
|
|
|
|
return Hash(folly::ByteRange(folly::StringPiece(commitID)));
|
|
|
|
} else if (commitID.size() == 2 * Hash::RAW_SIZE) {
|
|
|
|
// This looks like 40 bytes of hexadecimal data.
|
|
|
|
return Hash(commitID);
|
|
|
|
} else {
|
|
|
|
throw newEdenError(
|
2019-10-29 19:20:57 +03:00
|
|
|
EINVAL,
|
|
|
|
EdenErrorType::ARGUMENT_ERROR,
|
2018-03-21 02:34:01 +03:00
|
|
|
"expected argument to be a 20-byte binary hash or "
|
2019-08-02 02:48:01 +03:00
|
|
|
"40-byte hexadecimal hash; got \"",
|
|
|
|
commitID,
|
|
|
|
"\"");
|
2018-03-21 02:34:01 +03:00
|
|
|
}
|
2017-04-07 03:44:05 +03:00
|
|
|
}
|
2021-06-03 21:05:25 +03:00
|
|
|
|
|
|
|
class HashRootIdCodec : public RootIdCodec {
|
|
|
|
public:
|
2021-06-08 03:24:45 +03:00
|
|
|
RootId parseRootId(folly::StringPiece piece) override {
|
|
|
|
return RootId{hashFromThrift(piece).toString()};
|
2021-06-03 21:05:25 +03:00
|
|
|
}
|
2021-06-08 03:24:45 +03:00
|
|
|
std::string renderRootId(const RootId& rootId) override {
|
|
|
|
return folly::unhexlify(rootId.value());
|
2021-06-03 21:05:25 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace facebook::eden
|