Use selabel interfaces instead of matchpathcon
[platform/upstream/rpm.git] / lib / fsm.c
1 /** \ingroup payload
2  * \file lib/fsm.c
3  * File state machine to handle a payload from a package.
4  */
5
6 #include "system.h"
7
8 #include <utime.h>
9 #include <errno.h>
10 #if defined(HAVE_MMAP)
11 #include <sys/mman.h>
12 #endif
13 #if WITH_CAP
14 #include <sys/capability.h>
15 #endif
16
17 #include <rpm/rpmte.h>
18 #include <rpm/rpmts.h>
19 #include <rpm/rpmsq.h>
20 #include <rpm/rpmlog.h>
21
22 #include "rpmio/rpmio_internal.h"       /* fdInit/FiniDigest */
23 #include "lib/cpio.h"
24 #include "lib/fsm.h"
25 #define fsmUNSAFE       fsmStage
26 #include "lib/rpmfi_internal.h" /* XXX fi->apath, ... */
27 #include "lib/rpmte_internal.h" /* XXX rpmfs */
28 #include "lib/misc.h"           /* XXX unameToUid() and gnameToGid() */
29
30 #include "debug.h"
31
32 #define _FSM_DEBUG      0
33 int _fsm_debug = _FSM_DEBUG;
34
35 /* XXX Failure to remove is not (yet) cause for failure. */
36 static int strict_erasures = 0;
37
38 /** \ingroup payload
39  * Keeps track of the set of all hard links to a file in an archive.
40  */
41 struct hardLink_s {
42     hardLink_t next;
43     const char ** nsuffix;
44     int * filex;
45     struct stat sb;
46     nlink_t nlink;
47     nlink_t linksLeft;
48     int linkIndex;
49     int createdPath;
50 };
51
52 /** \ingroup payload
53  * Iterator across package file info, forward on install, backward on erase.
54  */
55 struct fsmIterator_s {
56     rpmts ts;                   /*!< transaction set. */
57     rpmte te;                   /*!< transaction element. */
58     rpmfi fi;                   /*!< transaction element file info. */
59     int reverse;                /*!< reversed traversal? */
60     int isave;                  /*!< last returned iterator index. */
61     int i;                      /*!< iterator index. */
62 };
63
64 /**
65  * Retrieve transaction set from file state machine iterator.
66  * @param fsm           file state machine
67  * @return              transaction set
68  */
69 static rpmts fsmGetTs(const FSM_t fsm) {
70     const FSMI_t iter = fsm->iter;
71     return (iter ? iter->ts : NULL);
72 }
73
74 /**
75  * Retrieve transaction element file info from file state machine iterator.
76  * @param fsm           file state machine
77  * @return              transaction element file info
78  */
79 static rpmfi fsmGetFi(const FSM_t fsm)
80 {
81     const FSMI_t iter = fsm->iter;
82     return (iter ? iter->fi : NULL);
83 }
84
85 static rpmte fsmGetTe(const FSM_t fsm)
86 {
87     const FSMI_t iter = fsm->iter;
88     return (iter ? iter->te : NULL);
89 }
90
91 #define SUFFIX_RPMORIG  ".rpmorig"
92 #define SUFFIX_RPMSAVE  ".rpmsave"
93 #define SUFFIX_RPMNEW   ".rpmnew"
94
95 /* Default directory and file permissions if not mapped */
96 #define _dirPerms 0755
97 #define _filePerms 0644
98
99 /* 
100  * XXX Forward declarations for previously exported functions to avoid moving 
101  * things around needlessly 
102  */ 
103 static const char * fileStageString(fileStage a);
104 static const char * fileActionString(rpmFileAction a);
105 static int fsmStage(FSM_t fsm, fileStage stage);
106
107 /** \ingroup payload
108  * Build path to file from file info, ornamented with subdir and suffix.
109  * @param fsm           file state machine data
110  * @param st            file stat info
111  * @param subdir        subdir to use (NULL disables)
112  * @param suffix        suffix to use (NULL disables)
113  * @retval              path to file (malloced)
114  */
115 static char * fsmFsPath(const FSM_t fsm,
116                 const struct stat * st,
117                 const char * subdir,
118                 const char * suffix)
119 {
120     char * s = NULL;
121
122     if (fsm) {
123         int isDir = (st && S_ISDIR(st->st_mode));
124         s = rstrscat(NULL, fsm->dirName,
125                            (!isDir && subdir) ? subdir : "",
126                            fsm->baseName,
127                            (!isDir && suffix) ? suffix : "",
128                            NULL);
129     }
130     return s;
131 }
132
133 /** \ingroup payload
134  * Destroy file info iterator.
135  * @param p             file info iterator
136  * @retval              NULL always
137  */
138 static void * mapFreeIterator(void * p)
139 {
140     FSMI_t iter = p;
141     if (iter) {
142 /* XXX rpmswExit() */
143         iter->ts = rpmtsFree(iter->ts);
144         iter->te = NULL; /* XXX rpmte is not refcounted yet */
145         iter->fi = rpmfiUnlink(iter->fi);
146     }
147     return _free(p);
148 }
149
150 /** \ingroup payload
151  * Create file info iterator.
152  * @param ts            transaction set
153  * @param fi            transaction element file info
154  * @return              file info iterator
155  */
156 static void *
157 mapInitIterator(rpmts ts, rpmte te, rpmfi fi)
158 {
159     FSMI_t iter = NULL;
160
161     iter = xcalloc(1, sizeof(*iter));
162     iter->ts = rpmtsLink(ts);
163     iter->te = te; /* XXX rpmte is not refcounted yet */
164     iter->fi = rpmfiLink(fi);
165     iter->reverse = (rpmteType(te) == TR_REMOVED);
166     iter->i = (iter->reverse ? (rpmfiFC(fi) - 1) : 0);
167     iter->isave = iter->i;
168     return iter;
169 }
170
171 /** \ingroup payload
172  * Return next index into file info.
173  * @param a             file info iterator
174  * @return              next index, -1 on termination
175  */
176 static int mapNextIterator(void * a)
177 {
178     FSMI_t iter = a;
179     int i = -1;
180
181     if (iter) {
182         const rpmfi fi = iter->fi;
183         if (iter->reverse) {
184             if (iter->i >= 0)   i = iter->i--;
185         } else {
186             if (iter->i < rpmfiFC(fi))  i = iter->i++;
187         }
188         iter->isave = i;
189     }
190     return i;
191 }
192
193 /** \ingroup payload
194  */
195 static int cpioStrCmp(const void * a, const void * b)
196 {
197     const char * afn = *(const char **)a;
198     const char * bfn = *(const char **)b;
199
200     /* Match rpm-4.0 payloads with ./ prefixes. */
201     if (afn[0] == '.' && afn[1] == '/') afn += 2;
202     if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
203
204     /* If either path is absolute, make it relative. */
205     if (afn[0] == '/')  afn += 1;
206     if (bfn[0] == '/')  bfn += 1;
207
208     return strcmp(afn, bfn);
209 }
210
211 /** \ingroup payload
212  * Locate archive path in file info.
213  * @param iter          file info iterator
214  * @param fsmPath       archive path
215  * @return              index into file info, -1 if archive path was not found
216  */
217 static int mapFind(FSMI_t iter, const char * fsmPath)
218 {
219     int ix = -1;
220
221     if (iter) {
222         const rpmfi fi = iter->fi;
223         int fc = rpmfiFC(fi);
224         if (fi && fc > 0 && fi->apath && fsmPath && *fsmPath) {
225             char ** p = NULL;
226
227             if (fi->apath != NULL)
228                 p = bsearch(&fsmPath, fi->apath, fc, sizeof(fsmPath),
229                         cpioStrCmp);
230             if (p) {
231                 iter->i = p - fi->apath;
232                 ix = mapNextIterator(iter);
233             }
234         }
235     }
236     return ix;
237 }
238
239 /** \ingroup payload
240  * Directory name iterator.
241  */
242 typedef struct dnli_s {
243     rpmfi fi;
244     char * active;
245     int reverse;
246     int isave;
247     int i;
248 } * DNLI_t;
249
250 /** \ingroup payload
251  * Destroy directory name iterator.
252  * @param a             directory name iterator
253  * @retval              NULL always
254  */
255 static void * dnlFreeIterator(void * a)
256 {
257     if (a) {
258         DNLI_t dnli = (void *)a;
259         if (dnli->active) free(dnli->active);
260     }
261     return _free(a);
262 }
263
264 /** \ingroup payload
265  */
266 static inline int dnlCount(const DNLI_t dnli)
267 {
268     return (dnli ? rpmfiDC(dnli->fi) : 0);
269 }
270
271 /** \ingroup payload
272  */
273 static inline int dnlIndex(const DNLI_t dnli)
274 {
275     return (dnli ? dnli->isave : -1);
276 }
277
278 /** \ingroup payload
279  * Create directory name iterator.
280  * @param fsm           file state machine data
281  * @param reverse       traverse directory names in reverse order?
282  * @return              directory name iterator
283  */
284 static
285 void * dnlInitIterator(const FSM_t fsm,
286                 int reverse)
287         
288 {
289     rpmfi fi = fsmGetFi(fsm);
290     rpmfs fs = rpmteGetFileStates(fsmGetTe(fsm));
291     DNLI_t dnli;
292     int i, j;
293     int dc;
294
295     if (fi == NULL)
296         return NULL;
297     dc = rpmfiDC(fi);
298     dnli = xcalloc(1, sizeof(*dnli));
299     dnli->fi = fi;
300     dnli->reverse = reverse;
301     dnli->i = (reverse ? dc : 0);
302
303     if (dc) {
304         dnli->active = xcalloc(dc, sizeof(*dnli->active));
305         int fc = rpmfiFC(fi);
306
307         /* Identify parent directories not skipped. */
308         for (i = 0; i < fc; i++)
309             if (!XFA_SKIPPING(rpmfsGetAction(fs, i)))
310                 dnli->active[rpmfiDIIndex(fi, i)] = 1;
311
312         /* Exclude parent directories that are explicitly included. */
313         for (i = 0; i < fc; i++) {
314             int dil;
315             size_t dnlen, bnlen;
316
317             if (!S_ISDIR(rpmfiFModeIndex(fi, i)))
318                 continue;
319
320             dil = rpmfiDIIndex(fi, i);
321             dnlen = strlen(rpmfiDNIndex(fi, dil));
322             bnlen = strlen(rpmfiBNIndex(fi, i));
323
324             for (j = 0; j < dc; j++) {
325                 const char * dnl;
326                 size_t jlen;
327
328                 if (!dnli->active[j] || j == dil)
329                     continue;
330                 dnl = rpmfiDNIndex(fi, j);
331                 jlen = strlen(dnl);
332                 if (jlen != (dnlen+bnlen+1))
333                     continue;
334                 if (!rstreqn(dnl, rpmfiDNIndex(fi, dil), dnlen))
335                     continue;
336                 if (!rstreqn(dnl+dnlen, rpmfiBNIndex(fi, i), bnlen))
337                     continue;
338                 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
339                     continue;
340                 /* This directory is included in the package. */
341                 dnli->active[j] = 0;
342                 break;
343             }
344         }
345
346         /* Print only once per package. */
347         if (!reverse) {
348             j = 0;
349             for (i = 0; i < dc; i++) {
350                 if (!dnli->active[i]) continue;
351                 if (j == 0) {
352                     j = 1;
353                     rpmlog(RPMLOG_DEBUG,
354         "========== Directories not explicitly included in package:\n");
355                 }
356                 rpmlog(RPMLOG_DEBUG, "%10d %s\n", i, rpmfiDNIndex(fi, i));
357             }
358             if (j)
359                 rpmlog(RPMLOG_DEBUG, "==========\n");
360         }
361     }
362     return dnli;
363 }
364
365 /** \ingroup payload
366  * Return next directory name (from file info).
367  * @param dnli          directory name iterator
368  * @return              next directory name
369  */
370 static
371 const char * dnlNextIterator(DNLI_t dnli)
372 {
373     const char * dn = NULL;
374
375     if (dnli) {
376         rpmfi fi = dnli->fi;
377         int dc = rpmfiDC(fi);
378         int i = -1;
379
380         if (dnli->active)
381         do {
382             i = (!dnli->reverse ? dnli->i++ : --dnli->i);
383         } while (i >= 0 && i < dc && !dnli->active[i]);
384
385         if (i >= 0 && i < dc)
386             dn = rpmfiDNIndex(fi, i);
387         else
388             i = -1;
389         dnli->isave = i;
390     }
391     return dn;
392 }
393
394 int fsmNext(FSM_t fsm, fileStage nstage)
395 {
396     fsm->nstage = nstage;
397     return fsmStage(fsm, fsm->nstage);
398 }
399
400 /**
401  * Map next file path and action.
402  * @param fsm           file state machine
403  */
404 static int fsmMapPath(FSM_t fsm)
405 {
406     rpmfi fi = fsmGetFi(fsm);   /* XXX const except for fstates */
407     int rc = 0;
408     int i;
409
410     fsm->osuffix = NULL;
411     fsm->nsuffix = NULL;
412     fsm->action = FA_UNKNOWN;
413
414     i = fsm->ix;
415     if (fi && i >= 0 && i < rpmfiFC(fi)) {
416         rpmte te = fsmGetTe(fsm);
417         rpmfs fs = rpmteGetFileStates(te);
418         /* XXX these should use rpmfiFFlags() etc */
419         fsm->action = rpmfsGetAction(fs, i);
420         fsm->fflags = rpmfiFFlagsIndex(fi, i);
421
422         /* src rpms have simple base name in payload. */
423         fsm->dirName = rpmfiDNIndex(fi, rpmfiDIIndex(fi, i));
424         fsm->baseName = rpmfiBNIndex(fi, i);
425
426         if (rpmteType(te) == TR_ADDED) {
427             switch (fsm->action) {
428             case FA_SKIPNSTATE:
429                 rpmfsSetState(fs, i, RPMFILE_STATE_NOTINSTALLED);
430                 break;
431             case FA_SKIPNETSHARED:
432                 rpmfsSetState(fs, i, RPMFILE_STATE_NETSHARED);
433                 break;
434             case FA_SKIPCOLOR:
435                 rpmfsSetState(fs, i, RPMFILE_STATE_WRONGCOLOR);
436                 break;
437             case FA_ALTNAME:
438                 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
439                     fsm->nsuffix = SUFFIX_RPMNEW;
440                 break;
441             case FA_SAVE:
442                 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
443                     fsm->osuffix = SUFFIX_RPMSAVE;
444                 break;
445             default:
446                 break;
447             }
448         }
449
450         if (fsm->action == FA_BACKUP && !(fsm->fflags & RPMFILE_GHOST)) {
451             /* XXX Don't if %ghost file. */
452             fsm->osuffix = (rpmteType(te) == TR_ADDED) ? SUFFIX_RPMORIG : SUFFIX_RPMSAVE;
453         }
454
455         if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
456             const struct stat * st = &fsm->sb;
457             fsm->path = _free(fsm->path);
458             fsm->path = fsmFsPath(fsm, st, fsm->subdir,
459                 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
460         }
461     }
462     return rc;
463 }
464
465 /** \ingroup payload
466  * Save hard link in chain.
467  * @param fsm           file state machine data
468  * @return              Is chain only partially filled?
469  */
470 static int saveHardLink(FSM_t fsm)
471 {
472     struct stat * st = &fsm->sb;
473     int rc = 0;
474     int ix = -1;
475     int j;
476
477     /* Find hard link set. */
478     for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
479         if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
480             break;
481     }
482
483     /* New hard link encountered, add new link to set. */
484     if (fsm->li == NULL) {
485         fsm->li = xcalloc(1, sizeof(*fsm->li));
486         fsm->li->next = NULL;
487         fsm->li->sb = *st;      /* structure assignment */
488         fsm->li->nlink = st->st_nlink;
489         fsm->li->linkIndex = fsm->ix;
490         fsm->li->createdPath = -1;
491
492         fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
493         memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
494         fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
495
496         if (fsm->goal == FSM_PKGBUILD)
497             fsm->li->linksLeft = st->st_nlink;
498         if (fsm->goal == FSM_PKGINSTALL)
499             fsm->li->linksLeft = 0;
500
501         fsm->li->next = fsm->links;
502         fsm->links = fsm->li;
503     }
504
505     if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
506     fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
507     fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
508     if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
509
510     if (fsm->goal == FSM_PKGBUILD)
511         return (fsm->li->linksLeft > 0);
512
513     if (fsm->goal != FSM_PKGINSTALL)
514         return 0;
515
516     if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
517         return 1;
518
519     /* Here come the bits, time to choose a non-skipped file name. */
520     {   rpmfs fs = rpmteGetFileStates(fsmGetTe(fsm));
521
522         for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
523             ix = fsm->li->filex[j];
524             if (ix < 0 || XFA_SKIPPING(rpmfsGetAction(fs, ix)))
525                 continue;
526             break;
527         }
528     }
529
530     /* Are all links skipped or not encountered yet? */
531     if (ix < 0 || j < 0)
532         return 1;       /* XXX W2DO? */
533
534     /* Save the non-skipped file name and map index. */
535     fsm->li->linkIndex = j;
536     fsm->path = _free(fsm->path);
537     fsm->ix = ix;
538     rc = fsmMapPath(fsm);
539     return rc;
540 }
541
542 /** \ingroup payload
543  * Destroy set of hard links.
544  * @param li            set of hard links
545  * @return              NULL always
546  */
547 static void * freeHardLink(hardLink_t li)
548 {
549     if (li) {
550         li->nsuffix = _free(li->nsuffix);       /* XXX elements are shared */
551         li->filex = _free(li->filex);
552     }
553     return _free(li);
554 }
555
556 FSM_t newFSM(cpioMapFlags mapflags)
557 {
558     FSM_t fsm = xcalloc(1, sizeof(*fsm));
559     fsm->mapFlags = mapflags;
560     return fsm;
561 }
562
563 FSM_t freeFSM(FSM_t fsm)
564 {
565     if (fsm) {
566         fsm->path = _free(fsm->path);
567         while ((fsm->li = fsm->links) != NULL) {
568             fsm->links = fsm->li->next;
569             fsm->li->next = NULL;
570             fsm->li = freeHardLink(fsm->li);
571         }
572         fsm->dnlx = _free(fsm->dnlx);
573         fsm->ldn = _free(fsm->ldn);
574         fsm->iter = mapFreeIterator(fsm->iter);
575     }
576     return _free(fsm);
577 }
578
579 /* forward declaration*/
580 static int fsmMkdirs(FSM_t fsm);
581
582 static int fsmCreate(FSM_t fsm)
583 {
584     int rc;
585     {   rpmts ts = fsmGetTs(fsm);
586 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
587         fsm->commit = ((ts && (rpmtsFlags(ts) & _tsmask) &&
588                         fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
589 #undef _tsmask
590     }
591     fsm->path = _free(fsm->path);
592     fsm->opath = _free(fsm->opath);
593     fsm->dnlx = _free(fsm->dnlx);
594
595     fsm->ldn = _free(fsm->ldn);
596     fsm->ldnalloc = fsm->ldnlen = 0;
597
598     fsm->rdsize = fsm->wrsize = 0;
599     fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
600     fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
601     if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
602         fsm->rdsize = 8 * BUFSIZ;
603         fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
604         fsm->wrsize = 8 * BUFSIZ;
605         fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
606     }
607
608     fsm->mkdirsdone = 0;
609     fsm->ix = -1;
610     fsm->links = NULL;
611     fsm->li = NULL;
612     errno = 0;  /* XXX get rid of EBADF */
613
614     /* Detect and create directories not explicitly in package. */
615     if (fsm->goal == FSM_PKGINSTALL) {
616         rc = fsmMkdirs(fsm);
617         if (!rc) fsm->mkdirsdone = 1;
618     }
619     return rc;
620 }
621
622 int fsmSetup(FSM_t fsm, fileStage goal,
623                 rpmts ts, rpmte te, rpmfi fi, FD_t cfd,
624                 rpm_loff_t * archiveSize, char ** failedFile)
625 {
626     int rc, ec = 0;
627
628     fsm->goal = goal;
629     if (cfd != NULL) {
630         fsm->cfd = fdLink(cfd);
631     }
632     fsm->cpioPos = 0;
633     fsm->iter = mapInitIterator(ts, te, fi);
634     fsm->digestalgo = rpmfiDigestAlgo(fi);
635
636     if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
637         void * ptr;
638         fsm->archivePos = 0;
639         ptr = rpmtsNotify(ts, te,
640                 RPMCALLBACK_INST_START, fsm->archivePos, fi->archiveSize);
641     }
642
643     fsm->archiveSize = archiveSize;
644     if (fsm->archiveSize)
645         *fsm->archiveSize = 0;
646     fsm->failedFile = failedFile;
647     if (fsm->failedFile)
648         *fsm->failedFile = NULL;
649
650     memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
651     if (fsm->goal == FSM_PKGINSTALL) {
652         if (ts && rpmtsGetTid(ts) != (rpm_tid_t)-1)
653             sprintf(fsm->sufbuf, ";%08x", (unsigned)rpmtsGetTid(ts));
654     }
655
656     ec = fsm->rc = 0;
657     rc = fsmCreate(fsm);
658     if (rc && !ec) ec = rc;
659
660     rc = fsmUNSAFE(fsm, fsm->goal);
661     if (rc && !ec) ec = rc;
662
663     if (fsm->archiveSize && ec == 0)
664         *fsm->archiveSize = fsm->cpioPos;
665
666 /* FIX: *fsm->failedFile may be NULL */
667    return ec;
668 }
669
670 int fsmTeardown(FSM_t fsm)
671 {
672     int rc = fsm->rc;
673
674     if (!rc)
675         rc = fsmUNSAFE(fsm, FSM_DESTROY);
676
677     fsm->iter = mapFreeIterator(fsm->iter);
678     if (fsm->cfd != NULL) {
679         fsm->cfd = fdFree(fsm->cfd);
680         fsm->cfd = NULL;
681     }
682     fsm->failedFile = NULL;
683     return rc;
684 }
685
686 static int fsmMapFContext(FSM_t fsm)
687 {
688     rpmts ts = fsmGetTs(fsm);
689     struct stat * st;
690     st = &fsm->sb;
691
692     /*
693      * Find file security context (if not disabled).
694      */
695     fsm->fcontext = NULL;
696     if (ts != NULL && !(rpmtsFlags(ts) & RPMTRANS_FLAG_NOCONTEXTS) && rpmtsSELabelHandle(ts)) {
697         security_context_t scon = NULL;
698
699         if (selabel_lookup_raw(rpmtsSELabelHandle(ts), &scon, fsm->path, st->st_mode) == 0 && scon != NULL) {
700             fsm->fcontext = scon;
701         }
702     }
703     return 0;
704 }
705
706 #if WITH_CAP
707 static int fsmMapFCaps(FSM_t fsm)
708 {
709     rpmfi fi = fsmGetFi(fsm);
710     const char *captxt = rpmfiFCapsIndex(fi, fsm->ix);
711     fsm->fcaps = NULL;
712     if (captxt && *captxt != '\0') {
713         cap_t fcaps = cap_from_text(captxt);
714         if (fcaps) {
715            fsm->fcaps = fcaps;
716         }
717     } 
718     return 0;
719 }
720 #endif
721
722 /**
723  * Map file stat(2) info.
724  * @param fsm           file state machine
725  */
726 static int fsmMapAttrs(FSM_t fsm)
727 {
728     struct stat * st = &fsm->sb;
729     rpmfi fi = fsmGetFi(fsm);
730     int i = fsm->ix;
731
732     /* this check is pretty moot,  rpmfi accessors check array bounds etc */
733     if (fi && i >= 0 && i < rpmfiFC(fi)) {
734         mode_t finalMode = rpmfiFModeIndex(fi, i);
735         dev_t finalRdev = rpmfiFRdevIndex(fi, i);
736         time_t finalMtime = rpmfiFMtimeIndex(fi, i);
737         const char *user = rpmfiFUserIndex(fi, i);
738         const char *group = rpmfiFGroupIndex(fi, i);
739         uid_t uid = 0;
740         gid_t gid = 0;
741
742         if (user && unameToUid(user, &uid)) {
743             if (fsm->goal == FSM_PKGINSTALL)
744                 rpmlog(RPMLOG_WARNING,
745                     _("user %s does not exist - using root\n"), user);
746             finalMode &= ~S_ISUID;      /* turn off suid bit */
747         }
748
749         if (group && gnameToGid(group, &gid)) {
750             if (fsm->goal == FSM_PKGINSTALL)
751                 rpmlog(RPMLOG_WARNING,
752                     _("group %s does not exist - using root\n"), group);
753             finalMode &= ~S_ISGID;      /* turn off sgid bit */
754         }
755
756         if (fsm->mapFlags & CPIO_MAP_MODE)
757             st->st_mode = (st->st_mode & S_IFMT) | (finalMode & ~S_IFMT);
758         if (fsm->mapFlags & CPIO_MAP_TYPE) {
759             st->st_mode = (st->st_mode & ~S_IFMT) | (finalMode & S_IFMT);
760             if ((S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
761             && st->st_nlink == 0)
762                 st->st_nlink = 1;
763             st->st_rdev = finalRdev;
764             st->st_mtime = finalMtime;
765         }
766         if (fsm->mapFlags & CPIO_MAP_UID)
767             st->st_uid = uid;
768         if (fsm->mapFlags & CPIO_MAP_GID)
769             st->st_gid = gid;
770
771         {   rpmts ts = fsmGetTs(fsm);
772
773             /*
774              * Set file digest (if not disabled).
775              */
776             if (ts != NULL && !(rpmtsFlags(ts) & RPMTRANS_FLAG_NOFILEDIGEST)) {
777                 fsm->digest = rpmfiFDigestIndex(fi, i, NULL, NULL);
778             } else {
779                 fsm->digest = NULL;
780             }
781         }
782     }
783     return 0;
784 }
785
786 /** \ingroup payload
787  * Create file from payload stream.
788  * @param fsm           file state machine data
789  * @return              0 on success
790  */
791 static int expandRegular(FSM_t fsm)
792 {
793     const struct stat * st = &fsm->sb;
794     rpm_loff_t left = st->st_size;
795     int rc = 0;
796
797     rc = fsmNext(fsm, FSM_WOPEN);
798     if (rc)
799         goto exit;
800
801     if (st->st_size > 0 && fsm->digest != NULL)
802         fdInitDigest(fsm->wfd, fsm->digestalgo, 0);
803
804     while (left) {
805
806         fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
807         rc = fsmNext(fsm, FSM_DREAD);
808         if (rc)
809             goto exit;
810
811         rc = fsmNext(fsm, FSM_WRITE);
812         if (rc)
813             goto exit;
814
815         left -= fsm->wrnb;
816
817         /* don't call this with fileSize == fileComplete */
818         if (!rc && left)
819             (void) fsmNext(fsm, FSM_NOTIFY);
820     }
821
822     if (st->st_size > 0 && fsm->digest) {
823         void * digest = NULL;
824         int asAscii = (fsm->digest == NULL ? 1 : 0);
825
826         (void) Fflush(fsm->wfd);
827         fdFiniDigest(fsm->wfd, fsm->digestalgo, &digest, NULL, asAscii);
828
829         if (digest == NULL) {
830             rc = CPIOERR_DIGEST_MISMATCH;
831             goto exit;
832         }
833
834         if (fsm->digest != NULL) {
835             size_t diglen = rpmDigestLength(fsm->digestalgo);
836             if (memcmp(digest, fsm->digest, diglen))
837                 rc = CPIOERR_DIGEST_MISMATCH;
838         } else {
839             rc = CPIOERR_DIGEST_MISMATCH;
840         }
841         digest = _free(digest);
842     }
843
844 exit:
845     (void) fsmNext(fsm, FSM_WCLOSE);
846     return rc;
847 }
848
849 static int fsmReadLink(FSM_t fsm)
850 {
851     int rc;
852     /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
853     rc = readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
854     if (_fsm_debug && (FSM_READLINK & FSM_SYSCALL))
855         rpmlog(RPMLOG_DEBUG, " %8s (%s, rdbuf, %d) %s\n", fileStageString(FSM_READLINK),
856                fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
857     if (rc < 0) rc = CPIOERR_READLINK_FAILED;
858     else {
859         fsm->rdnb = rc;
860         fsm->rdbuf[fsm->rdnb] = '\0';
861         rc = 0;
862     }
863     return rc;
864 }
865
866 /** \ingroup payload
867  * Write next item to payload stream.
868  * @param fsm           file state machine data
869  * @param writeData     should data be written?
870  * @return              0 on success
871  */
872 static int writeFile(FSM_t fsm, int writeData)
873 {
874     char * path = fsm->path;
875     char * opath = fsm->opath;
876     struct stat * st = &fsm->sb;
877     struct stat * ost = &fsm->osb;
878     char * symbuf = NULL;
879     rpm_loff_t left;
880     int rc;
881
882     st->st_size = (writeData ? ost->st_size : 0);
883
884     if (S_ISDIR(st->st_mode)) {
885         st->st_size = 0;
886     } else if (S_ISLNK(st->st_mode)) {
887         /*
888          * While linux puts the size of a symlink in the st_size field,
889          * I don't think that's a specified standard.
890          */
891         /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
892         rc = fsmReadLink(fsm);
893         if (rc) goto exit;
894         st->st_size = fsm->rdnb;
895         rstrcat(&symbuf, fsm->rdbuf);   /* XXX save readlink return. */
896     }
897
898     if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
899         fsm->path = rstrscat(NULL, (fsm->mapFlags & CPIO_MAP_ADDDOT) ? "." : "",
900                                    fsm->dirName, fsm->baseName, NULL);
901     } else if (fsm->mapFlags & CPIO_MAP_PATH) {
902         rpmfi fi = fsmGetFi(fsm);
903         fsm->path = xstrdup((fi->apath ? fi->apath[fsm->ix] : 
904                                          rpmfiBNIndex(fi, fsm->ix)));
905     }
906
907     rc = cpioHeaderWrite(fsm, st);
908     _free(fsm->path);
909     fsm->path = path;
910     if (rc) goto exit;
911
912     if (writeData && S_ISREG(st->st_mode)) {
913 #ifdef HAVE_MMAP
914         char * rdbuf = NULL;
915         void * mapped = MAP_FAILED;
916         size_t nmapped;
917         int xx;
918 #endif
919
920         rc = fsmNext(fsm, FSM_ROPEN);
921         if (rc) goto exit;
922
923         /* XXX unbuffered mmap generates *lots* of fdio debugging */
924 #ifdef HAVE_MMAP
925         nmapped = 0;
926         mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
927         if (mapped != MAP_FAILED) {
928             rdbuf = fsm->rdbuf;
929             fsm->rdbuf = (char *) mapped;
930             fsm->rdlen = nmapped = st->st_size;
931 #if defined(MADV_DONTNEED)
932             xx = madvise(mapped, nmapped, MADV_DONTNEED);
933 #endif
934         }
935 #endif
936
937         left = st->st_size;
938
939         while (left) {
940 #ifdef HAVE_MMAP
941           if (mapped != MAP_FAILED) {
942             fsm->rdnb = nmapped;
943           } else
944 #endif
945           {
946             fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
947             rc = fsmNext(fsm, FSM_READ);
948             if (rc) goto exit;
949           }
950
951             /* XXX DWRITE uses rdnb for I/O length. */
952             rc = fsmNext(fsm, FSM_DWRITE);
953             if (rc) goto exit;
954
955             left -= fsm->wrnb;
956         }
957
958 #ifdef HAVE_MMAP
959         if (mapped != MAP_FAILED) {
960             xx = msync(mapped, nmapped, MS_ASYNC);
961 #if defined(MADV_DONTNEED)
962             xx = madvise(mapped, nmapped, MADV_DONTNEED);
963 #endif
964             xx = munmap(mapped, nmapped);
965             fsm->rdbuf = rdbuf;
966         }
967 #endif
968
969     } else if (writeData && S_ISLNK(st->st_mode)) {
970         /* XXX DWRITE uses rdnb for I/O length. */
971         strcpy(fsm->rdbuf, symbuf);     /* XXX restore readlink buffer. */
972         fsm->rdnb = strlen(symbuf);
973         rc = fsmNext(fsm, FSM_DWRITE);
974         if (rc) goto exit;
975     }
976
977     rc = fsmNext(fsm, FSM_PAD);
978     if (rc) goto exit;
979
980     rc = 0;
981
982 exit:
983     if (fsm->rfd != NULL)
984         (void) fsmNext(fsm, FSM_RCLOSE);
985     fsm->opath = opath;
986     fsm->path = path;
987     free(symbuf);
988     return rc;
989 }
990
991 /** \ingroup payload
992  * Write set of linked files to payload stream.
993  * @param fsm           file state machine data
994  * @return              0 on success
995  */
996 static int writeLinkedFile(FSM_t fsm)
997 {
998     char * path = fsm->path;
999     const char * nsuffix = fsm->nsuffix;
1000     int iterIndex = fsm->ix;
1001     int ec = 0;
1002     int rc;
1003     int i;
1004
1005     fsm->path = NULL;
1006     fsm->nsuffix = NULL;
1007     fsm->ix = -1;
1008
1009     for (i = fsm->li->nlink - 1; i >= 0; i--) {
1010
1011         if (fsm->li->filex[i] < 0) continue;
1012
1013         fsm->ix = fsm->li->filex[i];
1014         rc = fsmMapPath(fsm);
1015
1016         /* Write data after last link. */
1017         rc = writeFile(fsm, (i == 0));
1018         if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
1019             ec = rc;
1020             *fsm->failedFile = xstrdup(fsm->path);
1021         }
1022
1023         fsm->path = _free(fsm->path);
1024         fsm->li->filex[i] = -1;
1025     }
1026
1027     fsm->ix = iterIndex;
1028     fsm->nsuffix = nsuffix;
1029     fsm->path = path;
1030     return ec;
1031 }
1032
1033 static int fsmStat(FSM_t fsm, int dolstat)
1034 {
1035     int rc;
1036     if (dolstat){
1037         rc = lstat(fsm->path, &fsm->osb);
1038     } else {
1039         rc = stat(fsm->path, &fsm->osb);
1040     }
1041     if (_fsm_debug && (FSM_STAT & FSM_SYSCALL) && rc && errno != ENOENT)
1042         rpmlog(RPMLOG_DEBUG, " %8s (%s, ost) %s\n",
1043                fileStageString(dolstat ? FSM_LSTAT : FSM_STAT),
1044                fsm->path, (rc < 0 ? strerror(errno) : ""));
1045     if (rc < 0) {
1046         rc = (errno == ENOENT ? CPIOERR_ENOENT : CPIOERR_LSTAT_FAILED);
1047         memset(&fsm->osb, 0, sizeof(fsm->osb)); /* XXX s390x hackery */
1048     }
1049     return rc;
1050 }
1051
1052 static int fsmVerify(FSM_t fsm);
1053
1054 /** \ingroup payload
1055  * Create pending hard links to existing file.
1056  * @param fsm           file state machine data
1057  * @return              0 on success
1058  */
1059 static int fsmMakeLinks(FSM_t fsm)
1060 {
1061     char * path = fsm->path;
1062     char * opath = fsm->opath;
1063     const char * nsuffix = fsm->nsuffix;
1064     int iterIndex = fsm->ix;
1065     int ec = 0;
1066     int rc;
1067     int i;
1068
1069     fsm->path = NULL;
1070     fsm->opath = NULL;
1071     fsm->nsuffix = NULL;
1072     fsm->ix = -1;
1073
1074     fsm->ix = fsm->li->filex[fsm->li->createdPath];
1075     rc = fsmMapPath(fsm);
1076     fsm->opath = fsm->path;
1077     fsm->path = NULL;
1078     for (i = 0; i < fsm->li->nlink; i++) {
1079         if (fsm->li->filex[i] < 0) continue;
1080         if (fsm->li->createdPath == i) continue;
1081
1082         fsm->ix = fsm->li->filex[i];
1083         fsm->path = _free(fsm->path);
1084         rc = fsmMapPath(fsm);
1085         if (XFA_SKIPPING(fsm->action)) continue;
1086
1087         rc = fsmVerify(fsm);
1088         if (!rc) continue;
1089         if (!(rc == CPIOERR_ENOENT)) break;
1090
1091         /* XXX link(fsm->opath, fsm->path) */
1092         rc = link(fsm->opath, fsm->path);
1093         if (_fsm_debug && (FSM_LINK & FSM_SYSCALL))
1094             rpmlog(RPMLOG_DEBUG, " %8s (%s, %s) %s\n", fileStageString(FSM_LINK),
1095                 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1096         if (rc < 0)     rc = CPIOERR_LINK_FAILED;
1097
1098         if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
1099             ec = rc;
1100             *fsm->failedFile = xstrdup(fsm->path);
1101         }
1102
1103         fsm->li->linksLeft--;
1104     }
1105     fsm->path = _free(fsm->path);
1106     fsm->opath = _free(fsm->opath);
1107
1108     fsm->ix = iterIndex;
1109     fsm->nsuffix = nsuffix;
1110     fsm->path = path;
1111     fsm->opath = opath;
1112     return ec;
1113 }
1114
1115 /** \ingroup payload
1116  * Commit hard linked file set atomically.
1117  * @param fsm           file state machine data
1118  * @return              0 on success
1119  */
1120 static int fsmCommitLinks(FSM_t fsm)
1121 {
1122     char * path = fsm->path;
1123     const char * nsuffix = fsm->nsuffix;
1124     int iterIndex = fsm->ix;
1125     struct stat * st = &fsm->sb;
1126     int rc = 0;
1127     nlink_t i;
1128
1129     fsm->path = NULL;
1130     fsm->nsuffix = NULL;
1131     fsm->ix = -1;
1132
1133     for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
1134         if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
1135             break;
1136     }
1137
1138     for (i = 0; i < fsm->li->nlink; i++) {
1139         if (fsm->li->filex[i] < 0) continue;
1140         fsm->ix = fsm->li->filex[i];
1141         rc = fsmMapPath(fsm);
1142         if (!XFA_SKIPPING(fsm->action))
1143             rc = fsmNext(fsm, FSM_COMMIT);
1144         fsm->path = _free(fsm->path);
1145         fsm->li->filex[i] = -1;
1146     }
1147
1148     fsm->ix = iterIndex;
1149     fsm->nsuffix = nsuffix;
1150     fsm->path = path;
1151     return rc;
1152 }
1153
1154 static int fsmRmdir(FSM_t fsm)
1155 {
1156     int rc = rmdir(fsm->path);
1157     if (_fsm_debug && (FSM_RMDIR & FSM_SYSCALL))
1158         rpmlog(RPMLOG_DEBUG, " %8s (%s) %s\n", fileStageString(FSM_RMDIR),
1159                fsm->path, (rc < 0 ? strerror(errno) : ""));
1160     if (rc < 0)
1161         switch (errno) {
1162         case ENOENT:        rc = CPIOERR_ENOENT;    break;
1163         case ENOTEMPTY:     rc = CPIOERR_ENOTEMPTY; break;
1164         default:            rc = CPIOERR_RMDIR_FAILED; break;
1165         }
1166     return rc;
1167 }
1168
1169 static int fsmLsetfcon(FSM_t fsm)
1170 {
1171     int rc = 0;
1172     if (fsm->fcontext == NULL || *fsm->fcontext == '\0'
1173         || rstreq(fsm->fcontext, "<<none>>"))
1174         return rc;
1175     rc = lsetfilecon(fsm->path, (security_context_t)fsm->fcontext);
1176     if (_fsm_debug && (FSM_LSETFCON & FSM_SYSCALL))
1177         rpmlog(RPMLOG_DEBUG, " %8s (%s, %s) %s\n", fileStageString(FSM_LSETFCON),
1178                fsm->path, fsm->fcontext,
1179                (rc < 0 ? strerror(errno) : ""));
1180     if (rc < 0) rc = (errno == EOPNOTSUPP ? 0 : CPIOERR_LSETFCON_FAILED);
1181     return rc;
1182 }
1183
1184 static int fsmMkdir(FSM_t fsm)
1185 {
1186     int rc = mkdir(fsm->path, (fsm->sb.st_mode & 07777));
1187     if (_fsm_debug && (FSM_MKDIR & FSM_SYSCALL))
1188         rpmlog(RPMLOG_DEBUG, " %8s (%s, 0%04o) %s\n", fileStageString(FSM_MKDIR),
1189                fsm->path, (unsigned)(fsm->sb.st_mode & 07777),
1190                (rc < 0 ? strerror(errno) : ""));
1191     if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1192     return rc;
1193 }
1194
1195 /**
1196  * Create (if necessary) directories not explicitly included in package.
1197  * @param fsm           file state machine data
1198  * @return              0 on success
1199  */
1200 static int fsmMkdirs(FSM_t fsm)
1201 {
1202     struct stat * st = &fsm->sb;
1203     struct stat * ost = &fsm->osb;
1204     char * path = fsm->path;
1205     const char *dpath;
1206     mode_t st_mode = st->st_mode;
1207     void * dnli = dnlInitIterator(fsm, 0);
1208     int dc = dnlCount(dnli);
1209     int rc = 0;
1210     int i;
1211     rpmts ts = fsmGetTs(fsm);
1212     security_context_t scon = NULL;
1213
1214     fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1215     if (fsm->dnlx != NULL)
1216     while ((dpath = dnlNextIterator(dnli)) != NULL) {
1217         size_t dnlen = strlen(dpath);
1218         char * te, dn[dnlen+1];
1219
1220         dc = dnlIndex(dnli);
1221         if (dc < 0) continue;
1222         fsm->dnlx[dc] = dnlen;
1223         if (dnlen <= 1)
1224             continue;
1225
1226         if (dnlen <= fsm->ldnlen && rstreq(dpath, fsm->ldn))
1227             continue;
1228
1229         /* Copy as we need to modify the string */
1230         (void) stpcpy(dn, dpath);
1231         fsm->path = dn;
1232
1233         /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1234         for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1235             if (*te != '/')
1236                 continue;
1237
1238             *te = '\0';
1239
1240             /* Already validated? */
1241             if (i < fsm->ldnlen &&
1242                 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1243                 rstreqn(fsm->path, fsm->ldn, i))
1244             {
1245                 *te = '/';
1246                 /* Move pre-existing path marker forward. */
1247                 fsm->dnlx[dc] = (te - dn);
1248                 continue;
1249             }
1250
1251             /* Validate next component of path. */
1252             rc = fsmStat(fsm, 1); /* lstat */
1253             *te = '/';
1254
1255             /* Directory already exists? */
1256             if (rc == 0 && S_ISDIR(ost->st_mode)) {
1257                 /* Move pre-existing path marker forward. */
1258                 fsm->dnlx[dc] = (te - dn);
1259             } else if (rc == CPIOERR_ENOENT) {
1260                 *te = '\0';
1261                 st->st_mode = S_IFDIR | (_dirPerms & 07777);
1262                 rc = fsmMkdir(fsm);
1263                 if (!rc) {
1264                     /* XXX FIXME? only new dir will have context set. */
1265                     /* Get file security context from patterns. */
1266                     if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOCONTEXTS) && rpmtsSELabelHandle(ts)) {
1267                         if (selabel_lookup_raw(rpmtsSELabelHandle(ts), &scon, fsm->path, st->st_mode) == 0 &&
1268                             scon != NULL) {
1269                             fsm->fcontext = scon;
1270                             rc = fsmLsetfcon(fsm);
1271                         }
1272                     }
1273
1274                     if (fsm->fcontext == NULL)
1275                         rpmlog(RPMLOG_DEBUG,
1276                             "%s directory created with perms %04o, no context.\n",
1277                             fsm->path, (unsigned)(st->st_mode & 07777));
1278                     else {
1279                         rpmlog(RPMLOG_DEBUG,
1280                             "%s directory created with perms %04o, context %s.\n",
1281                             fsm->path, (unsigned)(st->st_mode & 07777),
1282                             fsm->fcontext);
1283                         freecon(fsm->fcontext);
1284                     }
1285                     fsm->fcontext = NULL;
1286                 }
1287                 *te = '/';
1288             }
1289             if (rc)
1290                 break;
1291         }
1292         if (rc) break;
1293
1294         /* Save last validated path. */
1295 /* FIX: ldn/path annotations ? */
1296         if (fsm->ldnalloc < (dnlen + 1)) {
1297             fsm->ldnalloc = dnlen + 100;
1298             fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1299         }
1300         if (fsm->ldn != NULL) { /* XXX can't happen */
1301             strcpy(fsm->ldn, fsm->path);
1302             fsm->ldnlen = dnlen;
1303         }
1304     }
1305     dnli = dnlFreeIterator(dnli);
1306
1307     fsm->path = path;
1308     st->st_mode = st_mode;              /* XXX restore st->st_mode */
1309 /* FIX: ldn/path annotations ? */
1310     return rc;
1311 }
1312
1313 static void removeSBITS(const char *path)
1314 {
1315     struct stat stb;
1316     if (lstat(path, &stb) == 0 && S_ISREG(stb.st_mode)) {
1317         if ((stb.st_mode & 06000) != 0) {
1318             (void) chmod(path, stb.st_mode & 0777);
1319         }
1320 #if WITH_CAP
1321         if (stb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) {
1322             (void) cap_set_file(path, NULL);
1323         }
1324 #endif
1325     }
1326 }
1327
1328 /********************************************************************/
1329
1330 static int fsmInit(FSM_t fsm)
1331 {
1332     int rc = 0;
1333
1334     fsm->path = _free(fsm->path);
1335     fsm->postpone = 0;
1336     fsm->diskchecked = fsm->exists = 0;
1337     fsm->subdir = NULL;
1338     fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1339     fsm->action = FA_UNKNOWN;
1340     fsm->osuffix = NULL;
1341     fsm->nsuffix = NULL;
1342
1343     if (fsm->goal == FSM_PKGINSTALL) {
1344         /* Read next header from payload, checking for end-of-payload. */
1345         rc = fsmUNSAFE(fsm, FSM_NEXT);
1346     }
1347     if (rc) return rc;
1348
1349     /* Identify mapping index. */
1350     fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1351                ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1352
1353     /* Detect end-of-loop and/or mapping error. */
1354     if (fsm->ix < 0) {
1355         if (fsm->goal == FSM_PKGINSTALL) {
1356 #if 0
1357             rpmlog(RPMLOG_WARNING,
1358                    _("archive file %s was not found in header file list\n"),
1359                    fsm->path);
1360 #endif
1361             if (fsm->failedFile && *fsm->failedFile == NULL)
1362                 *fsm->failedFile = xstrdup(fsm->path);
1363             rc = CPIOERR_UNMAPPED_FILE;
1364         } else {
1365             rc = CPIOERR_HDR_TRAILER;
1366         }
1367         return rc;
1368     }
1369
1370     /* On non-install, mode must be known so that dirs don't get suffix. */
1371     if (fsm->goal != FSM_PKGINSTALL) {
1372         rpmfi fi = fsmGetFi(fsm);
1373         fsm->sb.st_mode = rpmfiFModeIndex(fi, fsm->ix);
1374     }
1375
1376     /* Generate file path. */
1377     rc = fsmMapPath(fsm);
1378     if (rc) return rc;
1379
1380     /* Perform lstat/stat for disk file. */
1381     if (fsm->path != NULL &&
1382         !(fsm->goal == FSM_PKGINSTALL && S_ISREG(fsm->sb.st_mode)))
1383     {
1384         rc = fsmStat(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)));
1385         if (rc == CPIOERR_ENOENT) {
1386             // errno = saveerrno; XXX temporary commented out
1387             rc = 0;
1388             fsm->exists = 0;
1389         } else if (rc == 0) {
1390             fsm->exists = 1;
1391         }
1392     } else {
1393         /* Skip %ghost files on build. */
1394         fsm->exists = 0;
1395     }
1396     fsm->diskchecked = 1;
1397     if (rc) return rc;
1398
1399     /* On non-install, the disk file stat is what's remapped. */
1400     if (fsm->goal != FSM_PKGINSTALL)
1401         fsm->sb = fsm->osb;                     /* structure assignment */
1402
1403     /* Remap file perms, owner, and group. */
1404     rc = fsmMapAttrs(fsm);
1405     if (rc) return rc;
1406
1407     fsm->postpone = XFA_SKIPPING(fsm->action);
1408     if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1409         /* FIX: saveHardLink can modify fsm */
1410         if (S_ISREG(fsm->sb.st_mode) && fsm->sb.st_nlink > 1)
1411             fsm->postpone = saveHardLink(fsm);
1412     }
1413     return rc;
1414
1415 }
1416
1417 static int fsmUnlink(FSM_t fsm)
1418 {
1419     int rc = 0;
1420     if (fsm->mapFlags & CPIO_SBIT_CHECK)
1421         removeSBITS(fsm->path);
1422     rc = unlink(fsm->path);
1423     if (_fsm_debug && (FSM_UNLINK & FSM_SYSCALL))
1424         rpmlog(RPMLOG_DEBUG, " %8s (%s) %s\n", fileStageString(FSM_UNLINK),
1425                fsm->path, (rc < 0 ? strerror(errno) : ""));
1426     if (rc < 0)
1427         rc = (errno == ENOENT ? CPIOERR_ENOENT : CPIOERR_UNLINK_FAILED);
1428     return rc;
1429 }
1430
1431 static int fsmRename(FSM_t fsm)
1432 {
1433     if (fsm->mapFlags & CPIO_SBIT_CHECK)
1434         removeSBITS(fsm->path);
1435     int rc = rename(fsm->opath, fsm->path);
1436 #if defined(ETXTBSY) && defined(__HPUX__)
1437     if (rc && errno == ETXTBSY) {
1438         char *path = NULL;
1439         rstrscat(&path, fsm->path, "-RPMDELETE", NULL);
1440         /*
1441          * XXX HP-UX (and other os'es) don't permit rename to busy
1442          * XXX files.
1443          */
1444         rc = rename(fsm->path, path);
1445         if (!rc) rc = rename(fsm->opath, fsm->path);
1446         free(path);
1447     }
1448 #endif
1449     if (_fsm_debug && (FSM_RENAME & FSM_SYSCALL))
1450         rpmlog(RPMLOG_DEBUG, " %8s (%s, %s) %s\n", fileStageString(FSM_RENAME),
1451                fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1452     if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1453     return rc;
1454 }
1455
1456
1457 static int fsmChown(FSM_t fsm)
1458 {
1459     int rc = chown(fsm->path, fsm->sb.st_uid, fsm->sb.st_gid);
1460     if (_fsm_debug && (FSM_CHOWN & FSM_SYSCALL))
1461         rpmlog(RPMLOG_DEBUG, " %8s (%s, %d, %d) %s\n", fileStageString(FSM_CHOWN),
1462                fsm->path, (int)fsm->sb.st_uid, (int)fsm->sb.st_gid,
1463                (rc < 0 ? strerror(errno) : ""));
1464     if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1465     return rc;
1466 }
1467
1468 static int fsmLChown(FSM_t fsm)
1469 {
1470     int rc = 0;
1471     rc = lchown(fsm->path, fsm->sb.st_uid, fsm->sb.st_gid);
1472     if (_fsm_debug && (FSM_LCHOWN & FSM_SYSCALL))
1473         rpmlog(RPMLOG_DEBUG, " %8s (%s, %d, %d) %s\n", fileStageString(FSM_LCHOWN),
1474                fsm->path, (int)fsm->sb.st_uid, (int)fsm->sb.st_gid,
1475                (rc < 0 ? strerror(errno) : ""));
1476     if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1477     return rc;
1478 }
1479
1480 static int fsmChmod(FSM_t fsm)
1481 {
1482     int rc = chmod(fsm->path, (fsm->sb.st_mode & 07777));
1483     if (_fsm_debug && (FSM_CHMOD & FSM_SYSCALL))
1484         rpmlog(RPMLOG_DEBUG, " %8s (%s, 0%04o) %s\n", fileStageString(FSM_CHMOD),
1485                fsm->path, (unsigned)(fsm->sb.st_mode & 07777),
1486                (rc < 0 ? strerror(errno) : ""));
1487     if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1488     return rc;
1489 }
1490
1491 static int fsmUtime(FSM_t fsm)
1492 {
1493     int rc = 0;
1494     struct utimbuf stamp;
1495     stamp.actime = fsm->sb.st_mtime;
1496     stamp.modtime = fsm->sb.st_mtime;
1497     rc = utime(fsm->path, &stamp);
1498     if (_fsm_debug && (FSM_UTIME & FSM_SYSCALL))
1499         rpmlog(RPMLOG_DEBUG, " %8s (%s, 0x%x) %s\n", fileStageString(FSM_UTIME),
1500                fsm->path, (unsigned)fsm->sb.st_mtime,
1501                (rc < 0 ? strerror(errno) : ""));
1502     if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1503     return rc;
1504 }
1505
1506 #if WITH_CAP
1507 static int fsmSetcap(FSM_t fsm)
1508 {
1509     int rc = cap_set_file(fsm->path, fsm->fcaps);
1510     if (rc < 0) {
1511         rc = CPIOERR_SETCAP_FAILED;
1512     }
1513     return rc;
1514 }
1515 #endif /* WITH_CAP */
1516
1517 static int fsmVerify(FSM_t fsm)
1518 {
1519     int rc;
1520     struct stat * st = &fsm->sb;
1521     struct stat * ost = &fsm->osb;
1522     int saveerrno = errno;
1523
1524     if (fsm->diskchecked && !fsm->exists) {
1525         return CPIOERR_ENOENT;
1526     }
1527     if (S_ISREG(st->st_mode)) {
1528         /*
1529          * XXX HP-UX (and other os'es) don't permit unlink on busy
1530          * XXX files.
1531          */
1532         fsm->opath = fsm->path;
1533         fsm->path = rstrscat(NULL, fsm->path, "-RPMDELETE", NULL);
1534         rc = fsmRename(fsm);
1535         if (!rc)
1536             (void) fsmUnlink(fsm);
1537         else
1538             rc = CPIOERR_UNLINK_FAILED;
1539         _free(fsm->path);
1540         fsm->path = fsm->opath;
1541         fsm->opath = NULL;
1542         return (rc ? rc : CPIOERR_ENOENT);      /* XXX HACK */
1543     } else if (S_ISDIR(st->st_mode)) {
1544         if (S_ISDIR(ost->st_mode)) return 0;
1545         if (S_ISLNK(ost->st_mode)) {
1546             rc = fsmStat(fsm, 0);
1547             if (rc == CPIOERR_ENOENT) rc = 0;
1548             if (rc) return rc;
1549             errno = saveerrno;
1550             if (S_ISDIR(ost->st_mode)) return 0;
1551         }
1552     } else if (S_ISLNK(st->st_mode)) {
1553         if (S_ISLNK(ost->st_mode)) {
1554             /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1555             rc = fsmReadLink(fsm);
1556             errno = saveerrno;
1557             if (rc) return rc;
1558             if (rstreq(fsm->opath, fsm->rdbuf)) return 0;
1559         }
1560     } else if (S_ISFIFO(st->st_mode)) {
1561         if (S_ISFIFO(ost->st_mode)) return 0;
1562     } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1563         if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1564             (ost->st_rdev == st->st_rdev)) return 0;
1565     } else if (S_ISSOCK(st->st_mode)) {
1566         if (S_ISSOCK(ost->st_mode)) return 0;
1567     }
1568     /* XXX shouldn't do this with commit/undo. */
1569     rc = 0;
1570     if (fsm->stage == FSM_PROCESS) rc = fsmUnlink(fsm);
1571     if (rc == 0)        rc = CPIOERR_ENOENT;
1572     return (rc ? rc : CPIOERR_ENOENT);  /* XXX HACK */
1573 }
1574
1575 /********************************************************************/
1576
1577 #define IS_DEV_LOG(_x)  \
1578         ((_x) != NULL && strlen(_x) >= (sizeof("/dev/log")-1) && \
1579         rstreqn((_x), "/dev/log", sizeof("/dev/log")-1) && \
1580         ((_x)[sizeof("/dev/log")-1] == '\0' || \
1581          (_x)[sizeof("/dev/log")-1] == ';'))
1582
1583 /**
1584  * File state machine driver.
1585  * @param fsm           file state machine
1586  * @param stage         next stage
1587  * @return              0 on success
1588  */
1589 static int fsmStage(FSM_t fsm, fileStage stage)
1590 {
1591 #ifdef  UNUSED
1592     fileStage prevStage = fsm->stage;
1593     const char * const prev = fileStageString(prevStage);
1594 #endif
1595     static int modulo = 4;
1596     const char * const cur = fileStageString(stage);
1597     struct stat * st = &fsm->sb;
1598     struct stat * ost = &fsm->osb;
1599     int saveerrno = errno;
1600     int rc = fsm->rc;
1601     rpm_loff_t left;
1602
1603 #define _fafilter(_a)   \
1604     (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1605         ? fileActionString(_a) : "")
1606
1607     if (stage & FSM_DEAD) {
1608         /* do nothing */
1609     } else if (stage & FSM_INTERNAL) {
1610         if (_fsm_debug && !(stage & FSM_SYSCALL))
1611             rpmlog(RPMLOG_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1612                 cur,
1613                 (unsigned)st->st_mode, (int)st->st_nlink,
1614                 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1615                 (fsm->path ? fsm->path : ""),
1616                 _fafilter(fsm->action));
1617     } else {
1618         fsm->stage = stage;
1619         if (_fsm_debug || !(stage & FSM_VERBOSE))
1620             rpmlog(RPMLOG_DEBUG, "%-8s  %06o%3d (%4d,%4d)%10d %s %s\n",
1621                 cur,
1622                 (unsigned)st->st_mode, (int)st->st_nlink,
1623                 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1624                 (fsm->path ? fsm->path : ""),
1625                 _fafilter(fsm->action));
1626     }
1627 #undef  _fafilter
1628
1629     switch (stage) {
1630     case FSM_UNKNOWN:
1631         break;
1632     case FSM_PKGINSTALL:
1633         while (1) {
1634             /* Clean fsm, free'ing memory. Read next archive header. */
1635             rc = fsmInit(fsm);
1636
1637             /* Exit on end-of-payload. */
1638             if (rc == CPIOERR_HDR_TRAILER) {
1639                 rc = 0;
1640                 break;
1641             }
1642
1643             /* Exit on error. */
1644             if (rc) {
1645                 fsm->postpone = 1;
1646                 (void) fsmNext(fsm, FSM_UNDO);
1647                 break;
1648             }
1649
1650             /* Extract file from archive. */
1651             rc = fsmNext(fsm, FSM_PROCESS);
1652             if (rc) {
1653                 (void) fsmNext(fsm, FSM_UNDO);
1654                 break;
1655             }
1656
1657             /* Notify on success. */
1658             (void) fsmNext(fsm, FSM_NOTIFY);
1659
1660             rc = fsmNext(fsm, FSM_FINI);
1661             if (rc) {
1662                 break;
1663             }
1664         }
1665         break;
1666     case FSM_PKGERASE:
1667     case FSM_PKGCOMMIT:
1668         while (1) {
1669             /* Clean fsm, free'ing memory. */
1670             rc = fsmInit(fsm);
1671
1672             /* Exit on end-of-payload. */
1673             if (rc == CPIOERR_HDR_TRAILER) {
1674                 rc = 0;
1675                 break;
1676             }
1677
1678             /* Rename/erase next item. */
1679             if (fsmNext(fsm, FSM_FINI))
1680                 break;
1681         }
1682         break;
1683     case FSM_PKGBUILD:
1684         while (1) {
1685
1686             rc = fsmInit(fsm);
1687
1688             /* Exit on end-of-payload. */
1689             if (rc == CPIOERR_HDR_TRAILER) {
1690                 rc = 0;
1691                 break;
1692             }
1693
1694             /* Exit on error. */
1695             if (rc) {
1696                 fsm->postpone = 1;
1697                 (void) fsmNext(fsm, FSM_UNDO);
1698                 break;
1699             }
1700
1701             /* Copy file into archive. */
1702             rc = fsmNext(fsm, FSM_PROCESS);
1703             if (rc) {
1704                 (void) fsmNext(fsm, FSM_UNDO);
1705                 break;
1706             }
1707
1708             /* Notify on success. */
1709             (void) fsmNext(fsm, FSM_NOTIFY);
1710
1711             if (fsmNext(fsm, FSM_FINI))
1712                 break;
1713         }
1714
1715         /* Flush partial sets of hard linked files. */
1716         if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1717             nlink_t i, nlink;
1718             int j;
1719             while ((fsm->li = fsm->links) != NULL) {
1720                 fsm->links = fsm->li->next;
1721                 fsm->li->next = NULL;
1722
1723                 /* Re-calculate link count for archive header. */
1724                 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1725                     if (fsm->li->filex[i] < 0)
1726                         continue;
1727                     nlink++;
1728                     if (j == -1) j = i;
1729                 }
1730                 /* XXX force the contents out as well. */
1731                 if (j != 0) {
1732                     fsm->li->filex[0] = fsm->li->filex[j];
1733                     fsm->li->filex[j] = -1;
1734                 }
1735                 fsm->li->sb.st_nlink = nlink;
1736
1737                 fsm->sb = fsm->li->sb;  /* structure assignment */
1738                 fsm->osb = fsm->sb;     /* structure assignment */
1739
1740                 if (!rc) rc = writeLinkedFile(fsm);
1741
1742                 fsm->li = freeHardLink(fsm->li);
1743             }
1744         }
1745
1746         if (!rc)
1747             rc = cpioTrailerWrite(fsm);
1748
1749         break;
1750     case FSM_PROCESS:
1751         if (fsm->postpone) {
1752             if (fsm->goal == FSM_PKGINSTALL)
1753                 rc = fsmNext(fsm, FSM_EAT);
1754             break;
1755         }
1756
1757         if (fsm->goal == FSM_PKGBUILD) {
1758             if (fsm->fflags & RPMFILE_GHOST) /* XXX Don't if %ghost file. */
1759                 break;
1760             if (S_ISREG(st->st_mode) && st->st_nlink > 1) {
1761                 hardLink_t li, prev;
1762
1763 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1764                 rc = writeLinkedFile(fsm);
1765                 if (rc) break;  /* W2DO? */
1766
1767                 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1768                      if (li == fsm->li)
1769                         break;
1770
1771                 if (prev == NULL)
1772                     fsm->links = fsm->li->next;
1773                 else
1774                     prev->next = fsm->li->next;
1775                 fsm->li->next = NULL;
1776                 fsm->li = freeHardLink(fsm->li);
1777             } else {
1778                 rc = writeFile(fsm, 1);
1779             }
1780             break;
1781         }
1782
1783         if (fsm->goal != FSM_PKGINSTALL)
1784             break;
1785
1786         if (S_ISREG(st->st_mode)) {
1787             char * path = fsm->path;
1788             if (fsm->osuffix)
1789                 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1790             rc = fsmVerify(fsm);
1791
1792             if (rc == 0 && fsm->osuffix) {
1793                 char * opath = fsm->opath;
1794                 fsm->opath = fsm->path;
1795                 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1796                 rc = fsmRename(fsm);
1797                 if (!rc)
1798                     rpmlog(RPMLOG_WARNING,
1799                         _("%s saved as %s\n"),
1800                                 (fsm->opath ? fsm->opath : ""),
1801                                 (fsm->path ? fsm->path : ""));
1802                 fsm->path = _free(fsm->path);
1803                 fsm->opath = opath;
1804             }
1805
1806             fsm->path = path;
1807             if (!(rc == CPIOERR_ENOENT)) return rc;
1808             rc = expandRegular(fsm);
1809         } else if (S_ISDIR(st->st_mode)) {
1810             mode_t st_mode = st->st_mode;
1811             rc = fsmVerify(fsm);
1812             if (rc == CPIOERR_ENOENT) {
1813                 st->st_mode &= ~07777;          /* XXX abuse st->st_mode */
1814                 st->st_mode |=  00700;
1815                 rc = fsmMkdir(fsm);
1816                 st->st_mode = st_mode;          /* XXX restore st->st_mode */
1817             }
1818         } else if (S_ISLNK(st->st_mode)) {
1819             char * opath = fsm->opath;
1820
1821             if ((st->st_size + 1) > fsm->rdsize) {
1822                 rc = CPIOERR_HDR_SIZE;
1823                 break;
1824             }
1825
1826             fsm->wrlen = st->st_size;
1827             rc = fsmNext(fsm, FSM_DREAD);
1828             if (!rc && fsm->rdnb != fsm->wrlen)
1829                 rc = CPIOERR_READ_FAILED;
1830             if (rc) break;
1831
1832             fsm->wrbuf[st->st_size] = '\0';
1833             /* XXX symlink(fsm->opath, fsm->path) */
1834             fsm->opath = fsm->wrbuf;
1835             rc = fsmVerify(fsm);
1836             if (rc == CPIOERR_ENOENT) {
1837                 rc = symlink(fsm->opath, fsm->path);
1838                 if (_fsm_debug && (FSM_SYMLINK & FSM_SYSCALL))
1839                     rpmlog(RPMLOG_DEBUG, " %8s (%s, %s) %s\n", fileStageString(FSM_SYMLINK),
1840                            fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1841                 if (rc < 0)     rc = CPIOERR_SYMLINK_FAILED;
1842             }
1843             fsm->opath = opath;         /* XXX restore fsm->path */
1844         } else if (S_ISFIFO(st->st_mode)) {
1845             mode_t st_mode = st->st_mode;
1846             /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1847             rc = fsmVerify(fsm);
1848             if (rc == CPIOERR_ENOENT) {
1849                 st->st_mode = 0000;             /* XXX abuse st->st_mode */
1850
1851                 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1852                 if (_fsm_debug && (FSM_MKFIFO & FSM_SYSCALL))
1853                     rpmlog(RPMLOG_DEBUG, " %8s (%s, 0%04o) %s\n", fileStageString(FSM_MKFIFO),
1854                            fsm->path, (unsigned)(st->st_mode & 07777),
1855                            (rc < 0 ? strerror(errno) : ""));
1856                 if (rc < 0)     rc = CPIOERR_MKFIFO_FAILED;
1857
1858                 st->st_mode = st_mode;  /* XXX restore st->st_mode */
1859             }
1860         } else if (S_ISCHR(st->st_mode) ||
1861                    S_ISBLK(st->st_mode) ||
1862     S_ISSOCK(st->st_mode))
1863         {
1864             rc = fsmVerify(fsm);
1865             if (rc == CPIOERR_ENOENT) {
1866                 /* FIX: check S_IFIFO or dev != 0 */
1867                 rc = mknod(fsm->path, (fsm->sb.st_mode & ~07777), fsm->sb.st_rdev);
1868                 if (_fsm_debug && (FSM_MKNOD & FSM_SYSCALL))
1869                     rpmlog(RPMLOG_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", fileStageString(FSM_MKNOD),
1870                            fsm->path, (unsigned)(fsm->sb.st_mode & ~07777),
1871                            (unsigned)fsm->sb.st_rdev,
1872                            (rc < 0 ? strerror(errno) : ""));
1873                 if (rc < 0)     rc = CPIOERR_MKNOD_FAILED;
1874             }
1875         } else {
1876             /* XXX Special case /dev/log, which shouldn't be packaged anyways */
1877             if (!IS_DEV_LOG(fsm->path))
1878                 rc = CPIOERR_UNKNOWN_FILETYPE;
1879         }
1880         if (S_ISREG(st->st_mode) && st->st_nlink > 1) {
1881             fsm->li->createdPath = fsm->li->linkIndex;
1882             rc = fsmMakeLinks(fsm);
1883         }
1884         break;
1885     case FSM_POST:
1886         break;
1887     case FSM_MKLINKS:
1888         rc = fsmMakeLinks(fsm);
1889         break;
1890     case FSM_NOTIFY:            /* XXX move from fsm to psm -> tsm */
1891         if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1892             rpmts ts = fsmGetTs(fsm);
1893             rpmte te = fsmGetTe(fsm);
1894             rpmfi fi = fsmGetFi(fsm);
1895             void * ptr;
1896             if (fsm->cpioPos > fsm->archivePos) {
1897                 fsm->archivePos = fsm->cpioPos;
1898                 ptr = rpmtsNotify(ts, te, RPMCALLBACK_INST_PROGRESS,
1899                         fsm->archivePos, fi->archiveSize);
1900             }
1901         }
1902         break;
1903     case FSM_UNDO:
1904         if (fsm->postpone)
1905             break;
1906         if (fsm->goal == FSM_PKGINSTALL) {
1907             /* XXX only erase if temp fn w suffix is in use */
1908             if (fsm->sufbuf[0] != '\0') {
1909                 if (S_ISDIR(st->st_mode)) {
1910                     (void) fsmRmdir(fsm);
1911                 } else {
1912                     (void) fsmUnlink(fsm);
1913                 }
1914             }
1915             errno = saveerrno;
1916         }
1917         if (fsm->failedFile && *fsm->failedFile == NULL)
1918             *fsm->failedFile = xstrdup(fsm->path);
1919         break;
1920     case FSM_FINI:
1921         if (!fsm->postpone && fsm->commit) {
1922             if (fsm->goal == FSM_PKGINSTALL)
1923                 rc = ((S_ISREG(st->st_mode) && st->st_nlink > 1)
1924                         ? fsmCommitLinks(fsm) : fsmNext(fsm, FSM_COMMIT));
1925             if (fsm->goal == FSM_PKGCOMMIT)
1926                 rc = fsmNext(fsm, FSM_COMMIT);
1927             if (fsm->goal == FSM_PKGERASE)
1928                 rc = fsmNext(fsm, FSM_COMMIT);
1929         }
1930         fsm->path = _free(fsm->path);
1931         fsm->opath = _free(fsm->opath);
1932         memset(st, 0, sizeof(*st));
1933         memset(ost, 0, sizeof(*ost));
1934         break;
1935     case FSM_COMMIT:
1936         /* Rename pre-existing modified or unmanaged file. */
1937         if (fsm->osuffix && fsm->diskchecked &&
1938           (fsm->exists || (fsm->goal == FSM_PKGINSTALL && S_ISREG(st->st_mode))))
1939         {
1940             char * opath = fsm->opath;
1941             char * path = fsm->path;
1942             fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1943             fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1944             rc = fsmRename(fsm);
1945             if (!rc) {
1946                 rpmlog(RPMLOG_WARNING, _("%s saved as %s\n"),
1947                                 (fsm->opath ? fsm->opath : ""),
1948                                 (fsm->path ? fsm->path : ""));
1949             }
1950             fsm->path = _free(fsm->path);
1951             fsm->path = path;
1952             fsm->opath = _free(fsm->opath);
1953             fsm->opath = opath;
1954         }
1955
1956         /* Remove erased files. */
1957         if (fsm->goal == FSM_PKGERASE) {
1958             if (fsm->action == FA_ERASE) {
1959                 rpmte te = fsmGetTe(fsm);
1960                 if (S_ISDIR(st->st_mode)) {
1961                     rc = fsmRmdir(fsm);
1962                     if (!rc) break;
1963                     switch (rc) {
1964                     case CPIOERR_ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1965                     case CPIOERR_ENOTEMPTY:
1966         /* XXX make sure that build side permits %missingok on directories. */
1967                         if (fsm->fflags & RPMFILE_MISSINGOK)
1968                             break;
1969
1970                         /* XXX common error message. */
1971                         rpmlog(
1972                             (strict_erasures ? RPMLOG_ERR : RPMLOG_DEBUG),
1973                             _("%s rmdir of %s failed: Directory not empty\n"), 
1974                                 rpmteTypeString(te), fsm->path);
1975                         break;
1976                     default:
1977                         rpmlog(
1978                             (strict_erasures ? RPMLOG_ERR : RPMLOG_DEBUG),
1979                                 _("%s rmdir of %s failed: %s\n"),
1980                                 rpmteTypeString(te), fsm->path, strerror(errno));
1981                         break;
1982                     }
1983                 } else {
1984                     rc = fsmUnlink(fsm);
1985                     if (!rc) break;
1986                     switch (rc) {
1987                     case CPIOERR_ENOENT:
1988                         if (fsm->fflags & RPMFILE_MISSINGOK)
1989                             break;
1990                     default:
1991                         rpmlog(
1992                             (strict_erasures ? RPMLOG_ERR : RPMLOG_DEBUG),
1993                                 _("%s unlink of %s failed: %s\n"),
1994                                 rpmteTypeString(te), fsm->path, strerror(errno));
1995                         break;
1996                     }
1997                 }
1998             }
1999             /* XXX Failure to remove is not (yet) cause for failure. */
2000             if (!strict_erasures) rc = 0;
2001             break;
2002         }
2003
2004         /* XXX Special case /dev/log, which shouldn't be packaged anyways */
2005         if (!S_ISSOCK(st->st_mode) && !IS_DEV_LOG(fsm->path)) {
2006             /* Rename temporary to final file name. */
2007             if (!S_ISDIR(st->st_mode) &&
2008                 (fsm->subdir || fsm->suffix || fsm->nsuffix))
2009             {
2010                 fsm->opath = fsm->path;
2011                 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
2012                 rc = fsmRename(fsm);
2013                 if (!rc && fsm->nsuffix) {
2014                     char * opath = fsmFsPath(fsm, st, NULL, NULL);
2015                     rpmlog(RPMLOG_WARNING, _("%s created as %s\n"),
2016                                 (opath ? opath : ""),
2017                                 (fsm->path ? fsm->path : ""));
2018                     opath = _free(opath);
2019                 }
2020                 fsm->opath = _free(fsm->opath);
2021             }
2022             /*
2023              * Set file security context (if not disabled).
2024              */
2025             if (!rc && !getuid()) {
2026                 rc = fsmMapFContext(fsm);
2027                 if (!rc) {
2028                     rc = fsmLsetfcon(fsm);
2029                     freecon(fsm->fcontext);     
2030                 }
2031                 fsm->fcontext = NULL;
2032             }
2033             if (S_ISLNK(st->st_mode)) {
2034                 if (!rc && !getuid())
2035                     rc = fsmLChown(fsm);
2036             } else {
2037                 if (!rc && !getuid())
2038                     rc = fsmChown(fsm);
2039                 if (!rc)
2040                     rc = fsmChmod(fsm);
2041                 if (!rc) {
2042                     time_t mtime = st->st_mtime;
2043                     rpmfi fi = fsmGetFi(fsm);
2044                     st->st_mtime = rpmfiFMtimeIndex(fi, fsm->ix);
2045                     rc = fsmUtime(fsm);
2046                     st->st_mtime = mtime;
2047                 }
2048 #if WITH_CAP
2049                 if (!rc && !S_ISDIR(st->st_mode) && !getuid()) {
2050                     rc = fsmMapFCaps(fsm);
2051                     if (!rc && fsm->fcaps) {
2052                         rc = fsmSetcap(fsm);
2053                         cap_free(fsm->fcaps);
2054                     }
2055                     fsm->fcaps = NULL;
2056                 }
2057 #endif /* WITH_CAP */
2058             }
2059         }
2060
2061         /* Notify on success. */
2062         if (!rc)                rc = fsmNext(fsm, FSM_NOTIFY);
2063         else if (fsm->failedFile && *fsm->failedFile == NULL) {
2064             *fsm->failedFile = fsm->path;
2065             fsm->path = NULL;
2066         }
2067         break;
2068     case FSM_DESTROY:
2069         fsm->path = _free(fsm->path);
2070
2071         /* Check for hard links missing from payload. */
2072         while ((fsm->li = fsm->links) != NULL) {
2073             fsm->links = fsm->li->next;
2074             fsm->li->next = NULL;
2075             if (fsm->goal == FSM_PKGINSTALL &&
2076                         fsm->commit && fsm->li->linksLeft)
2077             {
2078                 for (nlink_t i = 0 ; i < fsm->li->linksLeft; i++) {
2079                     if (fsm->li->filex[i] < 0)
2080                         continue;
2081                     rc = CPIOERR_MISSING_HARDLINK;
2082                     if (fsm->failedFile && *fsm->failedFile == NULL) {
2083                         fsm->ix = fsm->li->filex[i];
2084                         if (!fsmMapPath(fsm)) {
2085                             /* Out-of-sync hardlinks handled as sub-state */
2086                             *fsm->failedFile = fsm->path;
2087                             fsm->path = NULL;
2088                         }
2089                     }
2090                     break;
2091                 }
2092             }
2093             if (fsm->goal == FSM_PKGBUILD &&
2094                 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
2095             {
2096                 rc = CPIOERR_MISSING_HARDLINK;
2097             }
2098             fsm->li = freeHardLink(fsm->li);
2099         }
2100         fsm->ldn = _free(fsm->ldn);
2101         fsm->ldnalloc = fsm->ldnlen = 0;
2102         fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
2103         fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
2104         break;
2105     case FSM_NEXT:
2106         rc = fsmNext(fsm, FSM_POS);
2107         if (!rc)
2108             rc = cpioHeaderRead(fsm, st);       /* Read next payload header. */
2109         if (rc) break;
2110         if (rstreq(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
2111             fsm->path = _free(fsm->path);
2112             rc = CPIOERR_HDR_TRAILER;
2113         }
2114         if (!rc)
2115             rc = fsmNext(fsm, FSM_POS);
2116         break;
2117     case FSM_EAT:
2118         for (left = st->st_size; left > 0; left -= fsm->rdnb) {
2119             fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
2120             rc = fsmNext(fsm, FSM_DREAD);
2121             if (rc)
2122                 break;
2123         }
2124         break;
2125     case FSM_POS:
2126         left = (modulo - (fsm->cpioPos % modulo)) % modulo;
2127         if (left) {
2128             fsm->wrlen = left;
2129             (void) fsmNext(fsm, FSM_DREAD);
2130         }
2131         break;
2132     case FSM_PAD:
2133         left = (modulo - (fsm->cpioPos % modulo)) % modulo;
2134         if (left) {
2135             memset(fsm->rdbuf, 0, left);
2136             /* XXX DWRITE uses rdnb for I/O length. */
2137             fsm->rdnb = left;
2138             (void) fsmNext(fsm, FSM_DWRITE);
2139         }
2140         break;
2141     case FSM_DREAD:
2142         fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
2143         if (_fsm_debug && (stage & FSM_SYSCALL))
2144             rpmlog(RPMLOG_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
2145                 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
2146                 (int)fsm->wrlen, (int)fsm->rdnb);
2147         if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
2148             rc = CPIOERR_READ_FAILED;
2149         if (fsm->rdnb > 0)
2150             fsm->cpioPos += fsm->rdnb;
2151         break;
2152     case FSM_DWRITE:
2153         fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
2154         if (_fsm_debug && (stage & FSM_SYSCALL))
2155             rpmlog(RPMLOG_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
2156                 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
2157                 (int)fsm->rdnb, (int)fsm->wrnb);
2158         if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
2159             rc = CPIOERR_WRITE_FAILED;
2160         if (fsm->wrnb > 0)
2161             fsm->cpioPos += fsm->wrnb;
2162         break;
2163
2164     case FSM_ROPEN:
2165         fsm->rfd = Fopen(fsm->path, "r.ufdio");
2166         if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2167             if (fsm->rfd != NULL)       (void) fsmNext(fsm, FSM_RCLOSE);
2168             fsm->rfd = NULL;
2169             rc = CPIOERR_OPEN_FAILED;
2170             break;
2171         }
2172         if (_fsm_debug && (stage & FSM_SYSCALL))
2173             rpmlog(RPMLOG_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2174                 fsm->path, fsm->rfd, fsm->rdbuf);
2175         break;
2176     case FSM_READ:
2177         fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2178         if (_fsm_debug && (stage & FSM_SYSCALL))
2179             rpmlog(RPMLOG_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2180                 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2181         if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2182             rc = CPIOERR_READ_FAILED;
2183         break;
2184     case FSM_RCLOSE:
2185         if (fsm->rfd != NULL) {
2186             if (_fsm_debug && (stage & FSM_SYSCALL))
2187                 rpmlog(RPMLOG_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2188             (void) rpmswAdd(rpmtsOp(fsmGetTs(fsm), RPMTS_OP_DIGEST),
2189                         fdOp(fsm->rfd, FDSTAT_DIGEST));
2190             (void) Fclose(fsm->rfd);
2191             errno = saveerrno;
2192         }
2193         fsm->rfd = NULL;
2194         break;
2195     case FSM_WOPEN:
2196         fsm->wfd = Fopen(fsm->path, "w.ufdio");
2197         if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2198             if (fsm->wfd != NULL)       (void) fsmNext(fsm, FSM_WCLOSE);
2199             fsm->wfd = NULL;
2200             rc = CPIOERR_OPEN_FAILED;
2201         }
2202         if (_fsm_debug && (stage & FSM_SYSCALL))
2203             rpmlog(RPMLOG_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2204                 fsm->path, fsm->wfd, fsm->wrbuf);
2205         break;
2206     case FSM_WRITE:
2207         fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2208         if (_fsm_debug && (stage & FSM_SYSCALL))
2209             rpmlog(RPMLOG_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2210                 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2211         if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2212             rc = CPIOERR_WRITE_FAILED;
2213         break;
2214     case FSM_WCLOSE:
2215         if (fsm->wfd != NULL) {
2216             if (_fsm_debug && (stage & FSM_SYSCALL))
2217                 rpmlog(RPMLOG_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2218             (void) rpmswAdd(rpmtsOp(fsmGetTs(fsm), RPMTS_OP_DIGEST),
2219                         fdOp(fsm->wfd, FDSTAT_DIGEST));
2220             (void) Fclose(fsm->wfd);
2221             errno = saveerrno;
2222         }
2223         fsm->wfd = NULL;
2224         break;
2225
2226     default:
2227         break;
2228     }
2229
2230     if (!(stage & FSM_INTERNAL)) {
2231         fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2232     }
2233     return rc;
2234 }
2235
2236 /**
2237  * Return formatted string representation of file disposition.
2238  * @param a             file dispostion
2239  * @return              formatted string
2240  */
2241 static const char * fileActionString(rpmFileAction a)
2242 {
2243     switch (a) {
2244     case FA_UNKNOWN:    return "unknown";
2245     case FA_CREATE:     return "create";
2246     case FA_COPYOUT:    return "copyout";
2247     case FA_COPYIN:     return "copyin";
2248     case FA_BACKUP:     return "backup";
2249     case FA_SAVE:       return "save";
2250     case FA_SKIP:       return "skip";
2251     case FA_ALTNAME:    return "altname";
2252     case FA_ERASE:      return "erase";
2253     case FA_SKIPNSTATE: return "skipnstate";
2254     case FA_SKIPNETSHARED: return "skipnetshared";
2255     case FA_SKIPCOLOR:  return "skipcolor";
2256     default:            return "???";
2257     }
2258 }
2259
2260 /**
2261  * Return formatted string representation of file stages.
2262  * @param a             file stage
2263  * @return              formatted string
2264  */
2265 static const char * fileStageString(fileStage a) {
2266     switch(a) {
2267     case FSM_UNKNOWN:   return "unknown";
2268
2269     case FSM_PKGINSTALL:return "INSTALL";
2270     case FSM_PKGERASE:  return "ERASE";
2271     case FSM_PKGBUILD:  return "BUILD";
2272     case FSM_PKGCOMMIT: return "COMMIT";
2273     case FSM_PKGUNDO:   return "UNDO";
2274
2275     case FSM_CREATE:    return "create";
2276     case FSM_INIT:      return "init";
2277     case FSM_MAP:       return "map";
2278     case FSM_MKDIRS:    return "mkdirs";
2279     case FSM_RMDIRS:    return "rmdirs";
2280     case FSM_PRE:       return "pre";
2281     case FSM_PROCESS:   return "process";
2282     case FSM_POST:      return "post";
2283     case FSM_MKLINKS:   return "mklinks";
2284     case FSM_NOTIFY:    return "notify";
2285     case FSM_UNDO:      return "undo";
2286     case FSM_FINI:      return "fini";
2287     case FSM_COMMIT:    return "commit";
2288     case FSM_DESTROY:   return "destroy";
2289     case FSM_VERIFY:    return "verify";
2290
2291     case FSM_UNLINK:    return "unlink";
2292     case FSM_RENAME:    return "rename";
2293     case FSM_MKDIR:     return "mkdir";
2294     case FSM_RMDIR:     return "rmdir";
2295     case FSM_LSETFCON:  return "lsetfcon";
2296     case FSM_CHOWN:     return "chown";
2297     case FSM_LCHOWN:    return "lchown";
2298     case FSM_CHMOD:     return "chmod";
2299     case FSM_UTIME:     return "utime";
2300     case FSM_SYMLINK:   return "symlink";
2301     case FSM_LINK:      return "link";
2302     case FSM_MKFIFO:    return "mkfifo";
2303     case FSM_MKNOD:     return "mknod";
2304     case FSM_LSTAT:     return "lstat";
2305     case FSM_STAT:      return "stat";
2306     case FSM_READLINK:  return "readlink";
2307     case FSM_SETCAP:    return "setcap";
2308
2309     case FSM_NEXT:      return "next";
2310     case FSM_EAT:       return "eat";
2311     case FSM_POS:       return "pos";
2312     case FSM_PAD:       return "pad";
2313     case FSM_TRAILER:   return "trailer";
2314     case FSM_HREAD:     return "hread";
2315     case FSM_HWRITE:    return "hwrite";
2316     case FSM_DREAD:     return "Fread";
2317     case FSM_DWRITE:    return "Fwrite";
2318
2319     case FSM_ROPEN:     return "Fopen";
2320     case FSM_READ:      return "Fread";
2321     case FSM_RCLOSE:    return "Fclose";
2322     case FSM_WOPEN:     return "Fopen";
2323     case FSM_WRITE:     return "Fwrite";
2324     case FSM_WCLOSE:    return "Fclose";
2325
2326     default:            return "???";
2327     }
2328 }