File: Build.pm

package info (click to toggle)
sbuild 0.78.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,900 kB
  • sloc: perl: 15,816; sh: 1,449; sql: 797; lisp: 304; makefile: 301
file content (3453 lines) | stat: -rw-r--r-- 115,529 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
#
# Build.pm: build library for sbuild
# Copyright © 2005      Ryan Murray <rmurray@debian.org>
# Copyright © 2005-2010 Roger Leigh <rleigh@debian.org>
# Copyright © 2008      Simon McVittie <smcv@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#######################################################################

package Sbuild::Build;

use strict;
use warnings;

use English;
use POSIX;
use Errno qw(:POSIX);
use Fcntl;
use File::Temp qw(mkdtemp);
use File::Basename qw(basename dirname);
use File::Path qw(make_path);
use FileHandle;
use File::Copy qw(); # copy is already exported from Sbuild, so don't export
		     # anything.
use Dpkg::Arch;
use Dpkg::Control;
use Dpkg::Index;
use Dpkg::Version;
use Dpkg::Deps qw(deps_concat deps_parse);
use Dpkg::Changelog::Debian;
use Scalar::Util 'refaddr';

use MIME::Lite;
use Term::ANSIColor;

use Sbuild qw($devnull binNMU_version copy isin debug send_mail
              dsc_files dsc_pkgver strftime_c);
use Sbuild::Base;
use Sbuild::ChrootInfoSchroot;
use Sbuild::ChrootInfoUnshare;
use Sbuild::ChrootInfoSudo;
use Sbuild::ChrootInfoAutopkgtest;
use Sbuild::ChrootRoot;
use Sbuild::Sysconfig qw($version $release_date);
use Sbuild::Sysconfig;
use Sbuild::Resolver qw(get_resolver);
use Sbuild::Exception;

BEGIN {
    use Exporter ();
    our (@ISA, @EXPORT);

    @ISA = qw(Exporter Sbuild::Base);

    @EXPORT = qw();
}

our $saved_stdout = undef;
our $saved_stderr = undef;

sub new {
    my $class = shift;
    my $dsc = shift;
    my $conf = shift;

    my $self = $class->SUPER::new($conf);
    bless($self, $class);

    $self->set('ABORT', undef);
    $self->set('Job', $dsc);
    $self->set('Build Dir', '');
    $self->set('Max Lock Trys', 120);
    $self->set('Lock Interval', 5);
    $self->set('Pkg Status', 'pending');
    $self->set('Pkg Status Trigger', undef);
    $self->set('Pkg Start Time', 0);
    $self->set('Pkg End Time', 0);
    $self->set('Pkg Fail Stage', 'init');
    $self->set('Build Start Time', 0);
    $self->set('Build End Time', 0);
    $self->set('Install Start Time', 0);
    $self->set('Install End Time', 0);
    $self->set('This Time', 0);
    $self->set('This Space', 0);
    $self->set('Sub Task', 'initialisation');
    $self->set('Host', Sbuild::ChrootRoot->new($self->get('Config')));
    # Host execution defaults
    my $host_defaults = $self->get('Host')->get('Defaults');
    $host_defaults->{'USER'} = $self->get_conf('USERNAME');
    $host_defaults->{'DIR'} = $self->get_conf('HOME');
    $host_defaults->{'STREAMIN'} = $devnull;
    $host_defaults->{'ENV'}->{'LC_ALL'} = 'C.UTF-8';
    $host_defaults->{'ENV'}->{'SHELL'} = '/bin/sh';
    $host_defaults->{'ENV_FILTER'} = $self->get_conf('ENVIRONMENT_FILTER');
    # Note, this should never fail.  But, we should handle failure anyway.
    $self->get('Host')->begin_session();

    $self->set('Session', undef);
    $self->set('Dependency Resolver', undef);
    $self->set('Log File', undef);
    $self->set('Log Stream', undef);
    $self->set('Summary Stats', {});
    $self->set('dpkg-buildpackage pid', undef);

    # DSC, package and version information:
    $self->set_dsc($dsc);

    # If the job name contains an underscore then it is either the filename of
    # a dsc or a pkgname_version string. In both cases we can already extract
    # the version number. Otherwise it is a bare source package name and the
    # version will initially be unknown.
    if ($dsc =~ m/_/) {
	$self->set_version($dsc);
    } else {
	$self->set('Package', $dsc);
    }

    return $self;
}

sub request_abort {
    my $self = shift;
    my $reason = shift;

    $self->log_error("ABORT: $reason (requesting cleanup and shutdown)\n");
    $self->set('ABORT', $reason);

    # Send signal to dpkg-buildpackage immediately if it's running.
    if (defined $self->get('dpkg-buildpackage pid')) {
	# Handling ABORT in the loop reading from the stdout/stderr output of
	# dpkg-buildpackage is suboptimal because then the ABORT signal would
	# only be handled once the build process writes to stdout or stderr
	# which might not be immediately.
	my $pid = $self->get('dpkg-buildpackage pid');
	# Sending the pid negated to send to the whole process group.
	kill "TERM", -$pid;
    }
}

sub check_abort {
    my $self = shift;

    if ($self->get('ABORT')) {
	Sbuild::Exception::Build->throw(error => "Aborting build: " .
					$self->get('ABORT'),
					failstage => "abort");
    }
}

sub set_dsc {
    my $self = shift;
    my $dsc = shift;

    debug("Setting DSC: $dsc\n");

    $self->set('DSC', $dsc);
    $self->set('Source Dir', dirname($dsc));
    $self->set('DSC Base', basename($dsc));

    debug("DSC = " . $self->get('DSC') . "\n");
    debug("Source Dir = " . $self->get('Source Dir') . "\n");
    debug("DSC Base = " . $self->get('DSC Base') . "\n");
}

sub set_version {
    my $self = shift;
    my $pkgv = shift;

    debug("Setting package version: $pkgv\n");

    my ($pkg, $version);
    if (-f $pkgv && -r $pkgv) {
	($pkg, $version) = dsc_pkgver($pkgv);
    } else {
	($pkg, $version) = split /_/, $pkgv;
    }
    my $pver = Dpkg::Version->new($version, check => 1);
    return if (!defined($pkg) || !defined($version) || !defined($pver));
    my ($o_version);
    $o_version = $pver->version();

    # Original version (no binNMU or other addition)
    my $oversion = $version;
    # Original version with stripped epoch
    my $osversion = $o_version;
    $osversion .= '-' . $pver->revision() unless $pver->{'no_revision'};

    # Add binNMU to version if needed.
    if ($self->get_conf('BIN_NMU') || $self->get_conf('APPEND_TO_VERSION')
	|| defined $self->get_conf('BIN_NMU_CHANGELOG')) {
	if (defined $self->get_conf('BIN_NMU_CHANGELOG')) {
	    # extract the binary version from the custom changelog entry
	    open(CLOGFH, '<', \$self->get_conf('BIN_NMU_CHANGELOG'));
	    my $changes = Dpkg::Changelog::Debian->new();
	    $changes->parse(*CLOGFH, "descr");
	    my @data = $changes->get_range({count => 1});
	    $version = $data[0]->get_version();
	    close(CLOGFH);
	} else {
	    # compute the binary version from the original version and the
	    # requested binNMU and append-to-version parameters
	    $version = binNMU_version($version,
		$self->get_conf('BIN_NMU_VERSION'),
		$self->get_conf('APPEND_TO_VERSION'));
	}
    }

    my $bver = Dpkg::Version->new($version, check => 1);
    return if (!defined($bver));
    my ($b_epoch, $b_version, $b_revision);
    $b_epoch = $bver->epoch();
    $b_epoch = "" if $bver->{'no_epoch'};
    $b_version = $bver->version();
    $b_revision = $bver->revision();
    $b_revision = "" if $bver->{'no_revision'};

    # Version with binNMU or other additions and stripped epoch
    my $sversion = $b_version;
    $sversion .= '-' . $b_revision if $b_revision ne '';

    $self->set('Package', $pkg);
    $self->set('Version', $version);
    $self->set('Package_Version', "${pkg}_$version");
    $self->set('Package_OVersion', "${pkg}_$oversion");
    $self->set('Package_OSVersion', "${pkg}_$osversion");
    $self->set('Package_SVersion', "${pkg}_$sversion");
    $self->set('OVersion', $oversion);
    $self->set('OSVersion', $osversion);
    $self->set('SVersion', $sversion);
    $self->set('VersionEpoch', $b_epoch);
    $self->set('VersionUpstream', $b_version);
    $self->set('VersionDebian', $b_revision);
    $self->set('DSC File', "${pkg}_${osversion}.dsc");
    $self->set('DSC Dir', "${pkg}-${b_version}");

    debug("Package = " . $self->get('Package') . "\n");
    debug("Version = " . $self->get('Version') . "\n");
    debug("Package_Version = " . $self->get('Package_Version') . "\n");
    debug("Package_OVersion = " . $self->get('Package_OVersion') . "\n");
    debug("Package_OSVersion = " . $self->get('Package_OSVersion') . "\n");
    debug("Package_SVersion = " . $self->get('Package_SVersion') . "\n");
    debug("OVersion = " . $self->get('OVersion') . "\n");
    debug("OSVersion = " . $self->get('OSVersion') . "\n");
    debug("SVersion = " . $self->get('SVersion') . "\n");
    debug("VersionEpoch = " . $self->get('VersionEpoch') . "\n");
    debug("VersionUpstream = " . $self->get('VersionUpstream') . "\n");
    debug("VersionDebian = " . $self->get('VersionDebian') . "\n");
    debug("DSC File = " . $self->get('DSC File') . "\n");
    debug("DSC Dir = " . $self->get('DSC Dir') . "\n");
}

sub set_status {
    my $self = shift;
    my $status = shift;

    $self->set('Pkg Status', $status);
    if (defined($self->get('Pkg Status Trigger'))) {
	$self->get('Pkg Status Trigger')->($self, $status);
    }
}

sub get_status {
    my $self = shift;

    return $self->get('Pkg Status');
}

# This function is the main entry point into the package build.  It
# provides a top-level exception handler and does the initial setup
# including initiating logging and creating host chroot.  The nested
# run_ functions it calls are separate in order to permit running
# cleanup tasks in a strict order.
sub run {
    my $self = shift;

    eval {
	$self->check_abort();

	$self->set_status('building');

	$self->set('Pkg Start Time', time);
	$self->set('Pkg End Time', $self->get('Pkg Start Time'));

	# Acquire the architectures we're building for and on.
	$self->set('Host Arch', $self->get_conf('HOST_ARCH'));
	$self->set('Build Arch', $self->get_conf('BUILD_ARCH'));
	$self->set('Build Profiles', $self->get_conf('BUILD_PROFILES'));

	# Acquire the build type in the nomenclature used by the --build
	# argument of dpkg-buildpackage
	my $buildtype;
	if ($self->get_conf('BUILD_SOURCE')) {
	    if ($self->get_conf('BUILD_ARCH_ANY')) {
		if ($self->get_conf('BUILD_ARCH_ALL')) {
		    $buildtype = "full";
		} else {
		    $buildtype = "source,any";
		}
	    } else {
		if ($self->get_conf('BUILD_ARCH_ALL')) {
		    $buildtype = "source,all";
		} else {
		    $buildtype = "source";
		}
	    }
	} else {
	    if ($self->get_conf('BUILD_ARCH_ANY')) {
		if ($self->get_conf('BUILD_ARCH_ALL')) {
		    $buildtype = "binary";
		} else {
		    $buildtype = "any";
		}
	    } else {
		if ($self->get_conf('BUILD_ARCH_ALL')) {
		    $buildtype = "all";
		} else {
		    Sbuild::Exception::Build->throw(error => "Neither architecture specific nor architecture independent or source package specified to be built.",
			failstage => "init");
		}
	    }
	}
	$self->set('Build Type', $buildtype);

	my $dist = $self->get_conf('DISTRIBUTION');
	if (!defined($dist) || !$dist) {
	    Sbuild::Exception::Build->throw(error => "No distribution defined",
					    failstage => "init");
	}

	# TODO: Get package name from build object
	if (!$self->open_build_log()) {
	    Sbuild::Exception::Build->throw(error => "Failed to open build log",
					    failstage => "init");
	}

	# Set a chroot to run commands in host
	my $host = $self->get('Host');

	# Host execution defaults (set streams)
	my $host_defaults = $host->get('Defaults');
	$host_defaults->{'STREAMIN'} = $devnull;
	$host_defaults->{'STREAMOUT'} = $self->get('Log Stream');
	$host_defaults->{'STREAMERR'} = $self->get('Log Stream');

	$self->check_abort();
	$self->run_chroot();
    };

    debug("Error run(): $@") if $@;

    my $e;
    if ($e = Exception::Class->caught('Sbuild::Exception::Build')) {
	if ($e->status) {
	    $self->set_status($e->status);
	} else {
	    $self->set_status("failed");
	}
	$self->set('Pkg Fail Stage', $e->failstage);
	$e->rethrow();
    }
}

# Pack up source if needed and then run the main chroot session.
# Close log during return/failure.
sub run_chroot {
    my $self = shift;

    eval {
	$self->check_abort();
	$self->run_chroot_session();
    };

    debug("Error run_chroot(): $@") if $@;

    # Log exception info and set status and fail stage prior to
    # closing build log.
    my $e;
    if ($e = Exception::Class->caught('Sbuild::Exception::Build')) {
	$self->log_error("$e\n");
	$self->log_info($e->info."\n")
	    if ($e->info);
	if ($e->status) {
	    $self->set_status($e->status);
	} else {
	    $self->set_status("failed");
	}
	$self->set('Pkg Fail Stage', $e->failstage);
    }

    $self->close_build_log();

    if ($e) {
	$e->rethrow();
    }
}

