lib/fsm.c

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

Generated on Fri Oct 12 08:44:53 2007 for rpm by  doxygen 1.5.2