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