# Create main chroot session and package resolver.  Creates a lock in
# the chroot to prevent concurrent chroot usage (only important for
# non-snapshot chroots).  Ends chroot session on return/failure.
sub run_chroot_session {
    my $self=shift;

    eval {
	$self->check_abort();
	my $chroot_info;
	if ($self->get_conf('CHROOT_MODE') eq 'schroot') {
	    $chroot_info = Sbuild::ChrootInfoSchroot->new($self->get('Config'));
	} elsif ($self->get_conf('CHROOT_MODE') eq 'autopkgtest') {
	    $chroot_info = Sbuild::ChrootInfoAutopkgtest->new($self->get('Config'));
	} elsif ($self->get_conf('CHROOT_MODE') eq 'unshare') {
	    $chroot_info = Sbuild::ChrootInfoUnshare->new($self->get('Config'));
	} else {
	    $chroot_info = Sbuild::ChrootInfoSudo->new($self->get('Config'));
	}

	my $host = $self->get('Host');

	my $session = $chroot_info->create('chroot',
					   $self->get_conf('DISTRIBUTION'),
					   $self->get_conf('CHROOT'),
					   $self->get_conf('BUILD_ARCH'));
        if (!defined $session) {
	    Sbuild::Exception::Build->throw(error => "Error creating chroot",
					    failstage => "create-session");
        }

	$self->check_abort();
	if (!$session->begin_session()) {
	    Sbuild::Exception::Build->throw(error => "Error creating chroot session: skipping " .
					    $self->get('Package'),
					    failstage => "create-session");
	}

	$self->set('Session', $session);

	$self->check_abort();
	my $chroot_arch =  $self->chroot_arch();
	if ($self->get_conf('BUILD_ARCH') ne $chroot_arch) {
	    Sbuild::Exception::Build->throw(
		error => "Requested build architecture (" .
		$self->get_conf('BUILD_ARCH') .
		") and chroot architecture (" . $chroot_arch .
		") do not match.  Skipping build.",
		info => "Please specify the correct architecture with --build, or use a chroot of the correct architecture",
		failstage => "create-session");
	}

	if (defined($self->get_conf('BUILD_PATH')) && $self->get_conf('BUILD_PATH')) {
	    my $build_path = $self->get_conf('BUILD_PATH');
	    $self->set('Build Dir', $build_path);
	    if (!($session->test_directory($build_path))) {
		if (!$session->mkdir($build_path, { PARENTS => 1})) {
		    Sbuild::Exception::Build->throw(
			error => "Buildpath: " . $build_path . " cannot be created",
			failstage => "create-session");
		}
	    } else {
		my $isempty = <<END;
if (opendir my \$dfh, "$build_path") {
    while (defined(my \$file=readdir \$dfh)) {
	next if \$file eq "." or \$file eq "..";
	closedir \$dfh;
	exit 1
    }
    closedir \$dfh;
    exit 0
}
exit 2
END
		$session->run_command({
			COMMAND => ['perl', '-e', $isempty],
			USER => 'root',
			DIR => '/'
		    });
		if ($? == 1) {
		    Sbuild::Exception::Build->throw(
			error => "Buildpath: " . $build_path . " is not empty",
			failstage => "create-session");
		}
		elsif ($? == 2) {
		    Sbuild::Exception::Build->throw(
			error => "Buildpath: " . $build_path . " cannot be read. Insufficient permissions?",
			failstage => "create-session");
		}
	    }
	} else {
		# we run mktemp within the chroot instead of using File::Temp::tempdir because the user
		# running sbuild might not have permissions creating a directory in /build. This happens
		# when the chroot was extracted in a different user namespace than the outer user
		$self->check_abort();
		my $tmpdir = $session->mktemp({
			TEMPLATE => "/build/" . $self->get('Package') . '-XXXXXX',
			DIRECTORY => 1});
		if (!$tmpdir) {
			$self->log_error("unable to mktemp\n");
			Sbuild::Exception::Build->throw(error => "unable to mktemp",
				failstage => "create-build-dir");
		}
		$self->check_abort();
		$self->set('Build Dir', $tmpdir);
	}


	# Run pre build external commands
	$self->check_abort();
	if(!$self->run_external_commands("pre-build-commands")) {
	    Sbuild::Exception::Build->throw(error => "Failed to execute pre-build-commands",
		failstage => "run-pre-build-commands");
	}

	# Log colouring
	$self->build_log_colour('red', '^E: ');
	$self->build_log_colour('yellow', '^W: ');
	$self->build_log_colour('green', '^I: ');
	$self->build_log_colour('red', '^Status:');
	$self->build_log_colour('green', '^Status: successful$');
	$self->build_log_colour('yellow', '^Keeping session: ');
	$self->build_log_colour('red', '^Lintian:');
	$self->build_log_colour('yellow', '^Lintian: warn$');
	$self->build_log_colour('green', '^Lintian: pass$');
	$self->build_log_colour('green', '^Lintian: info$');
	$self->build_log_colour('red', '^Piuparts:');
	$self->build_log_colour('green', '^Piuparts: pass$');
	$self->build_log_colour('red', '^Autopkgtest:');
	$self->build_log_colour('yellow', '^Autopkgtest: no tests$');
	$self->build_log_colour('green', '^Autopkgtest: pass$');

	# Log filtering
	my $filter;
	$filter = $session->get('Location');
	$filter =~ s;^/;;;
	$self->build_log_filter($filter , 'CHROOT');

	# Need tempdir to be writable and readable by sbuild group.
	$self->check_abort();
	if (!$session->chown($self->get('Build Dir'), $self->get_conf('BUILD_USER'), 'sbuild')) {
	    Sbuild::Exception::Build->throw(error => "Failed to set sbuild group ownership on chroot build dir",
					    failstage => "create-build-dir");
	}
	$self->check_abort();
	if (!$session->chmod($self->get('Build Dir'), "ug=rwx,o=,a-s")) {
	    Sbuild::Exception::Build->throw(error => "Failed to set sbuild group ownership on chroot build dir",
					    failstage => "create-build-dir");
	}

	$self->check_abort();
	# Needed so chroot commands log to build log
	$session->set('Log Stream', $self->get('Log Stream'));
	$host->set('Log Stream', $self->get('Log Stream'));

	# Chroot execution defaults
	my $chroot_defaults = $session->get('Defaults');
	$chroot_defaults->{'DIR'} = $self->get('Build Dir');
	$chroot_defaults->{'STREAMIN'} = $devnull;
	$chroot_defaults->{'STREAMOUT'} = $self->get('Log Stream');
	$chroot_defaults->{'STREAMERR'} = $self->get('Log Stream');
	$chroot_defaults->{'ENV'}->{'LC_ALL'} = 'C.UTF-8';
	$chroot_defaults->{'ENV'}->{'SHELL'} = '/bin/sh';
	$chroot_defaults->{'ENV'}->{'HOME'} = '/sbuild-nonexistent';
	$chroot_defaults->{'ENV_FILTER'} = $self->get_conf('ENVIRONMENT_FILTER');

	my $resolver = get_resolver($self->get('Config'), $session, $host);
	$resolver->set('Log Stream', $self->get('Log Stream'));
	$resolver->set('Arch', $self->get_conf('ARCH'));
	$resolver->set('Host Arch', $self->get_conf('HOST_ARCH'));
	$resolver->set('Build Arch', $self->get_conf('BUILD_ARCH'));
	$resolver->set('Build Profiles', $self->get_conf('BUILD_PROFILES'));
	$resolver->set('Build Dir', $self->get('Build Dir'));
	$self->set('Dependency Resolver', $resolver);

	# Lock chroot so it won't be tampered with during the build.
	$self->check_abort();
	my $jobname;
	# the version might not yet be known if the user only passed a package
	# name without a version to sbuild
	if ($self->get('Package_SVersion')) {
	    $jobname = $self->get('Package_SVersion');
	} else {
	    $jobname = $self->get('Package');
	}
	if (!$session->lock_chroot($jobname, $$, $self->get_conf('USERNAME'))) {
	    Sbuild::Exception::Build->throw(error => "Error locking chroot session: skipping " .
					    $self->get('Package'),
					    failstage => "lock-session");
	}

	$self->check_abort();
	$self->run_chroot_session_locked();
    };

    debug("Error run_chroot_session(): $@") if $@;

    if ($self->get('Pkg Status') ne "successful") {
	if(!$self->run_external_commands("post-build-failed-commands")) {
	    Sbuild::Exception::Build->throw(error => "Failed to execute post-build-commands",
		failstage => "run-post-build-failed-commands");
	}
    }

    # End chroot session
    my $session = $self->get('Session');
    if (defined $session) {
	    my $end_session =
		    ($self->get_conf('PURGE_SESSION') eq 'always' ||
		     ($self->get_conf('PURGE_SESSION') eq 'successful' &&
		      $self->get_status() eq 'successful')) ? 1 : 0;
	    if ($end_session) {
		    $session->end_session();
	    } else {
		    $self->log("Keeping session: " . $session->get('Session ID') . "\n");
	    }
	    $session = undef;
    }
    $self->set('Session', $session);

    my $e;
    if ($e = Exception::Class->caught('Sbuild::Exception::Build')) {
	$e->rethrow();
    }
}

# Run tasks in a *locked* chroot.  Update and upgrade packages.
# Unlocks chroot on return/failure.
sub run_chroot_session_locked {
    my $self = shift;

    eval {
	my $session = $self->get('Session');
	my $resolver = $self->get('Dependency Resolver');

	# Run specified chroot setup commands
	$self->check_abort();
	if(!$self->run_external_commands("chroot-setup-commands")) {
	    Sbuild::Exception::Build->throw(error => "Failed to execute chroot-setup-commands",
		failstage => "run-chroot-setup-commands");
	}

	$self->check_abort();


	$self->check_abort();
	if (!$resolver->setup()) {
		Sbuild::Exception::Build->throw(error => "resolver setup failed",
						failstage => "resolver setup");
	}

	my $filter;
	$filter = $resolver->get('Dummy package path');
	$filter =~ s;^/;;;
	$self->build_log_filter($filter , 'RESOLVERDIR');

	$self->check_abort();
	$self->run_chroot_update();

	$self->check_abort();
	$self->run_fetch_install_packages();
    };

    debug("Error run_chroot_session_locked(): $@") if $@;

    my $session = $self->get('Session');
    my $resolver = $self->get('Dependency Resolver');

    $resolver->cleanup();
    # Unlock chroot now it's cleaned up and ready for other users.
    $session->unlock_chroot();

    my $e;
    if ($e = Exception::Class->caught('Sbuild::Exception::Build')) {
	$e->rethrow();
    }
}

sub run_chroot_update {
    my $self = shift;
    my $resolver = $self->get('Dependency Resolver');

    eval {
	if ($self->get_conf('APT_CLEAN') || $self->get_conf('APT_UPDATE') ||
	    $self->get_conf('APT_DISTUPGRADE') || $self->get_conf('APT_UPGRADE')) {
	    $self->log_subsection('Update chroot');
	}

	# Clean APT cache.
	$self->check_abort();
	if ($self->get_conf('APT_CLEAN')) {
	    if ($resolver->clean()) {
		# Since apt-clean was requested specifically, fail on
		# error when not in buildd mode.
		$self->log_error("apt-get clean failed\n");
		if ($self->get_conf('SBUILD_MODE') ne 'buildd') {
		    Sbuild::Exception::Build->throw(error => "apt-get clean failed",
						    failstage => "apt-get-clean");
		}
	    }
	}

	# Update APT cache.
	$self->check_abort();
	if ($self->get_conf('APT_UPDATE')) {
	    if ($resolver->update()) {
		# Since apt-update was requested specifically, fail on
		# error when not in buildd mode.
		if ($self->get_conf('SBUILD_MODE') ne 'buildd') {
		    Sbuild::Exception::Build->throw(error => "apt-get update failed",
						    failstage => "apt-get-update");
		}
	    }
	} else {
	    # If it was requested not to do an apt update, the build and host
	    # architecture must already be part of the chroot. If they are not
	    # and thus added during the sbuild run, issue a warning because
	    # then the package build dependencies will likely fail to be
	    # installable.
	    #
	    # The logic which checks which architectures are needed is in
	    # ResolverBase.pm, so we just check whether any architectures
	    # where added with 'dpkg --add-architecture' because if any were
	    # added an update is most likely needed.
	    if (keys %{$resolver->get('Added Foreign Arches')}) {
		$self->log_warning("Additional architectures were added but apt update was disabled. Build dependencies might not be satisfiable.\n");
	    }
	}

	# Upgrade using APT.
	$self->check_abort();
	if ($self->get_conf('APT_DISTUPGRADE')) {
	    if ($resolver->distupgrade()) {
		# Since apt-distupgrade was requested specifically, fail on
		# error when not in buildd mode.
		if ($self->get_conf('SBUILD_MODE') ne 'buildd') {
		    Sbuild::Exception::Build->throw(error => "apt-get dist-upgrade failed",
						    failstage => "apt-get-dist-upgrade");
		}
	    }
	} elsif ($self->get_conf('APT_UPGRADE')) {
	    if ($resolver->upgrade()) {
		# Since apt-upgrade was requested specifically, fail on
		# error when not in buildd mode.
		if ($self->get_conf('SBUILD_MODE') ne 'buildd') {
		    Sbuild::Exception::Build->throw(error => "apt-get upgrade failed",
						    failstage => "apt-get-upgrade");
		}
	    }
	}
    };

    debug("Error run_chroot_update(): $@") if $@;

    my $e = Exception::Class->caught('Sbuild::Exception::Build');
    if ($e) {
	$self->run_external_commands("chroot-update-failed-commands");
	$e->rethrow();
    }
}

