-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
236 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 . | ||
|
@@ -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:\ | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters