blob: 7f9fc272fbb3f5aee5a2a781ebb989bf208a8023 [file] [log] [blame]
blueswir184778502008-10-26 20:33:16 +00001/* 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*/
Peter Maydellaafd7582016-01-29 17:49:55 +00006#include "qemu/osdep.h"
blueswir184778502008-10-26 20:33:16 +00007#include <sys/param.h>
8#include <dirent.h>
Veronia Bahaaf348b6d2016-03-20 19:16:19 +02009#include "qemu/cutils.h"
10#include "qemu/path.h"
blueswir184778502008-10-26 20:33:16 +000011
12struct pathelem
13{
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];
22};
23
24static struct pathelem *base;
25
26/* First N chars of S1 match S2, and S2 is N chars long. */
27static int strneq(const char *s1, unsigned int n, const char *s2)
28{
29 unsigned int i;
30
31 for (i = 0; i < n; i++)
32 if (s1[i] != s2[i])
33 return 0;
34 return s2[i] == 0;
35}
36
Mike Frysinger2296f192011-02-07 01:05:56 -050037static struct pathelem *add_entry(struct pathelem *root, const char *name,
Stefan Weilddd23632013-10-02 22:40:29 +020038 unsigned type);
blueswir184778502008-10-26 20:33:16 +000039
40static struct pathelem *new_entry(const char *root,
41 struct pathelem *parent,
42 const char *name)
43{
zhanghailiangbc5008a2014-08-18 15:49:22 +080044 struct pathelem *new = g_malloc(sizeof(*new));
45 new->name = g_strdup(name);
Stefan Weile4ada482013-01-16 18:37:23 +010046 new->pathname = g_strdup_printf("%s/%s", root, name);
blueswir184778502008-10-26 20:33:16 +000047 new->num_entries = 0;
48 return new;
49}
50
51#define streq(a,b) (strcmp((a), (b)) == 0)
52
Mike Frysinger2296f192011-02-07 01:05:56 -050053/* Not all systems provide this feature */
Richard Henderson338d80d2013-01-04 16:39:33 -080054#if defined(DT_DIR) && defined(DT_UNKNOWN) && defined(DT_LNK)
Mike Frysinger2296f192011-02-07 01:05:56 -050055# define dirent_type(dirent) ((dirent)->d_type)
Richard Henderson338d80d2013-01-04 16:39:33 -080056# define is_dir_maybe(type) \
57 ((type) == DT_DIR || (type) == DT_UNKNOWN || (type) == DT_LNK)
Mike Frysinger2296f192011-02-07 01:05:56 -050058#else
59# define dirent_type(dirent) (1)
60# define is_dir_maybe(type) (type)
61#endif
62
blueswir184778502008-10-26 20:33:16 +000063static struct pathelem *add_dir_maybe(struct pathelem *path)
64{
65 DIR *dir;
66
67 if ((dir = opendir(path->pathname)) != NULL) {
68 struct dirent *dirent;
69
70 while ((dirent = readdir(dir)) != NULL) {
71 if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")){
Mike Frysinger2296f192011-02-07 01:05:56 -050072 path = add_entry(path, dirent->d_name, dirent_type(dirent));
blueswir184778502008-10-26 20:33:16 +000073 }
74 }
75 closedir(dir);
76 }
77 return path;
78}
79
Mike Frysinger2296f192011-02-07 01:05:56 -050080static struct pathelem *add_entry(struct pathelem *root, const char *name,
Stefan Weilddd23632013-10-02 22:40:29 +020081 unsigned type)
blueswir184778502008-10-26 20:33:16 +000082{
Mike Frysinger2296f192011-02-07 01:05:56 -050083 struct pathelem **e;
84
blueswir184778502008-10-26 20:33:16 +000085 root->num_entries++;
86
zhanghailiangbc5008a2014-08-18 15:49:22 +080087 root = g_realloc(root, sizeof(*root)
blueswir184778502008-10-26 20:33:16 +000088 + sizeof(root->entries[0])*root->num_entries);
Mike Frysinger2296f192011-02-07 01:05:56 -050089 e = &root->entries[root->num_entries-1];
blueswir184778502008-10-26 20:33:16 +000090
Mike Frysinger2296f192011-02-07 01:05:56 -050091 *e = new_entry(root->pathname, root, name);
92 if (is_dir_maybe(type)) {
93 *e = add_dir_maybe(*e);
94 }
95
blueswir184778502008-10-26 20:33:16 +000096 return root;
97}
98
99/* This needs to be done after tree is stabilized (ie. no more reallocs!). */
100static void set_parents(struct pathelem *child, struct pathelem *parent)
101{
102 unsigned int i;
103
104 child->parent = parent;
105 for (i = 0; i < child->num_entries; i++)
106 set_parents(child->entries[i], child);
107}
108
109/* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
110static const char *
111follow_path(const struct pathelem *cursor, const char *name)
112{
113 unsigned int i, namelen;
114
115 name += strspn(name, "/");
116 namelen = strcspn(name, "/");
117
118 if (namelen == 0)
119 return cursor->pathname;
120
121 if (strneq(name, namelen, ".."))
122 return follow_path(cursor->parent, name + namelen);
123
124 if (strneq(name, namelen, "."))
125 return follow_path(cursor, name + namelen);
126
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);
130
131 /* Not found */
132 return NULL;
133}
134
135void init_paths(const char *prefix)
136{
137 char pref_buf[PATH_MAX];
138
139 if (prefix[0] == '\0' ||
140 !strcmp(prefix, "/"))
141 return;
142
143 if (prefix[0] != '/') {
144 char *cwd = getcwd(NULL, 0);
145 size_t pref_buf_len = sizeof(pref_buf);
146
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);
155
156 base = new_entry("", NULL, pref_buf);
157 base = add_dir_maybe(base);
158 if (base->num_entries == 0) {
Kirill Batuzov00a9cac2014-04-10 18:07:57 +0400159 g_free(base->pathname);
zhanghailiangbc5008a2014-08-18 15:49:22 +0800160 g_free(base->name);
161 g_free(base);
blueswir184778502008-10-26 20:33:16 +0000162 base = NULL;
163 } else {
164 set_parents(base, base);
165 }
166}
167
168/* Look for path in emulation dir, otherwise return name. */
169const char *path(const char *name)
170{
171 /* Only do absolute paths: quick and dirty, but should mostly be OK.
172 Could do relative by tracking cwd. */
Blue Swirl37022082009-08-15 07:51:59 +0000173 if (!base || !name || name[0] != '/')
blueswir184778502008-10-26 20:33:16 +0000174 return name;
175
176 return follow_path(base, name) ?: name;
177}