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.
15 #include "qemu-common.h"
19 /* Name of this, eg. lib */
21 /* Full path name, eg. /usr/gnemul/x86-linux/lib. */
23 struct pathelem *parent;
25 unsigned int num_entries;
26 struct pathelem *entries[0];
29 static struct pathelem *base;
31 /* First N chars of S1 match S2, and S2 is N chars long. */
32 static int strneq(const char *s1, unsigned int n, const char *s2)
36 for (i = 0; i < n; i++)
42 static struct pathelem *add_entry(struct pathelem *root, const char *name);
44 static struct pathelem *new_entry(const char *root,
45 struct pathelem *parent,
48 struct pathelem *new = malloc(sizeof(*new));
49 new->name = strdup(name);
50 asprintf(&new->pathname, "%s/%s", root, name);
55 #define streq(a,b) (strcmp((a), (b)) == 0)
57 static struct pathelem *add_dir_maybe(struct pathelem *path)
61 if ((dir = opendir(path->pathname)) != NULL) {
62 struct dirent *dirent;
64 while ((dirent = readdir(dir)) != NULL) {
65 if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")){
66 path = add_entry(path, dirent->d_name);
74 static struct pathelem *add_entry(struct pathelem *root, const char *name)
78 root = realloc(root, sizeof(*root)
79 + sizeof(root->entries[0])*root->num_entries);
81 root->entries[root->num_entries-1] = new_entry(root->pathname, root, name);
82 root->entries[root->num_entries-1]
83 = add_dir_maybe(root->entries[root->num_entries-1]);
87 /* This needs to be done after tree is stabilized (ie. no more reallocs!). */
88 static void set_parents(struct pathelem *child, struct pathelem *parent)
92 child->parent = parent;
93 for (i = 0; i < child->num_entries; i++)
94 set_parents(child->entries[i], child);
97 /* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
99 follow_path(const struct pathelem *cursor, const char *name)
101 unsigned int i, namelen;
103 name += strspn(name, "/");
104 namelen = strcspn(name, "/");
107 return cursor->pathname;
109 if (strneq(name, namelen, ".."))
110 return follow_path(cursor->parent, name + namelen);
112 if (strneq(name, namelen, "."))
113 return follow_path(cursor, name + namelen);
115 for (i = 0; i < cursor->num_entries; i++)
116 if (strneq(name, namelen, cursor->entries[i]->name))
117 return follow_path(cursor->entries[i], name + namelen);
123 void init_paths(const char *prefix)
125 char pref_buf[PATH_MAX];
127 if (prefix[0] == '\0' ||
128 !strcmp(prefix, "/"))
131 if (prefix[0] != '/') {
132 char *cwd = getcwd(NULL, 0);
133 size_t pref_buf_len = sizeof(pref_buf);
137 pstrcpy(pref_buf, sizeof(pref_buf), cwd);
138 pstrcat(pref_buf, pref_buf_len, "/");
139 pstrcat(pref_buf, pref_buf_len, prefix);
142 pstrcpy(pref_buf, sizeof(pref_buf), prefix + 1);
144 base = new_entry("", NULL, pref_buf);
145 base = add_dir_maybe(base);
146 if (base->num_entries == 0) {
150 set_parents(base, base);
154 /* Look for path in emulation dir, otherwise return name. */
155 const char *path(const char *name)
157 /* Only do absolute paths: quick and dirty, but should mostly be OK.
158 Could do relative by tracking cwd. */
159 if (!base || name[0] != '/')
162 return follow_path(base, name) ?: name;