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