~ubuntu-branches/ubuntu/quantal/zeroc-ice/quantal

« back to all changes in this revision

Viewing changes to cpp/src/IcePatch2/Util.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cleto Martin Angelina
  • Date: 2011-04-25 18:44:24 UTC
  • mfrom: (6.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110425184424-sep9i9euu434vq4c
Tags: 3.4.1-7
* Bug fix: "libdb5.1-java.jar was renamed to db.jar", thanks to Ondřej
  Surý (Closes: #623555).
* Bug fix: "causes noise in php5", thanks to Jayen Ashar (Closes:
  #623533).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// **********************************************************************
2
 
//
3
 
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
4
 
//
5
 
// This copy of Ice is licensed to you under the terms described in the
6
 
// ICE_LICENSE file included in this distribution.
7
 
//
8
 
// **********************************************************************
9
 
 
10
 
//
11
 
// We need to include io.h first to get the proper signature for
12
 
// _wfindfirst
13
 
//
14
 
#ifdef _WIN32
15
 
#   include <io.h>
16
 
#endif
17
 
 
18
 
#include <IceUtil/DisableWarnings.h>
19
 
#include <IceUtil/IceUtil.h>
20
 
#include <IceUtil/StringUtil.h>
21
 
#define ICE_PATCH2_API_EXPORTS
22
 
#include <IcePatch2/Util.h>
23
 
#include <openssl/sha.h>
24
 
#include <bzlib.h>
25
 
#include <iomanip>
26
 
#include <OS.h>
27
 
 
28
 
#ifdef _WIN32
29
 
#   include <direct.h>
30
 
#else
31
 
#   include <unistd.h>
32
 
#   include <dirent.h>
33
 
#endif
34
 
 
35
 
#ifdef __BCPLUSPLUS__
36
 
#   include <iterator>
37
 
#endif
38
 
 
39
 
const char* IcePatch2::checksumFile = "IcePatch2.sum";
40
 
const char* IcePatch2::logFile = "IcePatch2.log";
41
 
 
42
 
//
43
 
// Solaris 9 and before doesn't have scandir() or alphasort().
44
 
//
45
 
#ifdef __sun
46
 
 
47
 
extern "C" static int
48
 
ice_scandir(const char* dir, struct dirent*** namelist,
49
 
        int (*select)(const struct dirent*),
50
 
        int (*compar)(const void*, const void*))
51
 
{
52
 
    DIR* d;
53
 
    struct dirent* entry;
54
 
    register int i = 0;
55
 
    size_t entrysize;
56
 
 
57
 
    if((d = opendir(dir)) == 0)
58
 
    {
59
 
        return -1;
60
 
    }
61
 
 
62
 
    *namelist = 0;
63
 
    while((entry = readdir(d)) != 0)
64
 
    {
65
 
        if(select == 0 || (select != 0 && (*select)(entry)))
66
 
        {
67
 
            *namelist = (struct dirent**)realloc((void*)(*namelist), (size_t)((i + 1) * sizeof(struct dirent*)));
68
 
            if(*namelist == 0)
69
 
            {
70
 
                closedir(d);
71
 
                return -1;
72
 
            }
73
 
 
74
 
            entrysize = sizeof(struct dirent) - sizeof(entry->d_name) + strlen(entry->d_name) + 1;
75
 
            (*namelist)[i] = (struct dirent*)malloc(entrysize);
76
 
            if((*namelist)[i] == 0)
77
 
            {
78
 
                closedir(d);
79
 
                return -1;
80
 
            }
81
 
            memcpy((*namelist)[i], entry, entrysize);
82
 
            ++i;
83
 
        }
84
 
    }
85
 
 
86
 
    if(closedir(d))
87
 
    {
88
 
        return -1;
89
 
    }
90
 
 
91
 
    if(i == 0)
92
 
    {
93
 
        return -1;
94
 
    }
95
 
 
96
 
    if(compar != 0)
97
 
    {
98
 
        qsort((void *)(*namelist), (size_t)i, sizeof(struct dirent *), compar);
99
 
    }
100
 
 
101
 
    return i;
102
 
}
103
 
 
104
 
extern "C" static int
105
 
ice_alphasort(const void* v1, const void* v2)
106
 
{
107
 
    const struct dirent **a = (const struct dirent **)v1;
108
 
    const struct dirent **b = (const struct dirent **)v2;
109
 
    return(strcmp((*a)->d_name, (*b)->d_name));
110
 
}
111
 
 
112
 
#endif
113
 
 
114
 
using namespace std;
115
 
using namespace Ice;
116
 
using namespace IcePatch2;
117
 
 
118
 
bool
119
 
IcePatch2::writeFileInfo(FILE* fp, const FileInfo& info)
120
 
{
121
 
    int rc = fprintf(fp, "%s\t%s\t%d\t%d\n", 
122
 
                     IceUtilInternal::escapeString(info.path, "").c_str(),
123
 
                     bytesToString(info.checksum).c_str(),
124
 
                     info.size,
125
 
                     static_cast<int>(info.executable));
126
 
    return rc > 0;
127
 
}
128
 
 
129
 
bool
130
 
IcePatch2::readFileInfo(FILE* fp, FileInfo& info)
131
 
{
132
 
    string data;
133
 
    char buf[BUFSIZ];
134
 
    while(fgets(buf, static_cast<int>(sizeof(buf)), fp) != 0)
135
 
    {
136
 
        data += buf;
137
 
 
138
 
        size_t len = strlen(buf);
139
 
        if(buf[len - 1] == '\n')
140
 
        {
141
 
            break;
142
 
        }
143
 
    }
144
 
    if(data.empty())
145
 
    {
146
 
        return false;
147
 
    }
148
 
 
149
 
    istringstream is(data);
150
 
 
151
 
    string s;
152
 
    getline(is, s, '\t');
153
 
    IceUtilInternal::unescapeString(s, 0, s.size(), info.path);
154
 
 
155
 
    getline(is, s, '\t');
156
 
    info.checksum = stringToBytes(s);
157
 
 
158
 
    is >> info.size;
159
 
    is >> info.executable;
160
 
 
161
 
    return true;
162
 
}
163
 
 
164
 
string
165
 
IcePatch2::bytesToString(const ByteSeq& bytes)
166
 
{
167
 
/*
168
 
    ostringstream s;
169
 
 
170
 
    for(ByteSeq::const_iterator p = bytes.begin(); p != bytes.end(); ++p)
171
 
    {
172
 
        s << setw(2) << setfill('0') << hex << static_cast<int>(*p);
173
 
    }
174
 
 
175
 
    return s.str();
176
 
*/
177
 
 
178
 
    static const char* toHex = "0123456789abcdef";
179
 
 
180
 
    string s;
181
 
    s.resize(bytes.size() * 2);
182
 
 
183
 
    for(unsigned int i = 0; i < bytes.size(); ++i)
184
 
    {
185
 
        s[i * 2] = toHex[(bytes[i] >> 4) & 0xf];
186
 
        s[i * 2 + 1] = toHex[bytes[i] & 0xf];
187
 
    }
188
 
 
189
 
    return s;
190
 
}
191
 
 
192
 
ByteSeq
193
 
IcePatch2::stringToBytes(const string& str)
194
 
{
195
 
    ByteSeq bytes;
196
 
    bytes.reserve((str.size() + 1) / 2);
197
 
 
198
 
    for(unsigned int i = 0; i + 1 < str.size(); i += 2)
199
 
    {
200
 
/*
201
 
        istringstream is(str.substr(i, 2));
202
 
        int byte;
203
 
        is >> hex >> byte;
204
 
*/
205
 
 
206
 
        int byte = 0;
207
 
 
208
 
        for(int j = 0; j < 2; ++j)
209
 
        {
210
 
            char c = str[i + j];
211
 
 
212
 
            if(c >= '0' && c <= '9')
213
 
            {
214
 
                byte |= c - '0';
215
 
            }
216
 
            else if(c >= 'a' && c <= 'f')
217
 
            {
218
 
                byte |= 10 + c - 'a';
219
 
            }
220
 
            else if(c >= 'A' && c <= 'F')
221
 
            {
222
 
                byte |= 10 + c - 'A';
223
 
            }
224
 
 
225
 
            if(j == 0)
226
 
            {
227
 
                byte <<= 4;
228
 
            }
229
 
        }
230
 
 
231
 
        bytes.push_back(static_cast<Byte>(byte));
232
 
    }
233
 
 
234
 
    return bytes;
235
 
}
236
 
 
237
 
string
238
 
IcePatch2::simplify(const string& path)
239
 
{
240
 
    string result = path;
241
 
 
242
 
    string::size_type pos;
243
 
 
244
 
#ifdef _WIN32
245
 
    pos = 0;
246
 
    if(result.find("\\\\") == 0)
247
 
    {
248
 
        pos = 2;
249
 
    }
250
 
 
251
 
    for(; pos < result.size(); ++pos)
252
 
    {
253
 
        if(result[pos] == '\\')
254
 
        {
255
 
            result[pos] = '/';
256
 
        }
257
 
    }
258
 
#endif
259
 
 
260
 
    pos = 0;
261
 
    while((pos = result.find("//", pos)) != string::npos)
262
 
    {
263
 
        result.erase(pos, 1);
264
 
    }
265
 
 
266
 
    pos = 0;
267
 
    while((pos = result.find("/./", pos)) != string::npos)
268
 
    {
269
 
        result.erase(pos, 2);
270
 
    }
271
 
 
272
 
    while(result.substr(0, 4) == "/../")
273
 
    {
274
 
        result.erase(0, 3);
275
 
    }
276
 
 
277
 
    if(result.substr(0, 2) == "./")
278
 
    {
279
 
        result.erase(0, 2);
280
 
    }
281
 
 
282
 
    if(result == "/." ||
283
 
       (result.size() == 4 && isalpha(static_cast<unsigned char>(result[0])) && result[1] == ':' && 
284
 
        result[2] == '/' && result[3] == '.'))
285
 
    {
286
 
       return result.substr(0, result.size() - 1);
287
 
    }
288
 
 
289
 
    if(result.size() >= 2 && result.substr(result.size() - 2, 2) == "/.")
290
 
    {
291
 
        result.erase(result.size() - 2, 2);
292
 
    }
293
 
 
294
 
    if(result == "/" || (result.size() == 3 && isalpha(static_cast<unsigned char>(result[0])) && result[1] == ':' &&
295
 
       result[2] == '/'))
296
 
    {
297
 
        return result;
298
 
    }
299
 
 
300
 
    if(result.size() >= 1 && result[result.size() - 1] == '/')
301
 
    {
302
 
        result.erase(result.size() - 1);
303
 
    }
304
 
 
305
 
    if(result == "/..")
306
 
    {
307
 
        result = "/";
308
 
    }
309
 
 
310
 
    return result;
311
 
}
312
 
 
313
 
bool
314
 
IcePatch2::isRoot(const string& pa)
315
 
{
316
 
    string path = simplify(pa);
317
 
#ifdef _WIN32
318
 
    return path == "/" || path.size() == 3 && isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':' && 
319
 
           path[2] == '/';
320
 
#else
321
 
    return path == "/";
322
 
#endif
323
 
}
324
 
 
325
 
string
326
 
IcePatch2::getSuffix(const string& pa)
327
 
{
328
 
    const string path = simplify(pa);
329
 
 
330
 
    string::size_type dotPos = path.rfind('.');
331
 
    string::size_type slashPos = path.rfind('/');
332
 
 
333
 
    if(dotPos == string::npos || (slashPos != string::npos && slashPos > dotPos))
334
 
    {
335
 
        return string();
336
 
    }
337
 
 
338
 
    return path.substr(dotPos + 1);
339
 
}
340
 
 
341
 
string
342
 
IcePatch2::getWithoutSuffix(const string& pa)
343
 
{
344
 
    const string path = simplify(pa);
345
 
 
346
 
    string::size_type dotPos = path.rfind('.');
347
 
    string::size_type slashPos = path.rfind('/');
348
 
 
349
 
    if(dotPos == string::npos || (slashPos != string::npos && slashPos > dotPos))
350
 
    {
351
 
        return path;
352
 
    }
353
 
 
354
 
    return path.substr(0, dotPos);
355
 
}
356
 
 
357
 
bool
358
 
IcePatch2::ignoreSuffix(const string& path)
359
 
{
360
 
    string suffix = getSuffix(path);
361
 
    return suffix == "md5" // For legacy IcePatch.
362
 
        || suffix == "tot" // For legacy IcePatch.
363
 
        || suffix == "bz2"
364
 
        || suffix == "bz2temp";
365
 
}
366
 
 
367
 
string
368
 
IcePatch2::getBasename(const string& pa)
369
 
{
370
 
    const string path = simplify(pa);
371
 
 
372
 
    string::size_type pos = path.rfind('/');
373
 
    if(pos == string::npos)
374
 
    {
375
 
        return path;
376
 
    }
377
 
    else
378
 
    {
379
 
        return path.substr(pos + 1);
380
 
    }
381
 
}
382
 
 
383
 
string
384
 
IcePatch2::getDirname(const string& pa)
385
 
{
386
 
    const string path = simplify(pa);
387
 
 
388
 
    string::size_type pos = path.rfind('/');
389
 
    if(pos == string::npos)
390
 
    {
391
 
        return string();
392
 
    }
393
 
    else
394
 
    {
395
 
        return path.substr(0, pos);
396
 
    }
397
 
}
398
 
 
399
 
void
400
 
IcePatch2::rename(const string& fromPa, const string& toPa)
401
 
{
402
 
 
403
 
    const string fromPath = simplify(fromPa);
404
 
    const string toPath = simplify(toPa);
405
 
 
406
 
    OS::remove(toPath); // We ignore errors, as the file we are renaming to might not exist.
407
 
 
408
 
    if(OS::rename(fromPath ,toPath) == -1)
409
 
    {
410
 
        throw "cannot rename `" + fromPath + "' to  `" + toPath + "': " + IceUtilInternal::lastErrorToString();
411
 
    }
412
 
}
413
 
 
414
 
void
415
 
IcePatch2::remove(const string& pa)
416
 
{
417
 
    const string path = simplify(pa);
418
 
 
419
 
    OS::structstat buf;
420
 
    if(OS::osstat(path, &buf) == -1)
421
 
    {
422
 
        throw "cannot stat `" + path + "':\n" + IceUtilInternal::lastErrorToString();
423
 
    }
424
 
 
425
 
    if(S_ISDIR(buf.st_mode))
426
 
    {
427
 
        if(OS::rmdir(path) == -1)
428
 
        {
429
 
            if(errno == EACCES)
430
 
            {
431
 
                assert(false);
432
 
            }
433
 
            throw "cannot remove directory `" + path + "':\n" + IceUtilInternal::lastErrorToString();
434
 
        }
435
 
    }
436
 
    else
437
 
    {
438
 
        if(OS::remove(path) == -1)
439
 
        {
440
 
            throw "cannot remove file `" + path + "':\n" + IceUtilInternal::lastErrorToString();
441
 
        }
442
 
    }
443
 
}
444
 
 
445
 
void
446
 
IcePatch2::removeRecursive(const string& pa)
447
 
{
448
 
    const string path = simplify(pa);
449
 
 
450
 
    OS::structstat buf;
451
 
    if(OS::osstat(path, &buf) == -1)
452
 
    {
453
 
        throw "cannot stat `" + path + "':\n" + IceUtilInternal::lastErrorToString();
454
 
    }
455
 
 
456
 
    if(S_ISDIR(buf.st_mode))
457
 
    {
458
 
        StringSeq paths = readDirectory(path);
459
 
        for(StringSeq::const_iterator p = paths.begin(); p != paths.end(); ++p)
460
 
        {
461
 
            removeRecursive(path + '/' + *p);
462
 
        }
463
 
 
464
 
        if(!isRoot(path))
465
 
        {
466
 
            if(OS::rmdir(path) == -1)
467
 
            {
468
 
                throw "cannot remove directory `" + path + "':\n" + IceUtilInternal::lastErrorToString();
469
 
            }
470
 
        }
471
 
    }
472
 
    else
473
 
    {
474
 
        if(OS::remove(path) == -1)
475
 
        {
476
 
            throw "cannot remove file `" + path + "':\n" + IceUtilInternal::lastErrorToString();
477
 
        }
478
 
    }
479
 
}
480
 
 
481
 
StringSeq
482
 
IcePatch2::readDirectory(const string& pa)
483
 
{
484
 
    const string path = simplify(pa);
485
 
 
486
 
#ifdef _WIN32
487
 
 
488
 
    StringSeq result;
489
 
    const wstring fs = IceUtil::stringToWstring(simplify(path + "/*"));
490
 
 
491
 
#  ifdef __BCPLUSPLUS__
492
 
    struct _wffblk data;
493
 
    int h = _wfindfirst(fs.c_str(), &data, FA_DIREC);
494
 
    if(h == -1)
495
 
    {
496
 
        if(_doserrno == ENMFILE)
497
 
        {
498
 
            return result;
499
 
        }
500
 
        throw "cannot read directory `" + path + "':\n" + IceUtilInternal::lastErrorToString();
501
 
    }
502
 
 
503
 
 
504
 
    while(true)
505
 
    {
506
 
        string name = IceUtil::wstringToString(data.ff_name);
507
 
        assert(!name.empty());
508
 
 
509
 
        if(name != ".." && name != ".")
510
 
        {
511
 
            result.push_back(name);
512
 
        }
513
 
 
514
 
        if(_wfindnext(&data) == -1)
515
 
        {
516
 
            if(errno == ENOENT)
517
 
            {
518
 
                break;
519
 
            }
520
 
 
521
 
            string ex = "cannot read directory `" + path + "':\n" + IceUtilInternal::lastErrorToString();
522
 
            _wfindclose(&data);
523
 
            throw ex;
524
 
        }
525
 
    }
526
 
 
527
 
    _wfindclose(&data);
528
 
#  else
529
 
    struct _wfinddata_t data;
530
 
 
531
 
#    if defined(_MSC_VER) && (_MSC_VER < 1300)
532
 
    long h = _wfindfirst(fs.c_str(), &data);
533
 
#    else
534
 
    intptr_t h = _wfindfirst(fs.c_str(), &data);
535
 
#    endif
536
 
    if(h == -1)
537
 
    {
538
 
        throw "cannot read directory `" + path + "':\n" + IceUtilInternal::lastErrorToString();
539
 
    }
540
 
 
541
 
    while(true)
542
 
    {
543
 
        string name = IceUtil::wstringToString(data.name);
544
 
        assert(!name.empty());
545
 
 
546
 
        if(name != ".." && name != ".")
547
 
        {
548
 
            result.push_back(name);
549
 
        }
550
 
 
551
 
        if(_wfindnext(h, &data) == -1)
552
 
        {
553
 
            if(errno == ENOENT)
554
 
            {
555
 
                break;
556
 
            }
557
 
 
558
 
            string ex = "cannot read directory `" + path + "':\n" + IceUtilInternal::lastErrorToString();
559
 
            _findclose(h);
560
 
            throw ex;
561
 
        }
562
 
    }
563
 
 
564
 
    _findclose(h);
565
 
#  endif
566
 
 
567
 
    sort(result.begin(), result.end());
568
 
    return result;
569
 
 
570
 
#else
571
 
 
572
 
    struct dirent **namelist;
573
 
#ifdef __sun
574
 
    int n = ice_scandir(path.c_str(), &namelist, 0, ice_alphasort);
575
 
#else
576
 
    int n = scandir(path.c_str(), &namelist, 0, alphasort);
577
 
#endif
578
 
    if(n < 0)
579
 
    {
580
 
        throw "cannot read directory `" + path + "':\n" + IceUtilInternal::lastErrorToString();
581
 
    }
582
 
 
583
 
    StringSeq result;
584
 
    result.reserve(n - 2);
585
 
 
586
 
    for(int i = 0; i < n; ++i)
587
 
    {
588
 
        string name = namelist[i]->d_name;
589
 
        assert(!name.empty());
590
 
 
591
 
        free(namelist[i]);
592
 
 
593
 
        if(name != ".." && name != ".")
594
 
        {
595
 
            result.push_back(name);
596
 
        }
597
 
    }
598
 
 
599
 
    free(namelist);
600
 
    return result;
601
 
 
602
 
#endif
603
 
}
604
 
 
605
 
void
606
 
IcePatch2::createDirectory(const string& pa)
607
 
{
608
 
    const string path = simplify(pa);
609
 
 
610
 
    if(OS::mkdir(path, 0777) == -1)
611
 
    {
612
 
        if(errno != EEXIST)
613
 
        {
614
 
            throw "cannot create directory `" + path + "':\n" + IceUtilInternal::lastErrorToString();
615
 
        }
616
 
    }
617
 
}
618
 
 
619
 
void
620
 
IcePatch2::createDirectoryRecursive(const string& pa)
621
 
{
622
 
    const string path = simplify(pa);
623
 
 
624
 
    string dir = getDirname(path);
625
 
    if(!dir.empty())
626
 
    {
627
 
        createDirectoryRecursive(dir);
628
 
    }
629
 
 
630
 
    if(!isRoot(path + "/"))
631
 
    {
632
 
        OS::structstat buf;
633
 
        if(OS::osstat(path, &buf) != -1)
634
 
        {
635
 
            if(S_ISDIR(buf.st_mode))
636
 
            {
637
 
                return;
638
 
            }
639
 
        }
640
 
 
641
 
        if(OS::mkdir(path, 0777) == -1)
642
 
        {
643
 
            if(errno != EEXIST)
644
 
            {
645
 
                throw "cannot create directory `" + path + "':\n" + IceUtilInternal::lastErrorToString();
646
 
            }
647
 
        }
648
 
    }
649
 
}
650
 
 
651
 
void
652
 
IcePatch2::compressBytesToFile(const string& pa, const ByteSeq& bytes, Int pos)
653
 
{
654
 
    const string path = simplify(pa);
655
 
 
656
 
    FILE* stdioFile = OS::fopen(path, "wb");
657
 
    if(!stdioFile)
658
 
    {
659
 
        throw "cannot open `" + path + "' for writing:\n" + IceUtilInternal::lastErrorToString();
660
 
    }
661
 
 
662
 
    int bzError;
663
 
    BZFILE* bzFile = BZ2_bzWriteOpen(&bzError, stdioFile, 5, 0, 0);
664
 
    if(bzError != BZ_OK)
665
 
    {
666
 
        string ex = "BZ2_bzWriteOpen failed";
667
 
        if(bzError == BZ_IO_ERROR)
668
 
        {
669
 
            ex += string(": ") + IceUtilInternal::lastErrorToString();
670
 
        }
671
 
        fclose(stdioFile);
672
 
        throw ex;
673
 
    }
674
 
 
675
 
    BZ2_bzWrite(&bzError, bzFile, const_cast<Byte*>(&bytes[pos]), static_cast<int>(bytes.size() - pos));
676
 
    if(bzError != BZ_OK)
677
 
    {
678
 
        string ex = "BZ2_bzWrite failed";
679
 
        if(bzError == BZ_IO_ERROR)
680
 
        {
681
 
            ex += string(": ") + IceUtilInternal::lastErrorToString();
682
 
        }
683
 
        BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0);
684
 
        fclose(stdioFile);
685
 
        throw ex;
686
 
    }
687
 
 
688
 
    BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0);
689
 
    if(bzError != BZ_OK)
690
 
    {
691
 
        string ex = "BZ2_bzWriteClose failed";
692
 
        if(bzError == BZ_IO_ERROR)
693
 
        {
694
 
            ex += string(": ") + IceUtilInternal::lastErrorToString();
695
 
        }
696
 
        fclose(stdioFile);
697
 
        throw ex;
698
 
    }
699
 
 
700
 
    fclose(stdioFile);
701
 
}
702
 
 
703
 
void
704
 
IcePatch2::decompressFile(const string& pa)
705
 
{
706
 
    const string path = simplify(pa);
707
 
    const string pathBZ2 = path + ".bz2";
708
 
 
709
 
    FILE* fp = 0;
710
 
    FILE* stdioFileBZ2 = 0;
711
 
    int bzError;
712
 
    BZFILE* bzFile = 0;
713
 
 
714
 
    try
715
 
    {
716
 
        fp = OS::fopen(path, "wb");
717
 
        if(!fp)
718
 
        {
719
 
            throw "cannot open `" + path + "' for writing:\n" + IceUtilInternal::lastErrorToString();
720
 
        }
721
 
        
722
 
        stdioFileBZ2 = OS::fopen(pathBZ2, "rb");
723
 
        if(!stdioFileBZ2)
724
 
        {
725
 
            throw "cannot open `" + pathBZ2 + "' for reading:\n" + IceUtilInternal::lastErrorToString();
726
 
        }
727
 
 
728
 
#ifdef __BCPLUSPLUS__
729
 
        //
730
 
        // The BZ2_bzReadOpen/BZ2_bzRead/BZ2_bzReadClose functions fail with BCC
731
 
        //
732
 
        OS::structstat buf;
733
 
        if(OS::osstat(pathBZ2, &buf) == -1)
734
 
        {
735
 
            throw "cannot stat `" + pathBZ2 + "':\n" + IceUtilInternal::lastErrorToString();
736
 
        }
737
 
 
738
 
        ByteSeq compressedBytes(buf.st_size);
739
 
        if(fread(&compressedBytes[0], buf.st_size, 1, stdioFileBZ2) != 1)
740
 
        {
741
 
             throw "cannot read from `" + pathBZ2 + "':\n" + IceUtilInternal::lastErrorToString();
742
 
        }
743
 
 
744
 
        ByteSeq uncompressedBytes;
745
 
        unsigned int uncompressedLen = buf.st_size * 2;
746
 
        while(true)
747
 
        {
748
 
            uncompressedBytes.resize(uncompressedLen);
749
 
            int bzError = BZ2_bzBuffToBuffDecompress(&uncompressedBytes[0], &uncompressedLen, &compressedBytes[0], 
750
 
                                                     buf.st_size, 0, 0);
751
 
            if(bzError == BZ_OK)
752
 
            {
753
 
                break;
754
 
            }
755
 
            else if(bzError == BZ_OUTBUFF_FULL)
756
 
            {
757
 
                uncompressedLen *= 2;
758
 
                continue;
759
 
            }
760
 
            else
761
 
            {
762
 
                string ex = "BZ2_bzBuffToBuffDecompress failed";
763
 
                if(bzError == BZ_IO_ERROR)
764
 
                {
765
 
                    ex += string(": ") + IceUtilInternal::lastErrorToString();
766
 
                }
767
 
                throw ex;
768
 
            }
769
 
        }
770
 
 
771
 
        if(fwrite(&uncompressedBytes[0], uncompressedLen, 1, fp) != 1)
772
 
        {
773
 
            throw "cannot write to `" + path + "':\n" + IceUtilInternal::lastErrorToString();
774
 
        }
775
 
#else
776
 
        bzFile = BZ2_bzReadOpen(&bzError, stdioFileBZ2, 0, 0, 0, 0);
777
 
        if(bzError != BZ_OK)
778
 
        {
779
 
            string ex = "BZ2_bzReadOpen failed";
780
 
            if(bzError == BZ_IO_ERROR)
781
 
            {
782
 
                ex += string(": ") + IceUtilInternal::lastErrorToString();
783
 
            }
784
 
            throw ex;
785
 
        }
786
 
        
787
 
        const Int numBZ2 = 64 * 1024;
788
 
        Byte bytesBZ2[numBZ2];
789
 
        
790
 
        while(bzError != BZ_STREAM_END)
791
 
        {
792
 
            int sz = BZ2_bzRead(&bzError, bzFile, bytesBZ2, numBZ2);
793
 
            if(bzError != BZ_OK && bzError != BZ_STREAM_END)
794
 
            {
795
 
                string ex = "BZ2_bzRead failed";
796
 
                if(bzError == BZ_IO_ERROR)
797
 
                {
798
 
                    ex += string(": ") + IceUtilInternal::lastErrorToString();
799
 
                }
800
 
                throw ex;
801
 
            }
802
 
            
803
 
            if(sz > 0)
804
 
            {
805
 
                long pos = ftell(stdioFileBZ2);
806
 
                if(pos == -1)
807
 
                {
808
 
                    throw "cannot get read position for `" + pathBZ2 + "':\n" + IceUtilInternal::lastErrorToString();
809
 
                }
810
 
                
811
 
                if(fwrite(bytesBZ2, sz, 1, fp) != 1)
812
 
                {
813
 
                    throw "cannot write to `" + path + "':\n" + IceUtilInternal::lastErrorToString();
814
 
                }
815
 
            }
816
 
        }
817
 
        
818
 
        BZ2_bzReadClose(&bzError, bzFile);
819
 
        bzFile = 0;
820
 
        if(bzError != BZ_OK)
821
 
        {
822
 
            string ex = "BZ2_bzReadClose failed";
823
 
            if(bzError == BZ_IO_ERROR)
824
 
            {
825
 
                ex += string(": ") + IceUtilInternal::lastErrorToString();
826
 
            }
827
 
            throw ex;
828
 
        }
829
 
#endif
830
 
    }
831
 
    catch(...)
832
 
    {
833
 
        if(bzFile != 0)
834
 
        {
835
 
            BZ2_bzReadClose(&bzError, bzFile);
836
 
        }
837
 
        if(stdioFileBZ2 != 0)
838
 
        {
839
 
            fclose(stdioFileBZ2);
840
 
        }
841
 
        if(fp != 0)
842
 
        {
843
 
            fclose(fp);
844
 
        }
845
 
        throw;
846
 
    }
847
 
 
848
 
    fclose(stdioFileBZ2);
849
 
    fclose(fp);
850
 
}
851
 
 
852
 
void
853
 
IcePatch2::setFileFlags(const string& pa, const FileInfo& info)
854
 
{
855
 
#ifndef _WIN32 // Windows doesn't support the executable flag
856
 
    const string path = simplify(pa);
857
 
    OS::structstat buf;
858
 
    if(OS::osstat(path, &buf) == -1)
859
 
    {
860
 
        throw "cannot stat `" + path + "':\n" + IceUtilInternal::lastErrorToString();
861
 
    }
862
 
    chmod(path.c_str(), info.executable ? buf.st_mode | S_IXUSR : buf.st_mode & ~S_IXUSR);
863
 
#endif
864
 
}
865
 
 
866
 
static bool
867
 
getFileInfoSeqInt(const string& basePath, const string& relPath, int compress, GetFileInfoSeqCB* cb,
868
 
                  FileInfoSeq& infoSeq)
869
 
{
870
 
    if(relPath == checksumFile || relPath == logFile)
871
 
    {
872
 
        return true;
873
 
    }
874
 
 
875
 
    const string path = simplify(basePath + '/' + relPath);
876
 
 
877
 
    if(ignoreSuffix(path))
878
 
    {
879
 
        const string pathWithoutSuffix = getWithoutSuffix(path);
880
 
 
881
 
        if(ignoreSuffix(pathWithoutSuffix))
882
 
        {
883
 
            if(cb && !cb->remove(relPath))
884
 
            {
885
 
                return false;
886
 
            }
887
 
 
888
 
            remove(path); // Removing file with suffix for another file that already has a suffix.
889
 
        }
890
 
        else
891
 
        {
892
 
            OS::structstat buf;
893
 
            if(OS::osstat(getWithoutSuffix(path), &buf) == -1)
894
 
            {
895
 
                if(errno == ENOENT)
896
 
                {
897
 
                    if(cb && !cb->remove(relPath))
898
 
                    {
899
 
                        return false;
900
 
                    }
901
 
 
902
 
                    remove(path); // Removing orphaned file.
903
 
                }
904
 
                else
905
 
                {
906
 
                    throw "cannot stat `" + path + "':\n" + IceUtilInternal::lastErrorToString();
907
 
                }
908
 
            }
909
 
            else if(buf.st_size == 0)
910
 
            {
911
 
                if(cb && !cb->remove(relPath))
912
 
                {
913
 
                    return false;
914
 
                }
915
 
 
916
 
                remove(path); // Removing file with suffix for empty file.
917
 
            }
918
 
        }
919
 
    }
920
 
    else
921
 
    {
922
 
 
923
 
        OS::structstat buf;
924
 
        if(OS::osstat(path, &buf) == -1)
925
 
        {
926
 
            throw "cannot stat `" + path + "':\n" + IceUtilInternal::lastErrorToString();
927
 
        }
928
 
 
929
 
        if(S_ISDIR(buf.st_mode))
930
 
        {
931
 
            FileInfo info;
932
 
            info.path = relPath;
933
 
            info.size = -1;
934
 
            info.executable = false;
935
 
 
936
 
            ByteSeq bytes(relPath.size());
937
 
            copy(relPath.begin(), relPath.end(), bytes.begin());
938
 
 
939
 
            ByteSeq bytesSHA(20);
940
 
            if(!bytes.empty())
941
 
            {
942
 
                SHA1(reinterpret_cast<unsigned char*>(&bytes[0]), bytes.size(),
943
 
                     reinterpret_cast<unsigned char*>(&bytesSHA[0]));
944
 
            }
945
 
            else
946
 
            {
947
 
                fill(bytesSHA.begin(), bytesSHA.end(), 0);
948
 
            }
949
 
            info.checksum.swap(bytesSHA);
950
 
 
951
 
            infoSeq.push_back(info);
952
 
 
953
 
            StringSeq content = readDirectory(path);
954
 
            for(StringSeq::const_iterator p = content.begin(); p != content.end() ; ++p)
955
 
            {
956
 
                if(!getFileInfoSeqInt(basePath, simplify(relPath + '/' + *p), compress, cb, infoSeq))
957
 
                {
958
 
                    return false;
959
 
                }
960
 
            }
961
 
        }
962
 
        else if(S_ISREG(buf.st_mode))
963
 
        {
964
 
            FileInfo info;
965
 
            info.path = relPath;
966
 
            info.size = 0;
967
 
#ifdef _WIN32
968
 
            info.executable = false; // Windows doesn't support the executable flag
969
 
#else
970
 
            info.executable = buf.st_mode & S_IXUSR;
971
 
#endif
972
 
 
973
 
            OS::structstat bufBZ2;
974
 
            const string pathBZ2 = path + ".bz2";
975
 
            bool doCompress = false;
976
 
            if(buf.st_size != 0 && compress > 0)
977
 
            {
978
 
                //
979
 
                // compress == 0: Never compress.
980
 
                // compress == 1: Compress if necessary.
981
 
                // compress >= 2: Always compress.
982
 
                //
983
 
                if(compress >= 2 || OS::osstat(pathBZ2, &bufBZ2) == -1 || buf.st_mtime >= bufBZ2.st_mtime)
984
 
                {
985
 
                    if(cb && !cb->compress(relPath))
986
 
                    {
987
 
                        return false;
988
 
                    }
989
 
 
990
 
                    doCompress = true;
991
 
                }
992
 
                else
993
 
                {
994
 
                    info.size = static_cast<Int>(bufBZ2.st_size);
995
 
                }
996
 
            }
997
 
 
998
 
            if(cb && !cb->checksum(relPath))
999
 
            {
1000
 
                return false;
1001
 
            }
1002
 
 
1003
 
            ByteSeq bytesSHA(20);
1004
 
 
1005
 
            if(relPath.size() + buf.st_size == 0)
1006
 
            {
1007
 
                fill(bytesSHA.begin(), bytesSHA.end(), 0);
1008
 
            }
1009
 
            else
1010
 
            {
1011
 
                SHA_CTX ctx;
1012
 
                SHA1_Init(&ctx);
1013
 
                if(relPath.size() != 0)
1014
 
                {
1015
 
                    SHA1_Update(&ctx, reinterpret_cast<const void*>(relPath.c_str()), relPath.size());
1016
 
                }
1017
 
 
1018
 
                if(buf.st_size != 0)
1019
 
                {
1020
 
#ifdef __BCPLUSPLUS__
1021
 
                    //
1022
 
                    // The BZ2_bzWriteOpen/BZ2_bzWrite/BZ2_bzWriteClose functions fail with BCC
1023
 
                    //
1024
 
                    if(doCompress)
1025
 
                    {
1026
 
                        int fd = OS::open(path.c_str(), O_BINARY|O_RDONLY);
1027
 
                        if(fd == -1)
1028
 
                        {
1029
 
                            throw "cannot open `" + path + "' for reading:\n" + IceUtilInternal::lastErrorToString();
1030
 
                        }
1031
 
 
1032
 
                        ByteSeq uncompressedBytes(buf.st_size);
1033
 
                    
1034
 
                        if(read(fd, &uncompressedBytes[0], buf.st_size) == -1)
1035
 
                        {
1036
 
                            close(fd);
1037
 
                            throw "cannot read from `" + path + "':\n" + IceUtilInternal::lastErrorToString();
1038
 
                        }
1039
 
 
1040
 
                        unsigned int compressedLen = buf.st_size * 1.01 + 600;
1041
 
                        ByteSeq compressedBytes(compressedLen);
1042
 
 
1043
 
                        int bzError = BZ2_bzBuffToBuffCompress(&compressedBytes[0], &compressedLen, 
1044
 
                                                               &uncompressedBytes[0], buf.st_size, 5, 0, 0);
1045
 
                        if(bzError != BZ_OK)
1046
 
                        {
1047
 
                            string ex = "BZ2_bzBuffToBuffCompress failed";
1048
 
                            if(bzError == BZ_IO_ERROR)
1049
 
                            {
1050
 
                                ex += string(": ") + IceUtilInternal::lastErrorToString();
1051
 
                            }
1052
 
                            close(fd);
1053
 
                            throw ex;
1054
 
                        }
1055
 
                        close(fd);
1056
 
 
1057
 
                        const string pathBZ2Temp = path + ".bz2temp";
1058
 
                        FILE* stdioFile = OS::fopen(pathBZ2Temp, "wb");
1059
 
                        if(fwrite(&compressedBytes[0], compressedLen, 1, stdioFile) != 1)
1060
 
                        {
1061
 
                            fclose(stdioFile);
1062
 
                            throw "cannot write to `" + pathBZ2Temp + "':\n" + IceUtilInternal::lastErrorToString();
1063
 
                        }
1064
 
                        fclose(stdioFile);
1065
 
 
1066
 
                        rename(pathBZ2Temp, pathBZ2);
1067
 
 
1068
 
                        info.size = compressedLen;
1069
 
                    }
1070
 
#endif
1071
 
 
1072
 
                    int fd = OS::open(path.c_str(), O_BINARY|O_RDONLY);
1073
 
                    if(fd == -1)
1074
 
                    {
1075
 
                        throw "cannot open `" + path + "' for reading:\n" + IceUtilInternal::lastErrorToString();
1076
 
                    }
1077
 
 
1078
 
#ifndef __BCPLUSPLUS__
1079
 
                    const string pathBZ2Temp = path + ".bz2temp";
1080
 
                    FILE* stdioFile = 0;
1081
 
                    int bzError = 0;
1082
 
                    BZFILE* bzFile = 0;
1083
 
                    if(doCompress)
1084
 
                    {
1085
 
                        stdioFile = OS::fopen(simplify(pathBZ2Temp), "wb");
1086
 
                        if(!stdioFile)
1087
 
                        {
1088
 
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
1089
 
                            _close(fd);
1090
 
#else
1091
 
                            close(fd);
1092
 
#endif
1093
 
                            throw "cannot open `" + pathBZ2Temp + "' for writing:\n" + IceUtilInternal::lastErrorToString();
1094
 
                        }
1095
 
 
1096
 
                        bzFile = BZ2_bzWriteOpen(&bzError, stdioFile, 5, 0, 0);
1097
 
                        if(bzError != BZ_OK)
1098
 
                        {
1099
 
                            string ex = "BZ2_bzWriteOpen failed";
1100
 
                            if(bzError == BZ_IO_ERROR)
1101
 
                            {
1102
 
                            ex += string(": ") + IceUtilInternal::lastErrorToString();
1103
 
                            }
1104
 
                            fclose(stdioFile);
1105
 
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
1106
 
                            _close(fd);
1107
 
#else
1108
 
                            close(fd);
1109
 
#endif
1110
 
                            throw ex;
1111
 
                        }
1112
 
                    }
1113
 
#endif
1114
 
 
1115
 
                    unsigned int bytesLeft = static_cast<unsigned int>(buf.st_size);
1116
 
                    while(bytesLeft > 0)
1117
 
                    {
1118
 
                        ByteSeq bytes(min(bytesLeft, 1024u*1024));
1119
 
                        if(
1120
 
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
1121
 
                            _read(fd, &bytes[0], static_cast<unsigned int>(bytes.size()))
1122
 
#else
1123
 
                            read(fd, &bytes[0], static_cast<unsigned int>(bytes.size()))
1124
 
#endif
1125
 
                            == -1)
1126
 
                        {
1127
 
#ifndef __BCPLUSPLUS__
1128
 
                            if(doCompress)
1129
 
                            {
1130
 
                                fclose(stdioFile);
1131
 
                            }
1132
 
#endif
1133
 
 
1134
 
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
1135
 
                            _close(fd);
1136
 
#else
1137
 
                            close(fd);
1138
 
#endif
1139
 
                            throw "cannot read from `" + path + "':\n" + IceUtilInternal::lastErrorToString();
1140
 
                        }
1141
 
                        bytesLeft -= static_cast<unsigned int>(bytes.size());
1142
 
 
1143
 
#ifndef __BCPLUSPLUS__
1144
 
                        if(doCompress)
1145
 
                        {
1146
 
                            BZ2_bzWrite(&bzError, bzFile, const_cast<Byte*>(&bytes[0]), static_cast<int>(bytes.size()));
1147
 
                            if(bzError != BZ_OK)
1148
 
                            {
1149
 
                                string ex = "BZ2_bzWrite failed";
1150
 
                                if(bzError == BZ_IO_ERROR)
1151
 
                                {
1152
 
                                    ex += string(": ") + IceUtilInternal::lastErrorToString();
1153
 
                                }
1154
 
                                BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0);
1155
 
                                fclose(stdioFile);
1156
 
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
1157
 
                                _close(fd);
1158
 
#else
1159
 
                                close(fd);
1160
 
#endif
1161
 
                                throw ex;
1162
 
                            }
1163
 
                        }
1164
 
#endif
1165
 
 
1166
 
                        SHA1_Update(&ctx, reinterpret_cast<const void*>(&bytes[0]), bytes.size());
1167
 
                    }
1168
 
 
1169
 
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
1170
 
                    _close(fd);
1171
 
#else
1172
 
                    close(fd);
1173
 
#endif
1174
 
 
1175
 
#ifndef __BCPLUSPLUS__
1176
 
                    if(doCompress)
1177
 
                    {
1178
 
                        BZ2_bzWriteClose(&bzError, bzFile, 0, 0, 0);
1179
 
                        if(bzError != BZ_OK)
1180
 
                        {
1181
 
                            string ex = "BZ2_bzWriteClose failed";
1182
 
                            if(bzError == BZ_IO_ERROR)
1183
 
                            {
1184
 
                                ex += string(": ") + IceUtilInternal::lastErrorToString();
1185
 
                            }
1186
 
                            fclose(stdioFile);
1187
 
                            throw ex;
1188
 
                        }
1189
 
 
1190
 
                        fclose(stdioFile);
1191
 
 
1192
 
                        rename(pathBZ2Temp, pathBZ2);
1193
 
 
1194
 
                        if(OS::osstat(pathBZ2, &bufBZ2) == -1)
1195
 
                        {
1196
 
                            throw "cannot stat `" + pathBZ2 + "':\n" + IceUtilInternal::lastErrorToString();
1197
 
                        }
1198
 
 
1199
 
                        info.size = static_cast<Int>(bufBZ2.st_size);
1200
 
                    }
1201
 
#endif
1202
 
                }
1203
 
 
1204
 
                SHA1_Final(reinterpret_cast<unsigned char*>(&bytesSHA[0]), &ctx);
1205
 
            }
1206
 
 
1207
 
            info.checksum.swap(bytesSHA);
1208
 
 
1209
 
            infoSeq.push_back(info);
1210
 
        }
1211
 
    }
1212
 
 
1213
 
    return true;
1214
 
}
1215
 
 
1216
 
