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