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