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/cutils.h"
10 #include "qemu/path.h"
14 /* Name of this, eg. lib */
16 /* Full path name, eg. /usr/gnemul/x86-linux/lib. */
18 struct pathelem
*parent
;
20 unsigned int num_entries
;
21 struct pathelem
*entries
[0];
24 static struct pathelem
*base
;
26 /* First N chars of S1 match S2, and S2 is N chars long. */
27 static int strneq(const char *s1
, unsigned int n
, const char *s2
)
31 for (i
= 0; i
< n
; i
++)
37 static struct pathelem
*add_entry(struct pathelem
*root
, const char *name
,
40 static struct pathelem
*new_entry(const char *root
,
41 struct pathelem
*parent
,
44 struct pathelem
*new = g_malloc(sizeof(*new));
45 new->name
= g_strdup(name
);
46 new->pathname
= g_strdup_printf("%s/%s", root
, name
);
51 #define streq(a,b) (strcmp((a), (b)) == 0)
53 /* Not all systems provide this feature */
54 #if defined(DT_DIR) && defined(DT_UNKNOWN) && defined(DT_LNK)
55 # define dirent_type(dirent) ((dirent)->d_type)
56 # define is_dir_maybe(type) \
57 ((type) == DT_DIR || (type) == DT_UNKNOWN || (type) == DT_LNK)
59 # define dirent_type(dirent) (1)
60 # define is_dir_maybe(type) (type)
63 static struct pathelem
*add_dir_maybe(struct pathelem
*path
)
67 if ((dir
= opendir(path
->pathname
)) != NULL
) {
68 struct dirent
*dirent
;
70 while ((dirent
= readdir(dir
)) != NULL
) {
71 if (!streq(dirent
->d_name
,".") && !streq(dirent
->d_name
,"..")){
72 path
= add_entry(path
, dirent
->d_name
, dirent_type(dirent
));
80 static struct pathelem
*add_entry(struct pathelem
*root
, const char *name
,
87 root
= g_realloc(root
, sizeof(*root
)
88 + sizeof(root
->entries
[0])*root
->num_entries
);
89 e
= &root
->entries
[root
->num_entries
-1];
91 *e
= new_entry(root
->pathname
, root
, name
);
92 if (is_dir_maybe(type
)) {
93 *e
= add_dir_maybe(*e
);
99 /* This needs to be done after tree is stabilized (ie. no more reallocs!). */
100 static void set_parents(struct pathelem
*child
, struct pathelem
*parent
)
104 child
->parent
= parent
;
105 for (i
= 0; i
< child
->num_entries
; i
++)
106 set_parents(child
->entries
[i
], child
);
109 /* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
111 follow_path(const struct pathelem
*cursor
, const char *name
)
113 unsigned int i
, namelen
;
115 name
+= strspn(name
, "/");
116 namelen
= strcspn(name
, "/");
119 return cursor
->pathname
;
121 if (strneq(name
, namelen
, ".."))
122 return follow_path(cursor
->parent
, name
+ namelen
);
124 if (strneq(name
, namelen
, "."))
125 return follow_path(cursor
, name
+ namelen
);
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
);
135 void init_paths(const char *prefix
)
137 char pref_buf
[PATH_MAX
];
139 if (prefix
[0] == '\0' ||
140 !strcmp(prefix
, "/"))
143 if (prefix
[0] != '/') {
144 char *cwd
= getcwd(NULL
, 0);
145 size_t pref_buf_len
= sizeof(pref_buf
);
149 pstrcpy(pref_buf
, sizeof(pref_buf
), cwd
);
150 pstrcat(pref_buf
, pref_buf_len
, "/");
151 pstrcat(pref_buf
, pref_buf_len
, prefix
);
154 pstrcpy(pref_buf
, sizeof(pref_buf
), prefix
+ 1);
156 base
= new_entry("", NULL
, pref_buf
);
157 base
= add_dir_maybe(base
);
158 if (base
->num_entries
== 0) {
159 g_free(base
->pathname
);
164 set_parents(base
, base
);
168 /* Look for path in emulation dir, otherwise return name. */
169 const char *path(const char *name
)
171 /* Only do absolute paths: quick and dirty, but should mostly be OK.
172 Could do relative by tracking cwd. */
173 if (!base
|| !name
|| name
[0] != '/')
176 return follow_path(base
, name
) ?: name
;