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