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