This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bob.cc
1671 lines (1539 loc) · 48.8 KB
/
bob.cc
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// bob.cc - part of the GAP installer BOB
//
// Copyright (C) by Max Neunhoeffer 2012
// This file is free software, see license information at the end.
//
#include "bob.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <archive.h>
#include <archive_entry.h>
#include <curl/curl.h>
#include "hl_sha1.h"
using namespace std;
namespace BOB {
vector<Test *> &alltests(void)
{
static vector<Test *> *alltests_ = new vector<Test *>;
return *alltests_;
}
bool compareTestPtrs(Test *a, Test *b)
{
return a->name < b->name;
}
Test::Test(string name, int phase, testfunc_t tester)
:name(name),phase(phase),tester(tester),num(0),res(UNKNOWN)
{
vector<Test *>::iterator pos
= lower_bound(alltests().begin(),alltests().end(),this,
compareTestPtrs);
alltests().insert(pos,this);
}
Test *Test::find(string name)
{
static vector<Test *> &tests = alltests();
int i = 0;
int j = tests.size();
int k;
int res;
// invariant: the right position is >= i and < j
while (j-i >= 1) {
k = (i+j)/2; // we always have i <= k < j
res = name.compare(tests[k]->name);
if (res < 0) // name < tests[k]->name
j = k;
else if (res > 0) // name > tests[k]->name
i = k+1;
else // we found it
return tests[k];
}
return NULL;
}
vector<Component *> &allcomps(void)
{
static vector<Component *> *allcomponents_ = new vector<Component *>;
return *allcomponents_;
}
bool compareComponentPtrs(Component *a, Component *b)
{
return a->name < b->name;
}
Component::Component(string name, const char *dependencies[],
prereqfunc prerequisites, workerfunc getter, workerfunc builder)
: name(name), prereq(prerequisites), prereqres(UNKNOWN),
get(getter), getres(UNKNOWN), build(builder), buildres(UNKNOWN)
{
int i = 0;
const char *p;
while (true) {
p = dependencies[i++];
if (!p) break;
depends.push_back(string(p));
}
vector<Component *>::iterator pos
= lower_bound(allcomps().begin(),allcomps().end(),this,
compareComponentPtrs);
allcomps().insert(pos,this);
}
int Component::findnr(string name)
{
static vector<Component *> &comps = allcomps();
int i = 0;
int j = comps.size();
int k;
int res;
// invariant: the right position is >= i and < j
while (j-i >= 1) {
k = (i+j)/2; // we always have i <= k < j
res = name.compare(comps[k]->name);
if (res < 0) // name < tests[k]->name
j = k;
else if (res > 0) // name > tests[k]->name
i = k+1;
else // we found it
return k;
}
return -1;
}
Component *Component::find(string name)
{
int pos = Component::findnr(name);
if (pos < 0) return NULL;
else return allcomps().at(pos);
}
// Some standard tests:
static int Have_make_Test(string &st)
{
if (which("make",st)) return 0;
else {
st = "";
return -1;
}
}
Test Have_make("Have_make",1,Have_make_Test);
static int Which_C_Compiler_Test(string &st)
{
string CC = getenvironment("CC");
if (CC.size() > 0 && which(CC,st)) return 0;
if (which("gcc",st)) return 0;
if (which("cc",st)) return 0;
if (which("CC",st)) return 0;
st = "";
return -1;
}
Test Which_C_Compiler("Which_C_Compiler",1,Which_C_Compiler_Test);
static int C_Compiler_Name_Test(string &st)
{
if (Which_C_Compiler.num != 0) return -1;
size_t pos = Which_C_Compiler.str.rfind('/');
if (pos == string::npos)
st = Which_C_Compiler.str;
else
st = Which_C_Compiler.str.substr(pos+1);
return 0;
}
Test C_Compiler_Name("C_Compiler_Name",2,C_Compiler_Name_Test);
static int Which_Architecture_Test(string &st)
{
#if SYS_IS_WINDOWS
st = "WINDOWS";
return 0;
#endif
#if SYS_IS_LINUX
st = "LINUX";
return 0;
#endif
#if SYS_IS_OSX
st = "OSX";
return 0;
#endif
#if SYS_IS_BSD
st = "BSD";
return 0;
#endif
st = "UNKNOWN";
return 0;
}
Test Which_Architecture("Which_Architecture",1,Which_Architecture_Test);
static int Which_OS_Variant_Test(string &st)
{
string path;
#if SYS_IS_LINUX
if (which("apt-get",path)) {
st = "apt-get";
return 0;
} else if (which("rpm",path)) {
st = "rpm";
return 0;
} else if (which("emerge",path)) {
st = "emerge";
return 0;
}
#endif
#if SYS_IS_OSX
if (which("fink",path)) {
st = "fink";
return 0;
} else if (which("brew",path)) {
st = "homebrew";
return 0;
} else if (which("ports",path)) {
st = "macports";
return 0;
}
st = "none";
return 0;
#endif
st = "unknown";
return 0;
}
Test Which_OS_Variant("Which_OS_Variant",2,Which_OS_Variant_Test);
static int Which_Wordsize_Test(string &st)
{
if (Which_C_Compiler.num != 0) return 0;
fstream testprog("/tmp/wordsize.c",fstream::out | fstream::trunc);
testprog << "#include <stdio.h>" << endl;
testprog << "int main(void) {" << endl;
testprog << " printf(\"%ld\\n\",sizeof(void *));" << endl;
testprog << " return 0;" << endl;
testprog << "}" << endl;
testprog.close();
try { sh(Which_C_Compiler.str+" /tmp/wordsize.c -o /tmp/wordsize",0,true); }
catch (Status e) { return 0; }
FILE *prog = popen("/tmp/wordsize","r");
if (prog == NULL) return 0;
int size,ret;
ret = fscanf(prog,"%d",&size);
fclose(prog);
unlink("/tmp/wordsize.c");
unlink("/tmp/wordsize");
size = ret == 1 ? size*8 : 0;
stringstream output;
output << "Word size is " << size << "bit.";
out(OK,output.str());
st = output.str();
return size;
}
Test Which_Wordsize("Which_Wordsize",2,Which_Wordsize_Test);
Status Have_C_Header(string headername, bool m32)
{
string m32opt = m32 ? " -m32" : "";
if (Which_C_Compiler.num != 0) return ERROR;
fstream testprog("/tmp/headertest.c",fstream::out | fstream::trunc);
testprog << "#include <" << headername << ">" << endl;
testprog << "int main(void) {" << endl;
testprog << " return 0;" << endl;
testprog << "}" << endl;
testprog.close();
try {
sh(Which_C_Compiler.str+" "+getenvironment("CPPFLAGS")+" "+
getenvironment("CFLAGS")+m32opt+
" -E /tmp/headertest.c -o /tmp/headertest",0,true);
}
catch (Status e) { return ERROR; }
unlink("/tmp/headertest.c");
unlink("/tmp/headertest");
return OK;
}
Status Have_C_Library(string lib, bool m32)
{
string m32opt = m32 ? " -m32" : "";
if (Which_C_Compiler.num != 0) return ERROR;
fstream testprog("/tmp/libtest.c",fstream::out | fstream::trunc);
testprog << "int main(void) {" << endl;
testprog << " return 0;" << endl;
testprog << "}" << endl;
testprog.close();
try {
sh(Which_C_Compiler.str+" "+getenvironment("CPPFLAGS")+" "+
getenvironment("CFLAGS")+m32opt+" /tmp/libtest.c -o /tmp/libtest "+
getenvironment("LDFLAGS")+" "+lib,0,true);
}
catch (Status e) { return ERROR; }
unlink("/tmp/libtest.c");
unlink("/tmp/libtest");
return OK;
}
static int Can_Compile_32bit_Test(string &st)
{
if (Which_C_Compiler.num != 0 ||
Which_Wordsize.num != 64 ||
(C_Compiler_Name.str != "gcc" && C_Compiler_Name.str != "clang")) {
st = "no";
return -1;
}
out(OK,"We are on a 64-bit system using gcc or clang.");
out(OK,"Checking whether or not we can compile in 32-bit mode...");
fstream testprog("/tmp/lib32test.c",fstream::out | fstream::trunc);
testprog << "int main(void) {" << endl;
testprog << " return 0;" << endl;
testprog << "}" << endl;
testprog.close();
try {
sh(Which_C_Compiler.str+" "+getenvironment("CPPFLAGS")+" "+
getenvironment("CFLAGS")+" -m32 /tmp/lib32test.c -o /tmp/lib32test "+
getenvironment("LDFLAGS"),0,true);
}
catch (Status e) {
st = "no";
out(OK,"No, we cannot compile 32-bit mode executables.");
if (Which_Architecture.str == "LINUX") {
out(ADVICE,"If you are running a debian-like Linux, you can "
"install the");
out(ADVICE,"necessary tools by doing:");
out(ADVICE," apt-get install ia32-libs libc6-dev-i386 "
"lib32readline5-dev");
out(ADVICE,"with root privileges (using su or sudo).");
}
return -1;
}
unlink("/tmp/lib32test.c");
unlink("/tmp/lib32test");
st = "yes";
out(OK,"Yes, we can compile 32-bit mode executables.");
return 0;
}
Test Can_Compile_32bit("Can_Compile_32bit",3,Can_Compile_32bit_Test);
static int Double_Compile_Test(string &st)
{
if (Which_Wordsize.num == 64 &&
(C_Compiler_Name.str == "gcc" || C_Compiler_Name.str == "clang") &&
Can_Compile_32bit.num == 0) {
out(OK,"Performing 64-bit and 32-bit compilation.");
st = "DoubleCompile";
return 1;
} else {
st = "SingleCompile";
out(OK,"Performing single compilation.");
return 0;
}
}
Test Double_Compile("Double_Compile",4,Double_Compile_Test);
// Access to the environment:
vector<string> envkeys;
vector<string> envvals;
void initenvironment(char *environ[])
{
char *p,*q;
int i;
string key;
string val;
vector<string>::iterator pos;
for (i = 0,p = environ[0];p;p = environ[++i]) {
q = strchr(p,'=');
key = string(p,0,q-p);
val = string(q+1);
pos = lower_bound(envkeys.begin(),envkeys.end(),key);
envvals.insert(envvals.begin() + (pos - envkeys.begin()),val);
envkeys.insert(pos,key);
}
}
string getenvironment(string key)
{
vector<string>::iterator pos;
pos = lower_bound(envkeys.begin(),envkeys.end(),key);
if (pos >= envkeys.end() || *pos != key) {
return "";
} else
return envvals[pos - envkeys.begin()];
}
void setenvironment(string key, string val)
{
vector<string>::iterator pos;
pos = lower_bound(envkeys.begin(),envkeys.end(),key);
if (pos >= envkeys.end() || *pos != key) {
envvals.insert(envvals.begin() + (pos-envkeys.begin()),val);
envkeys.insert(pos,key);
} else
envvals[pos-envkeys.begin()] = val;
}
void delenvironment(string key)
{
vector<string>::iterator pos;
pos = lower_bound(envkeys.begin(),envkeys.end(),key);
if (pos < envkeys.end() && *pos == key) {
envvals.erase(envvals.begin() + (pos - envkeys.begin()));
envkeys.erase(pos);
}
}
char **prepareenvironment()
{
char **p = new char * [envkeys.size()+1];
char **pp = p;
char *q;
unsigned int i;
for (i = 0;i < envkeys.size();i++) {
q = (char *) malloc(envkeys[i].size()+2+envvals[i].size());
strcpy(q,envkeys[i].c_str());
q[envkeys[i].size()] = '=';
strcpy(q+envkeys[i].size()+1,envvals[i].c_str());
*pp++ = q;
}
*pp++ = NULL;
return p;
}
void freepreparedenvironment(char **p)
{
int i = 0;
char *q;
while (true) {
q = p[i++];
if (q == NULL) break;
free(q);
}
delete[] p;
}
// Utility functions:
int verbose = 3;
bool nonetwork = false;
string boblogfilename;
string buildlogfilename;
void out(Status severity, string msg)
{
fstream outs(boblogfilename.c_str(),fstream::out | fstream::app);
outs << msg << endl;
outs.close();
if (verbose >= 3 ||
(verbose >= 2 && severity == WARN) ||
(verbose >= 1 && severity == ERROR)) {
if (verbose >= 4) cout << "BOB:";
if (severity == WARN) cout << "Warning:";
else if (severity == ADVICE) cout << "Advice:";
else if (severity == ERROR) cout << "Error:";
cout << msg << endl;
}
}
bool which(string name, string &res)
{
size_t pos,posold;
string absname;
pos = name.find('/');
if (pos == string::npos) {
string path = getenvironment("PATH");
pos = path.find(':');
posold = 0;
while (posold != string::npos) {
if (pos == string::npos)
absname = path.substr(posold);
else
absname = path.substr(posold,pos-posold);
if (absname[absname.size()-1] != '/') absname.push_back('/');
absname += name;
if (access(absname.c_str(),X_OK) == 0) {
res = absname;
return true;
}
if (pos == string::npos)
posold = string::npos;
else {
posold = pos+1;
pos = path.find(':',pos+1);
}
}
res = "";
return false;
} else { // We are given a pathname
if (access(name.c_str(),X_OK) == 0) {
res = name;
return true;
} else {
res = "";
return false;
}
}
}
bool exists(string filename)
{
struct stat stFileInfo;
return (stat(filename.c_str(), &stFileInfo) == 0);
}
bool isdir(string dirname)
{
struct stat stFileInfo;
return stat(dirname.c_str(), &stFileInfo) == 0 &&
S_ISDIR(stFileInfo.st_mode);
}
static CURL *curl = NULL;
void shutdowncurl()
{
if (curl) {
curl_easy_cleanup(curl);
curl = NULL;
curl_global_cleanup();
}
}
Status downloadname(string targetdir, string url, string &localname)
{
size_t pos = url.rfind('/');
if (pos == string::npos) {
out(ERROR,"Given URL does not contain /.");
return ERROR;
}
localname = targetdir+"bobdownloads/"+url.substr(pos+1);
return OK;
}
Status download(string url, string localname)
{
CURLcode res;
if (curl == NULL) {
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl == NULL) {
out(ERROR,"Cannot initialize curl library.");
return ERROR;
}
atexit(shutdowncurl);
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
FILE *outs = fopen(localname.c_str(),"w");
if (outs == NULL) {
out(ERROR,"Cannot write to "+localname+" .");
return ERROR;
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, outs);
if (verbose >= 3)
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
else
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
res = curl_easy_perform(curl);
if (res != 0) {
stringstream msg(stringstream::out);
msg << "Curl error " << res << ".";
out(ERROR,msg.str());
fclose(outs);
return ERROR;
}
if (fclose(outs) != 0) {
out(ERROR,"Cannot close "+localname+" .");
return ERROR;
}
return OK;
}
void get(string targetdir, string url, string &filename, bool alwaysget)
{
Status res;
if (downloadname(targetdir,url,filename) == ERROR) throw ERROR;
if (!alwaysget && access(filename.c_str(),R_OK) == 0) {
out(OK,"Already have "+url);
return;
}
res = download(url,filename);
if (res != OK) throw res;
}
unsigned char *sha1(FILE *f)
{
HL_SHA1_CTX c;
SHA1 sha;
static hl_uint8 md[SHA1HashSize];
int i;
unsigned char buf[4096];
sha.SHA1Reset(&c);
while (true) {
i = fread(buf,1,4096,f);
if (i <= 0) break;
sha.SHA1Input(&c,(const hl_uint8 *) buf,(unsigned int)i);
}
sha.SHA1Result(&c,md);
return (unsigned char *)md;
}
static inline int hexdig(char c)
{
if (c >= '0' && c <= '9') return (int) (c-'0');
else if (c >= 'a' && c <= 'f') return (int) (c-'a'+10);
else if (c >= 'A' && c <= 'F') return (int) (c-'A'+19);
return 0;
}
Status checksha1(string filename, string hash)
{
if (hash.size() < 40) return OK;
FILE *f = fopen(filename.c_str(),"r");
if (f == NULL) return ERROR;
unsigned char *md = sha1(f);
fclose(f);
int i;
for (i = 0;i < 20;i++)
if (md[i] != hexdig(hash[2*i])*16+hexdig(hash[2*i+1]))
return ERROR;
return OK;
}
void getind(string targetdir, string url, string &archivename)
{
string filename;
string url2;
string hash;
Status res;
size_t pos;
out(OK,"Getting link file...");
if (nonetwork)
get(targetdir, url, filename, false);
// WARN or ERROR exception is handed through
else
get(targetdir, url, filename, true);
// WARN or ERROR exception is handed through
fstream file;
file.exceptions ( ifstream::failbit | ifstream::badbit );
try {
file.open(filename.c_str(),fstream::in);
}
catch (ifstream::failure e) {
out(ERROR,"Could not write link file "+filename+" .");
throw ERROR;
}
try {
getline(file,url2);
if (url2 != "BOB") {
file.close();
out(ERROR,"Link file "+filename+" corrupt.");
throw ERROR;
}
getline(file,url2);
getline(file,hash);
file.close();
}
catch (ifstream::failure e) {
file.close();
out(ERROR,"Could not read link file "+filename+" .");
throw ERROR;
}
// Now check if it is already there:
if (downloadname(targetdir,url2,archivename) == ERROR) throw ERROR;
pos = archivename.rfind('/');
if (pos != string::npos)
out(OK,string("Archive name: ")+archivename.substr(pos+1));
// Is it already there?
if (access(archivename.c_str(),R_OK) == 0) {
out(OK,"Checking sha1 checksum of file that was found...");
if (checksha1(archivename,hash) == OK) return;
out(OK,"Checksum of archive found is wrong, downloading it again...");
}
// Now get it:
res = download(url2,archivename);
if (res == ERROR) throw ERROR;
// Now check it (again):
out(OK,"Checking sha1 checksum of downloaded file...");
res = checksha1(archivename,hash);
if (res == ERROR) {
out(ERROR,"SHA1 checksum of downloaded file "+archivename+
" was wrong!");
throw ERROR;
}
}
static int copy_data(struct archive *ar, struct archive *aw)
{
int r;
const void *buff;
size_t size;
int64_t offset;
for (;;) {
r = archive_read_data_block(ar, &buff, &size, &offset);
if (r == ARCHIVE_EOF) return ARCHIVE_OK;
if (r != ARCHIVE_OK) return r;
r = archive_write_data_block(aw, buff, size, offset);
if (r != ARCHIVE_OK) {
out(ERROR,string("Unpack write error: ")+
archive_error_string(aw));
return r;
}
}
}
void unpack(string archivename)
{
struct archive *a;
struct archive *ext;
struct archive_entry *entry;
int r;
const char *filename = archivename.c_str();
Status res = ERROR;
a = archive_read_new();
archive_read_support_format_tar(a);
archive_read_support_format_zip(a);
archive_read_support_compression_gzip(a);
archive_read_support_compression_bzip2(a);
archive_read_support_compression_xz(a);
archive_read_support_compression_compress(a);
if (filename != NULL && strcmp(filename, "-") == 0) filename = NULL;
if ((r = archive_read_open_filename(a, filename, 10240))) {
out(ERROR,"Cannot read archive "+archivename+"\nMessage: "+
archive_error_string(a));
archive_read_finish(a);
throw ERROR;
}
ext = archive_write_disk_new();
archive_write_disk_set_options(ext, ARCHIVE_EXTRACT_TIME);
archive_write_disk_set_standard_lookup(ext);
while (true) { // will be left by break
r = archive_read_next_header(a, &entry);
if (r == ARCHIVE_EOF) { res = OK; break; }
if (r != ARCHIVE_OK) {
out(ERROR,"Bad entry in archive "+archivename+"\nMessage: "+
archive_error_string(a));
break;
}
if (verbose >= 4) out(OK,string("x ")+archive_entry_pathname(entry));
r = archive_write_header(ext, entry);
if (r != ARCHIVE_OK) {
out(ERROR,"Could not write header for entry extracting "+
archivename+"\nMessage: "+
archive_error_string(ext));
break;
} else {
copy_data(a, ext);
r = archive_write_finish_entry(ext);
if (r != ARCHIVE_OK) {
out(ERROR,"Could not finish extraction of "+
archivename+"\nMessage: "+
archive_error_string(ext));
break;
}
}
}
archive_read_close(a);
archive_read_finish(a);
archive_write_close(ext);
archive_write_finish(ext);
if (res != OK) throw res;
}
static string massagearg(string cmd)
{
string res;
unsigned long i;
res.reserve(cmd.size());
for (i = 0;i < cmd.size();i++) {
if (cmd[i] == '~') {
if (i+1 < cmd.size() && cmd[i+1] == '~') {
res.push_back('~');
i++;
} else
res.push_back(' ');
} else
res.push_back(cmd[i]);
}
return res;
}
void sh(string cmd, int stdinfd, bool quiet)
{
string prog;
string path;
vector<string> args;
vector<char const *> argv;
char **envp;
unsigned int i;
size_t pos,oldpos;
pid_t pid;
// First log command if not quiet:
if (!quiet) {
fstream logfile(buildlogfilename.c_str(),fstream::out | fstream::app);
logfile << "COMMAND:" << cmd << endl;
logfile.close();
}
// Split string into command and arguments:
pos = cmd.find(' ');
if (pos != string::npos) {
prog = cmd.substr(0,pos);
while (true) {
oldpos = pos+1;
pos = cmd.find(' ',oldpos);
if (pos == string::npos) {
if (oldpos < cmd.size()-1)
args.push_back(massagearg(cmd.substr(oldpos)));
break;
}
if (pos > oldpos)
args.push_back(massagearg(cmd.substr(oldpos,pos-oldpos)));
}
} else prog = cmd;
argv.push_back(prog.c_str()); // Give the prog as 0-th argument
for (i = 0;i < args.size();i++)
argv.push_back(args[i].c_str());
argv.push_back(NULL);
if (!which(prog,path)) {
if (!quiet) out(ERROR,"Could not execute command:\n "+cmd);
throw ERROR;
}
int fds[2];
if (pipe(fds) != 0) {
if (!quiet) out(ERROR,"Could not create pipes for:\n "+cmd);
throw ERROR;
}
pid = fork();
if (pid == 0) { // the child
// Open file build.log, dup to stdout and stderr
// Close all other file descriptors
if (stdinfd != 0) dup2(stdinfd,0);
dup2(fds[1],1);
dup2(fds[1],2);
for (int fd = 3;fd < 64;fd++) close(fd);
envp = prepareenvironment();
if (execve(path.c_str(), (char *const *) &(argv[0]),envp) == -1) {
freepreparedenvironment(envp);
cerr << "Errno: " << errno << endl;
if (!quiet) out(ERROR,"Cannot execve.");
exit(17);
}
}
close(fds[1]);
fstream logfile(buildlogfilename.c_str(),fstream::out | fstream::app);
FILE *output = fdopen(fds[0],"r");
setlinebuf(output);
char buf[1024];
char *p;
while (true) {
p = fgets(buf,1024,output);
if (p != NULL) {
if (verbose >= 4) cout << buf;
logfile << buf;
logfile.flush();
}
else break;
}
fclose(output);
logfile.close();
int status;
waitpid(pid,&status,0);
if (WEXITSTATUS(status) != 0) {
if (!quiet) out(ERROR,"Subprocess "+cmd+" returned with an error.");
throw ERROR;
}
}
int shbg(string cmd, int stdinfd, bool quiet)
{
string prog;
string path;
vector<string> args;
vector<char const *> argv;
char **envp;
unsigned int i;
size_t pos,oldpos;
pid_t pid;
// First log command if not quiet:
if (!quiet) {
fstream logfile(buildlogfilename.c_str(),fstream::out | fstream::app);
logfile << "COMMAND:" << cmd << endl;
logfile.close();
}
// Split string into command and arguments:
pos = cmd.find(' ');
if (pos != string::npos) {
prog = cmd.substr(0,pos);
while (true) {
oldpos = pos+1;
pos = cmd.find(' ',oldpos);
if (pos == string::npos) {
if (oldpos < cmd.size()-1)
args.push_back(massagearg(cmd.substr(oldpos)));
break;
}
if (pos > oldpos)
args.push_back(massagearg(cmd.substr(oldpos,pos-oldpos)));
}
} else prog = cmd;
argv.push_back(prog.c_str()); // Give the prog as 0-th argument
for (i = 0;i < args.size();i++)
argv.push_back(args[i].c_str());
argv.push_back(NULL);
if (!which(prog,path)) {
if (!quiet) out(ERROR,"Could not execute command:\n "+cmd);
return -1;
}
pid = fork();
if (pid == 0) { // the child
// Open file build.log, dup to stdout and stderr
if (stdinfd != 0) dup2(stdinfd,0);
int logfile = open(buildlogfilename.c_str(),O_WRONLY | O_APPEND);
dup2(logfile,1);
dup2(logfile,2);
// Close all other file descriptors
for (int fd = 3;fd < 64;fd++) close(fd);
envp = prepareenvironment();
if (execve(path.c_str(), (char *const *) &(argv[0]),envp) == -1) {
freepreparedenvironment(envp);
cerr << "Errno: " << errno << endl;
if (!quiet) out(ERROR,"Cannot execve.");
exit(17);
}
}
return pid;
}
Status rmrf(string dirname)
{
DIR *dp;
struct dirent *ep;
string abs_filename;
struct stat stFileInfo;
dp = opendir (dirname.c_str());
if (dp != NULL) {
while ((ep = readdir(dp)) != NULL) {
if(strcmp(ep->d_name, ".") && strcmp(ep->d_name, "..")) {
abs_filename = dirname + "/" + ep->d_name;
if (lstat(abs_filename.c_str(), &stFileInfo) == 0) {
if(S_ISDIR(stFileInfo.st_mode))
rmrf(abs_filename);
else
unlink(abs_filename.c_str());
}
}
}
(void) closedir (dp);
}
if (rmdir(dirname.c_str()) == 0) return OK;
else return ERROR;
}
Status cp(string from, string to)
{
fstream fin,fout;
char buf[1024];
size_t len;
fstream logfile(buildlogfilename.c_str(),fstream::out | fstream::app);
logfile << "COMMAND:cp " << from << " " << to << endl;
if (verbose >= 4) cout << "COMMAND:cd " << from << " " << to << endl;
fin.exceptions(ifstream::badbit);
fout.exceptions(ofstream::badbit);
try {
fin.open(from.c_str(),fstream::in);
fout.open(to.c_str(),fstream::out | fstream::trunc);
while (!fin.eof()) {
fin.read(buf,1024);
len = fin.gcount();
if (len > 0) fout.write(buf,len);
}
fout.close();
fin.close();
}