2022-02-15 04:30:27 -05:00
|
|
|
// 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)
|
|
|
|
|
{
|
2022-03-08 05:13:41 -05:00
|
|
|
if (unsync::IsDirectory(Options.Input))
|
2022-02-15 04:30:27 -05:00
|
|
|
{
|
|
|
|
|
UNSYNC_VERBOSE(L"'%ls' is a directory", Options.Input.wstring().c_str());
|
|
|
|
|
|
2022-03-08 08:17:08 -05:00
|
|
|
FPath InputRoot = Options.Input;
|
|
|
|
|
FPath ManifestRoot = InputRoot / ".unsync";
|
|
|
|
|
FPath DirectoryManifestPath;
|
2022-02-15 04:30:27 -05:00
|
|
|
|
|
|
|
|
if (Options.Output.empty())
|
|
|
|
|
{
|
|
|
|
|
DirectoryManifestPath = ManifestRoot / "manifest.bin";
|
|
|
|
|
|
2022-03-08 08:17:08 -05:00
|
|
|
bool bDirectoryExists = (PathExists(ManifestRoot) && IsDirectory(ManifestRoot)) || (GDryRun || CreateDirectories(ManifestRoot));
|
2022-02-15 04:30:27 -05:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2022-03-08 08:17:08 -05:00
|
|
|
FPath OutputFilename = Options.Output;
|
2022-02-15 04:30:27 -05:00
|
|
|
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);
|
|
|
|
|
}
|
2022-02-22 02:09:06 -05:00
|
|
|
return SaveBlocks(Blocks128, Options.BlockSize, OutputFilename) ? 0 : 1;
|
2022-02-15 04:30:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UNSYNC_ERROR(L"Failed to open file '%ls'", Options.Input.wstring().c_str());
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace unsync
|