blob: 5479f76c6d831c0eb28b23952c5e5d7a0fd26135 [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>
blueswir184778502008-10-26 20:33:16 +00009#include "qemu-common.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020010#include "qemu/cutils.h"
11#include "qemu/path.h"
blueswir184778502008-10-26 20:33:16 +000012
13struct pathelem
14{
15 /* Name of this, eg. lib */
16 char *name;
17 /* Full path name, eg. /usr/gnemul/x86-linux/lib. */
18 char *pathname;
19 struct pathelem *parent;
20 /* Children */
21 unsigned int num_entries;
22 struct pathelem *entries[0];
23};
24
25static struct pathelem *base;
26
27/* First N chars of S1 match S2, and S2 is N chars long. */
28static int strneq(const char *s1, unsigned int n, const char *s2)
29{
30 unsigned int i;
31
32 for (i = 0; i < n; i++)
33 if (s1[i] != s2[i])
34 return 0;
35 return s2[i] == 0;
36}
37
Mike Frysinger2296f192011-02-07 01:05:56 -050038static struct pathelem *add_entry(struct pathelem *root, const char *name,
Stefan Weilddd23632013-10-02 22:40:29 +020039 unsigned type);
blueswir184778502008-10-26 20:33:16 +000040
41static struct pathelem *new_entry(const char *root,
42 struct pathelem *parent,
43 const char *name)
44{
zhanghailiangbc5008a2014-08-18 15:49:22 +080045 struct pathelem *new = g_malloc(sizeof(*new));
46 new->name = g_strdup(name);
Stefan Weile4ada482013-01-16 18:37:23 +010047 new->pathname = g_strdup_printf("%s/%s", root, name);
blueswir184778502008-10-26 20:33:16 +000048 new->num_entries = 0;
49 return new;
50}
51
52#define streq(a,b) (strcmp((a), (b)) == 0)
53
Mike Frysinger2296f192011-02-07 01:05:56 -050054/* Not all systems provide this feature */
Richard Henderson338d80d2013-01-04 16:39:33 -080055#if defined(DT_DIR) && defined(DT_UNKNOWN) && defined(DT_LNK)
Mike Frysinger2296f192011-02-07 01:05:56 -050056# define dirent_type(dirent) ((dirent)->d_type)
Richard Henderson338d80d2013-01-04 16:39:33 -080057# define is_dir_maybe(type) \
58 ((type) == DT_DIR || (type) == DT_UNKNOWN || (type) == DT_LNK)
Mike Frysinger2296f192011-02-07 01:05:56 -050059#else
60# define dirent_type(dirent) (1)
61# define is_dir_maybe(type) (type)
62#endif
63
blueswir184778502008-10-26 20:33:16 +000064static struct pathelem *add_dir_maybe(struct pathelem *path)
65{
66 DIR *dir;
67
68 if ((dir = opendir(path->pathname)) != NULL) {
69 struct dirent *dirent;
70
71 while ((dirent = readdir(dir)) != NULL) {
72 if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")){
Mike Frysinger2296f192011-02-07 01:05:56 -050073 path = add_entry(path, dirent->d_name, dirent_type(dirent));
blueswir184778502008-10-26 20:33:16 +000074 }
75 }
76 closedir(dir);
77 }
78 return path;
79}
80
Mike Frysinger2296f192011-02-07 01:05:56 -050081static struct pathelem *add_entry(struct pathelem *root, const char *name,
Stefan Weilddd23632013-10-02 22:40:29 +020082 unsigned type)
blueswir184778502008-10-26 20:33:16 +000083{
Mike Frysinger2296f192011-02-07 01:05:56 -050084 struct pathelem **e;
85
blueswir184778502008-10-26 20:33:16 +000086 root->num_entries++;
87
zhanghailiangbc5008a2014-08-18 15:49:22 +080088 root = g_realloc(root, sizeof(*root)
blueswir184778502008-10-26 20:33:16 +000089 + sizeof(root->entries[0])*root->num_entries);
Mike Frysinger2296f192011-02-07 01:05:56 -050090 e = &root->entries[root->num_entries-1];
blueswir184778502008-10-26 20:33:16 +000091
Mike Frysinger2296f192011-02-07 01:05:56 -050092 *e = new_entry(root->pathname, root, name);
93 if (is_dir_maybe(type)) {
94 *e = add_dir_maybe(*e);
95 }
96
blueswir184778502008-10-26 20:33:16 +000097 return root;
98}
99
100/* This needs to be done after tree is stabilized (ie. no more reallocs!). */
101static void set_parents(struct pathelem *child, struct pathelem *parent)
102{
103 unsigned int i;
104
105 child->parent = parent;
106 for (i = 0; i < child->num_entries; i++)
107 set_parents(child->entries[i], child);
108}
109
110/* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
111static const char *
112follow_path(const struct pathelem *cursor, const char *name)
113{
114 unsigned int i, namelen;
115
116 name += strspn(name, "/");
117 namelen = strcspn(name, "/");
118
119 if (namelen == 0)
120 return cursor->pathname;
121
122 if (strneq(name, namelen, ".."))
123 return follow_path(cursor->parent, name + namelen);
124
125 if (strneq(name, namelen, "."))
126 return follow_path(cursor, name + namelen);
127
128 for (i = 0; i < cursor->num_entries; i++)
129 if (strneq(name, namelen, cursor->entries[i]->name))
130 return follow_path(cursor->entries[i], name + namelen);
131
132 /* Not found */
133 return NULL;
134}
135
136void init_paths(const char *prefix)
137{
138 char pref_buf[PATH_MAX];
139
140 if (prefix[0] == '\0' ||
141 !strcmp(prefix, "/"))
142 return;
143
144 if (prefix[0] != '/') {
145 char *cwd = getcwd(NULL, 0);
146 size_t pref_buf_len = sizeof(pref_buf);
147
148 if (!cwd)
149 abort();
150 pstrcpy(pref_buf, sizeof(pref_buf), cwd);
151 pstrcat(pref_buf, pref_buf_len, "/");
152 pstrcat(pref_buf, pref_buf_len, prefix);
153 free(cwd);
154 } else
155 pstrcpy(pref_buf, sizeof(pref_buf), prefix + 1);
156
157 base = new_entry("", NULL, pref_buf);
158 base = add_dir_maybe(base);
159 if (base->num_entries == 0) {
Kirill Batuzov00a9cac2014-04-10 18:07:57 +0400160 g_free(base->pathname);
zhanghailiangbc5008a2014-08-18 15:49:22 +0800161 g_free(base->name);
162 g_free(base);
blueswir184778502008-10-26 20:33:16 +0000163 base = NULL;
164 } else {
165 set_parents(base, base);
166 }
167}
168
169/* Look for path in emulation dir, otherwise return name. */
170const char *path(const char *name)
171{
172 /* Only do absolute paths: quick and dirty, but should mostly be OK.
173 Could do relative by tracking cwd. */
Blue Swirl37022082009-08-15 07:51:59 +0000174 if (!base || !name || name[0] != '/')
blueswir184778502008-10-26 20:33:16 +0000175 return name;
176
177 return follow_path(base, name) ?: name;
178}