Re: Proposal: an addition to glib for getting the absolute path of the current binary.



Hi,

I'm so glad this topic came up. (; 
On Solaris, it's a requirement that all packages must be
relocatable. GNOME got a temporary waiver and we keep putting this
off due to more urgent stuff...

Let me throw in some ideas that we came up with when talking with
some other Sun folks.

> The problem:
> 
> Only Win32 and Linux, as far as I know, provide a way to get the absolute
> path of a given binary. On Linux you can read /proc/self/exe, or for a way
> that works for libraries as well, /proc/self/maps. On Win32 there is a
> GetModuleFilename() API.

[...]

Hmm... it'd be great to find a platform independent
solution -- something that would also work on, say, Solaris (;

> The solution:
> 
> It would be nice if glib could provide something like the following API:
> 
> gboolean g_get_absolute_path(gchar *fallback, gchar **out);

Since on most Unix flavours there's no way to find the
path to the running binary, I think we should try something else.
I propose a function (I'll call this g_find_file) that returns the
absolute location of a file specified by a relative path, searching
through a list of base directories, for example:

	g_find_file ("share/images/splash.png")

would return "/usr/share/images/splash.png".

Now the question is how to define the list of prefixes to search.
Two obvious choices are
 - an environment variable
 - a global config file

I think an env variable can cause a lot of problems if it
doesn't get properly delegated to all executables that need
it (like bonobo components, etc), so I would go for a global
config file. The location of this file should be glib's
$sysconfdir. (This path could be added to the pkgconfig .pc
file, in case you want to read it in an install script or
something.)

I believe this proposal addresses all the issues in the
original post, except when a non-root user wants to install
something. Something like a dot-file in $HOME could help.
It would only be used if (getuid () == geteuid ()).

A slightly more complicated version of this proposal is to
keep multiple lists of directories in the global config file,
something like this:

sysconfdir = /etc:/usr/local/etc:/opt/gnome-2.7/etc
datadir = /usr/share:/net/someserver/share

And pass in the name of the directory to the g_find_file function:

	g_find_file ("datadir", "images/splash.png")

How would this work?

This is what much of the GNOME code currently does:
You put -DFOODIR=\"$(foodir)\" in your Makefile and then you have
something like

	FOODIR "/relative/path/to/file"

in the C code. Instead, you could just use

	g_find_file ("foodir", "/relative/path/to/file")

in most cases. The overhead is basically a few stat() calls.

What do people think?

Thanks,

Laca




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]