summaryrefslogtreecommitdiff
path: root/lib/fsm.c
blob: 5ebf28d4d7b03fa9e2b9803cc94574f17fb04355 (plain)
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
/** \ingroup payload
 * \file lib/fsm.c
 * File state machine to handle a payload from a package.
 */

#include "system.h"

#include <utime.h>
#include <errno.h>
#if WITH_CAP
#include <sys/capability.h>
#endif

#include <rpm/rpmte.h>
#include <rpm/rpmts.h>
#include <rpm/rpmsq.h>
#include <rpm/rpmlog.h>

#include "rpmio/rpmio_internal.h"	/* fdInit/FiniDigest */
#include "lib/cpio.h"
#include "lib/fsm.h"
#define	fsmUNSAFE	fsmStage
#include "lib/rpmfi_internal.h"	/* XXX fi->apath, ... */
#include "lib/rpmte_internal.h"	/* XXX rpmfs */
#include "lib/rpmts_internal.h"	/* rpmtsSELabelFoo() only */
#include "lib/rpmug.h"
#include "lib/cpio.h"

#include "debug.h"

#define	_FSM_DEBUG	0
int _fsm_debug = _FSM_DEBUG;

extern int _fsm_debug;

/** \ingroup payload
 */
enum cpioMapFlags_e {
    CPIO_MAP_PATH	= (1 << 0),
    CPIO_MAP_MODE	= (1 << 1),
    CPIO_MAP_UID	= (1 << 2),
    CPIO_MAP_GID	= (1 << 3),
    CPIO_FOLLOW_SYMLINKS= (1 << 4), /*!< only for building. */
    CPIO_MAP_ABSOLUTE	= (1 << 5),
    CPIO_MAP_ADDDOT	= (1 << 6),
    CPIO_MAP_TYPE	= (1 << 8),  /*!< only for building. */
    CPIO_SBIT_CHECK	= (1 << 9)
};
typedef rpmFlags cpioMapFlags;

typedef struct fsmIterator_s * FSMI_t;
typedef struct fsm_s * FSM_t;

typedef struct hardLink_s * hardLink_t;

typedef enum fileStage_e {
    FSM_PKGINSTALL,
    FSM_PKGERASE,
    FSM_PKGBUILD,
} fileStage;

/* XXX Failure to remove is not (yet) cause for failure. */
static int strict_erasures = 0;

/** \ingroup payload
 * Keeps track of the set of all hard links to a file in an archive.
 */
struct hardLink_s {
    hardLink_t next;
    const char ** nsuffix;
    int * filex;
    struct stat sb;
    nlink_t nlink;
    nlink_t linksLeft;
    int linkIndex;
    int createdPath;
};

/** \ingroup payload
 * Iterator across package file info, forward on install, backward on erase.
 */
struct fsmIterator_s {
    rpmfs fs;			/*!< file state info. */
    rpmfi fi;			/*!< transaction element file info. */
    int reverse;		/*!< reversed traversal? */
    int isave;			/*!< last returned iterator index. */
    int i;			/*!< iterator index. */
};

/** \ingroup payload
 * File name and stat information.
 */
struct fsm_s {
    char * path;		/*!< Current file name. */
    char * buf;			/*!<  read: Buffer. */
    size_t bufsize;		/*!<  read: Buffer allocated size. */
    FSMI_t iter;		/*!< File iterator. */
    int ix;			/*!< Current file iterator index. */
    hardLink_t links;		/*!< Pending hard linked file(s). */
    char ** failedFile;		/*!< First file name that failed. */
    const char * osuffix;	/*!< Old, preserved, file suffix. */
    const char * nsuffix;	/*!< New, created, file suffix. */
    char * suffix;		/*!< Current file suffix. */
    int postpone;		/*!< Skip remaining stages? */
    int diskchecked;		/*!< Has stat(2) been performed? */
    int exists;			/*!< Does current file exist on disk? */
    cpioMapFlags mapFlags;	/*!< Bit(s) to control mapping. */
    const char * dirName;	/*!< File directory name. */
    const char * baseName;	/*!< File base name. */
    struct selabel_handle *sehandle;	/*!< SELinux label handle (if any). */

    unsigned fflags;		/*!< File flags. */
    rpmFileAction action;	/*!< File disposition. */
    fileStage goal;		/*!< Package state machine goal. */
    struct stat sb;		/*!< Current file stat(2) info. */
    struct stat osb;		/*!< Original file stat(2) info. */
};


/**
 * Retrieve transaction element file info from file state machine iterator.
 * @param fsm		file state machine
 * @return		transaction element file info
 */
static rpmfi fsmGetFi(const FSM_t fsm)
{
    const FSMI_t iter = fsm->iter;
    return (iter ? iter->fi : NULL);
}

static rpmfs fsmGetFs(const FSM_t fsm)
{
    const FSMI_t iter = fsm->iter;
    return (iter ? iter->fs : NULL);
}

#define	SUFFIX_RPMORIG	".rpmorig"
#define	SUFFIX_RPMSAVE	".rpmsave"
#define	SUFFIX_RPMNEW	".rpmnew"

/* Default directory and file permissions if not mapped */
#define _dirPerms 0755
#define _filePerms 0644

/* 
 * XXX Forward declarations for previously exported functions to avoid moving 
 * things around needlessly 
 */ 
static const char * fileActionString(rpmFileAction a);

/** \ingroup payload
 * Build path to file from file info, optionally ornamented with suffix.
 * @param fsm		file state machine data
 * @param isDir		directory or regular path?
 * @param suffix	suffix to use (NULL disables)
 * @retval		path to file (malloced)
 */
static char * fsmFsPath(const FSM_t fsm, int isDir,
			const char * suffix)
{
    return rstrscat(NULL,
		    fsm->dirName, fsm->baseName,
		    (!isDir && suffix) ? suffix : "",
		    NULL);
}

/** \ingroup payload
 * Destroy file info iterator.
 * @param p		file info iterator
 * @retval		NULL always
 */
static FSMI_t mapFreeIterator(FSMI_t iter)
{
    if (iter) {
	iter->fs = NULL; /* rpmfs is not refcounted */
	iter->fi = rpmfiFree(iter->fi);
	free(iter);
    }
    return NULL;
}

/** \ingroup payload
 * Create file info iterator.
 * @param fi		transaction element file info
 * @return		file info iterator
 */
static FSMI_t 
mapInitIterator(rpmfs fs, rpmfi fi, int reverse)
{
    FSMI_t iter = NULL;

    iter = xcalloc(1, sizeof(*iter));
    iter->fs = fs; /* rpmfs is not refcounted */
    iter->fi = rpmfiLink(fi);
    iter->reverse = reverse;
    iter->i = (iter->reverse ? (rpmfiFC(fi) - 1) : 0);
    iter->isave = iter->i;
    return iter;
}

/** \ingroup payload
 * Return next index into file info.
 * @param a		file info iterator
 * @return		next index, -1 on termination
 */
static int mapNextIterator(FSMI_t iter)
{
    int i = -1;

    if (iter) {
	const rpmfi fi = iter->fi;
	if (iter->reverse) {
	    if (iter->i >= 0)	i = iter->i--;
	} else {
    	    if (iter->i < rpmfiFC(fi))	i = iter->i++;
	}
	iter->isave = i;
    }
    return i;
}

/** \ingroup payload
 */
static int cpioStrCmp(const void * a, const void * b)
{
    const char * afn = *(const char **)a;
    const char * bfn = *(const char **)b;

    /* Match rpm-4.0 payloads with ./ prefixes. */
    if (afn[0] == '.' && afn[1] == '/')	afn += 2;
    if (bfn[0] == '.' && bfn[1] == '/')	bfn += 2;

    /* If either path is absolute, make it relative. */
    if (afn[0] == '/')	afn += 1;
    if (bfn[0] == '/')	bfn += 1;

    return strcmp(afn, bfn);
}

/** \ingroup payload
 * Locate archive path in file info.
 * @param iter		file info iterator
 * @param fsmPath	archive path
 * @return		index into file info, -1 if archive path was not found
 */
