5 #include "Common/MyInitGuid.h"
7 #include "Common/CommandLineParser.h"
8 #include "Common/MyException.h"
9 #include "Common/IntToString.h"
10 #include "Common/StdOutStream.h"
11 #include "Common/StringConvert.h"
12 #include "Common/StringToInt.h"
14 #include "Windows/FileDir.h"
15 #include "Windows/FileName.h"
16 #include "Windows/Defs.h"
17 #include "Windows/Error.h"
19 #include "Windows/MemoryLock.h"
22 #include "../../IPassword.h"
23 #include "../../ICoder.h"
24 #include "../Common/UpdateAction.h"
25 #include "../Common/Update.h"
26 #include "../Common/Extract.h"
27 #include "../Common/ArchiveCommandLine.h"
28 #include "../Common/ExitCode.h"
29 #ifdef EXTERNAL_CODECS
30 #include "../Common/LoadCodecs.h"
33 #include "../../Compress/LZMA_Alone/LzmaBenchCon.h"
36 #include "OpenCallbackConsole.h"
37 #include "ExtractCallbackConsole.h"
38 #include "UpdateCallbackConsole.h"
40 #include "../../MyVersion.h"
42 #if defined( _WIN32) && defined( _7ZIP_LARGE_PAGES)
45 #include "../../../../C/Alloc.h"
49 using namespace NWindows;
50 using namespace NFile;
51 using namespace NCommandLineParser;
53 HINSTANCE g_hInstance = 0;
54 extern CStdOutStream *g_StdStream;
56 static const char *kCopyrightString = "\n7-Zip"
57 #ifndef EXTERNAL_CODECS
65 " " MY_VERSION_COPYRIGHT_DATE "\n";
67 static const char *kHelpString =
72 #ifndef EXTERNAL_CODECS
76 " <command> [<switches>...] <archive_name> [<file_names>...]\n"
77 " [<@listfiles...>]\n"
80 " a: Add files to archive\n"
82 " d: Delete files from archive\n"
83 " e: Extract files from archive (without using directory names)\n"
84 " l: List contents of archive\n"
85 // " l[a|t][f]: List contents of archive\n"
86 // " a - with Additional fields\n"
87 // " t - with all fields\n"
88 // " f - with Full pathnames\n"
89 " t: Test integrity of archive\n"
90 " u: Update files to archive\n"
91 " x: eXtract files with full paths\n"
93 " -ai[r[-|0]]{@listfile|!wildcard}: Include archives\n"
94 " -ax[r[-|0]]{@listfile|!wildcard}: eXclude archives\n"
95 " -bd: Disable percentage indicator\n"
96 " -i[r[-|0]]{@listfile|!wildcard}: Include filenames\n"
97 " -m{Parameters}: set compression Method\n"
98 " -o{Directory}: set Output directory\n"
99 " -p{Password}: set Password\n"
100 " -r[-|0]: Recurse subdirectories\n"
101 " -scs{UTF-8 | WIN | DOS}: set charset for list files\n"
102 " -sfx[{name}]: Create SFX archive\n"
103 " -si[{name}]: read data from stdin\n"
104 " -slt: show technical information for l (List) command\n"
105 " -so: write data to stdout\n"
106 " -ssc[-]: set sensitive case mode\n"
107 " -ssw: compress shared files\n"
108 " -t{Type}: Set type of archive\n"
109 " -v{Size}[b|k|m|g]: Create volumes\n"
110 " -u[-][p#][q#][r#][x#][y#][z#][!newArchiveName]: Update options\n"
111 " -w[{path}]: assign Work directory. Empty path means a temporary directory\n"
112 " -x[r[-|0]]]{@listfile|!wildcard}: eXclude filenames\n"
113 " -y: assume Yes on all queries\n";
115 // ---------------------------
116 // exception messages
118 static const char *kEverythingIsOk = "Everything is Ok";
119 static const char *kUserErrorMessage = "Incorrect command line"; // NExitCode::kUserError
121 static const wchar_t *kDefaultSfxModule = L"7zCon.sfx";
123 static void ShowMessageAndThrowException(CStdOutStream &s, LPCSTR message, NExitCode::EEnum code)
125 s << message << endl;
129 static void PrintHelpAndExit(CStdOutStream &s) // yyy
132 ShowMessageAndThrowException(s, kUserErrorMessage, NExitCode::kUserError);
136 static void GetArguments(int numArguments, const char *arguments[], UStringVector &parts)
139 for(int i = 0; i < numArguments; i++)
141 UString s = MultiByteToUnicodeString(arguments[i]);
147 static void ShowCopyrightAndHelp(CStdOutStream &s, bool needHelp)
149 s << kCopyrightString;
150 // s << "# CPUs: " << (UInt64)NWindows::NSystem::GetNumberOfProcessors() << "\n";
155 #ifdef EXTERNAL_CODECS
156 static void PrintString(CStdOutStream &stdStream, const AString &s, int size)
158 int len = s.Length();
160 for (int i = len; i < size; i++)
165 static void PrintString(CStdOutStream &stdStream, const UString &s, int size)
167 int len = s.Length();
169 for (int i = len; i < size; i++)
173 static inline char GetHex(Byte value)
175 return (char)((value < 10) ? ('0' + value) : ('A' + (value - 10)));
180 int numArguments, const char *arguments[]
188 UStringVector commandStrings;
190 NCommandLineParser::SplitCommandLine(GetCommandLineW(), commandStrings);
192 GetArguments(numArguments, arguments, commandStrings);
195 if(commandStrings.Size() == 1)
197 ShowCopyrightAndHelp(g_StdOut, true);
200 commandStrings.Delete(0);
202 CArchiveCommandLineOptions options;
204 CArchiveCommandLineParser parser;
206 parser.Parse1(commandStrings, options);
210 ShowCopyrightAndHelp(g_StdOut, true);
214 #if defined(_WIN32) && defined(_7ZIP_LARGE_PAGES)
215 if (options.LargePages)
218 NSecurity::EnableLockMemoryPrivilege();
222 CStdOutStream &stdStream = options.StdOutMode ? g_StdErr : g_StdOut;
223 g_StdStream = &stdStream;
225 if (options.EnableHeaders)
226 ShowCopyrightAndHelp(stdStream, false);
228 parser.Parse2(options);
230 CCodecs *codecs = new CCodecs;
232 #ifdef EXTERNAL_CODECS
237 > compressCodecsInfo = codecs;
238 HRESULT result = codecs->Load();
240 throw CSystemException(result);
242 bool isExtractGroupCommand = options.Command.IsFromExtractGroup();
243 if (options.Command.CommandType == NCommandType::kInfo)
245 stdStream << endl << "Formats:" << endl;
247 for (i = 0; i < codecs->Formats.Size(); i++)
249 const CArcInfoEx &arc = codecs->Formats[i];
250 #ifdef EXTERNAL_CODECS
251 if (arc.LibIndex >= 0)
254 ConvertUInt64ToString(arc.LibIndex, s);
255 PrintString(stdStream, s, 2);
261 stdStream << (char)(arc.UpdateEnabled ? 'C' : ' ');
262 stdStream << (char)(arc.KeepName ? 'K' : ' ');
264 PrintString(stdStream, arc.Name, 6);
267 for (int t = 0; t < arc.Exts.Size(); t++)
269 const CArcExtInfo &ext = arc.Exts[t];
271 if (!ext.AddExt.IsEmpty())
279 PrintString(stdStream, s, 14);
281 const CByteBuffer &sig = arc.StartSignature;
282 for (size_t j = 0; j < sig.GetCapacity(); j++)
285 if (b > 0x20 && b < 0x80)
287 stdStream << (char)b;
291 stdStream << GetHex((Byte)((b >> 4) & 0xF));
292 stdStream << GetHex((Byte)(b & 0xF));
298 stdStream << endl << "Codecs:" << endl;
300 #ifdef EXTERNAL_CODECS
302 if (codecs->GetNumberOfMethods(&numMethods) == S_OK)
303 for (UInt32 j = 0; j < numMethods; j++)
305 int libIndex = codecs->GetCodecLibIndex(j);
309 ConvertUInt64ToString(libIndex, s);
310 PrintString(stdStream, s, 2);
315 stdStream << (char)(codecs->GetCodecEncoderIsAssigned(j) ? 'C' : ' ');
318 HRESULT res = codecs->GetCodecId(j, id);
320 id = (UInt64)(Int64)-1;
322 ConvertUInt64ToString(id, s, 16);
323 PrintString(stdStream, s, 8);
325 PrintString(stdStream, codecs->GetCodecName(j), 11);
329 throw "incorrect Codec ID";
335 else if (options.Command.CommandType == NCommandType::kBenchmark)
337 if (options.Method.CompareNoCase(L"CRC") == 0)
339 HRESULT res = CrcBenchCon((FILE *)stdStream, options.NumIterations, options.NumThreads, options.DictionarySize);
344 stdStream << "\nCRC Error\n";
345 return NExitCode::kFatalError;
347 throw CSystemException(res);
352 HRESULT res = LzmaBenchCon(
356 (FILE *)stdStream, options.NumIterations, options.NumThreads, options.DictionarySize);
361 stdStream << "\nDecoding Error\n";
362 return NExitCode::kFatalError;
364 throw CSystemException(res);
368 else if (isExtractGroupCommand || options.Command.CommandType == NCommandType::kList)
370 if(isExtractGroupCommand)
372 CExtractCallbackConsole *ecs = new CExtractCallbackConsole;
373 CMyComPtr<IFolderArchiveExtractCallback> extractCallback = ecs;
375 ecs->OutStream = &stdStream;
376 ecs->PasswordIsDefined = options.PasswordEnabled;
377 ecs->Password = options.Password;
380 COpenCallbackConsole openCallback;
381 openCallback.OutStream = &stdStream;
382 openCallback.PasswordIsDefined = options.PasswordEnabled;
383 openCallback.Password = options.Password;
386 eo.StdOutMode = options.StdOutMode;
387 eo.PathMode = options.Command.GetPathMode();
388 eo.TestMode = options.Command.IsTestMode();
389 eo.OverwriteMode = options.OverwriteMode;
390 eo.OutputDir = options.OutputDir;
391 eo.YesToAll = options.YesToAll;
393 eo.Properties = options.ExtractProperties;
395 UString errorMessage;
396 CDecompressStat stat;
397 HRESULT result = DecompressArchives(
399 options.ArchivePathsSorted,
400 options.ArchivePathsFullSorted,
401 options.WildcardCensor.Pairs.Front().Head,
402 eo, &openCallback, ecs, errorMessage, stat);
403 if (!errorMessage.IsEmpty())
405 stdStream << endl << "Error: " << errorMessage;
411 if (ecs->NumArchives > 1)
412 stdStream << "Archives: " << ecs->NumArchives << endl;
413 if (ecs->NumArchiveErrors != 0 || ecs->NumFileErrors != 0)
415 if (ecs->NumArchives > 1)
418 if (ecs->NumArchiveErrors != 0)
419 stdStream << "Archive Errors: " << ecs->NumArchiveErrors << endl;
420 if (ecs->NumFileErrors != 0)
421 stdStream << "Sub items Errors: " << ecs->NumFileErrors << endl;
424 throw CSystemException(result);
425 return NExitCode::kFatalError;
428 throw CSystemException(result);
429 if (stat.NumFolders != 0)
430 stdStream << "Folders: " << stat.NumFolders << endl;
431 if (stat.NumFiles != 1 || stat.NumFolders != 0)
432 stdStream << "Files: " << stat.NumFiles << endl;
434 << "Size: " << stat.UnpackSize << endl
435 << "Compressed: " << stat.PackSize << endl;
439 UInt64 numErrors = 0;
440 HRESULT result = ListArchives(
442 options.ArchivePathsSorted,
443 options.ArchivePathsFullSorted,
444 options.WildcardCensor.Pairs.Front().Head,
445 options.EnableHeaders,
447 options.PasswordEnabled,
448 options.Password, numErrors);
451 g_StdOut << endl << "Errors: " << numErrors;
452 return NExitCode::kFatalError;
455 throw CSystemException(result);
458 else if(options.Command.IsFromUpdateGroup())
462 CUpdateOptions &uo = options.UpdateOptions;
463 if (uo.SfxMode && uo.SfxModule.IsEmpty())
464 uo.SfxModule = kDefaultSfxModule;
466 bool passwordIsDefined =
467 options.PasswordEnabled && !options.Password.IsEmpty();
469 COpenCallbackConsole openCallback;
470 openCallback.OutStream = &stdStream;
471 openCallback.PasswordIsDefined = passwordIsDefined;
472 openCallback.Password = options.Password;
474 CUpdateCallbackConsole callback;
475 callback.EnablePercents = options.EnablePercents;
476 callback.PasswordIsDefined = passwordIsDefined;
477 callback.AskPassword = options.PasswordEnabled && options.Password.IsEmpty();
478 callback.Password = options.Password;
479 callback.StdOutMode = uo.StdOutMode;
480 callback.Init(&stdStream);
482 CUpdateErrorInfo errorInfo;
484 if (!uo.Init(codecs, options.ArchiveName, options.ArcType))
485 throw "Unsupported archive type";
486 HRESULT result = UpdateArchive(codecs,
487 options.WildcardCensor, uo,
488 errorInfo, &openCallback, &callback);
490 int exitCode = NExitCode::kSuccess;
491 if (callback.CantFindFiles.Size() > 0)
494 stdStream << "WARNINGS for files:" << endl << endl;
495 int numErrors = callback.CantFindFiles.Size();
496 for (int i = 0; i < numErrors; i++)
498 stdStream << callback.CantFindFiles[i] << " : ";
499 stdStream << NError::MyFormatMessageW(callback.CantFindCodes[i]) << endl;
501 stdStream << "----------------" << endl;
502 stdStream << "WARNING: Cannot find " << numErrors << " file";
506 exitCode = NExitCode::kWarning;
512 if (!errorInfo.Message.IsEmpty())
514 message += errorInfo.Message;
517 if (!errorInfo.FileName.IsEmpty())
519 message += errorInfo.FileName;
522 if (!errorInfo.FileName2.IsEmpty())
524 message += errorInfo.FileName2;
527 if (errorInfo.SystemError != 0)
529 message += NError::MyFormatMessageW(errorInfo.SystemError);
532 if (!message.IsEmpty())
533 stdStream << L"\nError:\n" << message;
534 throw CSystemException(result);
536 int numErrors = callback.FailedFiles.Size();
539 if (callback.CantFindFiles.Size() == 0)
540 stdStream << kEverythingIsOk << endl;
545 stdStream << "WARNINGS for files:" << endl << endl;
546 for (int i = 0; i < numErrors; i++)
548 stdStream << callback.FailedFiles[i] << " : ";
549 stdStream << NError::MyFormatMessageW(callback.FailedCodes[i]) << endl;
551 stdStream << "----------------" << endl;
552 stdStream << "WARNING: Cannot open " << numErrors << " file";
556 exitCode = NExitCode::kWarning;
561 PrintHelpAndExit(stdStream);