Sanity.
[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 @*/      /* XXX void ptr args */
23 /*@access FSMI_t @*/
24 /*@access FSM_t @*/
25
26 /*@access rpmfi @*/
27
28 #define alloca_strdup(_s)       strcpy(alloca(strlen(_s)+1), (_s))
29
30 #define _FSM_DEBUG      0
31 /*@unchecked@*/
32 int _fsm_debug = _FSM_DEBUG;
33
34 /* XXX Failure to remove is not (yet) cause for failure. */
35 /*@-exportlocal -exportheadervar@*/
36 /*@unchecked@*/
37 int strict_erasures = 0;
38 /*@=exportlocal =exportheadervar@*/
39
40 rpmts fsmGetTs(const FSM_t fsm) {
41     const FSMI_t iter = fsm->iter;
42     /*@-compdef -refcounttrans -retexpose -usereleased @*/
43     return (iter ? iter->ts : NULL);
44     /*@=compdef =refcounttrans =retexpose =usereleased @*/
45 }
46
47 rpmfi fsmGetFi(const FSM_t fsm)
48 {
49     const FSMI_t iter = fsm->iter;
50     /*@-compdef -refcounttrans -retexpose -usereleased @*/
51     return (iter ? iter->fi : NULL);
52     /*@=compdef =refcounttrans =retexpose =usereleased @*/
53 }
54
55 #define SUFFIX_RPMORIG  ".rpmorig"
56 #define SUFFIX_RPMSAVE  ".rpmsave"
57 #define SUFFIX_RPMNEW   ".rpmnew"
58
59 /** \ingroup payload
60  * Build path to file from file info, ornamented with subdir and suffix.
61  * @param fsm           file state machine data
62  * @param st            file stat info
63  * @param subdir        subdir to use (NULL disables)
64  * @param suffix        suffix to use (NULL disables)
65  * @retval              path to file
66  */
67 static /*@only@*//*@null@*/
68 const char * fsmFsPath(/*@special@*/ /*@null@*/ const FSM_t fsm,
69                 /*@null@*/ const struct stat * st,
70                 /*@null@*/ const char * subdir,
71                 /*@null@*/ const char * suffix)
72         /*@uses fsm->dirName, fsm->baseName */
73         /*@*/
74 {
75     const char * s = NULL;
76
77     if (fsm) {
78         int nb;
79         char * t;
80         nb = strlen(fsm->dirName) +
81             (st && !S_ISDIR(st->st_mode) ? (subdir ? strlen(subdir) : 0) : 0) +
82             (st && !S_ISDIR(st->st_mode) ? (suffix ? strlen(suffix) : 0) : 0) +
83             strlen(fsm->baseName) + 1;
84 /*@-boundswrite@*/
85         s = t = xmalloc(nb);
86         t = stpcpy(t, fsm->dirName);
87         if (st && !S_ISDIR(st->st_mode))
88             if (subdir) t = stpcpy(t, subdir);
89         t = stpcpy(t, fsm->baseName);
90         if (st && !S_ISDIR(st->st_mode))
91             if (suffix) t = stpcpy(t, suffix);
92 /*@=boundswrite@*/
93     }
94     return s;
95 }
96
97 /** \ingroup payload
98  * Destroy file info iterator.
99  * @param p             file info iterator
100  * @retval              NULL always
101  */
102 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/ void * p)
103         /*@globals fileSystem @*/
104         /*@modifies fileSystem @*/
105 {
106     FSMI_t iter = p;
107     if (iter) {
108         iter->ts = rpmtsFree(iter->ts);
109         iter->fi = rpmfiUnlink(iter->fi, "mapIterator");
110     }
111     return _free(p);
112 }
113
114 /** \ingroup payload
115  * Create file info iterator.
116  * @param ts            transaction set
117  * @param fi            transaction element file info
118  * @return              file info iterator
119  */
120 static void *
121 mapInitIterator(rpmts ts, rpmfi fi)
122         /*@modifies ts, fi @*/
123 {
124     FSMI_t iter = NULL;
125
126     iter = xcalloc(1, sizeof(*iter));
127     iter->ts = rpmtsLink(ts, "mapIterator");
128     iter->fi = rpmfiLink(fi, "mapIterator");
129     iter->reverse = (rpmteType(fi->te) == TR_REMOVED && fi->action != FA_COPYOUT);
130     iter->i = (iter->reverse ? (fi->fc - 1) : 0);
131     iter->isave = iter->i;
132     return iter;
133 }
134
135 /** \ingroup payload
136  * Return next index into file info.
137  * @param a             file info iterator
138  * @return              next index, -1 on termination
139  */
140 static int mapNextIterator(/*@null@*/ void * a)
141         /*@*/
142 {
143     FSMI_t iter = a;
144     int i = -1;
145
146     if (iter) {
147         const rpmfi fi = iter->fi;
148         if (iter->reverse) {
149             if (iter->i >= 0)   i = iter->i--;
150         } else {
151             if (iter->i < fi->fc)       i = iter->i++;
152         }
153         iter->isave = i;
154     }
155     return i;
156 }
157
158 /** \ingroup payload
159  */
160 /*@-boundsread@*/
161 static int cpioStrCmp(const void * a, const void * b)
162         /*@*/
163 {
164     const char * afn = *(const char **)a;
165     const char * bfn = *(const char **)b;
166
167     /* XXX Some 4+ year old rpm packages have basename only in payloads. */
168 #ifdef  VERY_OLD_BUGGY_RPM_PACKAGES
169     if (strchr(afn, '/') == NULL)
170         bfn = strrchr(bfn, '/') + 1;
171 #endif
172
173     /* Match rpm-4.0 payloads with ./ prefixes. */
174     if (afn[0] == '.' && afn[1] == '/') afn += 2;
175     if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
176
177     /* If either path is absolute, make it relative. */
178     if (afn[0] == '/')  afn += 1;
179     if (bfn[0] == '/')  bfn += 1;
180
181     return strcmp(afn, bfn);
182 }
183 /*@=boundsread@*/
184
185 /** \ingroup payload
186  * Locate archive path in file info.
187  * @param iter          file info iterator
188  * @param fsmPath       archive path
189  * @return              index into file info, -1 if archive path was not found
190  */
191 /*@-boundsread@*/
192 static int mapFind(/*@null@*/ FSMI_t iter, const char * fsmPath)
193         /*@modifies iter @*/
194 {
195     int ix = -1;
196
197     if (iter) {
198         const rpmfi fi = iter->fi;
199         if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
200             const char ** p = NULL;
201
202 /*@-boundswrite@*/
203             if (fi->apath != NULL)
204                 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath),
205                         cpioStrCmp);
206 /*@=boundswrite@*/
207             if (p) {
208                 iter->i = p - fi->apath;
209                 ix = mapNextIterator(iter);
210             }
211         }
212     }
213     return ix;
214 }
215 /*@=boundsread@*/
216
217 /** \ingroup payload
218  * Directory name iterator.
219  */
220 typedef struct dnli_s {
221     rpmfi fi;
222 /*@only@*/ /*@null@*/
223     char * active;
224     int reverse;
225     int isave;
226     int i;
227 } * DNLI_t;
228
229 /** \ingroup payload
230  * Destroy directory name iterator.
231  * @param a             directory name iterator
232  * @retval              NULL always
233  */
234 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * a)
235         /*@modifies a @*/
236 {
237     if (a) {
238         DNLI_t dnli = (void *)a;
239         if (dnli->active) free(dnli->active);
240     }
241     return _free(a);
242 }
243
244 /** \ingroup payload
245  */
246 static inline int dnlCount(const DNLI_t dnli)
247         /*@*/
248 {
249     return (dnli ? dnli->fi->dc : 0);
250 }
251
252 /** \ingroup payload
253  */
254 static inline int dnlIndex(const DNLI_t dnli)
255         /*@*/
256 {
257     return (dnli ? dnli->isave : -1);
258 }
259
260 /** \ingroup payload
261  * Create directory name iterator.
262  * @param fsm           file state machine data
263  * @param reverse       traverse directory names in reverse order?
264  * @return              directory name iterator
265  */
266 /*@-boundsread@*/
267 /*@-usereleased@*/
268 static /*@only@*/ void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
269                 int reverse)
270         /*@uses fsm->iter @*/ 
271         /*@*/
272 {
273     rpmfi fi = fsmGetFi(fsm);
274     DNLI_t dnli;
275     int i, j;
276
277     if (fi == NULL)
278         return NULL;
279     dnli = xcalloc(1, sizeof(*dnli));
280     dnli->fi = fi;
281     dnli->reverse = reverse;
282     /*@-branchstate@*/
283     dnli->i = (reverse ? fi->dc : 0);
284     /*@=branchstate@*/
285
286     if (fi->dc) {
287         dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
288
289         /* Identify parent directories not skipped. */
290 /*@-boundswrite@*/
291         for (i = 0; i < fi->fc; i++)
292             if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
293 /*@=boundswrite@*/
294
295         /* Exclude parent directories that are explicitly included. */
296         for (i = 0; i < fi->fc; i++) {
297             int dil, dnlen, bnlen;
298
299             if (!S_ISDIR(fi->fmodes[i]))
300                 continue;
301
302             dil = fi->dil[i];
303             dnlen = strlen(fi->dnl[dil]);
304             bnlen = strlen(fi->bnl[i]);
305
306             for (j = 0; j < fi->dc; j++) {
307                 const char * dnl;
308                 int jlen;
309
310                 if (!dnli->active[j] || j == dil)
311                     /*@innercontinue@*/ continue;
312                 dnl = fi->dnl[j];
313                 jlen = strlen(dnl);
314                 if (jlen != (dnlen+bnlen+1))
315                     /*@innercontinue@*/ continue;
316                 if (strncmp(dnl, fi->dnl[dil], dnlen))
317                     /*@innercontinue@*/ continue;
318                 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen))
319                     /*@innercontinue@*/ continue;
320                 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
321                     /*@innercontinue@*/ continue;
322                 /* This directory is included in the package. */
323 /*@-boundswrite@*/
324                 dnli->active[j] = 0;
325 /*@=boundswrite@*/
326                 /*@innerbreak@*/ break;
327             }
328         }
329
330         /* Print only once per package. */
331         if (!reverse) {
332             j = 0;
333             for (i = 0; i < fi->dc; i++) {
334                 if (!dnli->active[i]) continue;
335                 if (j == 0) {
336                     j = 1;
337                     rpmMessage(RPMMESS_DEBUG,
338         _("========== Directories not explictly included in package:\n"));
339                 }
340                 rpmMessage(RPMMESS_DEBUG, _("%10d %s\n"), i, fi->dnl[i]);
341             }
342             if (j)
343                 rpmMessage(RPMMESS_DEBUG, "==========\n");
344         }
345     }
346     return dnli;
347 }
348 /*@=usereleased@*/
349 /*@=boundsread@*/
350
351 /** \ingroup payload
352  * Return next directory name (from file info).
353  * @param dnli          directory name iterator
354  * @return              next directory name
355  */
356 /*@-boundsread@*/
357 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
358         /*@modifies dnli @*/
359 {
360     const char * dn = NULL;
361
362     if (dnli) {
363         rpmfi fi = dnli->fi;
364         int i = -1;
365
366         if (dnli->active)
367         do {
368             i = (!dnli->reverse ? dnli->i++ : --dnli->i);
369         } while (i >= 0 && i < fi->dc && !dnli->active[i]);
370
371         if (i >= 0 && i < fi->dc)
372             dn = fi->dnl[i];
373         else
374             i = -1;
375         dnli->isave = i;
376     }
377     return dn;
378 }
379 /*@=boundsread@*/
380
381 /** \ingroup payload
382  * Save hard link in chain.
383  * @param fsm           file state machine data
384  * @return              Is chain only partially filled?
385  */
386 /*@-boundsread@*/
387 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
388         /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
389         /*@defines fsm->li @*/
390         /*@releases fsm->path @*/
391         /*@globals errno, fileSystem, internalState @*/
392         /*@modifies fsm, errno, fileSystem, internalState @*/
393 {
394     struct stat * st = &fsm->sb;
395     int rc = 0;
396     int ix = -1;
397     int j;
398
399     /* Find hard link set. */
400     /*@-branchstate@*/
401     for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
402         if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
403             break;
404     }
405     /*@=branchstate@*/
406
407     /* New hard link encountered, add new link to set. */
408 /*@-boundswrite@*/
409     /*@-branchstate@*/
410     if (fsm->li == NULL) {
411         fsm->li = xcalloc(1, sizeof(*fsm->li));
412         fsm->li->next = NULL;
413         fsm->li->sb = *st;      /* structure assignment */
414         fsm->li->nlink = st->st_nlink;
415         fsm->li->linkIndex = fsm->ix;
416         fsm->li->createdPath = -1;
417
418         fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
419         memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
420         fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
421
422         if (fsm->goal == FSM_PKGBUILD)
423             fsm->li->linksLeft = st->st_nlink;
424         if (fsm->goal == FSM_PKGINSTALL)
425             fsm->li->linksLeft = 0;
426
427         /*@-kepttrans@*/
428         fsm->li->next = fsm->links;
429         /*@=kepttrans@*/
430         fsm->links = fsm->li;
431     }
432     /*@=branchstate@*/
433 /*@=boundswrite@*/
434
435     if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
436 /*@-boundswrite@*/
437     fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
438     /*@-observertrans -dependenttrans@*/
439     fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
440     /*@=observertrans =dependenttrans@*/
441 /*@=boundswrite@*/
442     if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
443
444     if (fsm->goal == FSM_PKGBUILD)
445         return (fsm->li->linksLeft > 0);
446
447     if (fsm->goal != FSM_PKGINSTALL)
448         return 0;
449
450     if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
451         return 1;
452
453     /* Here come the bits, time to choose a non-skipped file name. */
454     {   rpmfi fi = fsmGetFi(fsm);
455
456         for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
457             ix = fsm->li->filex[j];
458             if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
459                 continue;
460             break;
461         }
462     }
463
464     /* Are all links skipped or not encountered yet? */
465     if (ix < 0 || j < 0)
466         return 1;       /* XXX W2DO? */
467
468     /* Save the non-skipped file name and map index. */
469     fsm->li->linkIndex = j;
470     fsm->path = _free(fsm->path);
471     fsm->ix = ix;
472     rc = fsmStage(fsm, FSM_MAP);
473     return rc;
474 }
475 /*@=boundsread@*/
476
477 /** \ingroup payload
478  * Destroy set of hard links.
479  * @param li            set of hard links
480  * @return              NULL always
481  */
482 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink_s * li)
483         /*@modifies li @*/
484 {
485     if (li) {
486         li->nsuffix = _free(li->nsuffix);       /* XXX elements are shared */
487         li->filex = _free(li->filex);
488     }
489     return _free(li);
490 }
491
492 FSM_t newFSM(void)
493 {
494     FSM_t fsm = xcalloc(1, sizeof(*fsm));
495     return fsm;
496 }
497
498 FSM_t freeFSM(FSM_t fsm)
499 {
500     if (fsm) {
501         fsm->path = _free(fsm->path);
502         /*@-branchstate@*/
503         while ((fsm->li = fsm->links) != NULL) {
504             fsm->links = fsm->li->next;
505             fsm->li->next = NULL;
506             fsm->li = freeHardLink(fsm->li);
507         }
508         /*@=branchstate@*/
509         fsm->dnlx = _free(fsm->dnlx);
510         fsm->ldn = _free(fsm->ldn);
511         fsm->iter = mapFreeIterator(fsm->iter);
512     }
513     return _free(fsm);
514 }
515
516 int fsmSetup(FSM_t fsm, fileStage goal,
517                 const rpmts ts, const rpmfi fi, FD_t cfd,
518                 unsigned int * archiveSize, const char ** failedFile)
519 {
520     size_t pos = 0;
521     int rc, ec = 0;
522
523     fsm->goal = goal;
524     if (cfd != NULL) {
525         fsm->cfd = fdLink(cfd, "persist (fsm)");
526         pos = fdGetCpioPos(fsm->cfd);
527         fdSetCpioPos(fsm->cfd, 0);
528     }
529     fsm->iter = mapInitIterator(ts, fi);
530
531     if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
532         void * ptr;
533         fi->archivePos = 0;
534         ptr = rpmtsNotify(ts, fi->te,
535                 RPMCALLBACK_INST_START, fi->archivePos, fi->archiveSize);
536     }
537
538 /*@-boundswrite@*/
539     /*@-assignexpose@*/
540     fsm->archiveSize = archiveSize;
541     if (fsm->archiveSize)
542         *fsm->archiveSize = 0;
543     fsm->failedFile = failedFile;
544     if (fsm->failedFile)
545         *fsm->failedFile = NULL;
546     /*@=assignexpose@*/
547 /*@=boundswrite@*/
548
549     memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
550     if (fsm->goal == FSM_PKGINSTALL) {
551         if (ts && rpmtsGetTid(ts) > 0)
552             sprintf(fsm->sufbuf, ";%08x", (unsigned)rpmtsGetTid(ts));
553     }
554
555     ec = fsm->rc = 0;
556     rc = fsmStage(fsm, FSM_CREATE);
557     if (rc && !ec) ec = rc;
558
559     rc = fsmStage(fsm, fsm->goal);
560     if (rc && !ec) ec = rc;
561
562 /*@-boundswrite@*/
563     if (fsm->archiveSize && ec == 0)
564         *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
565 /*@=boundswrite@*/
566
567    return ec;
568 }
569
570 int fsmTeardown(FSM_t fsm)
571 {
572     int rc = fsm->rc;
573
574     if (!rc)
575         rc = fsmStage(fsm, FSM_DESTROY);
576
577     fsm->iter = mapFreeIterator(fsm->iter);
578     if (fsm->cfd != NULL) {
579         fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
580         fsm->cfd = NULL;
581     }
582     fsm->failedFile = NULL;
583     return rc;
584 }
585
586 int fsmMapPath(FSM_t fsm)
587 {
588     rpmfi fi = fsmGetFi(fsm);   /* XXX const except for fstates */
589     int rc = 0;
590     int i;
591
592     fsm->osuffix = NULL;
593     fsm->nsuffix = NULL;
594     fsm->astriplen = 0;
595     fsm->action = FA_UNKNOWN;
596     fsm->mapFlags = 0;
597
598     i = fsm->ix;
599     if (fi && i >= 0 && i < fi->fc) {
600
601 /*@-boundsread@*/
602         fsm->astriplen = fi->astriplen;
603         fsm->action = (fi->actions ? fi->actions[i] : fi->action);
604         fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
605         fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
606
607         /* src rpms have simple base name in payload. */
608         fsm->dirName = fi->dnl[fi->dil[i]];
609         fsm->baseName = fi->bnl[i];
610 /*@=boundsread@*/
611
612 /*@-boundswrite@*/
613         switch (fsm->action) {
614         case FA_SKIP:
615             break;
616         case FA_UNKNOWN:
617             break;
618
619         case FA_COPYOUT:
620             break;
621         case FA_COPYIN:
622         case FA_CREATE:
623 assert(rpmteType(fi->te) == TR_ADDED);
624             break;
625
626         case FA_SKIPNSTATE:
627             if (fi->fstates && rpmteType(fi->te) == TR_ADDED)
628                 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
629             break;
630
631         case FA_SKIPNETSHARED:
632             if (fi->fstates && rpmteType(fi->te) == TR_ADDED)
633                 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
634             break;
635
636         case FA_SKIPCOLOR:
637             if (fi->fstates && rpmteType(fi->te) == TR_ADDED)
638                 fi->fstates[i] = RPMFILE_STATE_WRONGCOLOR;
639             break;
640
641         case FA_BACKUP:
642             if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
643             switch (rpmteType(fi->te)) {
644             case TR_ADDED:
645                 fsm->osuffix = SUFFIX_RPMORIG;
646                 /*@innerbreak@*/ break;
647             case TR_REMOVED:
648                 fsm->osuffix = SUFFIX_RPMSAVE;
649                 /*@innerbreak@*/ break;
650             }
651             break;
652
653         case FA_ALTNAME:
654 assert(rpmteType(fi->te) == TR_ADDED);
655             if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
656                 fsm->nsuffix = SUFFIX_RPMNEW;
657             break;
658
659         case FA_SAVE:
660 assert(rpmteType(fi->te) == TR_ADDED);
661             if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
662                 fsm->osuffix = SUFFIX_RPMSAVE;
663             break;
664         case FA_ERASE:
665 #if 0   /* XXX is this a genhdlist fix? */
666             assert(rpmteType(fi->te) == TR_REMOVED);
667 #endif
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 errno, fileSystem, internalState @*/
746         /*@modifies fsm, errno, 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 errno, fileSystem, internalState @*/
813         /*@modifies fsm, errno, 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 != NULL)
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 errno, fileSystem, internalState @*/
953         /*@modifies fsm, errno, 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 errno, fileSystem, internalState @*/
1001         /*@modifies fsm, errno, 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 errno, fileSystem, internalState @*/
1066         /*@modifies fsm, errno, 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 errno, fileSystem, internalState @*/
1112         /*@modifies fsm, errno, 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 errno, fileSystem, internalState @*/
1169         /*@modifies fsm, errno, 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         /*@globals errno, fileSystem, internalState @*/
1282         /*@modifies fsm, errno, fileSystem, internalState @*/
1283 {
1284     int saveerrno = errno;
1285     int rc = 0;
1286
1287     if (fsm->path != NULL) {
1288         int saveernno = errno;
1289         rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1290                         ? FSM_LSTAT : FSM_STAT));
1291         if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1292             errno = saveerrno;
1293             rc = 0;
1294             fsm->exists = 0;
1295         } else if (rc == 0) {
1296             fsm->exists = 1;
1297         }
1298     } else {
1299         /* Skip %ghost files on build. */
1300         fsm->exists = 0;
1301     }
1302     return rc;
1303 }
1304 #endif
1305
1306 #define IS_DEV_LOG(_x)  \
1307         ((_x) != NULL && strlen(_x) >= (sizeof("/dev/log")-1) && \
1308         !strncmp((_x), "/dev/log", sizeof("/dev/log")-1) && \
1309         ((_x)[sizeof("/dev/log")-1] == '\0' || \
1310          (_x)[sizeof("/dev/log")-1] == ';'))
1311
1312 /*@-boundsread@*/
1313 /*@-compmempass@*/
1314 int fsmStage(FSM_t fsm, fileStage stage)
1315 {
1316 #ifdef  UNUSED
1317     fileStage prevStage = fsm->stage;
1318     const char * const prev = fileStageString(prevStage);
1319 #endif
1320     static int modulo = 4;
1321     const char * const cur = fileStageString(stage);
1322     struct stat * st = &fsm->sb;
1323     struct stat * ost = &fsm->osb;
1324     int saveerrno = errno;
1325     int rc = fsm->rc;
1326     size_t left;
1327     int i;
1328
1329 #define _fafilter(_a)   \
1330     (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1331         ? fileActionString(_a) : "")
1332
1333     if (stage & FSM_DEAD) {
1334         /* do nothing */
1335     } else if (stage & FSM_INTERNAL) {
1336         if (_fsm_debug && !(stage & FSM_SYSCALL))
1337             rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1338                 cur,
1339                 (unsigned)st->st_mode, (int)st->st_nlink,
1340                 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1341                 (fsm->path ? fsm->path : ""),
1342                 _fafilter(fsm->action));
1343     } else {
1344         fsm->stage = stage;
1345         if (_fsm_debug || !(stage & FSM_VERBOSE))
1346             rpmMessage(RPMMESS_DEBUG, "%-8s  %06o%3d (%4d,%4d)%10d %s %s\n",
1347                 cur,
1348                 (unsigned)st->st_mode, (int)st->st_nlink,
1349                 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1350                 (fsm->path ? fsm->path + fsm->astriplen : ""),
1351                 _fafilter(fsm->action));
1352     }
1353 #undef  _fafilter
1354
1355     /*@-branchstate@*/
1356     switch (stage) {
1357     case FSM_UNKNOWN:
1358         break;
1359     case FSM_PKGINSTALL:
1360         while (1) {
1361             /* Clean fsm, free'ing memory. Read next archive header. */
1362             rc = fsmStage(fsm, FSM_INIT);
1363
1364             /* Exit on end-of-payload. */
1365             if (rc == CPIOERR_HDR_TRAILER) {
1366                 rc = 0;
1367                 /*@loopbreak@*/ break;
1368             }
1369
1370             /* Exit on error. */
1371             if (rc) {
1372                 fsm->postpone = 1;
1373                 (void) fsmStage(fsm, FSM_UNDO);
1374                 /*@loopbreak@*/ break;
1375             }
1376
1377             /* Extract file from archive. */
1378             rc = fsmStage(fsm, FSM_PROCESS);
1379             if (rc) {
1380                 (void) fsmStage(fsm, FSM_UNDO);
1381                 /*@loopbreak@*/ break;
1382             }
1383
1384             /* Notify on success. */
1385             (void) fsmStage(fsm, FSM_NOTIFY);
1386
1387             rc = fsmStage(fsm, FSM_FINI);
1388             if (rc) {
1389                 /*@loopbreak@*/ break;
1390             }
1391         }
1392         break;
1393     case FSM_PKGERASE:
1394     case FSM_PKGCOMMIT:
1395         while (1) {
1396             /* Clean fsm, free'ing memory. */
1397             rc = fsmStage(fsm, FSM_INIT);
1398
1399             /* Exit on end-of-payload. */
1400             if (rc == CPIOERR_HDR_TRAILER) {
1401                 rc = 0;
1402                 /*@loopbreak@*/ break;
1403             }
1404
1405             /* Rename/erase next item. */
1406             if (fsmStage(fsm, FSM_FINI))
1407                 /*@loopbreak@*/ break;
1408         }
1409         break;
1410     case FSM_PKGBUILD:
1411         while (1) {
1412
1413             rc = fsmStage(fsm, FSM_INIT);
1414
1415             /* Exit on end-of-payload. */
1416             if (rc == CPIOERR_HDR_TRAILER) {
1417                 rc = 0;
1418                 /*@loopbreak@*/ break;
1419             }
1420
1421             /* Exit on error. */
1422             if (rc) {
1423                 fsm->postpone = 1;
1424                 (void) fsmStage(fsm, FSM_UNDO);
1425                 /*@loopbreak@*/ break;
1426             }
1427
1428             /* Copy file into archive. */
1429             rc = fsmStage(fsm, FSM_PROCESS);
1430             if (rc) {
1431                 (void) fsmStage(fsm, FSM_UNDO);
1432                 /*@loopbreak@*/ break;
1433             }
1434
1435             /* Notify on success. */
1436             (void) fsmStage(fsm, FSM_NOTIFY);
1437
1438             if (fsmStage(fsm, FSM_FINI))
1439                 /*@loopbreak@*/ break;
1440         }
1441
1442         /* Flush partial sets of hard linked files. */
1443         if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1444             int nlink, j;
1445             while ((fsm->li = fsm->links) != NULL) {
1446                 fsm->links = fsm->li->next;
1447                 fsm->li->next = NULL;
1448
1449                 /* Re-calculate link count for archive header. */
1450                 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1451                     if (fsm->li->filex[i] < 0)
1452                         /*@innercontinue@*/ continue;
1453                     nlink++;
1454                     if (j == -1) j = i;
1455                 }
1456                 /* XXX force the contents out as well. */
1457 /*@-boundswrite@*/
1458                 if (j != 0) {
1459                     fsm->li->filex[0] = fsm->li->filex[j];
1460                     fsm->li->filex[j] = -1;
1461                 }
1462 /*@=boundswrite@*/
1463                 fsm->li->sb.st_nlink = nlink;
1464
1465                 fsm->sb = fsm->li->sb;  /* structure assignment */
1466                 fsm->osb = fsm->sb;     /* structure assignment */
1467
1468                 if (!rc) rc = writeLinkedFile(fsm);
1469
1470                 fsm->li = freeHardLink(fsm->li);
1471             }
1472         }
1473
1474         if (!rc)
1475             rc = fsmStage(fsm, FSM_TRAILER);
1476
1477         break;
1478     case FSM_CREATE:
1479         {   rpmts ts = fsmGetTs(fsm);
1480 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1481             fsm->commit = ((ts && (rpmtsFlags(ts) & _tsmask) &&
1482                         fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1483 #undef _tsmask
1484         }
1485         fsm->path = _free(fsm->path);
1486         fsm->opath = _free(fsm->opath);
1487         fsm->dnlx = _free(fsm->dnlx);
1488
1489         fsm->ldn = _free(fsm->ldn);
1490         fsm->ldnalloc = fsm->ldnlen = 0;
1491
1492         fsm->rdsize = fsm->wrsize = 0;
1493         fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1494         fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1495         if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1496             fsm->rdsize = 8 * BUFSIZ;
1497             fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1498             fsm->wrsize = 8 * BUFSIZ;
1499             fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1500         }
1501
1502         fsm->mkdirsdone = 0;
1503         fsm->ix = -1;
1504         fsm->links = NULL;
1505         fsm->li = NULL;
1506         errno = 0;      /* XXX get rid of EBADF */
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                 errno = saveerrno;
1575                 rc = 0;
1576                 fsm->exists = 0;
1577             } else if (rc == 0) {
1578                 fsm->exists = 1;
1579             }
1580         } else {
1581             /* Skip %ghost files on build. */
1582             fsm->exists = 0;
1583         }
1584 #endif
1585         fsm->diskchecked = 1;
1586         if (rc) break;
1587
1588         /* On non-install, the disk file stat is what's remapped. */
1589 /*@-boundswrite@*/
1590         if (fsm->goal != FSM_PKGINSTALL)
1591             *st = *ost;                 /* structure assignment */
1592 /*@=boundswrite@*/
1593
1594         /* Remap file perms, owner, and group. */
1595         rc = fsmMapAttrs(fsm);
1596         if (rc) break;
1597
1598         fsm->postpone = XFA_SKIPPING(fsm->action);
1599         if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1600             /*@-evalorder@*/ /* FIX: saveHardLink can modify fsm */
1601             if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1602                 fsm->postpone = saveHardLink(fsm);
1603             /*@=evalorder@*/
1604         }
1605         break;
1606     case FSM_PRE:
1607         break;
1608     case FSM_MAP:
1609         rc = fsmMapPath(fsm);
1610         break;
1611     case FSM_MKDIRS:
1612         rc = fsmMkdirs(fsm);
1613         break;
1614     case FSM_RMDIRS:
1615         if (fsm->dnlx)
1616             rc = fsmRmdirs(fsm);
1617         break;
1618     case FSM_PROCESS:
1619         if (fsm->postpone) {
1620             if (fsm->goal == FSM_PKGINSTALL)
1621                 rc = fsmStage(fsm, FSM_EAT);
1622             break;
1623         }
1624
1625         if (fsm->goal == FSM_PKGBUILD) {
1626             if (fsm->fflags & RPMFILE_GHOST) /* XXX Don't if %ghost file. */
1627                 break;
1628             if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1629                 struct hardLink_s * li, * prev;
1630
1631 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1632                 rc = writeLinkedFile(fsm);
1633                 if (rc) break;  /* W2DO? */
1634
1635                 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1636                      if (li == fsm->li)
1637                         /*@loopbreak@*/ break;
1638
1639                 if (prev == NULL)
1640                     fsm->links = fsm->li->next;
1641                 else
1642                     prev->next = fsm->li->next;
1643                 fsm->li->next = NULL;
1644                 fsm->li = freeHardLink(fsm->li);
1645             } else {
1646                 rc = writeFile(fsm, 1);
1647             }
1648             break;
1649         }
1650
1651         if (fsm->goal != FSM_PKGINSTALL)
1652             break;
1653
1654         if (S_ISREG(st->st_mode)) {
1655             const char * path = fsm->path;
1656             if (fsm->osuffix)
1657                 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1658             rc = fsmStage(fsm, FSM_VERIFY);
1659
1660             if (rc == 0 && fsm->osuffix) {
1661                 const char * opath = fsm->opath;
1662                 fsm->opath = fsm->path;
1663                 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1664                 rc = fsmStage(fsm, FSM_RENAME);
1665                 if (!rc)
1666                     rpmMessage(RPMMESS_WARNING,
1667                         _("%s saved as %s\n"), fsm->opath, fsm->path);
1668                 fsm->path = _free(fsm->path);
1669                 fsm->opath = opath;
1670             }
1671
1672             /*@-dependenttrans@*/
1673             fsm->path = path;
1674             /*@=dependenttrans@*/
1675             if (rc != CPIOERR_LSTAT_FAILED) return rc;
1676             rc = expandRegular(fsm);
1677         } else if (S_ISDIR(st->st_mode)) {
1678             mode_t st_mode = st->st_mode;
1679             rc = fsmStage(fsm, FSM_VERIFY);
1680             if (rc == CPIOERR_LSTAT_FAILED) {
1681                 st->st_mode &= ~07777;          /* XXX abuse st->st_mode */
1682                 st->st_mode |=  00700;
1683                 rc = fsmStage(fsm, FSM_MKDIR);
1684                 st->st_mode = st_mode;          /* XXX restore st->st_mode */
1685             }
1686         } else if (S_ISLNK(st->st_mode)) {
1687             const char * opath = fsm->opath;
1688
1689             if ((st->st_size + 1) > fsm->rdsize) {
1690                 rc = CPIOERR_HDR_SIZE;
1691                 break;
1692             }
1693
1694             fsm->wrlen = st->st_size;
1695             rc = fsmStage(fsm, FSM_DREAD);
1696             if (!rc && fsm->rdnb != fsm->wrlen)
1697                 rc = CPIOERR_READ_FAILED;
1698             if (rc) break;
1699
1700 /*@-boundswrite@*/
1701             fsm->wrbuf[st->st_size] = '\0';
1702 /*@=boundswrite@*/
1703             /* XXX symlink(fsm->opath, fsm->path) */
1704             /*@-dependenttrans@*/
1705             fsm->opath = fsm->wrbuf;
1706             /*@=dependenttrans@*/
1707             rc = fsmStage(fsm, FSM_VERIFY);
1708             if (rc == CPIOERR_LSTAT_FAILED)
1709                 rc = fsmStage(fsm, FSM_SYMLINK);
1710             fsm->opath = opath;         /* XXX restore fsm->path */
1711         } else if (S_ISFIFO(st->st_mode)) {
1712             mode_t st_mode = st->st_mode;
1713             /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1714             rc = fsmStage(fsm, FSM_VERIFY);
1715             if (rc == CPIOERR_LSTAT_FAILED) {
1716                 st->st_mode = 0000;             /* XXX abuse st->st_mode */
1717                 rc = fsmStage(fsm, FSM_MKFIFO);
1718                 st->st_mode = st_mode;  /* XXX restore st->st_mode */
1719             }
1720         } else if (S_ISCHR(st->st_mode) ||
1721                    S_ISBLK(st->st_mode) ||
1722     /*@-unrecog@*/ S_ISSOCK(st->st_mode) /*@=unrecog@*/)
1723         {
1724             rc = fsmStage(fsm, FSM_VERIFY);
1725             if (rc == CPIOERR_LSTAT_FAILED)
1726                 rc = fsmStage(fsm, FSM_MKNOD);
1727         } else {
1728             /* XXX Special case /dev/log, which shouldn't be packaged anyways */
1729             if (!IS_DEV_LOG(fsm->path))
1730                 rc = CPIOERR_UNKNOWN_FILETYPE;
1731         }
1732         if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1733             fsm->li->createdPath = fsm->li->linkIndex;
1734             rc = fsmMakeLinks(fsm);
1735         }
1736         break;
1737     case FSM_POST:
1738         break;
1739     case FSM_MKLINKS:
1740         rc = fsmMakeLinks(fsm);
1741         break;
1742     case FSM_NOTIFY:            /* XXX move from fsm to psm -> tsm */
1743         if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1744             rpmts ts = fsmGetTs(fsm);
1745             rpmfi fi = fsmGetFi(fsm);
1746             void * ptr;
1747             unsigned int archivePos = fdGetCpioPos(fsm->cfd);
1748             if (archivePos > fi->archivePos) {
1749                 fi->archivePos = archivePos;
1750                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_PROGRESS,
1751                         fi->archivePos, fi->archiveSize);
1752             }
1753         }
1754         break;
1755     case FSM_UNDO:
1756         if (fsm->postpone)
1757             break;
1758         if (fsm->goal == FSM_PKGINSTALL) {
1759             (void) fsmStage(fsm,
1760                 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1761
1762 #ifdef  NOTYET  /* XXX remove only dirs just created, not all. */
1763             if (fsm->dnlx)
1764                 (void) fsmStage(fsm, FSM_RMDIRS);
1765 #endif
1766             errno = saveerrno;
1767         }
1768 /*@-boundswrite@*/
1769         if (fsm->failedFile && *fsm->failedFile == NULL)
1770             *fsm->failedFile = xstrdup(fsm->path);
1771 /*@=boundswrite@*/
1772         break;
1773     case FSM_FINI:
1774         if (!fsm->postpone && fsm->commit) {
1775             if (fsm->goal == FSM_PKGINSTALL)
1776                 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1777                         ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1778             if (fsm->goal == FSM_PKGCOMMIT)
1779                 rc = fsmStage(fsm, FSM_COMMIT);
1780             if (fsm->goal == FSM_PKGERASE)
1781                 rc = fsmStage(fsm, FSM_COMMIT);
1782         }
1783         fsm->path = _free(fsm->path);
1784         fsm->opath = _free(fsm->opath);
1785 /*@-boundswrite@*/
1786         memset(st, 0, sizeof(*st));
1787         memset(ost, 0, sizeof(*ost));
1788 /*@=boundswrite@*/
1789         break;
1790     case FSM_COMMIT:
1791         /* Rename pre-existing modified or unmanaged file. */
1792         if (fsm->osuffix && fsm->diskchecked &&
1793           (fsm->exists || (fsm->goal == FSM_PKGINSTALL && S_ISREG(st->st_mode))))
1794         {
1795             const char * opath = fsm->opath;
1796             const char * path = fsm->path;
1797             fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1798             fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1799             rc = fsmStage(fsm, FSM_RENAME);
1800             if (!rc) {
1801                 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1802                                 fsm->opath, fsm->path);
1803             }
1804             fsm->path = _free(fsm->path);
1805             fsm->path = path;
1806             fsm->opath = _free(fsm->opath);
1807             fsm->opath = opath;
1808         }
1809
1810         /* Remove erased files. */
1811         if (fsm->goal == FSM_PKGERASE) {
1812             if (fsm->action == FA_ERASE) {
1813                 rpmfi fi = fsmGetFi(fsm);
1814                 if (S_ISDIR(st->st_mode)) {
1815                     rc = fsmStage(fsm, FSM_RMDIR);
1816                     if (!rc) break;
1817                     switch (errno) {
1818                     case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1819                     case ENOTEMPTY:
1820         /* XXX make sure that build side permits %missingok on directories. */
1821                         if (fsm->fflags & RPMFILE_MISSINGOK)
1822                             /*@innerbreak@*/ break;
1823
1824                         /* XXX common error message. */
1825                         rpmError(
1826                             (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1827                             _("%s rmdir of %s failed: Directory not empty\n"), 
1828                                 rpmfiTypeString(fi), fsm->path);
1829                         /*@innerbreak@*/ break;
1830                     default:
1831                         rpmError(
1832                             (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1833                                 _("%s rmdir of %s failed: %s\n"),
1834                                 rpmfiTypeString(fi), fsm->path, strerror(errno));
1835                         /*@innerbreak@*/ break;
1836                     }
1837                 } else {
1838                     rc = fsmStage(fsm, FSM_UNLINK);
1839                     if (!rc) break;
1840                     if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1841                         rpmError(
1842                             (strict_erasures ? RPMERR_UNLINK : RPMDEBUG_UNLINK),
1843                                 _("%s unlink of %s failed: %s\n"),
1844                                 rpmfiTypeString(fi), fsm->path, strerror(errno));
1845                 }
1846             }
1847             /* XXX Failure to remove is not (yet) cause for failure. */
1848             if (!strict_erasures) rc = 0;
1849             break;
1850         }
1851
1852         /* XXX Special case /dev/log, which shouldn't be packaged anyways */
1853         if (!S_ISSOCK(st->st_mode) && !IS_DEV_LOG(fsm->path)) {
1854             /* Rename temporary to final file name. */
1855             if (!S_ISDIR(st->st_mode) &&
1856                 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1857             {
1858                 fsm->opath = fsm->path;
1859                 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1860                 rc = fsmStage(fsm, FSM_RENAME);
1861                 if (!rc && fsm->nsuffix) {
1862                     const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1863                     rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1864                                 (opath ? opath : ""), fsm->path);
1865                     opath = _free(opath);
1866                 }
1867                 fsm->opath = _free(fsm->opath);
1868             }
1869             if (S_ISLNK(st->st_mode)) {
1870                 if (!rc && !getuid())
1871                     rc = fsmStage(fsm, FSM_LCHOWN);
1872             } else {
1873                 if (!rc && !getuid())
1874                     rc = fsmStage(fsm, FSM_CHOWN);
1875                 if (!rc)
1876                     rc = fsmStage(fsm, FSM_CHMOD);
1877                 if (!rc) {
1878                     time_t mtime = st->st_mtime;
1879                     rpmfi fi = fsmGetFi(fsm);
1880                     if (fi->fmtimes)
1881                         st->st_mtime = fi->fmtimes[fsm->ix];
1882                     rc = fsmStage(fsm, FSM_UTIME);
1883                     st->st_mtime = mtime;
1884                 }
1885             }
1886         }
1887
1888         /* Notify on success. */
1889         if (!rc)                rc = fsmStage(fsm, FSM_NOTIFY);
1890         else if (fsm->failedFile && *fsm->failedFile == NULL) {
1891 /*@-boundswrite@*/
1892             *fsm->failedFile = fsm->path;
1893 /*@=boundswrite@*/
1894             fsm->path = NULL;
1895         }
1896         break;
1897     case FSM_DESTROY:
1898         fsm->path = _free(fsm->path);
1899
1900         /* Check for hard links missing from payload. */
1901         while ((fsm->li = fsm->links) != NULL) {
1902             fsm->links = fsm->li->next;
1903             fsm->li->next = NULL;
1904             if (fsm->goal == FSM_PKGINSTALL &&
1905                         fsm->commit && fsm->li->linksLeft)
1906             {
1907                 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1908                     if (fsm->li->filex[i] < 0)
1909                         /*@innercontinue@*/ continue;
1910                     rc = CPIOERR_MISSING_HARDLINK;
1911                     if (fsm->failedFile && *fsm->failedFile == NULL) {
1912                         fsm->ix = fsm->li->filex[i];
1913                         if (!fsmStage(fsm, FSM_MAP)) {
1914 /*@-boundswrite@*/
1915                             *fsm->failedFile = fsm->path;
1916 /*@=boundswrite@*/
1917                             fsm->path = NULL;
1918                         }
1919                     }
1920                     /*@loopbreak@*/ break;
1921                 }
1922             }
1923             if (fsm->goal == FSM_PKGBUILD &&
1924                 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
1925             {
1926                 rc = CPIOERR_MISSING_HARDLINK;
1927             }
1928             fsm->li = freeHardLink(fsm->li);
1929         }
1930         fsm->ldn = _free(fsm->ldn);
1931         fsm->ldnalloc = fsm->ldnlen = 0;
1932         fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1933         fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1934         break;
1935     case FSM_VERIFY:
1936         if (fsm->diskchecked && !fsm->exists) {
1937             rc = CPIOERR_LSTAT_FAILED;
1938             break;
1939         }
1940         if (S_ISREG(st->st_mode)) {
1941             char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1942 /*@-boundswrite@*/
1943             (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1944 /*@=boundswrite@*/
1945             /*
1946              * XXX HP-UX (and other os'es) don't permit unlink on busy
1947              * XXX files.
1948              */
1949             fsm->opath = fsm->path;
1950             fsm->path = path;
1951             rc = fsmStage(fsm, FSM_RENAME);
1952             if (!rc)
1953                     (void) fsmStage(fsm, FSM_UNLINK);
1954             else
1955                     rc = CPIOERR_UNLINK_FAILED;
1956             fsm->path = fsm->opath;
1957             fsm->opath = NULL;
1958             return (rc ? rc : CPIOERR_LSTAT_FAILED);    /* XXX HACK */
1959             /*@notreached@*/ break;
1960         } else if (S_ISDIR(st->st_mode)) {
1961             if (S_ISDIR(ost->st_mode))          return 0;
1962             if (S_ISLNK(ost->st_mode)) {
1963                 rc = fsmStage(fsm, FSM_STAT);
1964                 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1965                 if (rc) break;
1966                 errno = saveerrno;
1967                 if (S_ISDIR(ost->st_mode))      return 0;
1968             }
1969         } else if (S_ISLNK(st->st_mode)) {
1970             if (S_ISLNK(ost->st_mode)) {
1971         /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1972                 rc = fsmStage(fsm, FSM_READLINK);
1973                 errno = saveerrno;
1974                 if (rc) break;
1975                 if (!strcmp(fsm->opath, fsm->rdbuf))    return 0;
1976             }
1977         } else if (S_ISFIFO(st->st_mode)) {
1978             if (S_ISFIFO(ost->st_mode))         return 0;
1979         } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1980             if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1981                 (ost->st_rdev == st->st_rdev))  return 0;
1982         } else if (S_ISSOCK(st->st_mode)) {
1983             if (S_ISSOCK(ost->st_mode))         return 0;
1984         }
1985             /* XXX shouldn't do this with commit/undo. */
1986         rc = 0;
1987         if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1988         if (rc == 0)    rc = CPIOERR_LSTAT_FAILED;
1989         return (rc ? rc : CPIOERR_LSTAT_FAILED);        /* XXX HACK */
1990         /*@notreached@*/ break;
1991
1992     case FSM_UNLINK:
1993         rc = Unlink(fsm->path);
1994         if (_fsm_debug && (stage & FSM_SYSCALL))
1995             rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1996                 fsm->path, (rc < 0 ? strerror(errno) : ""));
1997         if (rc < 0)     rc = CPIOERR_UNLINK_FAILED;
1998         break;
1999     case FSM_RENAME:
2000         rc = Rename(fsm->opath, fsm->path);
2001 #if defined(ETXTBSY)
2002         if (rc && errno == ETXTBSY) {
2003             char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
2004             (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
2005             /*
2006              * XXX HP-UX (and other os'es) don't permit rename to busy
2007              * XXX files.
2008              */
2009             rc = Rename(fsm->path, path);
2010             if (!rc) rc = Rename(fsm->opath, fsm->path);
2011         }
2012 #endif
2013         if (_fsm_debug && (stage & FSM_SYSCALL))
2014             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
2015                 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
2016         if (rc < 0)     rc = CPIOERR_RENAME_FAILED;
2017         break;
2018     case FSM_MKDIR:
2019         rc = Mkdir(fsm->path, (st->st_mode & 07777));
2020         if (_fsm_debug && (stage & FSM_SYSCALL))
2021             rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
2022                 fsm->path, (unsigned)(st->st_mode & 07777),
2023                 (rc < 0 ? strerror(errno) : ""));
2024         if (rc < 0)     rc = CPIOERR_MKDIR_FAILED;
2025         break;
2026     case FSM_RMDIR:
2027         rc = Rmdir(fsm->path);
2028         if (_fsm_debug && (stage & FSM_SYSCALL))
2029             rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
2030                 fsm->path, (rc < 0 ? strerror(errno) : ""));
2031         if (rc < 0)     rc = CPIOERR_RMDIR_FAILED;
2032         break;
2033     case FSM_CHOWN:
2034         rc = chown(fsm->path, st->st_uid, st->st_gid);
2035         if (_fsm_debug && (stage & FSM_SYSCALL))
2036             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
2037                 fsm->path, (int)st->st_uid, (int)st->st_gid,
2038                 (rc < 0 ? strerror(errno) : ""));
2039         if (rc < 0)     rc = CPIOERR_CHOWN_FAILED;
2040         break;
2041     case FSM_LCHOWN:
2042 #if ! CHOWN_FOLLOWS_SYMLINK
2043         rc = lchown(fsm->path, st->st_uid, st->st_gid);
2044         if (_fsm_debug && (stage & FSM_SYSCALL))
2045             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
2046                 fsm->path, (int)st->st_uid, (int)st->st_gid,
2047                 (rc < 0 ? strerror(errno) : ""));
2048         if (rc < 0)     rc = CPIOERR_CHOWN_FAILED;
2049 #endif
2050         break;
2051     case FSM_CHMOD:
2052         rc = chmod(fsm->path, (st->st_mode & 07777));
2053         if (_fsm_debug && (stage & FSM_SYSCALL))
2054             rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
2055                 fsm->path, (unsigned)(st->st_mode & 07777),
2056                 (rc < 0 ? strerror(errno) : ""));
2057         if (rc < 0)     rc = CPIOERR_CHMOD_FAILED;
2058         break;
2059     case FSM_UTIME:
2060         {   struct utimbuf stamp;
2061             stamp.actime = st->st_mtime;
2062             stamp.modtime = st->st_mtime;
2063             rc = utime(fsm->path, &stamp);
2064             if (_fsm_debug && (stage & FSM_SYSCALL))
2065                 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
2066                         fsm->path, (unsigned)st->st_mtime,
2067                         (rc < 0 ? strerror(errno) : ""));
2068             if (rc < 0) rc = CPIOERR_UTIME_FAILED;
2069         }
2070         break;
2071     case FSM_SYMLINK:
2072         rc = symlink(fsm->opath, fsm->path);
2073         if (_fsm_debug && (stage & FSM_SYSCALL))
2074             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
2075                 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
2076         if (rc < 0)     rc = CPIOERR_SYMLINK_FAILED;
2077         break;
2078     case FSM_LINK:
2079         rc = Link(fsm->opath, fsm->path);
2080         if (_fsm_debug && (stage & FSM_SYSCALL))
2081             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
2082                 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
2083         if (rc < 0)     rc = CPIOERR_LINK_FAILED;
2084         break;
2085     case FSM_MKFIFO:
2086         rc = mkfifo(fsm->path, (st->st_mode & 07777));
2087         if (_fsm_debug && (stage & FSM_SYSCALL))
2088             rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
2089                 fsm->path, (unsigned)(st->st_mode & 07777),
2090                 (rc < 0 ? strerror(errno) : ""));
2091         if (rc < 0)     rc = CPIOERR_MKFIFO_FAILED;
2092         break;
2093     case FSM_MKNOD:
2094         /*@-unrecog -portability @*/ /* FIX: check S_IFIFO or dev != 0 */
2095         rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
2096         /*@=unrecog =portability @*/
2097         if (_fsm_debug && (stage & FSM_SYSCALL))
2098             rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
2099                 fsm->path, (unsigned)(st->st_mode & ~07777),
2100                 (unsigned)st->st_rdev,
2101                 (rc < 0 ? strerror(errno) : ""));
2102         if (rc < 0)     rc = CPIOERR_MKNOD_FAILED;
2103         break;
2104     case FSM_LSTAT:
2105         rc = Lstat(fsm->path, ost);
2106         if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
2107             rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
2108                 fsm->path, (rc < 0 ? strerror(errno) : ""));
2109         if (rc < 0)     rc = CPIOERR_LSTAT_FAILED;
2110         break;
2111     case FSM_STAT:
2112         rc = Stat(fsm->path, ost);
2113         if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
2114             rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
2115                 fsm->path, (rc < 0 ? strerror(errno) : ""));
2116         if (rc < 0)     rc = CPIOERR_STAT_FAILED;
2117         break;
2118     case FSM_READLINK:
2119         /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
2120 /*@-boundswrite@*/
2121         rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
2122 /*@=boundswrite@*/
2123         if (_fsm_debug && (stage & FSM_SYSCALL))
2124             rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
2125                 fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
2126         if (rc < 0)     rc = CPIOERR_READLINK_FAILED;
2127         else {
2128             fsm->rdnb = rc;
2129 /*@-boundswrite@*/
2130             fsm->rdbuf[fsm->rdnb] = '\0';
2131 /*@=boundswrite@*/
2132             rc = 0;
2133         }
2134         break;
2135     case FSM_CHROOT:
2136         break;
2137
2138     case FSM_NEXT:
2139         rc = fsmStage(fsm, FSM_HREAD);
2140         if (rc) break;
2141         if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
2142             fsm->path = _free(fsm->path);
2143             rc = CPIOERR_HDR_TRAILER;
2144         }
2145         if (!rc)
2146             rc = fsmStage(fsm, FSM_POS);
2147         break;
2148     case FSM_EAT:
2149         for (left = st->st_size; left > 0; left -= fsm->rdnb) {
2150             fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
2151             rc = fsmStage(fsm, FSM_DREAD);
2152             if (rc)
2153                 /*@loopbreak@*/ break;
2154         }
2155         break;
2156     case FSM_POS:
2157         left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2158         if (left) {
2159             fsm->wrlen = left;
2160             (void) fsmStage(fsm, FSM_DREAD);
2161         }
2162         break;
2163     case FSM_PAD:
2164         left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2165         if (left) {
2166 /*@-boundswrite@*/
2167             memset(fsm->rdbuf, 0, left);
2168 /*@=boundswrite@*/
2169             /* XXX DWRITE uses rdnb for I/O length. */
2170             fsm->rdnb = left;
2171             (void) fsmStage(fsm, FSM_DWRITE);
2172         }
2173         break;
2174     case FSM_TRAILER:
2175         rc = cpioTrailerWrite(fsm);
2176         break;
2177     case FSM_HREAD:
2178         rc = fsmStage(fsm, FSM_POS);
2179         if (!rc)
2180             rc = cpioHeaderRead(fsm, st);       /* Read next payload header. */
2181         break;
2182     case FSM_HWRITE:
2183         rc = cpioHeaderWrite(fsm, st);          /* Write next payload header. */
2184         break;
2185     case FSM_DREAD:
2186 /*@-boundswrite@*/
2187         fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
2188 /*@=boundswrite@*/
2189         if (_fsm_debug && (stage & FSM_SYSCALL))
2190             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
2191                 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
2192                 (int)fsm->wrlen, (int)fsm->rdnb);
2193         if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
2194             rc = CPIOERR_READ_FAILED;
2195         if (fsm->rdnb > 0)
2196             fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
2197         break;
2198     case FSM_DWRITE:
2199         fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
2200         if (_fsm_debug && (stage & FSM_SYSCALL))
2201             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
2202                 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
2203                 (int)fsm->rdnb, (int)fsm->wrnb);
2204         if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
2205             rc = CPIOERR_WRITE_FAILED;
2206         if (fsm->wrnb > 0)
2207             fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
2208         break;
2209
2210     case FSM_ROPEN:
2211         fsm->rfd = Fopen(fsm->path, "r.ufdio");
2212         if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2213             if (fsm->rfd != NULL)       (void) fsmStage(fsm, FSM_RCLOSE);
2214             fsm->rfd = NULL;
2215             rc = CPIOERR_OPEN_FAILED;
2216             break;
2217         }
2218         if (_fsm_debug && (stage & FSM_SYSCALL))
2219             rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2220                 fsm->path, fsm->rfd, fsm->rdbuf);
2221         break;
2222     case FSM_READ:
2223 /*@-boundswrite@*/
2224         fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2225 /*@=boundswrite@*/
2226         if (_fsm_debug && (stage & FSM_SYSCALL))
2227             rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2228                 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2229         if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2230             rc = CPIOERR_READ_FAILED;
2231         break;
2232     case FSM_RCLOSE:
2233         if (fsm->rfd != NULL) {
2234             if (_fsm_debug && (stage & FSM_SYSCALL))
2235                 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2236             (void) Fclose(fsm->rfd);
2237             errno = saveerrno;
2238         }
2239         fsm->rfd = NULL;
2240         break;
2241     case FSM_WOPEN:
2242         fsm->wfd = Fopen(fsm->path, "w.ufdio");
2243         if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2244             if (fsm->wfd != NULL)       (void) fsmStage(fsm, FSM_WCLOSE);
2245             fsm->wfd = NULL;
2246             rc = CPIOERR_OPEN_FAILED;
2247         }
2248         if (_fsm_debug && (stage & FSM_SYSCALL))
2249             rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2250                 fsm->path, fsm->wfd, fsm->wrbuf);
2251         break;
2252     case FSM_WRITE:
2253         fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2254         if (_fsm_debug && (stage & FSM_SYSCALL))
2255             rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2256                 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2257         if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2258             rc = CPIOERR_WRITE_FAILED;
2259         break;
2260     case FSM_WCLOSE:
2261         if (fsm->wfd != NULL) {
2262             if (_fsm_debug && (stage & FSM_SYSCALL))
2263                 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2264             (void) Fclose(fsm->wfd);
2265             errno = saveerrno;
2266         }
2267         fsm->wfd = NULL;
2268         break;
2269
2270     default:
2271         break;
2272     }
2273     /*@=branchstate@*/
2274
2275     if (!(stage & FSM_INTERNAL)) {
2276         fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2277     }
2278     return rc;
2279 }
2280 /*@=compmempass@*/
2281 /*@=boundsread@*/
2282
2283 /*@obserever@*/ const char *const fileActionString(fileAction a)
2284 {
2285     switch (a) {
2286     case FA_UNKNOWN:    return "unknown";
2287     case FA_CREATE:     return "create";
2288     case FA_COPYOUT:    return "copyout";
2289     case FA_COPYIN:     return "copyin";
2290     case FA_BACKUP:     return "backup";
2291     case FA_SAVE:       return "save";
2292     case FA_SKIP:       return "skip";
2293     case FA_ALTNAME:    return "altname";
2294     case FA_ERASE:      return "erase";
2295     case FA_SKIPNSTATE: return "skipnstate";
2296     case FA_SKIPNETSHARED: return "skipnetshared";
2297     case FA_SKIPCOLOR:  return "skipcolor";
2298     default:            return "???";
2299     }
2300     /*@notreached@*/
2301 }
2302
2303 /*@observer@*/ const char *const fileStageString(fileStage a) {
2304     switch(a) {
2305     case FSM_UNKNOWN:   return "unknown";
2306
2307     case FSM_PKGINSTALL:return "INSTALL";
2308     case FSM_PKGERASE:  return "ERASE";
2309     case FSM_PKGBUILD:  return "BUILD";
2310     case FSM_PKGCOMMIT: return "COMMIT";
2311     case FSM_PKGUNDO:   return "UNDO";
2312
2313     case FSM_CREATE:    return "create";
2314     case FSM_INIT:      return "init";
2315     case FSM_MAP:       return "map";
2316     case FSM_MKDIRS:    return "mkdirs";
2317     case FSM_RMDIRS:    return "rmdirs";
2318     case FSM_PRE:       return "pre";
2319     case FSM_PROCESS:   return "process";
2320     case FSM_POST:      return "post";
2321     case FSM_MKLINKS:   return "mklinks";
2322     case FSM_NOTIFY:    return "notify";
2323     case FSM_UNDO:      return "undo";
2324     case FSM_FINI:      return "fini";
2325     case FSM_COMMIT:    return "commit";
2326     case FSM_DESTROY:   return "destroy";
2327     case FSM_VERIFY:    return "verify";
2328
2329     case FSM_UNLINK:    return "Unlink";
2330     case FSM_RENAME:    return "Rename";
2331     case FSM_MKDIR:     return "Mkdir";
2332     case FSM_RMDIR:     return "rmdir";
2333     case FSM_CHOWN:     return "chown";
2334     case FSM_LCHOWN:    return "lchown";
2335     case FSM_CHMOD:     return "chmod";
2336     case FSM_UTIME:     return "utime";
2337     case FSM_SYMLINK:   return "symlink";
2338     case FSM_LINK:      return "Link";
2339     case FSM_MKFIFO:    return "mkfifo";
2340     case FSM_MKNOD:     return "mknod";
2341     case FSM_LSTAT:     return "Lstat";
2342     case FSM_STAT:      return "Stat";
2343     case FSM_READLINK:  return "Readlink";
2344     case FSM_CHROOT:    return "chroot";
2345
2346     case FSM_NEXT:      return "next";
2347     case FSM_EAT:       return "eat";
2348     case FSM_POS:       return "pos";
2349     case FSM_PAD:       return "pad";
2350     case FSM_TRAILER:   return "trailer";
2351     case FSM_HREAD:     return "hread";
2352     case FSM_HWRITE:    return "hwrite";
2353     case FSM_DREAD:     return "Fread";
2354     case FSM_DWRITE:    return "Fwrite";
2355
2356     case FSM_ROPEN:     return "Fopen";
2357     case FSM_READ:      return "Fread";
2358     case FSM_RCLOSE:    return "Fclose";
2359     case FSM_WOPEN:     return "Fopen";
2360     case FSM_WRITE:     return "Fwrite";
2361     case FSM_WCLOSE:    return "Fclose";
2362
2363     default:            return "???";
2364     }
2365     /*@noteached@*/
2366 }