1 /* Code to mangle pathnames into those matching a given prefix.
2 eg. open("/lib/foo.so") => open("/usr/gnemul/i386-linux/lib/foo.so");
4 The assumption is that this area does not change.
6 #include "qemu/osdep.h"
9 #include "qemu/cutils.h"
10 #include "qemu/path.h"
11 #include "qemu/thread.h"
13 static const char *base
;
14 static GHashTable
*hash
;
15 static QemuMutex lock
;
17 void init_paths(const char *prefix
)
19 if (prefix
[0] == '\0' || !strcmp(prefix
, "/")) {
23 if (prefix
[0] == '/') {
24 base
= g_strdup(prefix
);
26 char *cwd
= g_get_current_dir();
27 base
= g_build_filename(cwd
, prefix
, NULL
);
31 hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
32 qemu_mutex_init(&lock
);
35 /* Look for path in emulation dir, otherwise return name. */
36 const char *path(const char *name
)
41 /* Only do absolute paths: quick and dirty, but should mostly be OK. */
42 if (!base
|| !name
|| name
[0] != '/') {
46 qemu_mutex_lock(&lock
);
48 /* Have we looked up this file before? */
49 if (g_hash_table_lookup_extended(hash
, name
, &key
, &value
)) {
50 ret
= value
? value
: name
;
52 char *save
= g_strdup(name
);
53 char *full
= g_build_filename(base
, name
, NULL
);
55 /* Look for the path; record the result, pass or fail. */
56 if (access(full
, F_OK
) == 0) {
58 g_hash_table_insert(hash
, save
, full
);
63 g_hash_table_insert(hash
, save
, NULL
);
68 qemu_mutex_unlock(&lock
);