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