static int mapFind(FSMI_t iter, const char * fsmPath)
{
    int ix = -1;

    if (iter) {
	const rpmfi fi = iter->fi;
	int fc = rpmfiFC(fi);
	if (fi && fc > 0 && fi->apath && fsmPath && *fsmPath) {
	    char ** p = NULL;

	    if (fi->apath != NULL)
		p = bsearch(&fsmPath, fi->apath, fc, sizeof(fsmPath),
			cpioStrCmp);
	    if (p) {
		iter->i = p - fi->apath;
		ix = mapNextIterator(iter);
	    }
	}
    }
    return ix;
}

/** \ingroup payload
 * Directory name iterator.
 */
typedef struct dnli_s {
    rpmfi fi;
    char * active;
    int reverse;
    int isave;
    int i;
} * DNLI_t;

/** \ingroup payload
 * Destroy directory name iterator.
 * @param a		directory name iterator
 * @retval		NULL always
 */
static DNLI_t dnlFreeIterator(DNLI_t dnli)
{
    if (dnli) {
	if (dnli->active) free(dnli->active);
	free(dnli);
    }
    return NULL;
}

/** \ingroup payload
 * Create directory name iterator.
 * @param fi		file info set
 * @param fs		file state set
 * @param reverse	traverse directory names in reverse order?
 * @return		directory name iterator
 */
static DNLI_t dnlInitIterator(rpmfi fi, rpmfs fs, int reverse)
{
    DNLI_t dnli;
    int i, j;
    int dc;

    if (fi == NULL)
	return NULL;
    dc = rpmfiDC(fi);
    dnli = xcalloc(1, sizeof(*dnli));
    dnli->fi = fi;
    dnli->reverse = reverse;
    dnli->i = (reverse ? dc : 0);

    if (dc) {
	dnli->active = xcalloc(dc, sizeof(*dnli->active));
	int fc = rpmfiFC(fi);

	/* Identify parent directories not skipped. */
	for (i = 0; i < fc; i++)
            if (!XFA_SKIPPING(rpmfsGetAction(fs, i)))
		dnli->active[rpmfiDIIndex(fi, i)] = 1;

	/* Exclude parent directories that are explicitly included. */
	for (i = 0; i < fc; i++) {
	    int dil;
	    size_t dnlen, bnlen;

	    if (!S_ISDIR(rpmfiFModeIndex(fi, i)))
		continue;

	    dil = rpmfiDIIndex(fi, i);
	    dnlen = strlen(rpmfiDNIndex(fi, dil));
	    bnlen = strlen(rpmfiBNIndex(fi, i));

	    for (j = 0; j < dc; j++) {
		const char * dnl;
		size_t jlen;

		if (!dnli->active[j] || j == dil)
		    continue;
		dnl = rpmfiDNIndex(fi, j);
		jlen = strlen(dnl);
		if (jlen != (dnlen+bnlen+1))
		    continue;
		if (!rstreqn(dnl, rpmfiDNIndex(fi, dil), dnlen))
		    continue;
		if (!rstreqn(dnl+dnlen, rpmfiBNIndex(fi, i), bnlen))
		    continue;
		if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
		    continue;
		/* This directory is included in the package. */
		dnli->active[j] = 0;
		break;
	    }
	}

	/* Print only once per package. */
	if (!reverse) {
	    j = 0;
	    for (i = 0; i < dc; i++) {
		if (!dnli->active[i]) continue;
		if (j == 0) {
		    j = 1;
		    rpmlog(RPMLOG_DEBUG,
	"========== Directories not explicitly included in package:\n");
		}
		rpmlog(RPMLOG_DEBUG, "%10d %s\n", i, rpmfiDNIndex(fi, i));
	    }
	    if (j)
		rpmlog(RPMLOG_DEBUG, "==========\n");
	}
    }
    return dnli;
}

/** \ingroup payload
 * Return next directory name (from file info).
 * @param dnli		directory name iterator
 * @return		next directory name
 */
static
const char * dnlNextIterator(DNLI_t dnli)
{
    const char * dn = NULL;

    if (dnli) {
	rpmfi fi = dnli->fi;
	int dc = rpmfiDC(fi);
	int i = -1;

	if (dnli->active)
	do {
	    i = (!dnli->reverse ? dnli->i++ : --dnli->i);
	} while (i >= 0 && i < dc && !dnli->active[i]);

	if (i >= 0 && i < dc)
	    dn = rpmfiDNIndex(fi, i);
	else
	    i = -1;
	dnli->isave = i;
    }
    return dn;
}

/**
 * Map next file path and action.
 * @param fsm		file state machine
 * @param i		file index
 */
static int fsmMapPath(FSM_t fsm, int i)
{
    rpmfi fi = fsmGetFi(fsm);	/* XXX const except for fstates */
    int rc = 0;

    fsm->osuffix = NULL;
    fsm->nsuffix = NULL;
    fsm->action = FA_UNKNOWN;

    if (fi && i >= 0 && i < rpmfiFC(fi)) {
	rpmfs fs = fsmGetFs(fsm);
	/* XXX these should use rpmfiFFlags() etc */
	fsm->action = rpmfsGetAction(fs, i);
	fsm->fflags = rpmfiFFlagsIndex(fi, i);

	/* src rpms have simple base name in payload. */
	fsm->dirName = rpmfiDNIndex(fi, rpmfiDIIndex(fi, i));
	fsm->baseName = rpmfiBNIndex(fi, i);

	/* Never create backup for %ghost files. */
	if (fsm->goal != FSM_PKGBUILD && !(fsm->fflags & RPMFILE_GHOST)) {
	    switch (fsm->action) {
	    case FA_ALTNAME:
		fsm->nsuffix = SUFFIX_RPMNEW;
		break;
	    case FA_SAVE:
		fsm->osuffix = SUFFIX_RPMSAVE;
		break;
	    case FA_BACKUP:
		fsm->osuffix = (fsm->goal == FSM_PKGINSTALL) ?
				SUFFIX_RPMORIG : SUFFIX_RPMSAVE;
		break;
	    default:
		break;
	    }
	}

	if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
	    fsm->path = _free(fsm->path);
	    fsm->path = fsmFsPath(fsm, S_ISDIR(fsm->sb.st_mode),
		(fsm->suffix ? fsm->suffix : fsm->nsuffix));
	}
    }
    return rc;
}

/** \ingroup payload
 * Save hard link in chain.
 * @param fsm		file state machine data
 * @retval linkSet	hard link set when complete
 * @return		Is chain only partially filled?
 */
static int saveHardLink(FSM_t fsm, hardLink_t * linkSet)
{
    struct stat * st = &fsm->sb;
    int rc = 0;
    int ix = -1;
    int j;
    hardLink_t *tailp, li;

    /* Find hard link set. */
    for (tailp = &fsm->links; (li = *tailp) != NULL; tailp = &li->next) {
	if (li->sb.st_ino == st->st_ino && li->sb.st_dev == st->st_dev)
	    break;
    }

    /* New hard link encountered, add new link to set. */
    if (li == NULL) {
	li = xcalloc(1, sizeof(*li));
	li->next = NULL;
	li->sb = *st;	/* structure assignment */
	li->nlink = st->st_nlink;
	li->linkIndex = fsm->ix;
	li->createdPath = -1;

	li->filex = xcalloc(st->st_nlink, sizeof(li->filex[0]));
	memset(li->filex, -1, (st->st_nlink * sizeof(li->filex[0])));
	li->nsuffix = xcalloc(st->st_nlink, sizeof(*li->nsuffix));

	if (fsm->goal == FSM_PKGBUILD)
	    li->linksLeft = st->st_nlink;
	if (fsm->goal == FSM_PKGINSTALL)
	    li->linksLeft = 0;

	*tailp = li;	/* append to tail of linked list */
    }

    if (fsm->goal == FSM_PKGBUILD) --li->linksLeft;
    li->filex[li->linksLeft] = fsm->ix;
    li->nsuffix[li->linksLeft] = fsm->nsuffix;
    if (fsm->goal == FSM_PKGINSTALL) li->linksLeft++;

    if (fsm->goal == FSM_PKGBUILD)
	return (li->linksLeft > 0);

    if (fsm->goal != FSM_PKGINSTALL)
	return 0;

    if (!(st->st_size || li->linksLeft == st->st_nlink))
	return 1;

    /* Here come the bits, time to choose a non-skipped file name. */
    {	rpmfs fs = fsmGetFs(fsm);

	for (j = li->linksLeft - 1; j >= 0; j--) {
	    ix = li->filex[j];
	    if (ix < 0 || XFA_SKIPPING(rpmfsGetAction(fs, ix)))
		continue;
	    break;
	}
    }

    /* Are all links skipped or not encountered yet? */
    if (ix < 0 || j < 0)
	return 1;	/* XXX W2DO? */

    /* Save the non-skipped file name and map index. */
    li->linkIndex = j;
    if (linkSet)
	*linkSet = li;
    fsm->path = _free(fsm->path);
    fsm->ix = ix;
    rc = fsmMapPath(fsm, fsm->ix);
    return rc;
}

