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

VerCtrl.cpp « FileManager « UI « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: efed8468d676a8d5ad9f7bf1ac34b82515f5cc3f (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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// VerCtrl.cpp

#include "StdAfx.h"

#include "../../../Common/StringToInt.h"

#include "../../../Windows/FileName.h"
#include "../../../Windows/FileFind.h"

#include "App.h"
#include "RegistryUtils.h"
#include "OverwriteDialog.h"

#include "resource.h"

using namespace NWindows;
using namespace NFile;
using namespace NFind;
using namespace NDir;

static UString ConvertPath_to_Ctrl(const UString &path)
{
  UString s = path;
  s.Replace(L':', L'_');
  return s;
}

struct CFileDataInfo
{
  CByteBuffer Data;
  BY_HANDLE_FILE_INFORMATION Info;
  bool IsOpen;

  CFileDataInfo(): IsOpen (false) {}
  UInt64 GetSize() const { return (((UInt64)Info.nFileSizeHigh) << 32) + Info.nFileSizeLow; }
  bool Read(const UString &path);
};


bool CFileDataInfo::Read(const UString &path)
{
  IsOpen = false;
  NIO::CInFile file;
  if (!file.Open(path))
    return false;
  if (!file.GetFileInformation(&Info))
    return false;

  const UInt64 size = GetSize();
  const size_t size2 = (size_t)size;
  if (size2 != size || size2 > (1 << 28))
  {
    SetLastError(1);
    return false;
  }

  Data.Alloc(size2);

  size_t processedSize;
  if (!file.ReadFull(Data, size2, processedSize))
    return false;
  if (processedSize != size2)
  {
    SetLastError(1);
    return false;
  }
  IsOpen = true;
  return true;
}


static bool CreateComplexDir_for_File(const UString &path)
{
  FString resDirPrefix;
  FString resFileName;
  if (!GetFullPathAndSplit(path, resDirPrefix, resFileName))
    return false;
  return CreateComplexDir(resDirPrefix);
}


static bool ParseNumberString(const FString &s, UInt32 &number)
{
  const wchar_t *end;
  UInt64 result = ConvertStringToUInt64(s, &end);
  if (*end != 0 || s.IsEmpty() || result > (UInt32)0x7FFFFFFF)
    return false;
  number = (UInt32)result;
  return true;
}


static void WriteFile(const FString &path, bool createAlways, const CFileDataInfo &fdi, const CPanel &panel)
{
  NIO::COutFile outFile;
  if (!outFile.Create(path, createAlways)) // (createAlways = false) means CREATE_NEW
  {
    panel.MessageBox_LastError();
    return;
  }
  UInt32 processedSize;
  if (!outFile.Write(fdi.Data, (UInt32)fdi.Data.Size(), processedSize))
  {
    panel.MessageBox_LastError();
    return;
  }
  if (processedSize != fdi.Data.Size())
  {
    panel.MessageBox_Error(L"Write error");
    return;
  }
  if (!outFile.SetTime(
      &fdi.Info.ftCreationTime,
      &fdi.Info.ftLastAccessTime,
      &fdi.Info.ftLastWriteTime))
  {
    panel.MessageBox_LastError();
    return;
  }

  if (!SetFileAttrib(path, fdi.Info.dwFileAttributes))
  {
    panel.MessageBox_LastError();
    return;
  }
}


void CApp::VerCtrl(unsigned id)
{
  const CPanel &panel = GetFocusedPanel();
  
  if (!panel.Is_IO_FS_Folder())
  {
    panel.MessageBox_Error_UnsupportOperation();
    return;
  }

  CRecordVector<UInt32> indices;
  panel.GetSelectedItemsIndices(indices);

  if (indices.Size() != 1)
  {
    // panel.MessageBox_Error_UnsupportOperation();
    return;
  }

  const UString path = panel.GetItemFullPath(indices[0]);

  UString vercPath;
  ReadReg_VerCtrlPath(vercPath);
  if (vercPath.IsEmpty())
    return;
  NName::NormalizeDirPathPrefix(vercPath);

  FString dirPrefix;
  FString fileName;
  if (!GetFullPathAndSplit(path, dirPrefix, fileName))
  {
    panel.MessageBox_LastError();
    return;
  }

  const UString dirPrefix2 = vercPath + ConvertPath_to_Ctrl(dirPrefix);
  const UString path2 = dirPrefix2 + fileName;

  bool sameTime = false;
  bool sameData = false;
  bool areIdentical = false;

  CFileDataInfo fdi, fdi2;
  if (!fdi.Read(path))
  {
    panel.MessageBox_LastError();
    return;
  }

  if (fdi2.Read(path2))
  {
    sameData = (fdi.Data == fdi2.Data);
    sameTime = (CompareFileTime(&fdi.Info.ftLastWriteTime, &fdi2.Info.ftLastWriteTime) == 0);
    areIdentical = (sameData && sameTime);
  }

  const bool isReadOnly = NAttributes::IsReadOnly(fdi.Info.dwFileAttributes);

  if (id == IDM_VER_EDIT)
  {
    if (!isReadOnly)
    {
      panel.MessageBox_Error(L"File is not read-only");
      return;
    }
    
    if (!areIdentical)
    {
      if (fdi2.IsOpen)
      {
        NFind::CEnumerator enumerator;
        FString d2 = dirPrefix2;
        d2 += "_7vc";
        d2.Add_PathSepar();
        d2 += fileName;
        d2.Add_PathSepar();
        enumerator.SetDirPrefix(d2);
        NFind::CDirEntry fi;
        Int32 maxVal = -1;
        while (enumerator.Next(fi))
        {
          UInt32 val;
          if (!ParseNumberString(fi.Name, val))
            continue;
          if ((Int32)val > maxVal)
            maxVal = val;
        }

        UInt32 next = (UInt32)maxVal + 1;
        if (maxVal < 0)
        {
          next = 1;
          if (!::CreateComplexDir_for_File(path2))
          {
            panel.MessageBox_LastError();
            return;
          }
        }

        // we rename old file2 to some name;
        FString path_num = d2;
        {
          AString t;
          t.Add_UInt32((UInt32)next);
          while (t.Len() < 3)
            t.InsertAtFront('0');
          path_num += t;
        }

        if (maxVal < 0)
        {
          if (!::CreateComplexDir_for_File(path_num))
          {
            panel.MessageBox_LastError();
            return;
          }
        }

        if (!NDir::MyMoveFile(path2, path_num))
        {
          panel.MessageBox_LastError();
          return;
        }
      }
      else
      {
        if (!::CreateComplexDir_for_File(path2))
        {
          panel.MessageBox_LastError();
          return;
        }
      }
      /*
      if (!::CopyFile(fs2fas(path), fs2fas(path2), TRUE))
      {
        panel.MessageBox_LastError();
        return;
      }
      */
      WriteFile(path2,
          false,  // (createAlways = false) means CREATE_NEW
          fdi, panel);
    }
      
    if (!SetFileAttrib(path, fdi.Info.dwFileAttributes & ~(DWORD)FILE_ATTRIBUTE_READONLY))
    {
      panel.MessageBox_LastError();
      return;
    }

    return;
  }
  
  if (isReadOnly)
  {
    panel.MessageBox_Error(L"File is read-only");
    return;
  }

  if (id == IDM_VER_COMMIT)
  {
    if (sameData)
    {
      if (!sameTime)
      {
        panel.MessageBox_Error(
          L"Same data, but different timestamps.\n"
          L"Use `Revert` to recover timestamp.");
        return;
      }
    }
    if (!SetFileAttrib(path, fdi.Info.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
    {
      panel.MessageBox_LastError();
      return;
    }
    return;
  }

  if (id == IDM_VER_REVERT)
  {
    if (!fdi2.IsOpen)
    {
      panel.MessageBox_Error(L"No file to revert");
      return;
    }
    if (!sameData || !sameTime)
    {
      if (!sameData)
      {
        /*
        UString m;
        m = "Are you sure you want to revert file ?";
        m.Add_LF();
        m += path;
        if (::MessageBoxW(panel.GetParent(), m, L"Version Control: File Revert", MB_OKCANCEL | MB_ICONQUESTION) != IDOK)
          return;
        */
        COverwriteDialog dialog;
        
        dialog.OldFileInfo.SetTime(&fdi.Info.ftLastWriteTime);
        dialog.OldFileInfo.SetSize(fdi.GetSize());
        dialog.OldFileInfo.Name = path;
        
        dialog.NewFileInfo.SetTime(&fdi2.Info.ftLastWriteTime);
        dialog.NewFileInfo.SetSize(fdi2.GetSize());
        dialog.NewFileInfo.Name = path2;

        dialog.ShowExtraButtons = false;
        dialog.DefaultButton_is_NO = true;
        
        INT_PTR writeAnswer = dialog.Create(panel.GetParent());
        
        if (writeAnswer != IDYES)
          return;
      }
      
      WriteFile(path,
          true,  // (createAlways = true) means CREATE_ALWAYS
          fdi2, panel);
    }
    else
    {
      if (!SetFileAttrib(path, fdi2.Info.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
      {
        panel.MessageBox_LastError();
        return;
      }
    }
    return;
  }

  // if (id == IDM_VER_DIFF)
  {
    if (!fdi2.IsOpen)
      return;
    DiffFiles(path2, path);
  }
}