rpm 5.2.1

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

- File List
- Globals

rpmio/rpmxar.c
Go to the documentation of this file.

    00001 #include "system.h"
    00002
    00003 /* XXX Get rid of the pugly #ifdef's */
    00004 #if defined(WITH_XAR) && defined(HAVE_XAR_H)
    00005
    00006 #include "xar.h"
    00007
    00008 #if defined(__LCLINT__)
    00009 /*@-incondefs -redecl@*/
    00010 /*@null@*/
    00011 xar_t xar_open(const char *file, int32_t flags)
    00012         /*@*/;
    00013 int xar_close(/*@only@*/ xar_t x)
    00014         /*@globals fileSystem @*/
    00015         /*@modifies x, fileSystem @*/;
    00016 /*@null@*/
    00017 xar_iter_t xar_iter_new(void)
    00018         /*@*/;
    00019 /*@null@*/
    00020 xar_file_t xar_file_first(xar_t x, xar_iter_t i)
    00021         /*@modifies x, i @*/;
    00022 /*@null@*/
    00023 xar_file_t xar_file_next(xar_iter_t i)
    00024         /*@modifies i @*/;
    00025 /*@null@*/
    00026 xar_file_t xar_add_frombuffer(xar_t x, /*@null@*/ xar_file_t parent,
    00027                 const char *name, char *buffer, size_t length)
    00028         /*@globals fileSystem @*/
    00029         /*@modifies x, fileSystem @*/;
    00030 int32_t xar_extract_tobuffersz(xar_t x, xar_file_t f,
    00031                 char **buffer, size_t *size)
    00032         /*@globals fileSystem @*/
    00033         /*@modifies x, f, *buffer, *size @*/;
    00034 /*@only@*/
    00035 char *xar_get_path(xar_file_t f)
    00036         /*@*/;
    00037 /*@=incondefs =redecl@*/
    00038
    00039 #endif  /* __LCLINT__ */
    00040
    00041 #else   /* WITH_XAR */
    00042 #define READ    0
    00043 #define WRITE   1
    00044 #define xar_open(_fn, _f)       (NULL)
    00045 #define xar_close(_x)   (1)
    00046 #define xar_iter_new()          (NULL)
    00047 #define xar_iter_free(_i)
    00048 #define xar_file_first(_x, _i)  (NULL)
    00049 #define xar_file_next(_i)       (NULL)
    00050 #define xar_add_frombuffer(_x, _parent, _fn, _b, _bsize)        (NULL)
    00051 #define xar_extract_tobuffersz(_x, _f, _b, _bsize)      (1)
    00052 #define xar_get_path(_f)        "*No XAR*"
    00053 #define xar_opt_set(_a1, _a2, _a3) (1)
    00054 #define XAR_OPT_COMPRESSION 0
    00055 #define XAR_OPT_VAL_NONE 0
    00056 #define XAR_OPT_VAL_GZIP 0
    00057 #endif  /* WITH_XAR */
    00058
    00059 #define _RPMXAR_INTERNAL
    00060 #include <rpmxar.h>
    00061 #include <rpmio_internal.h>     /* for fdGetXAR */
    00062
    00063 #include "debug.h"
    00064
    00065 /*@access FD_t @*/
    00066
    00067 /*@unchecked@*/
    00068 int _xar_debug = 0;
    00069
    00070 /*@unchecked@*/ /*@only@*/ /*@null@*/
    00071 rpmioPool _xarPool;
    00072
    00073 /*@-globuse -mustmod@*/
    00074 static void rpmxarFini(void * _xar)
    00075         /*@globals fileSystem @*/
    00076         /*@modifies _xar, fileSystem @*/
    00077 {
    00078     rpmxar xar =_xar;
    00079     if (xar->i) {
    00080         xar_iter_free(xar->i);
    00081         xar->i = NULL;
    00082     }
    00083     if (xar->x) {
    00084         int xx;
    00085         xx = xar_close(xar->x);
    00086         xar->x = NULL;
    00087     }
    00088
    00089     xar->member = _free(xar->member);
    00090     xar->b = _free(xar->b);
    00091 }
    00092 /*@=globuse =mustmod@*/
    00093
    00094 static rpmxar rpmxarGetPool(/*@null@*/ rpmioPool pool)
    00095         /*@globals _xarPool, fileSystem @*/
    00096         /*@modifies pool, _xarPool, fileSystem @*/
    00097 {
    00098     rpmxar xar;
    00099
    00100     if (_xarPool == NULL) {
    00101         _xarPool = rpmioNewPool("xar", sizeof(*xar), -1, _xar_debug,
    00102                         NULL, NULL, rpmxarFini);
    00103         pool = _xarPool;
    00104     }
    00105     return (rpmxar) rpmioGetPool(pool, sizeof(*xar));
    00106 }
    00107
    00108 rpmxar rpmxarNew(const char * fn, const char * fmode)
    00109 {
    00110     rpmxar xar = rpmxarGetPool(_xarPool);
    00111     int flags = ((fmode && *fmode == 'w') ? WRITE : READ);
    00112
    00113 assert(fn != NULL);
    00114     xar->x = xar_open(fn, flags);
    00115     if (flags == READ) {
    00116         xar->i = xar_iter_new();
    00117         xar->first = 1;
    00118     }
    00119     return rpmxarLink(xar, "rpmxarNew");
    00120 }
    00121
    00122 int rpmxarNext(rpmxar xar)
    00123 {
    00124 if (_xar_debug)
    00125 fprintf(stderr, "--> rpmxarNext(%p) first %d\n", xar, xar->first);
    00126
    00127     if (xar->first) {
    00128         xar->f = xar_file_first(xar->x, xar->i);
    00129         xar->first = 0;
    00130     } else
    00131         xar->f = xar_file_next(xar->i);
    00132
    00133     return (xar->f == NULL ? 1 : 0);
    00134 }
    00135
    00136 int rpmxarPush(rpmxar xar, const char * fn, unsigned char * b, size_t bsize)
    00137 {
    00138     int payload = !strcmp(fn, "Payload");
    00139
    00140 /*@+charint@*/
    00141 if (_xar_debug)
    00142 fprintf(stderr, "--> rpmxarPush(%p, %s) %p[%u] %02x%02x%02x%02x%02x%02x%02x%02x\n", xar, fn, b, (unsigned)bsize, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
    00143 /*@=charint@*/
    00144
    00145     if (xar->x && b != NULL) {
    00146         if (payload) /* payload is already compressed */
    00147             (void) xar_opt_set(xar->x, XAR_OPT_COMPRESSION, XAR_OPT_VAL_NONE);
    00148         xar->f = xar_add_frombuffer(xar->x, NULL, fn, (char *)b, bsize);
    00149         if (payload) /* restore default xar compression */
    00150             (void) xar_opt_set(xar->x, XAR_OPT_COMPRESSION, XAR_OPT_VAL_GZIP);
    00151         if (xar->f == NULL)
    00152             return 2;
    00153     }
    00154     return 0;
    00155 }
    00156
    00157 int rpmxarPull(rpmxar xar, const char * fn)
    00158 {
    00159     const char * path = xar_get_path(xar->f);
    00160     int rc = 1;
    00161
    00162     if (fn != NULL && strcmp(fn, path)) {
    00163         path = _free(path);
    00164         return rc;
    00165     }
    00166     xar->member = _free(xar->member);
    00167     xar->member = path;
    00168
    00169     xar->b = _free(xar->b);
    00170     xar->bsize = xar->bx = 0;
    00171
    00172 /*@-nullstate @*/
    00173     rc = (int) xar_extract_tobuffersz(xar->x, xar->f, (char **)&xar->b, &xar->bsize);
    00174 /*@=nullstate @*/
    00175     if (rc)
    00176         return 1;
    00177
    00178 /*@+charint -nullpass -nullderef @*/
    00179 if (_xar_debug) {
    00180 unsigned char * b = xar->b;
    00181 size_t bsize = xar->bsize;
    00182 fprintf(stderr, "--> rpmxarPull(%p, %s) %p[%u] %02x%02x%02x%02x%02x%02x%02x%02x\n", xar, fn, b, (unsigned)bsize, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
    00183 }
    00184 /*@=charint =nullpass =nullderef @*/
    00185
    00186     return 0;
    00187 }
    00188
    00189 int rpmxarSwapBuf(rpmxar xar, unsigned char * b, size_t bsize,
    00190                 unsigned char ** obp, size_t * obsizep)
    00191 {
    00192 if (_xar_debug)
    00193 fprintf(stderr, "--> rpmxarSwapBuf(%p, %p[%u], %p, %p) %p[%u]\n", xar, b, (unsigned) bsize, obp, obsizep, xar->b, (unsigned) xar->bsize);
    00194
    00195     if (xar) {
    00196         if (obsizep != NULL)
    00197             *obsizep = xar->bsize;
    00198         if (obp != NULL) {
    00199 /*@-onlytrans@*/
    00200             *obp = xar->b;
    00201 /*@=onlytrans@*/
    00202             xar->b = NULL;
    00203         }
    00204         xar->b = _free(xar->b);
    00205 /*@-assignexpose -temptrans @*/
    00206         xar->b = b;
    00207 /*@=assignexpose =temptrans @*/
    00208         xar->bsize = bsize;
    00209     }
    00210 /*@-nullstate@*/
    00211     return 0;
    00212 /*@=nullstate@*/
    00213 }
    00214
    00215 ssize_t xarRead(void * cookie, /*@out@*/ char * buf, size_t count)
    00216 {
    00217     FD_t fd = cookie;
    00218     rpmxar xar = fdGetXAR(fd);
    00219     ssize_t rc = 0;
    00220
    00221 assert(xar != NULL);
    00222 #if 0
    00223     if ((xx = rpmxarNext(xar)) != 0)    return RPMRC_FAIL;
    00224     if ((xx = rpmxarPull(xar, "Signature")) != 0) return RPMRC_FAIL;
    00225     (void) rpmxarSwapBuf(xar, NULL, 0, &b, &nb);
    00226 #endif
    00227
    00228     rc = xar->bsize - xar->bx;
    00229     if (rc > 0) {
    00230         if (count < (size_t)rc) rc = count;
    00231 assert(xar->b != NULL);
    00232         memmove(buf, &xar->b[xar->bx], rc);
    00233         xar->bx += rc;
    00234     } else
    00235     if (rc < 0) {
    00236         rc = -1;
    00237     } else
    00238         rc = 0;
    00239
    00240 if (_xar_debug)
    00241 fprintf(stderr, "--> xarRead(%p,%p,0x%x) %s %p[%u:%u] rc 0x%x\n", cookie, buf, (unsigned)count, (xar->member ? xar->member : "(nil)"), xar->b, (unsigned)xar->bx, (unsigned)xar->bsize, (unsigned)rc);
    00242
    00243     return rc;
    00244 }