summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorjbj <devnull@localhost>2002-02-09 20:08:32 +0000
committerjbj <devnull@localhost>2002-02-09 20:08:32 +0000
commit65d033ec86e8b0322f7016dd7a1cc91aa4ec32f2 (patch)
treea1b02d49814d11aef762082de2e3cb9ffdb80d75 /examples
parent8ddeef05cd90c5b381d2ebea6b6b35a3000b553c (diff)
downloadrpm-65d033ec86e8b0322f7016dd7a1cc91aa4ec32f2.tar.gz
rpm-65d033ec86e8b0322f7016dd7a1cc91aa4ec32f2.tar.bz2
rpm-65d033ec86e8b0322f7016dd7a1cc91aa4ec32f2.zip
Update to rpm-4.0.x API.
CVS patchset: 5307 CVS date: 2002/02/09 20:08:32
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile30
-rw-r--r--examples/dumprpm.c172
-rw-r--r--examples/showdb.c81
-rw-r--r--examples/showdb2.c80
4 files changed, 201 insertions, 162 deletions
diff --git a/examples/Makefile b/examples/Makefile
index 514f23789..14dc15717 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -5,17 +5,33 @@
# (or whatever database library rpm was compiled with).
+CFLAGS= -O2 -Wall -I/usr/include/rpm
+VER = 4.0.4
+
+LDADD= -lrpm-$(VER) -lrpmdb-$(VER) -lrpmio-$(VER) -lpopt -lz
+
+COMPILE = $(CC) $(CFLAGS)
+LINK = $(CC)
+
all: dumprpm showdb showdb2
-dumprpm: dumprpm.c
- gcc -Wall dumprpm.c -lrpm -lintl -lgz -o dumprpm
+dumprpm: dumprpm.o
+ $(LINK) -o $@ $< $(LDADD)
-showdb: showdb.c
- gcc -Wall showdb.c -lrpm -lintl -ldb1 -lgz -o showdb
+showdb: showdb.o
+ $(LINK) -o $@ $< $(LDADD)
-showdb2: showdb2.c
- gcc -Wall showdb2.c -lrpm -lintl -ldb1 -lgz -o showdb2
+showdb2: showdb2.o
+ $(LINK) -o $@ $< $(LDADD)
clean:
- rm -f dumprpm showdb showdb2
+ rm -f *.o *.lo dumprpm showdb showdb2
+
+dumprpm.o: dumprpm.c
+ $(COMPILE) -o $@ -c dumprpm.c
+
+showdb.o: showdb.c
+ $(COMPILE) -o $@ -c showdb.c
+showdb2.o: showdb2.c
+ $(COMPILE) -o $@ -c showdb2.c
diff --git a/examples/dumprpm.c b/examples/dumprpm.c
index 637b7ad81..266994119 100644
--- a/examples/dumprpm.c
+++ b/examples/dumprpm.c
@@ -10,96 +10,100 @@
#include <string.h>
#include <stdlib.h>
-#include <rpm/rpmlib.h>
-
+#include <rpmlib.h>
+
+#ifndef HEADER_DUMP_INLINE
+/**
+ * Dump a header in human readable format (for debugging).
+ * @param h header
+ * @param flags 0 or HEADER_DUMP_INLINE
+ * @param tags array of tag name/value pairs
+ */
+void headerDump(Header h, FILE *f, int flags,
+ const struct headerTagTableEntry_s * tags)
+ /*@globals fileSystem @*/
+ /*@modifies f, fileSystem @*/;
+#define HEADER_DUMP_INLINE 1
+#endif
+
+static const struct headerTagTableEntry_s rpmSigTagTbl[] = {
+ { "RPMSIGTAG_SIZE", RPMSIGTAG_SIZE, },
+ { "RPMSIGTAG_PGP", RPMSIGTAG_PGP, },
+ { "RPMSIGTAG_MD5", RPMSIGTAG_MD5, },
+ { "RPMSIGTAG_GPG", RPMSIGTAG_GPG, },
+ { NULL, 0 }
+};
int main( int argc, char **argv )
{
- HeaderIterator iter;
- Header h, sig;
- int_32 itertag, type, count;
- void *p = NULL;
- char *name;
- FD_t fd;
- int stat;
-
- if( argc == 1 ) {
- fd = fdDup( STDIN_FILENO );
- } else {
- fd = fdOpen( argv[1], O_RDONLY, 0644 );
- }
-
- if( !fdValid(fd) ) {
- perror( "open" );
- exit( 1 );
- }
-
- stat = rpmReadPackageInfo( fd, &sig, &h );
- if( stat ) {
- fprintf( stderr, "rpmReadPackageInfo error status: %d\n%s\n",
- stat, strerror(errno) );
- exit( stat );
- }
-
- headerGetEntry( h, RPMTAG_NAME, &type, (void**)&name, &count );
-
- if( headerIsEntry(h,RPMTAG_PREIN) ) {
- printf( "There is a preinstall script for %s\n", name );
- }
- if( headerIsEntry(h,RPMTAG_POSTIN) ) {
- printf( "There is a postinstall script for %s\n", name );
- }
-
-
- /* NOTE:
- * This is actually rather a ridiculous thing to do, since headerDump
- * will incorrectly assume header types (RPMTAG_NAME, RPMTAG_RELEASE,
- * RPMTAG_SUMMARY). Since we're passing a signature, the correct types
- * would be (RPMSIGTAG_SIZE, RPMSIGTAG_PGP, and RPMSIGTAG_MD5).
- * TO FIX:
- * I think rpm's tagtable.c should define an rpmSigTable global var.
- * This is the perfect dual to rpmTagTable and would be passed to
- * headerDump when appropriate. It looks like someone intended to do
- * this at one time, but never finished it?
- */
-
- printf( "Dumping signatures...\n" );
- /* Use HEADER_DUMP_INLINE to include inline dumps of header items */
- headerDump( sig, stdout, HEADER_DUMP_INLINE, rpmTagTable );
-
- rpmFreeSignature( sig );
-
- printf( "Iterating through the header...\n" );
- iter = headerInitIterator( h );
- while( headerNextIterator( iter, &itertag, &type, &p, &count ) ) {
- /* printf( "itertag=%04d, type=%08lX, p=%08lX, c=%08lX\n",
- (int)itertag, (long)type, (long)p, (long)count ); */
-
- switch( itertag ) {
- case RPMTAG_SUMMARY:
- if( type == RPM_I18NSTRING_TYPE ) {
- /* We'll only print out the first string if there's an array */
- printf( "The Summary: \"%s\"\n", *(char**)p );
- }
- if( type == RPM_STRING_TYPE ) {
- printf( "The Summary: \"%s\"\n", (char*)p );
- }
- break;
- case RPMTAG_FILENAMES:
- printf( "There are %d files in %s\n", count, name );
- break;
+ HeaderIterator hi;
+ Header h, sig;
+ int_32 tag, type, count;
+ void * p = NULL;
+ char * name;
+ FD_t fd;
+ int rc;
+
+ if( argc == 1 )
+ fd = Fopen("-", "r.ufdio" );
+ else
+ fd = Fopen( argv[1], "r.ufdio" );
+
+ if( fd == NULL || Ferror(fd) ) {
+ fprintf(stderr, "cannot open %s: %s\n",
+ (argc == 1 ? "<stdin>" : argv[1]), Fstrerror(fd));
+ exit(EXIT_FAILURE);
+ }
+
+ rc = rpmReadPackageInfo( fd, &sig, &h );
+ if ( rc ) {
+ fprintf( stderr, "rpmReadPackageInfo error status: %d\n\n", rc );
+ exit(EXIT_FAILURE);
+ }
+
+ headerGetEntry( h, RPMTAG_NAME, &type, (void**)&name, &count );
+
+ if( headerIsEntry(h,RPMTAG_PREIN) ) {
+ printf( "There is a preinstall script for %s\n", name );
+ }
+ if( headerIsEntry(h,RPMTAG_POSTIN) ) {
+ printf( "There is a postinstall script for %s\n", name );
+ }
+
+
+ printf( "Dumping signatures...\n" );
+ /* Use HEADER_DUMP_INLINE to include inline dumps of header items */
+ headerDump( sig, stdout, HEADER_DUMP_INLINE, rpmSigTagTbl );
+
+ rpmFreeSignature( sig );
+
+ printf( "Iterating through the header...\n" );
+ hi = headerInitIterator( h );
+ while( headerNextIterator( hi, &tag, &type, &p, &count ) ) {
+ /* printf( "tag=%04d, type=%08lX, p=%08lX, c=%08lX\n",
+ (int)tag, (long)type, (long)p, (long)count ); */
+
+ switch( tag ) {
+ case RPMTAG_SUMMARY:
+ if( type == RPM_I18NSTRING_TYPE ) {
+ /* We'll only print out the first string if there's an array */
+ printf( "The Summary: \"%s\"\n", *(char**)p );
}
-
- /* rpmlib allocates a buffer to return these two values... */
- if( type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE ) {
- free( p );
+ if( type == RPM_STRING_TYPE ) {
+ printf( "The Summary: \"%s\"\n", (char*)p );
}
+ break;
+ case RPMTAG_BASENAMES:
+ printf( "There are %d files in %s\n", count, name );
+ break;
}
- headerFreeIterator( iter );
- headerFree( h );
-
- return 0;
-}
+ /* rpmlib allocates a buffer to return these two values... */
+ headerFreeData(p, type);
+ }
+ headerFreeIterator( hi );
+ headerFree( h );
+ return 0;
+}
diff --git a/examples/showdb.c b/examples/showdb.c
index def1ff25b..d77befd78 100644
--- a/examples/showdb.c
+++ b/examples/showdb.c
@@ -15,45 +15,54 @@
#include <unistd.h>
#include <stdlib.h>
-#include <rpm/rpmlib.h>
+#include <rpmlib.h>
+#ifndef HEADER_DUMP_INLINE
+/**
+ * Dump a header in human readable format (for debugging).
+ * @param h header
+ * @param flags 0 or HEADER_DUMP_INLINE
+ * @param tags array of tag name/value pairs
+ */
+void headerDump(Header h, FILE *f, int flags,
+ const struct headerTagTableEntry_s * tags)
+ /*@globals fileSystem @*/
+ /*@modifies f, fileSystem @*/;
+#define HEADER_DUMP_INLINE 1
+#endif
int main( int argc, char **argv )
{
- Header h;
- int offset;
- int_32 type, count;
- char *name;
- rpmdb db;
-
- rpmReadConfigFiles( NULL, NULL );
-
- printf( "Calling rpmdbOpen\n" );
- if( rpmdbOpen( "", &db, O_RDONLY, 0644 ) != 0 ) {
- fprintf( stderr, "cannot open database!\n" );
- exit( 1 );
- }
- printf( "rpmdbOpen done.\n" );
-
- offset = rpmdbFirstRecNum( db );
- while( offset ) {
- h = rpmdbGetRecord( db, offset );
- if( !h ) {
- fprintf( stderr, "Header Read Failed!\n" );
- exit( 1 );
- }
-
- headerGetEntry( h, RPMTAG_NAME, &type, (void**)&name, &count );
- if( strcmp(name,argv[1]) == 0 ) {
- headerDump( h, stdout, 1, rpmTagTable );
- }
-
- headerFree( h );
- offset = rpmdbNextRecNum( db, offset );
- }
-
- rpmdbClose( db );
-
- return 0;
+ rpmdbMatchIterator mi;
+ Header h;
+ int_32 type, count;
+ char *name;
+ rpmdb db;
+
+ rpmReadConfigFiles( NULL, NULL );
+
+ if( rpmdbOpen( "", &db, O_RDONLY, 0644 ) != 0 ) {
+ fprintf( stderr, "cannot open database!\n" );
+ exit(EXIT_FAILURE);
+ }
+
+ mi = rpmdbInitIterator(db, RPMDBI_PACKAGES, NULL, 0);
+ while ((h = rpmdbNextIterator(mi)) != NULL) {
+
+ headerGetEntry( h, RPMTAG_NAME, &type, (void**)&name, &count );
+ if( strcmp(name,argv[1]) == 0 )
+ headerDump( h, stdout, HEADER_DUMP_INLINE, rpmTagTable );
+
+ /*
+ * Note that the header reference is "owned" by the iterator,
+ * so no headerFree() is necessary.
+ */
+
+ }
+ mi = rpmdbFreeIterator(mi);
+
+ rpmdbClose( db );
+
+ return 0;
}
diff --git a/examples/showdb2.c b/examples/showdb2.c
index 111d4f95c..916d3c42c 100644
--- a/examples/showdb2.c
+++ b/examples/showdb2.c
@@ -15,44 +15,54 @@
#include <unistd.h>
#include <stdlib.h>
-#include <rpm/rpmlib.h>
+#include <rpmlib.h>
+#ifndef HEADER_DUMP_INLINE
+/**
+ * Dump a header in human readable format (for debugging).
+ * @param h header
+ * @param flags 0 or HEADER_DUMP_INLINE
+ * @param tags array of tag name/value pairs
+ */
+void headerDump(Header h, FILE *f, int flags,
+ const struct headerTagTableEntry_s * tags)
+ /*@globals fileSystem @*/
+ /*@modifies f, fileSystem @*/;
+#define HEADER_DUMP_INLINE 1
+#endif
int main( int argc, char **argv )
{
- Header h;
- int stat;
- rpmdb db;
- dbiIndexSet matches;
-
- if( argc != 2 ) {
- fprintf( stderr, "usage: showdb2 <search term>\n" );
- exit( 1 );
- }
-
- rpmReadConfigFiles( NULL, NULL );
-
- if( rpmdbOpen( "", &db, O_RDONLY, 0644 ) != 0 ) {
- fprintf( stderr, "cannot open RPM database.\n" );
- exit( 1 );
- }
-
- stat = rpmdbFindPackage( db, argv[1], &matches );
- printf( "Status is: %d\n", stat );
- if( stat == 0 ) {
- if( matches.count ) {
- printf( "Number of matches: %d\n", matches.count );
- h = rpmdbGetRecord( db, matches.recs[0].recOffset );
- if( h ) {
- headerDump( h, stdout, HEADER_DUMP_INLINE, rpmTagTable );
- headerFree( h );
- }
- }
- dbiFreeIndexRecord( matches );
- }
-
- rpmdbClose( db );
-
- return 0;
+ rpmdbMatchIterator mi;
+ Header h;
+ rpmdb db;
+
+ if( argc != 2 ) {
+ fprintf( stderr, "usage: showdb2 <search term>\n" );
+ exit(EXIT_FAILURE);
+ }
+
+ rpmReadConfigFiles( NULL, NULL );
+
+ if( rpmdbOpen( "", &db, O_RDONLY, 0644 ) != 0 ) {
+ fprintf( stderr, "cannot open RPM database.\n" );
+ exit( 1 );
+ }
+
+ mi = rpmdbInitIterator(db, RPMTAG_BASENAMES, argv[1], 0);
+ while ((h = rpmdbNextIterator(mi)) != NULL) {
+
+ headerDump( h, stdout, HEADER_DUMP_INLINE, rpmTagTable );
+
+ /*
+ * Note that the header reference is "owned" by the iterator,
+ * so no headerFree() is necessary.
+ */
+ }
+ mi = rpmdbFreeIterator(mi);
+
+ rpmdbClose( db );
+
+ return 0;
}