-
Notifications
You must be signed in to change notification settings - Fork 0
/
div.cpp
321 lines (298 loc) · 9.48 KB
/
div.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
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
#include <memory>
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <archive.h>
#include <archive_entry.h>
#include <json/json.h>
typedef std::pair<std::shared_ptr<uint8_t>, size_t> safe_array_t;
struct docker_image_manifest
{
std::string config;
std::vector<std::string> repoTags;
std::vector<std::string> layer;
};
docker_image_manifest parseManifestFile(safe_array_t data)
{
docker_image_manifest dim;
Json::Value root;
std::stringstream ss;
ss << data.first;
ss >> root;
auto manifest = root.get(Json::Value::ArrayIndex(0), Json::Value());
std::string config = manifest.get("Config", "").asString();
dim.config = config;
std::cerr << "Config: " << config << std::endl;
auto repoTags = manifest.get("RepoTags", Json::Value());
for (size_t i = 0; i < repoTags.size(); i++)
{
std::string repoTag = repoTags.get(Json::Value::ArrayIndex(i), Json::Value()).asString();
dim.repoTags.push_back(repoTag);
std::cerr << "RepoTag: " << repoTag << std::endl;
}
auto layers = manifest.get("Layers", Json::Value());
for (size_t i = 0; i < layers.size(); i++)
{
std::string layer = layers.get(Json::Value::ArrayIndex(i), Json::Value()).asString();
dim.layer.push_back(layer);
std::cerr << "Layer: " << layer << std::endl;
}
return dim;
}
std::vector<std::string> parseConfigJsonFile(safe_array_t data)
{
std::vector<std::string> r;
docker_image_manifest dim;
Json::Value root;
std::stringstream ss;
ss << data.first;
ss >> root;
auto cc = root.get("history", Json::Value());
for (size_t i = 0; i < cc.size(); i++)
{
auto obj = cc.get(i, Json::Value());
std::string cmd = obj.get("created_by", "").asString();
bool empty = obj.get("empty_layer", false).asBool();
if (!empty)
r.push_back(cmd);
std::cerr << "Cmd: " << cmd << std::endl;
}
return r;
}
void parseLayerJsonFile(safe_array_t data)
{
docker_image_manifest dim;
Json::Value root;
std::stringstream ss;
ss << data.first;
ss >> root;
auto cc = root.get("container_config", Json::Value());
auto cmda = cc.get("Cmd", Json::Value());
std::string cmd;
for (size_t i = 0; i < cmda.size(); i++)
{
if (i > 0)
cmd += " ";
cmd += cmda.get(i, "").asString();
}
std::cerr << "Cmd: " << cmd << std::endl;
}
safe_array_t readOneFile(const std::string& arcFile, const std::string& file)
{
safe_array_t result;
auto archive = archive_read_new();
archive_read_support_filter_all(archive);
archive_read_support_format_all(archive);
int r = archive_read_open_filename(archive, arcFile.c_str(), 10240); // Note 1
if (r != ARCHIVE_OK)
{
std::cerr << "archive_read_open_filename failed" << std::endl;
return safe_array_t();
}
struct archive_entry* entry;
while (archive_read_next_header(archive, &entry) == ARCHIVE_OK)
{
std::string fileName = archive_entry_pathname(entry);
if (fileName == file)
{
size_t fileSize = archive_entry_size(entry);
std::shared_ptr<uint8_t> fileBuf(new uint8_t[fileSize + 1]);
auto readSize = archive_read_data(archive, fileBuf.get(), fileSize);
if (readSize < 0)
{
std::cerr << "archive_read_data failed" << std::endl;
continue;
}
fileBuf.get()[fileSize] = 0;
result = std::make_pair(fileBuf, fileSize);
break;
}
//archive_read_data_skip(archive); // Note 2
}
r = archive_read_free(archive); // Note 3
if (r != ARCHIVE_OK)
{
std::cerr << "archive_read_free failed" << std::endl;
return safe_array_t();
}
return result;
}
safe_array_t readOneFile(const safe_array_t &input, const std::string& file)
{
safe_array_t result;
auto archive = archive_read_new();
archive_read_support_filter_all(archive);
archive_read_support_format_all(archive);
int r = archive_read_open_memory(archive, input.first.get(), input.second);
if (r != ARCHIVE_OK)
{
std::cerr << "archive_read_open_filename failed" << std::endl;
return safe_array_t();
}
struct archive_entry* entry;
while (archive_read_next_header(archive, &entry) == ARCHIVE_OK)
{
std::string fileName = archive_entry_pathname(entry);
if (fileName == file)
{
size_t fileSize = archive_entry_size(entry);
std::shared_ptr<uint8_t> fileBuf(new uint8_t[fileSize + 1]);
auto readSize = archive_read_data(archive, fileBuf.get(), fileSize);
if (readSize < 0)
{
std::cerr << "archive_read_data failed" << std::endl;
continue;
}
fileBuf.get()[fileSize = 0];
result = std::make_pair(fileBuf, fileSize);
break;
}
//archive_read_data_skip(archive); // Note 2
}
r = archive_read_free(archive); // Note 3
if (r != ARCHIVE_OK)
{
std::cerr << "archive_read_free failed" << std::endl;
return safe_array_t();
}
return result;
}
std::map<std::string, size_t> listFiles(const safe_array_t& input)
{
std::map<std::string, size_t> result;
auto archive = archive_read_new();
archive_read_support_filter_all(archive);
archive_read_support_format_all(archive);
int r = archive_read_open_memory(archive, input.first.get(), input.second);
if (r != ARCHIVE_OK)
{
std::cerr << "archive_read_open_filename failed" << std::endl;
return result;
}
struct archive_entry* entry;
while (archive_read_next_header(archive, &entry) == ARCHIVE_OK)
{
std::string fileName = archive_entry_pathname(entry);
result[fileName] = archive_entry_size(entry);
//archive_read_data_skip(archive); // Note 2
}
r = archive_read_free(archive); // Note 3
if (r != ARCHIVE_OK)
{
std::cerr << "archive_read_free failed" << std::endl;
return result;
}
return result;
}
static bool isWhiteoutOpaque(const std::string& s)
{
size_t offset = s.rfind('/') + 1;
std::string t = s.substr(offset);
return t == ".wh..wh..opq";
}
static std::string getWhiteout(const std::string& s)
{
size_t offset = s.rfind('/') + 1;
std::string t = s.substr(offset);
if ((t.length() > 4) && (t.substr(0, 4) == ".wh."))
return t.substr(4);
return "";
}
static void removeFile(std::map<std::string, size_t>& map, const std::string& file)
{
for (auto it = map.begin(); it != map.end(); )
{
if (it->first == file)
{
std::cout << " Delete " << it->first << std::endl;
it = map.erase(it);
}
else
++it;
}
}
static void removeDir(std::map<std::string, size_t> &map, const std::string &file)
{
for (auto it = map.begin(); it != map.end(); )
{
if ((it->first.length() >= file.length()) && (it->first[file.length()] == '/') && (it->first.substr(0, file.length()) == file))
{
std::cout << " Delete " << it->first << std::endl;
it = map.erase(it);
}
else
++it;
}
}
static std::string formatFileSize(uint64_t n)
{
uint64_t cmp = 1;
if (n < cmp * 1024)
return std::to_string(n / cmp) + " ";
cmp *= 1024;
if (n < cmp * 1024)
return std::to_string(n / cmp) + "K";
cmp *= 1024;
if (n < cmp * 1024)
return std::to_string(n / cmp) + "M";
cmp *= 1024;
if (n < cmp * 1024)
return std::to_string(n / cmp) + "G";
cmp *= 1024;
return std::to_string(n / cmp) + "T";
}
int main(int argc, char **argv)
{
if (argc < 2)
{
std::cerr << "docker-image-viewer" << std::endl;
std::cerr << "usage: tar_file" << std::endl;
return 1;
}
std::cerr << "docker-image-viewer" << std::endl;
std::string arcFile = argv[1];
auto manifest = readOneFile(arcFile, "manifest.json");
if (manifest.first == nullptr)
{
std::cerr << "readOneFile manifest.json failed" << std::endl;
return 2;
}
auto dim = parseManifestFile(manifest);
auto config = readOneFile(arcFile, dim.config);
if (config.first == nullptr)
{
std::cerr << "readOneFile " << dim.config << " failed" << std::endl;
return 2;
}
auto dic = parseConfigJsonFile(config);
std::map<std::string, size_t> fileMap;
int i = 0;
for (auto tarFileName : dim.layer)
{
//auto jsonFileName = tarFileName.substr(0, tarFileName.rfind('/') + 1) + "json";
//auto jsonFile = readOneFile(arcFile, jsonFileName);
//parseLayerJsonFile(jsonFile);
auto tarFile = readOneFile(arcFile, tarFileName);
std::cout << tarFileName << std::endl;
std::cout << dic[i++] << std::endl;
auto layerFileList = listFiles(tarFile);
for (auto lfn : layerFileList)
{
if (isWhiteoutOpaque(lfn.first)) continue;
auto wh = getWhiteout(lfn.first);
if (!wh.empty())
{
removeFile(fileMap, wh);
removeDir(fileMap, wh);
}
else
{
removeDir(fileMap, lfn.first);
fileMap[lfn.first] = lfn.second;
std::cout << " Write " << std::setw(5) << formatFileSize(lfn.second) << " " << lfn.first << std::endl;
}
}
}
return 0;
}