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