bool
1217
 
IcePatch2::getFileInfoSeq(const string& basePath, int compress, GetFileInfoSeqCB* cb,
1218
 
                          FileInfoSeq& infoSeq)
1219
 
{
1220
 
    return getFileInfoSeqSubDir(basePath, ".", compress, cb, infoSeq);
1221
 
}
1222
 
 
1223
 
bool
1224
 
IcePatch2::getFileInfoSeqSubDir(const string& basePa, const string& relPa, int compress, GetFileInfoSeqCB* cb,
1225
 
                                FileInfoSeq& infoSeq)
1226
 
{
1227
 
    const string basePath = simplify(basePa);
1228
 
    const string relPath = simplify(relPa);
1229
 
 
1230
 
    if(!getFileInfoSeqInt(basePath, relPath, compress, cb, infoSeq))
1231
 
    {
1232
 
        return false;
1233
 
    }
1234
 
 
1235
 
    sort(infoSeq.begin(), infoSeq.end(), FileInfoLess());
1236
 
    infoSeq.erase(unique(infoSeq.begin(), infoSeq.end(), FileInfoEqual()), infoSeq.end());
1237
 
 
1238
 
    return true;
1239
 
}
1240
 
 
1241
 
void
1242
 
IcePatch2::saveFileInfoSeq(const string& pa, const FileInfoSeq& infoSeq)
1243
 
