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