Skip to content

Commit

Permalink
checksum tool
Browse files Browse the repository at this point in the history
  • Loading branch information
XUranus committed Dec 12, 2023
1 parent afdfbe2 commit 93598dc
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 66 deletions.
56 changes: 2 additions & 54 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,58 +177,6 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_subdirectory("test")
endif()

# build executable tools
add_executable (vbackup
"cli/vbackup.cpp"
"cli/GetOption.cpp"
)

add_executable (vtools
"cli/vtools.cpp"
"cli/GetOption.cpp"
)

set_property(TARGET vbackup PROPERTY CXX_STANDARD 11)
set_property(TARGET vtools PROPERTY CXX_STANDARD 11)

# link vbackup executable
if(${CMAKE_HOST_WIN32})
target_link_libraries(
vbackup
volumebackup_static
# third part dependency provided by XUranus
minijson_static
minilogger_static
)
else()
# require linux-utils
target_link_libraries(
vbackup
volumebackup
pthread
# third part dependency provided by XUranus
minijson_static
minilogger_static
)
endif()
# build executable cli tools
add_subdirectory("cli")

# build vtools executable
target_link_libraries(
vtools
)

# build vcopymount executable
add_executable (vcopymount
"cli/vcopymount.cpp"
"cli/GetOption.cpp"
)

set_property(TARGET vcopymount PROPERTY CXX_STANDARD 11)

target_link_libraries(
vcopymount
volumebackup_static
# third part dependency provided by XUranus
minijson_static
minilogger_static
)
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ clone this repository and it's dependency recusively:
```bash
git clone [email protected]:XUranus/VolumeBackup.git --recursive
```
build library `volumebackup` and executable cli tools `vbackup`,`vcopymount` and `vtools`:
build library `volumebackup` and executable cli tools `vbackup`,`vcopymount` and `vshow`:
```bash
mkdir build && cd build
cmake .. && cmake --build .
Expand All @@ -51,9 +51,9 @@ cmake .. -DJNI_INCLUDE=your_jni_headers_directory_path && cmake --build .

## Cli Tools Usage
`vtool`, `vbackup` and `vcopymount` is provided as cli tools to backup/restore a volume
1. Use **vtools** to list and query volume info
1. Use **vshow** to list and query volume info
```
> vtools --list
> vshow --list
Name: \\?\Volume{a501f5cc-311e-423c-bc58-94a6c1b6b509}\
Path: \\.\HarddiskVolume3
C:\
Expand All @@ -64,7 +64,7 @@ Path: \\.\HarddiskVolume4
Name: \\?\Volume{52ef083b-6ba4-4683-a73a-23a7290139b0}\
Path: \\.\HarddiskVolume1
> vtools --volume=\\.\HarddiskVolume3
> vshow --volume=\\.\HarddiskVolume3
VolumeName: Windows
VolumeSize: 254792433664
VolumeSerialNumber: 3430564453
Expand Down
67 changes: 67 additions & 0 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
add_executable (vbackup
"vbackup.cpp"
"GetOption.cpp"
)

add_executable (vshow
"vshow.cpp"
"GetOption.cpp"
)

add_executable (vchecksum
"vchecksum.cpp"
"GetOption.cpp"
)

set_property(TARGET vbackup PROPERTY CXX_STANDARD 11)
set_property(TARGET vshow PROPERTY CXX_STANDARD 11)

# link vbackup executable
if(${CMAKE_HOST_WIN32})
target_link_libraries(
vbackup
volumebackup_static
# third part dependency provided by XUranus
minijson_static
minilogger_static
)
else()
# require linux-utils
target_link_libraries(
vbackup
volumebackup
pthread
# third part dependency provided by XUranus
minijson_static
minilogger_static
)
endif()

# build vshow executable
target_link_libraries(
vshow
)

# build vcopymount executable
add_executable (vcopymount
"vcopymount.cpp"
"GetOption.cpp"
)

# build vchecksum executable
target_link_libraries(
vchecksum
OpenSSL::SSL
OpenSSL::Crypto
)


set_property(TARGET vcopymount PROPERTY CXX_STANDARD 11)

