rpm/rpm2cpio.c

95 lines
2.2 KiB
C
Raw Normal View History

/* rpmarchive: spit out the main archive portion of a package */
#include "system.h"
#include <rpm/rpmlib.h> /* rpmReadPackageFile .. */
#include <rpm/rpmtag.h>
#include <rpm/rpmio.h>
#include <rpm/rpmpgp.h>
#include <rpm/rpmts.h>
#include "debug.h"
2007-09-18 11:53:37 +08:00
int main(int argc, char *argv[])
{
FD_t fdi, fdo;
Header h;
char * rpmio_flags = NULL;
int rc;
off_t payload_size;
FD_t gzdi;
Reimplement setprogname() and getprogname() to be generic and portable The RPM code contains setprogname()/getprogname() support implemented through compatiblity layer with very old GLIBC (internals supported back to '95 and earlier), before stabilization of the GNU C library. This compatiblity layer (__progname, __assert_progname, setprogname()) is supposed to support well archaic GLIBC, but on the other hand it pollutes the library namespace and introduces unpredicable compillation errors on BSD systems. The functions setprogname() and getprogname() are natively supported in NetBSD and work the same way as __progname from the GNU C library (they are even implemented in the same way - but with a slightly changed logic). The support for very old (20 years and older) GNU C Library is obfuscating the code, because it uses defines over defines without a word of explaination why to do so. It's important to note that the setprogname()/getprogname() was inconstiently implemented in the codebase, duplicating the code and/or functionality. Add new generic functions getprogname() and setprogname() and bind it to: - the current and for two decades stable GNU LIB C implementation, - the current NetBSD implementation (introduces to NetBSD in 2002), - fallback reimplementation functions of the setprogname() and getprogname() functionality for other systems. Don't support anymore old GNU Lib C internals and don't support older NetBSD systems, as they aren't supported for many years. Add to the codebase comments explaining the relevant codeparts.
2013-08-18 03:50:41 +08:00
xsetprogname(argv[0]); /* Portability call -- see system.h */
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);
rc = rpmReadPackageFile(ts, fdi, "rpm2cpio", &h);
ts = rpmtsFree(ts);
}
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(EXIT_FAILURE);
break;
case RPMRC_FAIL:
default:
fprintf(stderr, _("error reading header from package\n"));
exit(EXIT_FAILURE);
break;
}
/* Retrieve payload size and compression type. */
{ const char *compr = headerGetString(h, RPMTAG_PAYLOADCOMPRESSOR);
rpmio_flags = rstrscat(NULL, "r.", compr ? compr : "gzip", NULL);
payload_size = headerGetNumber(h, RPMTAG_LONGARCHIVESIZE);
}
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);
}
rc = (ufdCopy(gzdi, fdo) == payload_size) ? EXIT_SUCCESS : EXIT_FAILURE;
Fclose(fdo);
Fclose(gzdi); /* XXX gzdi == fdi */
return rc;
}