Releasing the former Stadia file transfer tools

The tools allow efficient and fast synchronization of large directory
trees from a Windows workstation to a Linux target machine.

cdc_rsync* support efficient copy of files by using content-defined
chunking (CDC) to identify chunks within files that can be reused.

asset_stream_manager + cdc_fuse_fs support efficient streaming of a
local directory to a remote virtual file system based on FUSE. It also
employs CDC to identify and reuse unchanged data chunks.
This commit is contained in:
Christian Schneider
2022-10-07 10:47:04 +02:00
commit 4326e972ac
364 changed files with 49410 additions and 0 deletions

99
manifest/content_id.cc Normal file
View File

@@ -0,0 +1,99 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "manifest/content_id.h"
#include "blake3.h"
namespace cdc_ft {
namespace {
// Converts |n| in the range 0..15 to its lower-case hex representation.
// Returns -1 if |n| is not in the range 0..15.
char IntToHex(uint8_t n) {
if (n <= 9) return '0' + n;
if (n <= 15) return 'a' + n - 10;
return -1;
}
// Converts the lower-case hex character |c| to its integer representation.
// Returns -1 if |c| is not a valid lower-case hex character.
int HexToInt(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
return -1;
}
} // namespace
// static
ContentIdProto ContentId::FromDataString(const std::string& data) {
return FromArray(data.c_str(), data.size());
}
// static
ContentIdProto ContentId::FromDataString(absl::string_view data) {
return FromArray(data.data(), data.size());
}
// static
ContentIdProto ContentId::FromArray(const void* data, size_t len) {
blake3_hasher state;
uint8_t out[kHashSize];
blake3_hasher_init(&state);
blake3_hasher_update(&state, data, len);
blake3_hasher_finalize(&state, out, kHashSize);
ContentIdProto content_id;
content_id.set_blake3_sum_160(out, kHashSize);
return content_id;
}
// static
std::string ContentId::ToHexString(const ContentIdProto& content_id) {
absl::string_view blake3_sum(content_id.blake3_sum_160());
std::string ret;
ret.reserve(blake3_sum.size() << 1);
for (size_t i = 0; i < blake3_sum.size(); ++i) {
ret.push_back(IntToHex(static_cast<uint8_t>(blake3_sum[i]) >> 4));
ret.push_back(IntToHex(static_cast<uint8_t>(blake3_sum[i]) & 0xf));
}
return ret;
}
// static
bool ContentId::FromHexString(const std::string& str,
ContentIdProto* content_id) {
if (str.size() != kHashSize * 2) return false;
std::string* hash = content_id->mutable_blake3_sum_160();
hash->clear();
hash->reserve(kHashSize);
for (int n = 0; n < str.size(); n += 2) {
int high = HexToInt(str[n]);
int low = HexToInt(str[n + 1]);
if (high == -1 || low == -1) {
hash->clear();
return false;
}
hash->push_back((high << 4) + low);
}
return true;
}
// static
uint8_t ContentId::GetByte(const ContentIdProto& content_id, size_t pos) {
if (pos >= content_id.blake3_sum_160().size()) return 0;
return content_id.blake3_sum_160()[pos];
}
} // namespace cdc_ft