{
1244
 
    {
1245
 
        const string path = simplify(pa + '/' + checksumFile);
1246
 
        
1247
 
        FILE* fp = OS::fopen(path, "w");
1248
 
        if(!fp)
1249
 
        {
1250
 
            throw "cannot open `" + path + "' for writing:\n" + IceUtilInternal::lastErrorToString();
1251
 
        }
1252
 
        try
1253
 
        {
1254
 
            for(FileInfoSeq::const_iterator p = infoSeq.begin(); p != infoSeq.end(); ++p)
1255
 
            {
1256
 
                if(!writeFileInfo(fp, *p))
1257
 
                {
1258
 
                    throw "error writing `" + path + "':\n" + IceUtilInternal::lastErrorToString();
1259
 
                }
1260
 
            }
1261
 
        }
1262
 
        catch(...)
1263
 
        {
1264
 
            fclose(fp);
1265
 
            throw;
1266
 
        }
1267
 
        fclose(fp);
1268
 
    }
1269
 
 
1270
 
    {
1271
 
        const string pathLog = simplify(pa + '/' + logFile);
1272
 
 
1273
 
        try
1274
 
        {
1275
 
            remove(pathLog);
1276
 
        }
1277
 
        catch(...)
1278
 
        {
1279
 
        }
1280
 
    }
1281
 
}
1282
 
 
1283
 
