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