mirror of
https://github.com/facebook/sapling.git
synced 2024-10-09 08:18:15 +03:00
49e49f9fc2
Summary: folly:format is deprecated in lieu of fmt and std::format. Migrate most of EdenFS to fmt instead. Differential Revision: D31025948 fbshipit-source-id: 82ed674d5e255ac129995b56bc8b9731a5fbf82e
67 lines
1.7 KiB
C++
67 lines
1.7 KiB
C++
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This software may be used and distributed according to the terms of the
|
|
* GNU General Public License version 2.
|
|
*/
|
|
|
|
#include "SerializedBlobMetadata.h"
|
|
|
|
#include <folly/Format.h>
|
|
#include <folly/Range.h>
|
|
#include <folly/lang/Bits.h>
|
|
|
|
#include <eden/fs/model/Hash.h>
|
|
#include <eden/fs/store/BlobMetadata.h>
|
|
|
|
namespace facebook::eden {
|
|
|
|
SerializedBlobMetadata::SerializedBlobMetadata(const BlobMetadata& metadata) {
|
|
serialize(metadata.sha1, metadata.size);
|
|
}
|
|
|
|
SerializedBlobMetadata::SerializedBlobMetadata(
|
|
const Hash& contentsHash,
|
|
uint64_t blobSize) {
|
|
serialize(contentsHash, blobSize);
|
|
}
|
|
|
|
folly::ByteRange SerializedBlobMetadata::slice() const {
|
|
return folly::ByteRange{data_};
|
|
}
|
|
|
|
BlobMetadata SerializedBlobMetadata::parse(
|
|
Hash blobID,
|
|
const StoreResult& result) {
|
|
auto bytes = result.bytes();
|
|
if (bytes.size() != SIZE) {
|
|
throw std::invalid_argument(fmt::format(
|
|
"Blob metadata for {} had unexpected size {}. Could not deserialize.",
|
|
blobID.toString(),
|
|
bytes.size()));
|
|
}
|
|
|
|
return unslice(bytes);
|
|
}
|
|
|
|
BlobMetadata SerializedBlobMetadata::unslice(folly::ByteRange bytes) {
|
|
uint64_t blobSizeBE;
|
|
memcpy(&blobSizeBE, bytes.data(), sizeof(uint64_t));
|
|
bytes.advance(sizeof(uint64_t));
|
|
auto contentsHash = Hash{bytes};
|
|
return BlobMetadata{contentsHash, folly::Endian::big(blobSizeBE)};
|
|
}
|
|
|
|
void SerializedBlobMetadata::serialize(
|
|
const Hash& contentsHash,
|
|
uint64_t blobSize) {
|
|
uint64_t blobSizeBE = folly::Endian::big(blobSize);
|
|
memcpy(data_.data(), &blobSizeBE, sizeof(uint64_t));
|
|
memcpy(
|
|
data_.data() + sizeof(uint64_t),
|
|
contentsHash.getBytes().data(),
|
|
Hash::RAW_SIZE);
|
|
}
|
|
|
|
} // namespace facebook::eden
|