- generate index for pkgid (aka Sigmd5) tag, query/verify by pkgid.
[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_s * 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         {   rpmTransactionSet ts = fsmGetTs(fsm);
679
680             if (ts != NULL && !(ts->transFlags & RPMTRANS_FLAG_NOMD5)) {
681                 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
682                 fsm->md5sum = (fi->md5s ? (fi->md5s + (16 * i)) : NULL);
683             } else {
684                 fsm->fmd5sum = NULL;
685                 fsm->md5sum = NULL;
686             }
687         }
688
689     }
690     return 0;
691 }
692
693 /** \ingroup payload
694  * Create file from payload stream.
695  * @param fsm           file state machine data
696  * @return              0 on success
697  */
698 static int expandRegular(/*@special@*/ FSM_t fsm)
699         /*@uses fsm->sb @*/
700         /*@globals fileSystem@*/
701         /*@modifies fsm, fileSystem @*/
702 {
703     const struct stat * st = &fsm->sb;
704     int left = st->st_size;
705     int rc = 0;
706
707     rc = fsmStage(fsm, FSM_WOPEN);
708     if (rc)
709         goto exit;
710
711     if (st->st_size > 0 && (fsm->fmd5sum || fsm->md5sum))
712         fdInitDigest(fsm->wfd, PGPHASHALGO_MD5, 0);
713
714     while (left) {
715
716         fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
717         rc = fsmStage(fsm, FSM_DREAD);
718         if (rc)
719             goto exit;
720
721         rc = fsmStage(fsm, FSM_WRITE);
722         if (rc)
723             goto exit;
724
725         left -= fsm->wrnb;
726
727         /* don't call this with fileSize == fileComplete */
728         if (!rc && left)
729             (void) fsmStage(fsm, FSM_NOTIFY);
730     }
731
732     if (st->st_size > 0 && (fsm->fmd5sum || fsm->md5sum)) {
733         void * md5sum = NULL;
734         int asAscii = (fsm->md5sum == NULL ? 1 : 0);
735
736         (void) Fflush(fsm->wfd);
737         fdFiniDigest(fsm->wfd, PGPHASHALGO_MD5, &md5sum, NULL, asAscii);
738
739         if (md5sum == NULL) {
740             rc = CPIOERR_MD5SUM_MISMATCH;
741             goto exit;
742         }
743
744         if (fsm->md5sum != NULL) {
745             if (memcmp(md5sum, fsm->md5sum, 16))
746                 rc = CPIOERR_MD5SUM_MISMATCH;
747         } else {
748             if (strcmp(md5sum, fsm->fmd5sum))
749                 rc = CPIOERR_MD5SUM_MISMATCH;
750         }
751         md5sum = _free(md5sum);
752     }
753
754 exit:
755     (void) fsmStage(fsm, FSM_WCLOSE);
756     return rc;
757 }
758
759 /** \ingroup payload
760  * Write next item to payload stream.
761  * @param fsm           file state machine data
762  * @param writeData     should data be written?
763  * @return              0 on success
764  */
765 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
766         /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
767         /*@globals fileSystem@*/
768         /*@modifies fsm, fileSystem @*/
769 {
770     const char * path = fsm->path;
771     const char * opath = fsm->opath;
772     struct stat * st = &fsm->sb;
773     struct stat * ost = &fsm->osb;
774     size_t pos = fdGetCpioPos(fsm->cfd);
775     char * symbuf = NULL;
776     int left;
777     int xx;
778     int rc;
779
780     st->st_size = (writeData ? ost->st_size : 0);
781
782     /*@-branchstate@*/
783     if (S_ISDIR(st->st_mode)) {
784         st->st_size = 0;
785     } else if (S_ISLNK(st->st_mode)) {
786         /*
787          * While linux puts the size of a symlink in the st_size field,
788          * I don't think that's a specified standard.
789          */
790         /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
791         rc = fsmStage(fsm, FSM_READLINK);
792         if (rc) goto exit;
793         st->st_size = fsm->rdnb;
794         symbuf = alloca_strdup(fsm->rdbuf);     /* XXX save readlink return. */
795     }
796     /*@=branchstate@*/
797
798     if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
799 /*@-compdef@*/ /* FIX: dirName/baseName annotations ? */
800         int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
801         char * t = alloca(nb);
802         *t = '\0';
803         fsm->path = t;
804         if (fsm->mapFlags & CPIO_MAP_ADDDOT)
805             *t++ = '.';
806         t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
807 /*@=compdef@*/
808     } else if (fsm->mapFlags & CPIO_MAP_PATH) {
809         TFI_t fi = fsmGetFi(fsm);
810         fsm->path =
811             (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
812     }
813
814     rc = fsmStage(fsm, FSM_HWRITE);
815     fsm->path = path;
816     if (rc) goto exit;
817
818     if (writeData && S_ISREG(st->st_mode)) {
819 #if HAVE_MMAP
820         char * rdbuf = NULL;
821         void * mapped = (void *)-1;
822         size_t nmapped;
823 #endif
824
825         rc = fsmStage(fsm, FSM_ROPEN);
826         if (rc) goto exit;
827
828         /* XXX unbuffered mmap generates *lots* of fdio debugging */
829 #if HAVE_MMAP
830         nmapped = 0;
831         mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
832         if (mapped != (void *)-1) {
833             rdbuf = fsm->rdbuf;
834             fsm->rdbuf = (char *) mapped;
835             fsm->rdlen = nmapped = st->st_size;
836             xx = madvise(mapped, nmapped, MADV_DONTNEED);
837         }
838 #endif
839
840         left = st->st_size;
841
842         while (left) {
843 #if HAVE_MMAP
844           if (mapped != (void *)-1) {
845             fsm->rdnb = nmapped;
846           } else
847 #endif
848           {
849             fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
850             rc = fsmStage(fsm, FSM_READ);
851             if (rc) goto exit;
852           }
853
854             /* XXX DWRITE uses rdnb for I/O length. */
855             rc = fsmStage(fsm, FSM_DWRITE);
856             if (rc) goto exit;
857
858             left -= fsm->wrnb;
859         }
860
861 #if HAVE_MMAP
862         if (mapped != (void *)-1) {
863             xx = msync(mapped, nmapped, MS_ASYNC);
864             /*@-noeffect@*/ xx = munmap(mapped, nmapped) /*@=noeffect@*/;
865             fsm->rdbuf = rdbuf;
866         }
867 #endif
868
869     } else if (writeData && S_ISLNK(st->st_mode)) {
870         /* XXX DWRITE uses rdnb for I/O length. */
871         strcpy(fsm->rdbuf, symbuf);     /* XXX restore readlink buffer. */
872         fsm->rdnb = strlen(symbuf);
873         rc = fsmStage(fsm, FSM_DWRITE);
874         if (rc) goto exit;
875     }
876
877     rc = fsmStage(fsm, FSM_PAD);
878     if (rc) goto exit;
879
880     {   const rpmTransactionSet ts = fsmGetTs(fsm);
881         TFI_t fi = fsmGetFi(fsm);
882         if (ts && ts->notify && fi) {
883             size_t size = (fdGetCpioPos(fsm->cfd) - pos);
884             /*@-type@*/ /* FIX: cast? */
885             /*@-noeffectuncon @*/ /* FIX: check rc */
886             (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
887                         rpmfiGetKey(fi), ts->notifyData);
888             /*@=noeffectuncon @*/
889             /*@=type@*/
890         }
891     }
892
893     rc = 0;
894
895 exit:
896     if (fsm->rfd)
897         (void) fsmStage(fsm, FSM_RCLOSE);
898     /*@-dependenttrans@*/
899     fsm->opath = opath;
900     fsm->path = path;
901     /*@=dependenttrans@*/
902     return rc;
903 }
904
905 /** \ingroup payload
906  * Write set of linked files to payload stream.
907  * @param fsm           file state machine data
908  * @return              0 on success
909  */
910 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
911         /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
912         /*@globals fileSystem@*/
913         /*@modifies fsm, fileSystem @*/
914 {
915     const char * path = fsm->path;
916     const char * nsuffix = fsm->nsuffix;
917     int iterIndex = fsm->ix;
918     int ec = 0;
919     int rc;
920     int i;
921
922     fsm->path = NULL;
923     fsm->nsuffix = NULL;
924     fsm->ix = -1;
925
926     for (i = fsm->li->nlink - 1; i >= 0; i--) {
927
928         if (fsm->li->filex[i] < 0) continue;
929
930         fsm->ix = fsm->li->filex[i];
931         rc = fsmStage(fsm, FSM_MAP);
932
933         /* Write data after last link. */
934         rc = writeFile(fsm, (i == 0));
935         if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
936             ec = rc;
937             *fsm->failedFile = xstrdup(fsm->path);
938         }
939
940         fsm->path = _free(fsm->path);
941         fsm->li->filex[i] = -1;
942     }
943
944     fsm->ix = iterIndex;
945     fsm->nsuffix = nsuffix;
946     fsm->path = path;
947     return ec;
948 }
949
950 /** \ingroup payload
951  * Create pending hard links to existing file.
952  * @param fsm           file state machine data
953  * @return              0 on success
954  */
955 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
956         /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
957         /*@globals fileSystem@*/
958         /*@modifies fsm, fileSystem @*/
959 {
960     const char * path = fsm->path;
961     const char * opath = fsm->opath;
962     const char * nsuffix = fsm->nsuffix;
963     int iterIndex = fsm->ix;
964     int ec = 0;
965     int rc;
966     int i;
967
968     fsm->path = NULL;
969     fsm->opath = NULL;
970     fsm->nsuffix = NULL;
971     fsm->ix = -1;
972
973     fsm->ix = fsm->li->filex[fsm->li->createdPath];
974     rc = fsmStage(fsm, FSM_MAP);
975     fsm->opath = fsm->path;
976     fsm->path = NULL;
977     /*@-branchstate@*/
978     for (i = 0; i < fsm->li->nlink; i++) {
979         if (fsm->li->filex[i] < 0) continue;
980         if (fsm->li->createdPath == i) continue;
981
982         fsm->ix = fsm->li->filex[i];
983         fsm->path = _free(fsm->path);
984         rc = fsmStage(fsm, FSM_MAP);
985         if (XFA_SKIPPING(fsm->action)) continue;
986
987         rc = fsmStage(fsm, FSM_VERIFY);
988         if (!rc) continue;
989         if (rc != CPIOERR_LSTAT_FAILED) break;
990
991         /* XXX link(fsm->opath, fsm->path) */
992         rc = fsmStage(fsm, FSM_LINK);
993         if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
994             ec = rc;
995             *fsm->failedFile = xstrdup(fsm->path);
996         }
997
998         fsm->li->linksLeft--;
999     }
1000     /*@=branchstate@*/
1001     fsm->path = _free(fsm->path);
1002     fsm->opath = _free(fsm->opath);
1003
1004     fsm->ix = iterIndex;
1005     fsm->nsuffix = nsuffix;
1006     fsm->path = path;
1007     fsm->opath = opath;
1008     return ec;
1009 }
1010
1011 /** \ingroup payload
1012  * Commit hard linked file set atomically.
1013  * @param fsm           file state machine data
1014  * @return              0 on success
1015  */
1016 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
1017         /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
1018                 fsm->li, fsm->links @*/
1019         /*@globals fileSystem@*/
1020         /*@modifies fsm, fileSystem @*/
1021 {
1022     const char * path = fsm->path;
1023     const char * nsuffix = fsm->nsuffix;
1024     int iterIndex = fsm->ix;
1025     struct stat * st = &fsm->sb;
1026     int rc = 0;
1027     int i;
1028
1029     fsm->path = NULL;
1030     fsm->nsuffix = NULL;
1031     fsm->ix = -1;
1032
1033     /*@-branchstate@*/
1034     for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
1035         if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
1036             break;
1037     }
1038     /*@=branchstate@*/
1039
1040     for (i = 0; i < fsm->li->nlink; i++) {
1041         if (fsm->li->filex[i] < 0) continue;
1042         fsm->ix = fsm->li->filex[i];
1043         rc = fsmStage(fsm, FSM_MAP);
1044         if (!XFA_SKIPPING(fsm->action))
1045             rc = fsmStage(fsm, FSM_COMMIT);
1046         fsm->path = _free(fsm->path);
1047         fsm->li->filex[i] = -1;
1048     }
1049
1050     fsm->ix = iterIndex;
1051     fsm->nsuffix = nsuffix;
1052     fsm->path = path;
1053     return rc;
1054 }
1055
1056 /**
1057  * Remove (if created) directories not explicitly included in package.
1058  * @param fsm           file state machine data
1059  * @return              0 on success
1060  */
1061 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
1062         /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
1063         /*@globals fileSystem@*/
1064         /*@modifies fsm, fileSystem @*/
1065 {
1066     const char * path = fsm->path;
1067     void * dnli = dnlInitIterator(fsm, 1);
1068     char * dn = fsm->rdbuf;
1069     int dc = dnlCount(dnli);
1070     int rc = 0;
1071
1072     fsm->path = NULL;
1073     dn[0] = '\0';
1074     /*@-observertrans -dependenttrans@*/
1075     if (fsm->ldn != NULL && fsm->dnlx != NULL)
1076     while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1077         int dnlen = strlen(fsm->path);
1078         char * te;
1079
1080         dc = dnlIndex(dnli);
1081         if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1082             continue;
1083
1084         /* Copy to avoid const on fsm->path. */
1085         te = stpcpy(dn, fsm->path) - 1;
1086         fsm->path = dn;
1087
1088         /* Remove generated directories. */
1089         /*@-usereleased@*/ /* LCL: te used after release? */
1090         do {
1091             if (*te == '/') {
1092                 *te = '\0';
1093                 rc = fsmStage(fsm, FSM_RMDIR);
1094                 *te = '/';
1095             }
1096             if (rc)
1097                 /*@innerbreak@*/ break;
1098             te--;
1099         } while ((te - fsm->path) > fsm->dnlx[dc]);
1100         /*@=usereleased@*/
1101     }
1102     dnli = dnlFreeIterator(dnli);
1103     /*@=observertrans =dependenttrans@*/
1104
1105     fsm->path = path;
1106     return rc;
1107 }
1108
1109 /**
1110  * Create (if necessary) directories not explicitly included in package.
1111  * @param fsm           file state machine data
1112  * @return              0 on success
1113  */
1114 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1115         /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1116                 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1117         /*@defines fsm->dnlx, fsm->ldn @*/
1118         /*@globals fileSystem@*/
1119         /*@modifies fsm, fileSystem @*/
1120 {
1121     struct stat * st = &fsm->sb;
1122     struct stat * ost = &fsm->osb;
1123     const char * path = fsm->path;
1124     mode_t st_mode = st->st_mode;
1125     void * dnli = dnlInitIterator(fsm, 0);
1126     char * dn = fsm->rdbuf;
1127     int dc = dnlCount(dnli);
1128     int rc = 0;
1129     int i;
1130
1131     fsm->path = NULL;
1132
1133     dn[0] = '\0';
1134     fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1135     /*@-observertrans -dependenttrans@*/
1136     if (fsm->dnlx != NULL)
1137     while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1138         int dnlen = strlen(fsm->path);
1139         char * te;
1140
1141         dc = dnlIndex(dnli);
1142         if (dc < 0) continue;
1143         fsm->dnlx[dc] = dnlen;
1144         if (dnlen <= 1)
1145             continue;
1146
1147         /*@-compdef -nullpass@*/        /* FIX: fsm->ldn not defined ??? */
1148         if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1149             continue;
1150         /*@=compdef =nullpass@*/
1151
1152         /* Copy to avoid const on fsm->path. */
1153         (void) stpcpy(dn, fsm->path);
1154         fsm->path = dn;
1155
1156         /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1157         for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1158             if (*te != '/')
1159                 /*@innercontinue@*/ continue;
1160
1161             *te = '\0';
1162
1163             /* Already validated? */
1164             /*@-usedef -compdef -nullpass -nullderef@*/
1165             if (i < fsm->ldnlen &&
1166                 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1167                 !strncmp(fsm->path, fsm->ldn, i))
1168             {
1169                 *te = '/';
1170                 /* Move pre-existing path marker forward. */
1171                 fsm->dnlx[dc] = (te - dn);
1172                 /*@innercontinue@*/ continue;
1173             }
1174             /*@=usedef =compdef =nullpass =nullderef@*/
1175
1176             /* Validate next component of path. */
1177             rc = fsmStage(fsm, FSM_LSTAT);
1178             *te = '/';
1179
1180             /* Directory already exists? */
1181             if (rc == 0 && S_ISDIR(ost->st_mode)) {
1182                 /* Move pre-existing path marker forward. */
1183                 fsm->dnlx[dc] = (te - dn);
1184             } else if (rc == CPIOERR_LSTAT_FAILED) {
1185                 TFI_t fi = fsmGetFi(fsm);
1186                 *te = '\0';
1187                 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1188                 rc = fsmStage(fsm, FSM_MKDIR);
1189                 if (!rc)
1190                     rpmMessage(RPMMESS_DEBUG,
1191                         _("%s directory created with perms %04o.\n"),
1192                         fsm->path, (unsigned)(st->st_mode & 07777));
1193                 *te = '/';
1194             }
1195             if (rc)
1196                 /*@innerbreak@*/ break;
1197         }
1198         if (rc) break;
1199
1200         /* Save last validated path. */
1201 /*@-compdef@*/ /* FIX: ldn/path annotations ? */
1202         if (fsm->ldnalloc < (dnlen + 1)) {
1203             fsm->ldnalloc = dnlen + 100;
1204             fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1205         }
1206         if (fsm->ldn != NULL) { /* XXX can't happen */
1207             strcpy(fsm->ldn, fsm->path);
1208             fsm->ldnlen = dnlen;
1209         }
1210 /*@=compdef@*/
1211     }
1212     dnli = dnlFreeIterator(dnli);
1213     /*@=observertrans =dependenttrans@*/
1214
1215     fsm->path = path;
1216     st->st_mode = st_mode;              /* XXX restore st->st_mode */
1217 /*@-compdef@*/ /* FIX: ldn/path annotations ? */
1218     return rc;
1219 /*@=compdef@*/
1220 }
1221
1222 #ifdef  NOTYET
1223 /**
1224  * Check for file on disk.
1225  * @param fsm           file state machine data
1226  * @return              0 on success
1227  */
1228 static int fsmStat(FSM_t fsm)
1229 {
1230     int saveerrno = errno;
1231     int rc = 0;
1232
1233     if (fsm->path != NULL) {
1234         int saveernno = errno;
1235         rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1236                         ? FSM_LSTAT : FSM_STAT));
1237         if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1238             errno = saveerrno;
1239             rc = 0;
1240             fsm->exists = 0;
1241         } else if (rc == 0) {
1242             fsm->exists = 1;
1243         }
1244     } else {
1245         /* Skip %ghost files on build. */
1246         fsm->exists = 0;
1247     }
1248     return rc;
1249 }
1250 #endif
1251
1252 /*@-compmempass@*/
1253 int fsmStage(FSM_t fsm, fileStage stage)
1254 {
1255 #ifdef  UNUSED
1256     fileStage prevStage = fsm->stage;
1257     const char * const prev = fileStageString(prevStage);
1258 #endif
1259     static int modulo = 4;
1260     const char * const cur = fileStageString(stage);
1261     struct stat * st = &fsm->sb;
1262     struct stat * ost = &fsm->osb;
1263     int saveerrno = errno;
1264     int rc = fsm->rc;
1265     size_t left;
1266     int i;
1267
1268 #define _fafilter(_a)   \
1269     (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1270         ? fileActionString(_a) : "")
1271
1272     if (stage & FSM_DEAD) {
1273         /* do nothing */
1274     } else if (stage & FSM_INTERNAL) {
1275         if (_fsm_debug && !(stage & FSM_SYSCALL))
1276             rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1277                 cur,
1278                 (unsigned)st->st_mode, (int)st->st_nlink,
1279                 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1280                 (fsm->path ? fsm->path : ""),
1281                 _fafilter(fsm->action));
1282     } else {
1283         fsm->stage = stage;
1284         if (_fsm_debug || !(stage & FSM_VERBOSE))
1285             rpmMessage(RPMMESS_DEBUG, "%-8s  %06o%3d (%4d,%4d)%10d %s %s\n",
1286                 cur,
1287                 (unsigned)st->st_mode, (int)st->st_nlink,
1288                 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1289                 (fsm->path ? fsm->path + fsm->astriplen : ""),
1290                 _fafilter(fsm->action));
1291     }
1292 #undef  _fafilter
1293
1294     /*@-branchstate@*/
1295     switch (stage) {
1296     case FSM_UNKNOWN:
1297         break;
1298     case FSM_PKGINSTALL:
1299         while (1) {
1300             /* Clean fsm, free'ing memory. Read next archive header. */
1301             rc = fsmStage(fsm, FSM_INIT);
1302
1303             /* Exit on end-of-payload. */
1304             if (rc == CPIOERR_HDR_TRAILER) {
1305                 rc = 0;
1306                 /*@loopbreak@*/ break;
1307             }
1308
1309             /* Exit on error. */
1310             if (rc) {
1311                 fsm->postpone = 1;
1312                 (void) fsmStage(fsm, FSM_UNDO);
1313                 /*@loopbreak@*/ break;
1314             }
1315
1316             /* Extract file from archive. */
1317             rc = fsmStage(fsm, FSM_PROCESS);
1318             if (rc) {
1319                 (void) fsmStage(fsm, FSM_UNDO);
1320                 /*@loopbreak@*/ break;
1321             }
1322
1323             /* Notify on success. */
1324             (void) fsmStage(fsm, FSM_NOTIFY);
1325
1326             rc = fsmStage(fsm, FSM_FINI);
1327             if (rc) {
1328                 /*@loopbreak@*/ break;
1329             }
1330         }
1331         break;
1332     case FSM_PKGERASE:
1333     case FSM_PKGCOMMIT:
1334         while (1) {
1335             /* Clean fsm, free'ing memory. */
1336             rc = fsmStage(fsm, FSM_INIT);
1337
1338             /* Exit on end-of-payload. */
1339             if (rc == CPIOERR_HDR_TRAILER) {
1340                 rc = 0;
1341                 /*@loopbreak@*/ break;
1342             }
1343
1344             /* Rename/erase next item. */
1345             if (fsmStage(fsm, FSM_FINI))
1346                 /*@loopbreak@*/ break;
1347         }
1348         break;
1349     case FSM_PKGBUILD:
1350         while (1) {
1351
1352             rc = fsmStage(fsm, FSM_INIT);
1353
1354             /* Exit on end-of-payload. */
1355             if (rc == CPIOERR_HDR_TRAILER) {
1356                 rc = 0;
1357                 /*@loopbreak@*/ break;
1358             }
1359
1360             /* Exit on error. */
1361             if (rc) {
1362                 fsm->postpone = 1;
1363                 (void) fsmStage(fsm, FSM_UNDO);
1364                 /*@loopbreak@*/ break;
1365             }
1366
1367             /* Copy file into archive. */
1368             rc = fsmStage(fsm, FSM_PROCESS);
1369             if (rc) {
1370                 (void) fsmStage(fsm, FSM_UNDO);
1371                 /*@loopbreak@*/ break;
1372             }
1373
1374             if (fsmStage(fsm, FSM_FINI))
1375                 /*@loopbreak@*/ break;
1376         }
1377
1378         /* Flush partial sets of hard linked files. */
1379         if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1380             int nlink, j;
1381             while ((fsm->li = fsm->links) != NULL) {
1382                 fsm->links = fsm->li->next;
1383                 fsm->li->next = NULL;
1384
1385                 /* Re-calculate link count for archive header. */
1386                 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1387                     if (fsm->li->filex[i] < 0)
1388                         /*@innercontinue@*/ continue;
1389                     nlink++;
1390                     if (j == -1) j = i;
1391                 }
1392                 /* XXX force the contents out as well. */
1393                 if (j != 0) {
1394                     fsm->li->filex[0] = fsm->li->filex[j];
1395                     fsm->li->filex[j] = -1;
1396                 }
1397                 fsm->li->sb.st_nlink = nlink;
1398
1399                 fsm->sb = fsm->li->sb;  /* structure assignment */
1400                 fsm->osb = fsm->sb;     /* structure assignment */
1401
1402                 if (!rc) rc = writeLinkedFile(fsm);
1403
1404                 fsm->li = freeHardLink(fsm->li);
1405             }
1406         }
1407
1408         if (!rc)
1409             rc = fsmStage(fsm, FSM_TRAILER);
1410
1411         break;
1412     case FSM_CREATE:
1413         {   rpmTransactionSet ts = fsmGetTs(fsm);
1414 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1415             fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1416                         fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1417 #undef _tsmask
1418         }
1419         fsm->path = _free(fsm->path);
1420         fsm->opath = _free(fsm->opath);
1421         fsm->dnlx = _free(fsm->dnlx);
1422
1423         fsm->ldn = _free(fsm->ldn);
1424         fsm->ldnalloc = fsm->ldnlen = 0;
1425
1426         fsm->rdsize = fsm->wrsize = 0;
1427         fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1428         fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1429         if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1430             fsm->rdsize = 8 * BUFSIZ;
1431             fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1432             fsm->wrsize = 8 * BUFSIZ;
1433             fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1434         }
1435
1436         fsm->mkdirsdone = 0;
1437         fsm->ix = -1;
1438         fsm->links = NULL;
1439         fsm->li = NULL;
1440         /*@-mods@*/
1441         errno = 0;      /* XXX get rid of EBADF */
1442         /*@=mods@*/
1443
1444         /* Detect and create directories not explicitly in package. */
1445         if (fsm->goal == FSM_PKGINSTALL) {
1446             rc = fsmStage(fsm, FSM_MKDIRS);
1447             if (!rc) fsm->mkdirsdone = 1;
1448         }
1449
1450         break;
1451     case FSM_INIT:
1452         fsm->path = _free(fsm->path);
1453         fsm->postpone = 0;
1454         fsm->diskchecked = fsm->exists = 0;
1455         fsm->subdir = NULL;
1456         fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1457         fsm->action = FA_UNKNOWN;
1458         fsm->osuffix = NULL;
1459         fsm->nsuffix = NULL;
1460
1461         if (fsm->goal == FSM_PKGINSTALL) {
1462             /* Read next header from payload, checking for end-of-payload. */
1463             rc = fsmStage(fsm, FSM_NEXT);
1464         }
1465         if (rc) break;
1466
1467         /* Identify mapping index. */
1468         fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1469                 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1470
1471         /* Detect end-of-loop and/or mapping error. */
1472         if (fsm->ix < 0) {
1473             if (fsm->goal == FSM_PKGINSTALL) {
1474 #if 0
1475                 rpmMessage(RPMMESS_WARNING,
1476                     _("archive file %s was not found in header file list\n"),
1477                         fsm->path);
1478 #endif
1479                 if (fsm->failedFile && *fsm->failedFile == NULL)
1480                     *fsm->failedFile = xstrdup(fsm->path);
1481                 rc = CPIOERR_UNMAPPED_FILE;
1482             } else {
1483                 rc = CPIOERR_HDR_TRAILER;
1484             }
1485             break;
1486         }
1487
1488         /* On non-install, mode must be known so that dirs don't get suffix. */
1489         if (fsm->goal != FSM_PKGINSTALL) {
1490             TFI_t fi = fsmGetFi(fsm);
1491             st->st_mode = fi->fmodes[fsm->ix];
1492         }
1493
1494         /* Generate file path. */
1495         rc = fsmStage(fsm, FSM_MAP);
1496         if (rc) break;
1497
1498         /* Perform lstat/stat for disk file. */
1499 #ifdef  NOTYET
1500         rc = fsmStat(fsm);
1501 #else
1502         if (fsm->path != NULL) {
1503             rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1504                         ? FSM_LSTAT : FSM_STAT));
1505             if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1506                 /*@-mods@*/
1507                 errno = saveerrno;
1508                 /*@=mods@*/
1509                 rc = 0;
1510                 fsm->exists = 0;
1511             } else if (rc == 0) {
1512                 fsm->exists = 1;
1513             }
1514         } else {
1515             /* Skip %ghost files on build. */
1516             fsm->exists = 0;
1517         }
1518 #endif
1519         fsm->diskchecked = 1;
1520         if (rc) break;
1521
1522         /* On non-install, the disk file stat is what's remapped. */
1523         if (fsm->goal != FSM_PKGINSTALL)
1524             *st = *ost;                 /* structure assignment */
1525
1526         /* Remap file perms, owner, and group. */
1527         rc = fsmMapAttrs(fsm);
1528         if (rc) break;
1529
1530         fsm->postpone = XFA_SKIPPING(fsm->action);
1531         if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1532             /*@-evalorder@*/ /* FIX: saveHardLink can modify fsm */
1533             if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1534                 fsm->postpone = saveHardLink(fsm);
1535             /*@=evalorder@*/
1536         }
1537         break;
1538     case FSM_PRE:
1539         break;
1540     case FSM_MAP:
1541         rc = fsmMapPath(fsm);
1542         break;
1543     case FSM_MKDIRS:
1544         rc = fsmMkdirs(fsm);
1545         break;
1546     case FSM_RMDIRS:
1547         if (fsm->dnlx)
1548             rc = fsmRmdirs(fsm);
1549         break;
1550     case FSM_PROCESS:
1551         if (fsm->postpone) {
1552             if (fsm->goal == FSM_PKGINSTALL)
1553                 rc = fsmStage(fsm, FSM_EAT);
1554             break;
1555         }
1556
1557         if (fsm->goal == FSM_PKGBUILD) {
1558             if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1559                 struct hardLink_s * li, * prev;
1560
1561 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1562                 rc = writeLinkedFile(fsm);
1563                 if (rc) break;  /* W2DO? */
1564
1565                 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1566                      if (li == fsm->li)
1567                         /*@loopbreak@*/ break;
1568
1569                 if (prev == NULL)
1570                     fsm->links = fsm->li->next;
1571                 else
1572                     prev->next = fsm->li->next;
1573                 fsm->li->next = NULL;
1574                 fsm->li = freeHardLink(fsm->li);
1575             } else {
1576                 rc = writeFile(fsm, 1);
1577             }
1578             break;
1579         }
1580
1581         if (fsm->goal != FSM_PKGINSTALL)
1582             break;
1583
1584         if (S_ISREG(st->st_mode)) {
1585             const char * path = fsm->path;
1586             if (fsm->osuffix)
1587                 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1588             rc = fsmStage(fsm, FSM_VERIFY);
1589
1590             if (rc == 0 && fsm->osuffix) {
1591                 const char * opath = fsm->opath;
1592                 fsm->opath = fsm->path;
1593                 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1594                 rc = fsmStage(fsm, FSM_RENAME);
1595                 if (!rc)
1596                     rpmMessage(RPMMESS_WARNING,
1597                         _("%s saved as %s\n"), fsm->opath, fsm->path);
1598                 fsm->path = _free(fsm->path);
1599                 fsm->opath = opath;
1600             }
1601
1602             /*@-dependenttrans@*/
1603             fsm->path = path;
1604             /*@=dependenttrans@*/
1605             if (rc != CPIOERR_LSTAT_FAILED) return rc;
1606             rc = expandRegular(fsm);
1607         } else if (S_ISDIR(st->st_mode)) {
1608             mode_t st_mode = st->st_mode;
1609             rc = fsmStage(fsm, FSM_VERIFY);
1610             if (rc == CPIOERR_LSTAT_FAILED) {
1611                 st->st_mode &= ~07777;          /* XXX abuse st->st_mode */
1612                 st->st_mode |=  00700;
1613                 rc = fsmStage(fsm, FSM_MKDIR);
1614                 st->st_mode = st_mode;          /* XXX restore st->st_mode */
1615             }
1616         } else if (S_ISLNK(st->st_mode)) {
1617             const char * opath = fsm->opath;
1618
1619             if ((st->st_size + 1) > fsm->rdsize) {
1620                 rc = CPIOERR_HDR_SIZE;
1621                 break;
1622             }
1623
1624             fsm->wrlen = st->st_size;
1625             rc = fsmStage(fsm, FSM_DREAD);
1626             if (!rc && fsm->rdnb != fsm->wrlen)
1627                 rc = CPIOERR_READ_FAILED;
1628             if (rc) break;
1629
1630             fsm->wrbuf[st->st_size] = '\0';
1631             /* XXX symlink(fsm->opath, fsm->path) */
1632             /*@-dependenttrans@*/
1633             fsm->opath = fsm->wrbuf;
1634             /*@=dependenttrans@*/
1635             rc = fsmStage(fsm, FSM_VERIFY);
1636             if (rc == CPIOERR_LSTAT_FAILED)
1637                 rc = fsmStage(fsm, FSM_SYMLINK);
1638             fsm->opath = opath;         /* XXX restore fsm->path */
1639         } else if (S_ISFIFO(st->st_mode)) {
1640             mode_t st_mode = st->st_mode;
1641             /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1642             rc = fsmStage(fsm, FSM_VERIFY);
1643             if (rc == CPIOERR_LSTAT_FAILED) {
1644                 st->st_mode = 0000;             /* XXX abuse st->st_mode */
1645                 rc = fsmStage(fsm, FSM_MKFIFO);
1646                 st->st_mode = st_mode;  /* XXX restore st->st_mode */
1647             }
1648         } else if (S_ISCHR(st->st_mode) ||
1649                    S_ISBLK(st->st_mode) ||
1650     /*@-unrecog@*/ S_ISSOCK(st->st_mode) /*@=unrecog@*/)
1651         {
1652             rc = fsmStage(fsm, FSM_VERIFY);
1653             if (rc == CPIOERR_LSTAT_FAILED)
1654                 rc = fsmStage(fsm, FSM_MKNOD);
1655         } else {
1656             /* XXX Special case /dev/log, which shouldn't be packaged anyways */
1657             if (strncmp(fsm->path, "/dev/log;", sizeof("/dev/log;")-1))
1658                 rc = CPIOERR_UNKNOWN_FILETYPE;
1659         }
1660         if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1661             fsm->li->createdPath = fsm->li->linkIndex;
1662             rc = fsmMakeLinks(fsm);
1663         }
1664         break;
1665     case FSM_POST:
1666         break;
1667     case FSM_MKLINKS:
1668         rc = fsmMakeLinks(fsm);
1669         break;
1670     case FSM_NOTIFY:            /* XXX move from fsm to psm -> tsm */
1671         if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1672             rpmTransactionSet ts = fsmGetTs(fsm);
1673             TFI_t fi = fsmGetFi(fsm);
1674             if (ts && ts->notify && fi) {
1675                 /*@-type@*/ /* FIX: cast? */
1676                 /*@-noeffectuncon @*/ /* FIX: check rc */
1677                 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1678                         fdGetCpioPos(fsm->cfd), fi->archiveSize,
1679                         rpmfiGetKey(fi), ts->notifyData);
1680                 /*@=noeffectuncon @*/
1681                 /*@=type@*/
1682             }
1683         }
1684         break;
1685     case FSM_UNDO:
1686         if (fsm->postpone)
1687             break;
1688         if (fsm->goal == FSM_PKGINSTALL) {
1689             (void) fsmStage(fsm,
1690                 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1691
1692 #ifdef  NOTYET  /* XXX remove only dirs just created, not all. */
1693             if (fsm->dnlx)
1694                 (void) fsmStage(fsm, FSM_RMDIRS);
1695 #endif
1696             /*@-mods@*/
1697             errno = saveerrno;
1698             /*@=mods@*/
1699         }
1700         if (fsm->failedFile && *fsm->failedFile == NULL)
1701             *fsm->failedFile = xstrdup(fsm->path);
1702         break;
1703     case FSM_FINI:
1704         if (!fsm->postpone && fsm->commit) {
1705             if (fsm->goal == FSM_PKGINSTALL)
1706                 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1707                         ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1708             if (fsm->goal == FSM_PKGCOMMIT)
1709                 rc = fsmStage(fsm, FSM_COMMIT);
1710             if (fsm->goal == FSM_PKGERASE)
1711                 rc = fsmStage(fsm, FSM_COMMIT);
1712         }
1713         fsm->path = _free(fsm->path);
1714         fsm->opath = _free(fsm->opath);
1715         memset(st, 0, sizeof(*st));
1716         memset(ost, 0, sizeof(*ost));
1717         break;
1718     case FSM_COMMIT:
1719         /* Rename pre-existing modified or unmanaged file. */
1720         if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1721             const char * opath = fsm->opath;
1722             const char * path = fsm->path;
1723             fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1724             fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1725             rc = fsmStage(fsm, FSM_RENAME);
1726             if (!rc) {
1727                 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1728                                 fsm->opath, fsm->path);
1729             }
1730             fsm->path = _free(fsm->path);
1731             fsm->path = path;
1732             fsm->opath = _free(fsm->opath);
1733             fsm->opath = opath;
1734         }
1735
1736         /* Remove erased files. */
1737         if (fsm->goal == FSM_PKGERASE) {
1738             if (fsm->action == FA_ERASE) {
1739                 TFI_t fi = fsmGetFi(fsm);
1740                 if (S_ISDIR(st->st_mode)) {
1741                     rc = fsmStage(fsm, FSM_RMDIR);
1742                     if (!rc) break;
1743                     switch (errno) {
1744                     case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1745                     case ENOTEMPTY:
1746         /* XXX make sure that build side permits %missingok on directories. */
1747                         if (fsm->fflags & RPMFILE_MISSINGOK)
1748                             /*@innerbreak@*/ break;
1749
1750                         /* XXX common error message. */
1751                         rpmError(
1752                             (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1753                             _("%s rmdir of %s failed: Directory not empty\n"), 
1754                                 fiTypeString(fi), fsm->path);
1755                         /*@innerbreak@*/ break;
1756                     default:
1757                         rpmError(
1758                             (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1759                                 _("%s rmdir of %s failed: %s\n"),
1760                                 fiTypeString(fi), fsm->path, strerror(errno));
1761                         /*@innerbreak@*/ break;
1762                     }
1763                 } else {
1764                     rc = fsmStage(fsm, FSM_UNLINK);
1765                     if (!rc) break;
1766                     if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1767                         rpmError(
1768                             (strict_erasures ? RPMERR_UNLINK : RPMDEBUG_UNLINK),
1769                                 _("%s unlink of %s failed: %s\n"),
1770                                 fiTypeString(fi), fsm->path, strerror(errno));
1771                 }
1772             }
1773             /* XXX Failure to remove is not (yet) cause for failure. */
1774             if (!strict_erasures) rc = 0;
1775             break;
1776         }
1777
1778         /* XXX Special case /dev/log, which shouldn't be packaged anyways */
1779         if (!S_ISSOCK(st->st_mode)
1780         &&  strncmp(fsm->path, "/dev/log;", sizeof("/dev/log;")-1))
1781         {
1782             /* Rename temporary to final file name. */
1783             if (!S_ISDIR(st->st_mode) &&
1784                 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1785             {
1786                 fsm->opath = fsm->path;
1787                 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1788                 rc = fsmStage(fsm, FSM_RENAME);
1789                 if (!rc && fsm->nsuffix) {
1790                     const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1791                     rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1792                                 (opath ? opath : ""), fsm->path);
1793                     opath = _free(opath);
1794                 }
1795                 fsm->opath = _free(fsm->opath);
1796             }
1797             if (S_ISLNK(st->st_mode)) {
1798                 if (!rc && !getuid())
1799                     rc = fsmStage(fsm, FSM_LCHOWN);
1800             } else {
1801                 if (!rc && !getuid())
1802                     rc = fsmStage(fsm, FSM_CHOWN);
1803                 if (!rc)
1804                     rc = fsmStage(fsm, FSM_CHMOD);
1805                 if (!rc) {
1806                     time_t mtime = st->st_mtime;
1807                     TFI_t fi = fsmGetFi(fsm);
1808                     if (fi->fmtimes)
1809                         st->st_mtime = fi->fmtimes[fsm->ix];
1810                     rc = fsmStage(fsm, FSM_UTIME);
1811                     st->st_mtime = mtime;
1812                 }
1813             }
1814         }
1815
1816         /* Notify on success. */
1817         if (!rc)                rc = fsmStage(fsm, FSM_NOTIFY);
1818         else if (fsm->failedFile && *fsm->failedFile == NULL) {
1819             *fsm->failedFile = fsm->path;
1820             fsm->path = NULL;
1821         }
1822         break;
1823     case FSM_DESTROY:
1824         fsm->path = _free(fsm->path);
1825
1826         /* Check for hard links missing from payload. */
1827         while ((fsm->li = fsm->links) != NULL) {
1828             fsm->links = fsm->li->next;
1829             fsm->li->next = NULL;
1830             if (fsm->goal == FSM_PKGINSTALL &&
1831                         fsm->commit && fsm->li->linksLeft)
1832             {
1833                 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1834                     if (fsm->li->filex[i] < 0)
1835                         /*@innercontinue@*/ continue;
1836                     rc = CPIOERR_MISSING_HARDLINK;
1837                     if (fsm->failedFile && *fsm->failedFile == NULL) {
1838                         fsm->ix = fsm->li->filex[i];
1839                         if (!fsmStage(fsm, FSM_MAP)) {
1840                             *fsm->failedFile = fsm->path;
1841                             fsm->path = NULL;
1842                         }
1843                     }
1844                     /*@loopbreak@*/ break;
1845                 }
1846             }
1847             if (fsm->goal == FSM_PKGBUILD &&
1848                 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
1849             {
1850                 rc = CPIOERR_MISSING_HARDLINK;
1851             }
1852             fsm->li = freeHardLink(fsm->li);
1853         }
1854         fsm->ldn = _free(fsm->ldn);
1855         fsm->ldnalloc = fsm->ldnlen = 0;
1856         fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1857         fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1858         break;
1859     case FSM_VERIFY:
1860         if (fsm->diskchecked && !fsm->exists) {
1861             rc = CPIOERR_LSTAT_FAILED;
1862             break;
1863         }
1864         if (S_ISREG(st->st_mode)) {
1865             char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1866             (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1867             /*
1868              * XXX HP-UX (and other os'es) don't permit unlink on busy
1869              * XXX files.
1870              */
1871             fsm->opath = fsm->path;
1872             fsm->path = path;
1873             rc = fsmStage(fsm, FSM_RENAME);
1874             if (!rc)
1875                     (void) fsmStage(fsm, FSM_UNLINK);
1876             else
1877                     rc = CPIOERR_UNLINK_FAILED;
1878             fsm->path = fsm->opath;
1879             fsm->opath = NULL;
1880             return (rc ? rc : CPIOERR_LSTAT_FAILED);    /* XXX HACK */
1881             /*@notreached@*/ break;
1882         } else if (S_ISDIR(st->st_mode)) {
1883             if (S_ISDIR(ost->st_mode))          return 0;
1884             if (S_ISLNK(ost->st_mode)) {
1885                 rc = fsmStage(fsm, FSM_STAT);
1886                 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1887                 if (rc) break;
1888                 /*@-mods@*/
1889                 errno = saveerrno;
1890                 /*@=mods@*/
1891                 if (S_ISDIR(ost->st_mode))      return 0;
1892             }
1893         } else if (S_ISLNK(st->st_mode)) {
1894             if (S_ISLNK(ost->st_mode)) {
1895         /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1896                 rc = fsmStage(fsm, FSM_READLINK);
1897                 /*@-mods@*/
1898                 errno = saveerrno;
1899                 /*@=mods@*/
1900                 if (rc) break;
1901                 if (!strcmp(fsm->opath, fsm->rdbuf))    return 0;
1902             }
1903         } else if (S_ISFIFO(st->st_mode)) {
1904             if (S_ISFIFO(ost->st_mode))         return 0;
1905         } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1906             if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1907                 (ost->st_rdev == st->st_rdev))  return 0;
1908         } else if (S_ISSOCK(st->st_mode)) {
1909             if (S_ISSOCK(ost->st_mode))         return 0;
1910         }
1911             /* XXX shouldn't do this with commit/undo. */
1912         rc = 0;
1913         if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1914         if (rc == 0)    rc = CPIOERR_LSTAT_FAILED;
1915         return (rc ? rc : CPIOERR_LSTAT_FAILED);        /* XXX HACK */
1916         /*@notreached@*/ break;
1917
1918     case FSM_UNLINK:
1919         rc = Unlink(fsm->path);
1920         if (_fsm_debug && (stage & FSM_SYSCALL))
1921             rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1922                 fsm->path, (rc < 0 ? strerror(errno) : ""));
1923         if (rc < 0)     rc = CPIOERR_UNLINK_FAILED;
1924         break;
1925     case FSM_RENAME:
1926         rc = Rename(fsm->opath, fsm->path);
1927         if (_fsm_debug && (stage & FSM_SYSCALL))
1928             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1929                 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1930         if (rc < 0)     rc = CPIOERR_RENAME_FAILED;
1931         break;
1932     case FSM_MKDIR:
1933         rc = Mkdir(fsm->path, (st->st_mode & 07777));
1934         if (_fsm_debug && (stage & FSM_SYSCALL))
1935             rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1936                 fsm->path, (unsigned)(st->st_mode & 07777),
1937                 (rc < 0 ? strerror(errno) : ""));
1938         if (rc < 0)     rc = CPIOERR_MKDIR_FAILED;
1939         break;
1940     case FSM_RMDIR:
1941         rc = Rmdir(fsm->path);
1942         if (_fsm_debug && (stage & FSM_SYSCALL))
1943             rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1944                 fsm->path, (rc < 0 ? strerror(errno) : ""));
1945         if (rc < 0)     rc = CPIOERR_RMDIR_FAILED;
1946         break;
1947     case FSM_CHOWN:
1948         rc = chown(fsm->path, st->st_uid, st->st_gid);
1949         if (_fsm_debug && (stage & FSM_SYSCALL))
1950             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1951                 fsm->path, (int)st->st_uid, (int)st->st_gid,
1952                 (rc < 0 ? strerror(errno) : ""));
1953         if (rc < 0)     rc = CPIOERR_CHOWN_FAILED;
1954         break;
1955     case FSM_LCHOWN:
1956 #if ! CHOWN_FOLLOWS_SYMLINK
1957         rc = lchown(fsm->path, st->st_uid, st->st_gid);
1958         if (_fsm_debug && (stage & FSM_SYSCALL))
1959             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1960                 fsm->path, (int)st->st_uid, (int)st->st_gid,
1961                 (rc < 0 ? strerror(errno) : ""));
1962         if (rc < 0)     rc = CPIOERR_CHOWN_FAILED;
1963 #endif
1964         break;
1965     case FSM_CHMOD:
1966         rc = chmod(fsm->path, (st->st_mode & 07777));
1967         if (_fsm_debug && (stage & FSM_SYSCALL))
1968             rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1969                 fsm->path, (unsigned)(st->st_mode & 07777),
1970                 (rc < 0 ? strerror(errno) : ""));
1971         if (rc < 0)     rc = CPIOERR_CHMOD_FAILED;
1972         break;
1973     case FSM_UTIME:
1974         {   struct utimbuf stamp;
1975             stamp.actime = st->st_mtime;
1976             stamp.modtime = st->st_mtime;
1977             rc = utime(fsm->path, &stamp);
1978             if (_fsm_debug && (stage & FSM_SYSCALL))
1979                 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1980                         fsm->path, (unsigned)st->st_mtime,
1981                         (rc < 0 ? strerror(errno) : ""));
1982             if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1983         }
1984         break;
1985     case FSM_SYMLINK:
1986         rc = symlink(fsm->opath, fsm->path);
1987         if (_fsm_debug && (stage & FSM_SYSCALL))
1988             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1989                 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1990         if (rc < 0)     rc = CPIOERR_SYMLINK_FAILED;
1991         break;
1992     case FSM_LINK:
1993         rc = Link(fsm->opath, fsm->path);
1994         if (_fsm_debug && (stage & FSM_SYSCALL))
1995             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1996                 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1997         if (rc < 0)     rc = CPIOERR_LINK_FAILED;
1998         break;
1999     case FSM_MKFIFO:
2000         rc = mkfifo(fsm->path, (st->st_mode & 07777));
2001         if (_fsm_debug && (stage & FSM_SYSCALL))
2002             rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
2003                 fsm->path, (unsigned)(st->st_mode & 07777),
2004                 (rc < 0 ? strerror(errno) : ""));
2005         if (rc < 0)     rc = CPIOERR_MKFIFO_FAILED;
2006         break;
2007     case FSM_MKNOD:
2008         /*@-unrecog -portability @*/ /* FIX: check S_IFIFO or dev != 0 */
2009         rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
2010         /*@=unrecog =portability @*/
2011         if (_fsm_debug && (stage & FSM_SYSCALL))
2012             rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
2013                 fsm->path, (unsigned)(st->st_mode & ~07777),
2014                 (unsigned)st->st_rdev,
2015                 (rc < 0 ? strerror(errno) : ""));
2016         if (rc < 0)     rc = CPIOERR_MKNOD_FAILED;
2017         break;
2018     case FSM_LSTAT:
2019         rc = Lstat(fsm->path, ost);
2020         if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
2021             rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
2022                 fsm->path, (rc < 0 ? strerror(errno) : ""));
2023         if (rc < 0)     rc = CPIOERR_LSTAT_FAILED;
2024         break;
2025     case FSM_STAT:
2026         rc = Stat(fsm->path, ost);
2027         if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
2028             rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
2029                 fsm->path, (rc < 0 ? strerror(errno) : ""));
2030         if (rc < 0)     rc = CPIOERR_STAT_FAILED;
2031         break;
2032     case FSM_READLINK:
2033         /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
2034         rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
2035         if (_fsm_debug && (stage & FSM_SYSCALL))
2036             rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
2037                 fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
2038         if (rc < 0)     rc = CPIOERR_READLINK_FAILED;
2039         else {
2040             fsm->rdnb = rc;
2041             fsm->rdbuf[fsm->rdnb] = '\0';
2042             rc = 0;
2043         }
2044         break;
2045     case FSM_CHROOT:
2046         break;
2047
2048     case FSM_NEXT:
2049         rc = fsmStage(fsm, FSM_HREAD);
2050         if (rc) break;
2051         if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
2052             fsm->path = _free(fsm->path);
2053             rc = CPIOERR_HDR_TRAILER;
2054         }
2055         if (!rc)
2056             rc = fsmStage(fsm, FSM_POS);
2057         break;
2058     case FSM_EAT:
2059         for (left = st->st_size; left > 0; left -= fsm->rdnb) {
2060             fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
2061             rc = fsmStage(fsm, FSM_DREAD);
2062             if (rc)
2063                 /*@loopbreak@*/ break;
2064         }
2065         break;
2066     case FSM_POS:
2067         left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2068         if (left) {
2069             fsm->wrlen = left;
2070             (void) fsmStage(fsm, FSM_DREAD);
2071         }
2072         break;
2073     case FSM_PAD:
2074         left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2075         if (left) {
2076             memset(fsm->rdbuf, 0, left);
2077             /* XXX DWRITE uses rdnb for I/O length. */
2078             fsm->rdnb = left;
2079             (void) fsmStage(fsm, FSM_DWRITE);
2080         }
2081         break;
2082     case FSM_TRAILER:
2083         rc = cpioTrailerWrite(fsm);
2084         break;
2085     case FSM_HREAD:
2086         rc = fsmStage(fsm, FSM_POS);
2087         if (!rc)
2088             rc = cpioHeaderRead(fsm, st);       /* Read next payload header. */
2089         break;
2090     case FSM_HWRITE:
2091         rc = cpioHeaderWrite(fsm, st);          /* Write next payload header. */
2092         break;
2093     case FSM_DREAD:
2094         fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
2095         if (_fsm_debug && (stage & FSM_SYSCALL))
2096             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
2097                 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
2098                 (int)fsm->wrlen, (int)fsm->rdnb);
2099         if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
2100             rc = CPIOERR_READ_FAILED;
2101         if (fsm->rdnb > 0)
2102             fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
2103         break;
2104     case FSM_DWRITE:
2105         fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
2106         if (_fsm_debug && (stage & FSM_SYSCALL))
2107             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
2108                 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
2109                 (int)fsm->rdnb, (int)fsm->wrnb);
2110         if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
2111             rc = CPIOERR_WRITE_FAILED;
2112         if (fsm->wrnb > 0)
2113             fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
2114         break;
2115
2116     case FSM_ROPEN:
2117         fsm->rfd = Fopen(fsm->path, "r.ufdio");
2118         if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2119             if (fsm->rfd)       (void) fsmStage(fsm, FSM_RCLOSE);
2120             fsm->rfd = NULL;
2121             rc = CPIOERR_OPEN_FAILED;
2122             break;
2123         }
2124         if (_fsm_debug && (stage & FSM_SYSCALL))
2125             rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2126                 fsm->path, fsm->rfd, fsm->rdbuf);
2127         break;
2128     case FSM_READ:
2129         fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2130         if (_fsm_debug && (stage & FSM_SYSCALL))
2131             rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2132                 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2133         if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2134             rc = CPIOERR_READ_FAILED;
2135         break;
2136     case FSM_RCLOSE:
2137         if (fsm->rfd) {
2138             if (_fsm_debug && (stage & FSM_SYSCALL))
2139                 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2140             (void) Fclose(fsm->rfd);
2141             /*@-mods@*/
2142             errno = saveerrno;
2143             /*@=mods@*/
2144         }
2145         fsm->rfd = NULL;
2146         break;
2147     case FSM_WOPEN:
2148         fsm->wfd = Fopen(fsm->path, "w.ufdio");
2149         if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2150             if (fsm->wfd)       (void) fsmStage(fsm, FSM_WCLOSE);
2151             fsm->wfd = NULL;
2152             rc = CPIOERR_OPEN_FAILED;
2153         }
2154         if (_fsm_debug && (stage & FSM_SYSCALL))
2155             rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2156                 fsm->path, fsm->wfd, fsm->wrbuf);
2157         break;
2158     case FSM_WRITE:
2159         fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2160         if (_fsm_debug && (stage & FSM_SYSCALL))
2161             rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2162                 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2163         if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2164             rc = CPIOERR_WRITE_FAILED;
2165         break;
2166     case FSM_WCLOSE:
2167         if (fsm->wfd) {
2168             if (_fsm_debug && (stage & FSM_SYSCALL))
2169                 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2170             (void) Fclose(fsm->wfd);
2171             /*@-mods@*/
2172             errno = saveerrno;
2173             /*@=mods@*/
2174         }
2175         fsm->wfd = NULL;
2176         break;
2177
2178     default:
2179         break;
2180     }
2181     /*@=branchstate@*/
2182
2183     if (!(stage & FSM_INTERNAL)) {
2184         fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2185     }
2186     return rc;
2187 }
2188 /*@=compmempass@*/
2189
2190 /*@obserever@*/ const char *const fileActionString(fileAction a)
2191 {
2192     switch (a) {
2193     case FA_UNKNOWN:    return "unknown";
2194     case FA_CREATE:     return "create";
2195     case FA_COPYOUT:    return "copyout";
2196     case FA_COPYIN:     return "copyin";
2197     case FA_BACKUP:     return "backup";
2198     case FA_SAVE:       return "save";
2199     case FA_SKIP:       return "skip";
2200     case FA_ALTNAME:    return "altname";
2201     case FA_ERASE:      return "erase";
2202     case FA_SKIPNSTATE: return "skipnstate";
2203     case FA_SKIPNETSHARED: return "skipnetshared";
2204     case FA_SKIPMULTILIB: return "skipmultilib";
2205     default:            return "???";
2206     }
2207     /*@notreached@*/
2208 }
2209
2210 /*@observer@*/ const char *const fileStageString(fileStage a) {
2211     switch(a) {
2212     case FSM_UNKNOWN:   return "unknown";
2213
2214     case FSM_PKGINSTALL:return "INSTALL";
2215     case FSM_PKGERASE:  return "ERASE";
2216     case FSM_PKGBUILD:  return "BUILD";
2217     case FSM_PKGCOMMIT: return "COMMIT";
2218     case FSM_PKGUNDO:   return "UNDO";
2219
2220     case FSM_CREATE:    return "create";
2221     case FSM_INIT:      return "init";
2222     case FSM_MAP:       return "map";
2223     case FSM_MKDIRS:    return "mkdirs";
2224     case FSM_RMDIRS:    return "rmdirs";
2225     case FSM_PRE:       return "pre";
2226     case FSM_PROCESS:   return "process";
2227     case FSM_POST:      return "post";
2228     case FSM_MKLINKS:   return "mklinks";
2229     case FSM_NOTIFY:    return "notify";
2230     case FSM_UNDO:      return "undo";
2231     case FSM_FINI:      return "fini";
2232     case FSM_COMMIT:    return "commit";
2233     case FSM_DESTROY:   return "destroy";
2234     case FSM_VERIFY:    return "verify";
2235
2236     case FSM_UNLINK:    return "Unlink";
2237     case FSM_RENAME:    return "Rename";
2238     case FSM_MKDIR:     return "Mkdir";
2239     case FSM_RMDIR:     return "rmdir";
2240     case FSM_CHOWN:     return "chown";
2241     case FSM_LCHOWN:    return "lchown";
2242     case FSM_CHMOD:     return "chmod";
2243     case FSM_UTIME:     return "utime";
2244     case FSM_SYMLINK:   return "symlink";
2245     case FSM_LINK:      return "Link";
2246     case FSM_MKFIFO:    return "mkfifo";
2247     case FSM_MKNOD:     return "mknod";
2248     case FSM_LSTAT:     return "Lstat";
2249     case FSM_STAT:      return "Stat";
2250     case FSM_READLINK:  return "Readlink";
2251     case FSM_CHROOT:    return "chroot";
2252
2253     case FSM_NEXT:      return "next";
2254     case FSM_EAT:       return "eat";
2255     case FSM_POS:       return "pos";
2256     case FSM_PAD:       return "pad";
2257     case FSM_TRAILER:   return "trailer";
2258     case FSM_HREAD:     return "hread";
2259     case FSM_HWRITE:    return "hwrite";
2260     case FSM_DREAD:     return "Fread";
2261     case FSM_DWRITE:    return "Fwrite";
2262
2263     case FSM_ROPEN:     return "Fopen";
2264     case FSM_READ:      return "Fread";
2265     case FSM_RCLOSE:    return "Fclose";
2266     case FSM_WOPEN:     return "Fopen";
2267     case FSM_WRITE:     return "Fwrite";
2268     case FSM_WCLOSE:    return "Fclose";
2269
2270     default:            return "???";
2271     }
2272     /*@noteached@*/
2273 }