/** \ingroup payload
 * Destroy set of hard links.
 * @param li		set of hard links
 * @return		NULL always
 */
static hardLink_t freeHardLink(hardLink_t li)
{
    if (li) {
	li->nsuffix = _free(li->nsuffix);	/* XXX elements are shared */
	li->filex = _free(li->filex);
	_free(li);
    }
    return NULL;
}

/* Check for hard links missing from payload */
static int checkHardLinks(FSM_t fsm)
{
    int rc = 0;
    rpmfs fs = fsmGetFs(fsm);

    for (hardLink_t li = fsm->links; li != NULL; li = li->next) {
	if (li->linksLeft) {
	    for (nlink_t i = 0 ; i < li->linksLeft; i++) {
		int ix = li->filex[i];
		if (ix < 0 || XFA_SKIPPING(rpmfsGetAction(fs, ix)))
		    continue;
		rc = CPIOERR_MISSING_HARDLINK;
		if (fsm->failedFile && *fsm->failedFile == NULL) {
		    if (!fsmMapPath(fsm, ix)) {
			/* Out-of-sync hardlinks handled as sub-state */
			*fsm->failedFile = fsm->path;
			fsm->path = NULL;
		    }
		}
		break;
	    }
	}
    }
    return rc;
}

static FSM_t fsmNew(fileStage goal, rpmfs fs, rpmfi fi, char ** failedFile)
{
    FSM_t fsm = xcalloc(1, sizeof(*fsm));

    fsm->ix = -1;
    fsm->goal = goal;
    fsm->iter = mapInitIterator(fs, fi, (goal == FSM_PKGERASE));

    /* common flags for all modes */
    fsm->mapFlags = CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;

    if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
        fsm->bufsize = 8 * BUFSIZ;
        fsm->buf = xmalloc(fsm->bufsize);
    }

    fsm->failedFile = failedFile;
    if (fsm->failedFile)
	*fsm->failedFile = NULL;

    return fsm;
}

static FSM_t fsmFree(FSM_t fsm)
{
    hardLink_t li;
    fsm->buf = _free(fsm->buf);
    fsm->bufsize = 0;

    fsm->iter = mapFreeIterator(fsm->iter);
    fsm->failedFile = NULL;

    fsm->path = _free(fsm->path);
    fsm->suffix = _free(fsm->suffix);

    while ((li = fsm->links) != NULL) {
	fsm->links = li->next;
	li->next = NULL;
	freeHardLink(li);
    }
    free(fsm);
    return NULL;
}

/* Find and set file security context */
static int fsmSetSELabel(struct selabel_handle *sehandle,
			 const char *path, mode_t mode)
{
    int rc = 0;
#if WITH_SELINUX
    if (sehandle) {
	security_context_t scon = NULL;

	if (selabel_lookup_raw(sehandle, &scon, path, mode) == 0) {
	    rc = lsetfilecon(path, scon);

	    if (_fsm_debug) {
		rpmlog(RPMLOG_DEBUG, " %8s (%s, %s) %s\n",
			__func__, path, scon,
			(rc < 0 ? strerror(errno) : ""));
	    }

	    if (rc < 0 && errno == EOPNOTSUPP)
		rc = 0;
	}

	freecon(scon);
    }
#endif
    return rc ? CPIOERR_LSETFCON_FAILED : 0;
}

static int fsmSetFCaps(const char *path, const char *captxt)
{
    int rc = 0;
#if WITH_CAP
    if (captxt && *captxt != '\0') {
	cap_t fcaps = cap_from_text(captxt);
	if (fcaps == NULL || cap_set_file(path, fcaps) != 0) {
	    rc = CPIOERR_SETCAP_FAILED;
	}
	cap_free(fcaps);
    } 
#endif
    return rc;
}

/**
 * Map file stat(2) info.
 * @param fsm		file state machine
 */
