rpm  4.12.0.1
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 <rpm/rpmlib.h> /* rpmReadPackageFile .. */
7 #include <rpm/rpmtag.h>
8 #include <rpm/rpmio.h>
9 #include <rpm/rpmpgp.h>
10 
11 #include <rpm/rpmts.h>
12 
13 #include "debug.h"
14 
15 int main(int argc, char *argv[])
16 {
17  FD_t fdi, fdo;
18  Header h;
19  char * rpmio_flags = NULL;
20  int rc;
21  off_t payload_size;
22  FD_t gzdi;
23 
24  setprogname(argv[0]); /* Retrofit glibc __progname */
25  rpmReadConfigFiles(NULL, NULL);
26  if (argc == 1)
27  fdi = fdDup(STDIN_FILENO);
28  else {
29  if (rstreq(argv[1], "-h") || rstreq(argv[1], "--help")) {
30  fprintf(stderr, "Usage: rpm2cpio file.rpm\n");
31  exit(EXIT_FAILURE);
32  }
33  fdi = Fopen(argv[1], "r.ufdio");
34  }
35 
36  if (Ferror(fdi)) {
37  fprintf(stderr, "%s: %s: %s\n", argv[0],
38  (argc == 1 ? "<stdin>" : argv[1]), Fstrerror(fdi));
39  exit(EXIT_FAILURE);
40  }
41  fdo = fdDup(STDOUT_FILENO);
42 
43  { rpmts ts = rpmtsCreate();
44  rpmVSFlags vsflags = 0;
45 
46  /* XXX retain the ageless behavior of rpm2cpio */
47  vsflags |= _RPMVSF_NODIGESTS;
48  vsflags |= _RPMVSF_NOSIGNATURES;
49  vsflags |= RPMVSF_NOHDRCHK;
50  (void) rpmtsSetVSFlags(ts, vsflags);
51 
52  rc = rpmReadPackageFile(ts, fdi, "rpm2cpio", &h);
53 
54  ts = rpmtsFree(ts);
55  }
56 
57  switch (rc) {
58  case RPMRC_OK:
59  case RPMRC_NOKEY:
60  case RPMRC_NOTTRUSTED:
61  break;
62  case RPMRC_NOTFOUND:
63  fprintf(stderr, _("argument is not an RPM package\n"));
64  exit(EXIT_FAILURE);
65  break;
66  case RPMRC_FAIL:
67  default:
68  fprintf(stderr, _("error reading header from package\n"));
69  exit(EXIT_FAILURE);
70  break;
71  }
72 
73  /* Retrieve payload size and compression type. */
74  { const char *compr = headerGetString(h, RPMTAG_PAYLOADCOMPRESSOR);
75  rpmio_flags = rstrscat(NULL, "r.", compr ? compr : "gzip", NULL);
76  payload_size = headerGetNumber(h, RPMTAG_LONGARCHIVESIZE);
77  }
78 
79  gzdi = Fdopen(fdi, rpmio_flags); /* XXX gzdi == fdi */
80  free(rpmio_flags);
81 
82  if (gzdi == NULL) {
83  fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
84  exit(EXIT_FAILURE);
85  }
86 
87  rc = (ufdCopy(gzdi, fdo) == payload_size) ? EXIT_SUCCESS : EXIT_FAILURE;
88 
89  Fclose(fdo);
90 
91  Fclose(gzdi); /* XXX gzdi == fdi */
92 
93  return rc;
94 }
#define setprogname(pn)
Definition: system.h:97
rpmRC rpmReadPackageFile(rpmts ts, FD_t fd, const char *fn, Header *hdrp)
Return package header from file handle, verifying digests/signatures.
rpmts rpmtsFree(rpmts ts)
Destroy transaction set, closing the database as well.
struct rpmts_s * rpmts
The main types involved in transaction manipulation.
Definition: rpmtypes.h:63
#define _(Text)
Definition: system.h:110
rpmts rpmtsCreate(void)
Create an empty transaction set.
int main(int argc, char *argv[])
Definition: rpm2cpio.c:15
struct _FD_s * FD_t
RPM IO file descriptor type.
Definition: rpmtypes.h:98
int rpmReadConfigFiles(const char *file, const char *target)
Read macro configuration file(s) for a target.
static int rstreq(const char *s1, const char *s2)
Test for string equality.
Definition: rpmstring.h:113
uint64_t headerGetNumber(Header h, rpmTagVal tag)
char * rstrscat(char **dest, const char *arg,...) RPM_GNUC_NULL_TERMINATED
Concatenate multiple strings with dynamically (re)allocated memory.
FD_t Fdopen(FD_t ofd, const char *fmode)
struct headerToken_s * Header
RPM header and data retrieval types.
Definition: rpmtypes.h:24
off_t ufdCopy(FD_t sfd, FD_t tfd)
rpmFlags rpmVSFlags
Definition: rpmts.h:110
rpmVSFlags rpmtsSetVSFlags(rpmts ts, rpmVSFlags vsflags)
Set verify signatures flag(s).
#define _RPMVSF_NOSIGNATURES
Definition: rpmts.h:118
const char * Fstrerror(FD_t fd)
strerror(3) clone.
FD_t fdDup(int fdno)
FD_t Fopen(const char *path, const char *fmode)
fopen(3) clone.
#define __progname
Definition: system.h:96
#define _RPMVSF_NODIGESTS
Definition: rpmts.h:112
const char * headerGetString(Header h, rpmTagVal tag)
Return a simple string tag from header.
int Ferror(FD_t fd)
ferror(3) clone.
int Fclose(FD_t fd)
fclose(3) clone.