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.
14
#include "qemu-common.h"
18
/* Name of this, eg. lib */
20
/* Full path name, eg. /usr/gnemul/x86-linux/lib. */
22
struct pathelem *parent;
24
unsigned int num_entries;
25
struct pathelem *entries[0];
28
static struct pathelem *base;
30
/* First N chars of S1 match S2, and S2 is N chars long. */
31
static int strneq(const char *s1, unsigned int n, const char *s2)
35
for (i = 0; i < n; i++)
41
static struct pathelem *add_entry(struct pathelem *root, const char *name,
44
static struct pathelem *new_entry(const char *root,
45
struct pathelem *parent,
48
struct pathelem *new = malloc(sizeof(*new));
49
new->name = strdup(name);
50
if (asprintf(&new->pathname, "%s/%s", root, name) == -1) {
51
printf("Cannot allocate memory\n");
58
#define streq(a,b) (strcmp((a), (b)) == 0)
60
/* Not all systems provide this feature */
61
#if defined(DT_DIR) && defined(DT_UNKNOWN)
62
# define dirent_type(dirent) ((dirent)->d_type)
63
# define is_dir_maybe(type) ((type) == DT_DIR || (type) == DT_UNKNOWN)
65
# define dirent_type(dirent) (1)
66
# define is_dir_maybe(type) (type)
69
static struct pathelem *add_dir_maybe(struct pathelem *path)
73
if ((dir = opendir(path->pathname)) != NULL) {
74
struct dirent *dirent;
76
while ((dirent = readdir(dir)) != NULL) {
77
if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")){
78
path = add_entry(path, dirent->d_name, dirent_type(dirent));
86
static struct pathelem *add_entry(struct pathelem *root, const char *name,
93
root = realloc(root, sizeof(*root)
94
+ sizeof(root->entries[0])*root->num_entries);
95
e = &root->entries[root->num_entries-1];
97
*e = new_entry(root->pathname, root, name);
98
if (is_dir_maybe(type)) {
99
*e = add_dir_maybe(*e);
105
/* This needs to be done after tree is stabilized (ie. no more reallocs!). */
106
static void set_parents(struct pathelem *child, struct pathelem *parent)
110
child->parent = parent;
111
for (i = 0; i < child->num_entries; i++)
112
set_parents(child->entries[i], child);
115
/* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
117
follow_path(const struct pathelem *cursor, const char *name)
119
unsigned int i, namelen;
121
name += strspn(name, "/");
122
namelen = strcspn(name, "/");
125
return cursor->pathname;
127
if (strneq(name, namelen, ".."))
128
return follow_path(cursor->parent, name + namelen);
130
if (strneq(name, namelen, "."))
131
return follow_path(cursor, name + namelen);
133
for (i = 0; i < cursor->num_entries; i++)
134
if (strneq(name, namelen, cursor->entries[i]->name))
135
return follow_path(cursor->entries[i], name + namelen);
141
void init_paths(const char *prefix)
143
char pref_buf[PATH_MAX];
145
if (prefix[0] == '\0' ||
146
!strcmp(prefix, "/"))
149
if (prefix[0] != '/') {
150
char *cwd = getcwd(NULL, 0);
151
size_t pref_buf_len = sizeof(pref_buf);
155
pstrcpy(pref_buf, sizeof(pref_buf), cwd);
156
pstrcat(pref_buf, pref_buf_len, "/");
157
pstrcat(pref_buf, pref_buf_len, prefix);
160
pstrcpy(pref_buf, sizeof(pref_buf), prefix + 1);
162
base = new_entry("", NULL, pref_buf);
163
base = add_dir_maybe(base);
164
if (base->num_entries == 0) {
168
set_parents(base, base);
172
/* Look for path in emulation dir, otherwise return name. */
173
const char *path(const char *name)
175
/* Only do absolute paths: quick and dirty, but should mostly be OK.
176
Could do relative by tracking cwd. */
177
if (!base || !name || name[0] != '/')
180
return follow_path(base, name) ?: name;