• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

rpmio/rpmio_internal.h

Go to the documentation of this file.
00001 #ifndef H_RPMIO_INTERNAL
00002 #define H_RPMIO_INTERNAL
00003 
00008 #include <rpmio.h>
00009 #include <rpmurl.h>
00010 
00011 #define _RPMPGP_INTERNAL
00012 #include <rpmpgp.h>
00013 
00014 #include <rpmxar.h>
00015 
00018 typedef struct _FDSTACK_s {
00019 /*@exposed@*/
00020     FDIO_t              io;
00021 /*@dependent@*/
00022     void *              fp;
00023     int                 fdno;
00024 } FDSTACK_t;
00025 
00029 typedef enum fdOpX_e {
00030     FDSTAT_READ         = 0,    
00031     FDSTAT_WRITE        = 1,    
00032     FDSTAT_SEEK         = 2,    
00033     FDSTAT_CLOSE        = 3,    
00034     FDSTAT_DIGEST       = 4,    
00035     FDSTAT_MAX          = 5
00036 } fdOpX;
00037 
00041 typedef /*@abstract@*/ struct {
00042     struct rpmop_s      ops[FDSTAT_MAX];        
00043 } * FDSTAT_t;
00044 
00047 typedef struct _FDDIGEST_s {
00048     pgpHashAlgo         hashalgo;
00049     DIGEST_CTX          hashctx;
00050 } * FDDIGEST_t;
00051 
00055 struct _FD_s {
00056 /*@refs@*/
00057     int         nrefs;
00058     int         flags;
00059 #define RPMIO_DEBUG_IO          0x40000000
00060 #define RPMIO_DEBUG_REFS        0x20000000
00061     int         magic;
00062 #define FDMAGIC                 0x04463138
00063     int         nfps;
00064     FDSTACK_t   fps[8];
00065     int         urlType;        /* ufdio: */
00066 
00067 /*@dependent@*/
00068     void *      url;            /* ufdio: URL info */
00069 /*@relnull@*/
00070     void *      req;            /* ufdio: HTTP request */
00071 
00072     int         rd_timeoutsecs; /* ufdRead: per FD_t timer */
00073     ssize_t     bytesRemain;    /* ufdio: */
00074     ssize_t     contentLength;  /* ufdio: */
00075     int         persist;        /* ufdio: */
00076     int         wr_chunked;     /* ufdio: */
00077 
00078     int         syserrno;       /* last system errno encountered */
00079 /*@observer@*/
00080     const void *errcookie;      /* gzdio/bzdio/ufdio: */
00081 
00082 /*null@*/
00083     const char *opath;          /* open(2) args. */
00084     int         oflags;
00085     mode_t      omode;
00086 
00087 /*@refcounted@*/ /*@relnull@*/
00088     rpmxar      xar;            /* xar archive wrapper */
00089 /*@refcounted@*/ /*@relnull@*/
00090     pgpDig      dig;            /* signature parameters */
00091 
00092     FDSTAT_t    stats;          /* I/O statistics */
00093 
00094     int         ndigests;
00095 #define FDDIGEST_MAX    4
00096     struct _FDDIGEST_s  digests[FDDIGEST_MAX];
00097 
00098     int         ftpFileDoneNeeded; /* ufdio: (FTP) */
00099     unsigned long long  fd_cpioPos;     /* cpio: */
00100 };
00101 /*@access FD_t@*/
00102 
00103 #define FDSANE(fd)      assert(fd && fd->magic == FDMAGIC)
00104 
00105 /*@-redecl@*/
00106 /*@unchecked@*/
00107 extern int _rpmio_debug;
00108 /*@=redecl@*/
00109 
00110 /*@-redecl@*/
00111 /*@unchecked@*/
00112 extern int _av_debug;
00113 /*@=redecl@*/
00114 
00115 /*@-redecl@*/
00116 /*@unchecked@*/
00117 extern int _ftp_debug;
00118 /*@=redecl@*/
00119 
00120 /*@-redecl@*/
00121 /*@unchecked@*/
00122 extern int _dav_debug;
00123 /*@=redecl@*/
00124 
00125 #define DBG(_f, _m, _x) \
00126     /*@-modfilesys@*/ \
00127     if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00128     /*@=modfilesys@*/
00129 
00130 #if defined(__LCLINT__XXX)
00131 #define DBGIO(_f, _x)
00132 #define DBGREFS(_f, _x)
00133 #else
00134 #define DBGIO(_f, _x)   DBG((_f), RPMIO_DEBUG_IO, _x)
00135 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00136 #endif
00137 
00138 #ifdef __cplusplus
00139 extern "C" {
00140 #endif
00141 
00144 int fdFgets(FD_t fd, char * buf, size_t len)
00145         /*@globals errno, fileSystem @*/
00146         /*@modifies *buf, fd, errno, fileSystem @*/;
00147 
00150 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
00151                 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
00152         /*@globals h_errno, fileSystem, internalState @*/
00153         /*@modifies *uret, fileSystem, internalState @*/;
00154 
00157 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00158         /*@globals fileSystem, internalState @*/
00159         /*@modifies data, fileSystem, internalState @*/;
00160 
00163 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00164         /*@globals h_errno, fileSystem, internalState @*/
00165         /*@modifies fileSystem, internalState @*/;
00166 
00169 int ufdClose( /*@only@*/ void * cookie)
00170         /*@globals fileSystem, internalState @*/
00171         /*@modifies cookie, fileSystem, internalState @*/;
00172 
00175 /*@unused@*/ static inline
00176 void fdSetOpen(FD_t fd, const char * path, int flags, mode_t mode)
00177         /*@modifies fd @*/
00178 {
00179     FDSANE(fd);
00180     if (fd->opath != NULL) {
00181         free((void *)fd->opath);
00182         fd->opath = NULL;
00183     }
00184     fd->opath = xstrdup(path);
00185     fd->oflags = flags;
00186     fd->omode = mode;
00187 }
00188 
00191 /*@unused@*/ static inline
00192 /*@null@*/ /*@observer@*/ const char * fdGetOPath(FD_t fd)
00193         /*@*/
00194 {
00195     FDSANE(fd);
00196     return fd->opath;
00197 }
00198 
00201 /*@unused@*/ static inline
00202 int fdGetOFlags(FD_t fd)
00203         /*@*/
00204 {
00205     FDSANE(fd);
00206     return fd->oflags;
00207 }
00208 
00211 /*@unused@*/ static inline
00212 mode_t fdGetOMode(FD_t fd)
00213         /*@*/
00214 {
00215     FDSANE(fd);
00216     return fd->omode;
00217 }
00218 
00221 /*@unused@*/ static inline
00222 void fdSetDig(FD_t fd, pgpDig dig)
00223         /*@modifies fd, dig @*/
00224 {
00225     FDSANE(fd);
00226     fd->dig = pgpDigFree(fd->dig);
00227     fd->dig = pgpDigLink(dig, "fdSetDig");
00228 }
00229 
00232 /*@unused@*/ static inline
00233 /*@null@*/ pgpDig fdGetDig(FD_t fd)
00234         /*@*/
00235 {
00236     FDSANE(fd);
00237     /*@-compdef -retexpose -refcounttrans -usereleased @*/
00238     return fd->dig;
00239     /*@=compdef =retexpose =refcounttrans =usereleased @*/
00240 }
00241 
00244 /*@unused@*/ static inline
00245 void fdSetXAR(FD_t fd, rpmxar xar)
00246         /*@modifies fd, xar @*/
00247 {
00248     FDSANE(fd);
00249     fd->xar = rpmxarLink(xar, "fdSetXAR");
00250 }
00251 
00254 /*@unused@*/ static inline
00255 /*@null@*/ rpmxar fdGetXAR(FD_t fd)
00256         /*@*/
00257 {
00258     FDSANE(fd);
00259     /*@-compdef -refcounttrans -retexpose -usereleased @*/
00260     return fd->xar;
00261     /*@=compdef =refcounttrans =retexpose =usereleased @*/
00262 }
00263 
00266 /*@unused@*/ static inline
00267 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
00268         /*@*/
00269 {
00270     FDSANE(fd);
00271     return fd->fps[fd->nfps].io;
00272 }
00273 
00276 /*@-nullstate@*/ /* FIX: io may be NULL */
00277 /*@unused@*/ static inline
00278 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
00279         /*@modifies fd @*/
00280 {
00281     FDSANE(fd);
00282     /*@-assignexpose@*/
00283     fd->fps[fd->nfps].io = io;
00284     /*@=assignexpose@*/
00285 }
00286 /*@=nullstate@*/
00287 
00290 /*@unused@*/ static inline
00291 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
00292         /*@*/
00293 {
00294     FDSANE(fd);
00295     /*@+voidabstract@*/
00296     return ((FILE *)fd->fps[fd->nfps].fp);
00297     /*@=voidabstract@*/
00298 }
00299 
00302 /*@unused@*/ static inline
00303 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
00304         /*@*/
00305 {
00306     FDSANE(fd);
00307     return fd->fps[fd->nfps].fp;
00308 }
00309 
00312 /*@-nullstate@*/ /* FIX: fp may be NULL */
00313 /*@unused@*/ static inline
00314 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
00315         /*@modifies fd @*/
00316 {
00317     FDSANE(fd);
00318     /*@-assignexpose@*/
00319     fd->fps[fd->nfps].fp = fp;
00320     /*@=assignexpose@*/
00321 }
00322 /*@=nullstate@*/
00323 
00326 /*@unused@*/ static inline
00327 int fdGetFdno(FD_t fd)
00328         /*@*/
00329 {
00330     FDSANE(fd);
00331     return fd->fps[fd->nfps].fdno;
00332 }
00333 
00336 /*@unused@*/ static inline
00337 void fdSetFdno(FD_t fd, int fdno)
00338         /*@modifies fd @*/
00339 {
00340     FDSANE(fd);
00341     fd->fps[fd->nfps].fdno = fdno;
00342 }
00343 
00346 /*@unused@*/ static inline
00347 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00348         /*@modifies fd @*/
00349 {
00350     FDSANE(fd);
00351     fd->contentLength = fd->bytesRemain = contentLength;
00352 }
00353 
00356 /*@unused@*/ static inline
00357 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00358         /*@modifies fd @*/
00359 {
00360     FDSANE(fd);
00361     if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00362         return;
00363     fd->nfps++;
00364     fdSetIo(fd, io);
00365     fdSetFp(fd, fp);
00366     fdSetFdno(fd, fdno);
00367 }
00368 
00371 /*@unused@*/ static inline
00372 void fdPop(FD_t fd)
00373         /*@modifies fd @*/
00374 {
00375     FDSANE(fd);
00376     if (fd->nfps < 0) return;
00377     fdSetIo(fd, NULL);
00378     fdSetFp(fd, NULL);
00379     fdSetFdno(fd, -1);
00380     fd->nfps--;
00381 }
00382 
00385 /*@unused@*/ static inline /*@null@*/
00386 rpmop fdstat_op(/*@null@*/ FD_t fd, fdOpX opx)
00387         /*@*/
00388 {
00389     rpmop op = NULL;
00390 
00391     if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00392         op = fd->stats->ops + opx;
00393     return op;
00394 }
00395 
00398 /*@unused@*/ static inline
00399 void fdstat_enter(/*@null@*/ FD_t fd, int opx)
00400         /*@globals internalState @*/
00401         /*@modifies internalState @*/
00402 {
00403     if (fd == NULL) return;
00404     if (fd->stats != NULL)
00405         (void) rpmswEnter(fdstat_op(fd, opx), 0);
00406 }
00407 
00410 /*@unused@*/ static inline
00411 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
00412         /*@globals internalState @*/
00413         /*@modifies fd, internalState @*/
00414 {
00415     if (fd == NULL) return;
00416     if (rc == -1)
00417         fd->syserrno = errno;
00418     else if (rc > 0 && fd->bytesRemain > 0)
00419         switch (opx) {
00420         case FDSTAT_READ:
00421         case FDSTAT_WRITE:
00422         fd->bytesRemain -= rc;
00423             break;
00424         default:
00425             break;
00426         }
00427     if (fd->stats != NULL)
00428         (void) rpmswExit(fdstat_op(fd, opx), rc);
00429 }
00430 
00433 /*@unused@*/ static inline
00434 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
00435         /*@globals fileSystem @*/
00436         /*@modifies *fp, fileSystem @*/
00437 {
00438     static int usec_scale = (1000*1000);
00439     int opx;
00440 
00441     if (fd == NULL || fd->stats == NULL) return;
00442     for (opx = 0; opx < 4; opx++) {
00443         rpmop op = &fd->stats->ops[opx];
00444         if (op->count <= 0) continue;
00445         switch (opx) {
00446         case FDSTAT_READ:
00447             if (msg) fprintf(fp, "%s:", msg);
00448             fprintf(fp, "%8d reads, %8lu total bytes in %d.%06d secs\n",
00449                 op->count, (unsigned long)op->bytes,
00450                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00451             /*@switchbreak@*/ break;
00452         case FDSTAT_WRITE:
00453             if (msg) fprintf(fp, "%s:", msg);
00454             fprintf(fp, "%8d writes, %8lu total bytes in %d.%06d secs\n",
00455                 op->count, (unsigned long)op->bytes,
00456                 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00457             /*@switchbreak@*/ break;
00458         case FDSTAT_SEEK:
00459             /*@switchbreak@*/ break;
00460         case FDSTAT_CLOSE:
00461             /*@switchbreak@*/ break;
00462         }
00463     }
00464 }
00465 
00468 /*@unused@*/ static inline
00469 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
00470         /*@modifies fd @*/
00471 {
00472     FDSANE(fd);
00473     fd->syserrno = syserrno;
00474     /*@-assignexpose@*/
00475     fd->errcookie = errcookie;
00476     /*@=assignexpose@*/
00477 }
00478 
00481 /*@unused@*/ static inline
00482 int fdGetRdTimeoutSecs(FD_t fd)
00483         /*@*/
00484 {
00485     FDSANE(fd);
00486     return fd->rd_timeoutsecs;
00487 }
00488 
00491 /*@unused@*/ static inline
00492 unsigned long long fdGetCpioPos(FD_t fd)
00493         /*@*/
00494 {
00495     FDSANE(fd);
00496     return fd->fd_cpioPos;
00497 }
00498 
00501 /*@unused@*/ static inline
00502 void fdSetCpioPos(FD_t fd, long int cpioPos)
00503         /*@modifies fd @*/
00504 {
00505     FDSANE(fd);
00506     fd->fd_cpioPos = cpioPos;
00507 }
00508 
00511 /*@mayexit@*/ /*@unused@*/ static inline
00512 FD_t c2f(/*@null@*/ void * cookie)
00513         /*@*/
00514 {
00515     /*@-castexpose@*/
00516     FD_t fd = (FD_t) cookie;
00517     /*@=castexpose@*/
00518     FDSANE(fd);
00519     /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
00520 }
00521 
00525 /*@unused@*/ static inline
00526 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00527         /*@globals internalState @*/
00528         /*@modifies fd, internalState @*/
00529 {
00530     FDDIGEST_t fddig = fd->digests + fd->ndigests;
00531     if (fddig != (fd->digests + FDDIGEST_MAX)) {
00532         fd->ndigests++;
00533         fddig->hashalgo = hashalgo;
00534         fdstat_enter(fd, FDSTAT_DIGEST);
00535         fddig->hashctx = rpmDigestInit(hashalgo, flags);
00536         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00537     }
00538 }
00539 
00543 /*@unused@*/ static inline
00544 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00545         /*@globals internalState @*/
00546         /*@modifies fd, internalState @*/
00547 {
00548     int i;
00549 
00550     if (buf != NULL && buflen > 0)
00551     for (i = fd->ndigests - 1; i >= 0; i--) {
00552         FDDIGEST_t fddig = fd->digests + i;
00553         if (fddig->hashctx == NULL)
00554             continue;
00555         fdstat_enter(fd, FDSTAT_DIGEST);
00556         (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00557         fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00558     }
00559 }
00560 
00563 /*@unused@*/ static inline
00564 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00565                 /*@null@*/ /*@out@*/ void * datap,
00566                 /*@null@*/ /*@out@*/ size_t * lenp,
00567                 int asAscii)
00568         /*@globals internalState @*/
00569         /*@modifies fd, *datap, *lenp, internalState @*/
00570 {
00571     int imax = -1;
00572     int i;
00573 
00574     for (i = fd->ndigests - 1; i >= 0; i--) {
00575         FDDIGEST_t fddig = fd->digests + i;
00576         if (fddig->hashctx == NULL)
00577             continue;
00578         if (i > imax) imax = i;
00579         if (fddig->hashalgo != hashalgo)
00580             continue;
00581         fdstat_enter(fd, FDSTAT_DIGEST);
00582         (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00583         fdstat_exit(fd, FDSTAT_DIGEST, 0);
00584         fddig->hashctx = NULL;
00585         break;
00586     }
00587     if (i < 0) {
00588         if (datap) *(void **)datap = NULL;
00589         if (lenp) *lenp = 0;
00590     }
00591 
00592     fd->ndigests = imax;
00593     if (i < imax)
00594         fd->ndigests++;         /* convert index to count */
00595 }
00596 
00599 /*@-mustmod@*/
00600 /*@unused@*/ static inline
00601 void fdStealDigest(FD_t fd, pgpDig dig)
00602         /*@modifies fd, dig @*/
00603 {
00604     int i;
00605 /*@-type@*/     /* FIX: getters for pgpDig internals */
00606     for (i = fd->ndigests - 1; i >= 0; i--) {
00607         FDDIGEST_t fddig = fd->digests + i;
00608         if (fddig->hashctx != NULL)
00609         switch (fddig->hashalgo) {
00610         case PGPHASHALGO_MD5:
00611 assert(dig->md5ctx == NULL);
00612 /*@-onlytrans@*/
00613             dig->md5ctx = fddig->hashctx;
00614 /*@=onlytrans@*/
00615             fddig->hashctx = NULL;
00616             /*@switchbreak@*/ break;
00617         case PGPHASHALGO_SHA1:
00618         case PGPHASHALGO_RIPEMD160:
00619 #if defined(HAVE_BEECRYPT_API_H)
00620         case PGPHASHALGO_SHA256:
00621         case PGPHASHALGO_SHA384:
00622         case PGPHASHALGO_SHA512:
00623 #endif
00624 assert(dig->sha1ctx == NULL);
00625 /*@-onlytrans@*/
00626             dig->sha1ctx = fddig->hashctx;
00627 /*@=onlytrans@*/
00628             fddig->hashctx = NULL;
00629             /*@switchbreak@*/ break;
00630         default:
00631             /*@switchbreak@*/ break;
00632         }
00633     }
00634 /*@=type@*/
00635 }
00636 /*@=mustmod@*/
00637 
00638 /*@-shadow@*/
00641 /*@unused@*/ static inline
00642 int fdFileno(/*@null@*/ void * cookie)
00643         /*@*/
00644 {
00645     FD_t fd;
00646     if (cookie == NULL) return -2;
00647     fd = c2f(cookie);
00648     return fd->fps[0].fdno;
00649 }
00650 /*@=shadow@*/
00651 
00659 int rpmioSlurp(const char * fn,
00660                 /*@out@*/ uint8_t ** bp, /*@out@*/ ssize_t * blenp)
00661         /*@globals h_errno, fileSystem, internalState @*/
00662         /*@modifies *bp, *blenp, fileSystem, internalState @*/;
00663 
00664 #ifdef __cplusplus
00665 }
00666 #endif
00667 
00668 #endif  /* H_RPMIO_INTERNAL */

Generated on Mon Nov 29 2010 05:18:47 for rpm by  doxygen 1.7.2