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