-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInMemoryArchiveUpdateCallback.cpp
175 lines (149 loc) · 5.26 KB
/
InMemoryArchiveUpdateCallback.cpp
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
#include "InMemoryArchiveUpdateCallback.h"
#include "InMemoryArchive.h"
#include "InMemoryInStream.h"
#include <iostream>
#include <fmt/format.h>
#include <7zip/CPP/Common/MyWindows.h>
#include "boost/nowide/convert.hpp"
UInt64 getFileTimeFromUnixTime(UInt32 unixTime)
{
static constexpr UInt32 kNumTimeQuantumsInSecond = 10000000;
static constexpr UInt32 kFileTimeStartYear = 1601;
static constexpr UInt32 kUnixTimeStartYear = 1970;
static constexpr UInt64 kUnixTimeOffset =
(UInt64)60 * 60 * 24 * (89 + 365 * (kUnixTimeStartYear - kFileTimeStartYear));
return (kUnixTimeOffset + (UInt64)unixTime) * kNumTimeQuantumsInSecond;
}
InMemoryArchiveUpdateCallback::~InMemoryArchiveUpdateCallback()
{
fmt::print("Cleanup InMemoryArchiveUpdateCallback");
}
InMemoryArchiveUpdateCallback::InMemoryArchiveUpdateCallback(InMemoryArchive* Archive) : Archive(Archive)
{}
Z7_COM7F_IMF(InMemoryArchiveUpdateCallback::SetTotal(UInt64 size))
{
fmt::print("InMemoryArchiveUpdateCallback::SetTotal({})\n", size);
return S_OK;
}
Z7_COM7F_IMF(InMemoryArchiveUpdateCallback::SetCompleted(const UInt64* completeValue))
{
fmt::print("InMemoryArchiveUpdateCallback::SetCompleted({})\n", completeValue != nullptr ? std::to_string(*completeValue) : "nullptr");
return S_OK;
}
Z7_COM7F_IMF(InMemoryArchiveUpdateCallback::GetUpdateItemInfo(UInt32 index,
Int32* newData, Int32* newProperties, UInt32* indexInArchive))
{
// GetUpdateItemInfo outs:
// *newData *newProps
// 0 0 - Copy data and properties from archive
// 0 1 - Copy data from archive, request new properties
// 1 0 - that combination is unused now
// 1 1 - Request new data and new properties. It can be used even for folders
//indexInArchive = -1 if there is no item in archive, or if it doesn't matter.
fmt::print("InMemoryArchiveUpdateCallback::GetUpdateItemInfo({})\n", index);
if (newData)
*newData = 1;
if (newProperties)
*newProperties = 1;
if (indexInArchive)
*indexInArchive = static_cast<UInt32>(-1);
return S_OK;
}
Z7_COM7F_IMF(InMemoryArchiveUpdateCallback::GetProperty(UInt32 index, PROPID propID, PROPVARIANT* value))
{
fmt::print("InMemoryArchiveUpdateCallback::GetProperty({}, {})\n", index, propID);
PropVariantClear(value);
if (index >= Archive->FileSystem.Files.size())
return S_FALSE;
bool IsDirectory = false;
bool IsAnti = false;
const auto& CurrentFile = Archive->FileSystem.Files[index];
switch (propID)
{
case kpidAttrib:
value->vt = VT_UI4;
value->ulVal = IsDirectory ? FILE_ATTRIBUTE_DIRECTORY : 0;
break;
case kpidCTime:
case kpidMTime:
{
const auto ModifiedTime = Archive->FileSystem.getModifiedTime(index);
const auto FileTime64 = getFileTimeFromUnixTime(std::chrono::duration_cast<std::chrono::seconds>(ModifiedTime.time_since_epoch()).count());
value->vt = VT_FILETIME;
value->filetime = {};
value->filetime.dwLowDateTime = (DWORD)FileTime64;
value->filetime.dwHighDateTime = (DWORD)(FileTime64 >> 32);
//FILETIME Filetime;
//GetSystemTimeAsFileTime(&Filetime);
//value->vt = VT_FILETIME;
//value->filetime = Filetime;
break;
}
case kpidHardLink:
value->vt = VT_BOOL;
value->boolVal = VARIANT_FALSE;
break;
case kpidPath:
{
const auto PathW = boost::nowide::widen(CurrentFile.Path);
value->vt = VT_BSTR;
value->bstrVal = ::SysAllocStringLen(PathW.c_str(), PathW.size());
break;
}
case kpidIsDir:
value->vt = VT_BOOL;
value->boolVal = IsDirectory ? VARIANT_TRUE : VARIANT_FALSE;
break;
case kpidIsAnti:
value->vt = VT_BOOL;
value->boolVal = IsAnti ? VARIANT_TRUE : VARIANT_FALSE;
break;
case kpidSize:
value->vt = VT_UI8;
value->uhVal.QuadPart = Archive->FileSystem.getFileSize(index);
break;
case kpidATime: break;
case kpidPosixAttrib: break;
}
return S_OK;
}
Z7_COM7F_IMF(InMemoryArchiveUpdateCallback::GetStream(UInt32 index, ISequentialInStream** inStream))
{
fmt::print("InMemoryArchiveUpdateCallback::GetStream({})\n", index);
*inStream = nullptr;
if (index >= Archive->FileSystem.Files.size())
return S_FALSE;
CMyComPtr<ISequentialInStream> InStream(new InMemoryInStream(Archive->FileSystem.getFileView(index)));
*inStream = InStream.Detach();
return S_OK;
}
Z7_COM7F_IMF(InMemoryArchiveUpdateCallback::SetOperationResult(Int32 operationResult))
{
fmt::print("InMemoryArchiveUpdateCallback::SetOperationResult({})\n", operationResult);
return S_OK;
}
Z7_COM7F_IMF(InMemoryArchiveUpdateCallback::GetVolumeSize(UInt32 index, UInt64* size))
{
*size = 1u << 31;
fmt::print("InMemoryArchiveUpdateCallback::GetVolumeSize({})\n", index);
return S_OK;
}
Z7_COM7F_IMF(InMemoryArchiveUpdateCallback::GetVolumeStream(UInt32 index, ISequentialOutStream** volumeStream))
{
*volumeStream = nullptr;
fmt::print("InMemoryArchiveUpdateCallback::GetVolumeStream({})\n", index);
return S_OK;
}
Z7_COM7F_IMF(InMemoryArchiveUpdateCallback::CryptoGetTextPassword2(Int32* passwordIsDefined, BSTR* password))
{
fmt::print("InMemoryArchiveUpdateCallback::CryptoGetTextPassword2()\n");
const bool PasswordIsDefined = !Archive->Password.empty();
if (PasswordIsDefined && password != nullptr)
{
const auto PasswordW = boost::nowide::widen(Archive->Password);
*password = ::SysAllocStringLen(PasswordW.c_str(), PasswordW.size());
}
if (passwordIsDefined)
*passwordIsDefined = PasswordIsDefined;
return S_OK;
}