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