mirror of
https://github.com/Xevion/easy7zip.git
synced 2025-12-09 10:07:10 -06:00
4.36 beta
This commit is contained in:
committed by
Kornel Lesiński
parent
191cf6781a
commit
8304895f29
354
7zip/FileManager/PanelCrc.cpp
Executable file
354
7zip/FileManager/PanelCrc.cpp
Executable file
@@ -0,0 +1,354 @@
|
||||
// PanelSplitFile.cpp
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include "resource.h"
|
||||
|
||||
#include "Common/Alloc.h"
|
||||
#include "Common/CRC.h"
|
||||
#include "Common/IntToString.h"
|
||||
#include "Common/StringConvert.h"
|
||||
|
||||
#include "Windows/FileIO.h"
|
||||
#include "Windows/FileFind.h"
|
||||
#include "Windows/FileName.h"
|
||||
#include "Windows/Thread.h"
|
||||
#include "Windows/Error.h"
|
||||
|
||||
#include "Resource/ProgressDialog2/ProgressDialog.h"
|
||||
#include "Resource/OverwriteDialog/resource.h"
|
||||
|
||||
#include "App.h"
|
||||
#include "FormatUtils.h"
|
||||
#include "LangUtils.h"
|
||||
|
||||
using namespace NWindows;
|
||||
using namespace NFile;
|
||||
using namespace NName;
|
||||
|
||||
static const UInt32 kBufSize = (1 << 15);
|
||||
|
||||
struct CDirEnumerator
|
||||
{
|
||||
UString BasePrefix;
|
||||
UStringVector FileNames;
|
||||
|
||||
CObjectVector<NFind::CEnumeratorW> Enumerators;
|
||||
UStringVector Prefixes;
|
||||
int Index;
|
||||
bool GetNextFile(NFind::CFileInfoW &fileInfo, bool &filled, UString &fullPath, DWORD &errorCode);
|
||||
void Init();
|
||||
};
|
||||
|
||||
void CDirEnumerator::Init()
|
||||
{
|
||||
Enumerators.Clear();
|
||||
Prefixes.Clear();
|
||||
Index = 0;
|
||||
}
|
||||
|
||||
bool CDirEnumerator::GetNextFile(NFind::CFileInfoW &fileInfo, bool &filled, UString &resPath, DWORD &errorCode)
|
||||
{
|
||||
filled = false;
|
||||
while(true)
|
||||
{
|
||||
if (Enumerators.IsEmpty())
|
||||
{
|
||||
if (Index >= FileNames.Size())
|
||||
return true;
|
||||
const UString &path = FileNames[Index];
|
||||
if (!NFind::FindFile(BasePrefix + path, fileInfo))
|
||||
{
|
||||
errorCode = ::GetLastError();
|
||||
resPath = path;
|
||||
return false;
|
||||
}
|
||||
Index++;
|
||||
resPath.Empty();
|
||||
break;
|
||||
}
|
||||
bool found;
|
||||
if (!Enumerators.Back().Next(fileInfo, found))
|
||||
{
|
||||
errorCode = ::GetLastError();
|
||||
resPath = Prefixes.Back();
|
||||
return false;
|
||||
}
|
||||
if (found)
|
||||
{
|
||||
resPath = Prefixes.Back();
|
||||
break;
|
||||
}
|
||||
Enumerators.DeleteBack();
|
||||
Prefixes.DeleteBack();
|
||||
}
|
||||
resPath += fileInfo.Name;
|
||||
if (fileInfo.IsDirectory())
|
||||
{
|
||||
UString prefix = resPath + (UString)(wchar_t)kDirDelimiter;
|
||||
Enumerators.Add(NFind::CEnumeratorW(BasePrefix + prefix + (UString)(wchar_t)kAnyStringWildcard));
|
||||
Prefixes.Add(prefix);
|
||||
}
|
||||
filled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
struct CThreadCrc
|
||||
{
|
||||
class CMyBuffer
|
||||
{
|
||||
void *_data;
|
||||
public:
|
||||
CMyBuffer(): _data(0) {}
|
||||
operator void *() { return _data; }
|
||||
bool Allocate(size_t size)
|
||||
{
|
||||
if (_data != 0)
|
||||
return false;
|
||||
_data = ::MidAlloc(size);
|
||||
return _data != 0;
|
||||
}
|
||||
~CMyBuffer() { ::MidFree(_data); }
|
||||
};
|
||||
|
||||
CProgressDialog *ProgressDialog;
|
||||
|
||||
CDirEnumerator DirEnumerator;
|
||||
|
||||
UInt64 NumFiles;
|
||||
UInt64 NumFolders;
|
||||
UInt64 DataSize;
|
||||
UInt32 DataCrcSum;
|
||||
UInt32 DataNameCrcSum;
|
||||
|
||||
HRESULT Result;
|
||||
DWORD ErrorCode;
|
||||
UString ErrorPath;
|
||||
UString Error;
|
||||
bool ThereIsError;
|
||||
|
||||
void Process2()
|
||||
{
|
||||
DataSize = NumFolders = NumFiles = DataCrcSum = DataNameCrcSum = 0;
|
||||
ProgressDialog->WaitCreating();
|
||||
|
||||
CMyBuffer bufferObject;
|
||||
if (!bufferObject.Allocate(kBufSize))
|
||||
{
|
||||
Error = L"Can not allocate memory";
|
||||
ThereIsError = true;
|
||||
return;
|
||||
}
|
||||
Byte *buffer = (Byte *)(void *)bufferObject;
|
||||
|
||||
UInt64 totalSize = 0;
|
||||
|
||||
DirEnumerator.Init();
|
||||
|
||||
UString scanningStr = LangString(IDS_SCANNING, 0x03020800);
|
||||
scanningStr += L" ";
|
||||
|
||||
while (true)
|
||||
{
|
||||
NFile::NFind::CFileInfoW fileInfo;
|
||||
bool filled;
|
||||
UString resPath;
|
||||
if (!DirEnumerator.GetNextFile(fileInfo, filled, resPath, ErrorCode))
|
||||
{
|
||||
ThereIsError = true;
|
||||
ErrorPath = resPath;
|
||||
return;
|
||||
}
|
||||
if (!filled)
|
||||
break;
|
||||
if (!fileInfo.IsDirectory())
|
||||
totalSize += fileInfo.Size;
|
||||
ProgressDialog->ProgressSynch.SetCurrentFileName(scanningStr + resPath);
|
||||
ProgressDialog->ProgressSynch.SetProgress(totalSize, 0);
|
||||
Result = ProgressDialog->ProgressSynch.SetPosAndCheckPaused(0);
|
||||
if (Result != S_OK)
|
||||
return;
|
||||
}
|
||||
|
||||
ProgressDialog->ProgressSynch.SetProgress(totalSize, 0);
|
||||
|
||||
DirEnumerator.Init();
|
||||
|
||||
while(true)
|
||||
{
|
||||
NFile::NFind::CFileInfoW fileInfo;
|
||||
bool filled;
|
||||
UString resPath;
|
||||
if (!DirEnumerator.GetNextFile(fileInfo, filled, resPath, ErrorCode))
|
||||
{
|
||||
ThereIsError = true;
|
||||
ErrorPath = resPath;
|
||||
return;
|
||||
}
|
||||
if (!filled)
|
||||
break;
|
||||
|
||||
CCRC crc;
|
||||
if (fileInfo.IsDirectory())
|
||||
NumFolders++;
|
||||
else
|
||||
{
|
||||
NFile::NIO::CInFile inFile;
|
||||
if (!inFile.Open(DirEnumerator.BasePrefix + resPath))
|
||||
{
|
||||
ErrorCode = ::GetLastError();
|
||||
ThereIsError = true;
|
||||
ErrorPath = resPath;
|
||||
return;
|
||||
}
|
||||
NumFiles++;
|
||||
ProgressDialog->ProgressSynch.SetCurrentFileName(resPath);
|
||||
while(true)
|
||||
{
|
||||
UInt32 processedSize;
|
||||
if (!inFile.Read(buffer, kBufSize, processedSize))
|
||||
{
|
||||
ErrorCode = ::GetLastError();
|
||||
ThereIsError = true;
|
||||
ErrorPath = resPath;
|
||||
return;
|
||||
}
|
||||
if (processedSize == 0)
|
||||
break;
|
||||
crc.Update(buffer, processedSize);
|
||||
DataSize += processedSize;
|
||||
Result = ProgressDialog->ProgressSynch.SetPosAndCheckPaused(DataSize);
|
||||
if (Result != S_OK)
|
||||
return;
|
||||
}
|
||||
DataCrcSum += crc.GetDigest();
|
||||
}
|
||||
for (int i = 0; i < resPath.Length(); i++)
|
||||
{
|
||||
wchar_t c = resPath[i];
|
||||
crc.UpdateByte(c & 0xFF);
|
||||
crc.UpdateByte((c >> 8) & 0xFF);
|
||||
}
|
||||
DataNameCrcSum += crc.GetDigest();
|
||||
Result = ProgressDialog->ProgressSynch.SetPosAndCheckPaused(DataSize);
|
||||
if (Result != S_OK)
|
||||
return;
|
||||
}
|
||||
}
|
||||
DWORD Process()
|
||||
{
|
||||
try { Process2(); }
|
||||
catch(...) { Error = L"Error"; ThereIsError = true;}
|
||||
ProgressDialog->MyClose();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static DWORD WINAPI MyThreadFunction(void *param)
|
||||
{
|
||||
return ((CThreadCrc *)param)->Process();
|
||||
}
|
||||
};
|
||||
|
||||
static void ConvertUInt32ToHex(UInt32 value, wchar_t *s)
|
||||
{
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
int t = value & 0xF;
|
||||
value >>= 4;
|
||||
s[7 - i] = (wchar_t)((t < 10) ? (L'0' + t) : (L'A' + (t - 10)));
|
||||
}
|
||||
s[8] = L'\0';
|
||||
}
|
||||
|
||||
void CApp::CalculateCrc()
|
||||
{
|
||||
int srcPanelIndex = GetFocusedPanelIndex();
|
||||
CPanel &srcPanel = Panels[srcPanelIndex];
|
||||
if (!srcPanel.IsFSFolder())
|
||||
{
|
||||
srcPanel.MessageBox(LangString(IDS_OPERATION_IS_NOT_SUPPORTED, 0x03020208));
|
||||
return;
|
||||
}
|
||||
CRecordVector<UInt32> indices;
|
||||
srcPanel.GetOperatedItemIndices(indices);
|
||||
if (indices.IsEmpty())
|
||||
return;
|
||||
|
||||
CThreadCrc combiner;
|
||||
for (int i = 0; i < indices.Size(); i++)
|
||||
combiner.DirEnumerator.FileNames.Add(srcPanel.GetItemName(indices[i]));
|
||||
combiner.DirEnumerator.BasePrefix = srcPanel._currentFolderPrefix;
|
||||
|
||||
CProgressDialog progressDialog;
|
||||
combiner.ProgressDialog = &progressDialog;
|
||||
combiner.ErrorCode = 0;
|
||||
combiner.Result = S_OK;
|
||||
combiner.ThereIsError = false;
|
||||
|
||||
UString progressWindowTitle = LangString(IDS_APP_TITLE, 0x03000000);
|
||||
UString title = LangString(IDS_CHECKSUM_CALCULATING, 0x03020710);
|
||||
|
||||
progressDialog.MainWindow = _window;
|
||||
progressDialog.MainTitle = progressWindowTitle;
|
||||
progressDialog.MainAddTitle = title + UString(L" ");
|
||||
|
||||
CThread thread;
|
||||
if (!thread.Create(CThreadCrc::MyThreadFunction, &combiner))
|
||||
throw 271824;
|
||||
progressDialog.Create(title, _window);
|
||||
|
||||
if (combiner.Result != S_OK)
|
||||
{
|
||||
if (combiner.Result != E_ABORT)
|
||||
srcPanel.MessageBoxError(combiner.Result);
|
||||
}
|
||||
else if (combiner.ThereIsError)
|
||||
{
|
||||
if (combiner.Error.IsEmpty())
|
||||
{
|
||||
UString message = combiner.DirEnumerator.BasePrefix + combiner.ErrorPath;
|
||||
message += L"\n";
|
||||
message += NError::MyFormatMessageW(combiner.ErrorCode);
|
||||
srcPanel.MessageBoxMyError(message);
|
||||
}
|
||||
else
|
||||
srcPanel.MessageBoxMyError(combiner.Error);
|
||||
}
|
||||
else
|
||||
{
|
||||
UString s;
|
||||
{
|
||||
wchar_t sz[32];
|
||||
|
||||
s += LangString(IDS_FILES_COLON, 0x02000320);
|
||||
s += L" ";
|
||||
ConvertUInt64ToString(combiner.NumFiles, sz);
|
||||
s += sz;
|
||||
s += L"\n";
|
||||
|
||||
s += LangString(IDS_FOLDERS_COLON, 0x02000321);
|
||||
s += L" ";
|
||||
ConvertUInt64ToString(combiner.NumFolders, sz);
|
||||
s += sz;
|
||||
s += L"\n";
|
||||
|
||||
s += LangString(IDS_SIZE_COLON, 0x02000322);
|
||||
s += L" ";
|
||||
ConvertUInt64ToString(combiner.DataSize, sz);
|
||||
s += MyFormatNew(IDS_FILE_SIZE, 0x02000982, sz);;
|
||||
s += L"\n";
|
||||
|
||||
s += LangString(IDS_CHECKSUM_CRC_DATA, 0x03020721);
|
||||
s += L" ";
|
||||
ConvertUInt32ToHex(combiner.DataCrcSum, sz);
|
||||
s += sz;
|
||||
s += L"\n";
|
||||
|
||||
s += LangString(IDS_CHECKSUM_CRC_DATA_NAMES, 0x03020722);
|
||||
s += L" ";
|
||||
ConvertUInt32ToHex(combiner.DataNameCrcSum, sz);
|
||||
s += sz;
|
||||
}
|
||||
srcPanel.MessageBoxInfo(s, LangString(IDS_CHECKSUM_INFORMATION, 0x03020720));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user