⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0326-0329.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:




<HTML>

<HEAD>

<TITLE>Maximum RPM (RPM):A Guide to the RPM Library API:EarthWeb Inc.-</TITLE>

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!-- ISBN=0672311054 //-->

<!-- TITLE=Maximum RPM (RPM)//-->

<!-- AUTHOR=Edward Bailey//-->

<!-- PUBLISHER=Macmillan Computer Publishing//-->

<!-- IMPRINT=Sams//-->

<!-- CHAPTER=21 //-->

<!-- PAGES=0305-0336 //-->

<!-- UNASSIGNED1 //-->

<!-- UNASSIGNED2 //-->









<P><CENTER>

<a href="0322-0325.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0330-0333.html">Next</A>

</CENTER></P>



<!-- END CODE //--><A NAME="PAGENUM-326"><P>Page 326</P></A>



<H4>

21.2.14. Header Entry Manipulation

</H4>



<P>The functions in this section provide the basic operations necessary to manipulate header

entries. The following header entry types are currently defined:

</P>



<UL>

<LI>     

RPM_NULL_TYPE&#151;This type is not used.

<LI>     

RPM_CHAR_TYPE&#151;The entry contains a single character.

<LI>     

RPM_INT8_TYPE&#151;The entry contains an 8-bit integer.

<LI>     

RPM_INT16_TYPE&#151;The entry contains a 16-bit integer.

<LI>     

RPM_INT32_TYPE&#151;The entry contains a 32-bit integer.

<LI>     

RPM_INT64_TYPE&#151;The entry contains a 64-bit integer.

<LI>     

RPM_STRING_TYPE&#151;The entry contains a null-terminated character string.

<LI>     

RPM_BIN_TYPE&#151;The entry contains binary data that will not be interpreted by rpmlib.

<LI>     

RPM_STRING_ARRAY_TYPE&#151;The entry contains an array of null-terminated strings.

</UL>



<H4><A NAME="ch21_ 66">

21.2.14.1. headerGetEntry()&#151;Get Entry from Header

</A></H4>



<P>This function retrieves the entry matching tag from header

h:

</P>



<!-- CODE //-->

<PRE>

#include &lt;rpm/rpmlib.h&gt;

#include &lt;rpm/header.h&gt;



int headerGetEntry(Header h,

                   int_32 tag,

                   int_32 *type,

                  void **p,

                   int_32 *c);

</PRE>

<!-- END CODE //-->



<P>The type of the entry is returned in type, a pointer to the data is returned in

p, and the size of the data is returned in c. Both

type and c may be NULL, in which case that data will not

be returned. Note that if the entry type is

RPM_STRING_ARRAY_TYPE, you must issue a free() on

p when done with the data.

</P>



<P>This function returns 1 on success, and 0 on failure.

</P>



<H4><A NAME="ch21_ 67">

21.2.14.2. headerAddEntry()&#151;Add Entry to Header

</A></H4>



<P>This function adds a new entry to the header h:

</P>



<!-- CODE //-->

<PRE>

#include &lt;rpm/rpmlib.h&gt;

#include &lt;rpm/header.h&gt;



int headerAddEntry(Header h,

                   int_32 tag,

                   int_32 type,

                  void *p,

                   int_32 c);

</PRE>

<!-- END CODE //-->



<P>The entry's tag is specified by the tag parameter, and the entry's type is specified by the

type parameter.

</P>



<A NAME="PAGENUM-327"><P>Page 327</P></A>







<P>The entry's data is pointed to by p, and the size of the data is specified by

c. This function always returns 1.

</P>

<P>



<CENTER>

<TABLE BGCOLOR="#FFFF99">

<TR><TD><B>

NOTE

</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

In versions of RPM prior to 2.3.3,

headerAddEntry() will only work successfully with headers produced by

headerCopy() and headerNew(). In particular,

headerAddEntry() is not supported when used to add entries to a header

produced by headerRead(). Later versions of RPM lift this restriction.

</BLOCKQUOTE></TD></TR>

</TABLE></CENTER>

</P>

<H4><A NAME="ch21_ 68">

21.2.14.3. headerIsEntry()&#151;Determine if Entry Is in Header

</A></H4>



<P>This function returns 1 if an entry with tag

tag is present in header h. If the tag is not

present, this function returns 0:

</P>

<!-- CODE SNIP //-->

<PRE>

#include &lt;rpm/rpmlib.h&gt;

#include &lt;rpm/header.h&gt;



int headerIsEntry(Header h,

                  int_32 tag);

</PRE>

<!-- END CODE SNIP //-->



<H4><A NAME="ch21_ 69">

21.2.15. Header Iterator Support

</A></H4>



<P>Iterators are used as a means to step from entry to entry, through an entire header. The

functions in this section are used to create, use, and free iterators.

</P>



<H4><A NAME="ch21_ 70">

21.2.15.1. headerInitIterator()&#151;Create an Iterator

</A></H4>



<P>This function returns a newly created iterator for the header

h:

</P>



<!-- CODE SNIP //-->

<PRE>

#include &lt;rpm/rpmlib.h&gt;

#include &lt;rpm/header.h&gt;



HeaderIterator headerInitIterator(Header h);

</PRE>

<!-- END CODE SNIP //-->



<H4><A NAME="ch21_ 71">

21.2.15.2. headerNextIterator()&#151;Step to the Next Entry

