You are not logged in.

#1 2007-08-12 20:38:11

b100dian
Member
Registered: 2007-06-09
Posts: 20

Pacman's libalpm documentation

Hello,

I hear lots 'bout pacman 3+ delivered with a separate library for package management.
The best docs I could find on the 'net' for arch linux package management was this: http://code.toofishes.net/pacman/doc/modules.html
And it doesn't sound (the URL) that official.

Is there any other documentation written? a short description of how pacman works, what calls to library in what order are made, in place of a "tutorial" would be GREAT.

I am (still) a GTK beginner but I would want to try writing another frontend:)

Thanks,
Vlad

Offline

#2 2007-08-12 21:13:19

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: Pacman's libalpm documentation

Unfortunately, documentation doesn't write itself, it requires people writing it smile
At least, you have a reference implementation : pacman.
It'll give you an example how to use the library, if you can take the time to understand it.


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#3 2007-08-12 22:37:44

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Pacman's libalpm documentation

code.toofishes.net is the webpage of one of pacman's developers - anything you find there is probably pretty decent.

Offline

#4 2007-08-13 06:25:50

b100dian
Member
Registered: 2007-06-09
Posts: 20

Re: Pacman's libalpm documentation

shining wrote:

Unfortunately, documentation doesn't write itself, it requires people writing it

I understand that, but it worth a try asking, isn't it?

Cerebral wrote:

code.toofishes.net is the webpage of one of pacman's developers

Good to know!

Ok, so i'll first start poking around with pacman's source!
Thanks

Offline

#5 2009-05-31 12:24:29

Rulatir
Banned
Registered: 2007-02-05
Posts: 94

Re: Pacman's libalpm documentation

Shining, would it really take that much of your precious time if you posted a code snippet showing how to initialize the library, "mount"/"register"/"load"/"open" (or whatever the term is) a database, get a package by name, and enumerate its dependencies? 15 min. at most, I believe.

Offline

#6 2009-05-31 12:31:47

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,672
Website

Re: Pacman's libalpm documentation

The reference implementation is pacman. Why write another example if you are ignoring the one that is already there...

Offline

#7 2009-06-09 13:35:48

drf
Member
From: Milano, Italy
Registered: 2008-01-13
Posts: 113

Re: Pacman's libalpm documentation

And have a look at aqpm, which is even easier to read since it wraps alpm by functionality

Offline

#8 2009-07-08 07:10:42

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: Pacman's libalpm documentation

There are three small standalone tool in src/util of pacman source :
http://projects.archlinux.org/?p=pacman … il;hb=HEAD

One I wrote earlier to sort pkg by size :

/*
 *  sortpkg.c : Display the locally installed packages sorted by size.
 *
 */

#include <stdio.h>

#include <alpm.h>
#include <alpm_list.h>

int pkg_cmp(const void *v1, const void *v2)
{
    pmpkg_t *p1 = (pmpkg_t *)v1;
    pmpkg_t *p2 = (pmpkg_t *)v2;
    unsigned long int s1 = alpm_pkg_get_size(p1);
    unsigned long int s2 = alpm_pkg_get_size(p2);
    return(s2 - s1);
}

int main(int argc, char **argv)
{
    pmdb_t *db = NULL;
    alpm_list_t *i;

    if(alpm_initialize() == -1) {
        fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(pm_errno));
        exit(EXIT_FAILURE);
    }

    alpm_option_set_dbpath("/var/lib/pacman");

    db = alpm_db_register_local();
    if(db == NULL) {
        fprintf(stderr, "error: could not register 'local' database (%s)\n",
                alpm_strerror(pm_errno));
        exit(EXIT_FAILURE);
    }

    alpm_list_t *list = alpm_db_getpkgcache(db);
    list = alpm_list_msort(list, alpm_list_count(list), pkg_cmp);

    for(i = list; i; i = alpm_list_next(i)) {
        pmpkg_t *pkg = alpm_list_getdata(i);
        printf("%s %lu\n", alpm_pkg_get_name(pkg), alpm_pkg_get_size(pkg)/1024);
    }

    if(alpm_release() == -1) {
        fprintf(stderr, "error releasing alpm: %s\n", alpm_strerror(pm_errno));
    }

    exit(EXIT_SUCCESS);
}

/* vim: set ts=2 sw=2 noet: */

The one Rulatir asked for (only with the local database, but only the "register" part is different with sync ones)

#include <stdio.h>

#include <alpm.h>
#include <alpm_list.h>

void displaydeps(pmpkg_t *pkg) {
    alpm_list_t *i;

    for(i = alpm_pkg_get_depends(pkg); i; i = alpm_list_next(i)) {
        pmdepend_t *dep = alpm_list_getdata(i);
        char *depstring = alpm_dep_get_string(dep);
        printf("%s \n", depstring);
    }
}

