rpm  5.4.15
rpmwf.c
Go to the documentation of this file.
1 
2 #include "system.h"
3 
4 #include <rpmio.h>
5 #include <rpmiotypes.h>
6 #include <rpmtypes.h>
7 
8 #include <rpmxar.h>
9 #define _RPMWF_INTERNAL
10 #include <rpmwf.h>
11 
12 #include "debug.h"
13 
14 /*@access FD_t @*/
15 /*@access rpmxar @*/ /* XXX fprintf */
16 
17 /*@unchecked@*/
18 int _rpmwf_debug = 0;
19 
20 rpmRC rpmwfPushXAR(rpmwf wf, const char * fn)
21 {
22  char * b = NULL;
23  size_t nb = 0;
24  int xx;
25 
26  if (!strcmp(fn, "Lead")) {
27  b = wf->l;
28  nb = wf->nl;
29  } else
30  if (!strcmp(fn, "Signature")) {
31  b = wf->s;
32  nb = wf->ns;
33  } else
34  if (!strcmp(fn, "Header")) {
35  b = wf->h;
36  nb = wf->nh;
37  } else
38  if (!strcmp(fn, "Payload")) {
39  b = wf->p;
40  nb = wf->np;
41  }
42 
43 if (_rpmwf_debug)
44 fprintf(stderr, "==> rpmwfPushXAR(%p, %s) %p[%u]\n", wf, fn, b, (unsigned) nb);
45 
46  if (!(b && nb > 0))
47  return RPMRC_FAIL;
48  xx = rpmxarPush(wf->xar, fn, (unsigned char *)b, nb);
49  return (xx == 0 ? RPMRC_OK : RPMRC_FAIL);
50 }
51 
52 rpmRC rpmwfPullXAR(rpmwf wf, const char * fn)
53 {
54  rpmRC rc = RPMRC_OK;
55  char * b = NULL;
56  size_t nb = 0;
57  int xx;
58 
59  xx = rpmxarPull(wf->xar, fn);
60  if (xx == 1)
61  return RPMRC_NOTFOUND;
62  xx = rpmxarSwapBuf(wf->xar, NULL, 0, (unsigned char **)&b, &nb);
63 
64 if (_rpmwf_debug)
65 fprintf(stderr, "==> rpmwfPullXAR(%p, %s) %p[%u]\n", wf, fn, b, (unsigned) nb);
66 
67  if (!strcmp(fn, "Lead")) {
68  wf->l = b;
69  wf->nl = nb;
70  } else
71  if (!strcmp(fn, "Signature")) {
72  wf->s = b;
73  wf->ns = nb;
74  } else
75  if (!strcmp(fn, "Header")) {
76  wf->h = b;
77  wf->nh = nb;
78  } else
79  if (!strcmp(fn, "Payload")) {
80  wf->p = b;
81  wf->np = nb;
82  } else
83  rc = RPMRC_NOTFOUND;
84 
85  return rc;
86 }
87 
89 {
90  int xx;
91 
92 if (_rpmwf_debug)
93 fprintf(stderr, "==> rpmwfFini(%p)\n", wf);
94 
95  if (wf->b && wf->b != (void *)-1) {
96  xx = munmap(wf->b, wf->nb);
97  wf->b = NULL;
98  }
99  if (wf->fd) {
100  (void) Fclose(wf->fd);
101  wf->fd = NULL;
102  }
103  return RPMRC_OK;
104 }
105 
106 static size_t hSize(rpmuint32_t *p)
107  /*@*/
108 {
109  return (8 + 8 + 16 * ntohl(p[2]) + ntohl(p[3]));
110 }
111 
112 rpmRC rpmwfInit(rpmwf wf, const char * fn, const char * fmode)
113 {
114 if (_rpmwf_debug)
115 fprintf(stderr, "==> rpmwfInit(%p, %s, %s)\n", wf, fn, fmode);
116  if (fn == NULL)
117  fn = wf->fn;
118 assert(fn != NULL);
119 
120 /*@-globs@*/
121  wf->fd = Fopen(fn, fmode);
122 /*@=globs@*/
123  if (wf->fd == NULL || Ferror(wf->fd)) {
124  (void) rpmwfFini(wf);
125  return RPMRC_NOTFOUND;
126  }
127 
128  if (fmode && *fmode == 'r') {
129  wf->b = mmap(NULL, wf->nb, PROT_READ, MAP_SHARED, Fileno(wf->fd), 0L);
130 
131  if (wf->b == (void *)-1) {
132  wf->b = NULL;
133  (void) rpmwfFini(wf);
134  return RPMRC_NOTFOUND;
135  }
136 
137  wf->l = (char *) wf->b;
138 assert(wf->l != NULL);
139  wf->nl = 96;
140 
141  wf->s = wf->l + wf->nl;
142  wf->ns = hSize((rpmuint32_t *)wf->s);
143  wf->ns += ((8 - (wf->ns % 8)) % 8); /* padding */
144 
145  wf->h = wf->s + wf->ns;
146  wf->nh = hSize((rpmuint32_t *)wf->h);
147 
148  wf->p = wf->h + wf->nh;
149  wf->np = wf->nb;
150  wf->np -= wf->nl + wf->ns + wf->nh;
151  }
152 
153  return RPMRC_OK;
154 }
155 
156 rpmRC rpmwfPushRPM(rpmwf wf, const char * fn)
157 {
158  char * b = NULL;
159  size_t nb = 0;
160 
161  if (!strcmp(fn, "Lead")) {
162  b = wf->l;
163  nb = wf->nl;
164  } else
165  if (!strcmp(fn, "Signature")) {
166  b = wf->s;
167  nb = wf->ns;
168  } else
169  if (!strcmp(fn, "Header")) {
170  b = wf->h;
171  nb = wf->nh;
172  } else
173  if (!strcmp(fn, "Payload")) {
174  b = wf->p;
175  nb = wf->np;
176  }
177 
178  if (!(b && nb > 0))
179  return RPMRC_NOTFOUND;
180 
181 if (_rpmwf_debug)
182 fprintf(stderr, "==> rpmwfPushRPM(%p, %s) %p[%u]\n", wf, fn, b, (unsigned) nb);
183 
184  if (Fwrite(b, sizeof(b[0]), nb, wf->fd) != nb)
185  return RPMRC_FAIL;
186 
187  return RPMRC_OK;
188 }
189 
190 /*@-mustmod@*/
191 static void rpmwfScrub(void *_wf)
192  /*@globals fileSystem, internalState @*/
193  /*@modifies _wf, fileSystem, internalState @*/
194 {
195  rpmwf wf = (rpmwf) _wf;
196 
197  if (wf->b == NULL) {
198 /*@-dependenttrans -onlytrans @*/ /* rpm needs dependent, xar needs only */
199  wf->l = _free(wf->l);
200  wf->s = _free(wf->s);
201  wf->h = _free(wf->h);
202  wf->p = _free(wf->p);
203 /*@=dependenttrans =onlytrans @*/
204  }
205 
206  wf->xar = rpmxarFree(wf->xar, "rpmwfFree");
207  (void) rpmwfFini(wf);
208 
209  wf->fn = _free(wf->fn);
210 }
211 /*@=mustmod@*/
212 
213 /*@unchecked@*/ /*@only@*/ /*@null@*/
215 
216 static rpmwf rpmwfGetPool(/*@null@*/ rpmioPool pool)
217  /*@globals _rpmwfPool, fileSystem @*/
218  /*@modifies pool, _rpmwfPool, fileSystem @*/
219 {
220  rpmwf wf;
221 
222  if (_rpmwfPool == NULL) {
223  _rpmwfPool = rpmioNewPool("wf", sizeof(*wf), -1, _rpmwf_debug,
224  NULL, NULL, rpmwfScrub);
225  pool = _rpmwfPool;
226  }
227  return (rpmwf) rpmioGetPool(pool, sizeof(*wf));
228 }
229 
230 rpmwf rpmwfNew(const char * fn)
231 {
232  struct stat sb, *st = &sb;
233  rpmwf wf;
234  int xx;
235 
236 /*@-globs@*/
237  if ((xx = Stat(fn, st)) < 0)
238  return NULL;
239 /*@=globs@*/
240  wf = rpmwfGetPool(_rpmwfPool);
241  wf->fn = xstrdup(fn);
242  wf->nb = (size_t)st->st_size;
243 
244  return rpmwfLink(wf, "rpmwfNew");
245 }
246 
247 static void rpmwfDumpItem(const char * item, unsigned char * b, size_t bsize)
248  /*@*/
249 {
250 /*@+charint -modfilesys @*/
251  fprintf(stderr, "\t%s:\t%p[%u]\t%02x%02x%02x%02x%02x%02x%02x%02x\n", item, b, (unsigned)bsize, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
252 /*@=charint =modfilesys @*/
253 }
254 
255 static void rpmwfDump(rpmwf wf, const char * msg, const char * fn)
256  /*@*/
257 {
258 /*@-modfilesys @*/
259  fprintf(stderr, "==> %s(%s) wf %p\n", msg, fn, wf);
260 /*@=modfilesys @*/
261 /*@-noeffect@*/
262  rpmwfDumpItem(" Lead", (unsigned char *)wf->l, wf->nl);
263  rpmwfDumpItem("Signature", (unsigned char *)wf->s, wf->ns);
264  rpmwfDumpItem(" Header", (unsigned char *)wf->h, wf->nh);
265  rpmwfDumpItem(" Payload", (unsigned char *)wf->p, wf->np);
266 /*@=noeffect@*/
267 }
268 
269 rpmwf rdRPM(const char * rpmfn)
270 {
271  rpmwf wf;
272  rpmRC rc;
273 
274  if ((wf = rpmwfNew(rpmfn)) == NULL)
275  return wf;
276 
277  if ((rc = rpmwfInit(wf, NULL, "r")) != RPMRC_OK) {
278  wf = rpmwfFree(wf);
279  return NULL;
280  }
281 
282 /*@-noeffect@*/
283 if (_rpmwf_debug) rpmwfDump(wf, "rdRPM", rpmfn);
284 /*@=noeffect@*/
285 
286  return wf;
287 }
288 
289 rpmwf rdXAR(const char * xarfn)
290 {
291  rpmwf wf;
292  rpmRC rc;
293 
294  if ((wf = rpmwfNew(xarfn)) == NULL)
295  return wf;
296 
297  wf->xar = rpmxarNew(wf->fn, "r");
298  if (wf->xar == NULL) {
299  wf = rpmwfFree(wf);
300  return NULL;
301  }
302 
303  while (rpmxarNext(wf->xar) == 0)
304  rc = rpmwfPullXAR(wf, NULL);
305  wf->xar = rpmxarFree(wf->xar, "rdXAR");
306 
307 /*@-noeffect@*/
308 if (_rpmwf_debug) rpmwfDump(wf, "rdXAR", xarfn);
309 /*@=noeffect@*/
310 
311  return wf;
312 }
313 
314 rpmRC wrXAR(const char * xarfn, rpmwf wf)
315 {
316  rpmRC rc;
317 
318 /*@-noeffect@*/
319 if (_rpmwf_debug) rpmwfDump(wf, "wrXAR", xarfn);
320 /*@=noeffect@*/
321 
322  wf->xar = rpmxarNew(xarfn, "w");
323  if (wf->xar == NULL)
324  return RPMRC_FAIL;
325 
326  if ((rc = rpmwfPushXAR(wf, "Lead")) != RPMRC_OK)
327  goto exit;
328  if ((rc = rpmwfPushXAR(wf, "Signature")) != RPMRC_OK)
329  goto exit;
330  if ((rc = rpmwfPushXAR(wf, "Header")) != RPMRC_OK)
331  goto exit;
332  if ((rc = rpmwfPushXAR(wf, "Payload")) != RPMRC_OK)
333  goto exit;
334 
335 exit:
336  wf->xar = rpmxarFree(wf->xar, "wrXAR");
337  return rc;
338 }
339 
340 rpmRC wrRPM(const char * rpmfn, rpmwf wf)
341 {
342  rpmRC rc;
343 
344  if ((rc = rpmwfInit(wf, rpmfn, "w")) != RPMRC_OK)
345  goto exit;
346 
347 if (_rpmwf_debug)
348 fprintf(stderr, "==> wrRPM(%s) wf %p\n\tLead %p[%u]\n\tSignature %p[%u]\n\tHeader %p[%u]\n\tPayload %p[%u]\n", rpmfn, wf, wf->l, (unsigned)wf->nl, wf->s, (unsigned) wf->ns, wf->h, (unsigned) wf->nh, wf->p, (unsigned) wf->np);
349 
350  if ((rc = rpmwfPushRPM(wf, "Lead")) != RPMRC_OK)
351  goto exit;
352  if ((rc = rpmwfPushRPM(wf, "Signature")) != RPMRC_OK)
353  goto exit;
354  if ((rc = rpmwfPushRPM(wf, "Header")) != RPMRC_OK)
355  goto exit;
356  if ((rc = rpmwfPushRPM(wf, "Payload")) != RPMRC_OK)
357  goto exit;
358 
359 exit:
360  (void) rpmwfFini(wf);
361 
362  return rc;
363 }
Structure(s)and methods for a XAR archive wrapper format.
const bson * b
Definition: bson.h:280
Structure(s)and methods for a archive wrapper format (e.g.
rpmRC rpmwfFini(rpmwf wf)
Definition: rpmwf.c:88
rpmwf rdRPM(const char *rpmfn)
Definition: rpmwf.c:269
rpmRC wrRPM(const char *rpmfn, rpmwf wf)
Definition: rpmwf.c:340
static void rpmwfDump(rpmwf wf, const char *msg, const char *fn)
Definition: rpmwf.c:255
size_t Fwrite(const void *buf, size_t size, size_t nmemb, FD_t fd)
fwrite(3) clone.
Definition: rpmio.c:2434
char * xstrdup(const char *str)
Definition: rpmmalloc.c:321
FD_t Fopen(const char *path, const char *_fmode)
fopen(3) clone.
Definition: rpmio.c:2840
int rpmxarNext(rpmxar xar)
Iterate a xar archive instance.
Definition: rpmxar.c:128
int Stat(const char *path, struct stat *st)
stat(2) clone.
Definition: rpmrpc.c:1361
static void rpmwfDumpItem(const char *item, unsigned char *b, size_t bsize)
Definition: rpmwf.c:247
unsigned int rpmuint32_t
Definition: rpmiotypes.h:28
int rpmxarPush(rpmxar xar, const char *fn, unsigned char *b, size_t bsize)
Definition: rpmxar.c:147
int rpmxarPull(rpmxar xar, const char *fn)
Definition: rpmxar.c:168
rpmioItem rpmioGetPool(rpmioPool pool, size_t size)
Get unused item from pool, or alloc a new item.
Definition: rpmmalloc.c:220
rpmRC rpmwfPullXAR(rpmwf wf, const char *fn)
Definition: rpmwf.c:52
struct rpmwf_s * rpmwf
Definition: rpmwf.h:16
static void rpmwfScrub(void *_wf)
Definition: rpmwf.c:191
static size_t hSize(rpmuint32_t *p)
Definition: rpmwf.c:106
rpmwf rpmwfNew(const char *fn)
Definition: rpmwf.c:230
rpmRC rpmwfPushXAR(rpmwf wf, const char *fn)
Definition: rpmwf.c:20
int Fclose(FD_t fd)
fclose(3) clone.
Definition: rpmio.c:2534
rpmioPool _rpmwfPool
Definition: rpmwf.c:214
rpmwf rpmwfFree(rpmwf wf)
rpmwf rpmwfLink(rpmwf wf, const char *msg)
Reference a wrapper format instance.
enum rpmRC_e rpmRC
RPM return codes.
int Ferror(FD_t fd)
ferror(3) clone.
Definition: rpmio.c:2951
#define L(CS)
Definition: fnmatch.c:161
static rpmwf rpmwfGetPool(rpmioPool pool)
Definition: rpmwf.c:216
rpmioPool rpmioNewPool(const char *name, size_t size, int limit, int flags, char *(*dbg)(void *item), void(*init)(void *item), void(*fini)(void *item))
Create a memory pool.
Definition: rpmmalloc.c:109
int rpmxarSwapBuf(rpmxar xar, unsigned char *b, size_t bsize, unsigned char **obp, size_t *obsizep)
Definition: rpmxar.c:203
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: rpmiotypes.h:756
rpmRC rpmwfPushRPM(rpmwf wf, const char *fn)
Definition: rpmwf.c:156
rpmRC wrXAR(const char *xarfn, rpmwf wf)
Definition: rpmwf.c:314
rpmRC rpmwfInit(rpmwf wf, const char *fn, const char *fmode)
Definition: rpmwf.c:112
int Fileno(FD_t fd)
fileno(3) clone.
Definition: rpmio.c:2998
rpmxar rpmxarFree(rpmxar xar, const char *msg)
Destroy a xar archive instance.
int _rpmwf_debug
Definition: rpmwf.c:18
rpmxar rpmxarNew(const char *fn, const char *fmode)
Create a xar archive instance.
Definition: rpmxar.c:112
rpmwf rdXAR(const char *xarfn)
Definition: rpmwf.c:289