summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/lzma/CPP/7zip/UI/Common/UpdatePair.cpp
blob: 99b0aafb28157740aad2418250b1d58790d171c2 (plain) (blame)
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
// UpdatePair.cpp

#include "StdAfx.h"

#include <time.h>
// #include <stdio.h>

#include "../../../Common/Wildcard.h"

#include "../../../Windows/TimeUtils.h"

#include "SortUtils.h"
#include "UpdatePair.h"

using namespace NWindows;
using namespace NTime;


/*
  a2.Prec =
  {
    0 (k_PropVar_TimePrec_0):
       if GetProperty(kpidMTime) returned 0 and
          GetProperty(kpidTimeType) did not returned VT_UI4.
       7z, wim, tar in 7-Zip before v21)
    in that case we use
      (prec) that is set by IOutArchive::GetFileTimeType()
  }
*/
       
static int MyCompareTime(unsigned prec, const CFiTime &f1, const CArcTime &a2)
{
  // except of precision, we also have limitation, when timestamp is out of range

  /* if (Prec) in archive item is defined, then use global (prec) */
  if (a2.Prec != k_PropVar_TimePrec_0)
    prec = a2.Prec;

  CArcTime a1;
  a1.Set_From_FiTime(f1);
  /* Set_From_FiTime() must set full form precision:
     k_PropVar_TimePrec_Base + numDigits
     windows: 7 digits, non-windows: 9 digits */

  if (prec == k_PropVar_TimePrec_DOS)
  {
    const UInt32 dosTime1 = a1.Get_DosTime();
    const UInt32 dosTime2 = a2.Get_DosTime();
    return MyCompare(dosTime1, dosTime2);
  }

  if (prec == k_PropVar_TimePrec_Unix)
  {
    const Int64 u2 = FileTime_To_UnixTime64(a2.FT);
    if (u2 == 0 || u2 == (UInt32)0xFFFFFFFF)
    {
      // timestamp probably was saturated in archive to 32-bit
      // so we use saturated 32-bit value for disk file too.
      UInt32 u1;
      FileTime_To_UnixTime(a1.FT, u1);
      const UInt32 u2_32 = (UInt32)u2;
      return MyCompare(u1, u2_32);
    }

    const Int64 u1 = FileTime_To_UnixTime64(a1.FT);
    return MyCompare(u1, u2);
    // prec = k_PropVar_TimePrec_Base; // for debug
  }

  if (prec == k_PropVar_TimePrec_0)
    prec = k_PropVar_TimePrec_Base + 7;
  else if (prec == k_PropVar_TimePrec_HighPrec)
    prec = k_PropVar_TimePrec_Base + 9;
  else if (prec < k_PropVar_TimePrec_Base)
    prec = k_PropVar_TimePrec_Base;
  else if (prec > k_PropVar_TimePrec_Base + 9)
    prec = k_PropVar_TimePrec_Base + 7;

  // prec now is full form: k_PropVar_TimePrec_Base + numDigits;
  if (prec > a1.Prec && a1.Prec >= k_PropVar_TimePrec_Base)
    prec = a1.Prec;

  const unsigned numDigits = prec - k_PropVar_TimePrec_Base;
  if (numDigits >= 7)
  {
    const int comp = CompareFileTime(&a1.FT, &a2.FT);
    if (comp != 0 || numDigits == 7)
      return comp;
    return MyCompare(a1.Ns100, a2.Ns100);
  }
  UInt32 d = 1;
  for (unsigned k = numDigits; k < 7; k++)
    d *= 10;
  const UInt64 v1 = a1.Get_FILETIME_as_UInt64() / d * d;
  const UInt64 v2 = a2.Get_FILETIME_as_UInt64() / d * d;
  // printf("\ndelta=%d numDigits=%d\n", (unsigned)(v1- v2), numDigits);
  return MyCompare(v1, v2);
}



static const char * const k_Duplicate_inArc_Message = "Duplicate filename in archive:";
static const char * const k_Duplicate_inDir_Message = "Duplicate filename on disk:";
static const char * const k_NotCensoredCollision_Message = "Internal file name collision (file on disk, file in archive):";

Z7_ATTR_NORETURN
static
void ThrowError(const char *message, const UString &s1, const UString &s2)
{
  UString m (message);
  m.Add_LF(); m += s1;
  m.Add_LF(); m += s2;
  throw m;
}

static int CompareArcItemsBase(const CArcItem &ai1, const CArcItem &ai2)
{
  const int res = CompareFileNames(ai1.Name, ai2.Name);
  if (res != 0)
    return res;
  if (ai1.IsDir != ai2.IsDir)
    return ai1.IsDir ? -1 : 1;
  return 0;
}

static int CompareArcItems(const unsigned *p1, const unsigned *p2, void *param)
{
  const unsigned i1 = *p1;
  const unsigned i2 = *p2;
  const CObjectVector<CArcItem> &arcItems = *(const CObjectVector<CArcItem> *)param;
  const int res = CompareArcItemsBase(arcItems[i1], arcItems[i2]);
  if (res != 0)
    return res;
  return MyCompare(i1, i2);
}