target_link_libraries(
vcopymount
volumebackup_static
# third part dependency provided by XUranus
minijson_static
minilogger_static
)
2 changes: 1 addition & 1 deletion cli/vbackup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <string>

#include "GetOption.h"
#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"
#include "native/FileSystemAPI.h"
#include "VolumeProtector.h"
#include "Logger.h"
Expand Down
151 changes: 151 additions & 0 deletions cli/vchecksum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* ================================================================
* Copyright (C) 2023 XUranus All rights reserved.
*
* File: vcheck.cpp
* Author: XUranus
* Date: 2023-07-01
* Description: a command line tool to diff volume data checksum
* ==================================================================
*/

#include "GetOption.h"

#include <cctype>
#include <cstdio>
#include <fstream>
#include <ios>
#include <iostream>
#include <cstdint>
#include <ostream>
#include <vector>
#include <string>
#include <cassert>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <openssl/evp.h>

using namespace xuranus::getopt;

namespace {
#ifdef _WIN32
const std::string SEPARATOR = "\\";
#else
const std::string SEPARATOR = "/";
#endif
}

static const char* g_helpMessage =
"vchecksum [options...] util for dump volume data checksum\n"
"[ -v | --volume= ] volume path\n"
"[ -b | --blocksize=] block size to calculate checksum\n"
"[ -o | --output=] output directory\n"
"[ -d | --sha256dump ] dump sha256 checksum to human readable text\n"
"[ -h | --help ] show help\n";

int PrintHelp()
{
::printf("%s\n", g_helpMessage);
return 0;
}

void ComputeSHA256(uint8_t* data, uint32_t len, uint8_t* output, uint32_t outputLen)
{
EVP_MD_CTX *mdctx = nullptr;
const EVP_MD *md = nullptr;
unsigned char mdValue[EVP_MAX_MD_SIZE] = { 0 };
unsigned int mdLen;

if ((md = EVP_get_digestbyname("SHA256")) == nullptr) {
std::cerr << "Unknown message digest SHA256" << std::endl;
return;
}

if ((mdctx = EVP_MD_CTX_new()) == nullptr) {
std::cout << "Memory allocation failed" << std::endl;
return;
}

EVP_DigestInit_ex(mdctx, md, nullptr);
EVP_DigestUpdate(mdctx, data, len);
EVP_DigestFinal_ex(mdctx, mdValue, &mdLen);
assert(mdLen == outputLen);
memcpy(output, mdValue, mdLen);
EVP_MD_CTX_free(mdctx);
return;
}

int ExecDumpVolumeSha256(
const std::string& volumePath,
const std::string& blockSizeString,
const std::string& outputDir)
{
std::string outputFile = outputDir + SEPARATOR + "sha256.checksum.txt";
std::ifstream volumeIn(volumePath, std::ios::binary);
if (!volumeIn.is_open()) {
std::cerr << "failed to open volume for read: " << volumePath << std::endl;
return 1;
}
std::ofstream fileOut(outputFile, std::ios::trunc);
if (!fileOut.is_open()) {
std::cerr << "failed to open checksum file for write: " << outputFile << std::endl;
return 1;
}
uint32_t blockSize = 1024 * 1024 * 4;
uint8_t* dataBuffer = new uint8_t[blockSize];
uint8_t checksumBuffer[32] = { 0 };
memset(dataBuffer, 0, blockSize);
memset(checksumBuffer, 0, 32);
std::cout << "== DUMP SHA256 CHECKSUM ===" << std::endl;
std::cout << "VolumePath: " << volumePath << std::endl;
std::cout << "OutPutFile: " << outputFile << std::endl;
std::cout << "BlockSize: " << blockSize << std::endl;
while (volumeIn.read(reinterpret_cast<char*>(dataBuffer), blockSize)) {
ComputeSHA256(dataBuffer, blockSize, checksumBuffer, 32);
for (int i = 0; i < 32; ++i) {
fileOut << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(checksumBuffer[i]);
}
fileOut << std::endl;
memset(dataBuffer, 0, blockSize);
memset(checksumBuffer, 0, 32);
}
volumeIn.close();
fileOut.close();
delete[] dataBuffer;
return 0;
}

