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