static int fsmMapAttrs(FSM_t fsm)
{
    struct stat * st = &fsm->sb;
    rpmfi fi = fsmGetFi(fsm);
    int i = fsm->ix;

    /* this check is pretty moot,  rpmfi accessors check array bounds etc */
    if (fi && i >= 0 && i < rpmfiFC(fi)) {
	ino_t finalInode = rpmfiFInodeIndex(fi, i);
	mode_t finalMode = rpmfiFModeIndex(fi, i);
	dev_t finalRdev = rpmfiFRdevIndex(fi, i);
	time_t finalMtime = rpmfiFMtimeIndex(fi, i);
	const char *user = rpmfiFUserIndex(fi, i);
	const char *group = rpmfiFGroupIndex(fi, i);
	uid_t uid = 0;
	gid_t gid = 0;

	if (user && rpmugUid(user, &uid)) {
	    if (fsm->goal == FSM_PKGINSTALL)
		rpmlog(RPMLOG_WARNING,
		    _("user %s does not exist - using root\n"), user);
	    finalMode &= ~S_ISUID;      /* turn off suid bit */
	}

	if (group && rpmugGid(group, &gid)) {
	    if (fsm->goal == FSM_PKGINSTALL)
		rpmlog(RPMLOG_WARNING,
		    _("group %s does not exist - using root\n"), group);
	    finalMode &= ~S_ISGID;	/* turn off sgid bit */
	}

	if (fsm->mapFlags & CPIO_MAP_MODE)
	    st->st_mode = (st->st_mode & S_IFMT) | (finalMode & ~S_IFMT);
	if (fsm->mapFlags & CPIO_MAP_TYPE) {
	    st->st_mode = (st->st_mode & ~S_IFMT) | (finalMode & S_IFMT);
	    if ((S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
	    && st->st_nlink == 0)
		st->st_nlink = 1;
	    st->st_ino = finalInode;
	    st->st_rdev = finalRdev;
	    st->st_mtime = finalMtime;
	}
	if (fsm->mapFlags & CPIO_MAP_UID)
	    st->st_uid = uid;
	if (fsm->mapFlags & CPIO_MAP_GID)
	    st->st_gid = gid;
    }
    return 0;
}

/** \ingroup payload
 * Create file from payload stream.
 * @param fsm		file state machine data
 * @param archive	payload archive
 * @return		0 on success
 */
static int expandRegular(FSM_t fsm, rpmpsm psm, rpmcpio_t archive, int nodigest)
{
    FD_t wfd = NULL;
    const struct stat * st = &fsm->sb;
    rpm_loff_t left = st->st_size;
    const unsigned char * fidigest = NULL;
    pgpHashAlgo digestalgo = 0;
    int rc = 0;

    wfd = Fopen(fsm->path, "w.ufdio");
    if (Ferror(wfd)) {
	rc = CPIOERR_OPEN_FAILED;
	goto exit;
    }

    if (!nodigest) {
	rpmfi fi = fsmGetFi(fsm);
	digestalgo = rpmfiDigestAlgo(fi);
	fidigest = rpmfiFDigestIndex(fi, fsm->ix, NULL, NULL);
    }

    if (st->st_size > 0 && fidigest)
	fdInitDigest(wfd, digestalgo, 0);

    while (left) {
        size_t len;
	len = (left > fsm->bufsize ? fsm->bufsize : left);
        if (rpmcpioRead(archive, fsm->buf, len) != len) {
            rc = CPIOERR_READ_FAILED;
	    goto exit;
        }
	if ((Fwrite(fsm->buf, sizeof(*fsm->buf), len, wfd) != len) || Ferror(wfd)) {
	    rc = CPIOERR_WRITE_FAILED;
	    goto exit;
	}

	left -= len;

	/* don't call this with fileSize == fileComplete */
	if (!rc && left)
	    rpmpsmNotify(psm, RPMCALLBACK_INST_PROGRESS, rpmcpioTell(archive));
    }

    if (st->st_size > 0 && fidigest) {
	void * digest = NULL;

	(void) Fflush(wfd);
	fdFiniDigest(wfd, digestalgo, &digest, NULL, 0);

	if (digest != NULL && fidigest != NULL) {
	    size_t diglen = rpmDigestLength(digestalgo);
	    if (memcmp(digest, fidigest, diglen)) {
		rc = CPIOERR_DIGEST_MISMATCH;
            }
	} else {
	    rc = CPIOERR_DIGEST_MISMATCH;
	}
	free(digest);
    }

exit:
    if (wfd) {
	int myerrno = errno;
	Fclose(wfd);
	errno = myerrno;
    }
    return rc;
}

static int fsmReadLink(const char *path,
		       char *buf, size_t bufsize, size_t *linklen)
{
    ssize_t llen = readlink(path, buf, bufsize - 1);
    int rc = CPIOERR_READLINK_FAILED;

    if (_fsm_debug) {
        rpmlog(RPMLOG_DEBUG, " %8s (%s, buf, %d) %s\n",
	       __func__,
               path, (int)(bufsize -1), (llen < 0 ? strerror(errno) : ""));
    }

    if (llen >= 0) {
	buf[llen] = '\0';
	rc = 0;
	*linklen = llen;
    }
    return rc;
}

/** \ingroup payload
 * Write next item to payload stream.
 * @param fsm		file state machine data
 * @param writeData	should data be written?
 * @param archive	payload archive
 * @param ix		file index
 * @return		0 on success
 */
static int writeFile(FSM_t fsm, int writeData, rpmcpio_t archive, int ix)
{
    FD_t rfd = NULL;
    char * path = fsm->path;
    struct stat * st = &fsm->sb;
    struct stat * ost = &fsm->osb;
    char * symbuf = NULL;
    rpm_loff_t left;
    int rc = 0;

    st->st_size = (writeData ? ost->st_size : 0);

    if (S_ISDIR(st->st_mode)) {
	st->st_size = 0;
    } else if (S_ISLNK(st->st_mode)) {
	/*
	 * While linux puts the size of a symlink in the st_size field,
	 * I don't think that's a specified standard.
	 */
        size_t linklen;
	rc = fsmReadLink(fsm->path, fsm->buf, fsm->bufsize, &linklen);
	if (rc) goto exit;
	st->st_size = linklen;
	rstrcat(&symbuf, fsm->buf);	/* XXX save readlink return. */
    }

    if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
	fsm->path = rstrscat(NULL, (fsm->mapFlags & CPIO_MAP_ADDDOT) ? "." : "",
				   fsm->dirName, fsm->baseName, NULL);
    } else if (fsm->mapFlags & CPIO_MAP_PATH) {
	rpmfi fi = fsmGetFi(fsm);
	fsm->path = xstrdup((fi->apath ? fi->apath[ix] : 
					 rpmfiBNIndex(fi, ix)));
    }

    rc = rpmcpioHeaderWrite(archive, fsm->path, st);
    _free(fsm->path);
    fsm->path = path;

    if (rc) goto exit;


    if (writeData && S_ISREG(st->st_mode)) {
	size_t len;

	rfd = Fopen(fsm->path, "r.ufdio");
	if (Ferror(rfd)) {
	    rc = CPIOERR_OPEN_FAILED;
	    goto exit;
	}
	
	left = st->st_size;

	while (left) {
	    len = (left > fsm->bufsize ? fsm->bufsize : left);
	    if (Fread(fsm->buf, sizeof(*fsm->buf), len, rfd) != len || Ferror(rfd)) {
		rc = CPIOERR_READ_FAILED;
		goto exit;
	    }

	    if (rpmcpioWrite(archive, fsm->buf, len) != len) {
		rc = CPIOERR_WRITE_FAILED;
		goto exit;
	    }
	    left -= len;
	}
    } else if (writeData && S_ISLNK(st->st_mode)) {
        size_t len = strlen(symbuf);
        if (rpmcpioWrite(archive, symbuf, len) != len) {
            rc = CPIOERR_WRITE_FAILED;
            goto exit;
        }
    }

exit:
    if (rfd) {
	/* preserve any prior errno across close */
	int myerrno = errno;
	Fclose(rfd);
	errno = myerrno;
    }
    fsm->path = path;
    free(symbuf);
    return rc;
}

/** \ingroup payload
 * Write set of linked files to payload stream.
 * @param fsm		file state machine data
 * @param archive	payload archive
 * @param li		link to write
 * @return		0 on success
 */
static int writeLinkedFile(FSM_t fsm, rpmcpio_t archive, hardLink_t li)
{
    char * path = fsm->path;
    const char * nsuffix = fsm->nsuffix;
    int ec = 0;
    int rc;
    int i;

    fsm->path = NULL;
    fsm->nsuffix = NULL;

    for (i = li->nlink - 1; i >= 0; i--) {

	if (li->filex[i] < 0) continue;

	rc = fsmMapPath(fsm, li->filex[i]);

	/* Write data after last link. */
	rc = writeFile(fsm, (i == 0), archive, li->filex[i]);
	if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
	    ec = rc;
	    *fsm->failedFile = xstrdup(fsm->path);
	}

	fsm->path = _free(fsm->path);
	li->filex[i] = -1;
    }

    fsm->nsuffix = nsuffix;
    fsm->path = path;
    return ec;
}

static int writeLinks(FSM_t fsm, rpmcpio_t archive)
{
    int j, rc = 0;
    nlink_t i, nlink;

    for (hardLink_t li = fsm->links; li; li = li->next) {
	/* Re-calculate link count for archive header. */
	for (j = -1, nlink = 0, i = 0; i < li->nlink; i++) {
	    if (li->filex[i] < 0)
		continue;
	    nlink++;
	    if (j == -1) j = i;
	}
	/* XXX force the contents out as well. */
	if (j != 0) {
	    li->filex[0] = li->filex[j];
	    li->filex[j] = -1;
	}
	li->sb.st_nlink = nlink;

	fsm->sb = li->sb;	/* structure assignment */
	fsm->osb = fsm->sb;	/* structure assignment */

	if (!rc) rc = writeLinkedFile(fsm, archive, li);
    }
    return rc;
}

static int fsmStat(const char *path, int dolstat, struct stat *sb)
{
    int rc;
    if (dolstat){
	rc = lstat(path, sb);
    } else {
        rc = stat(path, sb);
    }
    if (_fsm_debug && rc && errno != ENOENT)
        rpmlog(RPMLOG_DEBUG, " %8s (%s, ost) %s\n",
               __func__,
               path, (rc < 0 ? strerror(errno) : ""));
    if (rc < 0) {
        rc = (errno == ENOENT ? CPIOERR_ENOENT : CPIOERR_LSTAT_FAILED);
	/* WTH is this, and is it really needed, still? */
        memset(sb, 0, sizeof(*sb));	/* XXX s390x hackery */
    }
    return rc;
}

static int fsmVerify(FSM_t fsm);

/** \ingroup payload
 * Create pending hard links to existing file.
 * @param fsm		file state machine data
 * @param li		hard link
 * @return		0 on success
 */