# Fetch sources, run setup, fetch and install core and package build
# deps, then run build.  Cleans up build directory and uninstalls
# build depends on return/failure.
sub run_fetch_install_packages {
    my $self = shift;

    $self->check_abort();
    eval {
	my $session = $self->get('Session');
	my $resolver = $self->get('Dependency Resolver');

	$self->check_abort();
	if (!$self->fetch_source_files()) {
	    Sbuild::Exception::Build->throw(error => "Failed to fetch source files",
					    failstage => "fetch-src");
	}

	# Display message about chroot setup script option use being deprecated
	if ($self->get_conf('CHROOT_SETUP_SCRIPT')) {
	    my $msg = "setup-hook option is deprecated. It has been superseded by ";
	    $msg .= "the chroot-setup-commands feature. setup-hook script will be ";
	    $msg .= "run via chroot-setup-commands.\n";
	    $self->log_warning($msg);
	}

	$self->check_abort();
	$self->set('Install Start Time', time);
	$self->set('Install End Time', $self->get('Install Start Time'));
	my @coredeps = @{$self->get_conf('CORE_DEPENDS')};
	if ($self->get('Host Arch') ne $self->get('Build Arch')) {
	    my $crosscoredeps = $self->get_conf('CROSSBUILD_CORE_DEPENDS');
	    if (defined($crosscoredeps->{$self->get('Host Arch')})) {
	        push(@coredeps, @{$crosscoredeps->{$self->get('Host Arch')}});
	    } else {
		push(@coredeps, 'crossbuild-essential-' . $self->get('Host Arch') . ':native');
		# Also add the following to work around bug #815172
		push(@coredeps, 'libc-dev:' . $self->get('Host Arch'),
		    'libstdc++-dev:' . $self->get('Host Arch'));
            }
	}

	my @snapshot = ();
	@snapshot = ("gcc-snapshot") if ($self->get_conf('GCC_SNAPSHOT'));

	$resolver->add_dependencies('MAIN',
	    join(", ", $self->get('Build Depends') // (),
		       @{$self->get_conf('MANUAL_DEPENDS')}, @snapshot, @coredeps),
	    join(", ", $self->get('Build Depends Arch') // (),
		       @{$self->get_conf('MANUAL_DEPENDS_ARCH')}),
	    join(", ", $self->get('Build Depends Indep') // (),
		       @{$self->get_conf('MANUAL_DEPENDS_INDEP')}),
	    join(", ", $self->get('Build Conflicts') // (),
		       @{$self->get_conf('MANUAL_CONFLICTS')}),
	    join(", ", $self->get('Build Conflicts Arch') // (),
		       @{$self->get_conf('MANUAL_CONFLICTS_ARCH')}),
	    join(", ", $self->get('Build Conflicts Indep') // (),
		       @{$self->get_conf('MANUAL_CONFLICTS_INDEP')}));

	$self->log_subsection("Install package build dependencies");

	$self->check_abort();
	if (!$resolver->install_deps('main', 'MAIN')) {
	    Sbuild::Exception::Build->throw(error => "Package build dependencies not satisfied; skipping",
					    failstage => "install-deps");
	}
	$self->check_abort();
	if ($self->get_conf('PURGE_EXTRA_PACKAGES')) {
	    if (!$resolver->purge_extra_packages($self->get('Package'))) {
		Sbuild::Exception::Build->throw(error => "Chroot could not be cleaned of extra packages",
		    failstage => "install-deps");
	    }
	}
	$self->set('Install End Time', time);

	# the architecture check has to be done *after* build-essential is
	# installed because as part of the architecture check a perl script is
	# run inside the chroot which requires the Dpkg::Arch module which is
	# in libdpkg-perl which might not exist in the chroot but will get
	# installed by the build-essential package
	if(!$self->check_architectures()) {
	    Sbuild::Exception::Build->throw(error => "Architecture check failed",
					    failstage => "check-architecture");
	}

	$self->check_abort();
	$resolver->dump_build_environment();

	$self->check_abort();
	if ($self->build()) {
	    $self->set_status('successful');
	} else {
	    $self->set('Pkg Fail Stage', "build");
	    $self->set_status('failed');
	}

	# We run it here and not inside build() because otherwise, we cannot
	# set the overall status to failed due to lintian errors
	if ($self->get('Pkg Status') eq "successful") {
	    # Run lintian.
	    $self->check_abort();
	    my $ret = $self->run_lintian();
	    if (!$ret && $self->get_conf('LINTIAN_REQUIRE_SUCCESS')) {
		$self->set('Pkg Fail Stage', "post-build");
		$self->set_status("failed");
	    }
	}

	# Run specified chroot cleanup commands
	$self->check_abort();
	if (!$self->run_external_commands("chroot-cleanup-commands")) {
	    Sbuild::Exception::Build->throw(error => "Failed to execute chroot-cleanup-commands",
		failstage => "run-chroot-cleanup-commands");
	}

	# piuparts and autopkgtest must be run while the chroot is still open
	# because they might need files that are not available on the host,
	# for example the .dsc which might have been downloaded
	if ($self->get('Pkg Status') eq "successful") {
	    $self->log_subsection("Post Build");

	    # Run piuparts.
	    $self->check_abort();
	    my $ret = $self->run_piuparts();
	    if (!$ret && $self->get_conf('PIUPARTS_REQUIRE_SUCCESS')) {
		$self->set('Pkg Fail Stage', "post-build");
		$self->set_status("failed");
	    }

	    # Run autopkgtest.
	    $self->check_abort();
	    $ret = $self->run_autopkgtest();
	    if (!$ret && $self->get_conf('AUTOPKGTEST_REQUIRE_SUCCESS')) {
		$self->set('Pkg Fail Stage', "post-build");
		$self->set_status("failed");
	    }

	    # Run post build external commands
	    $self->check_abort();
	    if(!$self->run_external_commands("post-build-commands")) {
		Sbuild::Exception::Build->throw(error => "Failed to execute post-build-commands",
		    failstage => "run-post-build-commands");
	    }

	}
    };

    # If 'This Time' is still zero, then build() raised an exception and thus
    # the end time was never set. Thus, setting it here.
    # If we would set 'This Time' here unconditionally, then it would also
    # possibly include the times to run piuparts and autopkgtest.
    if ($self->get('This Time') == 0) {
	$self->set('This Time', $self->get('Pkg End Time') - $self->get('Pkg Start Time'));
	$self->set('This Time', 0) if $self->get('This Time') < 0;
    }
    # Same for 'This Space' which we must set here before everything gets
    # cleaned up.
    if ($self->get('This Space') == 0) {
	# Since the build apparently failed, we pass an empty list of the
	# build artifacts
	$self->set('This Space', $self->check_space());
    }

    debug("Error run_fetch_install_packages(): $@") if $@;

    # I catch the exception here and trigger the hook, if needed. Normally I'd
    # do this at the end of the function, but I want the hook to fire before we
    # clean up the environment. I re-throw the exception at the end, as usual
    my $e = Exception::Class->caught('Sbuild::Exception::Build');
    if ($e) {
	if ($e->status) {
	    $self->set_status($e->status);
	} else {
	    $self->set_status("failed");
	}
	$self->set('Pkg Fail Stage', $e->failstage);
    }
    if (!$self->get('ABORT') && defined $self->get('Pkg Fail Stage')) {
	if ($self->get('Pkg Fail Stage') eq 'build' ) {
	    if(!$self->run_external_commands("build-failed-commands")) {
		Sbuild::Exception::Build->throw(error => "Failed to execute build-failed-commands",
		    failstage => "run-build-failed-commands");
	    }
	} elsif($self->get('Pkg Fail Stage') eq 'install-deps' ) {
            my $could_not_explain = undef;

	    if (defined $self->get_conf('BD_UNINSTALLABLE_EXPLAINER')
		&& $self->get_conf('BD_UNINSTALLABLE_EXPLAINER') ne '') {
		if (!$self->explain_bd_uninstallable()) {
                    $could_not_explain = 1;
		}
	    }

	    if(!$self->run_external_commands("build-deps-failed-commands")) {
		Sbuild::Exception::Build->throw(error => "Failed to execute build-deps-failed-commands",
		    failstage => "run-build-deps-failed-commands");
	    }

            if( $could_not_explain ) {
                Sbuild::Exception::Build->throw(error => "Failed to explain bd-uninstallable",
                                                failstage => "explain-bd-uninstallable");
            }
	}
    }

    $self->log_subsection("Cleanup");
    my $session = $self->get('Session');
    my $resolver = $self->get('Dependency Resolver');

    my $purge_build_directory =
	($self->get_conf('PURGE_BUILD_DIRECTORY') eq 'always' ||
	 ($self->get_conf('PURGE_BUILD_DIRECTORY') eq 'successful' &&
	  $self->get_status() eq 'successful')) ? 1 : 0;
    my $purge_build_deps =
	($self->get_conf('PURGE_BUILD_DEPS') eq 'always' ||
	 ($self->get_conf('PURGE_BUILD_DEPS') eq 'successful' &&
	  $self->get_status() eq 'successful')) ? 1 : 0;
    my $is_cloned_session = (defined ($session->get('Session Purged')) &&
			     $session->get('Session Purged') == 1) ? 1 : 0;

    if ($purge_build_directory) {
	# Purge package build directory
	$self->log("Purging " . $self->get('Build Dir') . "\n");
	if (!$self->get('Session')->unlink($self->get('Build Dir'), { RECURSIVE => 1 })) {
		$self->log_error("unable to remove build directory\n");
	}
    }

    # Purge non-cloned session
    if ($is_cloned_session) {
	$self->log("Not cleaning session: cloned chroot in use\n");
    } else {
	if ($purge_build_deps) {
	    # Removing dependencies
	    $resolver->uninstall_deps();
	} else {
	    $self->log("Not removing build depends: as requested\n");
	}
    }


    # re-throw the previously-caught exception
    if ($e) {
	$e->rethrow();
    }
}

sub copy_to_chroot {
    my $self = shift;
    my $source = shift;
    my $chrootdest = shift;

    my $session = $self->get('Session');

    $self->check_abort();
    if(!$session->copy_to_chroot($source, $chrootdest)) {
	$self->log_error("Failed to copy $source to $chrootdest\n");
	return 0;
    }

    if (!$session->chown($chrootdest, $self->get_conf('BUILD_USER'), 'sbuild')) {
	$self->log_error("Failed to set sbuild group ownership on $chrootdest\n");
	return 0;
    }
    if (!$session->chmod($chrootdest, "ug=rw,o=r,a-s")) {
	$self->log_error("Failed to set 0644 permissions on $chrootdest\n");
	return 0;
    }

    return 1;
}
sub fetch_source_files {
    my $self = shift;

    my $build_dir = $self->get('Build Dir');
    my $host_arch = $self->get('Host Arch');
    my $resolver = $self->get('Dependency Resolver');

    my ($dscarchs, $dscpkg, $dscver, $dsc);

    my $build_depends = "";
    my $build_depends_arch = "";
    my $build_depends_indep = "";
    my $build_conflicts = "";
    my $build_conflicts_arch = "";
    my $build_conflicts_indep = "";
    local( *F );

    $self->log_subsection("Fetch source files");

    $self->check_abort();
    if ($self->get('DSC Base') =~ m/\.dsc$/) {
	my $dir = $self->get('Source Dir');

	# Work with a .dsc file.
	my $file = $self->get('DSC');
	$dsc = $self->get('DSC File');
	if (! -f $file || ! -r $file) {
	    $self->log_error("Could not find $file\n");
	    return 0;
	}
	my @cwd_files = dsc_files($file);

	# Copy the local source files into the build directory.
	$self->log_subsubsection("Local sources");
	$self->log("$file exists in $dir; copying to chroot\n");
	if (! $self->copy_to_chroot("$file", "$build_dir/$dsc")) {
	    return 0;
	}
	foreach (@cwd_files) {
	    if (! $self->copy_to_chroot("$dir/$_", "$build_dir/$_")) {
		return 0;
	    }
	}
    } else {
	my $pkg = $self->get('DSC');
	my $ver;

	if ($pkg =~ m/_/) {
	    ($pkg, $ver) = split /_/, $pkg;
	}

	# Use apt to download the source files
	$self->log_subsubsection("Check APT");
	my %entries = ();
	$self->log("Checking available source versions...\n");

	# We would like to call apt-cache with --only-source so that the
	# result only contains source packages with the given name but this
	# feature was only introduced in apt 1.1~exp10 so it is only available
	# in Debian Stretch and later
	my $pipe = $self->get('Dependency Resolver')->pipe_apt_command(
	    { COMMAND => [$self->get_conf('APT_CACHE'),
			  '-q', 'showsrc', $pkg],
	      USER => $self->get_conf('BUILD_USER'),
	      PRIORITY => 0,
	      DIR => '/'});
	if (!$pipe) {
	    $self->log_error("Can't open pipe to ".$self->get_conf('APT_CACHE').": $!\n");
	    return 0;
	}

	my $key_func = sub {
	    return $_[0]->{Package} . '_' . $_[0]->{Version};
	};

	my $index = Dpkg::Index->new(get_key_func=>$key_func);

	if (!$index->parse($pipe, 'apt-cache showsrc')) {
	    $self->log_error("Cannot parse output of apt-cache showsrc: $!\n");
	    return 0;
	}

	close($pipe);

	if ($?) {
	    $self->log_error($self->get_conf('APT_CACHE') . " exit status $?: $!\n");
	    return 0;
	}

	my $highestversion;
	my $highestdsc;

	foreach my $key ($index->get_keys()) {
	    my $cdata = $index->get_by_key($key);
	    my $pkgname = $cdata->{"Package"};
	    if (not defined($pkgname)) {
		$self->log_warning("apt-cache output without Package field\n");
		next;
	    }
	    # Since we cannot run apt-cache with --only-source because that
	    # feature was only introduced with apt 1.1~exp10, the result can
	    # contain source packages that we didn't ask for (but which
	    # contain binary packages of the name we specified). Since we only
	    # are interested in source packages of the given name, we skip
	    # everything that is a different source package.
	    if ($pkg ne $pkgname) {
		next;
	    }
	    my $pkgversion = $cdata->{"Version"};
	    if (not defined($pkgversion)) {
		$self->log_warning("apt-cache output without Version field\n");
		next;
	    }
	    if (defined($ver) and $ver ne $pkgversion) {
		next;
	    }
	    my $checksums = Dpkg::Checksums->new();
	    $checksums->add_from_control($cdata, use_files_for_md5 => 1);
	    my @files = grep {/\.dsc$/} $checksums->get_files();
	    if (scalar @files != 1) {
		$self->log_warning("apt-cache output with more than one .dsc\n");
		next;
	    }
	    if (!defined $highestdsc) {
		$highestdsc = $files[0];
		$highestversion = $pkgversion;
	    } else {
		if (version_compare($highestversion, $pkgversion) < 0) {
		    $highestdsc = $files[0];
		    $highestversion = $pkgversion;
		}
	    }
	}

	if (!defined $highestdsc) {
	    $self->log_error($self->get_conf('APT_CACHE') .
		" returned no information about $pkg source\n");
	    $self->log_error("Are there any deb-src lines in your /etc/apt/sources.list?\n");
	    return 0;
	}

	$self->set_dsc($highestdsc);
	$dsc = $highestdsc;

	$self->log_subsubsection("Download source files with APT");

	my $pipe2 = $self->get('Dependency Resolver')->pipe_apt_command(
	    { COMMAND => [$self->get_conf('APT_GET'), '--only-source', '-q', '-d', 'source', "$pkg=$highestversion"],
	      USER => $self->get_conf('BUILD_USER'),
	      PRIORITY => 0}) || return 0;

	while(<$pipe2>) {
	    $self->log($_);
	}
	close($pipe2);
	if ($?) {
	    $self->log_error($self->get_conf('APT_GET') . " for sources failed\n");
	    return 0;
	}
    }

    my $pipe = $self->get('Session')->get_read_file_handle("$build_dir/$dsc");
    if (!$pipe) {
	$self->log_error("unable to open pipe\n");
	return 0;
    }

    my $pdsc = Dpkg::Control->new(type => CTRL_PKG_SRC);
    $pdsc->set_options(allow_pgp => 1);
    if (!$pdsc->parse($pipe, "$build_dir/$dsc")) {
	$self->log_error("Error parsing $build_dir/$dsc\n");
	return 0;
    }

    close($pipe);

    $build_depends = $pdsc->{'Build-Depends'};
    $build_depends_arch = $pdsc->{'Build-Depends-Arch'};
    $build_depends_indep = $pdsc->{'Build-Depends-Indep'};
    $build_conflicts = $pdsc->{'Build-Conflicts'};
    $build_conflicts_arch = $pdsc->{'Build-Conflicts-Arch'};
    $build_conflicts_indep = $pdsc->{'Build-Conflicts-Indep'};
    $dscarchs = $pdsc->{'Architecture'};
    $dscpkg = $pdsc->{'Source'};
    $dscver = $pdsc->{'Version'};

    $self->set_version("${dscpkg}_${dscver}");

    $build_depends =~ s/\n\s+/ /g if defined $build_depends;
    $build_depends_arch =~ s/\n\s+/ /g if defined $build_depends_arch;
    $build_depends_indep =~ s/\n\s+/ /g if defined $build_depends_indep;
    $build_conflicts =~ s/\n\s+/ /g if defined $build_conflicts;
    $build_conflicts_arch =~ s/\n\s+/ /g if defined $build_conflicts_arch;
    $build_conflicts_indep =~ s/\n\s+/ /g if defined $build_conflicts_indep;

    $self->set('Build Depends', $build_depends);
    $self->set('Build Depends Arch', $build_depends_arch);
    $self->set('Build Depends Indep', $build_depends_indep);
    $self->set('Build Conflicts', $build_conflicts);
    $self->set('Build Conflicts Arch', $build_conflicts_arch);
    $self->set('Build Conflicts Indep', $build_conflicts_indep);

    $self->set('Dsc Architectures', $dscarchs);

    # we set up the following filters this late because the user might only
    # have specified a source package name to build without a version in which
    # case we only get to know the final build directory now
    my $filter;
    $filter = $self->get('Build Dir') . '/' . $self->get('DSC Dir');
    $filter =~ s;^/;;;
    $self->build_log_filter($filter, 'PKGBUILDDIR');
    $filter = $self->get('Build Dir');
    $filter =~ s;^/;;;
    $self->build_log_filter($filter, 'BUILDDIR');

    return 1;
}

