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