void GetUpdatePairInfoList(
    const CDirItems &dirItems,
    const CObjectVector<CArcItem> &arcItems,
    NFileTimeType::EEnum fileTimeType,
    CRecordVector<CUpdatePair> &updatePairs)
{
  CUIntVector dirIndices, arcIndices;
  
  const unsigned numDirItems = dirItems.Items.Size();
  const unsigned numArcItems = arcItems.Size();
  
  CIntArr duplicatedArcItem(numArcItems);
  {
    int *vals = &duplicatedArcItem[0];
    for (unsigned i = 0; i < numArcItems; i++)
      vals[i] = 0;
  }

  {
    arcIndices.ClearAndSetSize(numArcItems);
    if (numArcItems != 0)
    {
      unsigned *vals = &arcIndices[0];
      for (unsigned i = 0; i < numArcItems; i++)
        vals[i] = i;
    }
    arcIndices.Sort(CompareArcItems, (void *)&arcItems);
    for (unsigned i = 0; i + 1 < numArcItems; i++)
      if (CompareArcItemsBase(
          arcItems[arcIndices[i]],
          arcItems[arcIndices[i + 1]]) == 0)
      {
        duplicatedArcItem[i] = 1;
        duplicatedArcItem[i + 1] = -1;
      }
  }

  UStringVector dirNames;
  {
    dirNames.ClearAndReserve(numDirItems);
    unsigned i;
    for (i = 0; i < numDirItems; i++)
      dirNames.AddInReserved(dirItems.GetLogPath(i));
    SortFileNames(dirNames, dirIndices);
    for (i = 0; i + 1 < numDirItems; i++)
    {
      const UString &s1 = dirNames[dirIndices[i]];
      const UString &s2 = dirNames[dirIndices[i + 1]];
      if (CompareFileNames(s1, s2) == 0)
        ThrowError(k_Duplicate_inDir_Message, s1, s2);
    }
  }
  
  unsigned dirIndex = 0;
  unsigned arcIndex = 0;

  int prevHostFile = -1;
  const UString *prevHostName = NULL;
  
  while (dirIndex < numDirItems || arcIndex < numArcItems)
  {
    CUpdatePair pair;
    
    int dirIndex2 = -1;
    int arcIndex2 = -1;
    const CDirItem *di = NULL;
    const CArcItem *ai = NULL;
    
    int compareResult = -1;
    const UString *name = NULL;
    
    if (dirIndex < numDirItems)
    {
      dirIndex2 = (int)dirIndices[dirIndex];
      di = &dirItems.Items[(unsigned)dirIndex2];
    }

    if (arcIndex < numArcItems)
    {
      arcIndex2 = (int)arcIndices[arcIndex];
      ai = &arcItems[(unsigned)arcIndex2];
      compareResult = 1;
      if (dirIndex < numDirItems)
      {
        compareResult = CompareFileNames(dirNames[(unsigned)dirIndex2], ai->Name);
        if (compareResult == 0)
        {
          if (di->IsDir() != ai->IsDir)
            compareResult = (ai->IsDir ? 1 : -1);
        }
      }
    }
    
    if (compareResult < 0)
    {
      name = &dirNames[(unsigned)dirIndex2];
      pair.State = NUpdateArchive::NPairState::kOnlyOnDisk;
      pair.DirIndex = dirIndex2;
      dirIndex++;
    }
    else if (compareResult > 0)
    {
      name = &ai->Name;
      pair.State = ai->Censored ?
          NUpdateArchive::NPairState::kOnlyInArchive:
          NUpdateArchive::NPairState::kNotMasked;
      pair.ArcIndex = arcIndex2;
      arcIndex++;
    }
    else
    {
      const int dupl = duplicatedArcItem[arcIndex];
      if (dupl != 0)
        ThrowError(k_Duplicate_inArc_Message, ai->Name, arcItems[arcIndices[(unsigned)((int)arcIndex + dupl)]].Name);

      name = &dirNames[(unsigned)dirIndex2];
      if (!ai->Censored)
        ThrowError(k_NotCensoredCollision_Message, *name, ai->Name);
      
      pair.DirIndex = dirIndex2;
      pair.ArcIndex = arcIndex2;

      int compResult = 0;
      if (ai->MTime.Def)
      {
        compResult = MyCompareTime((unsigned)fileTimeType, di->MTime, ai->MTime);
      }
      switch (compResult)
      {
        case -1: pair.State = NUpdateArchive::NPairState::kNewInArchive; break;
        case  1: pair.State = NUpdateArchive::NPairState::kOldInArchive; break;
        default:
          pair.State = (ai->Size_Defined && di->Size == ai->Size) ?
              NUpdateArchive::NPairState::kSameFiles :
              NUpdateArchive::NPairState::kUnknowNewerFiles;
      }
      
      dirIndex++;
      arcIndex++;
    }
    
    if (
       #ifdef _WIN32
        (di && di->IsAltStream) ||
       #endif
        (ai && ai->IsAltStream))
    {
      if (prevHostName)
      {
        const unsigned hostLen = prevHostName->Len();
        if (name->Len() > hostLen)
          if ((*name)[hostLen] == ':' && CompareFileNames(*prevHostName, name->Left(hostLen)) == 0)
            pair.HostIndex = prevHostFile;
      }
    }
    else
    {
      prevHostFile = (int)updatePairs.Size();
      prevHostName = name;
    }
    
    updatePairs.Add(pair);
  }

  updatePairs.ReserveDown();
}