sub check_architectures {
    my $self = shift;
    my $resolver = $self->get('Dependency Resolver');
    my $dscarchs = $self->get('Dsc Architectures');
    my $build_arch = $self->get('Build Arch');
    my $host_arch = $self->get('Host Arch');
    my $session = $self->get('Session');

    $self->log_subsection("Check architectures");
    # Check for cross-arch dependencies
    # parse $build_depends* for explicit :arch and add the foreign arches, as needed
    #
    # This check only looks at the immediate build dependencies. This could
    # fail in a future where a foreign architecture direct build dependency of
    # architecture X depends on another foreign architecture package of
    # architecture Y. Architecture Y would not be added through this check as
    # sbuild will not traverse the dependency graph. Doing so would be very
    # complicated as new architectures would have to be added to a dependency
    # solver like dose3 as the graph is traversed and new architectures are
    # found.
    sub get_explicit_arches
    {
        my $visited_deps = pop;
        my @deps = @_;

        my %set;
        for my $dep (@deps)
        {
            # Break any recursion in the deps data structure (is this overkill?)
            next if !defined $dep;
            my $id = ref($dep) ? refaddr($dep) : "str:$dep";
            next if $visited_deps->{$id};
            $visited_deps->{$id} = 1;

            if ( exists( $dep->{archqual} ) )
            {
                if ( $dep->{archqual} )
                {
                    $set{$dep->{archqual}} = 1;
                }
            }
            else
            {
                for my $key (get_explicit_arches($dep->get_deps,
                                                 $visited_deps)) {
                    $set{$key} = 1;
                }
            }
        }

        return keys %set;
    }

    # we don't need to look at build conflicts here because conflicting with a
    # package of an explicit architecture does not mean that we need to enable
    # that architecture in the chroot
    my $build_depends_concat =
      deps_concat( grep {defined $_} ($self->get('Build Depends'),
                                      $self->get('Build Depends Arch'),
                                      $self->get('Build Depends Indep')));
    my $merged_depends = deps_parse( $build_depends_concat,
		reduce_arch => 1,
		host_arch => $self->get('Host Arch'),
		build_arch => $self->get('Build Arch'),
		build_dep => 1,
		reduce_profiles => 1,
		build_profiles => [ split / /, $self->get('Build Profiles') ]);
    if( !defined $merged_depends ) {
        my $msg = "Error! deps_parse() couldn't parse the Build-Depends '$build_depends_concat'";
        $self->log_error("$msg\n");
        return 0;
    }

    my @explicit_arches = get_explicit_arches($merged_depends, {});
    my @foreign_arches = grep {$_ !~ /any|all|native/} @explicit_arches;
    my $added_any_new;
    for my $foreign_arch(@foreign_arches)
    {
        $resolver->add_foreign_architecture($foreign_arch);
        $added_any_new = 1;
    }

    my @keylist=keys %{$resolver->get('Initial Foreign Arches')};
    $self->log('Initial Foreign Architectures: ' . join ' ', @keylist, "\n")
      if @keylist;
    $self->log('Foreign Architectures in build-deps: '. join ' ', @foreign_arches, "\n\n")
      if @foreign_arches;

    $self->run_chroot_update() if $added_any_new;

    # At this point, all foreign architectures should have been added to dpkg.
    # Thus, we now examine, whether the packages passed via --extra-package
    # can even be considered by dpkg inside the chroot with respect to their
    # architecture.

    # Retrieve all foreign architectures from the chroot. We need to do this
    # step because the user might've added more foreign arches to the chroot
    # beforehand.
    my @all_foreign_arches = split /\s+/, $session->read_command({
	    COMMAND => ['dpkg', '--print-foreign-architectures'],
	    USER => $self->get_conf('USERNAME'),
	});
    # we use an anonymous subroutine so that the referenced variables are
    # automatically rebound to their current values
    my $check_deb_arch = sub {
	my $pkg = shift;
	# Investigate the Architecture field of the binary package
	my $arch = $self->get('Host')->read_command({
		COMMAND => ['dpkg-deb', '--field', Cwd::abs_path($pkg), 'Architecture'],
		USER => $self->get_conf('USERNAME')
	    });
	if (!defined $arch) {
	    $self->log_warning("Failed to run dpkg-deb on $pkg. Skipping...\n");
	    next;
	}
	chomp $arch;
	# Only packages that are Architecture:all, the native architecture or
	# one of the configured foreign architectures are allowed.
	if ($arch ne 'all' and $arch ne $build_arch
		and !isin($arch, @all_foreign_arches)) {
	    $self->log_warning("Extra package $pkg of architecture $arch cannot be installed in the chroot\n");
	}
    };
    for my $deb (@{$self->get_conf('EXTRA_PACKAGES')}) {
	if (-f $deb) {
	    &$check_deb_arch($deb);
	} elsif (-d $deb) {
	    opendir(D, $deb);
	    while (my $f = readdir(D)) {
		next if (! -f "$deb/$f");
		next if ("$deb/$f" !~ /\.deb$/);
		&$check_deb_arch("$deb/$f");
	    }
	    closedir(D);
	} else {
	    $self->log_warning("$deb is neither a regular file nor a directory. Skipping...\n");
	}
    }

    # Check package arch makes sense to build
    if (!$dscarchs) {
	$self->log_warning("dsc has no Architecture: field -- skipping arch check!\n");
    } elsif ($self->get_conf('BUILD_SOURCE')) {
	# If the source package is to be built, then we do not need to check
	# if any of the source package's architectures can be built given the
	# current host architecture because then no matter the Architectures
	# field, at least the source package will end up getting built.
    } else {
	my $valid_arch;
	for my $a (split(/\s+/, $dscarchs)) {
	    # Check architecture wildcard matching with dpkg inside the chroot
	    # to avoid situations in which dpkg outside the chroot doesn't
	    # know about a new architecture yet
	    my $command = <<"EOF";
		use strict;
		use warnings;
		use Dpkg::Arch;
		if (Dpkg::Arch::debarch_is('$host_arch', '$a')) {
		    exit 0;
		}
		exit 1;
EOF
	    $session->run_command(
		{ COMMAND => ['perl',
			'-e',
			$command],
		    USER => 'root',
		    PRIORITY => 0,
		    DIR => '/' });
	    if ($? == 0) {
		$valid_arch = 1;
		last;
	    }
	}
	if ($dscarchs ne "any" && !($valid_arch) &&
	    !($dscarchs =~ /\ball\b/ && $self->get_conf('BUILD_ARCH_ALL')) )  {
	    my $msg = "dsc: $host_arch not in arch list or does not match any arch wildcards: $dscarchs -- skipping";
	    $self->log_error("$msg\n");
	    Sbuild::Exception::Build->throw(error => $msg,
					    status => "skipped",
					    failstage => "arch-check");
	    return 0;
	}
    }

    $self->log("Arch check ok ($host_arch included in $dscarchs)\n");

    return 1;
}

# Subroutine that runs any command through the system (i.e. not through the
# chroot. It takes a string of a command with arguments to run along with
# arguments whether to save STDOUT and/or STDERR to the log stream
sub run_command {
    my $self = shift;
    my $command = shift;
    my $log_output = shift;
    my $log_error = shift;
    my $chroot = shift;

    # Used to determine if we are to log from commands
    my ($out, $err, $defaults);

    # Run the command and save the exit status
	if (!$chroot)
	{
	    $defaults = $self->get('Host')->{'Defaults'};
	    $out = $defaults->{'STREAMOUT'} if ($log_output);
	    $err = $defaults->{'STREAMERR'} if ($log_error);

	    my %args = (PRIORITY  => 0,
			STREAMOUT => $out,
			STREAMERR => $err);
	    if(ref $command) {
		$args{COMMAND} = \@{$command};
		$args{COMMAND_STR} = "@{$command}";
	    } else {
		$args{COMMAND} = [split('\s+', $command)];
		$args{COMMAND_STR} = $command;
	    }

	    $self->get('Host')->run_command( \%args );
	} else {
	    $defaults = $self->get('Session')->{'Defaults'};
	    $out = $defaults->{'STREAMOUT'} if ($log_output);
	    $err = $defaults->{'STREAMERR'} if ($log_error);

	    my %args = (USER => 'root',
			PRIORITY => 0,
			STREAMOUT => $out,
			STREAMERR => $err);
	    if(ref $command) {
		$args{COMMAND} = \@{$command};
		$args{COMMAND_STR} = "@{$command}";
	    } else {
		$args{COMMAND} = [split('\s+', $command)];
		$args{COMMAND_STR} = $command;
	    }

	    $self->get('Session')->run_command( \%args );
	}
    my $status = $?;

    # Check if the command failed
    if ($status != 0) {
	return 0;
    }
    return 1;
}

# Subroutine that processes external commands to be run during various stages of
# an sbuild run. We also ask if we want to log any output from the commands
sub run_external_commands {
    my $self = shift;
    my $stage = shift;

    my $log_output = $self->get_conf('LOG_EXTERNAL_COMMAND_OUTPUT');
    my $log_error  = $self->get_conf('LOG_EXTERNAL_COMMAND_ERROR');

    # Return success now unless there are commands to run
    return 1 unless (${$self->get_conf('EXTERNAL_COMMANDS')}{$stage});

    # Determine which set of commands to run based on the parameter $stage
    my @commands = @{${$self->get_conf('EXTERNAL_COMMANDS')}{$stage}};
    return 1 if !(@commands);

    # Create appropriate log message and determine if the commands are to be
    # run inside the chroot or not, and as root or not.
    my $chroot = 0;
    if ($stage eq "pre-build-commands") {
	$self->log_subsection("Pre Build Commands");
    } elsif ($stage eq "chroot-setup-commands") {
	$self->log_subsection("Chroot Setup Commands");
	$chroot = 1;
    } elsif ($stage eq "chroot-update-failed-commands") {
	$self->log_subsection("Chroot-update Install Failed Commands");
	$chroot = 1;
    } elsif ($stage eq "build-deps-failed-commands") {
	$self->log_subsection("Build-Deps Install Failed Commands");
	$chroot = 1;
    } elsif ($stage eq "build-failed-commands") {
	$self->log_subsection("Generic Build Failed Commands");
	$chroot = 1;
    } elsif ($stage eq "starting-build-commands") {
	$self->log_subsection("Starting Timed Build Commands");
	$chroot = 1;
    } elsif ($stage eq "finished-build-commands") {
	$self->log_subsection("Finished Timed Build Commands");
	$chroot = 1;
    } elsif ($stage eq "chroot-cleanup-commands") {
	$self->log_subsection("Chroot Cleanup Commands");
	$chroot = 1;
    } elsif ($stage eq "post-build-commands") {
	$self->log_subsection("Post Build Commands");
    } elsif ($stage eq "post-build-failed-commands") {
	$self->log_subsection("Post Build Failed Commands");
    }

    # Run each command, substituting the various percent escapes (like
    # %SBUILD_DSC) from the commands to run with the appropriate subsitutions.
    my $hostarch = $self->get('Host Arch');
    my $buildarch = $self->get('Build Arch');
    my $build_dir = $self->get('Build Dir');
    my $shell_cmd = "bash -i </dev/tty >/dev/tty 2>/dev/tty";
    my %percent = (
	"%" => "%",
	"a" => $hostarch, "SBUILD_HOST_ARCH" => $hostarch,
	                  "SBUILD_BUILD_ARCH" => $buildarch,
	"b" => $build_dir, "SBUILD_BUILD_DIR" => $build_dir,
	"s" => $shell_cmd, "SBUILD_SHELL" => $shell_cmd,
    );
    if ($self->get('Changes File')) {
	my $changes = $self->get('Changes File');
	$percent{c} = $changes;
	$percent{SBUILD_CHANGES} = $changes;
    }
    # In case set_version has not been run yet, we do not know the dsc file or
    # directory yet. This can happen if the user only specified a source
    # package name without a version on the command line.
    if ($self->get('DSC Dir')) {
	my $dsc = $self->get('DSC');
	$percent{d} = $dsc;
	$percent{SBUILD_DSC} = $dsc;
	my $pkgbuild_dir = $build_dir . '/' . $self->get('DSC Dir');
	$percent{p} = $pkgbuild_dir;
	$percent{SBUILD_PKGBUILD_DIR} = $pkgbuild_dir;
    }
    if ($chroot == 0) {
	my $chroot_dir = $self->get('Session')->get('Location');
	$percent{r} = $chroot_dir;
	$percent{SBUILD_CHROOT_DIR} = $chroot_dir;
	# the %SBUILD_CHROOT_EXEC escape is only defined when the command is
	# to be run outside the chroot
	my $exec_string = $self->get('Session')->get_internal_exec_string();
	$percent{e} = $exec_string;
	$percent{SBUILD_CHROOT_EXEC} = $exec_string;
    }
    # Our escapes pattern, with longer escapes first, then sorted lexically.
    my $keyword_pat = join("|",
	sort {length $b <=> length $a || $a cmp $b} keys %percent);
    my $returnval = 1;
    foreach my $command (@commands) {

	my $substitute = sub {
	    foreach(@_) {
		if (/\%SBUILD_CHROOT_DIR/ || /\%r/) {
		    $self->log_warning("The %SBUILD_CHROOT_DIR and %r percentage escapes are deprecated and should not be used anymore. Please use %SBUILD_CHROOT_EXEC or %e instead.");
		}
		s{
		     # Match a percent followed by a valid keyword
		     \%($keyword_pat)
	     }{
		 # Substitute with the appropriate value only if it's defined
		 $percent{$1} || $&
	     }msxge;
	    }
	};

	my $command_str;
	if( ref $command ) {
	    $substitute->(@{$command});
	    $command_str = join(" ", @{$command});
	} else {
	    $substitute->($command);
	    $command_str = $command;
	}

	$self->log_subsubsection("$command_str");

	$returnval = $self->run_command($command, $log_output, $log_error, $chroot);
	$self->log("\n");
	if (!$returnval) {
	    $self->log_error("Command '$command_str' failed to run.\n");
	    # do not run any other commands of this type after the first
	    # failure
	    last;
	} else {
	    $self->log_info("Finished running '$command_str'.\n");
	}
    }
    $self->log("\nFinished processing commands.\n");
    $self->log_sep();
    return $returnval;
}

sub run_lintian {
    my $self = shift;
    my $session = $self->get('Session');

    return 1 unless ($self->get_conf('RUN_LINTIAN'));
    $self->set('Lintian Reason', 'error');

    if (!defined($session)) {
	$self->log_error("Session is undef. Cannot run lintian.\n");
	return 0;
    }

    $self->log_subsubsection("lintian");

    my $build_dir = $self->get('Build Dir');
    my $resolver = $self->get('Dependency Resolver');
    my $lintian = $self->get_conf('LINTIAN');
    my $changes = $self->get_changes();
    if (!defined($changes)) {
	$self->log_error(".changes is undef. Cannot run lintian.\n");
	return 0;
    }

    my @lintian_command = ($lintian);
    push @lintian_command, @{$self->get_conf('LINTIAN_OPTIONS')} if
        ($self->get_conf('LINTIAN_OPTIONS'));
    push @lintian_command, $changes;

    # If the source package was not instructed to be built, then it will not
    # be part of the .changes file and thus, the .dsc has to be passed to
    # lintian in addition to the .changes file.
    if (!$self->get_conf('BUILD_SOURCE')) {
	my $dsc = $self->get('DSC File');
	push @lintian_command, $dsc;
    }

    $resolver->add_dependencies('LINTIAN', 'lintian:native', "", "", "", "", "");
    return 1 unless $resolver->install_deps('lintian', 'LINTIAN');

    # we are not using read_command() because we also need the output for
    # non-zero exit codes
    my $pipe = $session->pipe_command(
        { COMMAND => \@lintian_command,
          PRIORITY => 0,
          DIR => $self->get('Build Dir'),
	  PIPE => "in"
        });
    if (!$pipe) {
        $self->log_error("Failed to exec Lintian: $!\n");
	return 0;
    }
    my $lintian_output = "";
    while (my $line = <$pipe>) {
	$self->log($line);
	$lintian_output .= $line;
    }
    close $pipe;

    $self->log("\n");
    if ($?) {
	my $status = $? >> 8;
        my $why = "unknown reason";
	$self->set('Lintian Reason', 'fail') if ($status == 1);
        $why = "runtime error" if ($status == 2);
        $why = "policy violation" if ($status == 1);
        $why = "received signal " . $? & 127 if ($? & 127);
        $self->log_error("Lintian run failed ($why)\n");

        return 0;
    } else {
	$self->set('Lintian Reason', 'pass');
	if ($lintian_output =~ m/^I: /m) {
	    $self->set('Lintian Reason', 'info');
	}
	if ($lintian_output =~ m/^W: /m) {
	    $self->set('Lintian Reason', 'warn');
	}
    }

    $self->log_info("Lintian run was successful.\n");
    return 1;
}

sub run_piuparts {
    my $self = shift;

    return 1 unless ($self->get_conf('RUN_PIUPARTS'));
    $self->set('Piuparts Reason', 'fail');

    $self->log_subsubsection("piuparts");

    my $piuparts = $self->get_conf('PIUPARTS');
    my @piuparts_command;
    # The default value is the empty array.
    # If the value is the default (empty array) prefix with 'sudo --'
    # If the value is a non-empty array, prefix with its values except if the
    # first value is an empty string in which case, prefix with nothing
    # If the value is not an array, prefix with that scalar except if the
    # scalar is the empty string in which case, prefix with nothing
    if (ref($self->get_conf('PIUPARTS_ROOT_ARGS')) eq "ARRAY") {
	if (scalar(@{$self->get_conf('PIUPARTS_ROOT_ARGS')})) {
	    if (@{$self->get_conf('PIUPARTS_ROOT_ARGS')}[0] ne '') {
		push @piuparts_command, @{$self->get_conf('PIUPARTS_ROOT_ARGS')};
	    }
	} else {
	    push @piuparts_command, 'sudo', '--';
	}
    } else {
	if ($self->get_conf('PIUPARTS_ROOT_ARGS') ne '') {
	    push @piuparts_command, $self->get_conf('PIUPARTS_ROOT_ARGS');
	}
    }
    push @piuparts_command, $piuparts;
    push @piuparts_command, @{$self->get_conf('PIUPARTS_OPTIONS')} if
        ($self->get_conf('PIUPARTS_OPTIONS'));
    push @piuparts_command, $self->get('Changes File');
    $self->get('Host')->run_command(
        { COMMAND => \@piuparts_command,
          PRIORITY => 0,
        });
    my $status = $? >> 8;

    # We must check for Ctrl+C (and other aborting signals) directly after
    # running the command so that we do not mark the piuparts run as successful
    # (the exit status will be zero)
    $self->check_abort();

    $self->log("\n");

    if ($status == 0) {
	$self->set('Piuparts Reason', 'pass');
    } else {
        $self->log_error("Piuparts run failed.\n");
        return 0;
    }

    $self->log_info("Piuparts run was successful.\n");
    return 1;
}

