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