rpm 5.2.1

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

- File List
- Globals

rpmxar.c
Go to the documentation of this file.
1 #include "system.h"
2
3 /* XXX Get rid of the pugly #ifdef's */
4 #if defined(WITH_XAR) && defined(HAVE_XAR_H)
5
6 #include "xar.h"
7
8 #if defined(__LCLINT__)
9 /*@-incondefs -redecl@*/
10 /*@null@*/
11 xar_t xar_open(const char *file, int32_t flags)
12 /*@*/;
13 int xar_close(/*@only@*/ xar_t x)
14 /*@globals fileSystem @*/
15 /*@modifies x, fileSystem @*/;
16 /*@null@*/
17 xar_iter_t xar_iter_new(void)
18 /*@*/;
19 /*@null@*/
20 xar_file_t xar_file_first(xar_t x, xar_iter_t i)
21 /*@modifies x, i @*/;
22 /*@null@*/
23 xar_file_t xar_file_next(xar_iter_t i)
24 /*@modifies i @*/;
25 /*@null@*/
26 xar_file_t xar_add_frombuffer(xar_t x, /*@null@*/ xar_file_t parent,
27 const char *name, char *buffer, size_t length)
28 /*@globals fileSystem @*/
29 /*@modifies x, fileSystem @*/;
30 int32_t xar_extract_tobuffersz(xar_t x, xar_file_t f,
31 char **buffer, size_t *size)
32 /*@globals fileSystem @*/
33 /*@modifies x, f, *buffer, *size @*/;
34 /*@only@*/
35 char *xar_get_path(xar_file_t f)
36 /*@*/;
37 /*@=incondefs =redecl@*/
38
39 #endif /* __LCLINT__ */
40
41 #else /* WITH_XAR */
42 #define READ 0
43 #define WRITE 1
44 #define xar_open(_fn, _f) (NULL)
45 #define xar_close(_x) (1)
46 #define xar_iter_new() (NULL)
47 #define xar_iter_free(_i)
48 #define xar_file_first(_x, _i) (NULL)
49 #define xar_file_next(_i) (NULL)
50 #define xar_add_frombuffer(_x, _parent, _fn, _b, _bsize) (NULL)
51 #define xar_extract_tobuffersz(_x, _f, _b, _bsize) (1)
52 #define xar_get_path(_f) "*No XAR*"
53 #define xar_opt_set(_a1, _a2, _a3) (1)
54 #define XAR_OPT_COMPRESSION 0
55 #define XAR_OPT_VAL_NONE 0
56 #define XAR_OPT_VAL_GZIP 0
57 #endif /* WITH_XAR */
58
59 #define _RPMXAR_INTERNAL
60 #include <rpmxar.h>
61 #include <rpmio_internal.h> /* for fdGetXAR */
62
63 #include "debug.h"
64
65 /*@access FD_t @*/
66
67 /*@unchecked@*/
68 int _xar_debug = 0;
69
70 /*@unchecked@*/ /*@only@*/ /*@null@*/
71 rpmioPool _xarPool;
72
73 /*@-globuse -mustmod@*/
74 static void rpmxarFini(void * _xar)
75 /*@globals fileSystem @*/
76 /*@modifies _xar, fileSystem @*/
77 {
78 rpmxar xar =_xar;
79 if (xar->i) {
80 xar_iter_free(xar->i);
81 xar->i = NULL;
82 }
83 if (xar->x) {
84 int xx;
85 xx = xar_close(xar->x);
86 xar->x = NULL;
87 }
88
89 xar->member = _free(xar->member);
90 xar->b = _free(xar->b);
91 }
92 /*@=globuse =mustmod@*/
93
94 static rpmxar rpmxarGetPool(/*@null@*/ rpmioPool pool)
95 /*@globals _xarPool, fileSystem @*/
96 /*@modifies pool, _xarPool, fileSystem @*/
97 {
98 rpmxar xar;
99
100 if (_xarPool == NULL) {
101 _xarPool = rpmioNewPool("xar", sizeof(*xar), -1, _xar_debug,
102 NULL, NULL, rpmxarFini);
103 pool = _xarPool;
104 }
105 return (rpmxar) rpmioGetPool(pool, sizeof(*xar));
106 }
107
108 rpmxar rpmxarNew(const char * fn, const char * fmode)
109 {
110 rpmxar xar = rpmxarGetPool(_xarPool);
111 int flags = ((fmode && *fmode == 'w') ? WRITE : READ);
112
113 assert(fn != NULL);
114 xar->x = xar_open(fn, flags);
115 if (flags == READ) {
116 xar->i = xar_iter_new();
117 xar->first = 1;
118 }
119 return rpmxarLink(xar, "rpmxarNew");
120 }
121
122 int rpmxarNext(rpmxar xar)
123 {
124 if (_xar_debug)
125 fprintf(stderr, "--> rpmxarNext(%p) first %d\n", xar, xar->first);
126
127 if (xar->first) {
128 xar->f = xar_file_first(xar->x, xar->i);
129 xar->first = 0;
130 } else
131 xar->f = xar_file_next(xar->i);
132
133 return (xar->f == NULL ? 1 : 0);
134 }
135
136 int rpmxarPush(rpmxar xar, const char * fn, unsigned char * b, size_t bsize)
137 {
138 int payload = !strcmp(fn, "Payload");
139
140 /*@+charint@*/
141 if (_xar_debug)
142 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]);
143 /*@=charint@*/
144
145 if (xar->x && b != NULL) {
146 if (payload) /* payload is already compressed */
147 (void) xar_opt_set(xar->x, XAR_OPT_COMPRESSION, XAR_OPT_VAL_NONE);
148 xar->f = xar_add_frombuffer(xar->x, NULL, fn, (char *)b, bsize);
149 if (payload) /* restore default xar compression */
150 (void) xar_opt_set(xar->x, XAR_OPT_COMPRESSION, XAR_OPT_VAL_GZIP);
151 if (xar->f == NULL)
152 return 2;
153 }
154 return 0;
155 }
156
157 int rpmxarPull(rpmxar xar, const char * fn)
158 {
159 const char * path = xar_get_path(xar->f);
160 int rc = 1;
161
162 if (fn != NULL && strcmp(fn, path)) {
163 path = _free(path);
164 return rc;
165 }
166 xar->member = _free(xar->member);
167 xar->member = path;
168
169 xar->b = _free(xar->b);
170 xar->bsize = xar->bx = 0;
171
172 /*@-nullstate @*/
173 rc = (int) xar_extract_tobuffersz(xar->x, xar->f, (char **)&xar->b, &xar->bsize);
174 /*@=nullstate @*/
175 if (rc)
176 return 1;
177
178 /*@+charint -nullpass -nullderef @*/
179 if (_xar_debug) {
180 unsigned char * b = xar->b;
181 size_t bsize = xar->bsize;
182 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]);
183 }
184 /*@=charint =nullpass =nullderef @*/
185
186 return 0;
187 }
188
189 int rpmxarSwapBuf(rpmxar xar, unsigned char * b, size_t bsize,
190 unsigned char ** obp, size_t * obsizep)
191 {
192 if (_xar_debug)
193 fprintf(stderr, "--> rpmxarSwapBuf(%p, %p[%u], %p, %p) %p[%u]\n", xar, b, (unsigned) bsize, obp, obsizep, xar->b, (unsigned) xar->bsize);
194
195 if (xar) {
196 if (obsizep != NULL)
197 *obsizep = xar->bsize;
198 if (obp != NULL) {
199 /*@-onlytrans@*/
200 *obp = xar->b;
201 /*@=onlytrans@*/
202 xar->b = NULL;
203 }
204 xar->b = _free(xar->b);
205 /*@-assignexpose -temptrans @*/
206 xar->b = b;
207 /*@=assignexpose =temptrans @*/
208 xar->bsize = bsize;
209 }
210 /*@-nullstate@*/
211 return 0;
212 /*@=nullstate@*/
213 }
214
215 ssize_t xarRead(void * cookie, /*@out@*/ char * buf, size_t count)
216 {
217 FD_t fd = cookie;
218 rpmxar xar = fdGetXAR(fd);
219 ssize_t rc = 0;
220
221 assert(xar != NULL);
222 #if 0
223 if ((xx = rpmxarNext(xar)) != 0) return RPMRC_FAIL;
224 if ((xx = rpmxarPull(xar, "Signature")) != 0) return RPMRC_FAIL;
225 (void) rpmxarSwapBuf(xar, NULL, 0, &b, &nb);
226 #endif
227
228 rc = xar->bsize - xar->bx;
229 if (rc > 0) {
230 if (count < (size_t)rc) rc = count;
231 assert(xar->b != NULL);
232 memmove(buf, &xar->b[xar->bx], rc);
233 xar->bx += rc;
234 } else
235 if (rc < 0) {
236 rc = -1;
237 } else
238 rc = 0;
239
240 if (_xar_debug)
241 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);
242
243 return rc;
244 }