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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
|
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************
#include <IcePatch/Util.h>
#include <IceUtil/InputUtil.h>
#include <fstream>
#include <sys/stat.h>
#include <openssl/md5.h>
#include <bzlib.h>
#ifdef _WIN32
# include <direct.h>
# include <io.h>
# include <sys/utime.h>
# define S_ISDIR(mode) ((mode) & _S_IFDIR)
# define S_ISREG(mode) ((mode) & _S_IFREG)
#else
# include <unistd.h>
# include <dirent.h>
# include <utime.h>
#endif
//
// Stupid Visual C++ defines min and max as macros :-(
//
#ifdef min
# undef min
#endif
#ifdef max
# undef max
#endif
using namespace std;
using namespace Ice;
using namespace IcePatch;
string
normalizePath(const string& path)
{
string result = path;
string::size_type pos;
for(pos = 0; pos < result.size(); ++pos)
{
if(result[pos] == '\\')
{
result[pos] = '/';
}
}
pos = 0;
while((pos = result.find("//", pos)) != string::npos)
{
result.erase(pos, 1);
}
pos = 0;
while((pos = result.find("/./", pos)) != string::npos)
{
result.erase(pos, 2);
}
if(result.substr(0, 2) == "./")
{
result.erase(0, 2);
}
if(result.size() >= 2 && result.substr(result.size() - 2, 2) == "/.")
{
result.erase(result.size() - 2, 2);
}
return result;
}
string
IcePatch::identityToPath(const Identity& ident)
{
assert(ident.category == "IcePatch");
return normalizePath(ident.name);
}
Identity
IcePatch::pathToIdentity(const string& path)
{
Identity ident;
ident.category = "IcePatch";
ident.name = normalizePath(path);
return ident;
}
string
IcePatch::getSuffix(const string& path)
{
string::size_type pos = path.rfind('.');
if(pos == string::npos)
{
return string();
}
else
{
return path.substr(pos + 1);
}
}
bool
IcePatch::ignoreSuffix(const string& path)
{
string suffix = getSuffix(path);
return suffix == "md5" || suffix == "md5temp" || suffix == "bz2" || suffix == "bz2temp";
}
string
IcePatch::removeSuffix(const string& path)
{
string::size_type pos = path.rfind('.');
if(pos == string::npos)
{
return path;
}
else
{
return path.substr(0, pos);
}
}
FileInfo
IcePatch::getFileInfo(const string& path, bool exceptionIfNotExist, const Ice::LoggerPtr& logger)
{
FileInfo result;
struct stat buf;
while(true)
{
if(::stat(path.c_str(), &buf) == -1)
{
if(!exceptionIfNotExist && errno == ENOENT)
{
FileInfo result;
result.size = 0;
result.time = 0;
result.type = FileTypeNotExist;
return result;
}
else
{
FileAccessException ex;
ex.reason = "cannot stat `" + path + "': " + strerror(errno);
throw ex;
}
}
result.size = buf.st_size;
result.time = buf.st_mtime;
if(IceUtil::Time::seconds(result.time) <= IceUtil::Time::now())
{
break;
}
if(logger)
{
Trace out(logger, "IcePatch");
out << "modification time for `" << path << "' is in the future\n";
out << "setting modification time to the current time.";
}
if(::utime(path.c_str(), 0) == -1)
{
if(errno != ENOENT)
{
FileAccessException ex;
ex.reason = "cannot utime `" + path + "': " + strerror(errno);
throw ex;
}
}
if(!ignoreSuffix(path))
{
try
{
removeRecursive(path + ".md5", logger);
}
catch(const FileAccessException&)
{
}
try
{
removeRecursive(path + ".bz2", logger);
}
catch(const FileAccessException&)
{
}
}
}
if(S_ISDIR(buf.st_mode))
{
result.type = FileTypeDirectory;
}
else if(S_ISREG(buf.st_mode))
{
result.type = FileTypeRegular;
}
else
{
FileAccessException ex;
ex.reason = "file type of `" + path + "' is not supported";
throw ex;
}
return result;
}
void
IcePatch::removeRecursive(const string& path, const Ice::LoggerPtr& logger)
{
if(getFileInfo(path, true, logger).type == FileTypeDirectory)
{
StringSeq paths = readDirectory(path);
for(StringSeq::const_iterator p = paths.begin(); p != paths.end(); ++p)
{
removeRecursive(*p, logger);
}
#ifdef _WIN32
if(_rmdir(path.c_str()) == -1)
#else
if(rmdir(path.c_str()) == -1)
#endif
{
FileAccessException ex;
ex.reason = "cannot remove directory `" + path + "': " + strerror(errno);
throw ex;
}
}
else
{
if(::remove(path.c_str()) == -1)
{
FileAccessException ex;
ex.reason = "cannot remove file `" + path + "': " + strerror(errno);
throw ex;
}
}
if(logger)
{
Trace out(logger, "IcePatch");
out << "removed file `" << path << "'";
}
}
StringSeq
IcePatch::readDirectory(const string& path)
{
#ifdef _WIN32
struct _finddata_t data;
long h = _findfirst((path + "/*").c_str(), &data);
if(h == -1)
{
FileAccessException ex;
ex.reason = "cannot read directory `" + path + "': " + strerror(errno);
throw ex;
}
StringSeq result;
while(true)
{
string name = data.name;
assert(!name.empty());
if(name != ".." && name != ".")
{
result.push_back(normalizePath(path + '/' + name));
}
if(_findnext(h, &data) == -1)
{
if(errno == ENOENT)
{
break;
}
FileAccessException ex;
ex.reason = "cannot read directory `" + path + "': " + strerror(errno);
_findclose(h);
throw ex;
}
}
_findclose(h);
sort(result.begin(), result.end());
return result;
#else
struct dirent **namelist;
int n = ::scandir(path.c_str(), &namelist, 0, alphasort);
if(n < 0)
{
FileAccessException ex;
ex.reason = "cannot read directory `" + path + "': " + strerror(errno);
throw ex;
}
StringSeq result;
result.reserve(n - 2);
for(int i = 0; i < n; ++i)
{
string name = namelist[i]->d_name;
assert(!name.empty());
free(namelist[i]);
if(name != ".." && name != ".")
{
result.push_back(normalizePath(path + '/' + name));
}
}
free(namelist);
return result;
#endif
}
void
IcePatch::createDirectory(const string& path)
{
#ifdef _WIN32
if(::_mkdir(path.c_str()) == -1)
#else
if(::mkdir(path.c_str(), 00777) == -1)
#endif
{
FileAccessException ex;
ex.reason = "cannot create directory `" + path + "': " + strerror(errno);
throw ex;
}
}
ByteSeq
IcePatch::getMD5(const string& path)
{
string pathMD5 = path + ".md5";
ifstream fileMD5(pathMD5.c_str(), ios::binary);
if(!fileMD5)
{
FileAccessException ex;
ex.reason = "cannot open `" + pathMD5 + "' for reading: " + strerror(errno);
throw ex;
}
ByteSeq bytesMD5;
bytesMD5.resize(16);
fileMD5.read(&bytesMD5[0], 16);
if(!fileMD5)
{
FileAccessException ex;
ex.reason = "cannot read `" + pathMD5 + "': " + strerror(errno);
throw ex;
}
if(fileMD5.gcount() < 16)
{
FileAccessException ex;
ex.reason = "could not read 16 bytes from `" + pathMD5 + "'";
throw ex;
}
fileMD5.close();
return bytesMD5;
}
void
IcePatch::putMD5(const string& path, const ByteSeq& bytesMD5)
{
assert(bytesMD5.size() == 16);
//
// Save the MD5 hash value to a temporary MD5 file.
//
string pathMD5Temp = path + ".md5temp";
ofstream fileMD5(pathMD5Temp.c_str(), ios::binary);
if(!fileMD5)
{
FileAccessException ex;
ex.reason = "cannot open `" + pathMD5Temp + "' for writing: " + strerror(errno);
throw ex;
}
fileMD5.write(&bytesMD5[0], 16);
if(!fileMD5)
{
FileAccessException ex;
ex.reason = "cannot write `" + pathMD5Temp + "': " + strerror(errno);
throw ex;
}
fileMD5.close();
//
// Rename the temporary MD5 file to the final MD5 file. This is
// done so that there can be no partial MD5 files after an
// abortive application termination.
//
string pathMD5 = path + ".md5";
::remove(pathMD5.c_str());
if(::rename(pathMD5Temp.c_str(), pathMD5.c_str()) == -1)
{
FileAccessException ex;
ex.reason = "cannot rename `" + pathMD5Temp + "' to `" + pathMD5 + "': " + strerror(errno);
throw ex;
}
}
void
IcePatch::createMD5(const string& path, const LoggerPtr& logger)
{
//
// The current directory is not permissible for MD5 value
// creation.
//
assert(path != ".");
ByteSeq bytes;
FileInfo info = getFileInfo(path, true, logger);
if(info.type == FileTypeDirectory)
{
//
// Create a summary of all MD5 files.
//
StringSeq paths = readDirectory(path);
for(StringSeq::const_iterator p = paths.begin(); p < paths.end(); ++p)
{
if(getSuffix(*p) == "md5")
{
string plainPath = removeSuffix(*p);
//
// We must take the filename into account, so that
// renaming a file will change the summary MD5.
//
copy(plainPath.begin(), plainPath.end(), back_inserter(bytes));
ByteSeq subBytesMD5 = getMD5(plainPath);
copy(subBytesMD5.begin(), subBytesMD5.end(), back_inserter(bytes));
}
}
}
else
{
assert(info.type == FileTypeRegular);
//
// Read the original file.
//
ifstream file(path.c_str(), ios::binary);
if(!file)
{
FileAccessException ex;
ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno);
throw ex;
}
bytes.resize(info.size);
if(bytes.size() > 0)
{
file.read(&bytes[0], bytes.size());
if(!file)
{
FileAccessException ex;
ex.reason = "cannot read `" + path + "': " + strerror(errno);
throw ex;
}
if(file.gcount() < static_cast<int>(bytes.size()))
{
FileAccessException ex;
ex.reason = "could not read all bytes from `" + path + "'";
throw ex;
}
}
file.close();
}
//
// Create the MD5 hash value.
//
ByteSeq bytesMD5;
bytesMD5.resize(16);
if(bytes.size() > 0)
{
MD5(reinterpret_cast<unsigned char*>(&bytes[0]), bytes.size(), reinterpret_cast<unsigned char*>(&bytesMD5[0]));
}
else
{
fill(bytesMD5.begin(), bytesMD5.end(), 0);
}
//
// Save the MD5 hash value.
//
putMD5(path, bytesMD5);
if(logger)
{
Trace out(logger, "IcePatch");
out << "created MD5 file for `" << path << "'";
}
}
ByteSeq
IcePatch::calcPartialMD5(const string& path, Int size, const LoggerPtr& logger)
{
if(size < 0)
{
FileAccessException ex;
ex.reason = "negative file size is illegal";
throw ex;
}
FileInfo info = getFileInfo(path, true, logger);
if(info.type == FileTypeDirectory)
{
FileAccessException ex;
ex.reason = "cannot calculate partial MD5 for `" + path + "' because this is a directory";
throw ex;
}
//
// Read the original file partially.
//
size = std::min(size, static_cast<Int>(info.size));
ifstream file(path.c_str(), ios::binary);
if(!file)
{
FileAccessException ex;
ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno);
throw ex;
}
ByteSeq bytes;
bytes.resize(size);
if(bytes.size() > 0)
{
file.read(&bytes[0], bytes.size());
if(!file)
{
FileAccessException ex;
ex.reason = "cannot read `" + path + "': " + strerror(errno);
throw ex;
}
if(file.gcount() < static_cast<int>(bytes.size()))
{
FileAccessException ex;
ex.reason = "could not read all bytes from `" + path + "'";
throw ex;
}
}
file.close();
//
// Create the MD5 hash value.
//
ByteSeq bytesMD5;
bytesMD5.resize(16);
if(bytes.size() > 0)
{
MD5(reinterpret_cast<unsigned char*>(&bytes[0]), bytes.size(), reinterpret_cast<unsigned char*>(&bytesMD5[0]));
}
else
{
fill(bytesMD5.begin(), bytesMD5.end(), 0);
}
return bytesMD5;
}
ByteSeq
IcePatch::getBZ2(const string& path, Int pos, Int num)
{
if(pos < 0)
{
FileAccessException ex;
ex.reason = "negative read offset is illegal";
throw ex;
}
if(num < 0)
{
FileAccessException ex;
ex.reason = "negative data segment size is illegal";
throw ex;
}
if(num > 1024 * 1024)
{
FileAccessException ex;
ex.reason = "maxium data segment size exceeded";
throw ex;
}
string pathBZ2 = path + ".bz2";
ifstream fileBZ2(pathBZ2.c_str(), ios::binary);
if(!fileBZ2)
{
FileAccessException ex;
ex.reason = "cannot open `" + pathBZ2 + "' for reading: " + strerror(errno);
throw ex;
}
fileBZ2.seekg(pos);
if(!fileBZ2)
{
FileAccessException ex;
ostringstream out;
out << "cannot seek position " << pos << " in file `" << path << "':" << strerror(errno);
ex.reason = out.str();
throw ex;
}
ByteSeq bytesBZ2;
bytesBZ2.resize(num);
fileBZ2.read(&bytesBZ2[0], bytesBZ2.size());
if(!fileBZ2 && !fileBZ2.eof())
{
FileAccessException ex;
ex.reason = "cannot read `" + pathBZ2 + "': " + strerror(errno);
throw ex;
}
bytesBZ2.resize(fileBZ2.gcount());
fileBZ2.close();
return bytesBZ2;
}
void
IcePatch::createBZ2(const string& path, const Ice::LoggerPtr& logger)
{
FileInfo info = getFileInfo(path, true, logger);
if(info.type == FileTypeDirectory)
{
FileAccessException ex;
ex.reason = "cannot create BZ2 file for `" + path + "' because this is a directory";
throw ex;
}
//
// Read the original file in blocks and write a temporary BZ2
// file.
//
ifstream file(path.c_str(), ios::binary);
if(!file)
{
FileAccessException ex;
ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno);
throw ex;
}
string pathBZ2Temp = path + ".bz2temp";
FILE* stdioFileBZ2 = fopen(pathBZ2Temp.c_str(), "wb");
if(!stdioFileBZ2)
{
FileAccessException ex;
ex.reason = "cannot open `" + pathBZ2Temp + "' for writing: " + strerror(errno);
throw ex;
}
int bzError;
BZFILE* bzFile = BZ2_bzWriteOpen(&bzError, stdioFileBZ2, 5, 0, 0);
if(bzError != BZ_OK)
{
FileAccessException ex;
ex.reason = "BZ2_bzWriteOpen failed";
if(bzError == BZ_IO_ERROR)
{
ex.reason += string(": ") + strerror(errno);
}
fclose(stdioFileBZ2);
throw ex;
}
const Int num = 64 * 1024;
Byte bytes[num];
while(!file.eof())
{
file.read(bytes, num);
if(!file && !file.eof())
{
FileAccessException ex;
ex.reason = "cannot read `" + path + "': " + strerror(errno);
BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0);
fclose(stdioFileBZ2);
throw ex;
}
if(file.gcount() > 0)
{
BZ2_bzWrite(&bzError, bzFile, bytes, file.gcount());
if(bzError != BZ_OK)
{
FileAccessException ex;
ex.reason = "BZ2_bzWrite failed";
if(bzError == BZ_IO_ERROR)
{
ex.reason += string(": ") + strerror(errno);
}
BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0);
fclose(stdioFileBZ2);
throw ex;
}
}
}
BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0);
if(bzError != BZ_OK)
{
FileAccessException ex;
ex.reason = "BZ2_bzWriteClose failed";
if(bzError == BZ_IO_ERROR)
{
ex.reason += string(": ") + strerror(errno);
}
fclose(stdioFileBZ2);
throw ex;
}
fclose(stdioFileBZ2);
file.close();
//
// Rename the temporary BZ2 file to the final BZ2 file. This is
// done so that there can be no partial BZ2 files after an
// abortive application termination.
//
string pathBZ2 = path + ".bz2";
::remove(pathBZ2.c_str());
if(::rename(pathBZ2Temp.c_str(), pathBZ2.c_str()) == -1)
{
FileAccessException ex;
ex.reason = "cannot rename `" + pathBZ2Temp + "' to `" + pathBZ2 + "': " + strerror(errno);
throw ex;
}
if(logger)
{
Trace out(logger, "IcePatch");
out << "created BZ2 file for `" << path << "'";
}
}
|