static int fsmMakeLinks(FSM_t fsm, hardLink_t li)
{
    char * path = fsm->path;
    char * opath = NULL;
    const char * nsuffix = fsm->nsuffix;
    int ec = 0;
    int rc;
    int i;

    fsm->path = NULL;
    fsm->nsuffix = NULL;

    rc = fsmMapPath(fsm, li->filex[li->createdPath]);
    opath = fsm->path;
    fsm->path = NULL;
    for (i = 0; i < li->nlink; i++) {
	if (li->filex[i] < 0) continue;
	if (li->createdPath == i) continue;

	fsm->path = _free(fsm->path);
	rc = fsmMapPath(fsm, li->filex[i]);
	if (XFA_SKIPPING(fsm->action)) continue;

	rc = fsmVerify(fsm);
	if (!rc) continue;
	if (!(rc == CPIOERR_ENOENT)) break;

	/* XXX link(opath, fsm->path) */
	rc = link(opath, fsm->path);
	if (_fsm_debug)
	    rpmlog(RPMLOG_DEBUG, " %8s (%s, %s) %s\n", __func__,
		opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
	if (rc < 0)	rc = CPIOERR_LINK_FAILED;

	if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
	    ec = rc;
	    *fsm->failedFile = xstrdup(fsm->path);
	}

	li->linksLeft--;
    }
    fsm->path = _free(fsm->path);
    free(opath);

    fsm->nsuffix = nsuffix;
    fsm->path = path;
    return ec;
}

static int fsmCommit(FSM_t fsm, int ix);

/** \ingroup payload
 * Commit hard linked file set atomically.
 * @param fsm		file state machine data
 * @return		0 on success
 */
static int fsmCommitLinks(FSM_t fsm)
{
    char * path = fsm->path;
    const char * nsuffix = fsm->nsuffix;
    struct stat * st = &fsm->sb;
    int rc = 0;
    nlink_t i;
    hardLink_t li;

    fsm->path = NULL;
    fsm->nsuffix = NULL;

    for (li = fsm->links; li != NULL; li = li->next) {
	if (li->sb.st_ino == st->st_ino && li->sb.st_dev == st->st_dev)
	    break;
    }

    for (i = 0; i < li->nlink; i++) {
	if (li->filex[i] < 0) continue;
	rc = fsmMapPath(fsm, li->filex[i]);
	if (!XFA_SKIPPING(fsm->action))
	    rc = fsmCommit(fsm, li->filex[i]);
	fsm->path = _free(fsm->path);
	li->filex[i] = -1;
    }

    fsm->nsuffix = nsuffix;
    fsm->path = path;
    return rc;
}

static int fsmRmdir(const char *path)
{
    int rc = rmdir(path);
    if (_fsm_debug)
	rpmlog(RPMLOG_DEBUG, " %8s (%s) %s\n", __func__,
	       path, (rc < 0 ? strerror(errno) : ""));
    if (rc < 0)
	switch (errno) {
	case ENOENT:        rc = CPIOERR_ENOENT;    break;
	case ENOTEMPTY:     rc = CPIOERR_ENOTEMPTY; break;
	default:            rc = CPIOERR_RMDIR_FAILED; break;
	}
    return rc;
}

static int fsmMkdir(const char *path, mode_t mode)
{
    int rc = mkdir(path, (mode & 07777));
    if (_fsm_debug)
	rpmlog(RPMLOG_DEBUG, " %8s (%s, 0%04o) %s\n", __func__,
	       path, (unsigned)(mode & 07777),
	       (rc < 0 ? strerror(errno) : ""));
    if (rc < 0)	rc = CPIOERR_MKDIR_FAILED;
    return rc;
}

static int fsmMkfifo(const char *path, mode_t mode)
{
    int rc = mkfifo(path, (mode & 07777));

    if (_fsm_debug) {
	rpmlog(RPMLOG_DEBUG, " %8s (%s, 0%04o) %s\n",
	       __func__, path, (unsigned)(mode & 07777),
	       (rc < 0 ? strerror(errno) : ""));
    }

    if (rc < 0)
	rc = CPIOERR_MKFIFO_FAILED;

    return rc;
}

static int fsmMknod(const char *path, mode_t mode, dev_t dev)
{
    /* FIX: check S_IFIFO or dev != 0 */
    int rc = mknod(path, (mode & ~07777), dev);

    if (_fsm_debug) {
	rpmlog(RPMLOG_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n",
	       __func__, path, (unsigned)(mode & ~07777),
	       (unsigned)dev, (rc < 0 ? strerror(errno) : ""));
    }

    if (rc < 0)
	rc = CPIOERR_MKNOD_FAILED;

    return rc;
}

/**
 * Create (if necessary) directories not explicitly included in package.
 * @param dnli		file state machine data
 * @param sehandle	selinux label handle (bah)
 * @return		0 on success
 */
static int fsmMkdirs(rpmfi fi, rpmfs fs, struct selabel_handle *sehandle)
{
    DNLI_t dnli = dnlInitIterator(fi, fs, 0);
    struct stat sb;
    const char *dpath;
    int dc = rpmfiDC(fi);
    int rc = 0;
    int i;
    int ldnlen = 0;
    int ldnalloc = 0;
    char * ldn = NULL;
    short * dnlx = NULL; 

    dnlx = (dc ? xcalloc(dc, sizeof(*dnlx)) : NULL);

    if (dnlx != NULL)
    while ((dpath = dnlNextIterator(dnli)) != NULL) {
	size_t dnlen = strlen(dpath);
	char * te, dn[dnlen+1];

	dc = dnli->isave;
	if (dc < 0) continue;
	dnlx[dc] = dnlen;
	if (dnlen <= 1)
	    continue;

	if (dnlen <= ldnlen && rstreq(dpath, ldn))
	    continue;

	/* Copy as we need to modify the string */
	(void) stpcpy(dn, dpath);

	/* Assume '/' directory exists, "mkdir -p" for others if non-existent */
	for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
	    if (*te != '/')
		continue;

	    *te = '\0';

	    /* Already validated? */
	    if (i < ldnlen &&
		(ldn[i] == '/' || ldn[i] == '\0') && rstreqn(dn, ldn, i))
	    {
		*te = '/';
		/* Move pre-existing path marker forward. */
		dnlx[dc] = (te - dn);
		continue;
	    }

	    /* Validate next component of path. */
	    rc = fsmStat(dn, 1, &sb); /* lstat */
	    *te = '/';

	    /* Directory already exists? */
	    if (rc == 0 && S_ISDIR(sb.st_mode)) {
		/* Move pre-existing path marker forward. */
		dnlx[dc] = (te - dn);
	    } else if (rc == CPIOERR_ENOENT) {
		*te = '\0';
		mode_t mode = S_IFDIR | (_dirPerms & 07777);
		rc = fsmMkdir(dn, mode);
		if (!rc) {
		    rc = fsmSetSELabel(sehandle, dn, mode);

		    rpmlog(RPMLOG_DEBUG,
			    "%s directory created with perms %04o\n",
			    dn, (unsigned)(mode & 07777));
		}
		*te = '/';
	    }
	    if (rc)
		break;
	}
	if (rc) break;

	/* Save last validated path. */
	if (ldnalloc < (dnlen + 1)) {
	    ldnalloc = dnlen + 100;
	    ldn = xrealloc(ldn, ldnalloc);
	}
	if (ldn != NULL) { /* XXX can't happen */
	    strcpy(ldn, dn);
	    ldnlen = dnlen;
	}
    }
    free(dnlx);
    free(ldn);
    dnlFreeIterator(dnli);

    return rc;
}

static void removeSBITS(const char *path)
{
    struct stat stb;
    if (lstat(path, &stb) == 0 && S_ISREG(stb.st_mode)) {
	if ((stb.st_mode & 06000) != 0) {
	    (void) chmod(path, stb.st_mode & 0777);
	}
#if WITH_CAP
	if (stb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) {
	    (void) cap_set_file(path, NULL);
	}
#endif
    }
}

/********************************************************************/

static void fsmReset(FSM_t fsm)
{
    fsm->path = _free(fsm->path);
    fsm->postpone = 0;
    fsm->diskchecked = fsm->exists = 0;
    fsm->action = FA_UNKNOWN;
    fsm->osuffix = NULL;
    fsm->nsuffix = NULL;
    memset(&(fsm->sb), 0, sizeof(fsm->sb));
    memset(&(fsm->osb), 0, sizeof(fsm->sb));
}

