File: PoolArchive.cpp

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (161 lines) | stat: -rw-r--r-- 4,043 bytes parent folder | download
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */


#include "PoolArchive.h"

#include <algorithm>
#include <stdexcept>
#include <sstream>
#include <string>
#include <cstring>
#include <iostream>
#include <assert.h>

#include "System/FileSystem/DataDirsAccess.h"
#include "System/FileSystem/FileSystem.h"
#include "System/Exceptions.h"
#include "System/StringUtil.h"
#include "System/Log/ILog.h"


CPoolArchiveFactory::CPoolArchiveFactory(): IArchiveFactory("sdp")
{
}

IArchive* CPoolArchiveFactory::DoCreateArchive(const std::string& filePath) const
{
	return new CPoolArchive(filePath);
}



static uint32_t parse_uint32(uint8_t c[4])
{
	uint32_t i = 0;
	i = c[0] << 24 | i;
	i = c[1] << 16 | i;
	i = c[2] <<  8 | i;
	i = c[3] <<  0 | i;
	return i;
}

static bool gz_really_read(gzFile file, voidp buf, unsigned int len)
{
	return (gzread(file, reinterpret_cast<char*>(buf), len) == len);
}



CPoolArchive::CPoolArchive(const std::string& name): CBufferedArchive(name)
{
	char c_name[255];
	uint8_t c_md5sum[16];
	uint8_t c_crc32[4];
	uint8_t c_size[4];
	uint8_t length;

	gzFile in = gzopen(name.c_str(), "rb");

	if (in == nullptr)
		throw content_error("[" + std::string(__func__) + "] could not open " + name);

	files.reserve(1024);
	stats.reserve(1024);

	while (gz_really_read(in, &length, 1)) {
		if (!gz_really_read(in, &c_name, length)) break;
		if (!gz_really_read(in, &c_md5sum, 16)) break;
		if (!gz_really_read(in, &c_crc32, 4)) break;
		if (!gz_really_read(in, &c_size, 4)) break;

		files.emplace_back();
		stats.emplace_back();
		FileData& f = files.back();
		FileStat& s = stats.back();

		f.name = std::move(std::string(c_name, length));
		std::memcpy(&f.md5sum, &c_md5sum, 16);
		f.crc32 = parse_uint32(c_crc32);
		f.size = parse_uint32(c_size);

		s.fileIndx = files.size() - 1;
		s.readTime = 0;

		lcNameIndex[f.name] = files.size() - 1;
	}

	isOpen = gzeof(in);
	gzclose(in);
}

CPoolArchive::~CPoolArchive()
{
	const std::string& name = GetArchiveName();
	const std::pair<uint64_t, uint64_t>& sums = GetSums();

	const unsigned long numZipFiles = files.size();
	const unsigned long sumInflSize = sums.first / 1024;
	const unsigned long sumReadTime = sums.second / (1000 * 1000);

	LOG_L(L_INFO, "[%s] name=\"%s\" numZipFiles=%lu sumInflSize=%lukb sumReadTime=%lums", __func__, name.c_str(), numZipFiles, sumInflSize, sumReadTime);

	std::partial_sort(stats.begin(), stats.begin() + std::min(stats.size(), size_t(10)), stats.end());

	// show top-10 worst access times
	for (size_t n = 0; n < std::min(stats.size(), size_t(10)); n++) {
		const FileStat& s = stats[n];
		const FileData& f = files[s.fileIndx];

		const unsigned long indx = s.fileIndx;
		const unsigned long time = s.readTime / (1000 * 1000);

		LOG_L(L_INFO, "\tfile=\"%s\" indx=%lu inflSize=%ukb readTime=%lums", f.name.c_str(), indx, f.size / 1024, time);
	}
}

bool CPoolArchive::GetFileImpl(unsigned int fid, std::vector<std::uint8_t>& buffer)
{
	assert(IsFileId(fid));

	FileData* f = &files[fid];
	FileStat* s = &stats[fid];

	constexpr const char table[] = "0123456789abcdef";
	char c_hex[32];

	for (int i = 0; i < 16; ++i) {
		c_hex[2 * i    ] = table[(f->md5sum[i] >> 4) & 0xf];
		c_hex[2 * i + 1] = table[ f->md5sum[i]       & 0xf];
	}

	const std::string prefix(c_hex,      2);
	const std::string postfix(c_hex + 2, 30);

	      std::string rpath = "pool/" + prefix + "/" + postfix + ".gz";
	const std::string  path = dataDirsAccess.LocateFile(FileSystem::FixSlashes(rpath));

	const spring_time startTime = spring_now();


	gzFile in = gzopen(path.c_str(), "rb");

	if (in == nullptr)
		return false;

	buffer.clear();
	buffer.resize(f->size);

	const int bytesRead = (buffer.empty()) ? 0 : gzread(in, reinterpret_cast<char*>(buffer.data()), f->size);
	gzclose(in);


	s->readTime = (spring_now() - startTime).toNanoSecsi();

	if (bytesRead != buffer.size()) {
		LOG_L(L_ERROR, "[PoolArchive::%s] could not read file \"%s\"", __func__, path.c_str());
		buffer.clear();
		return false;
	}

	return true;
}