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> |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 9 | #include "qemu-common.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame^] | 10 | #include "qemu/cutils.h" |
| 11 | #include "qemu/path.h" |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 12 | |
| 13 | struct pathelem |
| 14 | { |
| 15 | /* Name of this, eg. lib */ |
| 16 | char *name; |
| 17 | /* Full path name, eg. /usr/gnemul/x86-linux/lib. */ |
| 18 | char *pathname; |
| 19 | struct pathelem *parent; |
| 20 | /* Children */ |
| 21 | unsigned int num_entries; |
| 22 | struct pathelem *entries[0]; |
| 23 | }; |
| 24 | |
| 25 | static struct pathelem *base; |
| 26 | |
| 27 | /* First N chars of S1 match S2, and S2 is N chars long. */ |
| 28 | static int strneq(const char *s1, unsigned int n, const char *s2) |
| 29 | { |
| 30 | unsigned int i; |
| 31 | |
| 32 | for (i = 0; i < n; i++) |
| 33 | if (s1[i] != s2[i]) |
| 34 | return 0; |
| 35 | return s2[i] == 0; |
| 36 | } |
| 37 | |
Mike Frysinger | 2296f19 | 2011-02-07 01:05:56 -0500 | [diff] [blame] | 38 | static struct pathelem *add_entry(struct pathelem *root, const char *name, |
Stefan Weil | ddd2363 | 2013-10-02 22:40:29 +0200 | [diff] [blame] | 39 | unsigned type); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 40 | |
| 41 | static struct pathelem *new_entry(const char *root, |
| 42 | struct pathelem *parent, |
| 43 | const char *name) |
| 44 | { |
zhanghailiang | bc5008a | 2014-08-18 15:49:22 +0800 | [diff] [blame] | 45 | struct pathelem *new = g_malloc(sizeof(*new)); |
| 46 | new->name = g_strdup(name); |
Stefan Weil | e4ada48 | 2013-01-16 18:37:23 +0100 | [diff] [blame] | 47 | new->pathname = g_strdup_printf("%s/%s", root, name); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 48 | new->num_entries = 0; |
| 49 | return new; |
| 50 | } |
| 51 | |
| 52 | #define streq(a,b) (strcmp((a), (b)) == 0) |
| 53 | |
Mike Frysinger | 2296f19 | 2011-02-07 01:05:56 -0500 | [diff] [blame] | 54 | /* Not all systems provide this feature */ |
Richard Henderson | 338d80d | 2013-01-04 16:39:33 -0800 | [diff] [blame] | 55 | #if defined(DT_DIR) && defined(DT_UNKNOWN) && defined(DT_LNK) |
Mike Frysinger | 2296f19 | 2011-02-07 01:05:56 -0500 | [diff] [blame] | 56 | # define dirent_type(dirent) ((dirent)->d_type) |
Richard Henderson | 338d80d | 2013-01-04 16:39:33 -0800 | [diff] [blame] | 57 | # define is_dir_maybe(type) \ |
| 58 | ((type) == DT_DIR || (type) == DT_UNKNOWN || (type) == DT_LNK) |
Mike Frysinger | 2296f19 | 2011-02-07 01:05:56 -0500 | [diff] [blame] | 59 | #else |
| 60 | # define dirent_type(dirent) (1) |
| 61 | # define is_dir_maybe(type) (type) |
| 62 | #endif |
| 63 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 64 | static struct pathelem *add_dir_maybe(struct pathelem *path) |
| 65 | { |
| 66 | DIR *dir; |
| 67 | |
| 68 | if ((dir = opendir(path->pathname)) != NULL) { |
| 69 | struct dirent *dirent; |
| 70 | |
| 71 | while ((dirent = readdir(dir)) != NULL) { |
| 72 | if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")){ |
Mike Frysinger | 2296f19 | 2011-02-07 01:05:56 -0500 | [diff] [blame] | 73 | path = add_entry(path, dirent->d_name, dirent_type(dirent)); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | closedir(dir); |
| 77 | } |
| 78 | return path; |
| 79 | } |
| 80 | |
Mike Frysinger | 2296f19 | 2011-02-07 01:05:56 -0500 | [diff] [blame] | 81 | static struct pathelem *add_entry(struct pathelem *root, const char *name, |
Stefan Weil | ddd2363 | 2013-10-02 22:40:29 +0200 | [diff] [blame] | 82 | unsigned type) |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 83 | { |
Mike Frysinger | 2296f19 | 2011-02-07 01:05:56 -0500 | [diff] [blame] | 84 | struct pathelem **e; |
| 85 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 86 | root->num_entries++; |
| 87 | |
zhanghailiang | bc5008a | 2014-08-18 15:49:22 +0800 | [diff] [blame] | 88 | root = g_realloc(root, sizeof(*root) |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 89 | + sizeof(root->entries[0])*root->num_entries); |
Mike Frysinger | 2296f19 | 2011-02-07 01:05:56 -0500 | [diff] [blame] | 90 | e = &root->entries[root->num_entries-1]; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 91 | |
Mike Frysinger | 2296f19 | 2011-02-07 01:05:56 -0500 | [diff] [blame] | 92 | *e = new_entry(root->pathname, root, name); |
| 93 | if (is_dir_maybe(type)) { |
| 94 | *e = add_dir_maybe(*e); |
| 95 | } |
| 96 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 97 | return root; |
| 98 | } |
| 99 | |
| 100 | /* This needs to be done after tree is stabilized (ie. no more reallocs!). */ |
| 101 | static void set_parents(struct pathelem *child, struct pathelem *parent) |
| 102 | { |
| 103 | unsigned int i; |
| 104 | |
| 105 | child->parent = parent; |
| 106 | for (i = 0; i < child->num_entries; i++) |
| 107 | set_parents(child->entries[i], child); |
| 108 | } |
| 109 | |
| 110 | /* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */ |
| 111 | static const char * |
| 112 | follow_path(const struct pathelem *cursor, const char *name) |
| 113 | { |
| 114 | unsigned int i, namelen; |
| 115 | |
| 116 | name += strspn(name, "/"); |
| 117 | namelen = strcspn(name, "/"); |
| 118 | |
| 119 | if (namelen == 0) |
| 120 | return cursor->pathname; |
| 121 | |
| 122 | if (strneq(name, namelen, "..")) |
| 123 | return follow_path(cursor->parent, name + namelen); |
| 124 | |
| 125 | if (strneq(name, namelen, ".")) |
| 126 | return follow_path(cursor, name + namelen); |
| 127 | |
| 128 | for (i = 0; i < cursor->num_entries; i++) |
| 129 | if (strneq(name, namelen, cursor->entries[i]->name)) |
| 130 | return follow_path(cursor->entries[i], name + namelen); |
| 131 | |
| 132 | /* Not found */ |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | void init_paths(const char *prefix) |
| 137 | { |
| 138 | char pref_buf[PATH_MAX]; |
| 139 | |
| 140 | if (prefix[0] == '\0' || |
| 141 | !strcmp(prefix, "/")) |
| 142 | return; |
| 143 | |
| 144 | if (prefix[0] != '/') { |
| 145 | char *cwd = getcwd(NULL, 0); |
| 146 | size_t pref_buf_len = sizeof(pref_buf); |
| 147 | |
| 148 | if (!cwd) |
| 149 | abort(); |
| 150 | pstrcpy(pref_buf, sizeof(pref_buf), cwd); |
| 151 | pstrcat(pref_buf, pref_buf_len, "/"); |
| 152 | pstrcat(pref_buf, pref_buf_len, prefix); |
| 153 | free(cwd); |
| 154 | } else |
| 155 | pstrcpy(pref_buf, sizeof(pref_buf), prefix + 1); |
| 156 | |
| 157 | base = new_entry("", NULL, pref_buf); |
| 158 | base = add_dir_maybe(base); |
| 159 | if (base->num_entries == 0) { |
Kirill Batuzov | 00a9cac | 2014-04-10 18:07:57 +0400 | [diff] [blame] | 160 | g_free(base->pathname); |
zhanghailiang | bc5008a | 2014-08-18 15:49:22 +0800 | [diff] [blame] | 161 | g_free(base->name); |
| 162 | g_free(base); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 163 | base = NULL; |
| 164 | } else { |
| 165 | set_parents(base, base); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /* Look for path in emulation dir, otherwise return name. */ |
| 170 | const char *path(const char *name) |
| 171 | { |
| 172 | /* Only do absolute paths: quick and dirty, but should mostly be OK. |
| 173 | Could do relative by tracking cwd. */ |
Blue Swirl | 3702208 | 2009-08-15 07:51:59 +0000 | [diff] [blame] | 174 | if (!base || !name || name[0] != '/') |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 175 | return name; |
| 176 | |
| 177 | return follow_path(base, name) ?: name; |
| 178 | } |