void
1284
 
IcePatch2::loadFileInfoSeq(const string& pa, FileInfoSeq& infoSeq)
1285
 
{
1286
 
    {
1287
 
        const string path = simplify(pa + '/' + checksumFile);
1288
 
 
1289
 
        FILE* fp = OS::fopen(path, "r");
1290
 
        if(!fp)
1291
 
        {
1292
 
            throw "cannot open `" + path + "' for reading:\n" + IceUtilInternal::lastErrorToString();
1293
 
        }
1294
 
 
1295
 
        while(true)
1296
 
        {
1297
 
            FileInfo info;
1298
 
            if(readFileInfo(fp, info))
1299
 
            {
1300
 
                infoSeq.push_back(info);
1301
 
            }
1302
 
            else
1303
 
            {
1304
 
                break;
1305
 
            }
1306
 
        }
1307
 
        fclose(fp);
1308
 
 
1309
 
        sort(infoSeq.begin(), infoSeq.end(), FileInfoLess());
1310
 
        infoSeq.erase(unique(infoSeq.begin(), infoSeq.end(), FileInfoEqual()), infoSeq.end());
1311
 
    }
1312
 
 
1313
 
    {
1314
 
        const string pathLog = simplify(pa + '/' + logFile);
1315
 
 
1316
 
        FILE* fp = OS::fopen(pathLog, "r");
1317
 
        if(fp != 0)
1318
 
        {
1319
 
            FileInfoSeq remove;
1320
 
            FileInfoSeq update;
1321
 
            
1322
 
            while(true)
1323
 
            {
1324
 
                int c = fgetc(fp);
1325
 
                if(c == EOF)
1326
 
                {
1327
 
                    break;
1328
 
                }
1329
 
 
1330
 
                FileInfo info;
1331
 
                if(!readFileInfo(fp, info))
1332
 
                {
1333
 
                    break;
1334
 
                }
1335
 
 
1336
 
                if(c == '-')
1337
 
                {
1338
 
                    remove.push_back(info);
1339
 
                }
1340
 
                else if(c == '+')
1341
 
                {
1342
 
                    update.push_back(info);
1343
 
                }
1344
 
            }
1345
 
            fclose(fp);
1346
 
 
1347
 
            sort(remove.begin(), remove.end(), FileInfoLess());
1348
 
            remove.erase(unique(remove.begin(), remove.end(), FileInfoEqual()), remove.end());
1349
 
 
1350
 
            sort(update.begin(), update.end(), FileInfoLess());
1351
 
            update.erase(unique(update.begin(), update.end(), FileInfoEqual()), update.end());
1352
 
 
1353
 
            FileInfoSeq newInfoSeq;
1354
 
            newInfoSeq.reserve(infoSeq.size());
1355
 
            
1356
 
            set_difference(infoSeq.begin(),
1357
 
                           infoSeq.end(),
1358
 
                           remove.begin(),
1359
 
                           remove.end(),
1360
 
                           back_inserter(newInfoSeq),
1361
 
                           FileInfoLess());
1362
 
            
1363
 
            infoSeq.swap(newInfoSeq);
1364
 
            
1365
 
            newInfoSeq.clear();
1366
 
            newInfoSeq.reserve(infoSeq.size());
1367
 
 
1368
 
            set_union(infoSeq.begin(),
1369
 
                      infoSeq.end(),
1370
 
                      update.begin(),
1371
 
                      update.end(),
1372
 
                      back_inserter(newInfoSeq),
1373
 
                      FileInfoLess());
1374
 
            
1375
 
            infoSeq.swap(newInfoSeq);
1376
 
 
1377
 
            saveFileInfoSeq(pa, infoSeq);
1378
 
        }
1379
 
    }
1380
 
}
1381
 
 
1382
 