static int fsmInit(FSM_t fsm)
{
    int rc = 0;

    /* On non-install, mode must be known so that dirs don't get suffix. */
    if (fsm->goal != FSM_PKGINSTALL) {
	rpmfi fi = fsmGetFi(fsm);
	fsm->sb.st_mode = rpmfiFModeIndex(fi, fsm->ix);
    }

    /* Generate file path. */
    rc = fsmMapPath(fsm, fsm->ix);
    if (rc) return rc;

    /* Perform lstat/stat for disk file. */
    if (fsm->path != NULL &&
	!(fsm->goal == FSM_PKGINSTALL && S_ISREG(fsm->sb.st_mode)))
    {
	int dolstat = !(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS);
	rc = fsmStat(fsm->path, dolstat, &fsm->osb);
	if (rc == CPIOERR_ENOENT) {
	    // errno = saveerrno; XXX temporary commented out
	    rc = 0;
	    fsm->exists = 0;
	} else if (rc == 0) {
	    fsm->exists = 1;
	}
    } else {
	/* Skip %ghost files on build. */
	fsm->exists = 0;
    }
    fsm->diskchecked = 1;
    if (rc) return rc;

    /* On non-install, the disk file stat is what's remapped. */
    if (fsm->goal != FSM_PKGINSTALL)
	fsm->sb = fsm->osb;			/* structure assignment */

    /* Remap file perms, owner, and group. */
    rc = fsmMapAttrs(fsm);
    if (rc) return rc;

    fsm->postpone = XFA_SKIPPING(fsm->action);

    rpmlog(RPMLOG_DEBUG, "%-10s %06o%3d (%4d,%4d)%6d %s\n",
	   fileActionString(fsm->action), (int)fsm->sb.st_mode,
	   (int)fsm->sb.st_nlink, (int)fsm->sb.st_uid,
	   (int)fsm->sb.st_gid, (int)fsm->sb.st_size,
	    (fsm->path ? fsm->path : ""));

    return rc;

}

static int fsmSymlink(const char *opath, const char *path)
{
    int rc = symlink(opath, path);

    if (_fsm_debug) {
	rpmlog(RPMLOG_DEBUG, " %8s (%s, %s) %s\n", __func__,
	       opath, path, (rc < 0 ? strerror(errno) : ""));
    }

    if (rc < 0)
	rc = CPIOERR_SYMLINK_FAILED;
    return rc;
}

static int fsmUnlink(const char *path, cpioMapFlags mapFlags)
{
    int rc = 0;
    if (mapFlags & CPIO_SBIT_CHECK)
        removeSBITS(path);
    rc = unlink(path);
    if (_fsm_debug)
	rpmlog(RPMLOG_DEBUG, " %8s (%s) %s\n", __func__,
	       path, (rc < 0 ? strerror(errno) : ""));
    if (rc < 0)
	rc = (errno == ENOENT ? CPIOERR_ENOENT : CPIOERR_UNLINK_FAILED);
    return rc;
}

static int fsmRename(const char *opath, const char *path,
		     cpioMapFlags mapFlags)
{
    if (mapFlags & CPIO_SBIT_CHECK)
        removeSBITS(path);
    int rc = rename(opath, path);
#if defined(ETXTBSY) && defined(__HPUX__)
    /* XXX HP-UX (and other os'es) don't permit rename to busy files. */
    if (rc && errno == ETXTBSY) {
	char *rmpath = NULL;
	rstrscat(&rmpath, path, "-RPMDELETE", NULL);
	rc = rename(path, rmpath);
	if (!rc) rc = rename(opath, path);
	free(rmpath);
    }
#endif
    if (_fsm_debug)
	rpmlog(RPMLOG_DEBUG, " %8s (%s, %s) %s\n", __func__,
	       opath, path, (rc < 0 ? strerror(errno) : ""));
    if (rc < 0)	rc = CPIOERR_RENAME_FAILED;
    return rc;
}


static int fsmChown(const char *path, uid_t uid, gid_t gid)
{
    int rc = chown(path, uid, gid);
    if (rc < 0) {
	struct stat st;
	if (lstat(path, &st) == 0 && st.st_uid == uid && st.st_gid == gid)
	    rc = 0;
    }
    if (_fsm_debug)
	rpmlog(RPMLOG_DEBUG, " %8s (%s, %d, %d) %s\n", __func__,
	       path, (int)uid, (int)gid,
	       (rc < 0 ? strerror(errno) : ""));
    if (rc < 0)	rc = CPIOERR_CHOWN_FAILED;
    return rc;
}

static int fsmLChown(const char *path, uid_t uid, gid_t gid)
{
    int rc = lchown(path, uid, gid);
    if (rc < 0) {
	struct stat st;
	if (lstat(path, &st) == 0 && st.st_uid == uid && st.st_gid == gid)
	    rc = 0;
    }
    if (_fsm_debug)
	rpmlog(RPMLOG_DEBUG, " %8s (%s, %d, %d) %s\n", __func__,
	       path, (int)uid, (int)gid,
	       (rc < 0 ? strerror(errno) : ""));
    if (rc < 0)	rc = CPIOERR_CHOWN_FAILED;
    return rc;
}

static int fsmChmod(const char *path, mode_t mode)
{
    int rc = chmod(path, (mode & 07777));
    if (rc < 0) {
	struct stat st;
	if (lstat(path, &st) == 0 && (st.st_mode & 07777) == (mode & 07777))
	    rc = 0;
    }
    if (_fsm_debug)
	rpmlog(RPMLOG_DEBUG, " %8s (%s, 0%04o) %s\n", __func__,
	       path, (unsigned)(mode & 07777),
	       (rc < 0 ? strerror(errno) : ""));
    if (rc < 0)	rc = CPIOERR_CHMOD_FAILED;
    return rc;
}

static int fsmUtime(const char *path, time_t mtime)
{
    int rc = 0;
    struct utimbuf stamp;
    stamp.actime = mtime;
    stamp.modtime = mtime;
    rc = utime(path, &stamp);
    if (_fsm_debug)
	rpmlog(RPMLOG_DEBUG, " %8s (%s, 0x%x) %s\n", __func__,
	       path, (unsigned)mtime, (rc < 0 ? strerror(errno) : ""));
    if (rc < 0)	rc = CPIOERR_UTIME_FAILED;
    return rc;
}

static int fsmVerify(FSM_t fsm)
{
    int rc;
    struct stat * st = &fsm->sb;
    struct stat * ost = &fsm->osb;
    int saveerrno = errno;

    if (fsm->diskchecked && !fsm->exists) {
        return CPIOERR_ENOENT;
    }
    if (S_ISREG(st->st_mode)) {
	/* HP-UX (and other os'es) don't permit unlink on busy files. */
	char *rmpath = rstrscat(NULL, fsm->path, "-RPMDELETE", NULL);
	rc = fsmRename(fsm->path, rmpath, fsm->mapFlags);
	/* XXX shouldn't we take unlink return code here? */
	if (!rc)
	    (void) fsmUnlink(rmpath, fsm->mapFlags);
	else
	    rc = CPIOERR_UNLINK_FAILED;
	free(rmpath);
        return (rc ? rc : CPIOERR_ENOENT);	/* XXX HACK */
    } else if (S_ISDIR(st->st_mode)) {
        if (S_ISDIR(ost->st_mode)) return 0;
        if (S_ISLNK(ost->st_mode)) {
            rc = fsmStat(fsm->path, 0, &fsm->osb);
            if (rc == CPIOERR_ENOENT) rc = 0;
            if (rc) return rc;
            errno = saveerrno;
            if (S_ISDIR(ost->st_mode)) return 0;
        }
    } else if (S_ISLNK(st->st_mode)) {
        if (S_ISLNK(ost->st_mode)) {
            char buf[8 * BUFSIZ];
            size_t len;
            rc = fsmReadLink(fsm->path, buf, 8 * BUFSIZ, &len);
            errno = saveerrno;
            if (rc) return rc;
	    /* FSM_PROCESS puts link target to fsm->buf. */
            if (rstreq(fsm->buf, buf)) return 0;
        }
    } else if (S_ISFIFO(st->st_mode)) {
        if (S_ISFIFO(ost->st_mode)) return 0;
    } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
        if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
            (ost->st_rdev == st->st_rdev)) return 0;
    } else if (S_ISSOCK(st->st_mode)) {
        if (S_ISSOCK(ost->st_mode)) return 0;
    }
    /* XXX shouldn't do this with commit/undo. */
    rc = fsmUnlink(fsm->path, fsm->mapFlags);
    if (rc == 0)	rc = CPIOERR_ENOENT;
    return (rc ? rc : CPIOERR_ENOENT);	/* XXX HACK */
}