</A></H4>



<P>This function steps to the next entry in the header specified when the iterator

iter was created with headerInitIterator():

</P>

<!-- CODE //-->

<PRE>

#include &lt;rpm/rpmlib.h&gt;

#include &lt;rpm/header.h&gt;



int headerNextIterator(HeaderIterator iter,

                       int_32 *tag,

                       int_32 *type,

                      void **p,

                       int_32 *c);



</PRE>

<!-- END CODE //--><A NAME="PAGENUM-328"><P>Page 328</P></A>



<P>The next entry's tag, type, data, and size are returned in

tag, type, p, and c, respectively. Note that if the entry type is

RPM_STRING_ARRAY_TYPE, you must issue a free() on

p when done with the data.

</P>



<P>This function returns 1 if successful, and 0 if there are no more entries in the header.

</P>



<H4>

21.2.15.3. headerFreeIterator()&#151;Free an Iterator

</H4>



<P>This function frees the resources used by the iterator

iter:

</P>



<!-- CODE SNIP //-->

<PRE>

#include &lt;rpm/rpmlib.h&gt;

#include &lt;rpm/header.h&gt;



void    headerFreeIterator(HeaderIterator iter);

</PRE>

<!-- END CODE SNIP //-->



<H3><A NAME="ch21_ 72">

21.3. Sample Code

</A></H3>



<P>In this section, we'll study sample programs that make use of rpmlib to perform an

assortment of commonly required operations. We'll intersperse sections of code with a running

commentary to minimize page turning.

</P>



<H4><A NAME="ch21_ 73">

21.3.1. Example #1

</A></H4>



<P>In this example, we'll use a number of rpmlib's header-manipulation functions.

</P>



<P>Here we've included rpmlib.h, which is necessary for all programs that use rpmlib:

</P>



<!-- CODE //-->

<PRE>

#include &lt;errno.h&gt;

#include &lt;fcntl.h&gt;

#include &lt;stdio.h&gt;

#include &lt;unistd.h&gt;

#include &lt;string.h&gt;



#include &lt;rpm/rpmlib.h&gt;

</PRE>

<!-- END CODE //-->



<P>Here we've defined the program's storage. Note in particular the

HeaderIterator, Header, and int_32 data types:

</P>



<!-- CODE //-->

<PRE>

void main(int argc, char ** argv)

{

  HeaderIterator iter;

    Header h, sig;

     int_32 itertag, type, count;

   void **p = NULL;

     char *blather;

    char * name;



    int fd, stat;



</PRE>

<!-- END CODE //--><A NAME="PAGENUM-329"><P>Page 329</P></A>





<P>Standard stuff here. The first argument is supposed to be an RPM package file. It is

opened here. If there is no argument on the command line, the program will use

stdin instead:

</P>



<!-- CODE //-->

<PRE>

if (argc == 1) {

    fd = 0;

} else {

    fd = open(argv[1], O_RDONLY, 0644);

}



if (fd &lt; 0) {

    perror(&quot;open&quot;);

    exit(1);

}

</PRE>

<!-- END CODE //-->



<P>Here things start to get interesting! The signature and headers are read from the package

file that was just opened. Notice in the preceding code that we've defined

sig and h to be of type Header. That means we can use rpmlib's header-related functions on them. After a little bit

of error checking, it's time to move on&#133;.

</P>



<!-- CODE //-->

<PRE>

stat = rpmReadPackageInfo(fd, &amp;sig, &amp;h);

if (stat) {

  fprintf(stderr,

          &quot;rpmReadPackageInfo error status: %d\n%s\n&quot;,

          stat, strerror(errno));

    exit(stat);

}

</PRE>

<!-- END CODE //-->



<P>Now that we have the package's header, we get the package name (specified by the

RPMTAG_NAME in the call to headerGetEntry). Next, we see if the package has preinstall

(RPMTAG_PREIN) or postinstall (RPMTAG_POSTIN) scripts. If it does, we print out a message, along with the

package name:

</P>



<!-- CODE //-->

<PRE>

headerGetEntry(h, RPMTAG_NAME, &amp;type, (void **) &amp;name, &amp;count);



if (headerIsEntry(h, RPMTAG_PREIN)) {

    printf(&quot;There is a preinstall script for %s\n&quot;, name);

}



if (headerIsEntry(h, RPMTAG_POSTIN)) {

    printf(&quot;There is a postinstall script for %s\n&quot;, name);

}

</PRE>

<!-- END CODE //-->



<P>Turning to the other Header structure we've read, we print out the package's signatures in

human-readable form. When we're done, we free the block of signatures:

</P>



<!-- CODE SNIP //-->

<PRE>

printf(&quot;Dumping signatures...\n&quot;);

headerDump(sig, stdout, 1);



rpmFreeSignature(sig);

</PRE>

<!-- END CODE SNIP //-->



<P>Here we set up an iterator for the package's header. This will allow us to step through

each entry in the header:

</P>



<!-- CODE SNIP //-->

<PRE>

printf(&quot;Iterating through the header...\n&quot;);



iter = headerInitIterator(h);

</PRE>

<!-- END CODE SNIP //-->





<P><CENTER>

<a href="0322-0325.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0330-0333.html">Next</A>

</CENTER></P>











</td>
</tr>
</table>

<!-- begin footer information -->







</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -