memory: RCU ram_list.dirty_memory[] for safe RAM hotplug
[qemu/kevin.git] / util / path.c
blobd09e8c5e142582aab7004d8e175d4065d73fd3c5
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-common.h"
11 struct pathelem
13 /* Name of this, eg. lib */
14 char *name;
15 /* Full path name, eg. /usr/gnemul/x86-linux/lib. */
16 char *pathname;
17 struct pathelem *parent;
18 /* Children */
19 unsigned int num_entries;
20 struct pathelem *entries[0];
23 static struct pathelem *base;
25 /* First N chars of S1 match S2, and S2 is N chars long. */
26 static int strneq(const char *s1, unsigned int n, const char *s2)
28 unsigned int i;
30 for (i = 0; i < n; i++)
31 if (s1[i] != s2[i])
32 return 0;
33 return s2[i] == 0;
36 static struct pathelem *add_entry(struct pathelem *root, const char *name,
37 unsigned type);
39 static struct pathelem *new_entry(const char *root,
40 struct pathelem *parent,
41 const char *name)
43 struct pathelem *new = g_malloc(sizeof(*new));
44 new->name = g_strdup(name);
45 new->pathname = g_strdup_printf("%s/%s", root, name);
46 new->num_entries = 0;
47 return new;
50 #define streq(a,b) (strcmp((a), (b)) == 0)
52 /* Not all systems provide this feature */
53 #if defined(DT_DIR) && defined(DT_UNKNOWN) && defined(DT_LNK)
54 # define dirent_type(dirent) ((dirent)->d_type)
55 # define is_dir_maybe(type) \
56 ((type) == DT_DIR || (type) == DT_UNKNOWN || (type) == DT_LNK)
57 #else
58 # define dirent_type(dirent) (1)
59 # define is_dir_maybe(type) (type)
60 #endif
62 static struct pathelem *add_dir_maybe(struct pathelem *path)
64 DIR *dir;
66 if ((dir = opendir(path->pathname)) != NULL) {
67 struct dirent *dirent;
69 while ((dirent = readdir(dir)) != NULL) {
70 if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")){
71 path = add_entry(path, dirent->d_name, dirent_type(dirent));
74 closedir(dir);
76 return path;
79 static struct pathelem *add_entry(struct pathelem *root, const char *name,
80 unsigned type)
82 struct pathelem **e;
84 root->num_entries++;
86 root = g_realloc(root, sizeof(*root)
87 + sizeof(root->entries[0])*root->num_entries);
88 e = &root->entries[root->num_entries-1];
90 *e = new_entry(root->pathname, root, name);
91 if (is_dir_maybe(type)) {
92 *e = add_dir_maybe(*e);
95 return root;
98 /* This needs to be done after tree is stabilized (ie. no more reallocs!). */
99 static void set_parents(struct pathelem *child, struct pathelem *parent)
101 unsigned int i;
103 child->parent = parent;
104 for (i = 0; i < child->num_entries; i++)
105 set_parents(child->entries[i], child);
108 /* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
109 static const char *
110 follow_path(const struct pathelem *cursor, const char *name)
112 unsigned int i, namelen;
114 name += strspn(name, "/");
115 namelen = strcspn(name, "/");
117 if (namelen == 0)
118 return cursor->pathname;
120 if (strneq(name, namelen, ".."))
121 return follow_path(cursor->parent, name + namelen);
123 if (strneq(name, namelen, "."))
124 return follow_path(cursor, name + namelen);
126 for (i = 0; i < cursor->num_entries; i++)
127 if (strneq(name, namelen, cursor->entries[i]->name))
128 return follow_path(cursor->entries[i], name + namelen);
130 /* Not found */
131 return NULL;
134 void init_paths(const char *prefix)
136 char pref_buf[PATH_MAX];
138 if (prefix[0] == '\0' ||
139 !strcmp(prefix, "/"))
140 return;
142 if (prefix[0] != '/') {
143 char *cwd = getcwd(NULL, 0);
144 size_t pref_buf_len = sizeof(pref_buf);
146 if (!cwd)
147 abort();
148 pstrcpy(pref_buf, sizeof(pref_buf), cwd);
149 pstrcat(pref_buf, pref_buf_len, "/");
150 pstrcat(pref_buf, pref_buf_len, prefix);
151 free(cwd);
152 } else
153 pstrcpy(pref_buf, sizeof(pref_buf), prefix + 1);
155 base = new_entry("", NULL, pref_buf);
156 base = add_dir_maybe(base);
157 if (base->num_entries == 0) {
158 g_free(base->pathname);
159 g_free(base->name);
160 g_free(base);
161 base = NULL;
162 } else {
163 set_parents(base, base);
167 /* Look for path in emulation dir, otherwise return name. */
168 const char *path(const char *name)
170 /* Only do absolute paths: quick and dirty, but should mostly be OK.
171 Could do relative by tracking cwd. */
172 if (!base || !name || name[0] != '/')
173 return name;
175 return follow_path(base, name) ?: name;