#define	IS_DEV_LOG(_x)	\
	((_x) != NULL && strlen(_x) >= (sizeof("/dev/log")-1) && \
	rstreqn((_x), "/dev/log", sizeof("/dev/log")-1) && \
	((_x)[sizeof("/dev/log")-1] == '\0' || \
	 (_x)[sizeof("/dev/log")-1] == ';'))



/* Rename pre-existing modified or unmanaged file. */
static int fsmBackup(FSM_t fsm)
{
    int rc = 0;

    /* FIXME: %ghost can have backup action but no suffix */
    if ((fsm->action == FA_SAVE || fsm->action == FA_BACKUP) && fsm->osuffix) {
        char * opath = fsmFsPath(fsm, S_ISDIR(fsm->sb.st_mode), NULL);
        char * path = fsmFsPath(fsm, 0, fsm->osuffix);
        rc = fsmRename(opath, path, fsm->mapFlags);
        if (!rc) {
            rpmlog(RPMLOG_WARNING, _("%s saved as %s\n"), opath, path);
            fsm->exists = 0; /* it doesn't exist anymore... */
        }
        free(path);
        free(opath);
    }
    return rc;
}

static int fsmCommit(FSM_t fsm, int ix)
{
    int rc = 0;
    struct stat * st = &fsm->sb;

    /* XXX Special case /dev/log, which shouldn't be packaged anyways */
    if (!S_ISSOCK(st->st_mode) && !IS_DEV_LOG(fsm->path)) {
	/* Backup on-disk file if needed. Directories are handled earlier */
	if (!S_ISDIR(st->st_mode))
	    rc = fsmBackup(fsm);
        /* Rename temporary to final file name. */
        if (!S_ISDIR(st->st_mode) && (fsm->suffix || fsm->nsuffix)) {
            char *npath = fsmFsPath(fsm, 0, fsm->nsuffix);
            rc = fsmRename(fsm->path, npath, fsm->mapFlags);
            if (!rc && fsm->nsuffix) {
                char * opath = fsmFsPath(fsm, 0, NULL);
                rpmlog(RPMLOG_WARNING, _("%s created as %s\n"),
                       opath, npath);
                free(opath);
            }
            free(fsm->path);
            fsm->path = npath;
        }
        /* Set file security context (if enabled) */
        if (!rc && !getuid()) {
            rc = fsmSetSELabel(fsm->sehandle, fsm->path, fsm->sb.st_mode);
        }
        if (S_ISLNK(st->st_mode)) {
            if (!rc && !getuid())
                rc = fsmLChown(fsm->path, fsm->sb.st_uid, fsm->sb.st_gid);
        } else {
            rpmfi fi = fsmGetFi(fsm);
            if (!rc && !getuid())
                rc = fsmChown(fsm->path, fsm->sb.st_uid, fsm->sb.st_gid);
            if (!rc)
                rc = fsmChmod(fsm->path, fsm->sb.st_mode);
            if (!rc) {
                rc = fsmUtime(fsm->path, rpmfiFMtimeIndex(fi, ix));
                /* utime error is not critical for directories */
                if (rc && S_ISDIR(st->st_mode))
                    rc = 0;
            }
            /* Set file capabilities (if enabled) */
            if (!rc && !S_ISDIR(st->st_mode) && !getuid()) {
                rc = fsmSetFCaps(fsm->path, rpmfiFCapsIndex(fi, ix));
            }
        }
    }

    if (rc && fsm->failedFile && *fsm->failedFile == NULL) {
        *fsm->failedFile = fsm->path;
        fsm->path = NULL;
    }
    return rc;
}

/**
 * Return formatted string representation of file disposition.
 * @param a		file dispostion
 * @return		formatted string
 */
static const char * fileActionString(rpmFileAction a)
{
    switch (a) {
    case FA_UNKNOWN:	return "unknown";
    case FA_CREATE:	return "create";
    case FA_COPYOUT:	return "copyout";
    case FA_COPYIN:	return "copyin";
    case FA_BACKUP:	return "backup";
    case FA_SAVE:	return "save";
    case FA_SKIP:	return "skip";
    case FA_ALTNAME:	return "altname";
    case FA_ERASE:	return "erase";
    case FA_SKIPNSTATE: return "skipnstate";
    case FA_SKIPNETSHARED: return "skipnetshared";
    case FA_SKIPCOLOR:	return "skipcolor";
    default:		return "???";
    }
}

/* Remember any non-regular file state for recording in the rpmdb */
static void setFileState(rpmfs fs, int i, rpmFileAction action)
{
    switch (action) {
    case FA_SKIPNSTATE:
	rpmfsSetState(fs, i, RPMFILE_STATE_NOTINSTALLED);
	break;
    case FA_SKIPNETSHARED:
	rpmfsSetState(fs, i, RPMFILE_STATE_NETSHARED);
	break;
    case FA_SKIPCOLOR:
	rpmfsSetState(fs, i, RPMFILE_STATE_WRONGCOLOR);
	break;
    default:
	break;
    }
}

int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfi fi, FD_t cfd,
              rpmpsm psm, char ** failedFile)
{
    rpmfs fs = rpmteGetFileStates(te);
    FSM_t fsm = fsmNew(FSM_PKGINSTALL, fs, fi, failedFile);
    rpmcpio_t archive = rpmcpioOpen(cfd, O_RDONLY);
    struct stat * st = &fsm->sb;
    int saveerrno = errno;
    int rc = 0;
    int nodigest = (rpmtsFlags(ts) & RPMTRANS_FLAG_NOFILEDIGEST);

    if (!rpmteIsSource(te))
	fsm->mapFlags |= CPIO_SBIT_CHECK;

    if (archive == NULL)
	rc = CPIOERR_INTERNAL;

    fsm->sehandle = rpmtsSELabelHandle(ts);
    /* transaction id used for temporary path suffix while installing */
    rasprintf(&fsm->suffix, ";%08x", (unsigned)rpmtsGetTid(ts));

    /* Detect and create directories not explicitly in package. */
    if (!rc) {
	rc = fsmMkdirs(fi, rpmteGetFileStates(te), fsm->sehandle);
    }

    while (!rc) {
	hardLink_t li = NULL;

        /* Clean fsm, free'ing memory. */
	fsmReset(fsm);

	/* Read next payload header. */
        rc = rpmcpioHeaderRead(archive, &(fsm->path), &(fsm->sb));

	/* Detect and exit on end-of-payload. */
	if (rc == CPIOERR_HDR_TRAILER) {
	    rc = 0;
	    break;
	}

	if (rc) break;

	/* Identify mapping index. */
	fsm->ix = mapFind(fsm->iter, fsm->path);

	/* Mapping error */
	if (fsm->ix < 0) {
	    if (fsm->failedFile && *fsm->failedFile == NULL)
		*fsm->failedFile = xstrdup(fsm->path);
	    rc = CPIOERR_UNMAPPED_FILE;
	    break;
	}

        rc = fsmInit(fsm);

        /* Exit on error. */
        if (rc) {
            fsm->postpone = 1;
            break;
        }

	if (S_ISREG(fsm->sb.st_mode) && fsm->sb.st_nlink > 1)
	    fsm->postpone = saveHardLink(fsm, &li);

	setFileState(rpmteGetFileStates(te), fsm->ix, fsm->action);

        if (!fsm->postpone) {
            if (S_ISREG(st->st_mode)) {
                rc = fsmVerify(fsm);
                if (!(rc == CPIOERR_ENOENT)) return rc;
                rc = expandRegular(fsm, psm, archive, nodigest);
            } else if (S_ISDIR(st->st_mode)) {
		/* Directories replacing something need early backup */
                rc = fsmBackup(fsm);
                rc = fsmVerify(fsm);
                if (rc == CPIOERR_ENOENT) {
                    mode_t mode = st->st_mode;
                    mode &= ~07777;
                    mode |=  00700;
                    rc = fsmMkdir(fsm->path, mode);
                }
            } else if (S_ISLNK(st->st_mode)) {
                if ((st->st_size + 1) > fsm->bufsize) {
                    rc = CPIOERR_HDR_SIZE;
                } else if (rpmcpioRead(archive, fsm->buf, st->st_size) != st->st_size) {
                    rc = CPIOERR_READ_FAILED;
                } else {

                    fsm->buf[st->st_size] = '\0';
                    /* fsmVerify() assumes link target in fsm->buf */
                    rc = fsmVerify(fsm);
                    if (rc == CPIOERR_ENOENT) {
                        rc = fsmSymlink(fsm->buf, fsm->path);
                    }
                }
            } else if (S_ISFIFO(st->st_mode)) {
                /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
                rc = fsmVerify(fsm);
                if (rc == CPIOERR_ENOENT) {
                    rc = fsmMkfifo(fsm->path, 0000);
                }
            } else if (S_ISCHR(st->st_mode) ||
                       S_ISBLK(st->st_mode) ||
                       S_ISSOCK(st->st_mode))
            {
                rc = fsmVerify(fsm);
                if (rc == CPIOERR_ENOENT) {
                    rc = fsmMknod(fsm->path, fsm->sb.st_mode, fsm->sb.st_rdev);
                }
            } else {
                /* XXX Special case /dev/log, which shouldn't be packaged anyways */
                if (!IS_DEV_LOG(fsm->path))
                    rc = CPIOERR_UNKNOWN_FILETYPE;
            }
            if (li != NULL) {
                li->createdPath = li->linkIndex;
                rc = fsmMakeLinks(fsm, li);
            }
        }

        if (rc) {
            if (!fsm->postpone) {
                /* XXX only erase if temp fn w suffix is in use */
                if (fsm->suffix) {
                    if (S_ISDIR(st->st_mode)) {
                        (void) fsmRmdir(fsm->path);
                    } else {
                        (void) fsmUnlink(fsm->path, fsm->mapFlags);
                    }
                }
                errno = saveerrno;
                if (fsm->failedFile && *fsm->failedFile == NULL)
                    *fsm->failedFile = xstrdup(fsm->path);
            }

            break;
        }

        /* Notify on success. */
        rpmpsmNotify(psm, RPMCALLBACK_INST_PROGRESS, rpmcpioTell(archive));

        if (!fsm->postpone) {
            rc = ((S_ISREG(st->st_mode) && st->st_nlink > 1)
                  ? fsmCommitLinks(fsm) : fsmCommit(fsm, fsm->ix));
        }
        if (rc) {
            break;
        }
    }

    if (!rc)
	rc = checkHardLinks(fsm);

    /* No need to bother with close errors on read */
    rpmcpioFree(archive);
    fsmFree(fsm);

    return rc;
}