int main(int argc, char** argv)
{
std::string outputDir;
std::string volumePath;
std::string blockSize = "4MB";
bool sha256dump = false;

GetOptionResult result = GetOption(
const_cast<const char**>(argv) + 1,
argc - 1,
"v:b:o:dh",
{ "volume=", "blocksize=", "output=", "sha256dump", "help"});

for (const OptionResult opt: result.opts) {
std::cout << opt.option << " " << opt.value << std::endl;
if (opt.option == "o" || opt.option == "output=") {
outputDir = opt.value;
} else if (opt.option == "v" || opt.option == "volume=") {
volumePath = opt.value;
} if (opt.option == "b" || opt.option == "blocksize=") {
blockSize = opt.value;
} else if (opt.option == "h" || opt.option == "help") {
return PrintHelp();
} else if (opt.option == "d" || opt.option == "sha256dump") {
sha256dump = true;
}
}
if (sha256dump) {
return ExecDumpVolumeSha256(volumePath, blockSize, outputDir);
}
PrintHelp();
return 0;
}
4 changes: 2 additions & 2 deletions cli/vtools.cpp → cli/vshow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* ================================================================
* Copyright (C) 2023 XUranus All rights reserved.
*
* File: voltool.cpp
* File: vshow.cpp
* Author: XUranus
* Date: 2023-07-01
* Description: a command line tool to display volume info
Expand Down Expand Up @@ -46,7 +46,7 @@ namespace {
}

static const char* g_helpMessage =
"vtools [options...] util for getting local volume information\n"
"vshow [options...] util for getting local volume information\n"
"[ -v | --volume= ] query specified volume information\n"
"[ -l | --list ] list all local volumes\n"
"[ -h | --help ] show help\n";
Expand Down
2 changes: 1 addition & 1 deletion include/task/VolumeBlockWriter.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef VOLUMEBACKUP_BLOCK_WRITER_HEADER
#define VOLUMEBACKUP_BLOCK_WRITER_HEADER

#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"
#include "VolumeProtectTaskContext.h"
#include "native/RawIO.h"

Expand Down
2 changes: 1 addition & 1 deletion include/task/VolumeProtectTaskContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef VOLUMEBACKUP_PROTECT_TASK_CONTEXT_HEADER
#define VOLUMEBACKUP_PROTECT_TASK_CONTEXT_HEADER

#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"
#include "VolumeProtector.h"
#include "BlockingQueue.h"

Expand Down
2 changes: 1 addition & 1 deletion src/VolumeProtector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "VolumeProtector.h"
#include "VolumeBackupTask.h"
#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"
#include "VolumeRestoreTask.h"
#include "VolumeUtils.h"
#include "native/FileSystemAPI.h"
Expand Down
2 changes: 1 addition & 1 deletion src/task/VolumeBlockWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

#include "Logger.h"
#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"
#include "VolumeProtector.h"
#include "native/RawIO.h"
#include "VolumeBlockWriter.h"
Expand Down
6 changes: 5 additions & 1 deletion src/task/VolumeProtectTaskContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

#include "Logger.h"
#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"
#include "VolumeProtectTaskContext.h"
#include "VolumeProtector.h"
#include "native/FileSystemAPI.h"
Expand Down Expand Up @@ -424,6 +424,10 @@ bool VolumeTaskCheckpointTrait::FlushSessionWriter(std::shared_ptr<VolumeTaskSes

bool VolumeTaskCheckpointTrait::FlushSessionBitmap(SessionPtr session) const
{
if (!IsCheckpointEnabled(session)) {
DBGLOG("checkpoint not enabled, skip flush session bitmap");
return true;
}
auto checkpointSnapshot = TakeSessionCheckpointSnapshot(session);
std::string checkpointFilePath = session->sharedConfig->checkpointFilePath;
if (!checkpointSnapshot->SaveTo(checkpointFilePath)) {
Expand Down

0 comments on commit 93598dc

Please sign in to comment.