sub run_autopkgtest {
    my $self = shift;

    return 1 unless ($self->get_conf('RUN_AUTOPKGTEST'));

    $self->set('Autopkgtest Reason', 'fail');

    $self->log_subsubsection("autopkgtest");

    my $session = $self->get('Session');

    # sbuild used to check whether debian/tests/control exists and would not
    # run autopkgtest at all if it didn't. This is wrong behaviour because
    # even packages without a debian/tests/control or packages without a
    # Testsuite: field in debian/control might still have autopkgtests as they
    # are generated by autodep8. We will not attempt to recreate the autodep8
    # heuristics here and thus we will always run autopkgtest if
    # RUN_AUTOPKGTEST was set to true. Also see
    # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=916924

    my $autopkgtest = $self->get_conf('AUTOPKGTEST');
    my @autopkgtest_command;
    # The default value is the empty array.
    # If the value is the default (empty array) prefix with 'sudo --'
    # If the value is a non-empty array, prefix with its values except if the
    # first value is an empty string in which case, prefix with nothing
    # If the value is not an array, prefix with that scalar except if the
    # scalar is the empty string in which case, prefix with nothing
    if (ref($self->get_conf('AUTOPKGTEST_ROOT_ARGS')) eq "ARRAY") {
	if (scalar(@{$self->get_conf('AUTOPKGTEST_ROOT_ARGS')}) == 0) {
	    push @autopkgtest_command, 'sudo', '--';
	} elsif (@{$self->get_conf('AUTOPKGTEST_ROOT_ARGS')}[0] eq '') {
	    # do nothing if the first array element is the empty string
	} else {
	    push @autopkgtest_command, @{$self->get_conf('AUTOPKGTEST_ROOT_ARGS')};
	}
    } elsif ($self->get_conf('AUTOPKGTEST_ROOT_ARGS') eq '') {
	# do nothing if the configuration value is the empty string
    } else {
	push @autopkgtest_command, $self->get_conf('AUTOPKGTEST_ROOT_ARGS');
    }
    push @autopkgtest_command, $autopkgtest;
    my $tmpdir;
    my @cwd_files;
    # If the source package was not instructed to be built, then it will not
    # be part of the .changes file and thus, the .dsc has to be passed to
    # autopkgtest in addition to the .changes file.
    if (!$self->get_conf('BUILD_SOURCE')) {
	my $dsc = $self->get('DSC');
	# If the source package was downloaded by sbuild, then the .dsc
	# and the files it references have to be made available to the
	# host
	if (! -f $dsc || ! -r $dsc) {
	    my $build_dir = $self->get('Build Dir');
	    $tmpdir = mkdtemp("/tmp/tmp.sbuild.XXXXXXXXXX");
	    if (!$session->copy_from_chroot("$build_dir/$dsc", "$tmpdir/$dsc")) {
		$self->log_error("cannot copy .dsc from chroot\n");
		rmdir $tmpdir;
		return 0;
	    }
	    @cwd_files = dsc_files("$tmpdir/$dsc");
	    foreach (@cwd_files) {
		if (!$session->copy_from_chroot("$build_dir/$_", "$tmpdir/$_")) {
		    $self->log_error("cannot copy $_ from chroot\n");
		    unlink "$tmpdir/$.dsc";
		    foreach (@cwd_files) {
			unlink "$tmpdir/$_" if -f "$tmpdir/$_";
		    }
		    rmdir $tmpdir;
		    return 0;
		}
	    }
	    $dsc = "$tmpdir/$dsc";
	}
	push @autopkgtest_command, $dsc;
    }
    push @autopkgtest_command, $self->get('Changes File');
    if (scalar(@{$self->get_conf('AUTOPKGTEST_OPTIONS')})) {
	push @autopkgtest_command, @{$self->get_conf('AUTOPKGTEST_OPTIONS')};
    } else {
	push @autopkgtest_command, '--', 'null';
    }
    $self->get('Host')->run_command(
        { COMMAND => \@autopkgtest_command,
          PRIORITY => 0,
        });
    my $status = $? >> 8;
    # if the source package wasn't built and also initially downloaded by
    # sbuild, then the temporary directory that was created must be removed
    if (defined $tmpdir) {
	my $dsc = $self->get('DSC');
	unlink "$tmpdir/$dsc";
	foreach (@cwd_files) {
	    unlink "$tmpdir/$_";
	}
	rmdir $tmpdir;
    }

    # We must check for Ctrl+C (and other aborting signals) directly after
    # running the command so that we do not mark the autopkgtest as successful
    # (the exit status will be zero)
    # But we must check only after the temporary directory has been removed.
    $self->check_abort();

    $self->log("\n");

    if ($status == 0) {
	$self->set('Autopkgtest Reason', 'pass');
    } elsif ($status == 8) {
	$self->set('Autopkgtest Reason', 'no tests');
    } else {
	# fail if neither all tests passed nor was the package without tests
	$self->log_error("Autopkgtest run failed.\n");
	return 0;
    }

    $self->log_info("Autopkgtest run was successful.\n");
    return 1;
}

sub explain_bd_uninstallable {
    my $self = shift;

    my $resolver = $self->get('Dependency Resolver');

    my $dummy_pkg_name = $resolver->get_sbuild_dummy_pkg_name('main');

    if (!defined $self->get_conf('BD_UNINSTALLABLE_EXPLAINER')) {
	return 0;
    } elsif ($self->get_conf('BD_UNINSTALLABLE_EXPLAINER') eq '') {
	return 0;
    } elsif ($self->get_conf('BD_UNINSTALLABLE_EXPLAINER') eq 'apt') {
	my (@instd, @rmvd);
	my @apt_args = ('--simulate', \@instd, \@rmvd, 'install', $dummy_pkg_name,
	    '-oDebug::pkgProblemResolver=true', '-oDebug::pkgDepCache::Marker=1',
	    '-oDebug::pkgDepCache::AutoInstall=1', '-oDebug::BuildDeps=1'
	);
	$resolver->run_apt(@apt_args);
    } elsif ($self->get_conf('BD_UNINSTALLABLE_EXPLAINER') eq 'dose3') {
	# To retrieve all Packages files apt knows about we use "apt-get
	# indextargets" and "apt-helper cat-file". The former is able to
	# report the filesystem path of all input Packages files. The latter
	# is able to decompress the files if necessary.
	#
	# We do not use "apt-cache dumpavail" or convert the EDSP output to a
	# Packages file because that would make the package selection subject
	# to apt pinning. This limitation would be okay if there was only the
	# apt resolver but since there also exists the aptitude and aspcud
	# resolvers which are able to find solution without pinning
	# restrictions, we don't want to limit ourselves by it. In cases where
	# apt cannot find a solution, this check is supposed to allow the user
	# to know that choosing a different resolver might fix the problem.
	$resolver->add_dependencies('DOSE3', 'dose-distcheck:native', "", "", "", "", "");
	if (!$resolver->install_deps('dose3', 'DOSE3')) {
	    return 0;
	}

	my $session = $self->get('Session');
	my $pipe_apt = $session->pipe_command({
		COMMAND => [ 'apt-get', 'indextargets', '--format', '$(FILENAME)', 'Created-By: Packages' ],
		USER => $self->get_conf('BUILD_USER'),
	    });
	if (!$pipe_apt) {
	    $self->log_error("cannot open reading pipe from apt-get indextargets\n");
	    return 0;
	}

	my $host = $self->get_conf('HOST_ARCH');
	my $build = $self->get_conf('BUILD_ARCH');
	my @debforeignarg = ();
	if ($build ne $host) {
	    @debforeignarg = ('--deb-foreign-archs', $host);
	}

	# - We run dose-debcheck instead of dose-builddebcheck because we want
	#   to check the dummy binary package created by sbuild instead of the
	#   original source package Build-Depends.
	# - We use dose-debcheck instead of dose-distcheck because we cannot
	#   use the deb:// prefix on data from standard input.
	my $pipe_dose = $session->pipe_command({
		COMMAND => ['dose-debcheck',
		    '--checkonly', "$dummy_pkg_name:$host", '--verbose',
		    '--failures', '--successes', '--explain',
		    '--deb-native-arch', $self->get_conf('BUILD_ARCH'), @debforeignarg ],
		PRIORITY => 0,
		USER => $self->get_conf('BUILD_USER'),
		PIPE => 'out'
	    });
	if (!$pipe_dose) {
	    $self->log_error("cannot open writing pipe to dose-debcheck\n");
	    return 0;
	}

	# We parse file by file instead of concatenating all files because if
	# there are many files, we might exceed the maximum command length and
	# it avoids having to have the data from all Packages files in memory
	# all at once. Working with a smaller Dpkg::Index structure should
	# also result in faster store and retrieval times.
	while (my $fname = <$pipe_apt>) {
	    chomp $fname;
	    my $pipe_cat = $session->pipe_command({
		    COMMAND => [ '/usr/lib/apt/apt-helper', 'cat-file', $fname ],
		    USER => $self->get_conf('BUILD_USER'),
		});
	    if (!$pipe_cat) {
		$self->log_error("cannot open reading pipe from apt-helper\n");
		return 0;
	    }

	    # For native compilation we just pipe the output of apt-helper to
	    # dose3. For cross compilation we need to filter foreign
	    # architecture packages that are Essential:yes or
	    # Multi-Arch:foreign or otherwise dose3 might present a solution
	    # that installs foreign architecture Essential:yes or
	    # Multi-Arch:foreign packages.
	    if ($build eq $host) {
		File::Copy::copy $pipe_cat, $pipe_dose;
	    } else {
		my $key_func = sub {
		    return $_[0]->{Package} . ' ' . $_[0]->{Version} . ' ' . $_[0]->{Architecture};
		};

		my $index = Dpkg::Index->new(get_key_func=>$key_func);

		if (!$index->parse($pipe_cat, 'apt-helper cat-file')) {
		    $self->log_error("Cannot parse output of apt-helper cat-file: $!\n");
		    return 0;
		}

		foreach my $key ($index->get_keys()) {
		    my $cdata = $index->get_by_key($key);
		    my $arch = $cdata->{'Architecture'} // '';
		    my $ess = $cdata->{'Essential'} // '';
		    my $ma = $cdata->{'Multi-Arch'} // '';
		    if ($arch ne 'all' && $arch ne $build
			&& ($ess eq 'yes' || $ma eq 'foreign')) {
			next;
		    }
		    $cdata->output($pipe_dose);
		    print $pipe_dose "\n";
		}
	    }

	    close($pipe_cat);
	    if (($? >> 8) != 0) {
		$self->log_error("apt-helper failed\n");
		return 0;
	    }
	}

	close $pipe_dose;
	# - We expect an exit code of less than 64 of dose-debcheck. Any other
	#   exit code indicates abnormal program termination.
	if (($? >> 8) >= 64) {
	    $self->log_error("dose-debcheck failed\n");
	    return 0;
	}

	close $pipe_apt;
	if (($? >> 8) != 0) {
	    $self->log_error("apt-get indextargets failed\n");
	    return 0;
	}


    }

    return 1;
}

