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