target/ppc: Flush the TLB locally when the LPIDR is written
[qemu/kevin.git] / util / path.c
blob7f9fc272fbb3f5aee5a2a781ebb989bf208a8023
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.
5 */
6 #include "qemu/osdep.h"
7 #include <sys/param.h>
8 #include <dirent.h>
9 #include "qemu/cutils.h"
10 #include "qemu/path.h"
12 struct pathelem
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];
24 static struct pathelem *base;
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)
29 unsigned int i;
31 for (i = 0; i < n; i++)
32 if (s1[i] != s2[i])
33 return 0;
34 return s2[i] == 0;
37 static struct pathelem *add_entry(struct pathelem *root, const char *name,
38 unsigned type);
40 static struct pathelem *new_entry(const char *root,
41 struct pathelem *parent,
42 const char *name)
44 struct pathelem *new = g_malloc(sizeof(*new));
45 new->name = g_strdup(name);
46 new->pathname = g_strdup_printf("%s/%s", root, name);
47 new->num_entries = 0;
48 return new;
51 #define streq(a,b) (strcmp((a), (b)) == 0)
53 /* Not all systems provide this feature */
54 #if defined(DT_DIR) && defined(DT_UNKNOWN) && defined(DT_LNK)
55 # define dirent_type(dirent) ((dirent)->d_type)
56 # define is_dir_maybe(type) \
57 ((type) == DT_DIR || (type) == DT_UNKNOWN || (type) == DT_LNK)
58 #else
59 # define dirent_type(dirent) (1)
60 # define is_dir_maybe(type) (type)
61 #endif
63 static struct pathelem *add_dir_maybe(struct pathelem *path)
65 DIR *dir;
67 if ((dir = opendir(path->pathname)) != NULL) {
68 struct dirent *dirent;
70 while ((dirent = readdir(dir)) != NULL) {
71 if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")){
72 path = add_entry(path, dirent->d_name, dirent_type(dirent));
75 closedir(dir);
77 return path;
80 static struct pathelem *add_entry(struct pathelem *root, const char *name,
81 unsigned type)
83 struct pathelem **e;
85 root->num_entries++;
87 root = g_realloc(root, sizeof(*root)
88 + sizeof(root->entries[0])*root->num_entries);
89 e = &root->entries[root->num_entries-1];
91 *e = new_entry(root->pathname, root, name);
92 if (is_dir_maybe(type)) {
93 *e = add_dir_maybe(*e);
96 return root;
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)
102 unsigned int i;
104 child->parent = parent;
105 for (i = 0; i < child->num_entries; i++)
106 set_parents(child->entries[i], child);
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)
113 unsigned int i, namelen;
115 name += strspn(name, "/");
116 namelen = strcspn(name, "/");
118 if (namelen == 0)
119 return cursor->pathname;
121 if (strneq(name, namelen, ".."))
122 return follow_path(cursor->parent, name + namelen);
124 if (strneq(name, namelen, "."))
125 return follow_path(cursor, name + namelen);
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);
131 /* Not found */
132 return NULL;
135 void init_paths(const char *prefix)
137 char pref_buf[PATH_MAX];
139 if (prefix[0] == '\0' ||
140 !strcmp(prefix, "/"))
141 return;
143 if (prefix[0] != '/') {
144 char *cwd = getcwd(NULL, 0);
145 size_t pref_buf_len = sizeof(pref_buf);
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);
156 base = new_entry("", NULL, pref_buf);
157 base = add_dir_maybe(base);
158 if (base->num_entries == 0) {
159 g_free(base->pathname);
160 g_free(base->name);
161 g_free(base);
162 base = NULL;
163 } else {
164 set_parents(base, base);
168 /* Look for path in emulation dir, otherwise return name. */
169 const char *path(const char *name)
171 /* Only do absolute paths: quick and dirty, but should mostly be OK.
172 Could do relative by tracking cwd. */
173 if (!base || !name || name[0] != '/')
174 return name;
176 return follow_path(base, name) ?: name;