2008-10-26 21:33:16 +01:00
|
|
|
/* Code to mangle pathnames into those matching a given prefix.
|
|
|
|
eg. open("/lib/foo.so") => open("/usr/gnemul/i386-linux/lib/foo.so");
|
|
|
|
|
|
|
|
The assumption is that this area does not change.
|
|
|
|
*/
|
2016-01-29 18:49:55 +01:00
|
|
|
#include "qemu/osdep.h"
|
2008-10-26 21:33:16 +01:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <dirent.h>
|
2016-03-20 18:16:19 +01:00
|
|
|
#include "qemu/cutils.h"
|
|
|
|
#include "qemu/path.h"
|
2019-05-19 22:19:41 +02:00
|
|
|
#include "qemu/thread.h"
|
2008-10-26 21:33:16 +01:00
|
|
|
|
2019-05-19 22:19:41 +02:00
|
|
|
static const char *base;
|
|
|
|
static GHashTable *hash;
|
|
|
|
static QemuMutex lock;
|
2008-10-26 21:33:16 +01:00
|
|
|
|
2019-05-19 22:19:41 +02:00
|
|
|
void init_paths(const char *prefix)
|
2008-10-26 21:33:16 +01:00
|
|
|
{
|
2019-05-19 22:19:41 +02:00
|
|
|
if (prefix[0] == '\0' || !strcmp(prefix, "/")) {
|
|
|
|
return;
|
2008-10-26 21:33:16 +01:00
|
|
|
}
|
2011-02-07 07:05:56 +01:00
|
|
|
|
2019-05-19 22:19:41 +02:00
|
|
|
if (prefix[0] == '/') {
|
|
|
|
base = g_strdup(prefix);
|
|
|
|
} else {
|
|
|
|
char *cwd = g_get_current_dir();
|
|
|
|
base = g_build_filename(cwd, prefix, NULL);
|
|
|
|
g_free(cwd);
|
2011-02-07 07:05:56 +01:00
|
|
|
}
|
2008-10-26 21:33:16 +01:00
|
|
|
|
2019-05-19 22:19:41 +02:00
|
|
|
hash = g_hash_table_new(g_str_hash, g_str_equal);
|
|
|
|
qemu_mutex_init(&lock);
|
2008-10-26 21:33:16 +01:00
|
|
|
}
|
|
|
|
|
2019-05-19 22:19:41 +02:00
|
|
|
/* Look for path in emulation dir, otherwise return name. */
|
|
|
|
const char *path(const char *name)
|
2008-10-26 21:33:16 +01:00
|
|
|
{
|
2019-05-19 22:19:41 +02:00
|
|
|
gpointer key, value;
|
|
|
|
const char *ret;
|
2008-10-26 21:33:16 +01:00
|
|
|
|
2019-05-19 22:19:41 +02:00
|
|
|
/* Only do absolute paths: quick and dirty, but should mostly be OK. */
|
|
|
|
if (!base || !name || name[0] != '/') {
|
|
|
|
return name;
|
|
|
|
}
|
2008-10-26 21:33:16 +01:00
|
|
|
|
2019-05-19 22:19:41 +02:00
|
|
|
qemu_mutex_lock(&lock);
|
2008-10-26 21:33:16 +01:00
|
|
|
|
2019-05-19 22:19:41 +02:00
|
|
|
/* Have we looked up this file before? */
|
|
|
|
if (g_hash_table_lookup_extended(hash, name, &key, &value)) {
|
|
|
|
ret = value ? value : name;
|
2008-10-26 21:33:16 +01:00
|
|
|
} else {
|
2019-05-19 22:19:41 +02:00
|
|
|
char *save = g_strdup(name);
|
|
|
|
char *full = g_build_filename(base, name, NULL);
|
|
|
|
|
|
|
|
/* Look for the path; record the result, pass or fail. */
|
|
|
|
if (access(full, F_OK) == 0) {
|
|
|
|
/* Exists. */
|
|
|
|
g_hash_table_insert(hash, save, full);
|
|
|
|
ret = full;
|
|
|
|
} else {
|
|
|
|
/* Does not exist. */
|
|
|
|
g_free(full);
|
|
|
|
g_hash_table_insert(hash, save, NULL);
|
|
|
|
ret = name;
|
|
|
|
}
|
2008-10-26 21:33:16 +01:00
|
|
|
}
|
|
|
|
|
2019-05-19 22:19:41 +02:00
|
|
|
qemu_mutex_unlock(&lock);
|
|
|
|
return ret;
|
2008-10-26 21:33:16 +01:00
|
|
|
}
|