other-licenses/7zstub/src/7zip/Bundles/SFXSetup-moz/ExtractCallback.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/7zstub/src/7zip/Bundles/SFXSetup-moz/ExtractCallback.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,249 @@
     1.4 +// ExtractCallback.h
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "ExtractCallback.h"
     1.9 +
    1.10 +#include "Common/Wildcard.h"
    1.11 +#include "Common/StringConvert.h"
    1.12 +
    1.13 +#include "Windows/COM.h"
    1.14 +#include "Windows/FileDir.h"
    1.15 +#include "Windows/FileFind.h"
    1.16 +#include "Windows/Time.h"
    1.17 +#include "Windows/Defs.h"
    1.18 +#include "Windows/PropVariant.h"
    1.19 +
    1.20 +#include "Windows/PropVariantConversions.h"
    1.21 +
    1.22 +using namespace NWindows;
    1.23 +using namespace NFile;
    1.24 +
    1.25 +static LPCWSTR kErrorTitle = L"7-Zip";
    1.26 +static LPCWSTR kCantDeleteFile = L"Can not delete output file";
    1.27 +static LPCWSTR kCantOpenFile = L"Can not open output file";
    1.28 +static LPCWSTR kUnsupportedMethod = L"Unsupported Method";
    1.29 +// static LPCWSTR kCRCFailed = L"CRC Failed";
    1.30 +// static LPCWSTR kDataError = L"Data Error";
    1.31 +// static LPCWSTR kUnknownError = L""Unknown Error";
    1.32 +
    1.33 +void CExtractCallbackImp::Init(IInArchive *archiveHandler,
    1.34 +    const UString &directoryPath,   
    1.35 +    const UString &itemDefaultName,
    1.36 +    const FILETIME &utcLastWriteTimeDefault,
    1.37 +    UInt32 attributesDefault)
    1.38 +{
    1.39 +  _message.Empty();
    1.40 +  _isCorrupt = false;
    1.41 +  _itemDefaultName = itemDefaultName;
    1.42 +  _utcLastWriteTimeDefault = utcLastWriteTimeDefault;
    1.43 +  _attributesDefault = attributesDefault;
    1.44 +  _archiveHandler = archiveHandler;
    1.45 +  _directoryPath = directoryPath;
    1.46 +  NName::NormalizeDirPathPrefix(_directoryPath);
    1.47 +}
    1.48 +
    1.49 +STDMETHODIMP CExtractCallbackImp::SetTotal(UInt64 size)
    1.50 +{
    1.51 +  #ifndef _NO_PROGRESS
    1.52 +  ProgressDialog.ProgressSynch.SetProgress(size, 0);
    1.53 +  #endif
    1.54 +  return S_OK;
    1.55 +}
    1.56 +
    1.57 +STDMETHODIMP CExtractCallbackImp::SetCompleted(const UInt64 *completeValue)
    1.58 +{
    1.59 +  #ifndef _NO_PROGRESS
    1.60 +  while(true)
    1.61 +  {
    1.62 +    if(ProgressDialog.ProgressSynch.GetStopped())
    1.63 +      return E_ABORT;
    1.64 +    if(!ProgressDialog.ProgressSynch.GetPaused())
    1.65 +      break;
    1.66 +    ::Sleep(100);
    1.67 +  }
    1.68 +  if (completeValue != NULL)
    1.69 +    ProgressDialog.ProgressSynch.SetPos(*completeValue);
    1.70 +  #endif
    1.71 +  return S_OK;
    1.72 +}
    1.73 +
    1.74 +void CExtractCallbackImp::CreateComplexDirectory(const UStringVector &dirPathParts)
    1.75 +{
    1.76 +  UString fullPath = _directoryPath;
    1.77 +  for(int i = 0; i < dirPathParts.Size(); i++)
    1.78 +  {
    1.79 +    fullPath += dirPathParts[i];
    1.80 +    NDirectory::MyCreateDirectory(fullPath);
    1.81 +    fullPath += NName::kDirDelimiter;
    1.82 +  }
    1.83 +}
    1.84 +
    1.85 +STDMETHODIMP CExtractCallbackImp::GetStream(UInt32 index,
    1.86 +    ISequentialOutStream **outStream, Int32 askExtractMode)
    1.87 +{
    1.88 +  #ifndef _NO_PROGRESS
    1.89 +  if(ProgressDialog.ProgressSynch.GetStopped())
    1.90 +    return E_ABORT;
    1.91 +  #endif
    1.92 +  _outFileStream.Release();
    1.93 +  NCOM::CPropVariant propVariantName;
    1.94 +  RINOK(_archiveHandler->GetProperty(index, kpidPath, &propVariantName));
    1.95 +  UString fullPath;
    1.96 +  if(propVariantName.vt == VT_EMPTY)
    1.97 +    fullPath = _itemDefaultName;
    1.98 +  else 
    1.99 +  {
   1.100 +    if(propVariantName.vt != VT_BSTR)
   1.101 +      return E_FAIL;
   1.102 +    fullPath = propVariantName.bstrVal;
   1.103 +  }
   1.104 +  _filePath = fullPath;
   1.105 +
   1.106 +  // m_CurrentFilePath = GetSystemString(fullPath, _codePage);
   1.107 +  
   1.108 +  if(askExtractMode == NArchive::NExtract::NAskMode::kExtract)
   1.109 +  {
   1.110 +    NCOM::CPropVariant propVariant;
   1.111 +    RINOK(_archiveHandler->GetProperty(index, kpidAttributes, &propVariant));
   1.112 +    if (propVariant.vt == VT_EMPTY)
   1.113 +      _processedFileInfo.Attributes = _attributesDefault;
   1.114 +    else
   1.115 +    {
   1.116 +      if (propVariant.vt != VT_UI4)
   1.117 +        return E_FAIL;
   1.118 +      _processedFileInfo.Attributes = propVariant.ulVal;
   1.119 +    }
   1.120 +
   1.121 +    RINOK(_archiveHandler->GetProperty(index, kpidIsFolder, &propVariant));
   1.122 +    _processedFileInfo.IsDirectory = VARIANT_BOOLToBool(propVariant.boolVal);
   1.123 +
   1.124 +    bool isAnti = false;
   1.125 +    {
   1.126 +      NCOM::CPropVariant propVariantTemp;
   1.127 +      RINOK(_archiveHandler->GetProperty(index, kpidIsAnti, 
   1.128 +          &propVariantTemp));
   1.129 +      if (propVariantTemp.vt == VT_BOOL)
   1.130 +        isAnti = VARIANT_BOOLToBool(propVariantTemp.boolVal);
   1.131 +    }
   1.132 +
   1.133 +    RINOK(_archiveHandler->GetProperty(index, kpidLastWriteTime, &propVariant));
   1.134 +    switch(propVariant.vt)
   1.135 +    {
   1.136 +      case VT_EMPTY:
   1.137 +        _processedFileInfo.UTCLastWriteTime = _utcLastWriteTimeDefault;
   1.138 +        break;
   1.139 +      case VT_FILETIME:
   1.140 +        _processedFileInfo.UTCLastWriteTime = propVariant.filetime;
   1.141 +        break;
   1.142 +      default:
   1.143 +        return E_FAIL;
   1.144 +    }
   1.145 +
   1.146 +    UStringVector pathParts; 
   1.147 +    SplitPathToParts(fullPath, pathParts);
   1.148 +    if(pathParts.IsEmpty())
   1.149 +      return E_FAIL;
   1.150 +
   1.151 +    UString processedPath = fullPath;
   1.152 +
   1.153 +    if(!_processedFileInfo.IsDirectory)
   1.154 +      pathParts.DeleteBack();
   1.155 +    if (!pathParts.IsEmpty())
   1.156 +    {
   1.157 +      if (!isAnti)
   1.158 +        CreateComplexDirectory(pathParts);
   1.159 +    }
   1.160 +
   1.161 +    UString fullProcessedPath = _directoryPath + processedPath;
   1.162 +
   1.163 +    if(_processedFileInfo.IsDirectory)
   1.164 +    {
   1.165 +      _diskFilePath = fullProcessedPath;
   1.166 +
   1.167 +      if (isAnti)
   1.168 +        NDirectory::MyRemoveDirectory(_diskFilePath);
   1.169 +      return S_OK;
   1.170 +    }
   1.171 +
   1.172 +    NFind::CFileInfoW fileInfo;
   1.173 +    if(NFind::FindFile(fullProcessedPath, fileInfo))
   1.174 +    {
   1.175 +      if (!NDirectory::DeleteFileAlways(fullProcessedPath))
   1.176 +      {
   1.177 +        _message = kCantDeleteFile;
   1.178 +        return E_FAIL;
   1.179 +      }
   1.180 +    }
   1.181 +
   1.182 +    if (!isAnti)
   1.183 +    {
   1.184 +      _outFileStreamSpec = new COutFileStream;
   1.185 +      CMyComPtr<ISequentialOutStream> outStreamLoc(_outFileStreamSpec);
   1.186 +      if (!_outFileStreamSpec->Create(fullProcessedPath, true))
   1.187 +      {
   1.188 +        _message = kCantOpenFile;
   1.189 +        return E_FAIL;
   1.190 +      }
   1.191 +      _outFileStream = outStreamLoc;
   1.192 +      *outStream = outStreamLoc.Detach();
   1.193 +    }
   1.194 +    _diskFilePath = fullProcessedPath;
   1.195 +  }
   1.196 +  else
   1.197 +  {
   1.198 +    *outStream = NULL;
   1.199 +  }
   1.200 +  return S_OK;
   1.201 +}
   1.202 +
   1.203 +STDMETHODIMP CExtractCallbackImp::PrepareOperation(Int32 askExtractMode)
   1.204 +{
   1.205 +  _extractMode = false;
   1.206 +  switch (askExtractMode)
   1.207 +  {
   1.208 +    case NArchive::NExtract::NAskMode::kExtract:
   1.209 +      _extractMode = true;
   1.210 +      break;
   1.211 +  };
   1.212 +  return S_OK;
   1.213 +}
   1.214 +
   1.215 +STDMETHODIMP CExtractCallbackImp::SetOperationResult(Int32 resultEOperationResult)
   1.216 +{
   1.217 +  switch(resultEOperationResult)
   1.218 +  {
   1.219 +    case NArchive::NExtract::NOperationResult::kOK:
   1.220 +    {
   1.221 +      break;
   1.222 +    }
   1.223 +    default:
   1.224 +    {
   1.225 +      _outFileStream.Release();
   1.226 +      switch(resultEOperationResult)
   1.227 +      {
   1.228 +        case NArchive::NExtract::NOperationResult::kUnSupportedMethod:
   1.229 +          _message = kUnsupportedMethod;
   1.230 +          break;
   1.231 +        case NArchive::NExtract::NOperationResult::kCRCError:
   1.232 +          _isCorrupt = true;
   1.233 +          // _message = kCRCFailed;
   1.234 +          break;
   1.235 +        case NArchive::NExtract::NOperationResult::kDataError:
   1.236 +          _isCorrupt = true;
   1.237 +          // _message = kDataError;
   1.238 +          break;
   1.239 +        default:
   1.240 +          _isCorrupt = true;
   1.241 +      }
   1.242 +      return E_FAIL;
   1.243 +    }
   1.244 +  }
   1.245 +  if(_outFileStream != NULL)
   1.246 +    _outFileStreamSpec->File.SetLastWriteTime(&_processedFileInfo.UTCLastWriteTime);
   1.247 +  _outFileStream.Release();
   1.248 +  if (_extractMode)
   1.249 +    NDirectory::MySetFileAttributes(_diskFilePath, _processedFileInfo.Attributes);
   1.250 +  return S_OK;
   1.251 +}
   1.252 +

mercurial