/* rpmarchive: spit out the main archive portion of a package */
#include "system.h"
+const char *__progname;
-#include "rpmlib.h"
+#include <rpm/rpmlib.h> /* rpmReadPackageFile .. */
+#include <rpm/rpmtag.h>
+#include <rpm/rpmio.h>
+#include <rpm/rpmpgp.h>
-char *zlib_err [] = {
- "No",
- "Unix",
- "Data",
- "Memory",
- "Buffer",
- "Version"
-};
+#include <rpm/rpmts.h>
-int main(int argc, char **argv)
+#include "debug.h"
+
+int main(int argc, char *argv[])
{
- int fd;
- Header hd;
- int rc, isSource;
- char buffer[1024];
- int ct;
- gzFile stream;
+ FD_t fdi, fdo;
+ Header h;
+ char * rpmio_flags = NULL;
+ rpmRC rc;
+ FD_t gzdi;
- if (argc == 1) {
- fd = 0;
- } else {
- fd = open(argv[1], O_RDONLY, 0644);
+ setprogname(argv[0]); /* Retrofit glibc __progname */
+ rpmReadConfigFiles(NULL, NULL);
+ if (argc == 1)
+ fdi = fdDup(STDIN_FILENO);
+ else {
+ if (rstreq(argv[1], "-h") || rstreq(argv[1], "--help")) {
+ fprintf(stderr, "Usage: rpm2cpio file.rpm\n");
+ exit(EXIT_FAILURE);
+ }
+ fdi = Fopen(argv[1], "r.ufdio");
+ }
+
+ if (Ferror(fdi)) {
+ fprintf(stderr, "%s: %s: %s\n", argv[0],
+ (argc == 1 ? "<stdin>" : argv[1]), Fstrerror(fdi));
+ exit(EXIT_FAILURE);
}
+ fdo = fdDup(STDOUT_FILENO);
+
+ { rpmts ts = rpmtsCreate();
+ rpmVSFlags vsflags = 0;
+
+ /* XXX retain the ageless behavior of rpm2cpio */
+ vsflags |= _RPMVSF_NODIGESTS;
+ vsflags |= _RPMVSF_NOSIGNATURES;
+ vsflags |= RPMVSF_NOHDRCHK;
+ (void) rpmtsSetVSFlags(ts, vsflags);
- if (fd < 0) {
- perror("cannot open package");
- exit(1);
+ rc = rpmReadPackageFile(ts, fdi, "rpm2cpio", &h);
+
+ ts = rpmtsFree(ts);
}
- rc = rpmReadPackageHeader(fd, &hd, &isSource, NULL, NULL);
- if (rc == 1) {
+ switch (rc) {
+ case RPMRC_OK:
+ case RPMRC_NOKEY:
+ case RPMRC_NOTTRUSTED:
+ break;
+ case RPMRC_NOTFOUND:
fprintf(stderr, _("argument is not an RPM package\n"));
- exit(1);
- } else if (rc) {
+ exit(EXIT_FAILURE);
+ break;
+ case RPMRC_FAIL:
+ default:
fprintf(stderr, _("error reading header from package\n"));
- exit(1);
+ exit(EXIT_FAILURE);
+ break;
}
- stream = gzdopen(fd, "r");
-
- while ((ct = gzread(stream, &buffer, 1024)) > 0) {
- write(1, &buffer, ct);
+ /* Retrieve type of payload compression. */
+ { const char *compr = headerGetString(h, RPMTAG_PAYLOADCOMPRESSOR);
+ rpmio_flags = rstrscat(NULL, "r.", compr ? compr : "gzip", NULL);
}
- if (ct < 0){
- int zerror;
-
- gzerror (stream, &zerror);
- if (zerror == Z_ERRNO){
- perror ("While uncompressing");
- gzclose(stream);
- return 1;
- }
- fprintf (stderr, "rpm2cpio: zlib: %s error\n", zlib_err [-zerror - 1]);
+
+ gzdi = Fdopen(fdi, rpmio_flags); /* XXX gzdi == fdi */
+ free(rpmio_flags);
+
+ if (gzdi == NULL) {
+ fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
+ exit(EXIT_FAILURE);
}
- gzclose(stream);
+ rc = ufdCopy(gzdi, fdo);
+ rc = (rc <= 0) ? EXIT_FAILURE : EXIT_SUCCESS;
+ Fclose(fdo);
+
+ Fclose(gzdi); /* XXX gzdi == fdi */
- return 0;
+ return rc;
}