Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ExtractEngine.cpp « Far « UI « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 04bad72405db9f5abec8706d5e5f14aefa3d15de (plain)
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
// ExtractEngine.h

#include "StdAfx.h"

#ifndef _7ZIP_ST
#include "../../../Windows/Synchronization.h"
#endif

#include "../../../Common/StringConvert.h"

#include "ExtractEngine.h"
#include "FarUtils.h"
#include "Messages.h"
#include "OverwriteDialogFar.h"

using namespace NWindows;
using namespace NFar;

#ifndef _7ZIP_ST
static NSynchronization::CCriticalSection g_CriticalSection;
#define MT_LOCK NSynchronization::CCriticalSectionLock lock(g_CriticalSection);
#else
#define MT_LOCK
#endif


static HRESULT CheckBreak2()
{
  return WasEscPressed() ? E_ABORT : S_OK;
}

extern void PrintMessage(const char *message);

CExtractCallbackImp::~CExtractCallbackImp()
{
}

void CExtractCallbackImp::Init(
    UINT codePage,
    CProgressBox *progressBox,
    bool passwordIsDefined,
    const UString &password)
{
  m_PasswordIsDefined = passwordIsDefined;
  m_Password = password;
  m_CodePage = codePage;
  _percent = progressBox;
}

STDMETHODIMP CExtractCallbackImp::SetTotal(UInt64 size)
{
  MT_LOCK

  if (_percent)
  {
    _percent->Total = size;
    _percent->Print();
  }
  return CheckBreak2();
}

STDMETHODIMP CExtractCallbackImp::SetCompleted(const UInt64 *completeValue)
{
  MT_LOCK

  if (_percent)
  {
    if (completeValue)
      _percent->Completed = *completeValue;
    _percent->Print();
  }
  return CheckBreak2();
}

STDMETHODIMP CExtractCallbackImp::AskOverwrite(
    const wchar_t *existName, const FILETIME *existTime, const UInt64 *existSize,
    const wchar_t *newName, const FILETIME *newTime, const UInt64 *newSize,
    Int32 *answer)
{
  MT_LOCK

  NOverwriteDialog::CFileInfo oldFileInfo, newFileInfo;
  oldFileInfo.TimeIsDefined = (existTime != 0);
  if (oldFileInfo.TimeIsDefined)
    oldFileInfo.Time = *existTime;
  oldFileInfo.SizeIsDefined = (existSize != NULL);
  if (oldFileInfo.SizeIsDefined)
    oldFileInfo.Size = *existSize;
  oldFileInfo.Name = existName;

  newFileInfo.TimeIsDefined = (newTime != 0);
  if (newFileInfo.TimeIsDefined)
    newFileInfo.Time = *newTime;
  newFileInfo.SizeIsDefined = (newSize != NULL);
  if (newFileInfo.SizeIsDefined)
    newFileInfo.Size = *newSize;
  newFileInfo.Name = newName;
  
  NOverwriteDialog::NResult::EEnum result =
    NOverwriteDialog::Execute(oldFileInfo, newFileInfo);
  
  switch (result)
  {
  case NOverwriteDialog::NResult::kCancel:
    // *answer = NOverwriteAnswer::kCancel;
    // break;
    return E_ABORT;
  case NOverwriteDialog::NResult::kNo:
    *answer = NOverwriteAnswer::kNo;
    break;
  case NOverwriteDialog::NResult::kNoToAll:
    *answer = NOverwriteAnswer::kNoToAll;
    break;
  case NOverwriteDialog::NResult::kYesToAll:
    *answer = NOverwriteAnswer::kYesToAll;
    break;
  case NOverwriteDialog::NResult::kYes:
    *answer = NOverwriteAnswer::kYes;
    break;
  case NOverwriteDialog::NResult::kAutoRename:
    *answer = NOverwriteAnswer::kAutoRename;
    break;
  default:
    return E_FAIL;
  }
  
  return CheckBreak2();
}

static const char * const kTestString    =  "Testing";
static const char * const kExtractString =  "Extracting";
static const char * const kSkipString    =  "Skipping";

