rpm  5.4.15
rpmz.h
Go to the documentation of this file.
1 #ifndef H_RPMZ
2 #define H_RPMZ
3 
9 #include <rpmiotypes.h>
10 #include <rpmio.h>
11 #include <rpmlog.h>
12 #include <argv.h>
13 
14 #include <rpmsq.h>
15 /*@-bufferoverflowhigh -mustfreeonly@*/
16 #include <poptIO.h>
17 /*@=bufferoverflowhigh =mustfreeonly@*/
18 #if !defined(POPT_ARGFLAG_TOGGLE) /* XXX compat with popt < 1.15 */
19 #define POPT_ARGFLAG_TOGGLE 0
20 #endif
21 
22 #include "rpmzlog.h"
23 
24 #include "rpmzq.h"
25 
28 typedef struct rpmz_s * rpmz;
29 
30 #if defined(_RPMZ_INTERNAL)
31 
32 /*@unchecked@*/
33 extern struct rpmz_s __rpmz;
34 
35 /*@unchecked@*/
36 extern rpmz _rpmz;
37 
40 struct rpmz_s {
41 /*@observer@*/
42  const char *stdin_fn;
43 /*@observer@*/
44  const char *stdout_fn;
46 /*@null@*/
47  ARGV_t argv;
48 /*@null@*/
49  ARGV_t manifests;
50 /*@null@*/
51  const char * base_prefix;
53  char _ifn[PATH_MAX+1];
55 #if defined(_RPMZ_INTERNAL_XZ)
56  rpmuint64_t mem;
57  rpmuint64_t memlimit_encoder;
58  rpmuint64_t memlimit_decoder;
59  rpmuint64_t memlimit_custom;
60 
61 /*@relnull@*/
62  rpmiob iob;
63  size_t nb;
65 /*@null@*/
66  const char * isuffix;
67  enum rpmzFormat_e ifmt;
68  FDIO_t idio;
69  char ifmode[32];
70 /*@null@*/
71  FD_t ifd;
72  struct stat isb;
73 
74 /*@null@*/
75  const char * osuffix;
76  enum rpmzFormat_e ofmt;
77  FDIO_t odio;
78  char ofmode[32];
79 /*@null@*/
80  FD_t ofd;
81  struct stat osb;
82 
83  int _auto_adjust;
84  enum rpmzFormat_e _format_compress_auto;
85  lzma_options_lzma _options;
86  lzma_check _checksum;
87  lzma_filter _filters[LZMA_FILTERS_MAX + 1];
88  size_t _filters_count;
89 #endif /* _RPMZ_INTERNAL_XZ */
90 
91 /* ++++ rpmzq */
92  struct rpmzQueue_s _zq;
93 /*@owned@*/ /*@relnull@*/
94  rpmzQueue zq;
95 /* ---- rpmzq */
96 
97 };
98 
99 #endif /* _RPMZ_INTERNAL */
100 
101 /*==============================================================*/
102 
103 #ifdef __cplusplus
104 extern "C" {
105 #endif
106 
107 #if defined(_RPMZ_INTERNAL)
108 
110 static int rpmzLoadManifests(rpmz z)
111  /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
112  /*@modifies z, rpmGlobalMacroContext, fileSystem, internalState @*/
113 {
114  ARGV_t manifests;
115  const char * fn;
116  int rc = 0; /* assume success */
117 
118  if ((manifests = z->manifests) != NULL) /* note rc=0 return with no files to load. */
119  while ((fn = *manifests++) != NULL) {
120  unsigned lineno;
121  char * be = NULL;
122  rpmiob iob = NULL;
123  int xx = rpmiobSlurp(fn, &iob);
124  char * f;
125  char * fe;
126 
127  if (!(xx == 0 && iob != NULL)) {
128  fprintf(stderr, _("%s: Failed to open %s\n"), __progname, fn);
129  rc = -1;
130  goto bottom;
131  }
132 
133  be = (char *)(iob->b + iob->blen);
134  while (be > (char *)iob->b && (be[-1] == '\n' || be[-1] == '\r')) {
135  be--;
136  *be = '\0';
137  }
138 
139  /* Parse and save manifest items. */
140  lineno = 0;
141  for (f = (char *)iob->b; *f; f = fe) {
142  const char * path;
143  char * g, * ge;
144  lineno++;
145 
146  fe = f;
147  while (*fe && !(*fe == '\n' || *fe == '\r'))
148  fe++;
149  g = f;
150  ge = fe;
151  while (*fe && (*fe == '\n' || *fe == '\r'))
152  *fe++ = '\0';
153 
154  while (*g && xisspace((int)*g))
155  *g++ = '\0';
156  /* Skip comment lines. */
157  if (*g == '#')
158  /*@innercontinue@*/ continue;
159 
160  while (ge > g && xisspace(ge[-1]))
161  *--ge = '\0';
162  /* Skip empty lines. */
163  if (ge == g)
164  /*@innercontinue@*/ continue;
165 
166  /* Prepend z->base_prefix if specified. */
167  if (z->base_prefix)
168  path = rpmExpand(z->base_prefix, g, NULL);
169  else
170  path = rpmExpand(g, NULL);
171  (void) argvAdd(&z->argv, path);
172  path = _free(path);
173  }
174 
175 bottom:
176  iob = rpmiobFree(iob);
177  if (rc != 0)
178  goto exit;
179  }
180 
181 exit:
182  return rc;
183 }
184 
187 static rpmRC rpmzParseEnv(/*@unused@*/ rpmz z, /*@null@*/ const char * envvar,
188  struct poptOption optionsTable[])
189  /*@globals fileSystem, internalState @*/
190  /*@modifies fileSystem, internalState @*/
191 {
192  static char whitespace[] = " \f\n\r\t\v,";
193  static char _envvar[] = "RPMZ";
194  char * s = getenv((envvar ? envvar : _envvar));
195  ARGV_t av = NULL;
196  poptContext optCon = NULL;
197  rpmRC rc = RPMRC_OK;
198  int ac;
199  int xx;
200 
201  if (s == NULL)
202  goto exit;
203 
204  /* XXX todo: argvSplit() assumes single separator between args. */
205 /*@-nullstate@*/
206  xx = argvSplit(&av, s, whitespace);
207 /*@=nullstate@*/
208  ac = argvCount(av);
209  if (ac < 1)
210  goto exit;
211 
212  optCon = poptGetContext(__progname, ac, (const char **)av,
213  optionsTable, POPT_CONTEXT_KEEP_FIRST);
214 
215  /* Process all options, whine if unknown. */
216  while ((xx = poptGetNextOpt(optCon)) > 0) {
217  const char * optArg = poptGetOptArg(optCon);
218 /*@-dependenttrans -modobserver -observertrans @*/
219  optArg = _free(optArg);
220 /*@=dependenttrans =modobserver =observertrans @*/
221  switch (xx) {
222  default:
223 /*@-nullpass@*/
224  fprintf(stderr, _("%s: option table misconfigured (%d)\n"),
225  __progname, xx);
226 /*@=nullpass@*/
227  rc = RPMRC_FAIL;
228  goto exit;
229  /*@notreached@*/ /*@switchbreak@*/ break;
230  }
231  }
232 
233  if (xx < -1) {
234 /*@-nullpass@*/
235  fprintf(stderr, "%s: %s: %s\n", __progname,
236  poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
237  poptStrerror(xx));
238 /*@=nullpass@*/
239  rc = RPMRC_FAIL;
240  } else /* Check no args were in the envvar. */
241  if (argvCount(poptGetArgs(optCon))) {
242  fprintf(stderr, "%s: cannot provide files in %s envvar\n",
243  __progname, (envvar ? envvar : _envvar));
244  rc = RPMRC_FAIL;
245  }
246 
247 exit:
248  if (optCon)
249  optCon = poptFreeContext(optCon);
250  av = argvFree(av);
251  return rc;
252 }
253 #endif /* _RPMZ_INTERNAL */
254 
255 #if defined(_RPMZ_INTERNAL_XZ)
256 
258 static rpmRC rpmzParseArgv0(rpmz z, const char * argv0)
259  /*@globals fileSystem, internalState @*/
260  /*@modifies z, fileSystem, internalState @*/
261 {
262  rpmzQueue zq = z->zq;
263  const char * s = strrchr(argv0, '/');
264  const char * name = (s ? (s + 1) : argv0);
265  rpmRC rc = RPMRC_OK;
266 
267 #if defined(WITH_XZ)
268  if (strstr(name, "xz") != NULL) {
269  z->_format_compress_auto = RPMZ_FORMAT_XZ;
270  zq->format = RPMZ_FORMAT_XZ; /* XXX eliminate */
271  } else
272  if (strstr(name, "lz") != NULL) {
273  z->_format_compress_auto = RPMZ_FORMAT_LZMA;
274  zq->format = RPMZ_FORMAT_LZMA; /* XXX eliminate */
275  } else
276 #endif /* WITH_XZ */
277 #if defined(WITH_BZIP2)
278  if (strstr(name, "bz") != NULL) {
279  z->_format_compress_auto = RPMZ_FORMAT_BZIP2;
280  zq->format = RPMZ_FORMAT_BZIP2; /* XXX eliminate */
281  } else
282 #endif /* WITH_BZIP2 */
283 #if defined(WITH_ZLIB)
284  if (strstr(name, "gz") != NULL) {
285  z->_format_compress_auto = RPMZ_FORMAT_GZIP;
286  zq->format = RPMZ_FORMAT_GZIP; /* XXX eliminate */
287  } else
288  if (strstr(name, "zlib") != NULL) {
289  z->_format_compress_auto = RPMZ_FORMAT_ZLIB;
290  zq->format = RPMZ_FORMAT_ZLIB; /* XXX eliminate */
291  } else
292  /* XXX watchout for "bzip2" matching */
293  if (strstr(name, "zip") != NULL) {
294  z->_format_compress_auto = RPMZ_FORMAT_ZIP2;
295  zq->format = RPMZ_FORMAT_ZIP2; /* XXX eliminate */
296  } else
297 #endif /* WITH_ZLIB */
298  {
299  z->_format_compress_auto = RPMZ_FORMAT_AUTO;
300  zq->format = RPMZ_FORMAT_AUTO; /* XXX eliminate */
301  }
302 
303  if (strstr(name, "cat") != NULL) {
304  zq->mode = RPMZ_MODE_DECOMPRESS;
305  zq->flags |= RPMZ_FLAGS_STDOUT;
306  } else if (strstr(name, "un") != NULL) {
307  zq->mode = RPMZ_MODE_DECOMPRESS;
308  }
309 
310  return rc;
311 }
312 #endif /* _RPMZ_INTERNAL_XZ */
313 
314 #ifdef __cplusplus
315 }
316 #endif
317 
318 #endif
char * getenv(const char *name)
#define __progname
Definition: system.h:363
struct rpmzQueue_s * rpmzQueue
Definition: rpmzq.h:23
static struct poptOption optionsTable[]
Definition: rpmqv.c:168
Job queue and buffer pool management.
rpmiob rpmiobFree(rpmiob iob)
Destroy a I/O buffer instance.
Definition: rpmio.h:107
int rpmiobSlurp(const char *fn, rpmiob *iobp)
Definition: rpmiob.c:129
Yet Another syslog(3) API clone.
rpmz _rpmz
Definition: rpmz.c:94
int argvCount(const ARGV_t argv)
Return no.
Definition: argv.c:71
struct rpmz_s * rpmz
Definition: rpmz.h:28
struct rpmz_s __rpmz
Definition: rpmz.c:69
static int xisspace(int c)
Definition: rpmiotypes.h:555
unsigned long long rpmuint64_t
Definition: rpmiotypes.h:29
ARGV_t argvFree(ARGV_t argv)
Destroy an argv array.
Definition: argv.c:44
rpmzFormat_e
Definition: rpmzq.h:59
The FD_t File Handle data structure.
int argvAdd(ARGV_t *argvp, ARGstr_t val)
Add a string to an argv array.
Definition: argv.c:199
char * rpmExpand(const char *arg,...)
Return (malloc'ed) concatenated macro expansion(s).
Definition: macro.c:3250
enum rpmRC_e rpmRC
RPM return codes.
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: rpmiotypes.h:756
struct rpmiob_s * rpmiob
Definition: rpmiotypes.h:60
int argvSplit(ARGV_t *argvp, const char *str, const char *seps)
Split a string into an argv array.
Definition: argv.c:233
static const char * name
#define _(Text)
Definition: system.h:29
ARGstr_t * ARGV_t
Definition: argv.h:12
#define PATH_MAX
Definition: query.c:10