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
|
// **********************************************************************
//
// Copyright (c) 2002
// Mutable Realms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#include <IcePatch/Util.h>
#include <IcePatch/ClientUtil.h>
#include <fstream>
#include <bzlib.h>
using namespace std;
using namespace Ice;
using namespace IcePatch;
// TODO: ML: Move AbortException out of IcePatch, and into
// WishPatch. It is not used in IcePatch.
AbortException::AbortException(const char* file, int line) :
IceUtil::Exception(file, line)
{
}
std::string
AbortException::ice_name() const
{
return "AbortException";
}
Exception*
AbortException::ice_clone() const
{
return new AbortException(*this);
}
void
AbortException::ice_throw() const
{
throw *this;
}
string
IcePatch::pathToName(const string& path)
{
string::size_type pos = path.rfind('/');
if(pos == string::npos)
{
return path;
}
else
{
return path.substr(pos + 1);
}
}
void
IcePatch::getRegular(const RegularPrx& regular, ProgressCB& progressCB)
{
string path = identityToPath(regular->ice_getIdentity());
string pathBZ2 = path + ".bz2";
Int totalBZ2 = regular->getBZ2Size();
Int posBZ2 = 0;
//
// Check for partial BZ2 file.
//
FileInfo infoBZ2 = getFileInfo(pathBZ2, false);
if(infoBZ2.type == FileTypeRegular)
{
ByteSeq remoteBZ2MD5 = regular->getBZ2MD5(infoBZ2.size);
ByteSeq localBZ2MD5 = calcPartialMD5(pathBZ2, infoBZ2.size);
if(remoteBZ2MD5 == localBZ2MD5)
{
posBZ2 = infoBZ2.size;
}
}
//
// Get the BZ2 file.
//
progressCB.startDownload(totalBZ2, posBZ2);
ofstream fileBZ2(pathBZ2.c_str(), ios::binary | (posBZ2 ? ios::app : ios::openmode(0)));
if(!fileBZ2)
{
FileAccessException ex;
ex.reason = "cannot open `" + pathBZ2 + "' for writing: " + strerror(errno);
throw ex;
}
while(posBZ2 < totalBZ2)
{
const Int numBZ2 = 64 * 1024;
ByteSeq bytesBZ2 = regular->getBZ2(posBZ2, numBZ2);
if(bytesBZ2.empty())
{
break;
}
posBZ2 += bytesBZ2.size();
fileBZ2.write(&bytesBZ2[0], bytesBZ2.size());
if(!fileBZ2)
{
FileAccessException ex;
ex.reason = "cannot write `" + pathBZ2 + "': " + strerror(errno);
throw ex;
}
if(static_cast<Int>(bytesBZ2.size()) < numBZ2)
{
break;
}
progressCB.updateDownload(totalBZ2, posBZ2);
}
progressCB.finishedDownload(totalBZ2);
fileBZ2.close();
//
// Read the BZ2 file in blocks and write the original file.
//
ofstream file(path.c_str(), ios::binary);
if(!file)
{
FileAccessException ex;
ex.reason = "cannot open `" + path + "' for writing: " + strerror(errno);
throw ex;
}
FILE* stdioFileBZ2 = fopen(pathBZ2.c_str(), "rb");
if(!stdioFileBZ2)
{
FileAccessException ex;
ex.reason = "cannot open `" + pathBZ2 + "' for reading: " + strerror(errno);
throw ex;
}
int bzError;
BZFILE* bzFile = BZ2_bzReadOpen(&bzError, stdioFileBZ2, 0, 0, 0, 0);
if(bzError != BZ_OK)
{
FileAccessException ex;
ex.reason = "BZ2_bzReadOpen failed";
if(bzError == BZ_IO_ERROR)
{
ex.reason += string(": ") + strerror(errno);
}
fclose(stdioFileBZ2);
throw ex;
}
const Int numBZ2 = 64 * 1024;
Byte bytesBZ2[numBZ2];
try
{
progressCB.startUncompress(totalBZ2, 0);
while(bzError != BZ_STREAM_END)
{
int sz = BZ2_bzRead(&bzError, bzFile, bytesBZ2, numBZ2);
if(bzError != BZ_OK && bzError != BZ_STREAM_END)
{
FileAccessException ex;
ex.reason = "BZ2_bzRead failed";
if(bzError == BZ_IO_ERROR)
{
ex.reason += string(": ") + strerror(errno);
}
BZ2_bzReadClose(&bzError, bzFile);
fclose(stdioFileBZ2);
throw ex;
}
if(sz > 0)
{
long pos = ftell(stdioFileBZ2);
if(pos == -1)
{
FileAccessException ex;
ex.reason = "cannot get read position for `" + pathBZ2 + "': " + strerror(errno);
BZ2_bzReadClose(&bzError, bzFile);
fclose(stdioFileBZ2);
throw ex;
}
progressCB.updateUncompress(totalBZ2, pos);
file.write(bytesBZ2, sz);
if(!file)
{
FileAccessException ex;
ex.reason = "cannot write `" + path + "': " + strerror(errno);
BZ2_bzReadClose(&bzError, bzFile);
fclose(stdioFileBZ2);
throw ex;
}
}
}
progressCB.finishedUncompress(totalBZ2);
}
// TODO: ML: The code is broken. If an exception is raised above
// (for example, from the if(!file) {...} block), then
// BZ2_bzReadClose and fclose are called twice! Solution: Only
// close in this block, but not when an exception is raised above.
catch(...)
{
BZ2_bzReadClose(&bzError, bzFile);
fclose(stdioFileBZ2);
file.close();
throw;
}
BZ2_bzReadClose(&bzError, bzFile);
if(bzError != BZ_OK)
{
FileAccessException ex;
ex.reason = "BZ2_bzReadClose failed";
if(bzError == BZ_IO_ERROR)
{
ex.reason += string(": ") + strerror(errno);
}
fclose(stdioFileBZ2);
throw ex;
}
fclose(stdioFileBZ2);
file.close();
//
// Remove the BZ2 file, it is not needed anymore.
//
if(::remove(pathBZ2.c_str()) == -1)
{
FileAccessException ex;
ex.reason = "cannot remove file `" + pathBZ2 + "': " + strerror(errno);
throw ex;
}
//
// Create a MD5 file for the original file.
//
createMD5(path);
}
|