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"
10 #include "qemu/cutils.h"
11 #include "qemu/path.h"
15 /* Name of this, eg. lib */
17 /* Full path name, eg. /usr/gnemul/x86-linux/lib. */
19 struct pathelem
*parent
;
21 unsigned int num_entries
;
22 struct pathelem
*entries
[0];
25 static struct pathelem
*base
;
27 /* First N chars of S1 match S2, and S2 is N chars long. */
28 static int strneq(const char *s1
, unsigned int n
, const char *s2
)
32 for (i
= 0; i
< n
; i
++)
38 static struct pathelem
*add_entry(struct pathelem
*root
, const char *name
,
41 static struct pathelem
*new_entry(const char *root
,
42 struct pathelem
*parent
,
45 struct pathelem
*new = g_malloc(sizeof(*new));
46 new->name
= g_strdup(name
);
47 new->pathname
= g_strdup_printf("%s/%s", root
, name
);
52 #define streq(a,b) (strcmp((a), (b)) == 0)
54 /* Not all systems provide this feature */
55 #if defined(DT_DIR) && defined(DT_UNKNOWN) && defined(DT_LNK)
56 # define dirent_type(dirent) ((dirent)->d_type)
57 # define is_dir_maybe(type) \
58 ((type) == DT_DIR || (type) == DT_UNKNOWN || (type) == DT_LNK)
60 # define dirent_type(dirent) (1)
61 # define is_dir_maybe(type) (type)
64 static struct pathelem
*add_dir_maybe(struct pathelem
*path
)
68 if ((dir
= opendir(path
->pathname
)) != NULL
) {
69 struct dirent
*dirent
;
71 while ((dirent
= readdir(dir
)) != NULL
) {
72 if (!streq(dirent
->d_name
,".") && !streq(dirent
->d_name
,"..")){
73 path
= add_entry(path
, dirent
->d_name
, dirent_type(dirent
));
81 static struct pathelem
*add_entry(struct pathelem
*root
, const char *name
,
88 root
= g_realloc(root
, sizeof(*root
)
89 + sizeof(root
->entries
[0])*root
->num_entries
);
90 e
= &root
->entries
[root
->num_entries
-1];
92 *e
= new_entry(root
->pathname
, root
, name
);
93 if (is_dir_maybe(type
)) {
94 *e
= add_dir_maybe(*e
);
100 /* This needs to be done after tree is stabilized (ie. no more reallocs!). */
101 static void set_parents(struct pathelem
*child
, struct pathelem
*parent
)
105 child
->parent
= parent
;
106 for (i
= 0; i
< child
->num_entries
; i
++)
107 set_parents(child
->entries
[i
], child
);
110 /* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
112 follow_path(const struct pathelem
*cursor
, const char *name
)
114 unsigned int i
, namelen
;
116 name
+= strspn(name
, "/");
117 namelen
= strcspn(name
, "/");
120 return cursor
->pathname
;
122 if (strneq(name
, namelen
, ".."))
123 return follow_path(cursor
->parent
, name
+ namelen
);
125 if (strneq(name
, namelen
, "."))
126 return follow_path(cursor
, name
+ namelen
);
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
);
136 void init_paths(const char *prefix
)
138 char pref_buf
[PATH_MAX
];
140 if (prefix
[0] == '\0' ||
141 !strcmp(prefix
, "/"))
144 if (prefix
[0] != '/') {
145 char *cwd
= getcwd(NULL
, 0);
146 size_t pref_buf_len
= sizeof(pref_buf
);
150 pstrcpy(pref_buf
, sizeof(pref_buf
), cwd
);
151 pstrcat(pref_buf
, pref_buf_len
, "/");
152 pstrcat(pref_buf
, pref_buf_len
, prefix
);
155 pstrcpy(pref_buf
, sizeof(pref_buf
), prefix
+ 1);
157 base
= new_entry("", NULL
, pref_buf
);
158 base
= add_dir_maybe(base
);
159 if (base
->num_entries
== 0) {
160 g_free(base
->pathname
);
165 set_parents(base
, base
);
169 /* Look for path in emulation dir, otherwise return name. */
170 const char *path(const char *name
)
172 /* Only do absolute paths: quick and dirty, but should mostly be OK.
173 Could do relative by tracking cwd. */
174 if (!base
|| !name
|| name
[0] != '/')
177 return follow_path(base
, name
) ?: name
;