How to use libapt (or libept) in debian-like system to list packages and get their infos?

*爱你&永不变心* 提交于 2019-11-30 01:38:59

问题


Somebody used libapt or libept to list packages and get informations about package in a debian-like system?

Libapt is not well-documented at all, and i've found few examples and tutorials about libept. Can someone explain me best methods to

  1. get a list of every packages in the apt-system
  2. get informations about single packages (like name, version, dependences, description, etc.
  3. get list of files installed by a single package

Work directly with apt internal files is quite simple, but i want to use a library to respect apt specifications.


回答1:


In debian there is a package called libapt-pkg-doc which contains some documentation (like an API reference). Once installed, you can access it at file:///usr/share/doc/libapt-pkg-doc/html/index.xhtml.

I only just had a look at libapt and here is what I have learned so far:

How to list all packages:

#include <apt-pkg/cachefile.h>
#include <apt-pkg/pkgcache.h>

int main() {
    // _config and _system are defined in the libapt header files
    pkgInitConfig(*_config);
    pkgInitSystem(*_config, _system);

    pkgCacheFile cache_file;
    pkgCache* cache = cache_file.GetPkgCache();

    for (pkgCache::PkgIterator package = cache->PkgBegin(); !package.end(); package++) {
        std::cout << package.Name() << std::endl;
    }

    return 0;
}



回答2:


Take a look at how apt-cache(8) is implemented. Obtaining the source with apt is easy:

# apt-get source apt

In the source file cmdline/apt-cache.cc theres is a function called DumpPackage() which extracts information from a named file in the cache.




回答3:


There is also DPKG::Parse from CPAN if you are using perl.




回答4:


Just to mention, there is wpkg which has the ability to read Debian packages and give you all the information. However, it does not know anything about the apt or dpkg databases. It will be capable of reading a .deb and you can extract all the information and the files with it, all in C++.



来源:https://stackoverflow.com/questions/341520/how-to-use-libapt-or-libept-in-debian-like-system-to-list-packages-and-get-the

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