sub build {
    my $self = shift;

    my $dscfile = $self->get('DSC File');
    my $dscdir = $self->get('DSC Dir');
    my $pkg = $self->get('Package');
    my $build_dir = $self->get('Build Dir');
    my $host_arch = $self->get('Host Arch');
    my $build_arch = $self->get('Build Arch');
    my $session = $self->get('Session');

    my( $rv, $changes );
    local( *PIPE, *F, *F2 );

    $self->log_subsection("Build");
    $self->set('This Space', 0);

    my $tmpunpackdir = $dscdir;
    $tmpunpackdir =~ s/-.*$/.orig.tmp-nest/;
    $tmpunpackdir =~ s/_/-/;
    $tmpunpackdir = "$build_dir/$tmpunpackdir";

    $dscdir = "$build_dir/$dscdir";

    $self->log_subsubsection("Unpack source");
    if ($session->test_directory($dscdir) && $session->test_symlink($dscdir)) {
	# if the package dir already exists but is a symlink, complain
	$self->log_error("Cannot unpack source: a symlink to a directory with the\n".
		   "same name already exists.\n");
	return 0;
    }
    my $dsccontent = $session->read_file("$build_dir/$dscfile");
    if (!$dsccontent) {
	$self->log_error("Cannot read $build_dir/$dscfile\n");
    } else {
	$self->log($dsccontent);
	$self->log("\n");
    }
    if (!$session->test_directory($dscdir)) {
	$self->set('Sub Task', "dpkg-source");
	$session->run_command({
		COMMAND => [$self->get_conf('DPKG_SOURCE'),
		    '-x', $dscfile, $dscdir],
		USER => $self->get_conf('BUILD_USER'),
		DIR => $build_dir,
		PRIORITY => 0});
	if ($?) {
	    $self->log_error("FAILED [dpkg-source died]\n");
	    Sbuild::Exception::Build->throw(error => "FAILED [dpkg-source died]",
					    failstage => "unpack");
	}

	if (!$session->chmod($dscdir, 'g-s,go+rX', { RECURSIVE => 1 })) {
	    $self->log_error("chmod -R g-s,go+rX $dscdir failed.\n");
	    Sbuild::Exception::Build->throw(error => "chmod -R g-s,go+rX $dscdir failed",
					    failstage => "unpack");
	}
    }
    else {
	$self->log_subsubsection("Check unpacked source");
	# check if the unpacked tree is really the version we need
	my $clog = $session->read_command(
	    { COMMAND => ['dpkg-parsechangelog'],
	      USER => $self->get_conf('BUILD_USER'),
	      PRIORITY => 0,
	      DIR => $dscdir});
	if (!$clog) {
	    $self->log_error("unable to read from dpkg-parsechangelog\n");
	    Sbuild::Exception::Build->throw(error => "unable to read from dpkg-parsechangelog",
					    failstage => "check-unpacked-version");
	}
	$self->set('Sub Task', "dpkg-parsechangelog");

	if ($clog !~ /^Version:\s*(.+)\s*$/mi) {
	    $self->log_error("dpkg-parsechangelog didn't print Version:\n");
	    Sbuild::Exception::Build->throw(error => "dpkg-parsechangelog didn't print Version:",
					    failstage => "check-unpacked-version");
	}
    }

    $self->log_subsubsection("Check disk space");
    chomp(my $current_usage = $session->read_command({ COMMAND => ["du", "-k", "-s", "$dscdir"]}));
    if ($?) {
	$self->log_error("du exited with non-zero exit status $?\n");
	Sbuild::Exception::Build->throw(error => "du exited with non-zero exit status $?", failstage => "check-space");
    }
    $current_usage =~ /^(\d+)/;
    $current_usage = $1;
    if ($current_usage) {
	my $pipe = $session->pipe_command({ COMMAND => ["df", "-k", "$dscdir"]});
	my $free;
	while (<$pipe>) {
	    $free = (split /\s+/)[3];
	}
	close $pipe;
	if ($?) {
	    $self->log_error("df exited with non-zero exit status $?\n");
	    Sbuild::Exception::Build->throw(error => "df exited with non-zero exit status $?", failstage => "check-space");
	}
	if ($free < 2*$current_usage && $self->get_conf('CHECK_SPACE')) {
	    Sbuild::Exception::Build->throw(error => "Disk space is probably not sufficient for building.",
		info => "Source needs $current_usage KiB, while $free KiB is free.)",
		failstage => "check-space");
	} else {
	    $self->log("Sufficient free space for build\n");
	}
    }

    my $clogpipe = $session->pipe_command(
	{ COMMAND => ['dpkg-parsechangelog'],
	  USER => $self->get_conf('BUILD_USER'),
	  PRIORITY => 0,
	  DIR => $dscdir });
    if (!$clogpipe) {
	    $self->log_error("unable to read from dpkg-parsechangelog\n");
	    Sbuild::Exception::Build->throw(error => "unable to read from dpkg-parsechangelog",
					    failstage => "check-unpacked-version");
    }

    my $clog = Dpkg::Control->new(type => CTRL_CHANGELOG);
    if (!$clog->parse($clogpipe, "$dscdir/debian/changelog")) {
	$self->log_error("unable to parse debian/changelog\n");
	Sbuild::Exception::Build->throw(error => "unable to parse debian/changelog",
	    failstage => "check-unpacked-version");
    }

    close($clogpipe);

    my $name = $clog->{Source};
    my $version = $clog->{Version};
    my $dists = $clog->{Distribution};
    my $urgency = $clog->{Urgency};

    if ($dists ne $self->get_conf('DISTRIBUTION')) {
	$self->build_log_colour('yellow',
				"^Distribution: " . $self->get_conf('DISTRIBUTION') . "\$");
    }

    if ($self->get_conf('BIN_NMU') || $self->get_conf('APPEND_TO_VERSION')
	|| defined $self->get_conf('BIN_NMU_CHANGELOG')) {
	$self->log_subsubsection("Hack binNMU version");

	my $text = $session->read_file("$dscdir/debian/changelog");

	if (!$text) {
	    $self->log_error("Can't open debian/changelog -- no binNMU hack!\n");
	    Sbuild::Exception::Build->throw(error => "Can't open debian/changelog -- no binNMU hack: $!!",
		failstage => "hack-binNMU");
	}

	my $NMUversion = $self->get('Version');

	my $clogpipe = $session->get_write_file_handle("$dscdir/debian/changelog");

	if (!$clogpipe) {
	    $self->log_error("Can't open debian/changelog for binNMU hack: $!\n");
	    Sbuild::Exception::Build->throw(error => "Can't open debian/changelog for binNMU hack: $!",
		failstage => "hack-binNMU");
	}
	if (defined $self->get_conf('BIN_NMU_CHANGELOG')) {
	    my $clogentry = $self->get_conf('BIN_NMU_CHANGELOG');
	    # trim leading and trailing whitespace and linebreaks
	    $clogentry =~ s/^\s+|\s+$//g;
	    print $clogpipe $clogentry . "\n\n";
	} else {
	    if (!$self->get_conf('MAINTAINER_NAME')) {
		Sbuild::Exception::Build->throw(error => "No maintainer specified.",
		    info => 'When making changelog additions for a binNMU or appending a version suffix, a maintainer must be specified for the changelog entry e.g. using $maintainer_name, $uploader_name or $key_id, (or the equivalent command-line options)',
		    failstage => "check-space");
	    }

	    $dists = $self->get_conf('DISTRIBUTION');

	    print $clogpipe "$name ($NMUversion) $dists; urgency=low, binary-only=yes\n\n";
	    if ($self->get_conf('APPEND_TO_VERSION')) {
		print $clogpipe "  * Append ", $self->get_conf('APPEND_TO_VERSION'),
		" to version number; no source changes\n";
	    }
	    if ($self->get_conf('BIN_NMU')) {
		print $clogpipe "  * Binary-only non-maintainer upload for $host_arch; ",
		"no source changes.\n";
		print $clogpipe "  * ", join( "    ", split( "\n", $self->get_conf('BIN_NMU') )), "\n";
	    }
	    print $clogpipe "\n";

	    # Earlier implementations used the date of the last changelog
	    # entry for the new entry so that Multi-Arch:same packages would
	    # be co-installable (their shared changelogs had to match). This
	    # is not necessary anymore as binNMU changelogs are now written
	    # into architecture specific paths. Re-using the date of the last
	    # changelog entry has the disadvantage that this will effect
	    # SOURCE_DATE_EPOCH which in turn will make the timestamps of the
	    # files in the new package equal to the last version which can
	    # confuse backup programs.  By using the build date for the new
	    # binNMU changelog timestamp we make sure that the timestamps of
	    # changed files inside the new package advanced in comparison to
	    # the last version.
	    #
	    # The timestamp format has to follow Debian Policy §4.4
	    #   https://www.debian.org/doc/debian-policy/ch-source.html#s-dpkgchangelog
	    # which is the same format as `date -R`
	    my $date;
	    if (defined $self->get_conf('BIN_NMU_TIMESTAMP')) {
		if ($self->get_conf('BIN_NMU_TIMESTAMP') =~ /^\+?[1-9]\d*$/) {
		    $date = strftime_c "%a, %d %b %Y %H:%M:%S +0000",
		    gmtime($self->get_conf('BIN_NMU_TIMESTAMP'));
		} else {
		    $date = $self->get_conf('BIN_NMU_TIMESTAMP');
		}
	    } else {
		$date = strftime_c "%a, %d %b %Y %H:%M:%S +0000", gmtime();
	    }
	    print $clogpipe " -- " . $self->get_conf('MAINTAINER_NAME') . "  $date\n\n";
	}
	print $clogpipe $text;
	close($clogpipe);
	$self->log("Created changelog entry for binNMU version $NMUversion\n");
    }

    if ($session->test_regular_file("$dscdir/debian/files")) {
	local( *FILES );
	my @lines;
	my $FILES = $session->get_read_file_handle("$dscdir/debian/files");
	chomp( @lines = <$FILES> );
	close( $FILES );

	$self->log_warning("After unpacking, there exists a file debian/files with the contents:\n");

	$self->log_sep();
	foreach (@lines) {
	    $self->log($_);
	}
	$self->log_sep();
	$self->log("\n");

	$self->log_info("This should be reported as a bug.\n");
	$self->log_info("The file has been removed to avoid dpkg-genchanges errors.\n");

	unlink "$dscdir/debian/files";
    }

    # Build tree not writable during build (except for the sbuild
    # user performing the build).
    if (!$session->chmod($self->get('Build Dir'), 'go-w', { RECURSIVE => 1 })) {
	$self->log_error("chmod og-w " . $self->get('Build Dir') . " failed.\n");
	return 0;
    }

    if (!$self->run_external_commands("starting-build-commands")) {
	Sbuild::Exception::Build->throw(error => "Failed to execute starting-build-commands",
	    failstage => "run-starting-build-commands");
    }

    $self->set('Build Start Time', time);
    $self->set('Build End Time', $self->get('Build Start Time'));

    if ($session->test_regular_file("/etc/ld.so.conf") &&
       ! $session->test_regular_file_readable("/etc/ld.so.conf")) {
	$session->chmod('/etc/ld.so.conf', 'a+r');

	$self->log_subsubsection("Fix ld.so");
	$self->log("ld.so.conf was not readable! Fixed.\n");
    }

    my $buildcmd = [];
    push (@{$buildcmd}, $self->get_conf('BUILD_ENV_CMND'))
	if (defined($self->get_conf('BUILD_ENV_CMND')) &&
	    $self->get_conf('BUILD_ENV_CMND'));
    push (@{$buildcmd}, 'dpkg-buildpackage');

    if ($host_arch ne $build_arch) {
	push (@{$buildcmd}, '-a' . $host_arch);
    }

    if (defined($self->get_conf('BUILD_PROFILES')) &&
	$self->get_conf('BUILD_PROFILES')) {
	my $profiles = $self->get_conf('BUILD_PROFILES');
	$profiles =~ tr/ /,/;
	push (@{$buildcmd}, '-P' . $profiles);
    }

    if (defined($self->get_conf('PGP_OPTIONS')) &&
	$self->get_conf('PGP_OPTIONS')) {
	if (ref($self->get_conf('PGP_OPTIONS')) eq 'ARRAY') {
	    push (@{$buildcmd}, @{$self->get_conf('PGP_OPTIONS')});
        } else {
	    push (@{$buildcmd}, $self->get_conf('PGP_OPTIONS'));
	}
    }

    if (defined($self->get_conf('SIGNING_OPTIONS')) &&
	$self->get_conf('SIGNING_OPTIONS')) {
	if (ref($self->get_conf('SIGNING_OPTIONS')) eq 'ARRAY') {
	    push (@{$buildcmd}, @{$self->get_conf('SIGNING_OPTIONS')});
        } else {
	    push (@{$buildcmd}, $self->get_conf('SIGNING_OPTIONS'));
	}
    }

    use constant dpkgopt => [[["", "-B"], ["-A", "-b" ]], [["-S", "-G"], ["-g", ""]]];
    my $binopt = dpkgopt->[$self->get_conf('BUILD_SOURCE')]
			  [$self->get_conf('BUILD_ARCH_ALL')]
			  [$self->get_conf('BUILD_ARCH_ANY')];
    push (@{$buildcmd}, $binopt) if $binopt;
    push (@{$buildcmd}, "-sa") if ($self->get_conf('BUILD_SOURCE') && $self->get_conf('FORCE_ORIG_SOURCE'));
    push (@{$buildcmd}, "-r" . $self->get_conf('FAKEROOT'));

    if (defined($self->get_conf('DPKG_BUILDPACKAGE_USER_OPTIONS')) &&
	$self->get_conf('DPKG_BUILDPACKAGE_USER_OPTIONS')) {
	push (@{$buildcmd}, @{$self->get_conf('DPKG_BUILDPACKAGE_USER_OPTIONS')});
    }

    # Set up additional build environment variables.
    my %buildenv = %{$self->get_conf('BUILD_ENVIRONMENT')};
    $buildenv{'PATH'} = $self->get_conf('PATH');
    $buildenv{'LD_LIBRARY_PATH'} = $self->get_conf('LD_LIBRARY_PATH')
	if defined($self->get_conf('LD_LIBRARY_PATH'));

	# Add cross environment config
	if ($host_arch ne $build_arch) {
		$buildenv{'CONFIG_SITE'} = "/etc/dpkg-cross/cross-config." . $host_arch;
		# when cross-building, only set "nocheck" if DEB_BUILD_OPTIONS
		# was not already set. This allows overwriting the default by
		# setting the DEB_BUILD_OPTIONS environment variable
		if (!defined($ENV{'DEB_BUILD_OPTIONS'})) {
			$ENV{'DEB_BUILD_OPTIONS'} = "nocheck";
		}
	}

    # Explicitly add any needed environment to the environment filter
    # temporarily for dpkg-buildpackage.
    my @env_filter;
    foreach my $envvar (keys %buildenv) {
	push(@env_filter, "^$envvar\$");
    }

    # Dump build environment
    $self->log_subsubsection("User Environment");
    {
	my $envcmd = $session->read_command(
	    { COMMAND => ['env'],
	      ENV => \%buildenv,
	      ENV_FILTER => \@env_filter,
	      USER => $self->get_conf('BUILD_USER'),
	      SETSID => 1,
	      PRIORITY => 0,
	      DIR => $dscdir
	    });
	if (!$envcmd) {
	    $self->log_error("unable to open pipe\n");
	    Sbuild::Exception::Build->throw(error => "unable to open pipe",
					    failstage => "dump-build-env");
	}

	my @lines=sort(split /\n/, $envcmd);
	foreach my $line (@lines) {
	    $self->log("$line\n");
	}
    }

    $self->log_subsubsection("dpkg-buildpackage");
    $self->log("Command: " . join(' ', @{$buildcmd}) . "\n");

    my $command = {
	COMMAND => $buildcmd,
	ENV => \%buildenv,
	ENV_FILTER => \@env_filter,
	USER => $self->get_conf('BUILD_USER'),
	SETSID => 1,
	PRIORITY => 0,
	DIR => $dscdir,
	STREAMERR => \*STDOUT,
	DISABLE_NETWORK => 1,
    };

    my $pipe = $session->pipe_command($command);
    if (!$pipe) {
	$self->log_error("unable to open pipe\n");
	Sbuild::Exception::Build->throw(error => "unable to open pipe",
	    failstage => "dpkg-buildpackage");
    }

    $self->set('dpkg-buildpackage pid', $command->{'PID'});
    $self->set('Sub Task', "dpkg-buildpackage");

    my $timeout = $self->get_conf('INDIVIDUAL_STALLED_PKG_TIMEOUT')->{$pkg} ||
	$self->get_conf('STALLED_PKG_TIMEOUT');
    $timeout *= 60;
    my $timed_out = 0;
    my(@timeout_times, @timeout_sigs, $last_time);

    local $SIG{'ALRM'} = sub {
	my $pid = $self->get('dpkg-buildpackage pid');
	my $signal = ($timed_out > 0) ? "KILL" : "TERM";
	# negative pid to send to whole process group
	kill "$signal", -$pid;

	$timeout_times[$timed_out] = time - $last_time;
	$timeout_sigs[$timed_out] = $signal;
	$timed_out++;
	$timeout = 5*60; # only wait 5 minutes until next signal
    };

    alarm($timeout);
    # We do not use a while(<$pipe>) {} loop because that one would only read
    # full lines (until $/ is reached). But we do not want to tie "activity"
    # to receiving complete lines on standard output and standard error.
    # Receiving any data should be sufficient for a process to signal that it
    # is still active. Thus, instead of reading lines, we use sysread() which
    # will return us data once it is available even if the data is not
    # terminated by a newline. To still print correctly to the log, we collect
    # unterminated strings into an accumulator and print them to the log once
    # the newline shows up. This has the added advantage that we can now not
    # only treat \n as producing new lines ($/ is limited to a single
    # character) but can also produce new lines when encountering a \r as it
    # is common for progress-meter output of long-running processes.
    my $acc = "";
    while(1) {
	alarm($timeout);
	$last_time = time;
	# The buffer size is really arbitrary and just makes sure not to call
	# this function too often if lots of data is produced by the build.
	# The function will immediately return even with less data than the
	# buffer size once it is available.
	my $ret = sysread($pipe, my $buf, 1024);
	# sysread failed - this for example happens when the build timeouted
	# and is killed as a result
	if (!defined $ret) {
	    last;
	}
	# A return value of 0 signals EOF
	if ($ret == 0) {
	    last;
	}
	# We choose that lines shall not only be terminated by \n but that new
	# log lines are also produced after encountering a \r.
	# A negative limit is used to also produce trailing empty fields if
	# required (think of multiple trailing empty lines).
	my @parts = split /\r|\n/, $buf, -1;
	my $numparts = scalar @parts;
	if ($numparts == 1) {
	    # line terminator was not found
	    $acc .= $buf;
	} elsif ($numparts >= 2) {
	    # first match needs special treatment as it needs to be
	    # concatenated with $acc
	    my $first = shift @parts;
	    $self->log($acc . $first . "\n");
	    my $last = pop @parts;
	    for (my $i = 0; $i < $numparts - 2; $i++) {
		$self->log($parts[$i] . "\n");
	    }
	    # the last part is put into the accumulator. This might
	    # just be the empty string if $buf ended in a line
	    # terminator
	    $acc = $last;
	}
    }
    # If the output didn't end with a line terminator, just print out the rest
    # as we have it.
    if ($acc ne "") {
	$self->log($acc . "\n");
    }
    close($pipe);
    alarm(0);
    $rv = $?;
    $self->set('dpkg-buildpackage pid', undef);

    my $i;
    for( $i = 0; $i < $timed_out; ++$i ) {
	$self->log_error("Build killed with signal " . $timeout_sigs[$i] .
	           " after " . int($timeout_times[$i]/60) .
	           " minutes of inactivity\n");
    }
    $self->set('Build End Time', time);
    $self->set('Pkg End Time', time);
    $self->set('This Time', $self->get('Pkg End Time') - $self->get('Pkg Start Time'));
    $self->set('This Time', 0) if $self->get('This Time') < 0;

    $self->write_stats('build-time',
		       $self->get('Build End Time')-$self->get('Build Start Time'));
    $self->write_stats('install-download-time',
		       $self->get('Install End Time')-$self->get('Install Start Time'));
    my $finish_date = strftime_c "%FT%TZ", gmtime($self->get('Build End Time'));
    $self->log_sep();
    $self->log("Build finished at $finish_date\n");


    if (!$self->run_external_commands("finished-build-commands")) {
	Sbuild::Exception::Build->throw(error => "Failed to execute finished-build-commands",
	    failstage => "run-finished-build-commands");
    }

    my @space_files = ();

    $self->log_subsubsection("Finished");
    if ($rv) {
	Sbuild::Exception::Build->throw(error => "Build failure (dpkg-buildpackage died)",
	    failstage => "build");
    } else {
	$self->log_info("Built successfully\n");

	if ($session->test_regular_file_readable("$dscdir/debian/files")) {
	    my @files = $self->debian_files_list("$dscdir/debian/files");

	    foreach (@files) {
		if (!$session->test_regular_file("$build_dir/$_")) {
		    $self->log_error("Package claims to have built ".basename($_).", but did not.  This is a bug in the packaging.\n");
		    next;
		}
		if (/_all.u?deb$/ and not $self->get_conf('BUILD_ARCH_ALL')) {
		    $self->log_error("Package builds ".basename($_)." when binary-indep target is not called.  This is a bug in the packaging.\n");
		    $session->unlink("$build_dir/$_");
		    next;
		}
	    }
	}

	# Restore write access to build tree now build is complete.
	if (!$session->chmod($self->get('Build Dir'), 'g+w', { RECURSIVE => 1 })) {
	    $self->log_error("chmod g+w " . $self->get('Build Dir') . " failed.\n");
	    return 0;
	}

	$self->log_subsection("Changes");

	# we use an anonymous subroutine so that the referenced variables are
	# automatically rebound to their current values
	my $copy_changes = sub {
	    my $changes = shift;

	    my $F = $session->get_read_file_handle("$build_dir/$changes");
	    if (!$F) {
		$self->log_error("cannot get read file handle for $build_dir/$changes\n");
		Sbuild::Exception::Build->throw(error => "cannot get read file handle for $build_dir/$changes",
		    failstage => "parse-changes");
	    }
	    my $pchanges = Dpkg::Control->new(type => CTRL_FILE_CHANGES);
	    if (!$pchanges->parse($F, "$build_dir/$changes")) {
		$self->log_error("cannot parse $build_dir/$changes\n");
		Sbuild::Exception::Build->throw(error => "cannot parse $build_dir/$changes",
		    failstage => "parse-changes");
	    }
	    close($F);


	    if ($self->get_conf('OVERRIDE_DISTRIBUTION')) {
		$pchanges->{Distribution} = $self->get_conf('DISTRIBUTION');
	    }

	    my $sys_build_dir = $self->get_conf('BUILD_DIR');
	    if (!open( F2, ">$sys_build_dir/$changes.new" )) {
		$self->log("Cannot create $sys_build_dir/$changes.new: $!\n");
		$self->log("Distribution field may be wrong!!!\n");
		if ($build_dir) {
		    if(!$session->copy_from_chroot("$build_dir/$changes", ".")) {
			$self->log_error("Could not copy $build_dir/$changes to .\n");
		    }
		}
	    } else {
		$pchanges->output(\*STDOUT);
		$pchanges->output(\*F2);

		close( F2 );
		rename("$sys_build_dir/$changes.new", "$sys_build_dir/$changes")
		    or $self->log("$sys_build_dir/$changes.new could not be " .
		    "renamed to $sys_build_dir/$changes: $!\n");
		unlink("$build_dir/$changes")
		    if $build_dir;
	    }

	    return $pchanges;
	};

	$changes = $self->get_changes();
	if (!defined($changes)) {
	    $self->log_error(".changes is undef. Cannot copy build results.\n");
	    return 0;
	}
	my @cfiles;
	if ($session->test_regular_file_readable("$build_dir/$changes")) {
	    my(@do_dists, @saved_dists);
	    $self->log_subsubsection("$changes:");

	    my $pchanges = &$copy_changes($changes);
	    $self->set('Changes File', $self->get_conf('BUILD_DIR') . "/$changes");

	    my $checksums = Dpkg::Checksums->new();
	    $checksums->add_from_control($pchanges);

	    push(@cfiles, $checksums->get_files());

	}
	else {
	    $self->log_error("Can't find $changes -- can't dump info\n");
	}

	if ($self->get_conf('SOURCE_ONLY_CHANGES')) {
	    my $so_changes = $self->get('Package_SVersion') . "_source.changes";
	    $self->log_subsubsection("$so_changes:");
	    my $genchangescmd = ['dpkg-genchanges', '--build=source'];
	    if (defined($self->get_conf('SIGNING_OPTIONS')) &&
		$self->get_conf('SIGNING_OPTIONS')) {
		if (ref($self->get_conf('SIGNING_OPTIONS')) eq 'ARRAY') {
		    push (@{$genchangescmd}, @{$self->get_conf('SIGNING_OPTIONS')});
		} else {
		    push (@{$genchangescmd}, $self->get_conf('SIGNING_OPTIONS'));
		}
	    }
	    my $cfile = $session->read_command(
		{ COMMAND => $genchangescmd,
		    USER => $self->get_conf('BUILD_USER'),
		    PRIORITY => 0,
		    DIR => $dscdir});
	    if (!$cfile) {
		$self->log_error("dpkg-genchanges --build=source failed\n");
		Sbuild::Exception::Build->throw(error => "dpkg-genchanges --build=source failed",
		    failstage => "source-only-changes");
	    }
	    if (!$session->write_file("$build_dir/$so_changes", $cfile)) {
		$self->log_error("cannot write content to $build_dir/$so_changes\n");
		Sbuild::Exception::Build->throw(error => "cannot write content to $build_dir/$so_changes",
		    failstage => "source-only-changes");
	    }

	    my $pchanges = &$copy_changes($so_changes);
	}

	$self->log_subsection("Buildinfo");

	foreach (@cfiles) {
	    my $deb = "$build_dir/$_";
	    next if $deb !~ /\.buildinfo$/;
	    my $buildinfo = $session->read_file($deb);
	    if (!$buildinfo) {
		$self->log_error("Cannot read $deb\n");
	    } else {
		$self->log($buildinfo);
		$self->log("\n");
	    }
	}

	$self->log_subsection("Package contents");

	my @debcfiles = @cfiles;
	foreach (@debcfiles) {
	    my $deb = "$build_dir/$_";
	    next if $deb !~ /(\Q$host_arch\E|all)\.(udeb|deb)$/;

	    $self->log_subsubsection("$_");
	    my $dpkg_info = $session->read_command({COMMAND => ["dpkg", "--info", $deb]});
	    if (!$dpkg_info) {
		$self->log_error("Can't spawn dpkg: $! -- can't dump info\n");
	    }
	    else {
		$self->log($dpkg_info);
	    }
	    $self->log("\n");
	    my $dpkg_contents = $session->read_command({COMMAND => ["sh", "-c", "dpkg --contents $deb 2>&1 | sort -k6"]});
	    if (!$dpkg_contents) {
		$self->log_error("Can't spawn dpkg: $! -- can't dump info\n");
	    }
	    else {
		$self->log($dpkg_contents);
	    }
	    $self->log("\n");
	}

	foreach (@cfiles) {
	    push( @space_files, $self->get_conf('BUILD_DIR') . "/$_");
	    if (!$session->copy_from_chroot("$build_dir/$_", $self->get_conf('BUILD_DIR'))) {
		$self->log_error("Could not copy $build_dir/$_ to " . $self->get_conf('BUILD_DIR') . "\n");
	    }
	}
    }

    $self->set('This Space', $self->check_space(@space_files));

    return $rv == 0 ? 1 : 0;
}

