rpm  5.4.10
rpm2cpio.c
Go to the documentation of this file.
1 /* rpmarchive: spit out the main archive portion of a package */
2 
3 #include "system.h"
4 const char *__progname;
5 
6 #include <rpmio.h>
7 #include <rpmiotypes.h> /* XXX fnpyKey */
8 #include <rpmurl.h>
9 #include <rpmtypes.h>
10 #include <rpmtag.h>
11 #include <pkgio.h>
12 
13 #include <rpmts.h>
14 
15 #include "debug.h"
16 
17 #ifdef __cplusplus
18 
19 #define QVA_ISSET(_qvaflags, _FLAG) ((_qvaflags) & (VERIFY_##_FLAG))
20 
21 #define VSF_ISSET(_vsflags, _FLAG) ((_vsflags) & (RPMVSF_##_FLAG))
22 #define VSF_SET(_vsflags, _FLAG) \
23  (*((unsigned *)&(_vsflags)) |= (RPMVSF_##_FLAG))
24 #define VSF_CLR(_vsflags, _FLAG) \
25  (*((unsigned *)&(_vsflags)) &= ~(RPMVSF_##_FLAG))
26 
27 #else /* __cplusplus */
28 
29 #define QVA_ISSET(_qvaflags, _FLAG) ((_qvaflags) & (VERIFY_##_FLAG))
30 
31 #define VSF_ISSET(_vsflags, _FLAG) ((_vsflags) & (RPMVSF_##_FLAG))
32 #define VSF_SET(_vsflags, _FLAG) (_vsflags) |= (RPMVSF_##_FLAG)
33 #define VSF_CLR(_vsflags, _FLAG) (_vsflags) &= ~(RPMVSF_##_FLAG)
34 
35 #endif /* __cplusplus */
36 
37 int main(int argc, char **argv)
38 {
39  FD_t fdi, fdo;
40  Header h;
41  char * rpmio_flags;
42  rpmRC rc;
43  FD_t gzdi;
44 
45  setprogname(argv[0]); /* Retrofit glibc __progname */
46  if (argc == 1 || (argc == 2 && !strcmp(argv[1], "-")))
47  fdi = fdDup(STDIN_FILENO);
48  else {
49  int ut = urlPath(argv[1], NULL);
50  if (ut == URL_IS_HTTP || ut == URL_IS_HTTPS) {
51  fprintf(stderr, "%s: %s: HTTP/HTTPS transport is non-functional.\n",
52  argv[0], argv[1]);
53  exit(EXIT_FAILURE);
54  }
55  fdi = Fopen(argv[1], "r");
56  }
57 
58  if (Ferror(fdi)) {
59  fprintf(stderr, "%s: %s: %s\n", argv[0],
60  (argc == 1 ? "<stdin>" : argv[1]), Fstrerror(fdi));
61  exit(EXIT_FAILURE);
62  }
63  fdo = fdDup(STDOUT_FILENO);
64 
65  { rpmts ts = rpmtsCreate();
67 
68  /* XXX retain the ageless behavior of rpm2cpio */
69  vsflags = (rpmVSFlags) 0; /* XXX FIXME: ignore default disablers. */
70 #if defined(SUPPORT_NOSIGNATURES)
71  /* --nodigests */
72  VSF_SET(vsflags, NOSHA1HEADER);
73  VSF_SET(vsflags, NOMD5HEADER);
74  VSF_SET(vsflags, NOSHA1);
75  VSF_SET(vsflags, NOMD5);
76 
77  /* --nosignature */
78  VSF_SET(vsflags, NODSAHEADER);
79  VSF_SET(vsflags, NORSAHEADER);
80  VSF_SET(vsflags, NODSA);
81  VSF_SET(vsflags, NORSA);
82 
83  /* --nohdrchk */
84  VSF_SET(vsflags, NOHDRCHK);
85 
86  VSF_CLR(vsflags, NEEDPAYLOAD); /* XXX needed? */
87 #endif
88  (void) rpmtsSetVSFlags(ts, vsflags);
89 
90  /*@-mustmod@*/ /* LCL: segfault */
91  rc = rpmReadPackageFile(ts, fdi, "rpm2cpio", &h);
92  /*@=mustmod@*/
93 
94  (void)rpmtsFree(ts);
95  ts = NULL;
96  }
97 
98  switch (rc) {
99  case RPMRC_OK:
100  case RPMRC_NOKEY:
101  case RPMRC_NOTTRUSTED:
102  break;
103  case RPMRC_NOTFOUND:
104  fprintf(stderr, _("argument is not an RPM package\n"));
105  exit(EXIT_FAILURE);
106  break;
107  case RPMRC_FAIL:
108  default:
109  fprintf(stderr, _("error reading header from package\n"));
110  exit(EXIT_FAILURE);
111  break;
112  }
113 
114  /* Retrieve type of payload compression. */
115  { HE_t he = memset(alloca(sizeof(*he)), 0, sizeof(*he));
116  const char * payload_compressor = NULL;
117  char * t;
118  int xx;
119 
121  xx = headerGet(h, he, 0);
122  payload_compressor = (xx ? he->p.str : "gzip");
123 
124  rpmio_flags = t = alloca(sizeof("r.gzdio"));
125  *t++ = 'r';
126  if (!strcmp(payload_compressor, "gzip"))
127  t = stpcpy(t, ".gzdio");
128  if (!strcmp(payload_compressor, "bzip2"))
129  t = stpcpy(t, ".bzdio");
130  if (!strcmp(payload_compressor, "lzma"))
131  t = stpcpy(t, ".lzdio");
132  if (!strcmp(payload_compressor, "xz"))
133  t = stpcpy(t, ".xzdio");
134  he->p.ptr = _free(he->p.ptr);
135  }
136 
137  gzdi = Fdopen(fdi, rpmio_flags); /* XXX gzdi == fdi */
138  if (gzdi == NULL) {
139  fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
140  exit(EXIT_FAILURE);
141  }
142 
143  rc = ufdCopy(gzdi, fdo);
144  rc = (rc <= 0) ? EXIT_FAILURE : EXIT_SUCCESS;
145  Fclose(fdo);
146 
147  Fclose(gzdi); /* XXX gzdi == fdi */
148 
149  return rc;
150 }
const char * str
Definition: rpmtag.h:72
rpmTag tag
Definition: rpmtag.h:504
int ufdCopy(FD_t sfd, FD_t tfd)
Definition: rpmio.c:1546
#define setprogname(pn)
Definition: system.h:429
#define EXIT_FAILURE
FD_t Fopen(const char *path, const char *_fmode)
fopen(3) clone.
Definition: rpmio.c:2833
int headerGet(Header h, HE_t he, unsigned int flags)
Retrieve extension or tag value from a header.
Definition: header.c:2222
The Header data structure.
static rpmVSFlags vsflags
Definition: rpmcache.c:547
FD_t fdDup(int fdno)
Definition: rpmio.c:264
#define VSF_SET(_vsflags, _FLAG)
Definition: rpm2cpio.c:32
char * alloca()
const char * __progname
Definition: rpm2cpio.c:4
const char * Fstrerror(FD_t fd)
strerror(3) clone.
Definition: rpmio.c:2401
void * ptr
Definition: rpmtag.h:66
rpmTagData p
Definition: rpmtag.h:507
int main(int argc, char **argv)
Definition: rpm2cpio.c:37
rpmRC rpmReadPackageFile(rpmts ts, FD_t fd, const char *fn, Header *hdrp)
Return package header from file handle, verifying digests/signatures.
Definition: package.c:428
The FD_t File Handle data structure.
pgpVSFlags rpmVSFlags
Bit(s) to control digest and signature verification.
Definition: rpmts.h:30
int Fclose(FD_t fd)
fclose(3) clone.
Definition: rpmio.c:2534
enum rpmRC_e rpmRC
RPM return codes.
int Ferror(FD_t fd)
ferror(3) clone.
Definition: rpmio.c:2944
Definition: rpmtag.h:503
urltype urlPath(const char *url, const char **pathp)
Return path component of URL.
Definition: url.c:429
rpmts rpmtsFree(rpmts ts)
Destroy transaction set, closing the database as well.
rpmts rpmtsCreate(void)
Create an empty transaction set.
Definition: rpmts.c:1480
Methods to handle package elements.
rpmVSFlags rpmtsSetVSFlags(rpmts ts, rpmVSFlags vsflags)
Set verify signatures flag(s).
Definition: rpmts.c:843
char * stpcpy(char *dest, const char *src)
struct rpmts_s * rpmts
The RPM Transaction Set.
Definition: rpmtypes.h:14
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: rpmiotypes.h:647
Structures and prototypes used for an "rpmts" transaction set.
FD_t Fdopen(FD_t ofd, const char *fmode)
Definition: rpmio.c:2718
#define _(Text)
Definition: system.h:30
#define VSF_CLR(_vsflags, _FLAG)
Definition: rpm2cpio.c:33