xref: /openbmc/qemu/util/path.c (revision b917da4c)
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 "qemu/osdep.h"
7 #include <sys/param.h>
8 #include <dirent.h>
9 #include "qemu-common.h"
10 
11 struct pathelem
12 {
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];
21 };
22 
23 static struct pathelem *base;
24 
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)
27 {
28     unsigned int i;
29 
30     for (i = 0; i < n; i++)
31         if (s1[i] != s2[i])
32             return 0;
33     return s2[i] == 0;
34 }
35 
36 static struct pathelem *add_entry(struct pathelem *root, const char *name,
37                                   unsigned type);
38 
39 static struct pathelem *new_entry(const char *root,
40                                   struct pathelem *parent,
41                                   const char *name)
42 {
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;
48 }
49 
50 #define streq(a,b) (strcmp((a), (b)) == 0)
51 
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
61 
62 static struct pathelem *add_dir_maybe(struct pathelem *path)
63 {
64     DIR *dir;
65 
66     if ((dir = opendir(path->pathname)) != NULL) {
67         struct dirent *dirent;
68 
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));
72             }
73         }
74         closedir(dir);
75     }
76     return path;
77 }
78 
79 static struct pathelem *add_entry(struct pathelem *root, const char *name,
80                                   unsigned type)
81 {
82     struct pathelem **e;
83 
84     root->num_entries++;
85 
86     root = g_realloc(root, sizeof(*root)
87                    + sizeof(root->entries[0])*root->num_entries);
88     e = &root->entries[root->num_entries-1];
89 
90     *e = new_entry(root->pathname, root, name);
91     if (is_dir_maybe(type)) {
92         *e = add_dir_maybe(*e);
93     }
94 
95     return root;
96 }
97 
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)
100 {
101     unsigned int i;
102 
103     child->parent = parent;
104     for (i = 0; i < child->num_entries; i++)
105         set_parents(child->entries[i], child);
106 }
107 
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)
111 {
112     unsigned int i, namelen;
113 
114     name += strspn(name, "/");
115     namelen = strcspn(name, "/");
116 
117     if (namelen == 0)
118         return cursor->pathname;
119 
120     if (strneq(name, namelen, ".."))
121         return follow_path(cursor->parent, name + namelen);
122 
123     if (strneq(name, namelen, "."))
124         return follow_path(cursor, name + namelen);
125 
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);
129 
130     /* Not found */
131     return NULL;
132 }
133 
134 void init_paths(const char *prefix)
135 {
136     char pref_buf[PATH_MAX];
137 
138     if (prefix[0] == '\0' ||
139         !strcmp(prefix, "/"))
140         return;
141 
142     if (prefix[0] != '/') {
143         char *cwd = getcwd(NULL, 0);
144         size_t pref_buf_len = sizeof(pref_buf);
145 
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);
154 
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);
164     }
165 }
166 
167 /* Look for path in emulation dir, otherwise return name. */
168 const char *path(const char *name)
169 {
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;
174 
175     return follow_path(base, name) ?: name;
176 }
177