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
|
// **********************************************************************
//
// Copyright (c) 2002
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#include <IcePatch/NodeUtil.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <openssl/md5.h>
using namespace std;
using namespace Ice;
using namespace IcePatch;
ByteSeq
IcePatch::getMD5(const std::string& path)
{
//
// Stat the file to get a MD5 hash value for.
//
struct stat buf;
if (stat(path.c_str(), &buf) == -1)
{
NodeAccessException ex;
ex.reason = "cannot stat `" + path + "':" + strerror(errno);
throw ex;
}
else
{
if (!S_ISREG(buf.st_mode))
{
NodeAccessException ex;
ex.reason = "`" + path + "' is not a regular file";
throw ex;
}
}
//
// Stat the .md5 file. If it doesn't exist, or if it's outdated,
// set a flag to create a new MD5 hash value.
//
struct stat bufmd5;
string pathmd5 = path + ".md5";
unsigned char md5[16];
bool createmd5 = false;
if (stat(pathmd5.c_str(), &bufmd5) == -1)
{
if (errno == ENOENT)
{
createmd5 = true;
}
else
{
NodeAccessException ex;
ex.reason = "cannot stat `" + path + "':" + strerror(errno);
throw ex;
}
}
else
{
if (!S_ISREG(bufmd5.st_mode))
{
NodeAccessException ex;
ex.reason = "`" + path + "' is not a regular file";
throw ex;
}
if (bufmd5.st_size != 16)
{
NodeAccessException ex;
ex.reason = "`" + path + "' isn't 16 bytes in size";
throw ex;
}
if (bufmd5.st_mtime <= buf.st_mtime)
{
createmd5 = true;
}
}
if (createmd5)
{
//
// Open the original file and create a MD5 hash value
//
{
int fd = open(path.c_str(), O_RDONLY);
if (fd == -1)
{
NodeAccessException ex;
ex.reason = "cannot open `" + path + "' for reading:" + strerror(errno);
throw ex;
}
unsigned char* fileBuf = new unsigned char[buf.st_size];
try
{
int sz = read(fd, fileBuf, buf.st_size);
if (sz == -1)
{
NodeAccessException ex;
ex.reason = "cannot read `" + path + "':" + strerror(errno);
throw ex;
}
if (sz < buf.st_size)
{
NodeAccessException ex;
ex.reason = "could not read all bytes from `" + path + "'";
throw ex;
}
MD5(fileBuf, sz, md5);
close(fd);
delete [] fileBuf;
}
catch (...)
{
close(fd);
delete [] fileBuf;
throw;
}
}
//
// Write the MD5 hash value to the corresponding .md5 file.
//
{
int fd = open(pathmd5.c_str(), O_WRONLY | O_CREAT);
if (fd == -1)
{
NodeAccessException ex;
ex.reason = "cannot open `" + pathmd5 + "' for writing:" + strerror(errno);
throw ex;
}
try
{
int sz = write(fd, md5, 16);
if (sz == -1)
{
NodeAccessException ex;
ex.reason = "cannot read `" + path + "':" + strerror(errno);
throw ex;
}
if (sz < 16)
{
NodeAccessException ex;
ex.reason = "could not write 16 bytes to `" + path + "'";
throw ex;
}
close(fd);
}
catch (...)
{
close(fd);
throw;
}
}
}
else
{
//
// Read the MD5 hash value from the .md5 file.
//
int fd = open(pathmd5.c_str(), O_RDONLY);
if (fd == -1)
{
NodeAccessException ex;
ex.reason = "cannot open `" + path + "' for reading:" + strerror(errno);
throw ex;
}
try
{
int sz = read(fd, md5, 16);
if (sz == -1)
{
NodeAccessException ex;
ex.reason = "cannot read `" + path + "':" + strerror(errno);
throw ex;
}
if (sz < 16)
{
NodeAccessException ex;
ex.reason = "could not read 16 bytes from `" + path + "'";
throw ex;
}
close(fd);
}
catch (...)
{
close(fd);
throw;
}
}
//
// Convert array to byte sequence.
//
ByteSeq result;
result.resize(16);
for (int i = 0; i < 16; ++i)
{
result[i] = md5[i];
}
return result;
}
std::string
IcePatch::MD5ToString(const Ice::ByteSeq& md5)
{
if (md5.size() != 16)
{
return "illegal MD5 hash code";
}
ostringstream out;
for (int i = 0; i < 16; ++i)
{
int b = static_cast<int>(md5[i]);
if (b < 0)
{
b += 256;
}
out << hex << b;
}
return out.str();
}
|