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