Files
UnrealEngineUWP/Engine/Source/Programs/Unsync/Private/UnsyncCmdHash.cpp
Yuriy ODonnell b5709042fb Import Unsync into the main source tree
This is a binary patching and incremental downloading tool, similar to rsync or zsync. It aims to improve the large binary download processes that previously were served by robocopy (i.e. full packages produced by the build farm).

The original code can be found in `//depot/usr/yuriy.odonnell/unsync`. This commit is a branch from the original location to preserve history.

While the codebase is designed to be self-contained and does not depend on any engine libraries, it mostly follows the UE coding guidelines and can be built with UBT.

Currently only Windows is supported, however the tool is expected to also work on Mac and Linux in the future.

#rb Martin.Ridgers
#preflight skip

[CL 18993571 by Yuriy ODonnell in ue5-main branch]
2022-02-15 04:30:27 -05:00

103 lines
2.8 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "UnsyncCmdHash.h"
#include "UnsyncCore.h"
#include "UnsyncFile.h"
#include "UnsyncSerialization.h"
namespace unsync {
int32 // TODO: return a TResult
CmdHash(const FCmdHashOptions& Options)
{
if (unsync::bDirectory(Options.Input))
{
UNSYNC_VERBOSE(L"'%ls' is a directory", Options.Input.wstring().c_str());
fs::path InputRoot = Options.Input;
fs::path ManifestRoot = InputRoot / ".unsync";
fs::path DirectoryManifestPath;
if (Options.Output.empty())
{
DirectoryManifestPath = ManifestRoot / "manifest.bin";
bool bDirectoryExists =
(fs::exists(ManifestRoot) && fs::is_directory(ManifestRoot)) || (GDryRun || fs::create_directories(ManifestRoot));
if (!bDirectoryExists && !GDryRun)
{
UNSYNC_ERROR(L"Failed to initialize manifest directory '%ls'", ManifestRoot.wstring().c_str());
return 1;
}
}
else
{
DirectoryManifestPath = Options.Output;
}
FDirectoryManifest DirectoryManifest;
if (Options.bForce)
{
DirectoryManifest = CreateDirectoryManifest(Options.Input, Options.BlockSize, Options.Algorithm);
}
else if (Options.bIncremental)
{
UNSYNC_VERBOSE(L"Performing incremental directory manifest generation");
DirectoryManifest = CreateDirectoryManifestIncremental(Options.Input, Options.BlockSize, Options.Algorithm);
}
else
{
LoadOrCreateDirectoryManifest(DirectoryManifest, Options.Input, Options.BlockSize, Options.Algorithm);
}
if (!GDryRun)
{
SaveDirectoryManifest(DirectoryManifest, DirectoryManifestPath);
}
}
else
{
UNSYNC_VERBOSE(L"'%ls' is a file", Options.Input.wstring().c_str());
NativeFile OverlappedFile(Options.Input);
if (OverlappedFile.IsValid())
{
UNSYNC_VERBOSE(L"Computing blocks for '%ls' (%.2f MB)", Options.Input.wstring().c_str(), SizeMb(OverlappedFile.GetSize()));
FGenericBlockArray GenericBlocks = ComputeBlocks(OverlappedFile, Options.BlockSize, Options.Algorithm);
fs::path OutputFilename = Options.Output;
if (OutputFilename.empty())
{
OutputFilename = Options.Input.wstring() + std::wstring(L".unsync");
}
if (!GDryRun)
{
UNSYNC_VERBOSE(L"Saving blocks to '%ls'", OutputFilename.wstring().c_str());
std::vector<FBlock128> Blocks128;
Blocks128.reserve(GenericBlocks.size()); // #wip-widehash
for (const auto& It : GenericBlocks)
{
FBlock128 Block;
Block.HashStrong = It.HashStrong.ToHash128();
Block.HashWeak = It.HashWeak;
Block.Offset = It.Offset;
Block.Size = It.Size;
Blocks128.push_back(Block);
}
SaveBlocks(Blocks128, Options.BlockSize, OutputFilename) ? 0 : 1;
}
}
else
{
UNSYNC_ERROR(L"Failed to open file '%ls'", Options.Input.wstring().c_str());
return 1;
}
}
return 0;
}
} // namespace unsync