blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 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"); |
| 3 | |
| 4 | The assumption is that this area does not change. |
| 5 | */ |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 6 | #include "qemu/osdep.h" |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 7 | #include <sys/param.h> |
| 8 | #include <dirent.h> |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 9 | #include "qemu/cutils.h" |
| 10 | #include "qemu/path.h" |
Richard Henderson | f3a8bdc | 2019-05-19 13:19:41 -0700 | [diff] [blame] | 11 | #include "qemu/thread.h" |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 12 | |
Richard Henderson | f3a8bdc | 2019-05-19 13:19:41 -0700 | [diff] [blame] | 13 | static const char *base; |
| 14 | static GHashTable *hash; |
| 15 | static QemuMutex lock; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 16 | |
| 17 | void init_paths(const char *prefix) |
| 18 | { |
Richard Henderson | f3a8bdc | 2019-05-19 13:19:41 -0700 | [diff] [blame] | 19 | if (prefix[0] == '\0' || !strcmp(prefix, "/")) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 20 | return; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 21 | } |
Richard Henderson | f3a8bdc | 2019-05-19 13:19:41 -0700 | [diff] [blame] | 22 | |
| 23 | if (prefix[0] == '/') { |
| 24 | base = g_strdup(prefix); |
| 25 | } else { |
| 26 | char *cwd = g_get_current_dir(); |
| 27 | base = g_build_filename(cwd, prefix, NULL); |
| 28 | g_free(cwd); |
| 29 | } |
| 30 | |
| 31 | hash = g_hash_table_new(g_str_hash, g_str_equal); |
| 32 | qemu_mutex_init(&lock); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /* Look for path in emulation dir, otherwise return name. */ |
| 36 | const char *path(const char *name) |
| 37 | { |
Richard Henderson | f3a8bdc | 2019-05-19 13:19:41 -0700 | [diff] [blame] | 38 | gpointer key, value; |
| 39 | const char *ret; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 40 | |
Richard Henderson | f3a8bdc | 2019-05-19 13:19:41 -0700 | [diff] [blame] | 41 | /* Only do absolute paths: quick and dirty, but should mostly be OK. */ |
| 42 | if (!base || !name || name[0] != '/') { |
| 43 | return name; |
| 44 | } |
| 45 | |
| 46 | qemu_mutex_lock(&lock); |
| 47 | |
| 48 | /* Have we looked up this file before? */ |
| 49 | if (g_hash_table_lookup_extended(hash, name, &key, &value)) { |
| 50 | ret = value ? value : name; |
| 51 | } else { |
| 52 | char *save = g_strdup(name); |
| 53 | char *full = g_build_filename(base, name, NULL); |
| 54 | |
| 55 | /* Look for the path; record the result, pass or fail. */ |
| 56 | if (access(full, F_OK) == 0) { |
| 57 | /* Exists. */ |
| 58 | g_hash_table_insert(hash, save, full); |
| 59 | ret = full; |
| 60 | } else { |
| 61 | /* Does not exist. */ |
| 62 | g_free(full); |
| 63 | g_hash_table_insert(hash, save, NULL); |
| 64 | ret = name; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | qemu_mutex_unlock(&lock); |
| 69 | return ret; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 70 | } |