STDMETHODIMP CExtractCallbackImp::PrepareOperation(const wchar_t *name, Int32 /* isFolder */, Int32 askExtractMode, const UInt64 * /* position */)
{
  MT_LOCK

  m_CurrentFilePath = name;
  const char *s;

  switch (askExtractMode)
  {
    case NArchive::NExtract::NAskMode::kExtract: s = kExtractString; break;
    case NArchive::NExtract::NAskMode::kTest:    s = kTestString; break;
    case NArchive::NExtract::NAskMode::kSkip:    s = kSkipString; break;
    default: s = "???"; // return E_FAIL;
  };

  if (_percent)
  {
    _percent->Command = s;
    _percent->FileName = name;
    _percent->Print();
  }

  return CheckBreak2();
}

STDMETHODIMP CExtractCallbackImp::MessageError(const wchar_t *message)
{
  MT_LOCK

  AString s (UnicodeStringToMultiByte(message, CP_OEMCP));
  if (g_StartupInfo.ShowErrorMessage((const char *)s) == -1)
    return E_ABORT;

  return CheckBreak2();
}

void SetExtractErrorMessage(Int32 opRes, Int32 encrypted, AString &s)
{
  s.Empty();

  switch (opRes)
  {
    case NArchive::NExtract::NOperationResult::kOK:
      return;
    default:
    {
      UINT messageID = 0;
      switch (opRes)
      {
        case NArchive::NExtract::NOperationResult::kUnsupportedMethod:
          messageID = NMessageID::kExtractUnsupportedMethod;
          break;
        case NArchive::NExtract::NOperationResult::kCRCError:
          messageID = encrypted ?
            NMessageID::kExtractCRCFailedEncrypted :
            NMessageID::kExtractCRCFailed;
          break;
        case NArchive::NExtract::NOperationResult::kDataError:
          messageID = encrypted ?
            NMessageID::kExtractDataErrorEncrypted :
            NMessageID::kExtractDataError;
          break;
      }
      if (messageID != 0)
      {
        s = g_StartupInfo.GetMsgString(messageID);
        s.Replace((AString)" '%s'", AString());
      }
      else if (opRes == NArchive::NExtract::NOperationResult::kUnavailable)
        s = "Unavailable data";
      else if (opRes == NArchive::NExtract::NOperationResult::kUnexpectedEnd)
        s = "Unexpected end of data";
      else if (opRes == NArchive::NExtract::NOperationResult::kDataAfterEnd)
        s = "There are some data after the end of the payload data";
      else if (opRes == NArchive::NExtract::NOperationResult::kIsNotArc)
        s = "Is not archive";
      else if (opRes == NArchive::NExtract::NOperationResult::kHeadersError)
        s = "kHeaders Error";
      else
      {
        s = "Error #";
        s.Add_UInt32(opRes);
      }
    }
  }
}

STDMETHODIMP CExtractCallbackImp::SetOperationResult(Int32 opRes, Int32 encrypted)
{
  MT_LOCK

  if (opRes == NArchive::NExtract::NOperationResult::kOK)
  {
    if (_percent)
    {
      _percent->Command.Empty();
      _percent->FileName.Empty();
      _percent->Files++;
    }
  }
  else
  {
    AString s;
    SetExtractErrorMessage(opRes, encrypted, s);
    if (PrintErrorMessage(s, m_CurrentFilePath) == -1)
      return E_ABORT;
  }
  
  return CheckBreak2();
}


STDMETHODIMP CExtractCallbackImp::ReportExtractResult(Int32 opRes, Int32 encrypted, const wchar_t *name)
{
  MT_LOCK

  if (opRes != NArchive::NExtract::NOperationResult::kOK)
  {
    AString s;
    SetExtractErrorMessage(opRes, encrypted, s);
    if (PrintErrorMessage(s, name) == -1)
      return E_ABORT;
  }
  
  return CheckBreak2();
}

extern HRESULT GetPassword(UString &password);

STDMETHODIMP CExtractCallbackImp::CryptoGetTextPassword(BSTR *password)
{
  MT_LOCK

  if (!m_PasswordIsDefined)
  {
    RINOK(GetPassword(m_Password));
    m_PasswordIsDefined = true;
  }
  return StringToBstr(m_Password, password);
}