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
|
#include "writer.h"
#include <library/cpp/json/json_value.h>
#include <library/cpp/json/json_writer.h>
#include <util/string/join.h>
#include <contrib/libs/libarchive/libarchive/archive.h>
#include <contrib/libs/libarchive/libarchive/archive_entry.h>
using namespace NJson;
namespace NZipatch {
class TWriter::TImpl {
public:
TImpl(const TFsPath& path)
: Actions_(new TJsonValue(JSON_ARRAY))
, Meta_(new TJsonValue(JSON_MAP))
, Revprops_(new TJsonValue(JSON_MAP))
, Archive_(nullptr)
{
Archive_ = archive_write_new();
if (!Archive_) {
ythrow yexception() << "can't create archive object";
}
archive_write_set_format_zip(Archive_);
archive_write_zip_set_compression_deflate(Archive_);
if (ARCHIVE_OK != archive_write_open_filename(Archive_, TString(path).c_str())) {
ythrow yexception() << "can't open archive path = " << path;
}
}
~TImpl() {
if (Actions_ || Meta_ || Revprops_) {
Finish();
}
if (Archive_) {
archive_write_free(Archive_);
}
}
void Finish() {
if (Actions_) {
if (Archive_) {
WriteEntry("actions.json", WriteJson(Actions_.Get(), true, false));
}
Actions_.Destroy();
}
if (Meta_) {
if (Archive_) {
WriteEntry("meta.json", WriteJson(Meta_.Get(), true));
}
Meta_.Destroy();
}
if (Revprops_) {
if (Archive_) {
WriteEntry("revprops.json", WriteJson(Revprops_.Get(), true));
}
Revprops_.Destroy();
}
if (Archive_) {
archive_write_close(Archive_);
}
}
void Copy(const TString& path, const TOrigin& origin) {
Y_ASSERT(origin.Path);
Y_ASSERT(origin.Revision);
if (Actions_) {
TJsonValue item;
item["type"] = "svn_copy";
item["path"] = path;
item["orig_path"] = origin.Path;
item["orig_revision"] = origin.Revision;
Actions_->AppendValue(item);
}
}
void MkDir(const TString& path) {
if (Actions_) {
TJsonValue item;
item["type"] = "mkdir";
item["path"] = path;
Actions_->AppendValue(item);
}
}
void RemoveFile(const TString& path) {
if (Actions_) {
TJsonValue item;
item["type"] = "remove_file";
item["path"] = path;
Actions_->AppendValue(item);
}
}
void RemoveTree(const TString& path) {
if (Actions_) {
TJsonValue item;
item["type"] = "remove_tree";
item["path"] = path;
Actions_->AppendValue(item);
}
}
void StoreFile(
const TString& path,
const TString& data,
const bool execute,
const bool symlink,
const TMaybe<bool> binaryHint,
const TMaybe<bool> encrypted)
{
if (Actions_) {
const TString file = Join("/", "files", path);
TJsonValue item;
item["type"] = "store_file";
item["executable"] = execute;
item["path"] = path;
item["file"] = file;
if (binaryHint.Defined()) {
item["binary_hint"] = *binaryHint;
}
if (encrypted.Defined()) {
item["encrypted"] = *encrypted;
}
Actions_->AppendValue(item);
WriteEntry(file, data, symlink);
}
}
void SetBaseSvnRevision(ui64 revision) {
if (Meta_) {
(*Meta_)["base_svn_revision"] = revision;
}
}
void AddRevprop(const TString& prop, const TString& value) {
if (Revprops_) {
(*Revprops_)[prop] = value;
}
}
private:
void WriteEntry(
const TString& path,
const TString& data,
const bool symlink = false)
{
struct archive_entry* const entry = archive_entry_new();
// Write header.
archive_entry_set_pathname(entry, path.c_str());
archive_entry_set_size(entry, data.size());
archive_entry_set_filetype(entry, symlink ? AE_IFLNK : AE_IFREG);
archive_entry_set_perm(entry, 0644);
if (symlink) {
archive_entry_set_symlink(entry, data.c_str());
}
archive_write_header(Archive_, entry);
// Write data.
// If entry is symlink then entry size become zero.
if (archive_entry_size(entry) > 0) {
archive_write_data(Archive_, data.data(), data.size());
}
archive_entry_free(entry);
}
private:
THolder<NJson::TJsonValue> Actions_;
THolder<NJson::TJsonValue> Meta_;
THolder<NJson::TJsonValue> Revprops_;
struct archive* Archive_;
};
TWriter::TWriter(const TFsPath& path)
: Impl_(new TImpl(path))
{
}
TWriter::~TWriter()
{ }
void TWriter::Finish() {
Impl_->Finish();
}
void TWriter::SetBaseSvnRevision(ui64 revision) {
Impl_->SetBaseSvnRevision(revision);
}
void TWriter::AddRevprop(const TString& prop, const TString& value) {
Impl_->AddRevprop(prop, value);
}
void TWriter::Copy(const TString& path, const TOrigin& origin) {
Impl_->Copy(path, origin);
}
void TWriter::MkDir(const TString& path) {
Impl_->MkDir(path);
}
void TWriter::RemoveFile(const TString& path) {
Impl_->RemoveFile(path);
}
void TWriter::RemoveTree(const TString& path) {
Impl_->RemoveTree(path);
}
void TWriter::StoreFile(
const TString& path,
const TString& data,
const bool execute,
const bool symlink,
const TMaybe<bool> binaryHint,
const TMaybe<bool> encrypted)
{
Impl_->StoreFile(path, data, execute, symlink, binaryHint, encrypted);
}
} // namespace NZipatch
|