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