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) && defined(DT_LNK)
62 # define dirent_type(dirent) ((dirent)->d_type)
63 # define is_dir_maybe(type) \
64 ((type) == DT_DIR || (type) == DT_UNKNOWN || (type) == DT_LNK)
66 # define dirent_type(dirent) (1)
67 # define is_dir_maybe(type) (type)
70 static struct pathelem
*add_dir_maybe(struct pathelem
*path
)
74 if ((dir
= opendir(path
->pathname
)) != NULL
) {
75 struct dirent
*dirent
;
77 while ((dirent
= readdir(dir
)) != NULL
) {
78 if (!streq(dirent
->d_name
,".") && !streq(dirent
->d_name
,"..")){
79 path
= add_entry(path
, dirent
->d_name
, dirent_type(dirent
));
87 static struct pathelem
*add_entry(struct pathelem
*root
, const char *name
,
94 root
= realloc(root
, sizeof(*root
)
95 + sizeof(root
->entries
[0])*root
->num_entries
);
96 e
= &root
->entries
[root
->num_entries
-1];
98 *e
= new_entry(root
->pathname
, root
, name
);
99 if (is_dir_maybe(type
)) {
100 *e
= add_dir_maybe(*e
);
106 /* This needs to be done after tree is stabilized (ie. no more reallocs!). */
107 static void set_parents(struct pathelem
*child
, struct pathelem
*parent
)
111 child
->parent
= parent
;
112 for (i
= 0; i
< child
->num_entries
; i
++)
113 set_parents(child
->entries
[i
], child
);
116 /* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
118 follow_path(const struct pathelem
*cursor
, const char *name
)
120 unsigned int i
, namelen
;
122 name
+= strspn(name
, "/");
123 namelen
= strcspn(name
, "/");
126 return cursor
->pathname
;
128 if (strneq(name
, namelen
, ".."))
129 return follow_path(cursor
->parent
, name
+ namelen
);
131 if (strneq(name
, namelen
, "."))
132 return follow_path(cursor
, name
+ namelen
);
134 for (i
= 0; i
< cursor
->num_entries
; i
++)
135 if (strneq(name
, namelen
, cursor
->entries
[i
]->name
))
136 return follow_path(cursor
->entries
[i
], name
+ namelen
);
142 void init_paths(const char *prefix
)
144 char pref_buf
[PATH_MAX
];
146 if (prefix
[0] == '\0' ||
147 !strcmp(prefix
, "/"))
150 if (prefix
[0] != '/') {
151 char *cwd
= getcwd(NULL
, 0);
152 size_t pref_buf_len
= sizeof(pref_buf
);
156 pstrcpy(pref_buf
, sizeof(pref_buf
), cwd
);
157 pstrcat(pref_buf
, pref_buf_len
, "/");
158 pstrcat(pref_buf
, pref_buf_len
, prefix
);
161 pstrcpy(pref_buf
, sizeof(pref_buf
), prefix
+ 1);
163 base
= new_entry("", NULL
, pref_buf
);
164 base
= add_dir_maybe(base
);
165 if (base
->num_entries
== 0) {
169 set_parents(base
, base
);
173 /* Look for path in emulation dir, otherwise return name. */
174 const char *path(const char *name
)
176 /* Only do absolute paths: quick and dirty, but should mostly be OK.
177 Could do relative by tracking cwd. */
178 if (!base
|| !name
|| name
[0] != '/')
181 return follow_path(base
, name
) ?: name
;