# Produce a hash suitable for ENV export
sub get_env ($$) {
    my $self = shift;
    my $prefix = shift;

    sub _env_loop ($$$$) {
	my ($env,$ref,$keysref,$prefix) = @_;

	foreach my $key (keys( %{ $keysref } )) {
	    my $value = $ref->get($key);
	    next if (!defined($value));
	    next if (ref($value));
	    my $name = "${prefix}${key}";
	    $name =~ s/ /_/g;
	    $env->{$name} = $value;
        }
    }

    my $envlist = {};
    _env_loop($envlist, $self, $self, $prefix);
    _env_loop($envlist, $self->get('Config'), $self->get('Config')->{'KEYS'}, "${prefix}CONF_");
    return $envlist;
}

sub get_changes {
    my $self=shift;
    my $changes;

    if ($self->get_conf('BUILD_ARCH_ANY')) {
	$changes = $self->get('Package_SVersion') . '_' . $self->get('Host Arch') . '.changes';
    } elsif ($self->get_conf('BUILD_ARCH_ALL')) {
	$changes = $self->get('Package_SVersion') . "_all.changes";
    } elsif ($self->get_conf('BUILD_SOURCE')) {
	$changes = $self->get('Package_SVersion') . "_source.changes";
    }

    return $changes;
}

sub check_space {
    my $self = shift;
    my @files = @_;
    my $sum = 0;

    my $dscdir = $self->get('DSC Dir');
    return -1 unless (defined $dscdir);

    my $build_dir = $self->get('Build Dir');
    my $pkgbuilddir = "$build_dir/$dscdir";

    # if the source package was not yet unpacked, we will not attempt to compute
    # the required space.
    return -1 unless ($self->get('Session')->test_directory($pkgbuilddir));

    my ($space, $spacenum);

    # get the required space for the unpacked source package in the chroot
    $space = $self->get('Session')->read_command(
	{ COMMAND => ['du', '-k', '-s', $pkgbuilddir],
	    USER => $self->get_conf('USERNAME'),
	    PRIORITY => 0,
	    DIR => '/'});

    if (!$space) {
	$self->log_error("Cannot determine space needed for $pkgbuilddir (du failed)\n");
	return -1;
    }
    # remove the trailing path from the du output
    if (($spacenum) = $space =~ /^(\d+)/) {
	$sum += $spacenum;
    } else {
	$self->log_error("Cannot determine space needed for $pkgbuilddir (unexpected du output): $space\n");
	return -1;
    }

    # get the required space for all produced build artifacts on the host
    # running sbuild
    foreach my $file (@files) {
	$space = $self->get('Host')->read_command(
	    { COMMAND => ['du', '-k', '-s', $file],
	      USER => $self->get_conf('USERNAME'),
	      PRIORITY => 0,
	      DIR => '/'});

	if (!$space) {
	    $self->log_error("Cannot determine space needed for $file (du failed): $!\n");
	    return -1;
	}
	# remove the trailing path from the du output
	if (($spacenum) = $space =~ /^(\d+)/) {
	    $sum += $spacenum;
	} else {
	    $self->log_error("Cannot determine space needed for $file (unexpected du output): $space\n");
	    return -1;
	}
    }

    return $sum;
}

sub lock_file {
    my $self = shift;
    my $file = shift;
    my $for_srcdep = shift;
    my $lockfile = "$file.lock";
    my $try = 0;

  repeat:
    if (!sysopen( F, $lockfile, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644 )){
	if ($! == EEXIST) {
	    # lock file exists, wait
	    goto repeat if !open( F, "<$lockfile" );
	    my $line = <F>;
	    my ($pid, $user);
	    close( F );
	    if ($line !~ /^(\d+)\s+([\w\d.-]+)$/) {
		$self->log_warning("Bad lock file contents ($lockfile) -- still trying\n");
	    }
	    else {
		($pid, $user) = ($1, $2);
		if (kill( 0, $pid ) == 0 && $! == ESRCH) {
		    # process doesn't exist anymore, remove stale lock
		    $self->log_warning("Removing stale lock file $lockfile ".
				       "(pid $pid, user $user)\n");
		    unlink( $lockfile );
		    goto repeat;
		}
	    }
	    ++$try;
	    if (!$for_srcdep && $try > $self->get_conf('MAX_LOCK_TRYS')) {
		$self->log_warning("Lockfile $lockfile still present after " .
				   $self->get_conf('MAX_LOCK_TRYS') *
				   $self->get_conf('LOCK_INTERVAL') .
				   " seconds -- giving up\n");
		return;
	    }
	    $self->log("Another sbuild process ($pid by $user) is currently installing or removing packages -- waiting...\n")
		if $for_srcdep && $try == 1;
	    sleep $self->get_conf('LOCK_INTERVAL');
	    goto repeat;
	}
	$self->log_warning("Can't create lock file $lockfile: $!\n");
    }

    my $username = $self->get_conf('USERNAME');
    F->print("$$ $username\n");
    F->close();
}

sub unlock_file {
    my $self = shift;
    my $file = shift;
    my $lockfile = "$file.lock";

    unlink( $lockfile );
}

sub add_stat {
    my $self = shift;
    my $key = shift;
    my $value = shift;

    $self->get('Summary Stats')->{$key} = $value;
}

sub generate_stats {
    my $self = shift;
    my $resolver = $self->get('Dependency Resolver');

    $self->add_stat('Job', $self->get('Job'));
    $self->add_stat('Package', $self->get('Package'));
    # If the package fails early, then the version might not yet be known.
    # This can happen if the user only specified a source package name on the
    # command line and then the version will only be known after the source
    # package was successfully downloaded.
    if ($self->get('Version')) {
	$self->add_stat('Version', $self->get('Version'));
    }
    if ($self->get('OVersion')) {
	$self->add_stat('Source-Version', $self->get('OVersion'));
    }
    $self->add_stat('Machine Architecture', $self->get_conf('ARCH'));
    $self->add_stat('Host Architecture', $self->get('Host Arch'));
    $self->add_stat('Build Architecture', $self->get('Build Arch'));
    $self->add_stat('Build Profiles', $self->get('Build Profiles'))
        if $self->get('Build Profiles');
    $self->add_stat('Build Type', $self->get('Build Type'));
    my @keylist;
    if (defined $resolver) {
	@keylist=keys %{$resolver->get('Initial Foreign Arches')};
	push @keylist, keys %{$resolver->get('Added Foreign Arches')};
    }
    my $foreign_arches = join ' ', @keylist;
    $self->add_stat('Foreign Architectures', $foreign_arches )
        if $foreign_arches;
    $self->add_stat('Distribution', $self->get_conf('DISTRIBUTION'));
    if ($self->get('This Space') >= 0) {
	$self->add_stat('Space', $self->get('This Space'));
    } else {
	$self->add_stat('Space', "n/a");
    }
    $self->add_stat('Build-Time',
		    $self->get('Build End Time')-$self->get('Build Start Time'));
    $self->add_stat('Install-Time',
		    $self->get('Install End Time')-$self->get('Install Start Time'));
    $self->add_stat('Package-Time',
		    $self->get('Pkg End Time')-$self->get('Pkg Start Time'));
    if ($self->get('This Space') >= 0) {
	$self->add_stat('Build-Space', $self->get('This Space'));
    } else {
	$self->add_stat('Build-Space', "n/a");
    }
    $self->add_stat('Status', $self->get_status());
    $self->add_stat('Fail-Stage', $self->get('Pkg Fail Stage'))
	if ($self->get_status() ne "successful");
    $self->add_stat('Lintian', $self->get('Lintian Reason'))
	if $self->get('Lintian Reason');
    $self->add_stat('Piuparts', $self->get('Piuparts Reason'))
	if $self->get('Piuparts Reason');
    $self->add_stat('Autopkgtest', $self->get('Autopkgtest Reason'))
	if $self->get('Autopkgtest Reason');
}

sub log_stats {
    my $self = shift;
    foreach my $stat (sort keys %{$self->get('Summary Stats')}) {
	$self->log("${stat}: " . $self->get('Summary Stats')->{$stat} . "\n");
    }
}

sub print_stats {
    my $self = shift;
    foreach my $stat (sort keys %{$self->get('Summary Stats')}) {
	print STDOUT "${stat}: " . $self->get('Summary Stats')->{$stat} . "\n";
    }
}

sub write_stats {
    my $self = shift;

    return if (!$self->get_conf('BATCH_MODE'));

    my $stats_dir = $self->get_conf('STATS_DIR');

    return if not defined $stats_dir;

    if (! -d $stats_dir &&
	!mkdir $stats_dir) {
	$self->log_warning("Could not create $stats_dir: $!\n");
	return;
    }

    my ($cat, $val) = @_;
    local( *F );

    $self->lock_file($stats_dir, 0);
    open( F, ">>$stats_dir/$cat" );
    print F "$val\n";
    close( F );
    $self->unlock_file($stats_dir);
}

