- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

# lib/fsm.c

Go to the documentation of this file.

    00001
    00006 #include "system.h"
    00007
    00008 #include "psm.h"
    00009 #include "rpmerr.h"
    00010 #include "debug.h"
    00011
    00012 /*@access FD_t @*/
    00013 /*@access rpmTransactionSet @*/
    00014 /*@access TFI_t @*/
    00015 /*@access FSMI_t @*/
    00016 /*@access FSM_t @*/
    00017
    00018 #define alloca_strdup(_s)       strcpy(alloca(strlen(_s)+1), (_s))
    00019
    00020 int _fsm_debug = 0;
    00021
    00022 /* XXX Failure to remove is not (yet) cause for failure. */
    00023 /*@-exportlocal -exportheadervar@*/
    00024 int strict_erasures = 0;
    00025 /*@=exportlocal =exportheadervar@*/
    00026
    00027 rpmTransactionSet fsmGetTs(const FSM_t fsm) {
    00028     const FSMI_t iter = fsm->iter;
    00029     /*@-retexpose@*/
    00030     return (iter ? iter->ts : NULL);
    00031     /*@=retexpose@*/
    00032 }
    00033
    00034 TFI_t fsmGetFi(const FSM_t fsm)
    00035 {
    00036     const FSMI_t iter = fsm->iter;
    00037     /*@-retexpose@*/
    00038     return (iter ? iter->fi : NULL);
    00039     /*@=retexpose@*/
    00040 }
    00041
    00042 #define SUFFIX_RPMORIG  ".rpmorig"
    00043 #define SUFFIX_RPMSAVE  ".rpmsave"
    00044 #define SUFFIX_RPMNEW   ".rpmnew"
    00045
    00054 static /*@only@*//*@null@*/
    00055 const char * fsmFsPath(/*@special@*/ /*@null@*/ const FSM_t fsm,
    00056                 /*@null@*/ const struct stat * st,
    00057                 /*@null@*/ const char * subdir,
    00058                 /*@null@*/ const char * suffix)
    00059         /*@uses fsm->dirName, fsm->baseName */
    00060 {
    00061     const char * s = NULL;
    00062
    00063     if (fsm) {
    00064         int nb;
    00065         char * t;
    00066         /*@-nullpass@*/         /* LCL: subdir/suffix != NULL */
    00067         nb = strlen(fsm->dirName) +
    00068             (st && subdir && !S_ISDIR(st->st_mode) ? strlen(subdir) : 0) +
    00069             (st && suffix && !S_ISDIR(st->st_mode) ? strlen(suffix) : 0) +
    00070             strlen(fsm->baseName) + 1;
    00071         s = t = xmalloc(nb);
    00072         t = stpcpy(t, fsm->dirName);
    00073         if (st && subdir && !S_ISDIR(st->st_mode))
    00074             t = stpcpy(t, subdir);
    00075         t = stpcpy(t, fsm->baseName);
    00076         if (st && suffix && !S_ISDIR(st->st_mode))
    00077             t = stpcpy(t, suffix);
    00078         /*@=nullpass@*/
    00079     }
    00080     return s;
    00081 }
    00082
    00088 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/const void * p)
    00089 {
    00090     return _free((void *)p);
    00091 }
    00092
    00099 static void *
    00100 mapInitIterator(/*@kept@*/ const void * a, /*@kept@*/ const void * b)
    00101 {
    00102     rpmTransactionSet ts = (void *)a;
    00103     TFI_t fi = (void *)b;
    00104     FSMI_t iter = NULL;
    00105
    00106     iter = xcalloc(1, sizeof(*iter));
    00107     /*@-assignexpose@*/
    00108     iter->ts = ts;
    00109     iter->fi = fi;
    00110     /*@=assignexpose@*/
    00111     iter->reverse = (fi->type == TR_REMOVED && fi->action != FA_COPYOUT);
    00112     iter->i = (iter->reverse ? (fi->fc - 1) : 0);
    00113     iter->isave = iter->i;
    00114     return iter;
    00115 }
    00116
    00122 static int mapNextIterator(void * a) {
    00123     FSMI_t iter = a;
    00124     const TFI_t fi = iter->fi;
    00125     int i = -1;
    00126
    00127     if (iter->reverse) {
    00128         if (iter->i >= 0)       i = iter->i--;
    00129     } else {
    00130         if (iter->i < fi->fc)   i = iter->i++;
    00131     }
    00132     iter->isave = i;
    00133     return i;
    00134 }
    00135
    00138 static int cpioStrCmp(const void * a, const void * b)
    00139         /*@*/
    00140 {
    00141     const char * afn = *(const char **)a;
    00142     const char * bfn = *(const char **)b;
    00143
    00144     /* Match rpm-4.0 payloads with ./ prefixes. */
    00145     if (afn[0] == '.' && afn[1] == '/') afn += 2;
    00146     if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
    00147
    00148     /* If either path is absolute, make it relative. */
    00149     if (afn[0] == '/')  afn += 1;
    00150     if (bfn[0] == '/')  bfn += 1;
    00151
    00152     return strcmp(afn, bfn);
    00153 }
    00154
    00161 static int mapFind(void * a, const char * fsmPath)
    00162         /*@*/
    00163 {
    00164     FSMI_t iter = a;
    00165     const TFI_t fi = iter->fi;
    00166     int ix = -1;
    00167
    00168     if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
    00169         const char ** p = NULL;
    00170
    00171         if (fi->apath != NULL)
    00172             p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath),
    00173                         cpioStrCmp);
    00174         if (p) {
    00175             iter->i = p - fi->apath;
    00176             ix = mapNextIterator(iter);
    00177         }
    00178     }
    00179     return ix;
    00180 }
    00181
    00185 typedef struct dnli_s {
    00186 /*@dependent@*/ TFI_t fi;
    00187 /*@only@*/ /*@null@*/ char * active;
    00188     int reverse;
    00189     int isave;
    00190     int i;
    00191 } * DNLI_t;
    00192
    00198 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * a)
    00199         /*@modifies a @*/
    00200 {
    00201     if (a) {
    00202         DNLI_t dnli = (void *)a;
    00203         if (dnli->active) free(dnli->active);
    00204     }
    00205     return _free(a);
    00206 }
    00207
    00210 static inline int dnlCount(const DNLI_t dnli)
    00211         /*@*/
    00212 {
    00213     return (dnli ? dnli->fi->dc : 0);
    00214 }
    00215
    00218 static inline int dnlIndex(const DNLI_t dnli)
    00219         /*@*/
    00220 {
    00221     return (dnli ? dnli->isave : -1);
    00222 }
    00223
    00230 static /*@only@*/ void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
    00231                 int reverse)
    00232         /*@uses fsm->iter @*/
    00233         /*@*/
    00234 {
    00235     TFI_t fi = fsmGetFi(fsm);
    00236     DNLI_t dnli;
    00237     int i, j;
    00238
    00239     if (fi == NULL)
    00240         return NULL;
    00241     dnli = xcalloc(1, sizeof(*dnli));
    00242     dnli->fi = fi;
    00243     dnli->reverse = reverse;
    00244     dnli->i = (reverse ? fi->dc : 0);
    00245
    00246     if (fi->dc) {
    00247         dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
    00248
    00249         /* Identify parent directories not skipped. */
    00250         for (i = 0; i < fi->fc; i++)
    00251             if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
    00252
    00253         /* Exclude parent directories that are explicitly included. */
    00254         for (i = 0; i < fi->fc; i++) {
    00255             int dil, dnlen, bnlen;
    00256
    00257             if (!S_ISDIR(fi->fmodes[i]))
    00258                 continue;
    00259
    00260             dil = fi->dil[i];
    00261             dnlen = strlen(fi->dnl[dil]);
    00262             bnlen = strlen(fi->bnl[i]);
    00263
    00264             for (j = 0; j < fi->dc; j++) {
    00265                 const char * dnl;
    00266                 int jlen;
    00267
    00268                 if (!dnli->active[j] || j == dil) continue;
    00269                 dnl = fi->dnl[j];
    00270                 jlen = strlen(dnl);
    00271                 if (jlen != (dnlen+bnlen+1)) continue;
    00272                 if (strncmp(dnl, fi->dnl[dil], dnlen)) continue;
    00273                 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen)) continue;
    00274                 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
    00275                     continue;
    00276                 /* This directory is included in the package. */
    00277                 dnli->active[j] = 0;
    00278                 /*@innerbreak@*/ break;
    00279             }
    00280         }
    00281
    00282         /* Print only once per package. */
    00283         if (!reverse) {
    00284             j = 0;
    00285             for (i = 0; i < fi->dc; i++) {
    00286                 if (!dnli->active[i]) continue;
    00287                 if (j == 0) {
    00288                     j = 1;
    00289                     rpmMessage(RPMMESS_DEBUG,
    00290         _("========= Directories not explictly included in package:\n"));
    00291                 }
    00292                 rpmMessage(RPMMESS_DEBUG, _("%9d %s\n"), i, fi->dnl[i]);
    00293             }
    00294             if (j)
    00295                 rpmMessage(RPMMESS_DEBUG, "=========\n");
    00296         }
    00297     }
    00298     return dnli;
    00299 }
    00300
    00306 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
    00307         /*@modifies dnli @*/
    00308 {
    00309     const char * dn = NULL;
    00310
    00311     if (dnli) {
    00312         TFI_t fi = dnli->fi;
    00313         int i = -1;
    00314
    00315         if (dnli->active)
    00316         do {
    00317             i = (!dnli->reverse ? dnli->i++ : --dnli->i);
    00318         } while (i >= 0 && i < fi->dc && !dnli->active[i]);
    00319
    00320         if (i >= 0 && i < fi->dc)
    00321             dn = fi->dnl[i];
    00322         else
    00323             i = -1;
    00324         dnli->isave = i;
    00325     }
    00326     return dn;
    00327 }
    00328
    00334 /*@-compmempass@*/
    00335 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
    00336         /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
    00337         /*@defines fsm->li @*/
    00338         /*@releases fsm->path @*/
    00339         /*@modifies fsm @*/
    00340 {
    00341     struct stat * st = &fsm->sb;
    00342     int rc = 0;
    00343     int ix = -1;
    00344     int j;
    00345
    00346     /* Find hard link set. */
    00347     for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
    00348         if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
    00349             break;
    00350     }
    00351
    00352     /* New hard link encountered, add new link to set. */
    00353     if (fsm->li == NULL) {
    00354         fsm->li = xcalloc(1, sizeof(*fsm->li));
    00355         fsm->li->next = NULL;
    00356         fsm->li->sb = *st;      /* structure assignment */
    00357         fsm->li->nlink = st->st_nlink;
    00358         fsm->li->linkIndex = fsm->ix;
    00359         fsm->li->createdPath = -1;
    00360
    00361         fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
    00362         memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
    00363         fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
    00364
    00365         if (fsm->goal == FSM_PKGBUILD)
    00366             fsm->li->linksLeft = st->st_nlink;
    00367         if (fsm->goal == FSM_PKGINSTALL)
    00368             fsm->li->linksLeft = 0;
    00369
    00370         /*@-kepttrans@*/
    00371         fsm->li->next = fsm->links;
    00372         /*@=kepttrans@*/
    00373         fsm->links = fsm->li;
    00374     }
    00375
    00376     if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
    00377     fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
    00378     /*@-observertrans -dependenttrans@*/
    00379     fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
    00380     /*@=observertrans =dependenttrans@*/
    00381     if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
    00382
    00383     if (fsm->goal == FSM_PKGBUILD)
    00384         return (fsm->li->linksLeft > 0);
    00385
    00386     if (fsm->goal != FSM_PKGINSTALL)
    00387         return 0;
    00388
    00389     if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
    00390         return 1;
    00391
    00392     /* Here come the bits, time to choose a non-skipped file name. */
    00393     {   TFI_t fi = fsmGetFi(fsm);
    00394
    00395         for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
    00396             ix = fsm->li->filex[j];
    00397             if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
    00398                 continue;
    00399             break;
    00400         }
    00401     }
    00402
    00403     /* Are all links skipped or not encountered yet? */
    00404     if (ix < 0 || j < 0)
    00405         return 1;       /* XXX W2DO? */
    00406
    00407     /* Save the non-skipped file name and map index. */
    00408     fsm->li->linkIndex = j;
    00409     fsm->path = _free(fsm->path);
    00410     fsm->ix = ix;
    00411     rc = fsmStage(fsm, FSM_MAP);
    00412     /*@-nullstate@*/    /* FIX: fsm->path null annotation? */
    00413     return rc;
    00414     /*@=nullstate@*/
    00415 }
    00416 /*@=compmempass@*/
    00417
    00422 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink_s * li)
    00423         /*@modifies li @*/
    00424 {
    00425     if (li) {
    00426         li->nsuffix = _free(li->nsuffix);       /* XXX elements are shared */
    00427         li->filex = _free(li->filex);
    00428     }
    00429     return _free(li);
    00430 }
    00431
    00432 FSM_t newFSM(void)
    00433 {
    00434     FSM_t fsm = xcalloc(1, sizeof(*fsm));
    00435     return fsm;
    00436 }
    00437
    00438 FSM_t freeFSM(FSM_t fsm)
    00439 {
    00440     if (fsm) {
    00441         fsm->path = _free(fsm->path);
    00442         while ((fsm->li = fsm->links) != NULL) {
    00443             fsm->links = fsm->li->next;
    00444             fsm->li->next = NULL;
    00445             fsm->li = freeHardLink(fsm->li);
    00446         }
    00447         fsm->dnlx = _free(fsm->dnlx);
    00448         fsm->ldn = _free(fsm->ldn);
    00449         fsm->iter = mapFreeIterator(fsm->iter);
    00450     }
    00451     return _free(fsm);
    00452 }
    00453
    00454 int fsmSetup(FSM_t fsm, fileStage goal,
    00455                 const rpmTransactionSet ts, const TFI_t fi, FD_t cfd,
    00456                 unsigned int * archiveSize, const char ** failedFile)
    00457 {
    00458     size_t pos = 0;
    00459     int rc, ec = 0;
    00460
    00461     fsm->goal = goal;
    00462     if (cfd) {
    00463         fsm->cfd = fdLink(cfd, "persist (fsm)");
    00464         pos = fdGetCpioPos(fsm->cfd);
    00465         fdSetCpioPos(fsm->cfd, 0);
    00466     }
    00467     fsm->iter = mapInitIterator(ts, fi);
    00468
    00469     if (fsm->goal == FSM_PKGINSTALL) {
    00470         if (ts && ts->notify) {
    00471             (void)ts->notify(fi->h, RPMCALLBACK_INST_START, 0, fi->archiveSize,
    00472                 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
    00473         }
    00474     }
    00475
    00476     /*@-assignexpose@*/
    00477     fsm->archiveSize = archiveSize;
    00478     if (fsm->archiveSize)
    00479         *fsm->archiveSize = 0;
    00480     fsm->failedFile = failedFile;
    00481     if (fsm->failedFile)
    00482         *fsm->failedFile = NULL;
    00483     /*@=assignexpose@*/
    00484
    00485     memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
    00486     if (fsm->goal == FSM_PKGINSTALL) {
    00487         if (ts && ts->id > 0)
    00488             sprintf(fsm->sufbuf, ";%08x", (unsigned)ts->id);
    00489     }
    00490
    00491     ec = fsm->rc = 0;
    00492     rc = fsmStage(fsm, FSM_CREATE);
    00493     if (rc && !ec) ec = rc;
    00494
    00495     rc = fsmStage(fsm, fsm->goal);
    00496     if (rc && !ec) ec = rc;
    00497
    00498     if (fsm->archiveSize && ec == 0)
    00499         *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
    00500
    00501    return ec;
    00502 }
    00503
    00504 int fsmTeardown(FSM_t fsm) {
    00505     int rc = fsm->rc;
    00506
    00507     if (!rc)
    00508         rc = fsmStage(fsm, FSM_DESTROY);
    00509
    00510     fsm->iter = mapFreeIterator(fsm->iter);
    00511     if (fsm->cfd) {
    00512         fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
    00513         fsm->cfd = NULL;
    00514     }
    00515     fsm->failedFile = NULL;
    00516     /*@-nullstate@*/    /* FIX: fsm->iter null annotation? */
    00517     return rc;
    00518     /*@=nullstate@*/
    00519 }
    00520
    00521 int fsmMapPath(FSM_t fsm)
    00522 {
    00523     TFI_t fi = fsmGetFi(fsm);   /* XXX const except for fstates */
    00524     int rc = 0;
    00525     int i;
    00526
    00527     fsm->osuffix = NULL;
    00528     fsm->nsuffix = NULL;
    00529     fsm->astriplen = 0;
    00530     fsm->action = FA_UNKNOWN;
    00531     fsm->mapFlags = 0;
    00532
    00533     i = fsm->ix;
    00534     if (fi && i >= 0 && i < fi->fc) {
    00535
    00536         fsm->astriplen = fi->astriplen;
    00537         fsm->action = (fi->actions ? fi->actions[i] : fi->action);
    00538         fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
    00539         fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
    00540
    00541         /* src rpms have simple base name in payload. */
    00542         fsm->dirName = fi->dnl[fi->dil[i]];
    00543         fsm->baseName = fi->bnl[i];
    00544
    00545         switch (fsm->action) {
    00546         case FA_SKIP:
    00547             break;
    00548         case FA_SKIPMULTILIB:   /* XXX RPMFILE_STATE_MULTILIB? */
    00549             break;
    00550         case FA_UNKNOWN:
    00551             break;
    00552
    00553         case FA_COPYOUT:
    00554             break;
    00555         case FA_COPYIN:
    00556         case FA_CREATE:
    00557 assert(fi->type == TR_ADDED);
    00558             break;
    00559
    00560         case FA_SKIPNSTATE:
    00561             if (fi->fstates && fi->type == TR_ADDED)
    00562                 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
    00563             break;
    00564
    00565         case FA_SKIPNETSHARED:
    00566             if (fi->fstates && fi->type == TR_ADDED)
    00567                 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
    00568             break;
    00569
    00570         case FA_BACKUP:
    00571             if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
    00572             switch (fi->type) {
    00573             case TR_ADDED:
    00574                 fsm->osuffix = SUFFIX_RPMORIG;
    00575                 break;
    00576             case TR_REMOVED:
    00577                 fsm->osuffix = SUFFIX_RPMSAVE;
    00578                 break;
    00579             }
    00580             break;
    00581
    00582         case FA_ALTNAME:
    00583 assert(fi->type == TR_ADDED);
    00584             if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
    00585                 fsm->nsuffix = SUFFIX_RPMNEW;
    00586             break;
    00587
    00588         case FA_SAVE:
    00589 assert(fi->type == TR_ADDED);
    00590             if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
    00591                 fsm->osuffix = SUFFIX_RPMSAVE;
    00592             break;
    00593         case FA_ERASE:
    00594             assert(fi->type == TR_REMOVED);
    00595             /*
    00596              * XXX TODO: %ghost probably shouldn't be removed, but that changes
    00597              * legacy rpm behavior.
    00598              */
    00599             break;
    00600         default:
    00601             break;
    00602         }
    00603
    00604         if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
    00605             const struct stat * st = &fsm->sb;
    00606             fsm->path = _free(fsm->path);
    00607             /*@-nullstate@*/    /* FIX: fsm->path null annotation? */
    00608             fsm->path = fsmFsPath(fsm, st, fsm->subdir,
    00609                 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
    00610             /*@=nullstate@*/
    00611         }
    00612     }
    00613     return rc;
    00614 }
    00615
    00616 int fsmMapAttrs(FSM_t fsm)
    00617 {
    00618     struct stat * st = &fsm->sb;
    00619     TFI_t fi = fsmGetFi(fsm);
    00620     int i = fsm->ix;
    00621
    00622     if (fi && i >= 0 && i < fi->fc) {
    00623         mode_t perms =
    00624                 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
    00625         mode_t finalMode =
    00626                 (fi->fmodes ? fi->fmodes[i] : perms);
    00627         uid_t finalUid =
    00628                 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
    00629         gid_t finalGid =
    00630                 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
    00631         dev_t finalRdev =
    00632                 (fi->frdevs ? fi->frdevs[i] : 0);
    00633         int_32 finalMtime =
    00634                 (fi->fmtimes ? fi->fmtimes[i] : 0);
    00635
    00636         if (fsm->mapFlags & CPIO_MAP_MODE)
    00637             st->st_mode = (st->st_mode & S_IFMT) | (finalMode & ~S_IFMT);
    00638         if (fsm->mapFlags & CPIO_MAP_TYPE) {
    00639             st->st_mode = (st->st_mode & ~S_IFMT) | (finalMode & S_IFMT);
    00640             if ((S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
    00641             && st->st_nlink == 0)
    00642                 st->st_nlink = 1;
    00643             st->st_rdev = finalRdev;
    00644             st->st_mtime = finalMtime;
    00645         }
    00646         if (fsm->mapFlags & CPIO_MAP_UID)
    00647             st->st_uid = finalUid;
    00648         if (fsm->mapFlags & CPIO_MAP_GID)
    00649             st->st_gid = finalGid;
    00650
    00651         {   rpmTransactionSet ts = fsmGetTs(fsm);
    00652
    00653             if (ts != NULL && !(ts->transFlags & RPMTRANS_FLAG_NOMD5)) {
    00654                 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
    00655             } else {
    00656                 fsm->fmd5sum = NULL;
    00657             }
    00658         }
    00659
    00660     }
    00661     return 0;
    00662 }
    00663
    00670 static int expandRegular(/*@special@*/ FSM_t fsm)
    00671         /*@uses fsm->sb @*/
    00672         /*@modifies fsm, fileSystem @*/
    00673 {
    00674     const char * fmd5sum;
    00675     const struct stat * st = &fsm->sb;
    00676     int left = st->st_size;
    00677     int rc = 0;
    00678
    00679     rc = fsmStage(fsm, FSM_WOPEN);
    00680     if (rc)
    00681         goto exit;
    00682
    00683     /* XXX md5sum's will break on repackaging that includes modified files. */
    00684     fmd5sum = fsm->fmd5sum;
    00685
    00686     /* XXX This doesn't support brokenEndian checks. */
    00687     if (st->st_size > 0 && fmd5sum)
    00688         fdInitDigest(fsm->wfd, PGPHASHALGO_MD5, 0);
    00689
    00690     while (left) {
    00691
    00692         fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
    00693         rc = fsmStage(fsm, FSM_DREAD);
    00694         if (rc)
    00695             goto exit;
    00696
    00697         rc = fsmStage(fsm, FSM_WRITE);
    00698         if (rc)
    00699             goto exit;
    00700
    00701         left -= fsm->wrnb;
    00702
    00703         /* don't call this with fileSize == fileComplete */
    00704         if (!rc && left)
    00705             (void) fsmStage(fsm, FSM_NOTIFY);
    00706     }
    00707
    00708     if (st->st_size > 0 && fmd5sum) {
    00709         const char * md5sum = NULL;
    00710
    00711         (void) Fflush(fsm->wfd);
    00712         fdFiniDigest(fsm->wfd, PGPHASHALGO_MD5, (void **)&md5sum, NULL, 1);
    00713
    00714         if (md5sum == NULL) {
    00715             rc = CPIOERR_MD5SUM_MISMATCH;
    00716         } else {
    00717             if (strcmp(md5sum, fmd5sum))
    00718                 rc = CPIOERR_MD5SUM_MISMATCH;
    00719             md5sum = _free(md5sum);
    00720         }
    00721     }
    00722
    00723 exit:
    00724     (void) fsmStage(fsm, FSM_WCLOSE);
    00725     return rc;
    00726 }
    00727
    00734 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
    00735         /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
    00736         /*@modifies fsm, fileSystem @*/
    00737 {
    00738     const char * path = fsm->path;
    00739     const char * opath = fsm->opath;
    00740     struct stat * st = &fsm->sb;
    00741     struct stat * ost = &fsm->osb;
    00742     size_t pos = fdGetCpioPos(fsm->cfd);
    00743     char * symbuf = NULL;
    00744     int left;
    00745     int xx;
    00746     int rc;
    00747
    00748     st->st_size = (writeData ? ost->st_size : 0);
    00749
    00750     if (S_ISDIR(st->st_mode)) {
    00751         st->st_size = 0;
    00752     } else if (S_ISLNK(st->st_mode)) {
    00753         /*
    00754          * While linux puts the size of a symlink in the st_size field,
    00755          * I don't think that's a specified standard.
    00756          */
    00757         /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
    00758         rc = fsmStage(fsm, FSM_READLINK);
    00759         if (rc) goto exit;
    00760         st->st_size = fsm->rdnb;
    00761         symbuf = alloca_strdup(fsm->rdbuf);     /* XXX save readlink return. */
    00762     }
    00763
    00764     if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
    00765         int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
    00766         char * t = alloca(nb);
    00767         *t = '\0';
    00768         fsm->path = t;
    00769         if (fsm->mapFlags & CPIO_MAP_ADDDOT)
    00770             *t++ = '.';
    00771         t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
    00772     } else if (fsm->mapFlags & CPIO_MAP_PATH) {
    00773         TFI_t fi = fsmGetFi(fsm);
    00774         fsm->path =
    00775             (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
    00776     }
    00777
    00778     rc = fsmStage(fsm, FSM_HWRITE);
    00779     fsm->path = path;
    00780     if (rc) goto exit;
    00781
    00782     if (writeData && S_ISREG(st->st_mode)) {
    00783 #if HAVE_MMAP
    00784         char * rdbuf = NULL;
    00785         void * mapped = (void *)-1;
    00786         size_t nmapped;
    00787 #endif
    00788
    00789         rc = fsmStage(fsm, FSM_ROPEN);
    00790         if (rc) goto exit;
    00791
    00792         /* XXX unbuffered mmap generates *lots* of fdio debugging */
    00793 #if HAVE_MMAP
    00794         nmapped = 0;
    00795         mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
    00796         if (mapped != (void *)-1) {
    00797             rdbuf = fsm->rdbuf;
    00798             fsm->rdbuf = (char *) mapped;
    00799             fsm->rdlen = nmapped = st->st_size;
    00800 #if defined(MADV_DONTNEED)
    00801             xx = madvise(mapped, nmapped, MADV_DONTNEED);
    00802 #endif
    00803         }
    00804 #endif
    00805
    00806         left = st->st_size;
    00807
    00808         while (left) {
    00809 #if HAVE_MMAP
    00810           if (mapped != (void *)-1) {
    00811             fsm->rdnb = nmapped;
    00812           } else
    00813 #endif
    00814           {
    00815             fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
    00816             rc = fsmStage(fsm, FSM_READ);
    00817             if (rc) goto exit;
    00818           }
    00819
    00820             /* XXX DWRITE uses rdnb for I/O length. */
    00821             rc = fsmStage(fsm, FSM_DWRITE);
    00822             if (rc) goto exit;
    00823
    00824             left -= fsm->wrnb;
    00825         }
    00826
    00827 #if HAVE_MMAP
    00828         if (mapped != (void *)-1) {
    00829 #if defined(MADV_DONTNEED)
    00830             xx = madvise(mapped, nmapped, MADV_DONTNEED);
    00831 #endif
    00832             /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
    00833             fsm->rdbuf = rdbuf;
    00834         }
    00835 #endif
    00836
    00837     } else if (writeData && S_ISLNK(st->st_mode)) {
    00838         /* XXX DWRITE uses rdnb for I/O length. */
    00839         strcpy(fsm->rdbuf, symbuf);     /* XXX restore readlink buffer. */
    00840         fsm->rdnb = strlen(symbuf);
    00841         rc = fsmStage(fsm, FSM_DWRITE);
    00842         if (rc) goto exit;
    00843     }
    00844
    00845     rc = fsmStage(fsm, FSM_PAD);
    00846     if (rc) goto exit;
    00847
    00848     {   const rpmTransactionSet ts = fsmGetTs(fsm);
    00849         TFI_t fi = fsmGetFi(fsm);
    00850         if (ts && fi && ts->notify) {
    00851             size_t size = (fdGetCpioPos(fsm->cfd) - pos);
    00852             /*@-modunconnomods@*/
    00853             (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
    00854                         (fi->ap ? fi->ap->key : NULL), ts->notifyData);
    00855             /*@=modunconnomods@*/
    00856         }
    00857     }
    00858
    00859     rc = 0;
    00860
    00861 exit:
    00862     if (fsm->rfd)
    00863         (void) fsmStage(fsm, FSM_RCLOSE);
    00864     /*@-dependenttrans@*/
    00865     fsm->opath = opath;
    00866     fsm->path = path;
    00867     /*@=dependenttrans@*/
    00868     return rc;
    00869 }
    00870
    00876 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
    00877         /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
    00878         /*@modifies fsm, fileSystem @*/
    00879 {
    00880     const char * path = fsm->path;
    00881     const char * nsuffix = fsm->nsuffix;
    00882     int iterIndex = fsm->ix;
    00883     int ec = 0;
    00884     int rc;
    00885     int i;
    00886
    00887     fsm->path = NULL;
    00888     fsm->nsuffix = NULL;
    00889     fsm->ix = -1;
    00890
    00891     for (i = fsm->li->nlink - 1; i >= 0; i--) {
    00892
    00893         if (fsm->li->filex[i] < 0) continue;
    00894
    00895         fsm->ix = fsm->li->filex[i];
    00896         rc = fsmStage(fsm, FSM_MAP);
    00897
    00898         /* Write data after last link. */
    00899         rc = writeFile(fsm, (i == 0));
    00900         if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
    00901             ec = rc;
    00902             *fsm->failedFile = xstrdup(fsm->path);
    00903         }
    00904
    00905         fsm->path = _free(fsm->path);
    00906         fsm->li->filex[i] = -1;
    00907     }
    00908
    00909     fsm->ix = iterIndex;
    00910     fsm->nsuffix = nsuffix;
    00911     fsm->path = path;
    00912     return ec;
    00913 }
    00914
    00920 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
    00921         /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
    00922         /*@modifies fsm, fileSystem @*/
    00923 {
    00924     const char * path = fsm->path;
    00925     const char * opath = fsm->opath;
    00926     const char * nsuffix = fsm->nsuffix;
    00927     int iterIndex = fsm->ix;
    00928     int ec = 0;
    00929     int rc;
    00930     int i;
    00931
    00932     fsm->path = NULL;
    00933     fsm->opath = NULL;
    00934     fsm->nsuffix = NULL;
    00935     fsm->ix = -1;
    00936
    00937     fsm->ix = fsm->li->filex[fsm->li->createdPath];
    00938     rc = fsmStage(fsm, FSM_MAP);
    00939     fsm->opath = fsm->path;
    00940     fsm->path = NULL;
    00941     for (i = 0; i < fsm->li->nlink; i++) {
    00942         if (fsm->li->filex[i] < 0) continue;
    00943         if (fsm->li->createdPath == i) continue;
    00944
    00945         fsm->ix = fsm->li->filex[i];
    00946         fsm->path = _free(fsm->path);
    00947         rc = fsmStage(fsm, FSM_MAP);
    00948         if (XFA_SKIPPING(fsm->action)) continue;
    00949
    00950         rc = fsmStage(fsm, FSM_VERIFY);
    00951         if (!rc) continue;
    00952         if (rc != CPIOERR_LSTAT_FAILED) break;
    00953
    00954         /* XXX link(fsm->opath, fsm->path) */
    00955         rc = fsmStage(fsm, FSM_LINK);
    00956         if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
    00957             ec = rc;
    00958             *fsm->failedFile = xstrdup(fsm->path);
    00959         }
    00960
    00961         fsm->li->linksLeft--;
    00962     }
    00963     fsm->path = _free(fsm->path);
    00964     fsm->opath = _free(fsm->opath);
    00965
    00966     fsm->ix = iterIndex;
    00967     fsm->nsuffix = nsuffix;
    00968     fsm->path = path;
    00969     fsm->opath = opath;
    00970     return ec;
    00971 }
    00972
    00978 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
    00979         /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
    00980                 fsm->li, fsm->links @*/
    00981         /*@modifies fsm, fileSystem @*/
    00982 {
    00983     const char * path = fsm->path;
    00984     const char * nsuffix = fsm->nsuffix;
    00985     int iterIndex = fsm->ix;
    00986     struct stat * st = &fsm->sb;
    00987     int rc = 0;
    00988     int i;
    00989
    00990     fsm->path = NULL;
    00991     fsm->nsuffix = NULL;
    00992     fsm->ix = -1;
    00993
    00994     for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
    00995         if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
    00996             break;
    00997     }
    00998
    00999     for (i = 0; i < fsm->li->nlink; i++) {
    01000         if (fsm->li->filex[i] < 0) continue;
    01001         fsm->ix = fsm->li->filex[i];
    01002         rc = fsmStage(fsm, FSM_MAP);
    01003         if (!XFA_SKIPPING(fsm->action))
    01004             rc = fsmStage(fsm, FSM_COMMIT);
    01005         fsm->path = _free(fsm->path);
    01006         fsm->li->filex[i] = -1;
    01007     }
    01008
    01009     fsm->ix = iterIndex;
    01010     fsm->nsuffix = nsuffix;
    01011     fsm->path = path;
    01012     return rc;
    01013 }
    01014
    01020 /*@-compdef@*/
    01021 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
    01022         /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
    01023         /*@modifies fsm, fileSystem @*/
    01024 {
    01025     const char * path = fsm->path;
    01026     void * dnli = dnlInitIterator(fsm, 1);
    01027     char * dn = fsm->rdbuf;
    01028     int dc = dnlCount(dnli);
    01029     int rc = 0;
    01030
    01031     fsm->path = NULL;
    01032     dn[0] = '\0';
    01033     /*@-observertrans -dependenttrans@*/
    01034     if (fsm->ldn != NULL && fsm->dnlx != NULL)
    01035     while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
    01036         int dnlen = strlen(fsm->path);
    01037         char * te;
    01038
    01039         dc = dnlIndex(dnli);
    01040         if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
    01041             continue;
    01042
    01043         /* Copy to avoid const on fsm->path. */
    01044         te = stpcpy(dn, fsm->path) - 1;
    01045         fsm->path = dn;
    01046
    01047         /* Remove generated directories. */
    01048         do {
    01049             if (*te == '/') {
    01050                 *te = '\0';
    01051                 rc = fsmStage(fsm, FSM_RMDIR);
    01052                 *te = '/';
    01053             }
    01054             if (rc)
    01055                 /*@innerbreak@*/ break;
    01056             te--;
    01057         } while ((te - dn) > fsm->dnlx[dc]);
    01058     }
    01059     dnli = dnlFreeIterator(dnli);
    01060     /*@=observertrans =dependenttrans@*/
    01061
    01062     fsm->path = path;
    01063     return rc;
    01064 }
    01065 /*@=compdef@*/
    01066
    01072 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
    01073         /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
    01074                 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
    01075         /*@defines fsm->dnlx, fsm->ldn @*/
    01076         /*@modifies fsm, fileSystem @*/
    01077 {
    01078     struct stat * st = &fsm->sb;
    01079     struct stat * ost = &fsm->osb;
    01080     const char * path = fsm->path;
    01081     mode_t st_mode = st->st_mode;
    01082     void * dnli = dnlInitIterator(fsm, 0);
    01083     char * dn = fsm->rdbuf;
    01084     int dc = dnlCount(dnli);
    01085     int rc = 0;
    01086     int i;
    01087
    01088     fsm->path = NULL;
    01089
    01090     dn[0] = '\0';
    01091     fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
    01092     /*@-observertrans -dependenttrans@*/
    01093     if (fsm->dnlx != NULL)
    01094     while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
    01095         int dnlen = strlen(fsm->path);
    01096         char * te;
    01097
    01098         dc = dnlIndex(dnli);
    01099         if (dc < 0) continue;
    01100         fsm->dnlx[dc] = dnlen;
    01101         if (dnlen <= 1)
    01102             continue;
    01103
    01104         /*@-compdef -nullpass@*/        /* FIX: fsm->ldn not defined ??? */
    01105         if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
    01106             continue;
    01107         /*@=compdef =nullpass@*/
    01108
    01109         /* Copy to avoid const on fsm->path. */
    01110         (void) stpcpy(dn, fsm->path);
    01111         fsm->path = dn;
    01112
    01113         /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
    01114         for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
    01115             if (*te != '/') continue;
    01116
    01117             *te = '\0';
    01118
    01119             /* Already validated? */
    01120             /*@-usedef -compdef -nullpass -nullderef@*/
    01121             if (i < fsm->ldnlen &&
    01122                 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
    01123                 !strncmp(fsm->path, fsm->ldn, i))
    01124             {
    01125                 *te = '/';
    01126                 /* Move pre-existing path marker forward. */
    01127                 fsm->dnlx[dc] = (te - dn);
    01128                 continue;
    01129             }
    01130             /*@=usedef =compdef =nullpass =nullderef@*/
    01131
    01132             /* Validate next component of path. */
    01133             rc = fsmStage(fsm, FSM_LSTAT);
    01134             *te = '/';
    01135
    01136             /* Directory already exists? */
    01137             if (rc == 0 && S_ISDIR(ost->st_mode)) {
    01138                 /* Move pre-existing path marker forward. */
    01139                 fsm->dnlx[dc] = (te - dn);
    01140             } else if (rc == CPIOERR_LSTAT_FAILED) {
    01141                 TFI_t fi = fsmGetFi(fsm);
    01142                 *te = '\0';
    01143                 st->st_mode = S_IFDIR | (fi->dperms & 07777);
    01144                 rc = fsmStage(fsm, FSM_MKDIR);
    01145                 if (!rc)
    01146                     rpmMessage(RPMMESS_DEBUG,
    01147                         _("%s directory created with perms %04o.\n"),
    01148                         fsm->path, (unsigned)(st->st_mode & 07777));
    01149                 *te = '/';
    01150             }
    01151             if (rc)
    01152                 /*@innerbreak@*/ break;
    01153         }
    01154         if (rc) break;
    01155
    01156         /* Save last validated path. */
    01157         if (fsm->ldnalloc < (dnlen + 1)) {
    01158             fsm->ldnalloc = dnlen + 100;
    01159             fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
    01160         }
    01161         if (fsm->ldn != NULL) { /* XXX can't happen */
    01162             strcpy(fsm->ldn, fsm->path);
    01163             fsm->ldnlen = dnlen;
    01164         }
    01165     }
    01166     dnli = dnlFreeIterator(dnli);
    01167     /*@=observertrans =dependenttrans@*/
    01168
    01169     fsm->path = path;
    01170     st->st_mode = st_mode;              /* XXX restore st->st_mode */
    01171     return rc;
    01172 }
    01173
    01174 #ifdef  NOTYET
    01175
    01180 static int fsmStat(FSM_t fsm)
    01181 {
    01182     int saveerrno = errno;
    01183     int rc = 0;
    01184
    01185     if (fsm->path != NULL) {
    01186         int saveernno = errno;
    01187         rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
    01188                         ? FSM_LSTAT : FSM_STAT));
    01189         if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
    01190             errno = saveerrno;
    01191             rc = 0;
    01192             fsm->exists = 0;
    01193         } else if (rc == 0) {
    01194             fsm->exists = 1;
    01195         }
    01196     } else {
    01197         /* Skip %ghost files on build. */
    01198         fsm->exists = 0;
    01199     }
    01200     return rc;
    01201 }
    01202 #endif
    01203
    01204 /*@-compmempass@*/
    01205 int fsmStage(FSM_t fsm, fileStage stage)
    01206 {
    01207 #ifdef  UNUSED
    01208     fileStage prevStage = fsm->stage;
    01209     const char * const prev = fileStageString(prevStage);
    01210 #endif
    01211     static int modulo = 4;
    01212     const char * const cur = fileStageString(stage);
    01213     struct stat * st = &fsm->sb;
    01214     struct stat * ost = &fsm->osb;
    01215     int saveerrno = errno;
    01216     int rc = fsm->rc;
    01217     size_t left;
    01218     int i;
    01219
    01220 #define _fafilter(_a)   \
    01221     (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
    01222         ? fileActionString(_a) : "")
    01223
    01224     if (stage & FSM_DEAD) {
    01225         /* do nothing */
    01226     } else if (stage & FSM_INTERNAL) {
    01227         if (_fsm_debug && !(stage & FSM_SYSCALL))
    01228             rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
    01229                 cur,
    01230                 (unsigned)st->st_mode, (int)st->st_nlink,
    01231                 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
    01232                 (fsm->path ? fsm->path : ""),
    01233                 _fafilter(fsm->action));
    01234     } else {
    01235         fsm->stage = stage;
    01236         if (_fsm_debug || !(stage & FSM_VERBOSE))
    01237             rpmMessage(RPMMESS_DEBUG, "%-8s  %06o%3d (%4d,%4d)%10d %s %s\n",
    01238                 cur,
    01239                 (unsigned)st->st_mode, (int)st->st_nlink,
    01240                 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
    01241                 (fsm->path ? fsm->path + fsm->astriplen : ""),
    01242                 _fafilter(fsm->action));
    01243     }
    01244 #undef  _fafilter
    01245
    01246     switch (stage) {
    01247     case FSM_UNKNOWN:
    01248         break;
    01249     case FSM_PKGINSTALL:
    01250         while (1) {
    01251             /* Clean fsm, free'ing memory. Read next archive header. */
    01252             rc = fsmStage(fsm, FSM_INIT);
    01253
    01254             /* Exit on end-of-payload. */
    01255             if (rc == CPIOERR_HDR_TRAILER) {
    01256                 rc = 0;
    01257                 /*@loopbreak@*/ break;
    01258             }
    01259
    01260             /* Exit on error. */
    01261             if (rc) {
    01262                 fsm->postpone = 1;
    01263                 (void) fsmStage(fsm, FSM_UNDO);
    01264                 /*@loopbreak@*/ break;
    01265             }
    01266
    01267             /* Extract file from archive. */
    01268             rc = fsmStage(fsm, FSM_PROCESS);
    01269             if (rc) {
    01270                 (void) fsmStage(fsm, FSM_UNDO);
    01271                 /*@loopbreak@*/ break;
    01272             }
    01273
    01274             /* Notify on success. */
    01275             (void) fsmStage(fsm, FSM_NOTIFY);
    01276
    01277             rc = fsmStage(fsm, FSM_FINI);
    01278             if (rc) {
    01279                 /*@loopbreak@*/ break;
    01280             }
    01281         }
    01282         break;
    01283     case FSM_PKGERASE:
    01284     case FSM_PKGCOMMIT:
    01285         while (1) {
    01286             /* Clean fsm, free'ing memory. */
    01287             rc = fsmStage(fsm, FSM_INIT);
    01288
    01289             /* Exit on end-of-payload. */
    01290             if (rc == CPIOERR_HDR_TRAILER) {
    01291                 rc = 0;
    01292                 /*@loopbreak@*/ break;
    01293             }
    01294
    01295             /* Rename/erase next item. */
    01296             if (fsmStage(fsm, FSM_FINI))
    01297                 /*@loopbreak@*/ break;
    01298         }
    01299         break;
    01300     case FSM_PKGBUILD:
    01301         while (1) {
    01302
    01303             rc = fsmStage(fsm, FSM_INIT);
    01304
    01305             /* Exit on end-of-payload. */
    01306             if (rc == CPIOERR_HDR_TRAILER) {
    01307                 rc = 0;
    01308                 /*@loopbreak@*/ break;
    01309             }
    01310
    01311             /* Exit on error. */
    01312             if (rc) {
    01313                 fsm->postpone = 1;
    01314                 (void) fsmStage(fsm, FSM_UNDO);
    01315                 /*@loopbreak@*/ break;
    01316             }
    01317
    01318             /* Copy file into archive. */
    01319             rc = fsmStage(fsm, FSM_PROCESS);
    01320             if (rc) {
    01321                 (void) fsmStage(fsm, FSM_UNDO);
    01322                 /*@loopbreak@*/ break;
    01323             }
    01324
    01325             if (fsmStage(fsm, FSM_FINI))
    01326                 /*@loopbreak@*/ break;
    01327         }
    01328
    01329         /* Flush partial sets of hard linked files. */
    01330         if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
    01331             int nlink, j;
    01332             while ((fsm->li = fsm->links) != NULL) {
    01333                 fsm->links = fsm->li->next;
    01334                 fsm->li->next = NULL;
    01335
    01336                 /* Re-calculate link count for archive header. */
    01337                 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
    01338                     if (fsm->li->filex[i] < 0) continue;
    01339                     nlink++;
    01340                     if (j == -1) j = i;
    01341                 }
    01342                 /* XXX force the contents out as well. */
    01343                 if (j != 0) {
    01344                     fsm->li->filex[0] = fsm->li->filex[j];
    01345                     fsm->li->filex[j] = -1;
    01346                 }
    01347                 fsm->li->sb.st_nlink = nlink;
    01348
    01349                 fsm->sb = fsm->li->sb;  /* structure assignment */
    01350                 fsm->osb = fsm->sb;     /* structure assignment */
    01351
    01352                 if (!rc) rc = writeLinkedFile(fsm);
    01353
    01354                 fsm->li = freeHardLink(fsm->li);
    01355             }
    01356         }
    01357
    01358         if (!rc)
    01359             rc = fsmStage(fsm, FSM_TRAILER);
    01360
    01361         break;
    01362     case FSM_CREATE:
    01363         {   rpmTransactionSet ts = fsmGetTs(fsm);
    01364 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
    01365             fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
    01366                         fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
    01367 #undef _tsmask
    01368         }
    01369         fsm->path = _free(fsm->path);
    01370         fsm->opath = _free(fsm->opath);
    01371         fsm->dnlx = _free(fsm->dnlx);
    01372
    01373         fsm->ldn = _free(fsm->ldn);
    01374         fsm->ldnalloc = fsm->ldnlen = 0;
    01375
    01376         fsm->rdsize = fsm->wrsize = 0;
    01377         fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
    01378         fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
    01379         if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
    01380             fsm->rdsize = 8 * BUFSIZ;
    01381             fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
    01382             fsm->wrsize = 8 * BUFSIZ;
    01383             fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
    01384         }
    01385
    01386         fsm->mkdirsdone = 0;
    01387         fsm->ix = -1;
    01388         fsm->links = NULL;
    01389         fsm->li = NULL;
    01390         errno = 0;      /* XXX get rid of EBADF */
    01391
    01392         /* Detect and create directories not explicitly in package. */
    01393         if (fsm->goal == FSM_PKGINSTALL) {
    01394             rc = fsmStage(fsm, FSM_MKDIRS);
    01395             if (!rc) fsm->mkdirsdone = 1;
    01396         }
    01397
    01398         break;
    01399     case FSM_INIT:
    01400         fsm->path = _free(fsm->path);
    01401         fsm->postpone = 0;
    01402         fsm->diskchecked = fsm->exists = 0;
    01403         fsm->subdir = NULL;
    01404         fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
    01405         fsm->action = FA_UNKNOWN;
    01406         fsm->osuffix = NULL;
    01407         fsm->nsuffix = NULL;
    01408
    01409         if (fsm->goal == FSM_PKGINSTALL) {
    01410             /* Read next header from payload, checking for end-of-payload. */
    01411             rc = fsmStage(fsm, FSM_NEXT);
    01412         }
    01413         if (rc) break;
    01414
    01415         /* Identify mapping index. */
    01416         fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
    01417                 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
    01418
    01419         /* Detect end-of-loop and/or mapping error. */
    01420         if (fsm->ix < 0) {
    01421             if (fsm->goal == FSM_PKGINSTALL) {
    01422 #if 0
    01423                 rpmMessage(RPMMESS_WARNING,
    01424                     _("archive file %s was not found in header file list\n"),
    01425                         fsm->path);
    01426 #endif
    01427                 if (fsm->failedFile && *fsm->failedFile == NULL)
    01428                     *fsm->failedFile = xstrdup(fsm->path);
    01429                 rc = CPIOERR_UNMAPPED_FILE;
    01430             } else {
    01431                 rc = CPIOERR_HDR_TRAILER;
    01432             }
    01433             break;
    01434         }
    01435
    01436         /* On non-install, mode must be known so that dirs don't get suffix. */
    01437         if (fsm->goal != FSM_PKGINSTALL) {
    01438             TFI_t fi = fsmGetFi(fsm);
    01439             st->st_mode = fi->fmodes[fsm->ix];
    01440         }
    01441
    01442         /* Generate file path. */
    01443         rc = fsmStage(fsm, FSM_MAP);
    01444         if (rc) break;
    01445
    01446         /* Perform lstat/stat for disk file. */
    01447 #ifdef  NOTYET
    01448         rc = fsmStat(fsm);
    01449 #else
    01450         if (fsm->path != NULL) {
    01451             rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
    01452                         ? FSM_LSTAT : FSM_STAT));
    01453             if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
    01454                 errno = saveerrno;
    01455                 rc = 0;
    01456                 fsm->exists = 0;
    01457             } else if (rc == 0) {
    01458                 fsm->exists = 1;
    01459             }
    01460         } else {
    01461             /* Skip %ghost files on build. */
    01462             fsm->exists = 0;
    01463         }
    01464 #endif
    01465         fsm->diskchecked = 1;
    01466         if (rc) break;
    01467
    01468         /* On non-install, the disk file stat is what's remapped. */
    01469         if (fsm->goal != FSM_PKGINSTALL)
    01470             *st = *ost;                 /* structure assignment */
    01471
    01472         /* Remap file perms, owner, and group. */
    01473         rc = fsmMapAttrs(fsm);
    01474         if (rc) break;
    01475
    01476         fsm->postpone = XFA_SKIPPING(fsm->action);
    01477         if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
    01478             /*@-evalorder@*/
    01479             if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
    01480                 fsm->postpone = saveHardLink(fsm);
    01481             /*@=evalorder@*/
    01482         }
    01483         break;
    01484     case FSM_PRE:
    01485         break;
    01486     case FSM_MAP:
    01487         rc = fsmMapPath(fsm);
    01488         break;
    01489     case FSM_MKDIRS:
    01490         rc = fsmMkdirs(fsm);
    01491         break;
    01492     case FSM_RMDIRS:
    01493         if (fsm->dnlx)
    01494             rc = fsmRmdirs(fsm);
    01495         break;
    01496     case FSM_PROCESS:
    01497         if (fsm->postpone) {
    01498             if (fsm->goal == FSM_PKGINSTALL)
    01499                 rc = fsmStage(fsm, FSM_EAT);
    01500             break;
    01501         }
    01502
    01503         if (fsm->goal == FSM_PKGBUILD) {
    01504             if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
    01505                 struct hardLink_s * li, * prev;
    01506
    01507 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
    01508                 rc = writeLinkedFile(fsm);
    01509                 if (rc) break;  /* W2DO? */
    01510
    01511                 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
    01512                      if (li == fsm->li)
    01513                         /*@loopbreak@*/ break;
    01514
    01515                 if (prev == NULL)
    01516                     fsm->links = fsm->li->next;
    01517                 else
    01518                     prev->next = fsm->li->next;
    01519                 fsm->li->next = NULL;
    01520                 fsm->li = freeHardLink(fsm->li);
    01521             } else {
    01522                 rc = writeFile(fsm, 1);
    01523             }
    01524             break;
    01525         }
    01526
    01527         if (fsm->goal != FSM_PKGINSTALL)
    01528             break;
    01529
    01530         if (S_ISREG(st->st_mode)) {
    01531             const char * path = fsm->path;
    01532             if (fsm->osuffix)
    01533                 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
    01534             rc = fsmStage(fsm, FSM_VERIFY);
    01535
    01536             if (rc == 0 && fsm->osuffix) {
    01537                 const char * opath = fsm->opath;
    01538                 fsm->opath = fsm->path;
    01539                 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
    01540                 rc = fsmStage(fsm, FSM_RENAME);
    01541                 if (!rc)
    01542                     rpmMessage(RPMMESS_WARNING,
    01543                         _("%s saved as %s\n"), fsm->opath, fsm->path);
    01544                 fsm->path = _free(fsm->path);
    01545                 fsm->opath = opath;
    01546             }
    01547
    01548             /*@-dependenttrans@*/
    01549             fsm->path = path;
    01550             /*@=dependenttrans@*/
    01551             if (rc != CPIOERR_LSTAT_FAILED) return rc;
    01552             rc = expandRegular(fsm);
    01553         } else if (S_ISDIR(st->st_mode)) {
    01554             mode_t st_mode = st->st_mode;
    01555             rc = fsmStage(fsm, FSM_VERIFY);
    01556             if (rc == CPIOERR_LSTAT_FAILED) {
    01557                 st->st_mode &= ~07777;          /* XXX abuse st->st_mode */
    01558                 st->st_mode |=  00700;
    01559                 rc = fsmStage(fsm, FSM_MKDIR);
    01560                 st->st_mode = st_mode;          /* XXX restore st->st_mode */
    01561             }
    01562         } else if (S_ISLNK(st->st_mode)) {
    01563             const char * opath = fsm->opath;
    01564
    01565             if ((st->st_size + 1) > fsm->rdsize) {
    01566                 rc = CPIOERR_HDR_SIZE;
    01567                 break;
    01568             }
    01569
    01570             fsm->wrlen = st->st_size;
    01571             rc = fsmStage(fsm, FSM_DREAD);
    01572             if (!rc && fsm->rdnb != fsm->wrlen)
    01573                 rc = CPIOERR_READ_FAILED;
    01574             if (rc) break;
    01575
    01576             fsm->wrbuf[st->st_size] = '\0';
    01577             /* XXX symlink(fsm->opath, fsm->path) */
    01578             /*@-dependenttrans@*/
    01579             fsm->opath = fsm->wrbuf;
    01580             /*@=dependenttrans@*/
    01581             rc = fsmStage(fsm, FSM_VERIFY);
    01582             if (rc == CPIOERR_LSTAT_FAILED)
    01583                 rc = fsmStage(fsm, FSM_SYMLINK);
    01584             fsm->opath = opath;         /* XXX restore fsm->path */
    01585         } else if (S_ISFIFO(st->st_mode)) {
    01586             mode_t st_mode = st->st_mode;
    01587             /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
    01588             rc = fsmStage(fsm, FSM_VERIFY);
    01589             if (rc == CPIOERR_LSTAT_FAILED) {
    01590                 st->st_mode = 0000;             /* XXX abuse st->st_mode */
    01591                 rc = fsmStage(fsm, FSM_MKFIFO);
    01592                 st->st_mode = st_mode;  /* XXX restore st->st_mode */
    01593             }
    01594         } else if (S_ISCHR(st->st_mode) ||
    01595                    S_ISBLK(st->st_mode) ||
    01596     /*@-unrecog@*/ S_ISSOCK(st->st_mode) /*@=unrecog@*/)
    01597         {
    01598             rc = fsmStage(fsm, FSM_VERIFY);
    01599             if (rc == CPIOERR_LSTAT_FAILED)
    01600                 rc = fsmStage(fsm, FSM_MKNOD);
    01601         } else {
    01602             /* XXX Special case /dev/log, which shouldn't be packaged anyways */
    01603             if (strncmp(fsm->path, "/dev/log", sizeof("/dev/log")-1))
    01604                 rc = CPIOERR_UNKNOWN_FILETYPE;
    01605         }
    01606         if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
    01607             fsm->li->createdPath = fsm->li->linkIndex;
    01608             rc = fsmMakeLinks(fsm);
    01609         }
    01610         break;
    01611     case FSM_POST:
    01612         break;
    01613     case FSM_MKLINKS:
    01614         rc = fsmMakeLinks(fsm);
    01615         break;
    01616     case FSM_NOTIFY:            /* XXX move from fsm to psm -> tsm */
    01617         if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
    01618             rpmTransactionSet ts = fsmGetTs(fsm);
    01619             TFI_t fi = fsmGetFi(fsm);
    01620             if (ts && ts->notify && fi)
    01621                 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
    01622                         fdGetCpioPos(fsm->cfd), fi->archiveSize,
    01623                         (fi->ap ? fi->ap->key : NULL), ts->notifyData);
    01624         }
    01625         break;
    01626     case FSM_UNDO:
    01627         if (fsm->postpone)
    01628             break;
    01629         if (fsm->goal == FSM_PKGINSTALL) {
    01630             (void) fsmStage(fsm,
    01631                 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
    01632
    01633 #ifdef  NOTYET  /* XXX remove only dirs just created, not all. */
    01634             if (fsm->dnlx)
    01635                 (void) fsmStage(fsm, FSM_RMDIRS);
    01636 #endif
    01637             errno = saveerrno;
    01638         }
    01639         if (fsm->failedFile && *fsm->failedFile == NULL)
    01640             *fsm->failedFile = xstrdup(fsm->path);
    01641         break;
    01642     case FSM_FINI:
    01643         if (!fsm->postpone && fsm->commit) {
    01644             if (fsm->goal == FSM_PKGINSTALL)
    01645                 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
    01646                         ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
    01647             if (fsm->goal == FSM_PKGCOMMIT)
    01648                 rc = fsmStage(fsm, FSM_COMMIT);
    01649             if (fsm->goal == FSM_PKGERASE)
    01650                 rc = fsmStage(fsm, FSM_COMMIT);
    01651         }
    01652         fsm->path = _free(fsm->path);
    01653         fsm->opath = _free(fsm->opath);
    01654         memset(st, 0, sizeof(*st));
    01655         memset(ost, 0, sizeof(*ost));
    01656         break;
    01657     case FSM_COMMIT:
    01658         /* Rename pre-existing modified or unmanaged file. */
    01659         if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
    01660             const char * opath = fsm->opath;
    01661             const char * path = fsm->path;
    01662             /*@-nullstate@*/    /* FIX: fsm->opath null annotation? */
    01663             fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
    01664             /*@=nullstate@*/
    01665             /*@-nullstate@*/    /* FIX: fsm->path null annotation? */
    01666             fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
    01667             /*@=nullstate@*/
    01668             rc = fsmStage(fsm, FSM_RENAME);
    01669             if (!rc) {
    01670                 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
    01671                                 fsm->opath, fsm->path);
    01672             }
    01673             fsm->path = _free(fsm->path);
    01674             fsm->path = path;
    01675             fsm->opath = _free(fsm->opath);
    01676             fsm->opath = opath;
    01677         }
    01678
    01679         /* Remove erased files. */
    01680         if (fsm->goal == FSM_PKGERASE) {
    01681             if (fsm->action == FA_ERASE) {
    01682                 TFI_t fi = fsmGetFi(fsm);
    01683                 if (S_ISDIR(st->st_mode)) {
    01684                     rc = fsmStage(fsm, FSM_RMDIR);
    01685                     if (!rc) break;
    01686                     switch (errno) {
    01687                     case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
    01688                     case ENOTEMPTY:
    01689         /* XXX make sure that build side permits %missingok on directories. */
    01690                         if (fsm->fflags & RPMFILE_MISSINGOK)
    01691                             break;
    01692
    01693                         /* XXX common error message. */
    01694                         rpmError(
    01695                             (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
    01696                             _("%s rmdir of %s failed: Directory not empty\n"),
    01697                                 fiTypeString(fi), fsm->path);
    01698                         break;
    01699                     default:
    01700                         rpmError(
    01701                             (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
    01702                                 _("%s rmdir of %s failed: %s\n"),
    01703                                 fiTypeString(fi), fsm->path, strerror(errno));
    01704                         break;
    01705                     }
    01706                 } else {
    01707                     rc = fsmStage(fsm, FSM_UNLINK);
    01708                     if (!rc) break;
    01709                     if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
    01710                         rpmError(
    01711                             (strict_erasures ? RPMERR_UNLINK : RPMDEBUG_UNLINK),
    01712                                 _("%s unlink of %s failed: %s\n"),
    01713                                 fiTypeString(fi), fsm->path, strerror(errno));
    01714                 }
    01715             }
    01716             /* XXX Failure to remove is not (yet) cause for failure. */
    01717             if (!strict_erasures) rc = 0;
    01718             break;
    01719         }
    01720
    01721         /* XXX Special case /dev/log, which shouldn't be packaged anyways */
    01722         if (!S_ISSOCK(st->st_mode)
    01723         &&  strncmp(fsm->path, "/dev/log", sizeof("/dev/log")-1))
    01724         {
    01725             /* Rename temporary to final file name. */
    01726             if (!S_ISDIR(st->st_mode) &&
    01727                 (fsm->subdir || fsm->suffix || fsm->nsuffix))
    01728             {
    01729                 fsm->opath = fsm->path;
    01730                 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
    01731                 rc = fsmStage(fsm, FSM_RENAME);
    01732                 if (!rc && fsm->nsuffix) {
    01733                     const char * opath = fsmFsPath(fsm, st, NULL, NULL);
    01734                     rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
    01735                                 (opath ? opath : ""), fsm->path);
    01736                     opath = _free(opath);
    01737                 }
    01738                 fsm->opath = _free(fsm->opath);
    01739             }
    01740             if (S_ISLNK(st->st_mode)) {
    01741                 if (!rc && !getuid())
    01742                     rc = fsmStage(fsm, FSM_LCHOWN);
    01743             } else {
    01744                 if (!rc && !getuid())
    01745                     rc = fsmStage(fsm, FSM_CHOWN);
    01746                 if (!rc)
    01747                     rc = fsmStage(fsm, FSM_CHMOD);
    01748                 if (!rc) {
    01749                     time_t mtime = st->st_mtime;
    01750                     TFI_t fi = fsmGetFi(fsm);
    01751                     if (fi->fmtimes)
    01752                         st->st_mtime = fi->fmtimes[fsm->ix];
    01753                     rc = fsmStage(fsm, FSM_UTIME);
    01754                     st->st_mtime = mtime;
    01755                 }
    01756             }
    01757         }
    01758
    01759         /* Notify on success. */
    01760         if (!rc)                rc = fsmStage(fsm, FSM_NOTIFY);
    01761         else if (fsm->failedFile && *fsm->failedFile == NULL) {
    01762             *fsm->failedFile = fsm->path;
    01763             fsm->path = NULL;
    01764         }
    01765         break;
    01766     case FSM_DESTROY:
    01767         fsm->path = _free(fsm->path);
    01768
    01769         /* Check for hard links missing from payload. */
    01770         while ((fsm->li = fsm->links) != NULL) {
    01771             fsm->links = fsm->li->next;
    01772             fsm->li->next = NULL;
    01773             if (fsm->goal == FSM_PKGINSTALL &&
    01774                         fsm->commit && fsm->li->linksLeft)
    01775             {
    01776                 for (i = 0 ; i < fsm->li->linksLeft; i++) {
    01777                     if (fsm->li->filex[i] < 0) continue;
    01778                     rc = CPIOERR_MISSING_HARDLINK;
    01779                     if (fsm->failedFile && *fsm->failedFile == NULL) {
    01780                         fsm->ix = fsm->li->filex[i];
    01781                         if (!fsmStage(fsm, FSM_MAP)) {
    01782                             *fsm->failedFile = fsm->path;
    01783                             fsm->path = NULL;
    01784                         }
    01785                     }
    01786                     /*@loopbreak@*/ break;
    01787                 }
    01788             }
    01789             if (fsm->goal == FSM_PKGBUILD &&
    01790                 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
    01791             {
    01792                 rc = CPIOERR_MISSING_HARDLINK;
    01793             }
    01794             fsm->li = freeHardLink(fsm->li);
    01795         }
    01796         fsm->ldn = _free(fsm->ldn);
    01797         fsm->ldnalloc = fsm->ldnlen = 0;
    01798         fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
    01799         fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
    01800         break;
    01801     case FSM_VERIFY:
    01802         if (fsm->diskchecked && !fsm->exists) {
    01803             rc = CPIOERR_LSTAT_FAILED;
    01804             break;
    01805         }
    01806         if (S_ISREG(st->st_mode)) {
    01807             char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
    01808             (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
    01809             /*
    01810              * XXX HP-UX (and other os'es) don't permit unlink on busy
    01811              * XXX files.
    01812              */
    01813             fsm->opath = fsm->path;
    01814             fsm->path = path;
    01815             rc = fsmStage(fsm, FSM_RENAME);
    01816             if (!rc)
    01817                     (void) fsmStage(fsm, FSM_UNLINK);
    01818             else
    01819                     rc = CPIOERR_UNLINK_FAILED;
    01820             fsm->path = fsm->opath;
    01821             fsm->opath = NULL;
    01822             return (rc ? rc : CPIOERR_LSTAT_FAILED);    /* XXX HACK */
    01823             /*@notreached@*/ break;
    01824         } else if (S_ISDIR(st->st_mode)) {
    01825             if (S_ISDIR(ost->st_mode))          return 0;
    01826             if (S_ISLNK(ost->st_mode)) {
    01827                 rc = fsmStage(fsm, FSM_STAT);
    01828                 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
    01829                 if (rc) break;
    01830                 errno = saveerrno;
    01831                 if (S_ISDIR(ost->st_mode))      return 0;
    01832             }
    01833         } else if (S_ISLNK(st->st_mode)) {
    01834             if (S_ISLNK(ost->st_mode)) {
    01835         /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
    01836                 rc = fsmStage(fsm, FSM_READLINK);
    01837                 errno = saveerrno;
    01838                 if (rc) break;
    01839                 if (!strcmp(fsm->opath, fsm->rdbuf))    return 0;
    01840             }
    01841         } else if (S_ISFIFO(st->st_mode)) {
    01842             if (S_ISFIFO(ost->st_mode))         return 0;
    01843         } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
    01844             if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
    01845                 (ost->st_rdev == st->st_rdev))  return 0;
    01846         } else if (S_ISSOCK(st->st_mode)) {
    01847             if (S_ISSOCK(ost->st_mode))         return 0;
    01848         }
    01849             /* XXX shouldn't do this with commit/undo. */
    01850         rc = 0;
    01851         if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
    01852         if (rc == 0)    rc = CPIOERR_LSTAT_FAILED;
    01853         return (rc ? rc : CPIOERR_LSTAT_FAILED);        /* XXX HACK */
    01854         /*@notreached@*/ break;
    01855
    01856     case FSM_UNLINK:
    01857         rc = Unlink(fsm->path);
    01858         if (_fsm_debug && (stage & FSM_SYSCALL))
    01859             rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
    01860                 fsm->path, (rc < 0 ? strerror(errno) : ""));
    01861         if (rc < 0)     rc = CPIOERR_UNLINK_FAILED;
    01862         break;
    01863     case FSM_RENAME:
    01864         rc = Rename(fsm->opath, fsm->path);
    01865         if (_fsm_debug && (stage & FSM_SYSCALL))
    01866             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
    01867                 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
    01868         if (rc < 0)     rc = CPIOERR_RENAME_FAILED;
    01869         break;
    01870     case FSM_MKDIR:
    01871         rc = Mkdir(fsm->path, (st->st_mode & 07777));
    01872         if (_fsm_debug && (stage & FSM_SYSCALL))
    01873             rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
    01874                 fsm->path, (unsigned)(st->st_mode & 07777),
    01875                 (rc < 0 ? strerror(errno) : ""));
    01876         if (rc < 0)     rc = CPIOERR_MKDIR_FAILED;
    01877         break;
    01878     case FSM_RMDIR:
    01879         rc = Rmdir(fsm->path);
    01880         if (_fsm_debug && (stage & FSM_SYSCALL))
    01881             rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
    01882                 fsm->path, (rc < 0 ? strerror(errno) : ""));
    01883         if (rc < 0)     rc = CPIOERR_RMDIR_FAILED;
    01884         break;
    01885     case FSM_CHOWN:
    01886         rc = chown(fsm->path, st->st_uid, st->st_gid);
    01887         if (_fsm_debug && (stage & FSM_SYSCALL))
    01888             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
    01889                 fsm->path, (int)st->st_uid, (int)st->st_gid,
    01890                 (rc < 0 ? strerror(errno) : ""));
    01891         if (rc < 0)     rc = CPIOERR_CHOWN_FAILED;
    01892         break;
    01893     case FSM_LCHOWN:
    01894 #if ! CHOWN_FOLLOWS_SYMLINK
    01895         rc = lchown(fsm->path, st->st_uid, st->st_gid);
    01896         if (_fsm_debug && (stage & FSM_SYSCALL))
    01897             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
    01898                 fsm->path, (int)st->st_uid, (int)st->st_gid,
    01899                 (rc < 0 ? strerror(errno) : ""));
    01900         if (rc < 0)     rc = CPIOERR_CHOWN_FAILED;
    01901 #endif
    01902         break;
    01903     case FSM_CHMOD:
    01904         rc = chmod(fsm->path, (st->st_mode & 07777));
    01905         if (_fsm_debug && (stage & FSM_SYSCALL))
    01906             rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
    01907                 fsm->path, (unsigned)(st->st_mode & 07777),
    01908                 (rc < 0 ? strerror(errno) : ""));
    01909         if (rc < 0)     rc = CPIOERR_CHMOD_FAILED;
    01910         break;
    01911     case FSM_UTIME:
    01912         {   struct utimbuf stamp;
    01913             stamp.actime = st->st_mtime;
    01914             stamp.modtime = st->st_mtime;
    01915             rc = utime(fsm->path, &stamp);
    01916             if (_fsm_debug && (stage & FSM_SYSCALL))
    01917                 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
    01918                         fsm->path, (unsigned)st->st_mtime,
    01919                         (rc < 0 ? strerror(errno) : ""));
    01920             if (rc < 0) rc = CPIOERR_UTIME_FAILED;
    01921         }
    01922         break;
    01923     case FSM_SYMLINK:
    01924         rc = symlink(fsm->opath, fsm->path);
    01925         if (_fsm_debug && (stage & FSM_SYSCALL))
    01926             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
    01927                 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
    01928         if (rc < 0)     rc = CPIOERR_SYMLINK_FAILED;
    01929         break;
    01930     case FSM_LINK:
    01931         rc = Link(fsm->opath, fsm->path);
    01932         if (_fsm_debug && (stage & FSM_SYSCALL))
    01933             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
    01934                 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
    01935         if (rc < 0)     rc = CPIOERR_LINK_FAILED;
    01936         break;
    01937     case FSM_MKFIFO:
    01938         rc = mkfifo(fsm->path, (st->st_mode & 07777));
    01939         if (_fsm_debug && (stage & FSM_SYSCALL))
    01940             rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
    01941                 fsm->path, (unsigned)(st->st_mode & 07777),
    01942                 (rc < 0 ? strerror(errno) : ""));
    01943         if (rc < 0)     rc = CPIOERR_MKFIFO_FAILED;
    01944         break;
    01945     case FSM_MKNOD:
    01946         /*@-unrecog -portability @*/ /* FIX: check S_IFIFO or dev != 0 */
    01947         rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
    01948         /*@=unrecog =portability @*/
    01949         if (_fsm_debug && (stage & FSM_SYSCALL))
    01950             rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
    01951                 fsm->path, (unsigned)(st->st_mode & ~07777),
    01952                 (unsigned)st->st_rdev,
    01953                 (rc < 0 ? strerror(errno) : ""));
    01954         if (rc < 0)     rc = CPIOERR_MKNOD_FAILED;
    01955         break;
    01956     case FSM_LSTAT:
    01957         rc = Lstat(fsm->path, ost);
    01958         if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
    01959             rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
    01960                 fsm->path, (rc < 0 ? strerror(errno) : ""));
    01961         if (rc < 0)     rc = CPIOERR_LSTAT_FAILED;
    01962         break;
    01963     case FSM_STAT:
    01964         rc = Stat(fsm->path, ost);
    01965         if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
    01966             rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
    01967                 fsm->path, (rc < 0 ? strerror(errno) : ""));
    01968         if (rc < 0)     rc = CPIOERR_STAT_FAILED;
    01969         break;
    01970     case FSM_READLINK:
    01971         /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
    01972         rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
    01973         if (_fsm_debug && (stage & FSM_SYSCALL))
    01974             rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
    01975                 fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
    01976         if (rc < 0)     rc = CPIOERR_READLINK_FAILED;
    01977         else {
    01978             fsm->rdnb = rc;
    01979             fsm->rdbuf[fsm->rdnb] = '\0';
    01980             rc = 0;
    01981         }
    01982         break;
    01983     case FSM_CHROOT:
    01984         break;
    01985
    01986     case FSM_NEXT:
    01987         rc = fsmStage(fsm, FSM_HREAD);
    01988         if (rc) break;
    01989         if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
    01990             fsm->path = _free(fsm->path);
    01991             rc = CPIOERR_HDR_TRAILER;
    01992         }
    01993         if (!rc)
    01994             rc = fsmStage(fsm, FSM_POS);
    01995         break;
    01996     case FSM_EAT:
    01997         for (left = st->st_size; left > 0; left -= fsm->rdnb) {
    01998             fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
    01999             rc = fsmStage(fsm, FSM_DREAD);
    02000             if (rc)
    02001                 /*@loopbreak@*/ break;
    02002         }
    02003         break;
    02004     case FSM_POS:
    02005         left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
    02006         if (left) {
    02007             fsm->wrlen = left;
    02008             (void) fsmStage(fsm, FSM_DREAD);
    02009         }
    02010         break;
    02011     case FSM_PAD:
    02012         left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
    02013         if (left) {
    02014             memset(fsm->rdbuf, 0, left);
    02015             /* XXX DWRITE uses rdnb for I/O length. */
    02016             fsm->rdnb = left;
    02017             (void) fsmStage(fsm, FSM_DWRITE);
    02018         }
    02019         break;
    02020     case FSM_TRAILER:
    02021         rc = cpioTrailerWrite(fsm);
    02022         break;
    02023     case FSM_HREAD:
    02024         rc = fsmStage(fsm, FSM_POS);
    02025         if (!rc)
    02026             rc = cpioHeaderRead(fsm, st);       /* Read next payload header. */
    02027         break;
    02028     case FSM_HWRITE:
    02029         rc = cpioHeaderWrite(fsm, st);          /* Write next payload header. */
    02030         break;
    02031     case FSM_DREAD:
    02032         fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
    02033         if (_fsm_debug && (stage & FSM_SYSCALL))
    02034             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
    02035                 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
    02036                 (int)fsm->wrlen, (int)fsm->rdnb);
    02037         if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
    02038             rc = CPIOERR_READ_FAILED;
    02039         if (fsm->rdnb > 0)
    02040             fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
    02041         break;
    02042     case FSM_DWRITE:
    02043         fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
    02044         if (_fsm_debug && (stage & FSM_SYSCALL))
    02045             rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
    02046                 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
    02047                 (int)fsm->rdnb, (int)fsm->wrnb);
    02048         if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
    02049             rc = CPIOERR_WRITE_FAILED;
    02050         if (fsm->wrnb > 0)
    02051             fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
    02052         break;
    02053
    02054     case FSM_ROPEN:
    02055         fsm->rfd = Fopen(fsm->path, "r.ufdio");
    02056         if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
    02057             if (fsm->rfd)       (void) fsmStage(fsm, FSM_RCLOSE);
    02058             fsm->rfd = NULL;
    02059             rc = CPIOERR_OPEN_FAILED;
    02060             break;
    02061         }
    02062         if (_fsm_debug && (stage & FSM_SYSCALL))
    02063             rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
    02064                 fsm->path, fsm->rfd, fsm->rdbuf);
    02065         break;
    02066     case FSM_READ:
    02067         fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
    02068         if (_fsm_debug && (stage & FSM_SYSCALL))
    02069             rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
    02070                 cur, (int)fsm->rdlen, (int)fsm->rdnb);
    02071         if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
    02072             rc = CPIOERR_READ_FAILED;
    02073         break;
    02074     case FSM_RCLOSE:
    02075         if (fsm->rfd) {
    02076             if (_fsm_debug && (stage & FSM_SYSCALL))
    02077                 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
    02078             (void) Fclose(fsm->rfd);
    02079             errno = saveerrno;
    02080         }
    02081         fsm->rfd = NULL;
    02082         break;
    02083     case FSM_WOPEN:
    02084         fsm->wfd = Fopen(fsm->path, "w.ufdio");
    02085         if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
    02086             if (fsm->wfd)       (void) fsmStage(fsm, FSM_WCLOSE);
    02087             fsm->wfd = NULL;
    02088             rc = CPIOERR_OPEN_FAILED;
    02089         }
    02090         if (_fsm_debug && (stage & FSM_SYSCALL))
    02091             rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
    02092                 fsm->path, fsm->wfd, fsm->wrbuf);
    02093         break;
    02094     case FSM_WRITE:
    02095         fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
    02096         if (_fsm_debug && (stage & FSM_SYSCALL))
    02097             rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
    02098                 cur, (int)fsm->rdnb, (int)fsm->wrnb);
    02099         if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
    02100             rc = CPIOERR_WRITE_FAILED;
    02101         break;
    02102     case FSM_WCLOSE:
    02103         if (fsm->wfd) {
    02104             if (_fsm_debug && (stage & FSM_SYSCALL))
    02105                 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
    02106             (void) Fclose(fsm->wfd);
    02107             errno = saveerrno;
    02108         }
    02109         fsm->wfd = NULL;
    02110         break;
    02111
    02112     default:
    02113         break;
    02114     }
    02115
    02116     if (!(stage & FSM_INTERNAL)) {
    02117         fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
    02118     }
    02119     return rc;
    02120 }
    02121 /*@=compmempass@*/
    02122
    02123 /*@obserever@*/ const char *const fileActionString(fileAction a)
    02124 {
    02125     switch (a) {
    02126     case FA_UNKNOWN:    return "unknown";
    02127     case FA_CREATE:     return "create";
    02128     case FA_COPYOUT:    return "copyout";
    02129     case FA_COPYIN:     return "copyin";
    02130     case FA_BACKUP:     return "backup";
    02131     case FA_SAVE:       return "save";
    02132     case FA_SKIP:       return "skip";
    02133     case FA_ALTNAME:    return "altname";
    02134     case FA_ERASE:      return "erase";
    02135     case FA_SKIPNSTATE: return "skipnstate";
    02136     case FA_SKIPNETSHARED: return "skipnetshared";
    02137     case FA_SKIPMULTILIB: return "skipmultilib";
    02138     default:            return "???";
    02139     }
    02140     /*@notreached@*/
    02141 }
    02142
    02143 /*@observer@*/ const char *const fileStageString(fileStage a) {
    02144     switch(a) {
    02145     case FSM_UNKNOWN:   return "unknown";
    02146
    02147     case FSM_PKGINSTALL:return "INSTALL";
    02148     case FSM_PKGERASE:  return "ERASE";
    02149     case FSM_PKGBUILD:  return "BUILD";
    02150     case FSM_PKGCOMMIT: return "COMMIT";
    02151     case FSM_PKGUNDO:   return "UNDO";
    02152
    02153     case FSM_CREATE:    return "create";
    02154     case FSM_INIT:      return "init";
    02155     case FSM_MAP:       return "map";
    02156     case FSM_MKDIRS:    return "mkdirs";
    02157     case FSM_RMDIRS:    return "rmdirs";
    02158     case FSM_PRE:       return "pre";
    02159     case FSM_PROCESS:   return "process";
    02160     case FSM_POST:      return "post";
    02161     case FSM_MKLINKS:   return "mklinks";
    02162     case FSM_NOTIFY:    return "notify";
    02163     case FSM_UNDO:      return "undo";
    02164     case FSM_FINI:      return "fini";
    02165     case FSM_COMMIT:    return "commit";
    02166     case FSM_DESTROY:   return "destroy";
    02167     case FSM_VERIFY:    return "verify";
    02168
    02169     case FSM_UNLINK:    return "Unlink";
    02170     case FSM_RENAME:    return "Rename";
    02171     case FSM_MKDIR:     return "Mkdir";
    02172     case FSM_RMDIR:     return "rmdir";
    02173     case FSM_CHOWN:     return "chown";
    02174     case FSM_LCHOWN:    return "lchown";
    02175     case FSM_CHMOD:     return "chmod";
    02176     case FSM_UTIME:     return "utime";
    02177     case FSM_SYMLINK:   return "symlink";
    02178     case FSM_LINK:      return "Link";
    02179     case FSM_MKFIFO:    return "mkfifo";
    02180     case FSM_MKNOD:     return "mknod";
    02181     case FSM_LSTAT:     return "Lstat";
    02182     case FSM_STAT:      return "Stat";
    02183     case FSM_READLINK:  return "Readlink";
    02184     case FSM_CHROOT:    return "chroot";
    02185
    02186     case FSM_NEXT:      return "next";
    02187     case FSM_EAT:       return "eat";
    02188     case FSM_POS:       return "pos";
    02189     case FSM_PAD:       return "pad";
    02190     case FSM_TRAILER:   return "trailer";
    02191     case FSM_HREAD:     return "hread";
    02192     case FSM_HWRITE:    return "hwrite";
    02193     case FSM_DREAD:     return "Fread";
    02194     case FSM_DWRITE:    return "Fwrite";
    02195
    02196     case FSM_ROPEN:     return "Fopen";
    02197     case FSM_READ:      return "Fread";
    02198     case FSM_RCLOSE:    return "Fclose";
    02199     case FSM_WOPEN:     return "Fopen";
    02200     case FSM_WRITE:     return "Fwrite";
    02201     case FSM_WCLOSE:    return "Fclose";
    02202
    02203     default:            return "???";
    02204     }
    02205     /*@noteached@*/
    02206 }