Library for querying installed (Debian) packages in C?

馋奶兔 提交于 2019-12-11 07:35:25

问题


I need to check whether certain Debian packages are installed on a system in my C program. I could use external shell scripts which do grep magic of apt-cache output with system(), but it seems a bit inelegant and hackish, as well as the fact that it won't work if the user's installed language is different. Is there a C library I can hook into to query package installations?


回答1:


I don't think you'll find a shipped library that meets your criteria, however, the dpkg program internally does exactly what you are describing, and does not link against the C++ libs (or libstdc++ at all):

tpost@tpost-desktop:~$ ldd -v /usr/bin/dpkg
        linux-gate.so.1 =>  (0x00f33000)
        libpthread.so.0 => /lib/tls/i686/cmov/libpthread.so.0 (0x00dfb000)
        libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x0077a000)
        /lib/ld-linux.so.2 (0x006a6000)

If you apt-get source dpkg , I think you'll find the code that you need in lib/dpkg within the source tree, in particular database.c and parse.c.

The trick, of course is extracting just what you need from it. Also, the GPL may or may not agree with your project at hand, but at least it is an implementation to study.

Example of dpkg reading the database can be seen via dpkg -l | grep ii, for instance, to see all installed packages. It sounds like you just need to get that information into an elegant array or list, and I think you'll find inspiration within dpkg on just how to do that.

If you end up writing your own library (or wrapper around the bits in dpkg) please put it somewhere that other people can find it. The need you have is a recurring one that many people share.



来源:https://stackoverflow.com/questions/4115946/library-for-querying-installed-debian-packages-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!