1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
// CompressCall.cpp
#include "StdAfx.h"
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#undef _WIN32
#include "CompressCall.h"
// FIXME #include "Common/Random.h"
#include "Common/IntToString.h"
#include "Common/MyCom.h"
#include "Common/StringConvert.h"
#include "Windows/Synchronization.h"
// FIXME #include "Windows/FileMapping.h"
#include "Windows/FileDir.h"
#include "../FileManager/ProgramLocation.h"
#include "../FileManager/RegistryUtils.h"
#define NEED_NAME_WINDOWS_TO_UNIX
#include "myPrivate.h"
#ifndef _UNICODE
extern bool g_IsNT;
#endif // _UNICODE
using namespace NWindows;
static LPCWSTR kShowDialogSwitch = L" -ad";
static LPCWSTR kEmailSwitch = L" -seml.";
static LPCWSTR kMapSwitch = L" -i#";
static LPCWSTR kArchiveNoNameSwitch = L" -an";
static LPCWSTR kArchiveTypeSwitch = L" -t";
static LPCWSTR kArchiveMapSwitch = L" -ai#";
static LPCWSTR kStopSwitchParsing = L" --";
static LPCWSTR kLargePagesDisable = L" -slp-";
static void AddLagePagesSwitch(UString ¶ms)
{
#ifdef _WIN32
if (!ReadLockMemoryEnable())
params += kLargePagesDisable;
#endif
}
HRESULT MyCreateProcess(const UString ¶ms,
LPCWSTR curDir, bool waitFinish,
NWindows::NSynchronization::CBaseEvent *event)
{
printf("MyCreateProcess: waitFinish=%d event=%p\n",(unsigned)waitFinish,event);
printf("\tparams : %ls\n",(const wchar_t*)params);
printf("\tcurDir : %ls\n",(const wchar_t*)curDir);
wxString cmd(params);
wxString memoCurDir = wxGetCwd();
if (curDir) { // FIXME
wxSetWorkingDirectory(wxString(curDir));
// under MacOSX, a bundle does not keep the current directory
// between 7zFM and 7zG ...
// So, try to use the environment variable P7ZIP_CURRENT_DIR
char p7zip_current_dir[MAX_PATH];
AString aCurPath = GetAnsiString(curDir);
const char *dir2 = nameWindowToUnix((const char *)aCurPath);
snprintf(p7zip_current_dir,sizeof(p7zip_current_dir),"P7ZIP_CURRENT_DIR=%s/",dir2);
p7zip_current_dir[sizeof(p7zip_current_dir)-1] = 0;
putenv(p7zip_current_dir);
printf("putenv(%s)\n",p7zip_current_dir);
}
printf("MyCreateProcess: cmd='%ls'\n",(const wchar_t *)cmd);
long pid = 0;
if (waitFinish) pid = wxExecute(cmd, wxEXEC_SYNC); // FIXME process never ends and stays zombie ...
else pid = wxExecute(cmd, wxEXEC_ASYNC);
if (curDir) wxSetWorkingDirectory(memoCurDir);
// FIXME if (pid == 0) return E_FAIL;
return S_OK;
#ifdef _WIN32 // FIXME
const UString params2 = params;
BOOL result;
{
STARTUPINFOW startupInfo;
startupInfo.cb = sizeof(startupInfo);
startupInfo.lpReserved = 0;
startupInfo.lpDesktop = 0;
startupInfo.lpTitle = 0;
startupInfo.dwFlags = 0;
startupInfo.cbReserved2 = 0;
startupInfo.lpReserved2 = 0;
result = ::CreateProcessW(NULL, (LPWSTR)(LPCWSTR)params,
NULL, NULL, FALSE, 0, NULL,
curDir,
&startupInfo, &processInformation);
}
if (result == 0)
return ::GetLastError();
else
{
::CloseHandle(processInformation.hThread);
if (waitFinish)
WaitForSingleObject(processInformation.hProcess, INFINITE);
else if (event != NULL)
{
HANDLE handles[] = {processInformation.hProcess, *event };
::WaitForMultipleObjects(sizeof(handles) / sizeof(handles[0]),
handles, FALSE, INFINITE);
}
::CloseHandle(processInformation.hProcess);
}
return S_OK;
#endif
}
UString GetQuotedString(const UString &s)
{
return UString(L"\"") + s + UString(L"\"");
}
static UString Get7zGuiPath()
{
UString path;
UString folder;
if (GetProgramFolderPath(folder))
path += folder;
#ifdef _WIN32
path += L"7zG.exe";
#else
path += L"7zG";
#endif
return GetQuotedString(path);
}
#ifdef _WIN32
static HRESULT CreateTempEvent(const wchar_t *name,
NSynchronization::CManualResetEvent &event, UString &eventName)
{
CRandom random;
random.Init(GetTickCount());
for (;;)
{
int number = random.Generate();
wchar_t temp[32];
ConvertUInt64ToString((UInt32)number, temp);
eventName = name;
eventName += temp;
RINOK(event.CreateWithName(false, GetSystemString(eventName)));
if (::GetLastError() != ERROR_ALREADY_EXISTS)
return S_OK;
event.Close();
}
}
static HRESULT CreateMap(const UStringVector &names,
const UString &id,
CFileMapping &fileMapping, NSynchronization::CManualResetEvent &event,
UString ¶ms)
{
UInt32 extraSize = 2;
UInt32 dataSize = 0;
for (int i = 0; i < names.Size(); i++)
dataSize += (names[i].Length() + 1) * sizeof(wchar_t);
UInt32 totalSize = extraSize + dataSize;
UString mappingName;
CRandom random;
random.Init(GetTickCount());
for (;;)
{
int number = random.Generate();
wchar_t temp[32];
ConvertUInt64ToString(UInt32(number), temp);
mappingName = id;
mappingName += L"Mapping";
mappingName += temp;
if (!fileMapping.Create(INVALID_HANDLE_VALUE, NULL,
PAGE_READWRITE, totalSize, GetSystemString(mappingName)))
return E_FAIL;
if (::GetLastError() != ERROR_ALREADY_EXISTS)
break;
fileMapping.Close();
}
UString eventName;
RINOK(CreateTempEvent(id + L"MappingEndEvent", event, eventName));
params += mappingName;
params += L":";
wchar_t string[10];
ConvertUInt64ToString(totalSize, string);
params += string;
params += L":";
params += eventName;
LPVOID data = fileMapping.MapViewOfFile(FILE_MAP_WRITE, 0, totalSize);
if (data == NULL)
return E_FAIL;
{
wchar_t *curData = (wchar_t *)data;
*curData = 0;
curData++;
for (int i = 0; i < names.Size(); i++)
{
const UString &s = names[i];
memcpy(curData, (const wchar_t *)s, s.Length() * sizeof(wchar_t));
curData += s.Length();
*curData++ = L'\0';
}
}
return S_OK;
}
#endif
HRESULT CompressFiles(
const UString &curDir,
const UString &archiveName,
const UString &archiveType,
const UStringVector &names,
// const UString &outFolder,
bool email,
bool showDialog,
bool waitFinish)
{
/*
UString curDir;
if (names.Size() > 0)
{
NFile::NDirectory::GetOnlyDirPrefix(names[0], curDir);
}
*/
UString params;
params = Get7zGuiPath();
params += L" a";
#ifdef _WIN32
params += kMapSwitch;
// params += _fileNames[0];
UInt32 extraSize = 2;
UInt32 dataSize = 0;
for (int i = 0; i < names.Size(); i++)
dataSize += (names[i].Length() + 1) * sizeof(wchar_t);
UInt32 totalSize = extraSize + dataSize;
UString mappingName;
CFileMapping fileMapping;
CRandom random;
random.Init(GetTickCount());
for (;;)
{
int number = random.Generate();
wchar_t temp[32];
ConvertUInt64ToString(UInt32(number), temp);
mappingName = L"7zCompressMapping";
mappingName += temp;
if (!fileMapping.Create(INVALID_HANDLE_VALUE, NULL,
PAGE_READWRITE, totalSize, GetSystemString(mappingName)))
{
// MyMessageBox(IDS_ERROR, 0x02000605);
return E_FAIL;
}
if (::GetLastError() != ERROR_ALREADY_EXISTS)
break;
fileMapping.Close();
}
NSynchronization::CManualResetEvent event;
UString eventName;
RINOK(CreateTempEvent(L"7zCompressMappingEndEvent", event, eventName));
params += mappingName;
params += L":";
wchar_t string[10];
ConvertUInt64ToString(totalSize, string);
params += string;
params += L":";
params += eventName;
#else
char tempFile[256];
static int count = 1000;
sprintf(tempFile,"/tmp/7zCompress_%d_%d.tmp",(int)getpid(),count++);
FILE * file = fopen(tempFile,"w");
if (file)
{
for (int i = 0; i < names.Size(); i++) {
fprintf(file,"%ls\n",(const wchar_t *)names[i]);
printf(" TMP_%d : '%ls'\n",i,(const wchar_t *)names[i]);
}
fclose(file);
}
params += L" -i@";
params += GetUnicodeString(tempFile);
#endif
if (!archiveType.IsEmpty())
{
params += kArchiveTypeSwitch;
params += archiveType;
}
if (email)
params += kEmailSwitch;
if (showDialog)
params += kShowDialogSwitch;
AddLagePagesSwitch(params);
params += kStopSwitchParsing;
params += L" ";
params += GetQuotedString(archiveName);
#ifdef _WIN32
LPVOID data = fileMapping.MapViewOfFile(FILE_MAP_WRITE, 0, totalSize);
if (data == NULL)
{
// MyMessageBox(IDS_ERROR, 0x02000605);
return E_FAIL;
}
try
{
wchar_t *curData = (wchar_t *)data;
*curData = 0;
curData++;
for (int i = 0; i < names.Size(); i++)
{
const UString &unicodeString = names[i];
memcpy(curData, (const wchar_t *)unicodeString ,
unicodeString .Length() * sizeof(wchar_t));
curData += unicodeString.Length();
*curData++ = L'\0';
}
// MessageBox(0, params, 0, 0);
RINOK(MyCreateProcess(params,
(curDir.IsEmpty()? 0: (LPCWSTR)curDir),
waitFinish, &event));
}
catch(...)
{
UnmapViewOfFile(data);
throw;
}
UnmapViewOfFile(data);
/*
CThreadCompressMain *compressor = new CThreadCompressMain();;
compressor->FileNames = _fileNames;
CThread thread;
if (!thread.Create(CThreadCompressMain::MyThreadFunction, compressor))
throw 271824;
*/
#else
printf("CompressFiles : -%ls-\n",(const wchar_t *)params);
HRESULT res = MyCreateProcess(params,
(curDir.IsEmpty()? 0: (LPCWSTR)curDir),
true, /* &event FIXME */ 0);
printf("CompressFiles : END\n");
remove(tempFile);
#endif
return S_OK;
}
static HRESULT ExtractGroupCommand(const UStringVector &archivePaths,
const UString ¶ms)
{
UString params2 = params;
AddLagePagesSwitch(params2);
params2 += kArchiveNoNameSwitch;
#ifdef _WIN32
params2 += kArchiveMapSwitch;
CFileMapping fileMapping;
NSynchronization::CManualResetEvent event;
RINOK(CreateMap(archivePaths, L"7zExtract", fileMapping, event, params2));
return MyCreateProcess(params2, 0, false, &event);
#else
char tempFile[256];
static int count = 1000;
sprintf(tempFile,"/tmp/7zExtract_%d_%d.tmp",(int)getpid(),count++);
FILE * file = fopen(tempFile,"w");
if (file)
{
for (int i = 0; i < archivePaths.Size(); i++) {
fprintf(file,"%ls\n",(const wchar_t *)archivePaths[i]);
printf(" TMP_%d : '%ls'\n",i,(const wchar_t *)archivePaths[i]);
}
fclose(file);
}
params2 += L" -ai@";
params2 += GetUnicodeString(tempFile);
printf("ExtractGroupCommand : -%ls-\n",(const wchar_t *)params2);
HRESULT res = MyCreateProcess(params2, 0, true, /* &event FIXME */ 0);
printf("ExtractGroupCommand : END\n");
remove(tempFile);
return res;
#endif
}
HRESULT ExtractArchives(const UStringVector &archivePaths,
const UString &outFolder, bool showDialog)
{
UString params;
params = Get7zGuiPath();
params += L" x";
if (!outFolder.IsEmpty())
{
params += L" \"-o";
params += outFolder;
params += L"\"";
}
if (showDialog)
params += kShowDialogSwitch;
return ExtractGroupCommand(archivePaths, params);
}
HRESULT TestArchives(const UStringVector &archivePaths)
{
UString params;
params = Get7zGuiPath();
params += L" t";
return ExtractGroupCommand(archivePaths, params);
}
HRESULT Benchmark()
{
UString params;
params = Get7zGuiPath();
params += L" b";
return MyCreateProcess(params, 0, false, NULL);
}
|