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

rpmio/rpmio_internal.h

Go to the documentation of this file.
00001 #ifndef H_RPMIO_INTERNAL
00002 #define H_RPMIO_INTERNAL
00003 
00009 #include <rpmio.h>
00010 #include <rpmurl.h>
00011 #include <rpmpgp.h>
00012 
00015 typedef struct _FDSTACK_s {
00016     FDIO_t              io;
00017 /*@dependent@*/ void *  fp;
00018     int                 fdno;
00019 } FDSTACK_t;
00020 
00024 typedef struct {
00025     int                 count;  
00026     off_t               bytes;  
00027     time_t              msecs;  
00028 } OPSTAT_t;
00029 
00033 enum FDSTAT_e {
00034     FDSTAT_READ         = 0,    
00035     FDSTAT_WRITE        = 1,    
00036     FDSTAT_SEEK         = 2,    
00037     FDSTAT_CLOSE        = 3     
00038 };
00039 
00043 typedef /*@abstract@*/ struct {
00044     struct timeval      create; 
00045     struct timeval      begin;  
00046     OPSTAT_t            ops[4]; 
00047 } * FDSTAT_t;
00048 
00052 typedef enum rpmDigestFlags_e {
00053     RPMDIGEST_NONE      = 0
00054 } rpmDigestFlags;
00055 
00058 typedef struct _FDDIGEST_s {
00059     pgpHashAlgo         hashalgo;
00060     DIGEST_CTX          hashctx;
00061 } * FDDIGEST_t;
00062 
00068 /*@only@*/
00069 DIGEST_CTX rpmDigestDup(DIGEST_CTX octx)
00070         /*@*/;
00071 
00079 /*@only@*/
00080 DIGEST_CTX rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags)
00081         /*@*/;
00082 
00090 int rpmDigestUpdate(DIGEST_CTX ctx, const void * data, size_t len)
00091         /*@modifies ctx @*/;
00092 
00104 int rpmDigestFinal(/*@only@*/ DIGEST_CTX ctx,
00105         /*@null@*/ /*@out@*/ void ** datap,
00106         /*@null@*/ /*@out@*/ size_t * lenp, int asAscii)
00107                 /*@modifies *datap, *lenp @*/;
00108 
00112 struct _FD_s {
00113 /*@refs@*/ int  nrefs;
00114     int         flags;
00115 #define RPMIO_DEBUG_IO          0x40000000
00116 #define RPMIO_DEBUG_REFS        0x20000000
00117     int         magic;
00118 #define FDMAGIC                 0x04463138
00119     int         nfps;
00120     FDSTACK_t   fps[8];
00121     int         urlType;        /* ufdio: */
00122 
00123 /*@dependent@*/ void *  url;    /* ufdio: URL info */
00124     int         rd_timeoutsecs; /* ufdRead: per FD_t timer */
00125     ssize_t     bytesRemain;    /* ufdio: */
00126     ssize_t     contentLength;  /* ufdio: */
00127     int         persist;        /* ufdio: */
00128     int         wr_chunked;     /* ufdio: */
00129 
00130     int         syserrno;       /* last system errno encountered */
00131 /*@observer@*/ const void *errcookie;   /* gzdio/bzdio/ufdio: */
00132 
00133     FDSTAT_t    stats;          /* I/O statistics */
00134 
00135     int         ndigests;
00136 #define FDDIGEST_MAX    4
00137     struct _FDDIGEST_s  digests[FDDIGEST_MAX];
00138 
00139     int         ftpFileDoneNeeded; /* ufdio: (FTP) */
00140     unsigned int firstFree;     /* fadio: */
00141     long int    fileSize;       /* fadio: */
00142     long int    fd_cpioPos;     /* cpio: */
00143 };
00144 /*@access FD_t@*/
00145 
00146 #define FDSANE(fd)      assert(fd && fd->magic == FDMAGIC)
00147 
00148 /*@-redecl@*/
00149 /*@unchecked@*/
00150 extern int _rpmio_debug;
00151 /*@=redecl@*/
00152 
00153 /*@-redecl@*/
00154 extern int _ftp_debug;
00155 /*@=redecl@*/
00156 
00157 #define DBG(_f, _m, _x) \
00158     if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x
00159 
00160 #if defined(__LCLINT__XXX)
00161 #define DBGIO(_f, _x)
00162 #define DBGREFS(_f, _x)
00163 #else
00164 #define DBGIO(_f, _x)   DBG((_f), RPMIO_DEBUG_IO, _x)
00165 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00166 #endif
00167 
00168 #ifdef __cplusplus
00169 extern "C" {
00170 #endif
00171 
00174 int fdFgets(FD_t fd, char * buf, size_t len)
00175         /*@globals errno, fileSystem @*/
00176         /*@modifies *buf, fd, errno, fileSystem @*/;
00177 
00180 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
00181                 /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
00182         /*@globals fileSystem @*/
00183         /*@modifies *uret, fileSystem @*/;
00184 
00187 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00188         /*@globals fileSystem @*/
00189         /*@modifies data, fileSystem @*/;
00190 
00193 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00194         /*@globals fileSystem @*/
00195         /*@modifies fileSystem @*/;
00196 
00199 int ufdClose( /*@only@*/ void * cookie)
00200         /*@globals fileSystem @*/
00201         /*@modifies cookie, fileSystem @*/;
00202 
00205 /*@unused@*/ static inline
00206 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
00207         /*@*/
00208 {
00209     FDSANE(fd);
00210     return fd->fps[fd->nfps].io;
00211 }
00212 
00215 /*@-nullstate@*/ /* FIX: io may be NULL */
00216 /*@unused@*/ static inline
00217 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
00218         /*@modifies fd @*/
00219 {
00220     FDSANE(fd);
00221     /*@-assignexpose@*/
00222     fd->fps[fd->nfps].io = io;
00223     /*@=assignexpose@*/
00224 }
00225 /*@=nullstate@*/
00226 
00229 /*@unused@*/ static inline
00230 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
00231         /*@*/
00232 {
00233     FDSANE(fd);
00234     /*@+voidabstract@*/
00235     return ((FILE *)fd->fps[fd->nfps].fp);
00236     /*@=voidabstract@*/
00237 }
00238 
00241 /*@unused@*/ static inline
00242 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
00243         /*@*/
00244 {
00245     FDSANE(fd);
00246     return fd->fps[fd->nfps].fp;
00247 }
00248 
00251 /*@-nullstate@*/ /* FIX: fp may be NULL */
00252 /*@unused@*/ static inline
00253 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
00254         /*@modifies fd @*/
00255 {
00256     FDSANE(fd);
00257     /*@-assignexpose@*/
00258     fd->fps[fd->nfps].fp = fp;
00259     /*@=assignexpose@*/
00260 }
00261 /*@=nullstate@*/
00262 
00265 /*@unused@*/ static inline
00266 int fdGetFdno(FD_t fd)
00267         /*@*/
00268 {
00269     FDSANE(fd);
00270     return fd->fps[fd->nfps].fdno;
00271 }
00272 
00275 /*@unused@*/ static inline
00276 void fdSetFdno(FD_t fd, int fdno)
00277         /*@modifies fd @*/
00278 {
00279     FDSANE(fd);
00280     fd->fps[fd->nfps].fdno = fdno;
00281 }
00282 
00285 /*@unused@*/ static inline
00286 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00287         /*@modifies fd @*/
00288 {
00289     FDSANE(fd);
00290     fd->contentLength = fd->bytesRemain = contentLength;
00291 }
00292 
00295 /*@unused@*/ static inline
00296 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00297         /*@modifies fd @*/
00298 {
00299     FDSANE(fd);
00300     if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00301         return;
00302     fd->nfps++;
00303     fdSetIo(fd, io);
00304     fdSetFp(fd, fp);
00305     fdSetFdno(fd, fdno);
00306 }
00307 
00310 /*@unused@*/ static inline void fdPop(FD_t fd)
00311         /*@modifies fd @*/
00312 {
00313     FDSANE(fd);
00314     if (fd->nfps < 0) return;
00315     fdSetIo(fd, NULL);
00316     fdSetFp(fd, NULL);
00317     fdSetFdno(fd, -1);
00318     fd->nfps--;
00319 }
00320 
00323 /*@unused@*/ static inline void fdstat_enter(/*@null@*/ FD_t fd, int opx)
00324         /*@modifies fd @*/
00325 {
00326     if (fd == NULL || fd->stats == NULL) return;
00327     fd->stats->ops[opx].count++;
00328     (void) gettimeofday(&fd->stats->begin, NULL);
00329 }
00330 
00333 /*@unused@*/ static inline
00334 time_t tvsub(/*@null@*/ const struct timeval * etv,
00335                 /*@null@*/ const struct timeval * btv)
00336         /*@*/
00337 {
00338     time_t secs, usecs;
00339     if (etv == NULL  || btv == NULL) return 0;
00340     secs = etv->tv_sec - btv->tv_sec;
00341     for (usecs = etv->tv_usec - btv->tv_usec; usecs < 0; usecs += 1000000)
00342         secs++;
00343     return ((secs * 1000) + (usecs/1000));
00344 }
00345 
00348 /*@unused@*/ static inline
00349 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
00350         /*@modifies fd @*/
00351 {
00352     struct timeval end;
00353     if (fd == NULL) return;
00354     if (rc == -1) fd->syserrno = errno;
00355     if (fd->stats == NULL) return;
00356     (void) gettimeofday(&end, NULL);
00357     if (rc >= 0) {
00358         switch(opx) {
00359         case FDSTAT_SEEK:
00360             fd->stats->ops[opx].bytes = rc;
00361             break;
00362         default:
00363             fd->stats->ops[opx].bytes += rc;
00364             if (fd->bytesRemain > 0) fd->bytesRemain -= rc;
00365             break;
00366         }
00367     }
00368     fd->stats->ops[opx].msecs += tvsub(&end, &fd->stats->begin);
00369     fd->stats->begin = end;     /* structure assignment */
00370 }
00371 
00374 /*@unused@*/ static inline
00375 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
00376         /*@globals fileSystem @*/
00377         /*@modifies *fp, fileSystem @*/
00378 {
00379     int opx;
00380     if (fd == NULL || fd->stats == NULL) return;
00381     for (opx = 0; opx < 4; opx++) {
00382         OPSTAT_t *ops = &fd->stats->ops[opx];
00383         if (ops->count <= 0) continue;
00384         switch (opx) {
00385         case FDSTAT_READ:
00386             if (msg) fprintf(fp, "%s:", msg);
00387             fprintf(fp, "%8d reads, %8ld total bytes in %d.%03d secs\n",
00388                 ops->count, (long)ops->bytes,
00389                 (int)(ops->msecs/1000), (int)(ops->msecs%1000));
00390             /*@switchbreak@*/ break;
00391         case FDSTAT_WRITE:
00392             if (msg) fprintf(fp, "%s:", msg);
00393             fprintf(fp, "%8d writes, %8ld total bytes in %d.%03d secs\n",
00394                 ops->count, (long)ops->bytes,
00395                 (int)(ops->msecs/1000), (int)(ops->msecs%1000));
00396             /*@switchbreak@*/ break;
00397         case FDSTAT_SEEK:
00398             /*@switchbreak@*/ break;
00399         case FDSTAT_CLOSE:
00400             /*@switchbreak@*/ break;
00401         }
00402     }
00403 }
00404 
00407 /*@unused@*/ static inline
00408 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
00409         /*@modifies fd @*/
00410 {
00411     FDSANE(fd);
00412     fd->syserrno = syserrno;
00413     /*@-assignexpose@*/
00414     fd->errcookie = errcookie;
00415     /*@=assignexpose@*/
00416 }
00417 
00420 /*@unused@*/ static inline
00421 int fdGetRdTimeoutSecs(FD_t fd)
00422         /*@*/
00423 {
00424     FDSANE(fd);
00425     return fd->rd_timeoutsecs;
00426 }
00427 
00430 /*@unused@*/ static inline
00431 long int fdGetCpioPos(FD_t fd)
00432         /*@*/
00433 {
00434     FDSANE(fd);
00435     return fd->fd_cpioPos;
00436 }
00437 
00440 /*@unused@*/ static inline
00441 void fdSetCpioPos(FD_t fd, long int cpioPos)
00442         /*@modifies fd @*/
00443 {
00444     FDSANE(fd);
00445     fd->fd_cpioPos = cpioPos;
00446 }
00447 
00450 /*@mayexit@*/ /*@unused@*/ static inline
00451 FD_t c2f(/*@null@*/ void * cookie)
00452         /*@*/
00453 {
00454     /*@-castexpose@*/
00455     FD_t fd = (FD_t) cookie;
00456     /*@=castexpose@*/
00457     FDSANE(fd);
00458     /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
00459 }
00460 
00464 /*@unused@*/ static inline
00465 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00466         /*@modifies fd @*/
00467 {
00468     FDDIGEST_t fddig = fd->digests + fd->ndigests;
00469     if (fddig != (fd->digests + FDDIGEST_MAX)) {
00470         fd->ndigests++;
00471         fddig->hashalgo = hashalgo;
00472         fddig->hashctx = rpmDigestInit(hashalgo, flags);
00473     }
00474 }
00475 
00479 /*@unused@*/ static inline
00480 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00481         /*@modifies fd @*/
00482 {
00483     int i;
00484 
00485     if (buf != NULL && buflen > 0)
00486     for (i = fd->ndigests - 1; i >= 0; i--) {
00487         FDDIGEST_t fddig = fd->digests + i;
00488         if (fddig->hashctx == NULL)
00489             continue;
00490         (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00491     }
00492 }
00493 
00496 /*@unused@*/ static inline
00497 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00498                 /*@null@*/ /*@out@*/ void ** datap,
00499                 /*@null@*/ /*@out@*/ size_t * lenp,
00500                 int asAscii)
00501         /*@modifies fd, *datap, *lenp @*/
00502 {
00503     int imax = -1;
00504     int i;
00505 
00506     for (i = fd->ndigests - 1; i >= 0; i--) {
00507         FDDIGEST_t fddig = fd->digests + i;
00508         if (fddig->hashctx == NULL)
00509             continue;
00510         if (i > imax) imax = i;
00511         if (fddig->hashalgo != hashalgo)
00512             continue;
00513         (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00514         fddig->hashctx = NULL;
00515         break;
00516     }
00517     if (i < 0) {
00518         if (datap) *datap = NULL;
00519         if (lenp) *lenp = 0;
00520     }
00521 
00522     fd->ndigests = imax;
00523     if (i < imax)
00524         fd->ndigests++;         /* convert index to count */
00525 }
00526 
00527 /*@-shadow@*/
00530 /*@unused@*/ static inline
00531 int fdFileno(/*@null@*/ void * cookie)
00532         /*@*/
00533 {
00534     FD_t fd;
00535     if (cookie == NULL) return -2;
00536     fd = c2f(cookie);
00537     return fd->fps[0].fdno;
00538 }
00539 /*@=shadow@*/
00540 
00543 int rpmioSlurp(const char * fn,
00544                 /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
00545         /*@globals fileSystem @*/
00546         /*@modifies *bp, *blenp, fileSystem @*/;
00547 
00548 #ifdef __cplusplus
00549 }
00550 #endif
00551 
00552 #endif  /* H_RPMIO_INTERNAL */

Generated on Sun Feb 2 23:32:05 2003 for rpm by doxygen1.2.18