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