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