int rpmPackageFilesRemove(rpmts ts, rpmte te, rpmfi fi,
              rpmpsm psm, char ** failedFile)
{
    rpmfs fs = rpmteGetFileStates(te);
    FSM_t fsm = fsmNew(FSM_PKGERASE, fs, fi, failedFile);
    int rc = 0;

    if (!rpmteIsSource(te))
	fsm->mapFlags |= CPIO_SBIT_CHECK;

    while (!rc) {
        /* Clean fsm, free'ing memory. */
	fsmReset(fsm);

	/* Identify mapping index. */
	fsm->ix = mapNextIterator(fsm->iter);

        /* Exit on end-of-payload. */
        if (fsm->ix < 0)
            break;

        rc = fsmInit(fsm);

	if (!fsm->postpone)
	    rc = fsmBackup(fsm);

        /* Remove erased files. */
        if (!fsm->postpone && fsm->action == FA_ERASE) {
	    int missingok = (fsm->fflags & (RPMFILE_MISSINGOK | RPMFILE_GHOST));

            if (S_ISDIR(fsm->sb.st_mode)) {
                rc = fsmRmdir(fsm->path);
            } else {
                rc = fsmUnlink(fsm->path, fsm->mapFlags);
	    }

	    /*
	     * Missing %ghost or %missingok entries are not errors.
	     * XXX: Are non-existent files ever an actual error here? Afterall
	     * that's exactly what we're trying to accomplish here,
	     * and complaining about job already done seems like kinderkarten
	     * level "But it was MY turn!" whining...
	     */
	    if (rc == CPIOERR_ENOENT && missingok) {
		rc = 0;
	    }

	    /*
	     * Dont whine on non-empty directories for now. We might be able
	     * to track at least some of the expected failures though,
	     * such as when we knowingly left config file backups etc behind.
	     */
	    if (rc == CPIOERR_ENOTEMPTY) {
		rc = 0;
	    }

	    if (rc) {
		int lvl = strict_erasures ? RPMLOG_ERR : RPMLOG_WARNING;
		rpmlog(lvl, _("%s %s: remove failed: %s\n"),
			S_ISDIR(fsm->sb.st_mode) ? _("directory") : _("file"),
			fsm->path, strerror(errno));
            }
        }
        /* XXX Failure to remove is not (yet) cause for failure. */
        if (!strict_erasures) rc = 0;

        if (rc) break;

        /* Notify on success. */
        /* On erase we're iterating backwards, fixup for progress */
        rpm_loff_t amount = (fsm->ix >= 0) ?
            rpmfiFC(fsmGetFi(fsm)) - fsm->ix : 0;
        rpmpsmNotify(psm, RPMCALLBACK_UNINST_PROGRESS, amount);
    }

    fsmFree(fsm);

    return rc;
}


int rpmPackageFilesArchive(rpmfi fi, int isSrc, FD_t cfd,
              rpm_loff_t * archiveSize, char ** failedFile)
{
    rpmfs fs = rpmfsNew(rpmfiFC(fi), 0);;
    FSM_t fsm = fsmNew(FSM_PKGBUILD, fs, fi, failedFile);;
    rpmcpio_t archive = rpmcpioOpen(cfd, O_WRONLY);
    int rc = 0;

    fsm->mapFlags |= CPIO_MAP_TYPE;
    if (isSrc) 
	fsm->mapFlags |= CPIO_FOLLOW_SYMLINKS;

    if (archive == NULL) {
	rc = CPIOERR_INTERNAL;
    } else {
	int ghost, i, fc = rpmfiFC(fi);

	/* XXX Is this actually still needed? */
	for (i = 0; i < fc; i++) {
	    ghost = (rpmfiFFlagsIndex(fi, i) & RPMFILE_GHOST);
	    rpmfsSetAction(fs, i, ghost ? FA_SKIP : FA_COPYOUT);
	}
    }
	    
    while (!rc) {
	fsmReset(fsm);

	/* Identify mapping index. */
	fsm->ix = mapNextIterator(fsm->iter);

        /* Exit on end-of-payload. */
        if (fsm->ix < 0)
            break;

        rc = fsmInit(fsm);

        /* Exit on error. */
        if (rc) {
            fsm->postpone = 1;
            break;
        }

	if (S_ISREG(fsm->sb.st_mode) && fsm->sb.st_nlink > 1)
	    fsm->postpone = saveHardLink(fsm, NULL);

        if (fsm->postpone || fsm->fflags & RPMFILE_GHOST) /* XXX Don't if %ghost file. */
            continue;
        /* Hardlinks are handled later */
        if (!(S_ISREG(fsm->sb.st_mode) && fsm->sb.st_nlink > 1)) {
            /* Copy file into archive. */
            rc = writeFile(fsm, 1, archive, fsm->ix);
        }

        if (rc) {
            if (!fsm->postpone) {
                if (fsm->failedFile && *fsm->failedFile == NULL)
                    *fsm->failedFile = xstrdup(fsm->path);
            }

            break;
        }
    }

    /* Flush partial sets of hard linked files. */
    if (!rc)
        rc = writeLinks(fsm, archive);

    /* Finish the payload stream */
    if (!rc)
	rc = rpmcpioClose(archive);

    if (archiveSize)
	*archiveSize = (rc == 0) ? rpmcpioTell(archive) : 0;

    rpmcpioFree(archive);
    rpmfsFree(fs);
    fsmFree(fsm);

    return rc;
}