int main(int argc, char **argv)
{
    pmdb_t *db = NULL;
    alpm_list_t *i;

    if(argc != 2) {
        printf("usage : displaydeps <pkg>\n");
        exit(EXIT_FAILURE);
    }

    if(alpm_initialize() == -1) {
        fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(pm_errno));
        exit(EXIT_FAILURE);
    }

    alpm_option_set_dbpath("/var/lib/pacman");

    db = alpm_db_register_local();
    if(db == NULL) {
        fprintf(stderr, "error: could not register 'local' database (%s)\n",
                alpm_strerror(pm_errno));
        exit(EXIT_FAILURE);
    }

    /* if the package exists, list its dependencies */
    pmpkg_t *pkg = alpm_db_get_pkg(db, argv[1]);
    if(pkg) {
        displaydeps(pkg);
    } else {
        /* otherwise, search for matching packages */
        fprintf(stderr, "error: package %s does not exist\n", argv[1]);
        alpm_list_t *targets = alpm_list_add(NULL, argv[1]);
        alpm_list_t *res = alpm_db_search(db, targets);

        fprintf(stderr, "matching packages :\n");
        for(i = res; i; i = alpm_list_next(i)) {
            pmpkg_t *pkg = alpm_list_getdata(i);
            printf("%s\n", alpm_pkg_get_name(pkg));
        }
    }


    if(alpm_release() == -1) {
        fprintf(stderr, "error releasing alpm: %s\n", alpm_strerror(pm_errno));
    }

    exit(EXIT_SUCCESS);
}

/* vim: set ts=2 sw=2 noet: */

pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#9 2011-09-14 08:48:12

zester
Member
From: Seattle Wa
Registered: 2011-08-13
Posts: 156

Re: Pacman's libalpm documentation

Not to be a necromancer or anything but I fixed your examples to work with the current libalpm api

/*
 *  sortpkg.c : Display the locally installed packages sorted by size.
 *
 */

#include <stdio.h>

#include <alpm.h>
#include <alpm_list.h>

int pkg_cmp(const void *v1, const void *v2)
{
    pmpkg_t *p1 = (pmpkg_t *)v1;
    pmpkg_t *p2 = (pmpkg_t *)v2;
    unsigned long int s1 = alpm_pkg_get_size(p1);
    unsigned long int s2 = alpm_pkg_get_size(p2);
    return(s2 - s1);
}

int main(int argc, char **argv)
{
    pmdb_t *db = NULL;
    alpm_list_t *i;

    if(alpm_initialize() == -1) {
        fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(pm_errno));
        exit(EXIT_FAILURE);
    }

    alpm_option_set_dbpath("/var/lib/pacman");

    db = alpm_option_get_localdb();
    if(db == NULL) {
        fprintf(stderr, "error: could not register 'local' database (%s)\n",
                alpm_strerror(pm_errno));
        exit(EXIT_FAILURE);
    }

    alpm_list_t *list = alpm_db_get_pkgcache(db);
    list = alpm_list_msort(list, alpm_list_count(list), pkg_cmp);

    for(i = list; i; i = alpm_list_next(i)) {
        pmpkg_t *pkg = alpm_list_getdata(i);
        printf("%s %lu\n", alpm_pkg_get_name(pkg), alpm_pkg_get_size(pkg)/1024);
    }

    if(alpm_release() == -1) {
        fprintf(stderr, "error releasing alpm: %s\n", alpm_strerror(pm_errno));
    }

    exit(EXIT_SUCCESS);
}
#include <stdio.h>

#include <alpm.h>
#include <alpm_list.h>

void displaydeps(pmpkg_t *pkg) {
    alpm_list_t *i;

    for(i = alpm_pkg_get_depends(pkg); i; i = alpm_list_next(i)) {
        pmdepend_t *dep = alpm_list_getdata(i);
        char *depstring = alpm_dep_get_name(dep);
        printf("%s \n", depstring);
    }
}

int main(int argc, char **argv)
{
    pmdb_t *db = NULL;
    alpm_list_t *i;

    if(argc != 2) {
        printf("usage : displaydeps <pkg>\n");
        exit(EXIT_FAILURE);
    }

    if(alpm_initialize() == -1) {
        fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerror(pm_errno));
        exit(EXIT_FAILURE);
    }

    alpm_option_set_dbpath("/var/lib/pacman");

    db = alpm_option_get_localdb();
    if(db == NULL) {
        fprintf(stderr, "error: could not register 'local' database (%s)\n",
                alpm_strerror(pm_errno));
        exit(EXIT_FAILURE);
    }

    /* if the package exists, list its dependencies */
    pmpkg_t *pkg = alpm_db_get_pkg(db, argv[1]);
    if(pkg) {
        displaydeps(pkg);
    } else {
        /* otherwise, search for matching packages */
        fprintf(stderr, "error: package %s does not exist\n", argv[1]);
        alpm_list_t *targets = alpm_list_add(NULL, argv[1]);
        alpm_list_t *res = alpm_db_search(db, targets);

        fprintf(stderr, "matching packages :\n");
        for(i = res; i; i = alpm_list_next(i)) {
            pmpkg_t *pkg = alpm_list_getdata(i);
            printf("%s\n", alpm_pkg_get_name(pkg));
        }
    }


    if(alpm_release() == -1) {
        fprintf(stderr, "error releasing alpm: %s\n", alpm_strerror(pm_errno));
    }

    exit(EXIT_SUCCESS);
}

Offline

#10 2011-09-15 14:00:41

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,097

Re: Pacman's libalpm documentation

HolyNecroPost_759032.jpg


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

Board footer

Powered by FluxBB