Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

lib/fsm.c

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

Generated on Thu Apr 18 17:34:40 2002 for rpm by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002