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