Skip to content

Commit

Permalink
Add switch "-sumRelativePath" to store file paths in SUM output file …
Browse files Browse the repository at this point in the history
…as relative to the input directory
  • Loading branch information
idrassi committed Jul 26, 2021
1 parent 2efbe58 commit c3d75c0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
69 changes: 57 additions & 12 deletions DirHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ static HANDLE g_hOutputThread = NULL;
static std::wstring g_szLastErrorMsg;
static wstring g_currentDirectory;
static bool g_sumFileSkipped = false;
static bool g_bSumRelativePath = false;
static wstring g_inputDirPath;
static size_t g_inputDirPathLength = 0;


typedef BOOL(WINAPI* SetThreadGroupAffinityFn)(
Expand Down Expand Up @@ -1281,7 +1284,13 @@ void ProcessFile(HANDLE f, ULONGLONG fileSize, LPCTSTR szFilePath, bool bQuiet,

std::wstring szMsg = szDigestHex;
szMsg += L" ";
szMsg += szFilePath;
if (g_bSumRelativePath)
{
// remove the input directory from the path written to the SUM file
szMsg += (szFilePath + g_inputDirPathLength);
}
else
szMsg += szFilePath;
szMsg += L"\n";

if (g_threadsCount)
Expand Down Expand Up @@ -1520,7 +1529,7 @@ DWORD HashFile(const CPath& filePath, Hash* pHash, bool bIncludeNames, bool bStr
if (!digestList.empty())
{
// check that the current file is specified in the checksum file
It = digestList.find(szFilePath);
It = digestList.find(szFilePath);
if (It == digestList.end())
{
std::wstring szMsg = FormatString(_T("Error: file \"%s\" not found in checksum file.\n"), szFilePath);
Expand Down Expand Up @@ -2007,6 +2016,7 @@ typedef struct
bool bNoLogo;
bool bForceSumMode;
bool bUseThreads;
bool bSumRelativePath;
} ConfigParams;

void LoadDefaults(ConfigParams& iniParams)
Expand All @@ -2024,6 +2034,7 @@ void LoadDefaults(ConfigParams& iniParams)
iniParams.bNoLogo = false;
iniParams.bForceSumMode = false;
iniParams.bUseThreads = false;
iniParams.bSumRelativePath = false;

// get values from DirHash.ini fille if it exists
WCHAR szInitPath[1024];
Expand Down Expand Up @@ -2138,6 +2149,14 @@ void LoadDefaults(ConfigParams& iniParams)
else
iniParams.bUseThreads = false;
}

if (GetPrivateProfileStringW(L"Defaults", L"SumRelativePath", L"False", szValue, ARRAYSIZE(szValue), szInitPath))
{
if (_wcsicmp(szValue, L"True") == 0)
iniParams.bSumRelativePath = true;
else
iniParams.bSumRelativePath = false;
}
}
}

Expand Down Expand Up @@ -2349,6 +2368,15 @@ bool ParseSumFile(const wchar_t* sumFile, map<wstring, HashResultEntry>& digestL
wstring entryName = ptr;
// replace '/' by '\' for compatibility with checksum format on *nix platforms
std::replace(entryName.begin(), entryName.end(), L'/', L'\\');

// check that entreName starts by the input directory value. Otherwise add it.
if ( (entryName.length() < g_inputDirPathLength)
|| (_wcsicmp(g_inputDirPath.c_str(), entryName.substr(0, g_inputDirPathLength).c_str()))
)
{
entryName = g_inputDirPath + entryName;
}

digestLen = digest.size();
digestList[entryName].m_digest = digest;
bFailed = false;
Expand Down Expand Up @@ -2534,6 +2562,7 @@ int _tmain(int argc, _TCHAR* argv[])
g_bNoLogo = iniParams.bNoLogo;
bForceSumMode = iniParams.bForceSumMode;
bUseThreads = iniParams.bUseThreads;
g_bSumRelativePath = iniParams.bSumRelativePath;

if (_tcscmp(argv[1], _T("-benchmark")) == 0)
bBenchmarkOp = true;
Expand Down Expand Up @@ -2722,6 +2751,10 @@ int _tmain(int argc, _TCHAR* argv[])
{
bUseThreads = true;
}
else if (_tcsicmp(argv[i], _T("-sumRelativePath")) == 0)
{
g_bSumRelativePath = true;
}
else
{
if (outputFile) fclose(outputFile);
Expand Down Expand Up @@ -2804,6 +2837,25 @@ int _tmain(int argc, _TCHAR* argv[])
fflush(stdout);
}

inputArg = argv[1];
std::replace(inputArg.begin(), inputArg.end(), L'/', L'\\');

if (GetPathType(argv[1], bIsFile) && !bIsFile)
{
// remove any trailing backslash to harmonize directory names in case they are included
// in hash computations
size_t inputArgLen = wcslen(inputArg.c_str());
if (inputArg[inputArgLen - 1] == L'\\')
inputArg.erase(inputArgLen - 1, 1);

if (bSumMode || bVerifyMode)
{
// we store the input directory when -sum or -verify are specified
g_inputDirPath = inputArg + L"\\";
g_inputDirPathLength = wcslen(g_inputDirPath.c_str());
}
}

if (bVerifyMode)
{

Expand Down Expand Up @@ -2868,26 +2920,19 @@ int _tmain(int argc, _TCHAR* argv[])
StartThreads(!bQuiet || outputFile);
}

inputArg = argv[1];

if (GetPathType(inputArg.c_str(), bIsFile) && !bIsFile)
if (!bIsFile)
{
// remove any trailing backslash to harmonize directory names in case they are included
// in hash computations
size_t inputArgLen = wcslen(inputArg.c_str());
if (inputArg[inputArgLen - 1] == L'\\' || inputArg[inputArgLen - 1] == L'/')
inputArg.erase(inputArgLen - 1, 1);

CPath dirPath(inputArg.c_str());
dwError = HashDirectory(dirPath, pHash, bIncludeNames, bStripNames, excludeSpecList, bQuiet, bShowProgress, bSumMode, digestsList);

}
else
{
CPath filePath(inputArg.c_str());
dwError = NO_ERROR;
if (bSumMode)
{
// if input is a file, -sumRelativePath is irrelevant
g_bSumRelativePath = false;
if (!digestsList.empty())
{
// verification
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Home page: https://idrassi.github.io/DirHash/
Usage
------------

DirHash.exe DirectoryOrFilePath [HashAlgo] [-t ResultFileName] [-progress] [-sum] [-verify FileName] [threads] [-clip] [-lowercase] [-overwrite] [-quiet] [-nologo] [-nowait] [-skipError] [-hashnames [-stripnames]] [-exclude pattern1] [-exclude patter2]
DirHash.exe DirectoryOrFilePath [HashAlgo] [-t ResultFileName] [-progress] [-sum] [-sumRelativePath] [-verify FileName] [threads] [-clip] [-lowercase] [-overwrite] [-quiet] [-nologo] [-nowait] [-skipError] [-hashnames [-stripnames]] [-exclude pattern1] [-exclude patter2]

DirHash.exe -benchmark [HashAlgo | All] [-t ResultFileName] [-clip] [-overwrite] [-quiet] [-nologo] [-nowait]

Expand All @@ -37,6 +37,8 @@ if `-mscrypto` specified, program will use Windows native implementation of hash

if `-sum` is specified, program will output the hash of every file processed in a format similar to shasum.

if `-sumRelativePath` is specified (only when -sum is specified), the file paths are stored in the output file as relative to the input directory.

if `-verify` is specified, program will verify the hash against value(s) present on the specified file. The argument to this switch must be either a checksum file or a result file.

if `-threads` is specified (only when -sum or -verify specified), multithreading will be used to accelerate hashing of files.
Expand Down Expand Up @@ -64,13 +66,14 @@ If `-skipError` is specified, ignore any encountered errors and continue process
If `-nologo` is specified, don't display the copyright message and version number on startup.

DirHash can also be configured using a configuration file called DirHash.ini and which must be on the same folder as DirHash.exe.
When `Sum=True` is specified in DirHash.ini, it will have an effect only if "-verify" is not specified in the command line.
When `Sum=True` is specified in DirHash.ini, it will have an effect only if `-verify` is not specified in the command line.
An example of DirHash.ini is shown below:

```
[Defaults]
Hash=Blake3
Sum=True
SumRelativePath=True
Threads=True
Quiet=False
Nologo=True
Expand Down

0 comments on commit c3d75c0

Please sign in to comment.