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.
6 #include "qemu/osdep.h"
9 #include "qemu-common.h"
13 /* Name of this, eg. lib */
15 /* Full path name, eg. /usr/gnemul/x86-linux/lib. */
17 struct pathelem
*parent
;
19 unsigned int num_entries
;
20 struct pathelem
*entries
[0];
23 static struct pathelem
*base
;
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
)
30 for (i
= 0; i
< n
; i
++)
36 static struct pathelem
*add_entry(struct pathelem
*root
, const char *name
,
39 static struct pathelem
*new_entry(const char *root
,
40 struct pathelem
*parent
,
43 struct pathelem
*new = g_malloc(sizeof(*new));
44 new->name
= g_strdup(name
);
45 new->pathname
= g_strdup_printf("%s/%s", root
, name
);
50 #define streq(a,b) (strcmp((a), (b)) == 0)
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)
58 # define dirent_type(dirent) (1)
59 # define is_dir_maybe(type) (type)
62 static struct pathelem
*add_dir_maybe(struct pathelem
*path
)
66 if ((dir
= opendir(path
->pathname
)) != NULL
) {
67 struct dirent
*dirent
;
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
));
79 static struct pathelem
*add_entry(struct pathelem
*root
, const char *name
,
86 root
= g_realloc(root
, sizeof(*root
)
87 + sizeof(root
->entries
[0])*root
->num_entries
);
88 e
= &root
->entries
[root
->num_entries
-1];
90 *e
= new_entry(root
->pathname
, root
, name
);
91 if (is_dir_maybe(type
)) {
92 *e
= add_dir_maybe(*e
);
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
)
103 child
->parent
= parent
;
104 for (i
= 0; i
< child
->num_entries
; i
++)
105 set_parents(child
->entries
[i
], child
);
108 /* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
110 follow_path(const struct pathelem
*cursor
, const char *name
)
112 unsigned int i
, namelen
;
114 name
+= strspn(name
, "/");
115 namelen
= strcspn(name
, "/");
118 return cursor
->pathname
;
120 if (strneq(name
, namelen
, ".."))
121 return follow_path(cursor
->parent
, name
+ namelen
);
123 if (strneq(name
, namelen
, "."))
124 return follow_path(cursor
, name
+ namelen
);
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
);
134 void init_paths(const char *prefix
)
136 char pref_buf
[PATH_MAX
];
138 if (prefix
[0] == '\0' ||
139 !strcmp(prefix
, "/"))
142 if (prefix
[0] != '/') {
143 char *cwd
= getcwd(NULL
, 0);
144 size_t pref_buf_len
= sizeof(pref_buf
);
148 pstrcpy(pref_buf
, sizeof(pref_buf
), cwd
);
149 pstrcat(pref_buf
, pref_buf_len
, "/");
150 pstrcat(pref_buf
, pref_buf_len
, prefix
);
153 pstrcpy(pref_buf
, sizeof(pref_buf
), prefix
+ 1);
155 base
= new_entry("", NULL
, pref_buf
);
156 base
= add_dir_maybe(base
);
157 if (base
->num_entries
== 0) {
158 g_free(base
->pathname
);
163 set_parents(base
, base
);
167 /* Look for path in emulation dir, otherwise return name. */
168 const char *path(const char *name
)
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] != '/')
175 return follow_path(base
, name
) ?: name
;