You are not logged in.

#1 2013-02-13 11:10:29

skeptikos
Member
Registered: 2009-03-07
Posts: 11

[SOLVED] hacking libalpm

Hi all,

I've been doing some hacking on libalpm. Specifically I'm trying to write Ruby bindings for the library, but I've run into a bit of a problem with compilation.

I've reduced the problem down to a single c source file in which I try to simply initialize libalpm. Here's what it looks like:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
 
#include "pacman/lib/libalpm/alpm_list.h"
#include "pacman/lib/libalpm/alpm.h"
 
alpm_handle_t *ALPM_RB_HANDLE = NULL;
 
int main() {
  enum _alpm_errno_t err;
  char *root = "./";
  char *dbpath = "./";
 
  *ALPM_RB_HANDLE = alpm_initialize(root, dbpath, &err);
  return 1;
}

Unfortunately my experience with C is limited to that which I learned during a semester at University and I've gotten stuck on the following error.

$ gcc -Ipacman/lib/libalpm -Lpacman/lib/libalpm alpm_session.c 
alpm_session.c: In function ‘main’:
alpm_session.c:18:3: error: dereferencing pointer to incomplete type
$

Because calling

./configure

and

make

in the pacman folder ran through without any visible problems, I thought I might need to add the

libalpm_la-alpm.o

to the gcc command line, so I tried the following:

gcc -Ipacman/lib/libalpm -Lpacman/lib/libalpm pacman/lib/libalpm/libalpm_la-alpm.o alpm_session.c

and got the same error. After that, I looked at the definition of

__alpm_handle_t

in

pacman/lib/libalpm/handle.h

and thought that maybe all of the alpm_<types> that are referenced in it need to be compiled as well, so I came up with the following command line:

gcc -Ipacman/lib/libalpm -Lpacman/lib/libalpm pacman/lib/libalpm/libalpm_la-alpm.o pacman/lib/libalpm/libalpm_la-db.o pacman/lib/libalpm/libalpm_la-trans.o pacman/lib/libalpm/libalpm_la-handle.o pacman/lib/libalpm/libalpm_la-alpm_list.o alpm_session.c

and still got the same error. It's now becoming obvious that I'm not going to get further without some extra help. Can anyone tell me what I might be doing wrong, or how I might be able to debug this?

For bonus points, anyone who can tell me what the

.libs

folder in

pacman/lib/libalpm

is for, I'd be curious about that too, but I think that might be a question better suited for the pacman irc channel.

Thanks in advance,
chris

Last edited by skeptikos (2013-03-01 15:29:06)

Offline

#2 2013-02-13 11:38:10

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,388
Website

Re: [SOLVED] hacking libalpm

While I haven't used libalpm, I suspect this is the needed change:

- *ALPM_RB_HANDLE = alpm_initialize(root, dbpath, &err);
+ ALPM_RB_HANDLE = alpm_initialize(root, dbpath, &err);

If alpm_initialize returns a pointer to an initialized block of memory, you want to set the pointer address to the result, not the contents.  If you need to set the contents to the return value (which I find unlikely), you'd need to allocate space for the structure.

You'll want to check too whether you are expected to free the memory allocated by alpm_initialize.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#3 2013-02-13 13:30:20

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,092
Website

Re: [SOLVED] hacking libalpm

While I'm sure that you'd build a lovely new wheel, maybe you should start from one of the 4 projects on github that aim to do this already, or at least use them for inspiration. You seem to be unfamiliar with C.

https://github.com/Nss/alpm_ruby
https://github.com/flooose/alpm_ruby
https://github.com/juster/ruby-alpm
https://github.com/orospakr/pacman-ruby

I've no idea what state these bindings are in, particularly given the rapidly approaching pacman 4.1 release which will change some amount of API.

Last edited by falconindy (2013-02-13 13:35:33)

Offline

#4 2013-02-14 12:44:03

skeptikos
Member
Registered: 2009-03-07
Posts: 11

Re: [SOLVED] hacking libalpm

Trilby wrote:

While I haven't used libalpm, I suspect this is the needed change:

- *ALPM_RB_HANDLE = alpm_initialize(root, dbpath, &err);
+ ALPM_RB_HANDLE = alpm_initialize(root, dbpath, &err);

If alpm_initialize returns a pointer to an initialized block of memory, you want to set the pointer address to the result, not the contents.  If you need to set the contents to the return value (which I find unlikely), you'd need to allocate space for the structure.

You'll want to check too whether you are expected to free the memory allocated by alpm_initialize.

Yep, that did it. At least it got me further. Freeing and such are then next steps. I have to look at what alpm_release does.

@falconindy that's exactly why I decided to try my hand at this. I had studied a semester of C and liked it, but since I don't work with it regularly, what little I had learned got rusty. In fact, one of the repositories you listed is mine. I started a few years ago with it and haven't gotten back to it. I'm now in a situation where I can chip away at it regularly, so I thought I'd try. I I will look at the other projects that are out there too now thought.

Cheers,
Chris

Offline

Board footer

Powered by FluxBB