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
|
#include "reader.h"
#include <library/cpp/json/json_reader.h>
#include <library/cpp/json/json_value.h>
#include <util/generic/hash.h>
#include <util/memory/tempbuf.h>
#include <contrib/libs/libarchive/libarchive/archive.h>
#include <contrib/libs/libarchive/libarchive/archive_entry.h>
using namespace NJson;
namespace NZipatch {
class TReader::TImpl {
using TEntry = archive_entry;
public:
TImpl() {
if ((Archive_ = archive_read_new()) == nullptr) {
ythrow yexception() << "can't create archive object";
}
}
TImpl(const TFsPath& path)
: TImpl()
{
archive_read_support_filter_all(Archive_);
archive_read_support_format_zip(Archive_);
if (ARCHIVE_OK != archive_read_open_filename(Archive_, TString(path).c_str(), 10240)) {
ythrow yexception() << "can't open archive path = " << path;
}
Read();
}
TImpl(const TStringBuf buf)
: TImpl()
{
archive_read_support_filter_all(Archive_);
archive_read_support_format_zip(Archive_);
if (ARCHIVE_OK != archive_read_open_memory(Archive_, buf.data(), buf.size())) {
ythrow yexception() << "can't open in-memory archive";
}
Read();
}
~TImpl() {
for (const auto& item : Files_) {
archive_entry_free(item.second.first);
}
if (Archive_) {
archive_read_free(Archive_);
}
}
void Enumerate(TOnEvent cb) const {
for (const auto& item : Actions_) {
TEvent event;
event.Action = GetTypeFromString(item["type"].GetStringSafe(TString()));
event.Path = item["path"].GetStringSafe(TString());
event.Executable = item["executable"].GetBooleanSafe(false);
event.Symlink = false;
if (event.Action == Copy || event.Action == Move) {
event.Source.Path = item["orig_path"].GetStringSafe(TString());
event.Source.Revision = item["orig_revision"].GetUIntegerRobust();
}
if (event.Action == StoreFile) {
auto fi = Files_.find(event.Path);
if (fi == Files_.end()) {
ythrow yexception() << "can't find file; path = " << event.Path;
}
event.Data = fi->second.second;
event.Symlink = archive_entry_filetype(fi->second.first) == AE_IFLNK;
}
if (event.Path) {
cb(event);
}
}
}
private:
EAction GetTypeFromString(const TString& type) const {
if (type == "store_file") {
return StoreFile;
}
if (type == "mkdir") {
return MkDir;
}
if (type == "remove_file" || type == "remove_tree") {
return Remove;
}
if (type == "svn_copy") {
return Copy;
}
return Unknown;
}
void Read() {
TEntry* current = nullptr;
while (archive_read_next_header(Archive_, ¤t) == ARCHIVE_OK) {
const TStringBuf path(archive_entry_pathname(current));
if (path == "actions.json") {
TJsonValue value;
ReadJsonFastTree(GetData(current), &value, true);
for (const auto& item : value.GetArraySafe()) {
Actions_.push_back(item);
}
} else if (AsciiHasPrefix(path, "files/")) {
TEntry* entry = archive_entry_clone(current);
Files_.emplace(path.substr(6), std::make_pair(entry, GetData(current)));
}
}
archive_read_close(Archive_);
}
TString GetData(TEntry* current) const {
if (archive_entry_filetype(current) == AE_IFLNK) {
return archive_entry_symlink(current);
}
if (const auto size = archive_entry_size(current)) {
TTempBuf data(size);
if (archive_read_data(Archive_, data.Data(), size) != size) {
ythrow yexception() << "can't read entry";
}
return TString(data.Data(), size);
}
return TString();
}
private:
struct archive* Archive_;
TVector<TJsonValue> Actions_;
THashMap<TString, std::pair<TEntry*, TString>> Files_;
};
TReader::TReader(const TFsPath& path)
: Impl_(new TImpl(path))
{
}
TReader::TReader(const TStringBuf buf)
: Impl_(new TImpl(buf))
{
}
TReader::~TReader()
{ }
void TReader::Enumerate(TOnEvent cb) const {
Impl_->Enumerate(cb);
}
} // namespace NZipatch
|