void
1383
 
IcePatch2::getFileTree0(const FileInfoSeq& infoSeq, FileTree0& tree0)
1384
 
{
1385
 
    tree0.nodes.resize(256);
1386
 
    tree0.checksum.resize(20);
1387
 
 
1388
 
    ByteSeq allChecksums0;
1389
 
    allChecksums0.resize(256 * 20);
1390
 
    ByteSeq::iterator c0 = allChecksums0.begin();
1391
 
 
1392
 
    for(int i = 0; i < 256; ++i, c0 += 20)
1393
 
    {
1394
 
        FileTree1& tree1 = tree0.nodes[i];
1395
 
 
1396
 
        tree1.files.clear();
1397
 
        tree1.checksum.resize(20);
1398
 
        
1399
 
        FileInfoSeq::const_iterator p;
1400
 
        
1401
 
        for(p = infoSeq.begin(); p != infoSeq.end(); ++p)
1402
 
        {
1403
 
            if(i == static_cast<int>(p->checksum[0]))
1404
 
            {
1405
 
                tree1.files.push_back(*p);
1406
 
            }
1407
 
        }
1408
 
 
1409
 
        ByteSeq allChecksums1;
1410
 
        allChecksums1.resize(tree1.files.size() * 21); // 20 bytes for the checksum + 1 byte for the flag
1411
 
        ByteSeq::iterator c1 = allChecksums1.begin();
1412
 
 
1413
 
        for(p = tree1.files.begin(); p != tree1.files.end(); ++p, c1 += 21)
1414
 
        {
1415
 
            copy(p->checksum.begin(), p->checksum.end(), c1);
1416
 
            *(c1 + 20) = p->executable;
1417
 
        }
1418
 
        
1419
 
        if(!allChecksums1.empty())
1420
 
        {
1421
 
            SHA1(reinterpret_cast<unsigned char*>(&allChecksums1[0]), allChecksums1.size(),
1422
 
                 reinterpret_cast<unsigned char*>(&tree1.checksum[0]));
1423
 
        }
1424
 
        else
1425
 
        {
1426
 
            fill(tree1.checksum.begin(), tree1.checksum.end(), 0);
1427
 
        }
1428
 
 
1429
 
        copy(tree1.checksum.begin(), tree1.checksum.end(), c0);
1430
 
    }
1431
 
 
1432
 
    if(!allChecksums0.empty())
1433
 
    {
1434
 
        SHA1(reinterpret_cast<unsigned char*>(&allChecksums0[0]), allChecksums0.size(),
1435
 
             reinterpret_cast<unsigned char*>(&tree0.checksum[0]));
1436
 
    }
1437
 
    else
1438
 
    {
1439
 
        fill(tree0.checksum.begin(), tree0.checksum.end(), 0);
1440
 
    }
1441
 
}
1442