misc/libphysfs/lzma/CPP/7zip/Archive/7z/7zUpdate.cpp
changeset 12213 bb5522e88ab2
equal deleted inserted replaced
12212:ea891871f481 12213:bb5522e88ab2
       
     1 // UpdateMain.cpp
       
     2 
       
     3 #include "StdAfx.h"
       
     4 
       
     5 #include "7zUpdate.h"
       
     6 #include "7zFolderInStream.h"
       
     7 #include "7zEncode.h"
       
     8 #include "7zHandler.h"
       
     9 #include "7zOut.h"
       
    10 
       
    11 #include "../../Compress/Copy/CopyCoder.h"
       
    12 #include "../../Common/ProgressUtils.h"
       
    13 #include "../../Common/LimitedStreams.h"
       
    14 #include "../../Common/LimitedStreams.h"
       
    15 #include "../Common/ItemNameUtils.h"
       
    16 
       
    17 namespace NArchive {
       
    18 namespace N7z {
       
    19 
       
    20 static const wchar_t *kMatchFinderForBCJ2_LZMA = L"BT2";
       
    21 static const UInt32 kDictionaryForBCJ2_LZMA = 1 << 20;
       
    22 static const UInt32 kAlgorithmForBCJ2_LZMA = 1;
       
    23 static const UInt32 kNumFastBytesForBCJ2_LZMA = 64;
       
    24 
       
    25 static HRESULT WriteRange(IInStream *inStream, ISequentialOutStream *outStream, 
       
    26     UInt64 position, UInt64 size, ICompressProgressInfo *progress)
       
    27 {
       
    28   RINOK(inStream->Seek(position, STREAM_SEEK_SET, 0));
       
    29   CLimitedSequentialInStream *streamSpec = new CLimitedSequentialInStream;
       
    30   CMyComPtr<CLimitedSequentialInStream> inStreamLimited(streamSpec);
       
    31   streamSpec->SetStream(inStream);
       
    32   streamSpec->Init(size);
       
    33 
       
    34   NCompress::CCopyCoder *copyCoderSpec = new NCompress::CCopyCoder;
       
    35   CMyComPtr<ICompressCoder> copyCoder = copyCoderSpec;
       
    36   RINOK(copyCoder->Code(inStreamLimited, outStream, NULL, NULL, progress));
       
    37   return (copyCoderSpec->TotalSize == size ? S_OK : E_FAIL);
       
    38 }
       
    39 
       
    40 static int GetReverseSlashPos(const UString &name)
       
    41 {
       
    42   int slashPos = name.ReverseFind(L'/');
       
    43   #ifdef _WIN32
       
    44   int slash1Pos = name.ReverseFind(L'\\');
       
    45   slashPos = MyMax(slashPos, slash1Pos);
       
    46   #endif
       
    47   return slashPos;
       
    48 }
       
    49 
       
    50 int CUpdateItem::GetExtensionPos() const
       
    51 {
       
    52   int slashPos = GetReverseSlashPos(Name);
       
    53   int dotPos = Name.ReverseFind(L'.');
       
    54   if (dotPos < 0 || (dotPos < slashPos && slashPos >= 0))
       
    55     return Name.Length();
       
    56   return dotPos + 1;
       
    57 }
       
    58 
       
    59 UString CUpdateItem::GetExtension() const
       
    60 {
       
    61   return Name.Mid(GetExtensionPos());
       
    62 }
       
    63 
       
    64 #define RINOZ(x) { int __tt = (x); if (__tt != 0) return __tt; }
       
    65 
       
    66 static int CompareBuffers(const CByteBuffer &a1, const CByteBuffer &a2)
       
    67 {
       
    68   size_t c1 = a1.GetCapacity();
       
    69   size_t c2 = a2.GetCapacity();
       
    70   RINOZ(MyCompare(c1, c2));
       
    71   for (size_t i = 0; i < c1; i++)
       
    72     RINOZ(MyCompare(a1[i], a2[i]));
       
    73   return 0;
       
    74 }
       
    75 
       
    76 static int CompareCoders(const CCoderInfo &c1, const CCoderInfo &c2)
       
    77 {
       
    78   RINOZ(MyCompare(c1.NumInStreams, c2.NumInStreams));
       
    79   RINOZ(MyCompare(c1.NumOutStreams, c2.NumOutStreams));
       
    80   RINOZ(MyCompare(c1.MethodID, c2.MethodID));
       
    81   return CompareBuffers(c1.Properties, c2.Properties);
       
    82 }
       
    83 
       
    84 static int CompareBindPairs(const CBindPair &b1, const CBindPair &b2)
       
    85 {
       
    86   RINOZ(MyCompare(b1.InIndex, b2.InIndex));
       
    87   return MyCompare(b1.OutIndex, b2.OutIndex);
       
    88 }
       
    89 
       
    90 static int CompareFolders(const CFolder &f1, const CFolder &f2)
       
    91 {
       
    92   int s1 = f1.Coders.Size();
       
    93   int s2 = f2.Coders.Size();
       
    94   RINOZ(MyCompare(s1, s2));
       
    95   int i;
       
    96   for (i = 0; i < s1; i++)
       
    97     RINOZ(CompareCoders(f1.Coders[i], f2.Coders[i]));
       
    98   s1 = f1.BindPairs.Size();
       
    99   s2 = f2.BindPairs.Size();
       
   100   RINOZ(MyCompare(s1, s2));
       
   101   for (i = 0; i < s1; i++)
       
   102     RINOZ(CompareBindPairs(f1.BindPairs[i], f2.BindPairs[i]));
       
   103   return 0;
       
   104 }
       
   105 
       
   106 static int CompareFiles(const CFileItem &f1, const CFileItem &f2)
       
   107 {
       
   108   return MyStringCompareNoCase(f1.Name, f2.Name);
       
   109 }
       
   110 
       
   111 static int CompareFolderRefs(const int *p1, const int *p2, void *param)
       
   112 {
       
   113   int i1 = *p1;
       
   114   int i2 = *p2;
       
   115   const CArchiveDatabaseEx &db = *(const CArchiveDatabaseEx *)param;
       
   116   RINOZ(CompareFolders(
       
   117       db.Folders[i1],
       
   118       db.Folders[i2]));
       
   119   RINOZ(MyCompare(
       
   120       db.NumUnPackStreamsVector[i1],
       
   121       db.NumUnPackStreamsVector[i2]));
       
   122   if (db.NumUnPackStreamsVector[i1] == 0)
       
   123     return 0;
       
   124   return CompareFiles(
       
   125       db.Files[db.FolderStartFileIndex[i1]],
       
   126       db.Files[db.FolderStartFileIndex[i2]]);
       
   127 }
       
   128 
       
   129 ////////////////////////////////////////////////////////////
       
   130 
       
   131 static int CompareEmptyItems(const int *p1, const int *p2, void *param)
       
   132 {
       
   133   const CObjectVector<CUpdateItem> &updateItems = *(const CObjectVector<CUpdateItem> *)param;
       
   134   const CUpdateItem &u1 = updateItems[*p1];
       
   135   const CUpdateItem &u2 = updateItems[*p2];
       
   136   if (u1.IsDirectory != u2.IsDirectory)
       
   137     return (u1.IsDirectory) ? 1 : -1;
       
   138   if (u1.IsDirectory)
       
   139   {
       
   140     if (u1.IsAnti != u2.IsAnti)
       
   141       return (u1.IsAnti ? 1 : -1);
       
   142     int n = MyStringCompareNoCase(u1.Name, u2.Name);
       
   143     return -n;
       
   144   }
       
   145   if (u1.IsAnti != u2.IsAnti)
       
   146     return (u1.IsAnti ? 1 : -1);
       
   147   return MyStringCompareNoCase(u1.Name, u2.Name);
       
   148 }
       
   149 
       
   150 static const char *g_Exts = 
       
   151   " lzma 7z ace arc arj bz bz2 deb lzo lzx gz pak rpm sit tgz tbz tbz2 tgz cab ha lha lzh rar zoo" 
       
   152   " zip jar ear war msi"
       
   153   " 3gp avi mov mpeg mpg mpe wmv"
       
   154   " aac ape fla flac la mp3 m4a mp4 ofr ogg pac ra rm rka shn swa tta wv wma wav"
       
   155   " swf "
       
   156   " chm hxi hxs"
       
   157   " gif jpeg jpg jp2 png tiff  bmp ico psd psp"
       
   158   " awg ps eps cgm dxf svg vrml wmf emf ai md"
       
   159   " cad dwg pps key sxi"
       
   160   " max 3ds"
       
   161   " iso bin nrg mdf img pdi tar cpio xpi"
       
   162   " vfd vhd vud vmc vsv"
       
   163   " vmdk dsk nvram vmem vmsd vmsn vmss vmtm"
       
   164   " inl inc idl acf asa h hpp hxx c cpp cxx rc java cs pas bas vb cls ctl frm dlg def" 
       
   165   " f77 f f90 f95"
       
   166   " asm sql manifest dep "
       
   167   " mak clw csproj vcproj sln dsp dsw "
       
   168   " class "
       
   169   " bat cmd"
       
   170   " xml xsd xsl xslt hxk hxc htm html xhtml xht mht mhtml htw asp aspx css cgi jsp shtml"
       
   171   " awk sed hta js php php3 php4 php5 phptml pl pm py pyo rb sh tcl vbs"
       
   172   " text txt tex ans asc srt reg ini doc docx mcw dot rtf hlp xls xlr xlt xlw ppt pdf"
       
   173   " sxc sxd sxi sxg sxw stc sti stw stm odt ott odg otg odp otp ods ots odf"
       
   174   " abw afp cwk lwp wpd wps wpt wrf wri"
       
   175   " abf afm bdf fon mgf otf pcf pfa snf ttf"
       
   176   " dbf mdb nsf ntf wdb db fdb gdb"
       
   177   " exe dll ocx vbx sfx sys tlb awx com obj lib out o so "
       
   178   " pdb pch idb ncb opt";
       
   179 
       
   180 int GetExtIndex(const char *ext)
       
   181 {
       
   182   int extIndex = 1;
       
   183   const char *p = g_Exts;
       
   184   for (;;)
       
   185   {
       
   186     char c = *p++;
       
   187     if (c == 0)
       
   188       return extIndex;
       
   189     if (c == ' ')
       
   190       continue;
       
   191     int pos = 0;
       
   192     for (;;)
       
   193     {
       
   194       char c2 = ext[pos++];
       
   195       if (c2 == 0 && (c == 0 || c == ' '))
       
   196         return extIndex;
       
   197       if (c != c2)
       
   198         break;
       
   199       c = *p++;
       
   200     }
       
   201     extIndex++;
       
   202     for (;;)
       
   203     {
       
   204       if (c == 0)
       
   205         return extIndex;
       
   206       if (c == ' ')
       
   207         break;
       
   208       c = *p++;
       
   209     }
       
   210   }
       
   211 }
       
   212 
       
   213 struct CRefItem
       
   214 {
       
   215   UInt32 Index;
       
   216   const CUpdateItem *UpdateItem;
       
   217   UInt32 ExtensionPos;
       
   218   UInt32 NamePos;
       
   219   int ExtensionIndex;
       
   220   CRefItem(UInt32 index, const CUpdateItem &updateItem, bool sortByType):
       
   221     Index(index),
       
   222     UpdateItem(&updateItem),
       
   223     ExtensionPos(0),
       
   224     NamePos(0),
       
   225     ExtensionIndex(0)
       
   226   {
       
   227     if (sortByType)
       
   228     {
       
   229       int slashPos = GetReverseSlashPos(updateItem.Name);
       
   230       NamePos = ((slashPos >= 0) ? (slashPos + 1) : 0);
       
   231       int dotPos = updateItem.Name.ReverseFind(L'.');
       
   232       if (dotPos < 0 || (dotPos < slashPos && slashPos >= 0))
       
   233         ExtensionPos = updateItem.Name.Length();
       
   234       else 
       
   235       {
       
   236         ExtensionPos = dotPos + 1;
       
   237         UString us = updateItem.Name.Mid(ExtensionPos);
       
   238         if (!us.IsEmpty())
       
   239         {
       
   240           us.MakeLower();
       
   241           int i;
       
   242           AString s;
       
   243           for (i = 0; i < us.Length(); i++)
       
   244           {
       
   245             wchar_t c = us[i];
       
   246             if (c >= 0x80)
       
   247               break;
       
   248             s += (char)c;
       
   249           }
       
   250           if (i == us.Length())
       
   251             ExtensionIndex = GetExtIndex(s);
       
   252           else
       
   253             ExtensionIndex = 0;
       
   254         }
       
   255       }
       
   256     }
       
   257   }
       
   258 };
       
   259 
       
   260 static int CompareUpdateItems(const CRefItem *p1, const CRefItem *p2, void *param)
       
   261 {
       
   262   const CRefItem &a1 = *p1;
       
   263   const CRefItem &a2 = *p2;
       
   264   const CUpdateItem &u1 = *a1.UpdateItem;
       
   265   const CUpdateItem &u2 = *a2.UpdateItem;
       
   266   int n;
       
   267   if (u1.IsDirectory != u2.IsDirectory)
       
   268     return (u1.IsDirectory) ? 1 : -1;
       
   269   if (u1.IsDirectory)
       
   270   {
       
   271     if (u1.IsAnti != u2.IsAnti)
       
   272       return (u1.IsAnti ? 1 : -1);
       
   273     n = MyStringCompareNoCase(u1.Name, u2.Name);
       
   274     return -n;
       
   275   }
       
   276   bool sortByType = *(bool *)param;
       
   277   if (sortByType)
       
   278   {
       
   279     RINOZ(MyCompare(a1.ExtensionIndex, a2.ExtensionIndex))
       
   280     RINOZ(MyStringCompareNoCase(u1.Name + a1.ExtensionPos, u2.Name + a2.ExtensionPos));
       
   281     RINOZ(MyStringCompareNoCase(u1.Name + a1.NamePos, u2.Name + a2.NamePos));
       
   282     if (u1.IsLastWriteTimeDefined && u2.IsLastWriteTimeDefined)
       
   283       RINOZ(CompareFileTime(&u1.LastWriteTime, &u2.LastWriteTime));
       
   284     RINOZ(MyCompare(u1.Size, u2.Size))
       
   285   }
       
   286   return MyStringCompareNoCase(u1.Name, u2.Name);
       
   287 }
       
   288 
       
   289 struct CSolidGroup
       
   290 {
       
   291   CCompressionMethodMode Method;
       
   292   CRecordVector<UInt32> Indices;
       
   293 };
       
   294 
       
   295 static wchar_t *g_ExeExts[] =
       
   296 {
       
   297   L"dll",
       
   298   L"exe",
       
   299   L"ocx",
       
   300   L"sfx",
       
   301   L"sys"
       
   302 };
       
   303 
       
   304 static bool IsExeFile(const UString &ext)
       
   305 {
       
   306   for (int i = 0; i < sizeof(g_ExeExts) / sizeof(g_ExeExts[0]); i++)
       
   307     if (ext.CompareNoCase(g_ExeExts[i]) == 0)
       
   308       return true;
       
   309   return false;
       
   310 }
       
   311 
       
   312 static const UInt64 k_LZMA  = 0x030101;
       
   313 static const UInt64 k_BCJ   = 0x03030103;
       
   314 static const UInt64 k_BCJ2  = 0x0303011B;
       
   315 
       
   316 static bool GetMethodFull(UInt64 methodID, 
       
   317     UInt32 numInStreams, CMethodFull &methodResult)
       
   318 {
       
   319   methodResult.Id = methodID;
       
   320   methodResult.NumInStreams = numInStreams;
       
   321   methodResult.NumOutStreams = 1;
       
   322   return true;
       
   323 }
       
   324 
       
   325 static bool MakeExeMethod(const CCompressionMethodMode &method, 
       
   326     bool bcj2Filter, CCompressionMethodMode &exeMethod)
       
   327 {
       
   328   exeMethod = method;
       
   329   if (bcj2Filter)
       
   330   {
       
   331     CMethodFull methodFull;
       
   332     if (!GetMethodFull(k_BCJ2, 4, methodFull))
       
   333       return false;
       
   334     exeMethod.Methods.Insert(0, methodFull);
       
   335     if (!GetMethodFull(k_LZMA, 1, methodFull))
       
   336       return false;
       
   337     {
       
   338       CProp property;
       
   339       property.Id = NCoderPropID::kAlgorithm;
       
   340       property.Value = kAlgorithmForBCJ2_LZMA;
       
   341       methodFull.Properties.Add(property);
       
   342     }
       
   343     {
       
   344       CProp property;
       
   345       property.Id = NCoderPropID::kMatchFinder;
       
   346       property.Value = kMatchFinderForBCJ2_LZMA;
       
   347       methodFull.Properties.Add(property);
       
   348     }
       
   349     {
       
   350       CProp property;
       
   351       property.Id = NCoderPropID::kDictionarySize;
       
   352       property.Value = kDictionaryForBCJ2_LZMA;
       
   353       methodFull.Properties.Add(property);
       
   354     }
       
   355     {
       
   356       CProp property;
       
   357       property.Id = NCoderPropID::kNumFastBytes;
       
   358       property.Value = kNumFastBytesForBCJ2_LZMA;
       
   359       methodFull.Properties.Add(property);
       
   360     }
       
   361 
       
   362     exeMethod.Methods.Add(methodFull);
       
   363     exeMethod.Methods.Add(methodFull);
       
   364     CBind bind;
       
   365 
       
   366     bind.OutCoder = 0;
       
   367     bind.InStream = 0;
       
   368 
       
   369     bind.InCoder = 1;
       
   370     bind.OutStream = 0;
       
   371     exeMethod.Binds.Add(bind);
       
   372 
       
   373     bind.InCoder = 2;
       
   374     bind.OutStream = 1;
       
   375     exeMethod.Binds.Add(bind);
       
   376 
       
   377     bind.InCoder = 3;
       
   378     bind.OutStream = 2;
       
   379     exeMethod.Binds.Add(bind);
       
   380   }
       
   381   else
       
   382   {
       
   383     CMethodFull methodFull;
       
   384     if (!GetMethodFull(k_BCJ, 1, methodFull))
       
   385       return false;
       
   386     exeMethod.Methods.Insert(0, methodFull);
       
   387     CBind bind;
       
   388     bind.OutCoder = 0;
       
   389     bind.InStream = 0;
       
   390     bind.InCoder = 1;
       
   391     bind.OutStream = 0;
       
   392     exeMethod.Binds.Add(bind);
       
   393   }
       
   394   return true;
       
   395 }   
       
   396 
       
   397 static void SplitFilesToGroups(
       
   398     const CCompressionMethodMode &method, 
       
   399     bool useFilters, bool maxFilter,
       
   400     const CObjectVector<CUpdateItem> &updateItems,
       
   401     CObjectVector<CSolidGroup> &groups)
       
   402 {
       
   403   if (method.Methods.Size() != 1 || method.Binds.Size() != 0)
       
   404     useFilters = false;
       
   405   groups.Clear();
       
   406   groups.Add(CSolidGroup());
       
   407   groups.Add(CSolidGroup());
       
   408   CSolidGroup &generalGroup = groups[0];
       
   409   CSolidGroup &exeGroup = groups[1];
       
   410   generalGroup.Method = method;
       
   411   int i;
       
   412   for (i = 0; i < updateItems.Size(); i++)
       
   413   {
       
   414     const CUpdateItem &updateItem = updateItems[i];
       
   415     if (!updateItem.NewData)
       
   416       continue;
       
   417     if (!updateItem.HasStream())
       
   418       continue;
       
   419     if (useFilters)
       
   420     {
       
   421       const UString name = updateItem.Name;
       
   422       int dotPos = name.ReverseFind(L'.');
       
   423       if (dotPos >= 0)
       
   424       {
       
   425         UString ext = name.Mid(dotPos + 1);
       
   426         if (IsExeFile(ext))
       
   427         {
       
   428           exeGroup.Indices.Add(i);
       
   429           continue;
       
   430         }
       
   431       }
       
   432     }
       
   433     generalGroup.Indices.Add(i);
       
   434   }
       
   435   if (exeGroup.Indices.Size() > 0)
       
   436     if (!MakeExeMethod(method, maxFilter, exeGroup.Method))
       
   437       exeGroup.Method = method;
       
   438   for (i = 0; i < groups.Size();)
       
   439     if (groups[i].Indices.Size() == 0)
       
   440       groups.Delete(i);
       
   441     else
       
   442       i++;
       
   443 }
       
   444 
       
   445 static void FromUpdateItemToFileItem(const CUpdateItem &updateItem, 
       
   446     CFileItem &file)
       
   447 {
       
   448   file.Name = NItemName::MakeLegalName(updateItem.Name);
       
   449   if (updateItem.AttributesAreDefined)
       
   450     file.SetAttributes(updateItem.Attributes);
       
   451   
       
   452   if (updateItem.IsCreationTimeDefined)
       
   453     file.SetCreationTime(updateItem.CreationTime);
       
   454   if (updateItem.IsLastWriteTimeDefined)
       
   455     file.SetLastWriteTime(updateItem.LastWriteTime);
       
   456   if (updateItem.IsLastAccessTimeDefined)
       
   457     file.SetLastAccessTime(updateItem.LastAccessTime);
       
   458   
       
   459   file.UnPackSize = updateItem.Size;
       
   460   file.IsDirectory = updateItem.IsDirectory;
       
   461   file.IsAnti = updateItem.IsAnti;
       
   462   file.HasStream = updateItem.HasStream();
       
   463 }
       
   464 
       
   465 static HRESULT Update2(
       
   466     DECL_EXTERNAL_CODECS_LOC_VARS
       
   467     IInStream *inStream,
       
   468     const CArchiveDatabaseEx *database,
       
   469     const CObjectVector<CUpdateItem> &updateItems,
       
   470     ISequentialOutStream *seqOutStream,
       
   471     IArchiveUpdateCallback *updateCallback,
       
   472     const CUpdateOptions &options)
       
   473 {
       
   474   UInt64 numSolidFiles = options.NumSolidFiles;
       
   475   if (numSolidFiles == 0)
       
   476     numSolidFiles = 1;
       
   477   /*
       
   478   CMyComPtr<IOutStream> outStream;
       
   479   RINOK(seqOutStream->QueryInterface(IID_IOutStream, (void **)&outStream));
       
   480   if (!outStream)
       
   481     return E_NOTIMPL;
       
   482   */
       
   483 
       
   484   UInt64 startBlockSize = database != 0 ? database->ArchiveInfo.StartPosition: 0;
       
   485   if (startBlockSize > 0 && !options.RemoveSfxBlock)
       
   486   {
       
   487     RINOK(WriteRange(inStream, seqOutStream, 0, startBlockSize, NULL));
       
   488   }
       
   489 
       
   490   CRecordVector<int> fileIndexToUpdateIndexMap;
       
   491   if (database != 0)
       
   492   {
       
   493     fileIndexToUpdateIndexMap.Reserve(database->Files.Size());
       
   494     for (int i = 0; i < database->Files.Size(); i++)
       
   495       fileIndexToUpdateIndexMap.Add(-1);
       
   496   }
       
   497   int i;
       
   498   for(i = 0; i < updateItems.Size(); i++)
       
   499   {
       
   500     int index = updateItems[i].IndexInArchive;
       
   501     if (index != -1)
       
   502       fileIndexToUpdateIndexMap[index] = i;
       
   503   }
       
   504 
       
   505   CRecordVector<int> folderRefs;
       
   506   if (database != 0)
       
   507   {
       
   508     for(i = 0; i < database->Folders.Size(); i++)
       
   509     {
       
   510       CNum indexInFolder = 0;
       
   511       CNum numCopyItems = 0;
       
   512       CNum numUnPackStreams = database->NumUnPackStreamsVector[i];
       
   513       for (CNum fileIndex = database->FolderStartFileIndex[i];
       
   514       indexInFolder < numUnPackStreams; fileIndex++)
       
   515       {
       
   516         if (database->Files[fileIndex].HasStream)
       
   517         {
       
   518           indexInFolder++;
       
   519           int updateIndex = fileIndexToUpdateIndexMap[fileIndex];
       
   520           if (updateIndex >= 0)
       
   521             if (!updateItems[updateIndex].NewData)
       
   522               numCopyItems++;
       
   523         }
       
   524       }
       
   525       if (numCopyItems != numUnPackStreams && numCopyItems != 0)
       
   526         return E_NOTIMPL; // It needs repacking !!!
       
   527       if (numCopyItems > 0)
       
   528         folderRefs.Add(i);
       
   529     }
       
   530     folderRefs.Sort(CompareFolderRefs, (void *)database);
       
   531   }
       
   532 
       
   533   CArchiveDatabase newDatabase;
       
   534 
       
   535   ////////////////////////////
       
   536 
       
   537   COutArchive archive;
       
   538   RINOK(archive.Create(seqOutStream, false));
       
   539   RINOK(archive.SkeepPrefixArchiveHeader());
       
   540   UInt64 complexity = 0;
       
   541   for(i = 0; i < folderRefs.Size(); i++)
       
   542     complexity += database->GetFolderFullPackSize(folderRefs[i]);
       
   543   UInt64 inSizeForReduce = 0;
       
   544   for(i = 0; i < updateItems.Size(); i++)
       
   545   {
       
   546     const CUpdateItem &updateItem = updateItems[i];
       
   547     if (updateItem.NewData)
       
   548     {
       
   549       complexity += updateItem.Size;
       
   550       if (numSolidFiles == 1)
       
   551       {
       
   552         if (updateItem.Size > inSizeForReduce)
       
   553           inSizeForReduce = updateItem.Size;
       
   554       }
       
   555       else
       
   556         inSizeForReduce += updateItem.Size;
       
   557     }
       
   558   }
       
   559   RINOK(updateCallback->SetTotal(complexity));
       
   560   complexity = 0;
       
   561   RINOK(updateCallback->SetCompleted(&complexity));
       
   562 
       
   563 
       
   564   CLocalProgress *lps = new CLocalProgress;
       
   565   CMyComPtr<ICompressProgressInfo> progress = lps;
       
   566   lps->Init(updateCallback, true);
       
   567 
       
   568   /////////////////////////////////////////
       
   569   // Write Copy Items
       
   570 
       
   571   for(i = 0; i < folderRefs.Size(); i++)
       
   572   {
       
   573     int folderIndex = folderRefs[i];
       
   574     
       
   575     lps->ProgressOffset = complexity;
       
   576     UInt64 packSize = database->GetFolderFullPackSize(folderIndex);
       
   577     RINOK(WriteRange(inStream, archive.SeqStream,
       
   578         database->GetFolderStreamPos(folderIndex, 0), packSize, progress));
       
   579     complexity += packSize;
       
   580     
       
   581     const CFolder &folder = database->Folders[folderIndex];
       
   582     CNum startIndex = database->FolderStartPackStreamIndex[folderIndex];
       
   583     for (int j = 0; j < folder.PackStreams.Size(); j++)
       
   584     {
       
   585       newDatabase.PackSizes.Add(database->PackSizes[startIndex + j]);
       
   586       // newDatabase.PackCRCsDefined.Add(database.PackCRCsDefined[startIndex + j]);
       
   587       // newDatabase.PackCRCs.Add(database.PackCRCs[startIndex + j]);
       
   588     }
       
   589     newDatabase.Folders.Add(folder);
       
   590 
       
   591     CNum numUnPackStreams = database->NumUnPackStreamsVector[folderIndex];
       
   592     newDatabase.NumUnPackStreamsVector.Add(numUnPackStreams);
       
   593 
       
   594     CNum indexInFolder = 0;
       
   595     for (CNum fi = database->FolderStartFileIndex[folderIndex];
       
   596         indexInFolder < numUnPackStreams; fi++)
       
   597     {
       
   598       CFileItem file = database->Files[fi];
       
   599       if (file.HasStream)
       
   600       {
       
   601         indexInFolder++;
       
   602         int updateIndex = fileIndexToUpdateIndexMap[fi];
       
   603         if (updateIndex >= 0)
       
   604         {
       
   605           const CUpdateItem &updateItem = updateItems[updateIndex];
       
   606           if (updateItem.NewProperties)
       
   607           {
       
   608             CFileItem file2;
       
   609             FromUpdateItemToFileItem(updateItem, file2);
       
   610             file2.UnPackSize = file.UnPackSize;
       
   611             file2.FileCRC = file.FileCRC;
       
   612             file2.IsFileCRCDefined = file.IsFileCRCDefined;
       
   613             file2.HasStream = file.HasStream;
       
   614             file = file2;
       
   615           }
       
   616         }
       
   617         newDatabase.Files.Add(file);
       
   618       }
       
   619     }
       
   620   }
       
   621 
       
   622   /////////////////////////////////////////
       
   623   // Compress New Files
       
   624 
       
   625   CObjectVector<CSolidGroup> groups;
       
   626   SplitFilesToGroups(*options.Method, options.UseFilters, options.MaxFilter, 
       
   627       updateItems, groups);
       
   628 
       
   629   const UInt32 kMinReduceSize = (1 << 16);
       
   630   if (inSizeForReduce < kMinReduceSize)
       
   631     inSizeForReduce = kMinReduceSize;
       
   632 
       
   633   for (int groupIndex = 0; groupIndex < groups.Size(); groupIndex++)
       
   634   {
       
   635     const CSolidGroup &group = groups[groupIndex];
       
   636     int numFiles = group.Indices.Size();
       
   637     if (numFiles == 0)
       
   638       continue;
       
   639     CRecordVector<CRefItem> refItems;
       
   640     refItems.Reserve(numFiles);
       
   641     bool sortByType = (numSolidFiles > 1);
       
   642     for (i = 0; i < numFiles; i++)
       
   643       refItems.Add(CRefItem(group.Indices[i], updateItems[group.Indices[i]], sortByType));
       
   644     refItems.Sort(CompareUpdateItems, (void *)&sortByType);
       
   645     
       
   646     CRecordVector<UInt32> indices;
       
   647     indices.Reserve(numFiles);
       
   648 
       
   649     for (i = 0; i < numFiles; i++)
       
   650     {
       
   651       UInt32 index = refItems[i].Index;
       
   652       indices.Add(index);
       
   653       /*
       
   654       const CUpdateItem &updateItem = updateItems[index];
       
   655       CFileItem file;
       
   656       if (updateItem.NewProperties)
       
   657         FromUpdateItemToFileItem(updateItem, file);
       
   658       else
       
   659         file = database.Files[updateItem.IndexInArchive];
       
   660       if (file.IsAnti || file.IsDirectory)
       
   661         return E_FAIL;
       
   662       newDatabase.Files.Add(file);
       
   663       */
       
   664     }
       
   665     
       
   666     CEncoder encoder(group.Method);
       
   667 
       
   668     for (i = 0; i < numFiles;)
       
   669     {
       
   670       UInt64 totalSize = 0;
       
   671       int numSubFiles;
       
   672       UString prevExtension;
       
   673       for (numSubFiles = 0; i + numSubFiles < numFiles && 
       
   674           numSubFiles < numSolidFiles; numSubFiles++)
       
   675       {
       
   676         const CUpdateItem &updateItem = updateItems[indices[i + numSubFiles]];
       
   677         totalSize += updateItem.Size;
       
   678         if (totalSize > options.NumSolidBytes)
       
   679           break;
       
   680         if (options.SolidExtension)
       
   681         {
       
   682           UString ext = updateItem.GetExtension();
       
   683           if (numSubFiles == 0)
       
   684             prevExtension = ext;
       
   685           else
       
   686             if (ext.CompareNoCase(prevExtension) != 0)
       
   687               break;
       
   688         }
       
   689       }
       
   690       if (numSubFiles < 1)
       
   691         numSubFiles = 1;
       
   692 
       
   693       CFolderInStream *inStreamSpec = new CFolderInStream;
       
   694       CMyComPtr<ISequentialInStream> solidInStream(inStreamSpec);
       
   695       inStreamSpec->Init(updateCallback, &indices[i], numSubFiles);
       
   696       
       
   697       CFolder folderItem;
       
   698 
       
   699       int startPackIndex = newDatabase.PackSizes.Size();
       
   700       RINOK(encoder.Encode(
       
   701           EXTERNAL_CODECS_LOC_VARS
       
   702           solidInStream, NULL, &inSizeForReduce, folderItem, 
       
   703           archive.SeqStream, newDatabase.PackSizes, progress));
       
   704 
       
   705       for (; startPackIndex < newDatabase.PackSizes.Size(); startPackIndex++)
       
   706         lps->OutSize += newDatabase.PackSizes[startPackIndex];
       
   707 
       
   708       lps->InSize += folderItem.GetUnPackSize();
       
   709       // for()
       
   710       // newDatabase.PackCRCsDefined.Add(false);
       
   711       // newDatabase.PackCRCs.Add(0);
       
   712       
       
   713       newDatabase.Folders.Add(folderItem);
       
   714       
       
   715       CNum numUnPackStreams = 0;
       
   716       for (int subIndex = 0; subIndex < numSubFiles; subIndex++)
       
   717       {
       
   718         const CUpdateItem &updateItem = updateItems[indices[i + subIndex]];
       
   719         CFileItem file;
       
   720         if (updateItem.NewProperties)
       
   721           FromUpdateItemToFileItem(updateItem, file);
       
   722         else
       
   723           file = database->Files[updateItem.IndexInArchive];
       
   724         if (file.IsAnti || file.IsDirectory)
       
   725           return E_FAIL;
       
   726         
       
   727         /*
       
   728         CFileItem &file = newDatabase.Files[
       
   729               startFileIndexInDatabase + i + subIndex];
       
   730         */
       
   731         if (!inStreamSpec->Processed[subIndex])
       
   732         {
       
   733           continue;
       
   734           // file.Name += L".locked";
       
   735         }
       
   736 
       
   737         file.FileCRC = inStreamSpec->CRCs[subIndex];
       
   738         file.UnPackSize = inStreamSpec->Sizes[subIndex];
       
   739         if (file.UnPackSize != 0)
       
   740         {
       
   741           file.IsFileCRCDefined = true;
       
   742           file.HasStream = true;
       
   743           numUnPackStreams++;
       
   744         }
       
   745         else
       
   746         {
       
   747           file.IsFileCRCDefined = false;
       
   748           file.HasStream = false;
       
   749         }
       
   750         newDatabase.Files.Add(file);
       
   751       }
       
   752       // numUnPackStreams = 0 is very bad case for locked files
       
   753       // v3.13 doesn't understand it.
       
   754       newDatabase.NumUnPackStreamsVector.Add(numUnPackStreams);
       
   755       i += numSubFiles;
       
   756     }
       
   757   }
       
   758 
       
   759   {
       
   760     /////////////////////////////////////////
       
   761     // Write Empty Files & Folders
       
   762     
       
   763     CRecordVector<int> emptyRefs;
       
   764     for(i = 0; i < updateItems.Size(); i++)
       
   765     {
       
   766       const CUpdateItem &updateItem = updateItems[i];
       
   767       if (updateItem.NewData)
       
   768       {
       
   769         if (updateItem.HasStream())
       
   770           continue;
       
   771       }
       
   772       else
       
   773         if (updateItem.IndexInArchive != -1)
       
   774           if (database->Files[updateItem.IndexInArchive].HasStream)
       
   775             continue;
       
   776       emptyRefs.Add(i);
       
   777     }
       
   778     emptyRefs.Sort(CompareEmptyItems, (void *)&updateItems);
       
   779     for(i = 0; i < emptyRefs.Size(); i++)
       
   780     {
       
   781       const CUpdateItem &updateItem = updateItems[emptyRefs[i]];
       
   782       CFileItem file;
       
   783       if (updateItem.NewProperties)
       
   784         FromUpdateItemToFileItem(updateItem, file);
       
   785       else
       
   786         file = database->Files[updateItem.IndexInArchive];
       
   787       newDatabase.Files.Add(file);
       
   788     }
       
   789   }
       
   790     
       
   791   /*
       
   792   if (newDatabase.Files.Size() != updateItems.Size())
       
   793     return E_FAIL;
       
   794   */
       
   795 
       
   796   return archive.WriteDatabase(EXTERNAL_CODECS_LOC_VARS
       
   797       newDatabase, options.HeaderMethod, options.HeaderOptions);
       
   798 }
       
   799 
       
   800 #ifdef _7Z_VOL
       
   801 
       
   802 static const UInt64 k_Copy = 0x0;
       
   803 
       
   804 static HRESULT WriteVolumeHeader(COutArchive &archive, CFileItem &file, const CUpdateOptions &options)
       
   805 {
       
   806   CCoderInfo coder;
       
   807   coder.NumInStreams = coder.NumOutStreams = 1;
       
   808   coder.MethodID = k_Copy;
       
   809   
       
   810   CFolder folder;
       
   811   folder.Coders.Add(coder);
       
   812   folder.PackStreams.Add(0);
       
   813   
       
   814   CNum numUnPackStreams = 0;
       
   815   if (file.UnPackSize != 0)
       
   816   {
       
   817     file.IsFileCRCDefined = true;
       
   818     file.HasStream = true;
       
   819     numUnPackStreams++;
       
   820   }
       
   821   else
       
   822   {
       
   823     throw 1;
       
   824     file.IsFileCRCDefined = false;
       
   825     file.HasStream = false;
       
   826   }
       
   827   folder.UnPackSizes.Add(file.UnPackSize);
       
   828   
       
   829   CArchiveDatabase newDatabase;
       
   830   newDatabase.Files.Add(file);
       
   831   newDatabase.Folders.Add(folder);
       
   832   newDatabase.NumUnPackStreamsVector.Add(numUnPackStreams);
       
   833   newDatabase.PackSizes.Add(file.UnPackSize);
       
   834   newDatabase.PackCRCsDefined.Add(false);
       
   835   newDatabase.PackCRCs.Add(file.FileCRC);
       
   836   
       
   837   return archive.WriteDatabase(newDatabase, 
       
   838       options.HeaderMethod, 
       
   839       false, 
       
   840       false);
       
   841 }
       
   842 
       
   843 HRESULT UpdateVolume(
       
   844     IInStream *inStream,
       
   845     const CArchiveDatabaseEx *database,
       
   846     CObjectVector<CUpdateItem> &updateItems,
       
   847     ISequentialOutStream *seqOutStream,
       
   848     IArchiveUpdateCallback *updateCallback,
       
   849     const CUpdateOptions &options)
       
   850 {
       
   851   if (updateItems.Size() != 1)
       
   852     return E_NOTIMPL;
       
   853 
       
   854   CMyComPtr<IArchiveUpdateCallback2> volumeCallback;
       
   855   RINOK(updateCallback->QueryInterface(IID_IArchiveUpdateCallback2, (void **)&volumeCallback));
       
   856   if (!volumeCallback)
       
   857     return E_NOTIMPL;
       
   858 
       
   859   CMyComPtr<ISequentialInStream> fileStream;
       
   860   HRESULT result = updateCallback->GetStream(0, &fileStream);
       
   861   if (result != S_OK && result != S_FALSE)
       
   862     return result;
       
   863   if (result == S_FALSE)
       
   864     return E_FAIL;
       
   865   
       
   866   CFileItem file;
       
   867   
       
   868   const CUpdateItem &updateItem = updateItems[0];
       
   869   if (updateItem.NewProperties)
       
   870     FromUpdateItemToFileItem(updateItem, file);
       
   871   else
       
   872     file = database->Files[updateItem.IndexInArchive];
       
   873   if (file.IsAnti || file.IsDirectory)
       
   874     return E_FAIL;
       
   875 
       
   876   UInt64 complexity = 0;
       
   877   file.IsStartPosDefined = true;
       
   878   file.StartPos = 0;
       
   879   for (UInt64 volumeIndex = 0; true; volumeIndex++)
       
   880   { 
       
   881     UInt64 volSize;
       
   882     RINOK(volumeCallback->GetVolumeSize(volumeIndex, &volSize));
       
   883     UInt64 pureSize = COutArchive::GetVolPureSize(volSize, file.Name.Length(), true);
       
   884     CMyComPtr<ISequentialOutStream> volumeStream;
       
   885     RINOK(volumeCallback->GetVolumeStream(volumeIndex, &volumeStream));
       
   886    
       
   887     COutArchive archive;
       
   888     RINOK(archive.Create(volumeStream, true));
       
   889     RINOK(archive.SkeepPrefixArchiveHeader());
       
   890         
       
   891     CSequentialInStreamWithCRC *inCrcStreamSpec = new CSequentialInStreamWithCRC;
       
   892     CMyComPtr<ISequentialInStream> inCrcStream = inCrcStreamSpec;
       
   893     inCrcStreamSpec->Init(fileStream);
       
   894 
       
   895     RINOK(WriteRange(inCrcStream, volumeStream, pureSize, updateCallback, complexity));
       
   896     file.UnPackSize = inCrcStreamSpec->GetSize();
       
   897     if (file.UnPackSize == 0)
       
   898       break;
       
   899     file.FileCRC = inCrcStreamSpec->GetCRC();
       
   900     RINOK(WriteVolumeHeader(archive, file, options));
       
   901     file.StartPos += file.UnPackSize;
       
   902     if (file.UnPackSize < pureSize)
       
   903       break;
       
   904   }
       
   905   return S_OK;
       
   906 }
       
   907 
       
   908 class COutVolumeStream: 
       
   909   public ISequentialOutStream,
       
   910   public CMyUnknownImp
       
   911 {
       
   912   int _volIndex;
       
   913   UInt64 _volSize;
       
   914   UInt64 _curPos;
       
   915   CMyComPtr<ISequentialOutStream> _volumeStream;
       
   916   COutArchive _archive;
       
   917   CCRC _crc;
       
   918 
       
   919 public:
       
   920   MY_UNKNOWN_IMP
       
   921 
       
   922   CFileItem _file;
       
   923   CUpdateOptions _options;
       
   924   CMyComPtr<IArchiveUpdateCallback2> VolumeCallback;
       
   925   void Init(IArchiveUpdateCallback2 *volumeCallback, 
       
   926       const UString &name)  
       
   927   { 
       
   928     _file.Name = name;
       
   929     _file.IsStartPosDefined = true;
       
   930     _file.StartPos = 0;
       
   931     
       
   932     VolumeCallback = volumeCallback;
       
   933     _volIndex = 0;
       
   934     _volSize = 0;
       
   935   }
       
   936   
       
   937   HRESULT Flush();
       
   938   STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize);
       
   939 };
       
   940 
       
   941 HRESULT COutVolumeStream::Flush()
       
   942 {
       
   943   if (_volumeStream)
       
   944   {
       
   945     _file.UnPackSize = _curPos;
       
   946     _file.FileCRC = _crc.GetDigest();
       
   947     RINOK(WriteVolumeHeader(_archive, _file, _options));
       
   948     _archive.Close();
       
   949     _volumeStream.Release();
       
   950     _file.StartPos += _file.UnPackSize;
       
   951   }
       
   952   return S_OK;
       
   953 }
       
   954 
       
   955 STDMETHODIMP COutVolumeStream::Write(const void *data, UInt32 size, UInt32 *processedSize)
       
   956 {
       
   957   if(processedSize != NULL)
       
   958     *processedSize = 0;
       
   959   while(size > 0)
       
   960   {
       
   961     if (!_volumeStream)
       
   962     {
       
   963       RINOK(VolumeCallback->GetVolumeSize(_volIndex, &_volSize));
       
   964       RINOK(VolumeCallback->GetVolumeStream(_volIndex, &_volumeStream));
       
   965       _volIndex++;
       
   966       _curPos = 0;
       
   967       RINOK(_archive.Create(_volumeStream, true));
       
   968       RINOK(_archive.SkeepPrefixArchiveHeader());
       
   969       _crc.Init();
       
   970       continue;
       
   971     }
       
   972     UInt64 pureSize = COutArchive::GetVolPureSize(_volSize, _file.Name.Length());
       
   973     UInt32 curSize = (UInt32)MyMin(UInt64(size), pureSize - _curPos);
       
   974 
       
   975     _crc.Update(data, curSize);
       
   976     UInt32 realProcessed;
       
   977     RINOK(_volumeStream->Write(data, curSize, &realProcessed))
       
   978     data = (void *)((Byte *)data + realProcessed);
       
   979     size -= realProcessed;
       
   980     if(processedSize != NULL)
       
   981       *processedSize += realProcessed;
       
   982     _curPos += realProcessed;
       
   983     if (realProcessed != curSize && realProcessed == 0)
       
   984       return E_FAIL;
       
   985     if (_curPos == pureSize)
       
   986     {
       
   987       RINOK(Flush());
       
   988     }
       
   989   }
       
   990   return S_OK;
       
   991 }
       
   992 
       
   993 #endif
       
   994 
       
   995 HRESULT Update(
       
   996     DECL_EXTERNAL_CODECS_LOC_VARS
       
   997     IInStream *inStream,
       
   998     const CArchiveDatabaseEx *database,
       
   999     const CObjectVector<CUpdateItem> &updateItems,
       
  1000     ISequentialOutStream *seqOutStream,
       
  1001     IArchiveUpdateCallback *updateCallback,
       
  1002     const CUpdateOptions &options)
       
  1003 {
       
  1004   #ifdef _7Z_VOL
       
  1005   if (seqOutStream)
       
  1006   #endif
       
  1007     return Update2(
       
  1008         EXTERNAL_CODECS_LOC_VARS
       
  1009         inStream, database, updateItems,
       
  1010         seqOutStream, updateCallback, options);
       
  1011   #ifdef _7Z_VOL
       
  1012   if (options.VolumeMode)
       
  1013     return UpdateVolume(inStream, database, updateItems,
       
  1014       seqOutStream, updateCallback, options);
       
  1015   COutVolumeStream *volStreamSpec = new COutVolumeStream;
       
  1016   CMyComPtr<ISequentialOutStream> volStream = volStreamSpec;
       
  1017   CMyComPtr<IArchiveUpdateCallback2> volumeCallback;
       
  1018   RINOK(updateCallback->QueryInterface(IID_IArchiveUpdateCallback2, (void **)&volumeCallback));
       
  1019   if (!volumeCallback)
       
  1020     return E_NOTIMPL;
       
  1021   volStreamSpec->Init(volumeCallback, L"a.7z");
       
  1022   volStreamSpec->_options = options;
       
  1023   RINOK(Update2(inStream, database, updateItems,
       
  1024     volStream, updateCallback, options));
       
  1025   return volStreamSpec->Flush();
       
  1026   #endif
       
  1027 }
       
  1028 
       
  1029 }}