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