sub debian_files_list {
    my $self = shift;
    my $files = shift;

    my @list;

    debug("Parsing $files\n");
    my $session = $self->get('Session');

    my $pipe = $session->get_read_file_handle($files);
    if ($pipe) {
	while (<$pipe>) {
	    chomp;
	    my $f = (split( /\s+/, $_ ))[0];
	    push( @list, "$f" );
	    debug("  $f\n");
	}
	close( $pipe ) or $self->log_error("Failed to close $files\n") && return 1;
    }

    return @list;
}

# Figure out chroot architecture
sub chroot_arch {
    my $self = shift;

    chomp(my $chroot_arch = $self->get('Session')->read_command(
	{ COMMAND => ['dpkg', '--print-architecture'],
	  USER => $self->get_conf('BUILD_USER'),
	  PRIORITY => 0,
	  DIR => '/' }));

    if (!$chroot_arch) {
	Sbuild::Exception::Build->throw(error => "Can't determine architecture of chroot: $!",
	    failstage => "chroot-arch")
    }

    return $chroot_arch;
}

sub build_log_filter {
    my $self = shift;
    my $text = shift;
    my $replacement = shift;

    if ($self->get_conf('LOG_FILTER')) {
	$self->log($self->get('FILTER_PREFIX') . $text . ':' . $replacement . "\n");
    }
}

sub build_log_colour {
    my $self = shift;
    my $regex = shift;
    my $colour = shift;

    if ($self->get_conf('LOG_COLOUR')) {
	$self->log($self->get('COLOUR_PREFIX') . $colour . ':' . $regex . "\n");
    }
}

sub open_build_log {
    my $self = shift;

    my $date = strftime_c "%FT%TZ", gmtime($self->get('Pkg Start Time'));

    my $filter_prefix = '__SBUILD_FILTER_' . $$ . ':';
    $self->set('FILTER_PREFIX', $filter_prefix);
    my $colour_prefix = '__SBUILD_COLOUR_' . $$ . ':';
    $self->set('COLOUR_PREFIX', $colour_prefix);

    my $filename = $self->get_conf('LOG_DIR') . '/';
    # we might not know the pkgname_ver string if the user only specified a
    # package name without version
    if ($self->get('Package_SVersion')) {
	$filename .= $self->get('Package_SVersion');
    } else {
	$filename .= $self->get('Package');
    }
    $filename .= '_' . $self->get('Host Arch') . "-$date";
    $filename .= ".build" if $self->get_conf('SBUILD_MODE') ne 'buildd';

    open($saved_stdout, ">&STDOUT") or warn "Can't redirect stdout\n";
    open($saved_stderr, ">&STDERR") or warn "Can't redirect stderr\n";

    my $PLOG;

    my $pid;
    ($pid = open($PLOG, "|-"));
    if (!defined $pid) {
	warn "Cannot open pipe to '$filename': $!\n";
    } elsif ($pid == 0) {
	$SIG{'INT'} = 'IGNORE';
	$SIG{'TERM'} = 'IGNORE';
	$SIG{'QUIT'} = 'IGNORE';
	$SIG{'PIPE'} = 'IGNORE';

	$saved_stdout->autoflush(1);
	if (!$self->get_conf('NOLOG') &&
	    $self->get_conf('LOG_DIR_AVAILABLE')) {
	    unlink $filename; # To prevent opening symlink to elsewhere
	    open( CPLOG, ">$filename" ) or
		Sbuild::Exception::Build->throw(error => "Failed to open build log $filename: $!",
						failstage => "init");
	    CPLOG->autoflush(1);

	    # Create 'current' symlinks
	    if ($self->get_conf('SBUILD_MODE') eq 'buildd') {
		$self->log_symlink($filename,
				   $self->get_conf('BUILD_DIR') . '/current-' .
				   $self->get_conf('DISTRIBUTION'));
	    } else {
		my $symlinktarget = $filename;
		# if symlink target is in the same directory as the symlink
		# itself, make it a relative link instead of an absolute one
		if (Cwd::abs_path($self->get_conf('BUILD_DIR')) eq Cwd::abs_path(dirname($filename))) {
		    $symlinktarget = basename($filename)
		}
		my $symlinkname = $self->get_conf('BUILD_DIR') . '/';
		# we might not know the pkgname_ver string if the user only specified a
		# package name without version
		if ($self->get('Package_SVersion')) {
		    $symlinkname .= $self->get('Package_SVersion');
		} else {
		    $symlinkname .= $self->get('Package');
		}
		$symlinkname .= '_' . $self->get('Host Arch') . ".build";
		$self->log_symlink($symlinktarget, $symlinkname);
	    }
	}

	# Cache vars to avoid repeated hash lookups.
	my $nolog = $self->get_conf('NOLOG');
	my $log = $self->get_conf('LOG_DIR_AVAILABLE');
	my $verbose = $self->get_conf('VERBOSE');
	my $log_colour = $self->get_conf('LOG_COLOUR');
	my @filter = ();
	my @colour = ();
	my ($text, $replacement);
	my $filter_regex = "^$filter_prefix(.*):(.*)\$";
	my $colour_regex = "^$colour_prefix(.*):(.*)\$";
	my @ignore = ();

	while (<STDIN>) {
	    # Add a replacement pattern to filter (sent from main
	    # process in log stream).
	    if (m/$filter_regex/) {
		($text,$replacement)=($1,$2);
		$replacement = "<<$replacement>>";
		push (@filter, [$text, $replacement]);
		$_ = "I: NOTICE: Log filtering will replace '$text' with '$replacement'\n";
	    } elsif (m/$colour_regex/) {
		my ($colour, $regex);
		($colour,$regex)=($1,$2);
		push (@colour, [$colour, $regex]);
#		$_ = "I: NOTICE: Log colouring will colour '$regex' in $colour\n";
		next;
	    } else {
		# Filter out any matching patterns
		foreach my $pattern (@filter) {
		    ($text,$replacement) = @{$pattern};
		    s/\Q$text\E/$replacement/g;
		}
	    }
	    if (m/Deprecated key/ || m/please update your configuration/) {
		my $skip = 0;
		foreach my $ignore (@ignore) {
		    $skip = 1 if ($ignore eq $_);
		}
		next if $skip;
		push(@ignore, $_);
	    }

	    if ($nolog || $verbose) {
		my $colour = 'reset';
		if (-t $saved_stdout && $log_colour) {
		    foreach my $pattern (@colour) {
			if (m/$$pattern[0]/) {
			    $colour = $$pattern[1];
			}
		    }
		    if ($colour ne 'reset') {
			print $saved_stdout color $colour;
		    }
		}

		print $saved_stdout $_;
		if (-t $saved_stdout && $log_colour && $colour ne 'reset') {
		    print $saved_stdout color 'reset';
		}
	    }
	    if (!$nolog && $log) {
		    print CPLOG $_;
	    }
	}

	close CPLOG;
	exit 0;
    }

    $PLOG->autoflush(1);
    open(STDOUT, '>&', $PLOG) or warn "Can't redirect stdout\n";
    open(STDERR, '>&', $PLOG) or warn "Can't redirect stderr\n";
    $self->set('Log File', $filename);
    $self->set('Log Stream', $PLOG);

    my $hostname = $self->get_conf('HOSTNAME');
    $self->log("sbuild (Debian sbuild) $version ($release_date) on $hostname\n");

    my $arch_string = $self->get('Host Arch');
    my $head1 = $self->get('Package');
    if ($self->get('Version')) {
	$head1 .= ' ' . $self->get('Version');
    }
    $head1 .= ' (' . $arch_string . ') ';
    my $head2 = strftime_c "%a, %d %b %Y %H:%M:%S +0000",
			 gmtime($self->get('Pkg Start Time'));
    my $head = $head1;
    # If necessary, insert spaces so that $head1 is left aligned and $head2 is
    # right aligned. If the sum of the length of both is greater than the
    # available space of 76 characters, then no additional padding is
    # inserted.
    if (length($head1) + length($head2) <= 76) {
	$head .= ' ' x (76 - length($head1) - length($head2));
    }
    $head .= $head2;
    $self->log_section($head);

    $self->log("Package: " . $self->get('Package') . "\n");
    if ($self->get('Version') && $self->get('OVersion')) {
	$self->log("Version: " . $self->get('Version') . "\n");
	$self->log("Source Version: " . $self->get('OVersion') . "\n");
    }
    $self->log("Distribution: " . $self->get_conf('DISTRIBUTION') . "\n");
    $self->log("Machine Architecture: " . $self->get_conf('ARCH') . "\n");
    $self->log("Host Architecture: " . $self->get('Host Arch') . "\n");
    $self->log("Build Architecture: " . $self->get('Build Arch') . "\n");
    $self->log("Build Profiles: " . $self->get('Build Profiles') . "\n") if $self->get('Build Profiles');
    $self->log("Build Type: " . $self->get('Build Type') . "\n");
    $self->log("\n");
}

sub close_build_log {
    my $self = shift;

    my $time = $self->get('Pkg End Time');
    if ($time == 0) {
        $time = time;
    }
    my $date = strftime_c "%FT%TZ", gmtime($time);

    my $hours = int($self->get('This Time')/3600);
    my $minutes = int(($self->get('This Time')%3600)/60),
    my $seconds = int($self->get('This Time')%60),
    my $space = "no";
    if ($self->get('This Space') >= 0) {
	$space = sprintf("%dk", $self->get('This Space'));
    }

    my $filename = $self->get('Log File');

    # building status at this point means failure.
    if ($self->get_status() eq "building") {
	$self->set_status('failed');
    }

    $self->log_subsection('Summary');
    $self->generate_stats();
    $self->log_stats();

    $self->log_sep();
    $self->log("Finished at ${date}\n");
    $self->log(sprintf("Build needed %02d:%02d:%02d, %s disk space\n",
	       $hours, $minutes, $seconds, $space));

    if ($self->get_status() eq "successful") {
	if (defined($self->get_conf('KEY_ID')) && $self->get_conf('KEY_ID')) {
	    my $key_id = $self->get_conf('KEY_ID');
	    my $build_dir = $self->get_conf('BUILD_DIR');
	    my $changes;
	    $self->log(sprintf("Signature with key '%s' requested:\n", $key_id));
	    $changes = $self->get_changes();
	    if (!defined($changes)) {
		$self->log_error(".changes is undef. Cannot sign .changes.\n");
	    } else {
		system('debsign', '--re-sign', "-k$key_id", '--', "$build_dir/$changes");
	    }
	    if ($self->get_conf('SOURCE_ONLY_CHANGES')) {
		my $so_changes = $build_dir . '/' . $self->get('Package_SVersion') . "_source.changes";
		if (-r $so_changes) {
		    system('debsign', '--re-sign', "-k$key_id", '--', "$so_changes");
		} else {
		    $self->log_error("$so_changes unreadable. Cannot sign .changes.\n");
		}
	    }
	}
    }

    my $subject = "Log for " . $self->get_status() . " build of ";
    if ($self->get('Package_Version')) {
	$subject .= $self->get('Package_Version');
    } else {
	$subject .= $self->get('Package');
    }

    if ($self->get_conf('BUILD_SOURCE') && !$self->get_conf('BUILD_ARCH_ALL') && !$self->get_conf('BUILD_ARCH_ANY')) {
	$subject .= " source";
    }
    if ($self->get_conf('BUILD_ARCH_ALL') && !$self->get_conf('BUILD_ARCH_ANY')) {
	$subject .= " on all";
    } elsif ($self->get('Host Arch')) {
	$subject .= " on " . $self->get('Host Arch');
    }
    if ($self->get_conf('ARCHIVE')) {
	$subject .= " (" . $self->get_conf('ARCHIVE') . "/" . $self->get_conf('DISTRIBUTION') . ")";
    }
    else {
	    $subject .= " (dist=" . $self->get_conf('DISTRIBUTION') . ")";
    }

    open(STDERR, '>&', $saved_stderr) or warn "Can't redirect stderr\n"
	if defined($saved_stderr);
    open(STDOUT, '>&', $saved_stdout) or warn "Can't redirect stdout\n"
	if defined($saved_stdout);
    $saved_stderr->close();
    undef $saved_stderr;
    $saved_stdout->close();
    undef $saved_stdout;
    $self->set('Log File', undef);
    if (defined($self->get('Log Stream'))) {
	$self->get('Log Stream')->close(); # Close child logger process
	$self->set('Log Stream', undef);
    }

    $self->send_build_log($self->get_conf('MAILTO'), $subject, $filename)
	if (defined($filename) && -f $filename &&
	    $self->get_conf('MAILTO'));
}

sub send_build_log {
    my $self = shift;
    my $to = shift;
    my $subject = shift;
    my $filename = shift;

    my $conf = $self->get('Config');

    if ($conf->get('MIME_BUILD_LOG_MAILS')) {
	return $self->send_mime_build_log($to, $subject, $filename);
    } else {
        return send_mail($conf, $to, $subject, $filename);
    }
}

sub send_mime_build_log {
    my $self = shift;
    my $to = shift;
    my $subject = shift;
    my $filename = shift;

    my $conf = $self->get('Config');
    my $tmp; # Needed for gzip, here for proper scoping.

    my $msg = MIME::Lite->new(
	    From    => $conf->get('MAILFROM'),
	    To      => $to,
	    Subject => $subject,
	    Type    => 'multipart/mixed'
	    );

    # Add the GPG key ID to the mail if present so that it's clear if the log
    # still needs signing or not.
    if (defined($self->get_conf('KEY_ID')) && $self->get_conf('KEY_ID')) {
	$msg->add('Key-ID', $self->get_conf('KEY_ID'));
    }

    if (!$conf->get('COMPRESS_BUILD_LOG_MAILS')) {
	my $log_part = MIME::Lite->new(
		Type     => 'text/plain',
		Path     => $filename,
		Filename => basename($filename)
		);
	$log_part->attr('content-type.charset' => 'UTF-8');
	$msg->attach($log_part);
    } else {
	local( *F, *GZFILE );

	if (!open( F, "<$filename" )) {
	    warn "Cannot open $filename for mailing: $!\n";
	    return 0;
	}

	$tmp = File::Temp->new();
	tie *GZFILE, 'IO::Zlib', $tmp->filename, 'wb';

	while( <F> ) {
	    print GZFILE $_;
	}
	untie *GZFILE;

	close F;
	close GZFILE;

	$msg->attach(
		Type     => 'application/x-gzip',
		Path     => $tmp->filename,
		Filename => basename($filename) . '.gz'
		);
    }
    my $build_dir = $self->get_conf('BUILD_DIR');
    my $changes = $self->get_changes();
    if ($self->get_status() eq 'successful' && -r "$build_dir/$changes") {
	my $log_part = MIME::Lite->new(
		Type     => 'text/plain',
		Path     => "$build_dir/$changes",
		Filename => basename($changes)
		);
	$log_part->attr('content-type.charset' => 'UTF-8');
	$msg->attach($log_part);
    }

    my $stats = '';
    foreach my $stat (sort keys %{$self->get('Summary Stats')}) {
	$stats .= sprintf("%s: %s\n", $stat, $self->get('Summary Stats')->{$stat});
    }
    $msg->attach(
	Type => 'text/plain',
	Filename => basename($filename) . '.summary',
	Data => $stats
	);

    local $SIG{'PIPE'} = 'IGNORE';

    if (!open( MAIL, "|" . $conf->get('MAILPROG') . " -oem $to" )) {
	warn "Could not open pipe to " . $conf->get('MAILPROG') . ": $!\n";
	close( F );
	return 0;
    }

    $msg->print(\*MAIL);

    if (!close( MAIL )) {
	warn $conf->get('MAILPROG') . " failed (exit status $?)\n";
	return 0;
    }
    return 1;
}

sub log_symlink {
    my $self = shift;
    my $log = shift;
    my $dest = shift;

    unlink $dest; # Don't return on failure, since the symlink will fail.
    symlink $log, $dest;
}

1;